Wed, 31 Dec 2014 06:09:35 +0100
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) 2012 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/app_container.h" |
michael@0 | 6 | |
michael@0 | 7 | #include <Sddl.h> |
michael@0 | 8 | #include <vector> |
michael@0 | 9 | |
michael@0 | 10 | #include "base/logging.h" |
michael@0 | 11 | #include "base/memory/scoped_ptr.h" |
michael@0 | 12 | #include "base/win/startup_information.h" |
michael@0 | 13 | #include "sandbox/win/src/internal_types.h" |
michael@0 | 14 | |
michael@0 | 15 | namespace { |
michael@0 | 16 | |
michael@0 | 17 | // Converts the passed in sid string to a PSID that must be relased with |
michael@0 | 18 | // LocalFree. |
michael@0 | 19 | PSID ConvertSid(const string16& sid) { |
michael@0 | 20 | PSID local_sid; |
michael@0 | 21 | if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) |
michael@0 | 22 | return NULL; |
michael@0 | 23 | return local_sid; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | template <typename T> |
michael@0 | 27 | T BindFunction(const char* name) { |
michael@0 | 28 | HMODULE module = GetModuleHandle(sandbox::kKerneldllName); |
michael@0 | 29 | void* function = GetProcAddress(module, name); |
michael@0 | 30 | if (!function) { |
michael@0 | 31 | module = GetModuleHandle(sandbox::kKernelBasedllName); |
michael@0 | 32 | function = GetProcAddress(module, name); |
michael@0 | 33 | } |
michael@0 | 34 | return reinterpret_cast<T>(function); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | } // namespace |
michael@0 | 38 | |
michael@0 | 39 | namespace sandbox { |
michael@0 | 40 | |
michael@0 | 41 | AppContainerAttributes::AppContainerAttributes() { |
michael@0 | 42 | memset(&capabilities_, 0, sizeof(capabilities_)); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | AppContainerAttributes::~AppContainerAttributes() { |
michael@0 | 46 | for (size_t i = 0; i < attributes_.size(); i++) |
michael@0 | 47 | LocalFree(attributes_[i].Sid); |
michael@0 | 48 | LocalFree(capabilities_.AppContainerSid); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | ResultCode AppContainerAttributes::SetAppContainer( |
michael@0 | 52 | const string16& app_container_sid, |
michael@0 | 53 | const std::vector<string16>& capabilities) { |
michael@0 | 54 | DCHECK(!capabilities_.AppContainerSid); |
michael@0 | 55 | DCHECK(attributes_.empty()); |
michael@0 | 56 | capabilities_.AppContainerSid = ConvertSid(app_container_sid); |
michael@0 | 57 | if (!capabilities_.AppContainerSid) |
michael@0 | 58 | return SBOX_ERROR_INVALID_APP_CONTAINER; |
michael@0 | 59 | |
michael@0 | 60 | for (size_t i = 0; i < capabilities.size(); i++) { |
michael@0 | 61 | SID_AND_ATTRIBUTES sid_and_attributes; |
michael@0 | 62 | sid_and_attributes.Sid = ConvertSid(capabilities[i]); |
michael@0 | 63 | if (!sid_and_attributes.Sid) |
michael@0 | 64 | return SBOX_ERROR_INVALID_CAPABILITY; |
michael@0 | 65 | |
michael@0 | 66 | sid_and_attributes.Attributes = SE_GROUP_ENABLED; |
michael@0 | 67 | attributes_.push_back(sid_and_attributes); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | if (capabilities.size()) { |
michael@0 | 71 | capabilities_.CapabilityCount = static_cast<DWORD>(capabilities.size()); |
michael@0 | 72 | capabilities_.Capabilities = &attributes_[0]; |
michael@0 | 73 | } |
michael@0 | 74 | return SBOX_ALL_OK; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | ResultCode AppContainerAttributes::ShareForStartup( |
michael@0 | 78 | base::win::StartupInformation* startup_information) const { |
michael@0 | 79 | // The only thing we support so far is an AppContainer. |
michael@0 | 80 | if (!capabilities_.AppContainerSid) |
michael@0 | 81 | return SBOX_ERROR_INVALID_APP_CONTAINER; |
michael@0 | 82 | |
michael@0 | 83 | if (!startup_information->UpdateProcThreadAttribute( |
michael@0 | 84 | PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES, |
michael@0 | 85 | const_cast<SECURITY_CAPABILITIES*>(&capabilities_), |
michael@0 | 86 | sizeof(capabilities_))) { |
michael@0 | 87 | DPLOG(ERROR) << "Failed UpdateProcThreadAttribute"; |
michael@0 | 88 | return SBOX_ERROR_CANNOT_INIT_APPCONTAINER; |
michael@0 | 89 | } |
michael@0 | 90 | return SBOX_ALL_OK; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | bool AppContainerAttributes::HasAppContainer() const { |
michael@0 | 94 | return (capabilities_.AppContainerSid != NULL); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | ResultCode CreateAppContainer(const string16& sid, const string16& name) { |
michael@0 | 98 | PSID local_sid; |
michael@0 | 99 | if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) |
michael@0 | 100 | return SBOX_ERROR_INVALID_APP_CONTAINER; |
michael@0 | 101 | |
michael@0 | 102 | typedef HRESULT (WINAPI* AppContainerRegisterSidPtr)(PSID sid, |
michael@0 | 103 | LPCWSTR moniker, |
michael@0 | 104 | LPCWSTR display_name); |
michael@0 | 105 | static AppContainerRegisterSidPtr AppContainerRegisterSid = NULL; |
michael@0 | 106 | |
michael@0 | 107 | if (!AppContainerRegisterSid) { |
michael@0 | 108 | AppContainerRegisterSid = |
michael@0 | 109 | BindFunction<AppContainerRegisterSidPtr>("AppContainerRegisterSid"); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | ResultCode operation_result = SBOX_ERROR_GENERIC; |
michael@0 | 113 | if (AppContainerRegisterSid) { |
michael@0 | 114 | HRESULT rv = AppContainerRegisterSid(local_sid, name.c_str(), name.c_str()); |
michael@0 | 115 | if (SUCCEEDED(rv)) |
michael@0 | 116 | operation_result = SBOX_ALL_OK; |
michael@0 | 117 | else |
michael@0 | 118 | DLOG(ERROR) << "AppContainerRegisterSid error:" << std::hex << rv; |
michael@0 | 119 | } |
michael@0 | 120 | LocalFree(local_sid); |
michael@0 | 121 | return operation_result; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | ResultCode DeleteAppContainer(const string16& sid) { |
michael@0 | 125 | PSID local_sid; |
michael@0 | 126 | if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) |
michael@0 | 127 | return SBOX_ERROR_INVALID_APP_CONTAINER; |
michael@0 | 128 | |
michael@0 | 129 | typedef HRESULT (WINAPI* AppContainerUnregisterSidPtr)(PSID sid); |
michael@0 | 130 | static AppContainerUnregisterSidPtr AppContainerUnregisterSid = NULL; |
michael@0 | 131 | |
michael@0 | 132 | if (!AppContainerUnregisterSid) { |
michael@0 | 133 | AppContainerUnregisterSid = |
michael@0 | 134 | BindFunction<AppContainerUnregisterSidPtr>("AppContainerUnregisterSid"); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | ResultCode operation_result = SBOX_ERROR_GENERIC; |
michael@0 | 138 | if (AppContainerUnregisterSid) { |
michael@0 | 139 | HRESULT rv = AppContainerUnregisterSid(local_sid); |
michael@0 | 140 | if (SUCCEEDED(rv)) |
michael@0 | 141 | operation_result = SBOX_ALL_OK; |
michael@0 | 142 | else |
michael@0 | 143 | DLOG(ERROR) << "AppContainerUnregisterSid error:" << std::hex << rv; |
michael@0 | 144 | } |
michael@0 | 145 | LocalFree(local_sid); |
michael@0 | 146 | return operation_result; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | string16 LookupAppContainer(const string16& sid) { |
michael@0 | 150 | PSID local_sid; |
michael@0 | 151 | if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) |
michael@0 | 152 | return string16(); |
michael@0 | 153 | |
michael@0 | 154 | typedef HRESULT (WINAPI* AppContainerLookupMonikerPtr)(PSID sid, |
michael@0 | 155 | LPWSTR* moniker); |
michael@0 | 156 | typedef BOOLEAN (WINAPI* AppContainerFreeMemoryPtr)(void* ptr); |
michael@0 | 157 | |
michael@0 | 158 | static AppContainerLookupMonikerPtr AppContainerLookupMoniker = NULL; |
michael@0 | 159 | static AppContainerFreeMemoryPtr AppContainerFreeMemory = NULL; |
michael@0 | 160 | |
michael@0 | 161 | if (!AppContainerLookupMoniker || !AppContainerFreeMemory) { |
michael@0 | 162 | AppContainerLookupMoniker = |
michael@0 | 163 | BindFunction<AppContainerLookupMonikerPtr>("AppContainerLookupMoniker"); |
michael@0 | 164 | AppContainerFreeMemory = |
michael@0 | 165 | BindFunction<AppContainerFreeMemoryPtr>("AppContainerFreeMemory"); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | if (!AppContainerLookupMoniker || !AppContainerFreeMemory) |
michael@0 | 169 | return string16(); |
michael@0 | 170 | |
michael@0 | 171 | wchar_t* buffer = NULL; |
michael@0 | 172 | HRESULT rv = AppContainerLookupMoniker(local_sid, &buffer); |
michael@0 | 173 | if (FAILED(rv)) |
michael@0 | 174 | return string16(); |
michael@0 | 175 | |
michael@0 | 176 | string16 name(buffer); |
michael@0 | 177 | if (!AppContainerFreeMemory(buffer)) |
michael@0 | 178 | NOTREACHED(); |
michael@0 | 179 | return name; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | } // namespace sandbox |