1 // Copyright (c) 2006-2008 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 "base/win/scoped_handle.h"
6 #include "base/win/scoped_process_information.h"
7 #include "base/win/windows_version.h"
8 #include "sandbox/win/src/restricted_token_utils.h"
10 // launcher.exe is an application used to launch another application with a
11 // restricted token. This is to be used for testing only.
12 // The parameters are the level of security of the primary token, the
13 // impersonation token and the job object along with the command line to
15 // See the usage (launcher.exe without parameters) for the correct format.
19 // Starts the process described by the input parameter command_line in a job
20 // with a restricted token. Also set the main thread of this newly created
21 // process to impersonate a user with more rights so it can initialize
24 // Parameters: primary_level is the security level of the primary token.
25 // impersonation_level is the security level of the impersonation token used
26 // to initialize the process. job_level is the security level of the job
27 // object used to encapsulate the process.
29 // The output parameter job_handle is the handle to the job object. Closing this
30 // handle will kill the process started.
32 // Note: The process started with this function has to call RevertToSelf() as
33 // soon as possible to stop using the impersonation token and start being
36 // Note: The Unicode version of this function will fail if the command_line
37 // parameter is a const string.
38 DWORD
StartRestrictedProcessInJob(wchar_t* command_line
,
39 TokenLevel primary_level
,
40 TokenLevel impersonation_level
,
42 base::win::ScopedHandle
* job_handle
) {
44 DWORD err_code
= job
.Init(job_level
, NULL
, 0, 0);
45 if (ERROR_SUCCESS
!= err_code
)
48 if (JOB_UNPROTECTED
!= job_level
) {
49 // Share the Desktop handle to be able to use MessageBox() in the sandboxed
51 err_code
= job
.UserHandleGrantAccess(GetDesktopWindow());
52 if (ERROR_SUCCESS
!= err_code
)
56 // Create the primary (restricted) token for the process
57 base::win::ScopedHandle primary_token
;
58 err_code
= sandbox::CreateRestrictedToken(primary_level
, INTEGRITY_LEVEL_LAST
,
59 PRIMARY
, &primary_token
);
60 if (ERROR_SUCCESS
!= err_code
)
64 // Create the impersonation token (restricted) to be able to start the
66 base::win::ScopedHandle impersonation_token
;
67 err_code
= sandbox::CreateRestrictedToken(impersonation_level
,
70 &impersonation_token
);
71 if (ERROR_SUCCESS
!= err_code
)
75 STARTUPINFO startup_info
= {0};
76 PROCESS_INFORMATION temp_process_info
= {};
77 DWORD flags
= CREATE_SUSPENDED
;
79 if (base::win::GetVersion() < base::win::VERSION_WIN8
) {
80 // Windows 8 implements nested jobs, but for older systems we need to
81 // break out of any job we're in to enforce our restrictions.
82 flags
|= CREATE_BREAKAWAY_FROM_JOB
;
85 if (!::CreateProcessAsUser(primary_token
.Get(),
86 NULL
, // No application name.
88 NULL
, // No security attribute.
89 NULL
, // No thread attribute.
90 FALSE
, // Do not inherit handles.
92 NULL
, // Use the environment of the caller.
93 NULL
, // Use current directory of the caller.
95 &temp_process_info
)) {
96 return ::GetLastError();
98 base::win::ScopedProcessInformation
process_info(temp_process_info
);
100 // Change the token of the main thread of the new process for the
101 // impersonation token with more rights.
103 HANDLE temp_thread
= process_info
.thread_handle();
104 if (!::SetThreadToken(&temp_thread
, impersonation_token
.Get())) {
105 auto last_error
= ::GetLastError();
106 ::TerminateProcess(process_info
.process_handle(),
112 err_code
= job
.AssignProcessToJob(process_info
.process_handle());
113 if (ERROR_SUCCESS
!= err_code
) {
114 auto last_error
= ::GetLastError();
115 ::TerminateProcess(process_info
.process_handle(),
120 // Start the application
121 ::ResumeThread(process_info
.thread_handle());
123 *job_handle
= job
.Take();
125 return ERROR_SUCCESS
;
130 #define PARAM_IS(y) (argc > i) && (_wcsicmp(argv[i], y) == 0)
132 void PrintUsage(const wchar_t *application_name
) {
133 wprintf(L
"\n\nUsage: \n %ls --main level --init level --job level cmd_line ",
135 wprintf(L
"\n\n Levels : \n\tLOCKDOWN \n\tRESTRICTED "
136 L
"\n\tLIMITED_USER \n\tINTERACTIVE_USER \n\tNON_ADMIN \n\tUNPROTECTED");
137 wprintf(L
"\n\n main: Security level of the main token");
138 wprintf(L
"\n init: Security level of the impersonation token");
139 wprintf(L
"\n job: Security level of the job object");
142 bool GetTokenLevelFromString(const wchar_t *param
,
143 sandbox::TokenLevel
* level
) {
144 if (_wcsicmp(param
, L
"LOCKDOWN") == 0) {
145 *level
= sandbox::USER_LOCKDOWN
;
146 } else if (_wcsicmp(param
, L
"RESTRICTED") == 0) {
147 *level
= sandbox::USER_RESTRICTED
;
148 } else if (_wcsicmp(param
, L
"LIMITED_USER") == 0) {
149 *level
= sandbox::USER_LIMITED
;
150 } else if (_wcsicmp(param
, L
"INTERACTIVE_USER") == 0) {
151 *level
= sandbox::USER_INTERACTIVE
;
152 } else if (_wcsicmp(param
, L
"NON_ADMIN") == 0) {
153 *level
= sandbox::USER_NON_ADMIN
;
154 } else if (_wcsicmp(param
, L
"USER_RESTRICTED_SAME_ACCESS") == 0) {
155 *level
= sandbox::USER_RESTRICTED_SAME_ACCESS
;
156 } else if (_wcsicmp(param
, L
"UNPROTECTED") == 0) {
157 *level
= sandbox::USER_UNPROTECTED
;
165 bool GetJobLevelFromString(const wchar_t *param
, sandbox::JobLevel
* level
) {
166 if (_wcsicmp(param
, L
"LOCKDOWN") == 0) {
167 *level
= sandbox::JOB_LOCKDOWN
;
168 } else if (_wcsicmp(param
, L
"RESTRICTED") == 0) {
169 *level
= sandbox::JOB_RESTRICTED
;
170 } else if (_wcsicmp(param
, L
"LIMITED_USER") == 0) {
171 *level
= sandbox::JOB_LIMITED_USER
;
172 } else if (_wcsicmp(param
, L
"INTERACTIVE_USER") == 0) {
173 *level
= sandbox::JOB_INTERACTIVE
;
174 } else if (_wcsicmp(param
, L
"NON_ADMIN") == 0) {
175 wprintf(L
"\nNON_ADMIN is not a supported job type");
177 } else if (_wcsicmp(param
, L
"UNPROTECTED") == 0) {
178 *level
= sandbox::JOB_UNPROTECTED
;
186 int wmain(int argc
, wchar_t *argv
[]) {
187 // Extract the filename from the path.
188 wchar_t *app_name
= wcsrchr(argv
[0], L
'\\');
197 PrintUsage(app_name
);
201 sandbox::TokenLevel primary_level
= sandbox::USER_LOCKDOWN
;
202 sandbox::TokenLevel impersonation_level
=
203 sandbox::USER_RESTRICTED_SAME_ACCESS
;
204 sandbox::JobLevel job_level
= sandbox::JOB_LOCKDOWN
;
205 ATL::CString command_line
;
207 // parse command line.
208 for (int i
= 1; i
< argc
; ++i
) {
209 if (PARAM_IS(L
"--main")) {
212 if (!GetTokenLevelFromString(argv
[i
], &primary_level
)) {
213 wprintf(L
"\nAbord, Unrecognized main token level \"%ls\"", argv
[i
]);
214 PrintUsage(app_name
);
218 } else if (PARAM_IS(L
"--init")) {
221 if (!GetTokenLevelFromString(argv
[i
], &impersonation_level
)) {
222 wprintf(L
"\nAbord, Unrecognized init token level \"%ls\"", argv
[i
]);
223 PrintUsage(app_name
);
227 } else if (PARAM_IS(L
"--job")) {
230 if (!GetJobLevelFromString(argv
[i
], &job_level
)) {
231 wprintf(L
"\nAbord, Unrecognized job security level \"%ls\"", argv
[i
]);
232 PrintUsage(app_name
);
237 if (command_line
.GetLength()) {
238 command_line
+= L
' ';
240 command_line
+= argv
[i
];
244 if (!command_line
.GetLength()) {
245 wprintf(L
"\nAbord, No command line specified");
246 PrintUsage(app_name
);
250 wprintf(L
"\nLaunching command line: \"%ls\"\n", command_line
.GetBuffer());
252 base::win::ScopedHandle job_handle
;
253 DWORD err_code
= StartRestrictedProcessInJob(
254 command_line
.GetBuffer(),
259 if (ERROR_SUCCESS
!= err_code
) {
260 wprintf(L
"\nAbord, Error %d while launching command line.", err_code
);
264 wprintf(L
"\nPress any key to continue.");