OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/win/src/app_container.h" | 5 #include "sandbox/win/src/app_container.h" |
6 | 6 |
7 #include <Sddl.h> | 7 #include <Sddl.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/win/startup_information.h" | 12 #include "base/win/startup_information.h" |
13 #include "sandbox/win/src/internal_types.h" | 13 #include "sandbox/win/src/internal_types.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // Converts the passed in sid string to a PSID that must be relased with | 17 // Converts the passed in sid string to a PSID that must be relased with |
18 // LocalFree. | 18 // LocalFree. |
19 PSID ConvertSid(const string16& sid) { | 19 PSID ConvertSid(const string16& sid) { |
20 PSID local_sid; | 20 PSID local_sid; |
21 if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) | 21 if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) |
22 return NULL; | 22 return NULL; |
23 return local_sid; | 23 return local_sid; |
24 } | 24 } |
25 | 25 |
| 26 template <typename T> |
| 27 T BindFunction(const char* name) { |
| 28 HMODULE module = GetModuleHandle(sandbox::kKerneldllName); |
| 29 void* function = GetProcAddress(module, name); |
| 30 if (!function) { |
| 31 module = GetModuleHandle(sandbox::kKernelBasedllName); |
| 32 function = GetProcAddress(module, name); |
| 33 } |
| 34 return reinterpret_cast<T>(function); |
| 35 } |
| 36 |
26 } // namespace | 37 } // namespace |
27 | 38 |
28 namespace sandbox { | 39 namespace sandbox { |
29 | 40 |
30 AppContainerAttributes::AppContainerAttributes() { | 41 AppContainerAttributes::AppContainerAttributes() { |
31 memset(&capabilities_, 0, sizeof(capabilities_)); | 42 memset(&capabilities_, 0, sizeof(capabilities_)); |
32 } | 43 } |
33 | 44 |
34 AppContainerAttributes::~AppContainerAttributes() { | 45 AppContainerAttributes::~AppContainerAttributes() { |
35 for (size_t i = 0; i < attributes_.size(); i++) | 46 for (size_t i = 0; i < attributes_.size(); i++) |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 PSID local_sid; | 98 PSID local_sid; |
88 if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) | 99 if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) |
89 return SBOX_ERROR_INVALID_APP_CONTAINER; | 100 return SBOX_ERROR_INVALID_APP_CONTAINER; |
90 | 101 |
91 typedef HRESULT (WINAPI* AppContainerRegisterSidPtr)(PSID sid, | 102 typedef HRESULT (WINAPI* AppContainerRegisterSidPtr)(PSID sid, |
92 LPCWSTR moniker, | 103 LPCWSTR moniker, |
93 LPCWSTR display_name); | 104 LPCWSTR display_name); |
94 static AppContainerRegisterSidPtr AppContainerRegisterSid = NULL; | 105 static AppContainerRegisterSidPtr AppContainerRegisterSid = NULL; |
95 | 106 |
96 if (!AppContainerRegisterSid) { | 107 if (!AppContainerRegisterSid) { |
97 HMODULE module = GetModuleHandle(kKerneldllName); | 108 AppContainerRegisterSid = |
98 AppContainerRegisterSid = reinterpret_cast<AppContainerRegisterSidPtr>( | 109 BindFunction<AppContainerRegisterSidPtr>("AppContainerRegisterSid"); |
99 GetProcAddress(module, "AppContainerRegisterSid")); | |
100 } | 110 } |
101 | 111 |
102 ResultCode operation_result = SBOX_ERROR_GENERIC; | 112 ResultCode operation_result = SBOX_ERROR_GENERIC; |
103 if (AppContainerRegisterSid) { | 113 if (AppContainerRegisterSid) { |
104 HRESULT rv = AppContainerRegisterSid(local_sid, name.c_str(), name.c_str()); | 114 HRESULT rv = AppContainerRegisterSid(local_sid, name.c_str(), name.c_str()); |
105 if (SUCCEEDED(rv)) | 115 if (SUCCEEDED(rv)) |
106 operation_result = SBOX_ALL_OK; | 116 operation_result = SBOX_ALL_OK; |
107 else | 117 else |
108 DLOG(ERROR) << "AppContainerRegisterSid error:" << std::hex << rv; | 118 DLOG(ERROR) << "AppContainerRegisterSid error:" << std::hex << rv; |
109 } | 119 } |
110 LocalFree(local_sid); | 120 LocalFree(local_sid); |
111 return operation_result; | 121 return operation_result; |
112 } | 122 } |
113 | 123 |
114 ResultCode DeleteAppContainer(const string16& sid) { | 124 ResultCode DeleteAppContainer(const string16& sid) { |
115 PSID local_sid; | 125 PSID local_sid; |
116 if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) | 126 if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) |
117 return SBOX_ERROR_INVALID_APP_CONTAINER; | 127 return SBOX_ERROR_INVALID_APP_CONTAINER; |
118 | 128 |
119 typedef HRESULT (WINAPI* AppContainerUnregisterSidPtr)(PSID sid); | 129 typedef HRESULT (WINAPI* AppContainerUnregisterSidPtr)(PSID sid); |
120 static AppContainerUnregisterSidPtr AppContainerUnregisterSid = NULL; | 130 static AppContainerUnregisterSidPtr AppContainerUnregisterSid = NULL; |
121 | 131 |
122 if (!AppContainerUnregisterSid) { | 132 if (!AppContainerUnregisterSid) { |
123 HMODULE module = GetModuleHandle(kKerneldllName); | 133 AppContainerUnregisterSid = |
124 AppContainerUnregisterSid = reinterpret_cast<AppContainerUnregisterSidPtr>( | 134 BindFunction<AppContainerUnregisterSidPtr>("AppContainerUnregisterSid"); |
125 GetProcAddress(module, "AppContainerUnregisterSid")); | |
126 } | 135 } |
127 | 136 |
128 ResultCode operation_result = SBOX_ERROR_GENERIC; | 137 ResultCode operation_result = SBOX_ERROR_GENERIC; |
129 if (AppContainerUnregisterSid) { | 138 if (AppContainerUnregisterSid) { |
130 HRESULT rv = AppContainerUnregisterSid(local_sid); | 139 HRESULT rv = AppContainerUnregisterSid(local_sid); |
131 if (SUCCEEDED(rv)) | 140 if (SUCCEEDED(rv)) |
132 operation_result = SBOX_ALL_OK; | 141 operation_result = SBOX_ALL_OK; |
133 else | 142 else |
134 DLOG(ERROR) << "AppContainerUnregisterSid error:" << std::hex << rv; | 143 DLOG(ERROR) << "AppContainerUnregisterSid error:" << std::hex << rv; |
135 } | 144 } |
136 LocalFree(local_sid); | 145 LocalFree(local_sid); |
137 return operation_result; | 146 return operation_result; |
138 } | 147 } |
139 | 148 |
140 string16 LookupAppContainer(const string16& sid) { | 149 string16 LookupAppContainer(const string16& sid) { |
141 PSID local_sid; | 150 PSID local_sid; |
142 if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) | 151 if (!ConvertStringSidToSid(sid.c_str(), &local_sid)) |
143 return string16(); | 152 return string16(); |
144 | 153 |
145 typedef HRESULT (WINAPI* AppContainerLookupMonikerPtr)(PSID sid, | 154 typedef HRESULT (WINAPI* AppContainerLookupMonikerPtr)(PSID sid, |
146 LPWSTR* moniker); | 155 LPWSTR* moniker); |
147 typedef BOOLEAN (WINAPI* AppContainerFreeMemoryPtr)(void* ptr); | 156 typedef BOOLEAN (WINAPI* AppContainerFreeMemoryPtr)(void* ptr); |
148 | 157 |
149 static AppContainerLookupMonikerPtr AppContainerLookupMoniker = NULL; | 158 static AppContainerLookupMonikerPtr AppContainerLookupMoniker = NULL; |
150 static AppContainerFreeMemoryPtr AppContainerFreeMemory = NULL; | 159 static AppContainerFreeMemoryPtr AppContainerFreeMemory = NULL; |
151 | 160 |
152 if (!AppContainerLookupMoniker || !AppContainerFreeMemory) { | 161 if (!AppContainerLookupMoniker || !AppContainerFreeMemory) { |
153 HMODULE module = GetModuleHandle(kKerneldllName); | 162 AppContainerLookupMoniker = |
154 AppContainerLookupMoniker = reinterpret_cast<AppContainerLookupMonikerPtr>( | 163 BindFunction<AppContainerLookupMonikerPtr>("AppContainerLookupMoniker"); |
155 GetProcAddress(module, "AppContainerLookupMoniker")); | 164 AppContainerFreeMemory = |
156 AppContainerFreeMemory = reinterpret_cast<AppContainerFreeMemoryPtr>( | 165 BindFunction<AppContainerFreeMemoryPtr>("AppContainerFreeMemory"); |
157 GetProcAddress(module, "AppContainerFreeMemory")); | |
158 } | 166 } |
159 | 167 |
160 if (!AppContainerLookupMoniker || !AppContainerFreeMemory) | 168 if (!AppContainerLookupMoniker || !AppContainerFreeMemory) |
161 return string16(); | 169 return string16(); |
162 | 170 |
163 wchar_t* buffer = NULL; | 171 wchar_t* buffer = NULL; |
164 HRESULT rv = AppContainerLookupMoniker(local_sid, &buffer); | 172 HRESULT rv = AppContainerLookupMoniker(local_sid, &buffer); |
165 if (FAILED(rv)) | 173 if (FAILED(rv)) |
166 return string16(); | 174 return string16(); |
167 | 175 |
168 string16 name(buffer); | 176 string16 name(buffer); |
169 if (!AppContainerFreeMemory(buffer)) | 177 if (!AppContainerFreeMemory(buffer)) |
170 NOTREACHED(); | 178 NOTREACHED(); |
171 return name; | 179 return name; |
172 } | 180 } |
173 | 181 |
174 } // namespace sandbox | 182 } // namespace sandbox |
OLD | NEW |