1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/win/src/handle_table.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,159 @@ 1.4 +// Copyright (c) 2011 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef SANDBOX_SRC_HANDLE_TABLE_H_ 1.9 +#define SANDBOX_SRC_HANDLE_TABLE_H_ 1.10 + 1.11 +#include <windows.h> 1.12 +#include <vector> 1.13 + 1.14 +#include "base/basictypes.h" 1.15 +#include "base/strings/string16.h" 1.16 +#include "sandbox/win/src/nt_internals.h" 1.17 + 1.18 +namespace sandbox { 1.19 + 1.20 +// HandleTable retrieves the global handle table and provides helper classes 1.21 +// for iterating through the table and retrieving handle info. 1.22 +class HandleTable { 1.23 + public: 1.24 + static const char16* HandleTable::kTypeProcess; 1.25 + static const char16* HandleTable::kTypeThread; 1.26 + static const char16* HandleTable::kTypeFile; 1.27 + static const char16* HandleTable::kTypeDirectory; 1.28 + static const char16* HandleTable::kTypeKey; 1.29 + static const char16* HandleTable::kTypeWindowStation; 1.30 + static const char16* HandleTable::kTypeDesktop; 1.31 + static const char16* HandleTable::kTypeService; 1.32 + static const char16* HandleTable::kTypeMutex; 1.33 + static const char16* HandleTable::kTypeSemaphore; 1.34 + static const char16* HandleTable::kTypeEvent; 1.35 + static const char16* HandleTable::kTypeTimer; 1.36 + static const char16* HandleTable::kTypeNamedPipe; 1.37 + static const char16* HandleTable::kTypeJobObject; 1.38 + static const char16* HandleTable::kTypeFileMap; 1.39 + static const char16* HandleTable::kTypeAlpcPort; 1.40 + 1.41 + class Iterator; 1.42 + 1.43 + // Used by the iterator to provide simple caching accessors to handle data. 1.44 + class HandleEntry { 1.45 + public: 1.46 + bool operator==(const HandleEntry& rhs) const { 1.47 + return handle_entry_ == rhs.handle_entry_; 1.48 + } 1.49 + 1.50 + bool operator!=(const HandleEntry& rhs) const { 1.51 + return handle_entry_ != rhs.handle_entry_; 1.52 + } 1.53 + 1.54 + const SYSTEM_HANDLE_INFORMATION* handle_entry() const { 1.55 + return handle_entry_; 1.56 + } 1.57 + 1.58 + const OBJECT_TYPE_INFORMATION* TypeInfo(); 1.59 + 1.60 + const string16& Name(); 1.61 + 1.62 + const string16& Type(); 1.63 + 1.64 + bool IsType(const string16& type_string); 1.65 + 1.66 + private: 1.67 + friend class Iterator; 1.68 + friend class HandleTable; 1.69 + 1.70 + enum UpdateType { 1.71 + UPDATE_INFO_ONLY, 1.72 + UPDATE_INFO_AND_NAME, 1.73 + UPDATE_INFO_AND_TYPE_NAME, 1.74 + }; 1.75 + 1.76 + explicit HandleEntry(const SYSTEM_HANDLE_INFORMATION* handle_info_entry); 1.77 + 1.78 + bool needs_info_update() { return handle_entry_ != last_entry_; } 1.79 + 1.80 + void UpdateInfo(UpdateType flag); 1.81 + 1.82 + OBJECT_TYPE_INFORMATION* type_info_internal() { 1.83 + return reinterpret_cast<OBJECT_TYPE_INFORMATION*>( 1.84 + &(type_info_buffer_[0])); 1.85 + } 1.86 + 1.87 + const SYSTEM_HANDLE_INFORMATION* handle_entry_; 1.88 + const SYSTEM_HANDLE_INFORMATION* last_entry_; 1.89 + std::vector<BYTE> type_info_buffer_; 1.90 + string16 handle_name_; 1.91 + string16 type_name_; 1.92 + 1.93 + DISALLOW_COPY_AND_ASSIGN(HandleEntry); 1.94 + }; 1.95 + 1.96 + class Iterator { 1.97 + public: 1.98 + Iterator(const HandleTable& table, const SYSTEM_HANDLE_INFORMATION* start, 1.99 + const SYSTEM_HANDLE_INFORMATION* stop); 1.100 + 1.101 + Iterator(const Iterator& it); 1.102 + 1.103 + Iterator& operator++() { 1.104 + if (++(current_.handle_entry_) == end_) 1.105 + current_.handle_entry_ = table_.end(); 1.106 + return *this; 1.107 + } 1.108 + 1.109 + bool operator==(const Iterator& rhs) const { 1.110 + return current_ == rhs.current_; 1.111 + } 1.112 + 1.113 + bool operator!=(const Iterator& rhs) const { 1.114 + return current_ != rhs.current_; 1.115 + } 1.116 + 1.117 + HandleEntry& operator*() { return current_; } 1.118 + 1.119 + operator const SYSTEM_HANDLE_INFORMATION*() { 1.120 + return current_.handle_entry_; 1.121 + } 1.122 + 1.123 + HandleEntry* operator->() { return ¤t_; } 1.124 + 1.125 + private: 1.126 + const HandleTable& table_; 1.127 + HandleEntry current_; 1.128 + const SYSTEM_HANDLE_INFORMATION* end_; 1.129 + }; 1.130 + 1.131 + HandleTable(); 1.132 + 1.133 + Iterator begin() const { 1.134 + return Iterator(*this, handle_info()->Information, 1.135 + &handle_info()->Information[handle_info()->NumberOfHandles]); 1.136 + } 1.137 + 1.138 + const SYSTEM_HANDLE_INFORMATION_EX* handle_info() const { 1.139 + return reinterpret_cast<const SYSTEM_HANDLE_INFORMATION_EX*>( 1.140 + &(handle_info_buffer_[0])); 1.141 + } 1.142 + 1.143 + // Returns an iterator to the handles for only the supplied process ID. 1.144 + Iterator HandlesForProcess(ULONG process_id) const; 1.145 + const SYSTEM_HANDLE_INFORMATION* end() const { 1.146 + return &handle_info()->Information[handle_info()->NumberOfHandles]; 1.147 + } 1.148 + 1.149 + private: 1.150 + SYSTEM_HANDLE_INFORMATION_EX* handle_info_internal() { 1.151 + return reinterpret_cast<SYSTEM_HANDLE_INFORMATION_EX*>( 1.152 + &(handle_info_buffer_[0])); 1.153 + } 1.154 + 1.155 + std::vector<BYTE> handle_info_buffer_; 1.156 + 1.157 + DISALLOW_COPY_AND_ASSIGN(HandleTable); 1.158 +}; 1.159 + 1.160 +} // namespace sandbox 1.161 + 1.162 +#endif // SANDBOX_SRC_HANDLE_TABLE_H_