Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / sandbox / win / src / handle_table.cc
blob5ebcf99460b543d016673ddf024b918ecd9431ed
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sandbox/win/src/handle_table.h"
7 #include <algorithm>
8 #include <cstdlib>
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "sandbox/win/src/win_utils.h"
14 namespace {
16 bool CompareHandleEntries(const SYSTEM_HANDLE_INFORMATION& a,
17 const SYSTEM_HANDLE_INFORMATION& b) {
18 return a.ProcessId < b.ProcessId;
21 } // namespace
23 namespace sandbox {
25 const base::char16* HandleTable::kTypeProcess = L"Process";
26 const base::char16* HandleTable::kTypeThread = L"Thread";
27 const base::char16* HandleTable::kTypeFile = L"File";
28 const base::char16* HandleTable::kTypeDirectory = L"Directory";
29 const base::char16* HandleTable::kTypeKey = L"Key";
30 const base::char16* HandleTable::kTypeWindowStation = L"WindowStation";
31 const base::char16* HandleTable::kTypeDesktop = L"Desktop";
32 const base::char16* HandleTable::kTypeService = L"Service";
33 const base::char16* HandleTable::kTypeMutex = L"Mutex";
34 const base::char16* HandleTable::kTypeSemaphore = L"Semaphore";
35 const base::char16* HandleTable::kTypeEvent = L"Event";
36 const base::char16* HandleTable::kTypeTimer = L"Timer";
37 const base::char16* HandleTable::kTypeNamedPipe = L"NamedPipe";
38 const base::char16* HandleTable::kTypeJobObject = L"JobObject";
39 const base::char16* HandleTable::kTypeFileMap = L"FileMap";
40 const base::char16* HandleTable::kTypeAlpcPort = L"ALPC Port";
42 HandleTable::HandleTable() {
43 static NtQuerySystemInformation QuerySystemInformation = NULL;
44 if (!QuerySystemInformation)
45 ResolveNTFunctionPtr("NtQuerySystemInformation", &QuerySystemInformation);
47 ULONG size = 0x15000;
48 NTSTATUS result;
49 do {
50 handle_info_buffer_.resize(size);
51 result = QuerySystemInformation(SystemHandleInformation,
52 handle_info_internal(), size, &size);
53 } while (result == STATUS_INFO_LENGTH_MISMATCH);
55 // We failed, so make an empty table.
56 if (!NT_SUCCESS(result)) {
57 handle_info_buffer_.resize(0);
58 return;
61 // Sort it to make process lookups faster.
62 std::sort(handle_info_internal()->Information,
63 handle_info_internal()->Information +
64 handle_info_internal()->NumberOfHandles, CompareHandleEntries);
67 HandleTable::~HandleTable() {
70 HandleTable::Iterator HandleTable::HandlesForProcess(ULONG process_id) const {
71 SYSTEM_HANDLE_INFORMATION key;
72 key.ProcessId = static_cast<USHORT>(process_id);
74 const SYSTEM_HANDLE_INFORMATION* start = handle_info()->Information;
75 const SYSTEM_HANDLE_INFORMATION* finish =
76 &handle_info()->Information[handle_info()->NumberOfHandles];
78 start = std::lower_bound(start, finish, key, CompareHandleEntries);
79 if (start->ProcessId != process_id)
80 return Iterator(*this, finish, finish);
81 finish = std::upper_bound(start, finish, key, CompareHandleEntries);
82 return Iterator(*this, start, finish);
85 HandleTable::HandleEntry::HandleEntry(
86 const SYSTEM_HANDLE_INFORMATION* handle_info_entry)
87 : handle_entry_(handle_info_entry), last_entry_(0) {
90 HandleTable::HandleEntry::~HandleEntry() {
93 void HandleTable::HandleEntry::UpdateInfo(UpdateType flag) {
94 static NtQueryObject QueryObject = NULL;
95 if (!QueryObject)
96 ResolveNTFunctionPtr("NtQueryObject", &QueryObject);
98 NTSTATUS result;
100 // Always update the basic type info, but grab the names as needed.
101 if (needs_info_update()) {
102 handle_name_.clear();
103 type_name_.clear();
104 last_entry_ = handle_entry_;
106 // Most handle names are very short, so start small and reuse this buffer.
107 if (type_info_buffer_.empty())
108 type_info_buffer_.resize(sizeof(OBJECT_TYPE_INFORMATION)
109 + (32 * sizeof(wchar_t)));
110 ULONG size = static_cast<ULONG>(type_info_buffer_.size());
111 result = QueryObject(reinterpret_cast<HANDLE>(handle_entry_->Handle),
112 ObjectTypeInformation, type_info_internal(), size, &size);
113 while (result == STATUS_INFO_LENGTH_MISMATCH) {
114 type_info_buffer_.resize(size);
115 result = QueryObject(reinterpret_cast<HANDLE>(handle_entry_->Handle),
116 ObjectTypeInformation, type_info_internal(), size, &size);
119 if (!NT_SUCCESS(result)) {
120 type_info_buffer_.clear();
121 return;
125 // Don't bother copying out names until we ask for them, and then cache them.
126 switch (flag) {
127 case UPDATE_INFO_AND_NAME:
128 if (type_info_buffer_.size() && handle_name_.empty()) {
129 ULONG size = MAX_PATH;
130 scoped_ptr<UNICODE_STRING, base::FreeDeleter> name;
131 do {
132 name.reset(static_cast<UNICODE_STRING*>(malloc(size)));
133 DCHECK(name.get());
134 result = QueryObject(reinterpret_cast<HANDLE>(
135 handle_entry_->Handle), ObjectNameInformation, name.get(),
136 size, &size);
137 } while (result == STATUS_INFO_LENGTH_MISMATCH);
139 if (NT_SUCCESS(result)) {
140 handle_name_.assign(name->Buffer, name->Length / sizeof(wchar_t));
143 break;
145 case UPDATE_INFO_AND_TYPE_NAME:
146 if (!type_info_buffer_.empty() && type_info_internal()->Name.Buffer &&
147 type_name_.empty()) {
148 type_name_.assign(type_info_internal()->Name.Buffer,
149 type_info_internal()->Name.Length / sizeof(wchar_t));
151 break;
155 const OBJECT_TYPE_INFORMATION* HandleTable::HandleEntry::TypeInfo() {
156 UpdateInfo(UPDATE_INFO_ONLY);
157 return type_info_buffer_.empty() ? NULL : type_info_internal();
160 const base::string16& HandleTable::HandleEntry::Name() {
161 UpdateInfo(UPDATE_INFO_AND_NAME);
162 return handle_name_;
165 const base::string16& HandleTable::HandleEntry::Type() {
166 UpdateInfo(UPDATE_INFO_AND_TYPE_NAME);
167 return type_name_;
170 bool HandleTable::HandleEntry::IsType(const base::string16& type_string) {
171 UpdateInfo(UPDATE_INFO_ONLY);
172 if (type_info_buffer_.empty())
173 return false;
174 return type_string.compare(0,
175 type_info_internal()->Name.Length / sizeof(wchar_t),
176 type_info_internal()->Name.Buffer) == 0;
179 HandleTable::Iterator::Iterator(const HandleTable& table,
180 const SYSTEM_HANDLE_INFORMATION* start,
181 const SYSTEM_HANDLE_INFORMATION* end)
182 : table_(table), current_(start), end_(end) {
185 HandleTable::Iterator::Iterator(const Iterator& it)
186 : table_(it.table_), current_(it.current_.handle_entry_), end_(it.end_) {
189 } // namespace sandbox