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
3 // found in the LICENSE file.
5 #include "sandbox/win/src/app_container.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/win/startup_information.h"
13 #include "sandbox/win/src/internal_types.h"
17 // Converts the passed in sid string to a PSID that must be relased with
19 PSID
ConvertSid(const string16
& sid
) {
21 if (!ConvertStringSidToSid(sid
.c_str(), &local_sid
))
30 AppContainerAttributes::AppContainerAttributes() {
31 memset(&capabilities_
, 0, sizeof(capabilities_
));
34 AppContainerAttributes::~AppContainerAttributes() {
35 for (size_t i
= 0; i
< attributes_
.size(); i
++)
36 LocalFree(attributes_
[i
].Sid
);
37 LocalFree(capabilities_
.AppContainerSid
);
40 ResultCode
AppContainerAttributes::SetAppContainer(
41 const string16
& app_container_sid
,
42 const std::vector
<string16
>& capabilities
) {
43 DCHECK(!capabilities_
.AppContainerSid
);
44 DCHECK(attributes_
.empty());
45 capabilities_
.AppContainerSid
= ConvertSid(app_container_sid
);
46 if (!capabilities_
.AppContainerSid
)
47 return SBOX_ERROR_INVALID_APP_CONTAINER
;
49 for (size_t i
= 0; i
< capabilities
.size(); i
++) {
50 SID_AND_ATTRIBUTES sid_and_attributes
;
51 sid_and_attributes
.Sid
= ConvertSid(capabilities
[i
]);
52 if (!sid_and_attributes
.Sid
)
53 return SBOX_ERROR_INVALID_CAPABILITY
;
55 sid_and_attributes
.Attributes
= SE_GROUP_ENABLED
;
56 attributes_
.push_back(sid_and_attributes
);
59 if (capabilities
.size()) {
60 capabilities_
.CapabilityCount
= static_cast<DWORD
>(capabilities
.size());
61 capabilities_
.Capabilities
= &attributes_
[0];
66 ResultCode
AppContainerAttributes::ShareForStartup(
67 base::win::StartupInformation
* startup_information
) const {
68 // The only thing we support so far is an AppContainer.
69 if (!capabilities_
.AppContainerSid
)
70 return SBOX_ERROR_INVALID_APP_CONTAINER
;
72 if (!startup_information
->UpdateProcThreadAttribute(
73 PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES
,
74 const_cast<SECURITY_CAPABILITIES
*>(&capabilities_
),
75 sizeof(capabilities_
))) {
76 DPLOG(ERROR
) << "Failed UpdateProcThreadAttribute";
77 return SBOX_ERROR_CANNOT_INIT_APPCONTAINER
;
82 bool AppContainerAttributes::HasAppContainer() const {
83 return (capabilities_
.AppContainerSid
!= NULL
);
86 ResultCode
CreateAppContainer(const string16
& sid
, const string16
& name
) {
88 if (!ConvertStringSidToSid(sid
.c_str(), &local_sid
))
89 return SBOX_ERROR_INVALID_APP_CONTAINER
;
91 typedef HRESULT (WINAPI
* AppContainerRegisterSidPtr
)(PSID sid
,
93 LPCWSTR display_name
);
94 static AppContainerRegisterSidPtr AppContainerRegisterSid
= NULL
;
96 if (!AppContainerRegisterSid
) {
97 HMODULE module
= GetModuleHandle(kKerneldllName
);
98 AppContainerRegisterSid
= reinterpret_cast<AppContainerRegisterSidPtr
>(
99 GetProcAddress(module
, "AppContainerRegisterSid"));
102 ResultCode operation_result
= SBOX_ERROR_GENERIC
;
103 if (AppContainerRegisterSid
) {
104 HRESULT rv
= AppContainerRegisterSid(local_sid
, name
.c_str(), name
.c_str());
106 operation_result
= SBOX_ALL_OK
;
108 DLOG(ERROR
) << "AppContainerRegisterSid error:" << std::hex
<< rv
;
110 LocalFree(local_sid
);
111 return operation_result
;
114 ResultCode
DeleteAppContainer(const string16
& sid
) {
116 if (!ConvertStringSidToSid(sid
.c_str(), &local_sid
))
117 return SBOX_ERROR_INVALID_APP_CONTAINER
;
119 typedef HRESULT (WINAPI
* AppContainerUnregisterSidPtr
)(PSID sid
);
120 static AppContainerUnregisterSidPtr AppContainerUnregisterSid
= NULL
;
122 if (!AppContainerUnregisterSid
) {
123 HMODULE module
= GetModuleHandle(kKerneldllName
);
124 AppContainerUnregisterSid
= reinterpret_cast<AppContainerUnregisterSidPtr
>(
125 GetProcAddress(module
, "AppContainerUnregisterSid"));
128 ResultCode operation_result
= SBOX_ERROR_GENERIC
;
129 if (AppContainerUnregisterSid
) {
130 HRESULT rv
= AppContainerUnregisterSid(local_sid
);
132 operation_result
= SBOX_ALL_OK
;
134 DLOG(ERROR
) << "AppContainerUnregisterSid error:" << std::hex
<< rv
;
136 LocalFree(local_sid
);
137 return operation_result
;
140 string16
LookupAppContainer(const string16
& sid
) {
142 if (!ConvertStringSidToSid(sid
.c_str(), &local_sid
))
145 typedef HRESULT (WINAPI
* AppContainerLookupMonikerPtr
)(PSID sid
,
147 typedef BOOLEAN (WINAPI
* AppContainerFreeMemoryPtr
)(void* ptr
);
149 static AppContainerLookupMonikerPtr AppContainerLookupMoniker
= NULL
;
150 static AppContainerFreeMemoryPtr AppContainerFreeMemory
= NULL
;
152 if (!AppContainerLookupMoniker
|| !AppContainerFreeMemory
) {
153 HMODULE module
= GetModuleHandle(kKerneldllName
);
154 AppContainerLookupMoniker
= reinterpret_cast<AppContainerLookupMonikerPtr
>(
155 GetProcAddress(module
, "AppContainerLookupMoniker"));
156 AppContainerFreeMemory
= reinterpret_cast<AppContainerFreeMemoryPtr
>(
157 GetProcAddress(module
, "AppContainerFreeMemory"));
160 if (!AppContainerLookupMoniker
|| !AppContainerFreeMemory
)
163 wchar_t* buffer
= NULL
;
164 HRESULT rv
= AppContainerLookupMoniker(local_sid
, &buffer
);
168 string16
name(buffer
);
169 if (!AppContainerFreeMemory(buffer
))
174 } // namespace sandbox