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
3 // found in the LICENSE file.
5 #include "sandbox/win/src/window.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
14 // Gets the security attributes of a window object referenced by |handle|. The
15 // lpSecurityDescriptor member of the SECURITY_ATTRIBUTES parameter returned
16 // must be freed using LocalFree by the caller.
17 bool GetSecurityAttributes(HANDLE handle
, SECURITY_ATTRIBUTES
* attributes
) {
18 attributes
->bInheritHandle
= FALSE
;
19 attributes
->nLength
= sizeof(SECURITY_ATTRIBUTES
);
22 DWORD result
= ::GetSecurityInfo(handle
, SE_WINDOW_OBJECT
,
23 DACL_SECURITY_INFORMATION
, NULL
, NULL
, &dacl
,
24 NULL
, &attributes
->lpSecurityDescriptor
);
25 if (ERROR_SUCCESS
== result
)
35 ResultCode
CreateAltWindowStation(HWINSTA
* winsta
) {
36 // Get the security attributes from the current window station; we will
37 // use this as the base security attributes for the new window station.
38 SECURITY_ATTRIBUTES attributes
= {0};
39 if (!GetSecurityAttributes(::GetProcessWindowStation(), &attributes
)) {
40 return SBOX_ERROR_CANNOT_CREATE_WINSTATION
;
43 // Create the window station using NULL for the name to ask the os to
45 // TODO(nsylvain): don't ask for WINSTA_ALL_ACCESS if we don't need to.
46 *winsta
= ::CreateWindowStationW(NULL
, 0, WINSTA_ALL_ACCESS
, &attributes
);
47 LocalFree(attributes
.lpSecurityDescriptor
);
52 return SBOX_ERROR_CANNOT_CREATE_WINSTATION
;
55 ResultCode
CreateAltDesktop(HWINSTA winsta
, HDESK
* desktop
) {
56 std::wstring desktop_name
= L
"sbox_alternate_desktop_";
58 // Append the current PID to the desktop name.
60 _snwprintf_s(buffer
, sizeof(buffer
) / sizeof(wchar_t), L
"0x%X",
61 ::GetCurrentProcessId());
62 desktop_name
+= buffer
;
64 // Get the security attributes from the current desktop, we will use this as
65 // the base security attributes for the new desktop.
66 SECURITY_ATTRIBUTES attributes
= {0};
67 if (!GetSecurityAttributes(GetThreadDesktop(GetCurrentThreadId()),
69 return SBOX_ERROR_CANNOT_CREATE_DESKTOP
;
72 // Back up the current window station, in case we need to switch it.
73 HWINSTA current_winsta
= ::GetProcessWindowStation();
76 // We need to switch to the alternate window station before creating the
78 if (!::SetProcessWindowStation(winsta
)) {
79 ::LocalFree(attributes
.lpSecurityDescriptor
);
80 return SBOX_ERROR_CANNOT_CREATE_DESKTOP
;
84 // Create the destkop.
85 // TODO(nsylvain): don't ask for GENERIC_ALL if we don't need to.
86 *desktop
= ::CreateDesktop(desktop_name
.c_str(), NULL
, NULL
, 0, GENERIC_ALL
,
88 ::LocalFree(attributes
.lpSecurityDescriptor
);
91 // Revert to the right window station.
92 if (!::SetProcessWindowStation(current_winsta
)) {
93 return SBOX_ERROR_FAILED_TO_SWITCH_BACK_WINSTATION
;
100 return SBOX_ERROR_CANNOT_CREATE_DESKTOP
;
103 std::wstring
GetWindowObjectName(HANDLE handle
) {
104 // Get the size of the name.
106 ::GetUserObjectInformation(handle
, UOI_NAME
, NULL
, 0, &size
);
110 return std::wstring();
113 // Create the buffer that will hold the name.
114 scoped_ptr
<wchar_t[]> name_buffer(new wchar_t[size
]);
116 // Query the name of the object.
117 if (!::GetUserObjectInformation(handle
, UOI_NAME
, name_buffer
.get(), size
,
120 return std::wstring();
123 return std::wstring(name_buffer
.get());
126 std::wstring
GetFullDesktopName(HWINSTA winsta
, HDESK desktop
) {
129 return std::wstring();
134 name
= GetWindowObjectName(winsta
);
138 name
+= GetWindowObjectName(desktop
);
142 } // namespace sandbox