Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sandbox/src/handle_table.h" | 5 #include "sandbox/src/handle_table.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstdlib> | 8 #include <cstdlib> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "sandbox/src/win_utils.h" | 11 #include "sandbox/src/win_utils.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 bool CompareHandleEntries(const SYSTEM_HANDLE_INFORMATION& a, | 15 bool CompareHandleEntries(const SYSTEM_HANDLE_INFORMATION& a, |
| 16 const SYSTEM_HANDLE_INFORMATION& b) { | 16 const SYSTEM_HANDLE_INFORMATION& b) { |
| 17 return a.ProcessId < b.ProcessId; | 17 return a.ProcessId < b.ProcessId; |
| 18 } | 18 } |
| 19 | 19 |
| 20 static NtQueryObject QueryObject = NULL; | |
| 21 | |
| 20 } // namespace | 22 } // namespace |
| 21 | 23 |
| 22 namespace sandbox { | 24 namespace sandbox { |
| 23 | 25 |
| 24 const char16* HandleTable::kTypeProcess = L"Process"; | 26 const char16* HandleTable::kTypeProcess = L"Process"; |
| 25 const char16* HandleTable::kTypeThread = L"Thread"; | 27 const char16* HandleTable::kTypeThread = L"Thread"; |
| 26 const char16* HandleTable::kTypeFile = L"File"; | 28 const char16* HandleTable::kTypeFile = L"File"; |
| 27 const char16* HandleTable::kTypeDirectory = L"Directory"; | 29 const char16* HandleTable::kTypeDirectory = L"Directory"; |
| 28 const char16* HandleTable::kTypeKey = L"Key"; | 30 const char16* HandleTable::kTypeKey = L"Key"; |
| 29 const char16* HandleTable::kTypeWindowStation = L"WindowStation"; | 31 const char16* HandleTable::kTypeWindowStation = L"WindowStation"; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 finish = std::upper_bound(start, finish, key, CompareHandleEntries); | 79 finish = std::upper_bound(start, finish, key, CompareHandleEntries); |
| 78 return Iterator(*this, start, finish); | 80 return Iterator(*this, start, finish); |
| 79 } | 81 } |
| 80 | 82 |
| 81 HandleTable::HandleEntry::HandleEntry( | 83 HandleTable::HandleEntry::HandleEntry( |
| 82 const SYSTEM_HANDLE_INFORMATION* handle_info_entry) | 84 const SYSTEM_HANDLE_INFORMATION* handle_info_entry) |
| 83 : handle_entry_(handle_info_entry), last_entry_(0) { | 85 : handle_entry_(handle_info_entry), last_entry_(0) { |
| 84 } | 86 } |
| 85 | 87 |
| 86 void HandleTable::HandleEntry::UpdateInfo(UpdateType flag) { | 88 void HandleTable::HandleEntry::UpdateInfo(UpdateType flag) { |
| 87 static NtQueryObject QueryObject = NULL; | |
| 88 if (!QueryObject) | 89 if (!QueryObject) |
| 89 ResolveNTFunctionPtr("NtQueryObject", &QueryObject); | 90 ResolveNTFunctionPtr("NtQueryObject", &QueryObject); |
| 90 | 91 |
| 91 NTSTATUS result; | 92 NTSTATUS result; |
| 92 | 93 |
| 93 // Always update the basic type info, but grab the names as needed. | 94 // Always update the basic type info, but grab the names as needed. |
| 94 if (needs_info_update()) { | 95 if (needs_info_update()) { |
| 95 handle_name_.clear(); | 96 handle_name_.clear(); |
| 96 type_name_.clear(); | 97 type_name_.clear(); |
| 97 last_entry_ = handle_entry_; | 98 last_entry_ = handle_entry_; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 112 if (!NT_SUCCESS(result)) { | 113 if (!NT_SUCCESS(result)) { |
| 113 type_info_buffer_.clear(); | 114 type_info_buffer_.clear(); |
| 114 return; | 115 return; |
| 115 } | 116 } |
| 116 } | 117 } |
| 117 | 118 |
| 118 // Don't bother copying out names until we ask for them, and then cache them. | 119 // Don't bother copying out names until we ask for them, and then cache them. |
| 119 switch (flag) { | 120 switch (flag) { |
| 120 case UPDATE_INFO_AND_NAME: | 121 case UPDATE_INFO_AND_NAME: |
| 121 if (type_info_buffer_.size() && handle_name_.empty()) { | 122 if (type_info_buffer_.size() && handle_name_.empty()) { |
| 122 ULONG size = MAX_PATH; | 123 GetHandleName(reinterpret_cast<HANDLE>(handle_entry_->Handle), |
| 123 scoped_ptr<UNICODE_STRING> name; | 124 &handle_name_); |
| 124 do { | |
| 125 name.reset(reinterpret_cast<UNICODE_STRING*>(new BYTE[size])); | |
| 126 result = QueryObject(reinterpret_cast<HANDLE>( | |
| 127 handle_entry_->Handle), ObjectNameInformation, name.get(), | |
| 128 size, &size); | |
| 129 } while (result == STATUS_INFO_LENGTH_MISMATCH); | |
| 130 | |
| 131 if (NT_SUCCESS(result)) { | |
| 132 handle_name_.assign(name->Buffer, name->Length / sizeof(wchar_t)); | |
| 133 } | |
| 134 } | 125 } |
| 135 break; | 126 break; |
| 136 | 127 |
| 137 case UPDATE_INFO_AND_TYPE_NAME: | 128 case UPDATE_INFO_AND_TYPE_NAME: |
| 138 if (!type_info_buffer_.empty() && type_info_internal()->Name.Buffer && | 129 if (!type_info_buffer_.empty() && type_info_internal()->Name.Buffer && |
| 139 type_name_.empty()) { | 130 type_name_.empty()) { |
| 140 type_name_.assign(type_info_internal()->Name.Buffer, | 131 type_name_.assign(type_info_internal()->Name.Buffer, |
| 141 type_info_internal()->Name.Length / sizeof(wchar_t)); | 132 type_info_internal()->Name.Length / sizeof(wchar_t)); |
| 142 } | 133 } |
| 143 break; | 134 break; |
| 144 } | 135 } |
| 145 } | 136 } |
| 146 | 137 |
| 138 // Returns the object manager's name associated with a handle | |
| 139 NTSTATUS GetHandleName(HANDLE handle, string16* handle_name) { | |
| 140 if (!QueryObject) | |
| 141 ResolveNTFunctionPtr("NtQueryObject", &QueryObject); | |
| 142 | |
| 143 ULONG size = MAX_PATH; | |
| 144 scoped_ptr<UNICODE_STRING> name; | |
| 145 NTSTATUS result; | |
| 146 | |
| 147 do { | |
| 148 name.reset(reinterpret_cast<UNICODE_STRING*>(new BYTE[size])); | |
| 149 result = QueryObject(handle, ObjectNameInformation, name.get(), | |
| 150 size, &size); | |
|
nsylvain
2011/06/30 19:21:46
should you not align size with handle?
jschuh
2011/07/01 01:24:26
Done.
| |
| 151 } while (result == STATUS_INFO_LENGTH_MISMATCH); | |
| 152 | |
| 153 if (NT_SUCCESS(result)) | |
| 154 handle_name->assign(name->Buffer, name->Length / sizeof(wchar_t)); | |
| 155 | |
| 156 return result; | |
|
cpu_(ooo_6.6-7.5)
2011/06/30 20:39:39
I would return a boolean. That reflects its curren
jschuh
2011/07/01 01:24:26
Done.
| |
| 157 } | |
| 158 | |
| 147 const OBJECT_TYPE_INFORMATION* HandleTable::HandleEntry::TypeInfo() { | 159 const OBJECT_TYPE_INFORMATION* HandleTable::HandleEntry::TypeInfo() { |
| 148 UpdateInfo(UPDATE_INFO_ONLY); | 160 UpdateInfo(UPDATE_INFO_ONLY); |
| 149 return type_info_buffer_.empty() ? NULL : type_info_internal(); | 161 return type_info_buffer_.empty() ? NULL : type_info_internal(); |
| 150 } | 162 } |
| 151 | 163 |
| 152 const string16& HandleTable::HandleEntry::Name() { | 164 const string16& HandleTable::HandleEntry::Name() { |
| 153 UpdateInfo(UPDATE_INFO_AND_NAME); | 165 UpdateInfo(UPDATE_INFO_AND_NAME); |
| 154 return handle_name_; | 166 return handle_name_; |
| 155 } | 167 } |
| 156 | 168 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 172 const SYSTEM_HANDLE_INFORMATION* start, | 184 const SYSTEM_HANDLE_INFORMATION* start, |
| 173 const SYSTEM_HANDLE_INFORMATION* end) | 185 const SYSTEM_HANDLE_INFORMATION* end) |
| 174 : table_(table), current_(start), end_(end) { | 186 : table_(table), current_(start), end_(end) { |
| 175 } | 187 } |
| 176 | 188 |
| 177 HandleTable::Iterator::Iterator(const Iterator& it) | 189 HandleTable::Iterator::Iterator(const Iterator& it) |
| 178 : table_(it.table_), current_(it.current_.handle_entry_), end_(it.end_) { | 190 : table_(it.table_), current_(it.current_.handle_entry_), end_(it.end_) { |
| 179 } | 191 } |
| 180 | 192 |
| 181 } // namespace sandbox | 193 } // namespace sandbox |
| OLD | NEW |