security/sandbox/win/src/handle_table.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial