michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "sandbox/win/src/window.h" michael@0: michael@0: #include michael@0: michael@0: #include "base/logging.h" michael@0: #include "base/memory/scoped_ptr.h" michael@0: michael@0: namespace { michael@0: michael@0: // Gets the security attributes of a window object referenced by |handle|. The michael@0: // lpSecurityDescriptor member of the SECURITY_ATTRIBUTES parameter returned michael@0: // must be freed using LocalFree by the caller. michael@0: bool GetSecurityAttributes(HANDLE handle, SECURITY_ATTRIBUTES* attributes) { michael@0: attributes->bInheritHandle = FALSE; michael@0: attributes->nLength = sizeof(SECURITY_ATTRIBUTES); michael@0: michael@0: PACL dacl = NULL; michael@0: DWORD result = ::GetSecurityInfo(handle, SE_WINDOW_OBJECT, michael@0: DACL_SECURITY_INFORMATION, NULL, NULL, &dacl, michael@0: NULL, &attributes->lpSecurityDescriptor); michael@0: if (ERROR_SUCCESS == result) michael@0: return true; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: } michael@0: michael@0: namespace sandbox { michael@0: michael@0: ResultCode CreateAltWindowStation(HWINSTA* winsta) { michael@0: // Get the security attributes from the current window station; we will michael@0: // use this as the base security attributes for the new window station. michael@0: SECURITY_ATTRIBUTES attributes = {0}; michael@0: if (!GetSecurityAttributes(::GetProcessWindowStation(), &attributes)) { michael@0: return SBOX_ERROR_CANNOT_CREATE_WINSTATION; michael@0: } michael@0: michael@0: // Create the window station using NULL for the name to ask the os to michael@0: // generate it. michael@0: // TODO(nsylvain): don't ask for WINSTA_ALL_ACCESS if we don't need to. michael@0: *winsta = ::CreateWindowStationW(NULL, 0, WINSTA_ALL_ACCESS, &attributes); michael@0: LocalFree(attributes.lpSecurityDescriptor); michael@0: michael@0: if (*winsta) michael@0: return SBOX_ALL_OK; michael@0: michael@0: return SBOX_ERROR_CANNOT_CREATE_WINSTATION; michael@0: } michael@0: michael@0: ResultCode CreateAltDesktop(HWINSTA winsta, HDESK* desktop) { michael@0: std::wstring desktop_name = L"sbox_alternate_desktop_"; michael@0: michael@0: // Append the current PID to the desktop name. michael@0: wchar_t buffer[16]; michael@0: _snwprintf_s(buffer, sizeof(buffer) / sizeof(wchar_t), L"0x%X", michael@0: ::GetCurrentProcessId()); michael@0: desktop_name += buffer; michael@0: michael@0: // Get the security attributes from the current desktop, we will use this as michael@0: // the base security attributes for the new desktop. michael@0: SECURITY_ATTRIBUTES attributes = {0}; michael@0: if (!GetSecurityAttributes(GetThreadDesktop(GetCurrentThreadId()), michael@0: &attributes)) { michael@0: return SBOX_ERROR_CANNOT_CREATE_DESKTOP; michael@0: } michael@0: michael@0: // Back up the current window station, in case we need to switch it. michael@0: HWINSTA current_winsta = ::GetProcessWindowStation(); michael@0: michael@0: if (winsta) { michael@0: // We need to switch to the alternate window station before creating the michael@0: // desktop. michael@0: if (!::SetProcessWindowStation(winsta)) { michael@0: ::LocalFree(attributes.lpSecurityDescriptor); michael@0: return SBOX_ERROR_CANNOT_CREATE_DESKTOP; michael@0: } michael@0: } michael@0: michael@0: // Create the destkop. michael@0: // TODO(nsylvain): don't ask for GENERIC_ALL if we don't need to. michael@0: *desktop = ::CreateDesktop(desktop_name.c_str(), NULL, NULL, 0, GENERIC_ALL, michael@0: &attributes); michael@0: ::LocalFree(attributes.lpSecurityDescriptor); michael@0: michael@0: if (winsta) { michael@0: // Revert to the right window station. michael@0: if (!::SetProcessWindowStation(current_winsta)) { michael@0: return SBOX_ERROR_FAILED_TO_SWITCH_BACK_WINSTATION; michael@0: } michael@0: } michael@0: michael@0: if (*desktop) michael@0: return SBOX_ALL_OK; michael@0: michael@0: return SBOX_ERROR_CANNOT_CREATE_DESKTOP; michael@0: } michael@0: michael@0: std::wstring GetWindowObjectName(HANDLE handle) { michael@0: // Get the size of the name. michael@0: DWORD size = 0; michael@0: ::GetUserObjectInformation(handle, UOI_NAME, NULL, 0, &size); michael@0: michael@0: if (!size) { michael@0: NOTREACHED(); michael@0: return std::wstring(); michael@0: } michael@0: michael@0: // Create the buffer that will hold the name. michael@0: scoped_ptr name_buffer(new wchar_t[size]); michael@0: michael@0: // Query the name of the object. michael@0: if (!::GetUserObjectInformation(handle, UOI_NAME, name_buffer.get(), size, michael@0: &size)) { michael@0: NOTREACHED(); michael@0: return std::wstring(); michael@0: } michael@0: michael@0: return std::wstring(name_buffer.get()); michael@0: } michael@0: michael@0: std::wstring GetFullDesktopName(HWINSTA winsta, HDESK desktop) { michael@0: if (!desktop) { michael@0: NOTREACHED(); michael@0: return std::wstring(); michael@0: } michael@0: michael@0: std::wstring name; michael@0: if (winsta) { michael@0: name = GetWindowObjectName(winsta); michael@0: name += L'\\'; michael@0: } michael@0: michael@0: name += GetWindowObjectName(desktop); michael@0: return name; michael@0: } michael@0: michael@0: } // namespace sandbox