summaryrefslogtreecommitdiffstats
path: root/sandbox/src/handle_table.cc
blob: c7fcf0a92fdda88a0229ce721fc8c0083c497972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sandbox/src/handle_table.h"

#include <algorithm>
#include <cstdlib>

#include "base/memory/scoped_ptr.h"
#include "sandbox/src/win_utils.h"

namespace {

bool CompareHandleEntries(const SYSTEM_HANDLE_INFORMATION& a,
                          const SYSTEM_HANDLE_INFORMATION& b) {
  return a.ProcessId < b.ProcessId;
}

}  // namespace

namespace sandbox {

const char16* HandleTable::kTypeProcess = L"Process";
const char16* HandleTable::kTypeThread = L"Thread";
const char16* HandleTable::kTypeFile = L"File";
const char16* HandleTable::kTypeDirectory = L"Directory";
const char16* HandleTable::kTypeKey = L"Key";
const char16* HandleTable::kTypeWindowStation = L"WindowStation";
const char16* HandleTable::kTypeDesktop = L"Desktop";
const char16* HandleTable::kTypeService = L"Service";
const char16* HandleTable::kTypeMutex = L"Mutex";
const char16* HandleTable::kTypeSemaphore = L"Semaphore";
const char16* HandleTable::kTypeEvent = L"Event";
const char16* HandleTable::kTypeTimer = L"Timer";
const char16* HandleTable::kTypeNamedPipe = L"NamedPipe";
const char16* HandleTable::kTypeJobObject = L"JobObject";
const char16* HandleTable::kTypeFileMap = L"FileMap";
const char16* HandleTable::kTypeAlpcPort = L"ALPC Port";

HandleTable::HandleTable() {
  static NtQuerySystemInformation QuerySystemInformation = NULL;
  if (!QuerySystemInformation)
    ResolveNTFunctionPtr("NtQuerySystemInformation", &QuerySystemInformation);

  ULONG size = 0x15000;
  NTSTATUS result;
  do {
    handle_info_buffer_.resize(size);
    result = QuerySystemInformation(SystemHandleInformation,
        handle_info_internal(), size, &size);
  } while (result == STATUS_INFO_LENGTH_MISMATCH);

  // We failed, so make an empty table.
  if (!NT_SUCCESS(result)) {
    handle_info_buffer_.resize(0);
    return;
  }

  // Sort it to make process lookups faster.
  std::sort(handle_info_internal()->Information,
      handle_info_internal()->Information +
      handle_info_internal()->NumberOfHandles, CompareHandleEntries);
}

HandleTable::Iterator HandleTable::HandlesForProcess(ULONG process_id) const {
  SYSTEM_HANDLE_INFORMATION key;
  key.ProcessId = process_id;

  const SYSTEM_HANDLE_INFORMATION* start = handle_info()->Information;
  const SYSTEM_HANDLE_INFORMATION* finish =
      &handle_info()->Information[handle_info()->NumberOfHandles];

  start = std::lower_bound(start, finish, key, CompareHandleEntries);
  if (start->ProcessId != process_id)
    return Iterator(*this, finish, finish);
  finish = std::upper_bound(start, finish, key, CompareHandleEntries);
  return Iterator(*this, start, finish);
}

HandleTable::HandleEntry::HandleEntry(
    const SYSTEM_HANDLE_INFORMATION* handle_info_entry)
    : handle_entry_(handle_info_entry), last_entry_(0) {
}

void HandleTable::HandleEntry::UpdateInfo(UpdateType flag) {
  static NtQueryObject QueryObject = NULL;
  if (!QueryObject)
    ResolveNTFunctionPtr("NtQueryObject", &QueryObject);

  NTSTATUS result;

  // Always update the basic type info, but grab the names as needed.
  if (needs_info_update()) {
    handle_name_.clear();
    type_name_.clear();
    last_entry_ = handle_entry_;

    // Most handle names are very short, so start small and reuse this buffer.
    if (type_info_buffer_.empty())
      type_info_buffer_.resize(sizeof(OBJECT_TYPE_INFORMATION)
          + (32 * sizeof(wchar_t)));
    ULONG size = static_cast<ULONG>(type_info_buffer_.size());
    result = QueryObject(reinterpret_cast<HANDLE>(handle_entry_->Handle),
        ObjectTypeInformation, type_info_internal(), size, &size);
    while (result == STATUS_INFO_LENGTH_MISMATCH) {
      type_info_buffer_.resize(size);
      result = QueryObject(reinterpret_cast<HANDLE>(handle_entry_->Handle),
          ObjectTypeInformation, type_info_internal(), size, &size);
    }

    if (!NT_SUCCESS(result)) {
      type_info_buffer_.clear();
      return;
    }
  }

  // Don't bother copying out names until we ask for them, and then cache them.
  switch (flag) {
    case UPDATE_INFO_AND_NAME:
      if (type_info_buffer_.size() && handle_name_.empty()) {
        ULONG size = MAX_PATH;
        scoped_ptr<UNICODE_STRING> name;
        do {
          name.reset(reinterpret_cast<UNICODE_STRING*>(new BYTE[size]));
          result = QueryObject(reinterpret_cast<HANDLE>(
              handle_entry_->Handle), ObjectNameInformation, name.get(),
              size, &size);
        } while (result == STATUS_INFO_LENGTH_MISMATCH);

        if (NT_SUCCESS(result)) {
          handle_name_.assign(name->Buffer, name->Length / sizeof(wchar_t));
        }
      }
      break;

    case UPDATE_INFO_AND_TYPE_NAME:
      if (!type_info_buffer_.empty() && type_info_internal()->Name.Buffer &&
          type_name_.empty()) {
        type_name_.assign(type_info_internal()->Name.Buffer,
            type_info_internal()->Name.Length / sizeof(wchar_t));
      }
      break;
  }
}

const OBJECT_TYPE_INFORMATION* HandleTable::HandleEntry::TypeInfo() {
  UpdateInfo(UPDATE_INFO_ONLY);
  return type_info_buffer_.empty() ? NULL : type_info_internal();
}

const string16& HandleTable::HandleEntry::Name() {
  UpdateInfo(UPDATE_INFO_AND_NAME);
  return handle_name_;
}

const string16& HandleTable::HandleEntry::Type() {
  UpdateInfo(UPDATE_INFO_AND_TYPE_NAME);
  return type_name_;
}

bool HandleTable::HandleEntry::IsType(const string16& type_string) {
  UpdateInfo(UPDATE_INFO_ONLY);
  if (type_info_buffer_.empty())
    return false;
  return type_string.compare(0,
      type_info_internal()->Name.Length / sizeof(wchar_t),
      type_info_internal()->Name.Buffer) == 0;
}

HandleTable::Iterator::Iterator(const HandleTable& table,
                                const SYSTEM_HANDLE_INFORMATION* start,
                                const SYSTEM_HANDLE_INFORMATION* end)
    : table_(table), current_(start), end_(end) {
}

HandleTable::Iterator::Iterator(const Iterator& it)
    : table_(it.table_), current_(it.current_.handle_entry_), end_(it.end_) {
}

}  // namespace sandbox