1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "sandbox/src/restricted_token_utils.h"
// launcher.exe is an application used to launch another application with a
// restricted token. This is to be used for testing only.
// The parameters are the level of security of the primary token, the
// impersonation token and the job object along with the command line to
// execute.
// See the usage (launcher.exe without parameters) for the correct format.
#define PARAM_IS(y) (argc > i) && (_wcsicmp(argv[i], y) == 0)
void PrintUsage(const wchar_t *application_name) {
wprintf(L"\n\nUsage: \n %s --main level --init level --job level cmd_line ",
application_name);
wprintf(L"\n\n Levels : \n\tLOCKDOWN \n\tRESTRICTED "
L"\n\tLIMITED_USER \n\tINTERACTIVE_USER \n\tNON_ADMIN \n\tUNPROTECTED");
wprintf(L"\n\n main: Security level of the main token");
wprintf(L"\n init: Security level of the impersonation token");
wprintf(L"\n job: Security level of the job object");
}
bool GetTokenLevelFromString(const wchar_t *param,
sandbox::TokenLevel* level) {
if (_wcsicmp(param, L"LOCKDOWN") == 0) {
*level = sandbox::USER_LOCKDOWN;
} else if (_wcsicmp(param, L"RESTRICTED") == 0) {
*level = sandbox::USER_RESTRICTED;
} else if (_wcsicmp(param, L"LIMITED_USER") == 0) {
*level = sandbox::USER_LIMITED;
} else if (_wcsicmp(param, L"INTERACTIVE_USER") == 0) {
*level = sandbox::USER_INTERACTIVE;
} else if (_wcsicmp(param, L"NON_ADMIN") == 0) {
*level = sandbox::USER_NON_ADMIN;
} else if (_wcsicmp(param, L"USER_RESTRICTED_SAME_ACCESS") == 0) {
*level = sandbox::USER_RESTRICTED_SAME_ACCESS;
} else if (_wcsicmp(param, L"UNPROTECTED") == 0) {
*level = sandbox::USER_UNPROTECTED;
} else {
return false;
}
return true;
}
bool GetJobLevelFromString(const wchar_t *param, sandbox::JobLevel* level) {
if (_wcsicmp(param, L"LOCKDOWN") == 0) {
*level = sandbox::JOB_LOCKDOWN;
} else if (_wcsicmp(param, L"RESTRICTED") == 0) {
*level = sandbox::JOB_RESTRICTED;
} else if (_wcsicmp(param, L"LIMITED_USER") == 0) {
*level = sandbox::JOB_LIMITED_USER;
} else if (_wcsicmp(param, L"INTERACTIVE_USER") == 0) {
*level = sandbox::JOB_INTERACTIVE;
} else if (_wcsicmp(param, L"NON_ADMIN") == 0) {
wprintf(L"\nNON_ADMIN is not a supported job type");
return false;
} else if (_wcsicmp(param, L"UNPROTECTED") == 0) {
*level = sandbox::JOB_UNPROTECTED;
} else {
return false;
}
return true;
}
int wmain(int argc, wchar_t *argv[]) {
// Extract the filename from the path.
wchar_t *app_name = wcsrchr(argv[0], L'\\');
if (!app_name) {
app_name = argv[0];
} else {
app_name++;
}
// no argument
if (argc == 1) {
PrintUsage(app_name);
return -1;
}
sandbox::TokenLevel primary_level = sandbox::USER_LOCKDOWN;
sandbox::TokenLevel impersonation_level =
sandbox::USER_RESTRICTED_SAME_ACCESS;
sandbox::JobLevel job_level = sandbox::JOB_LOCKDOWN;
ATL::CString command_line;
// parse command line.
for (int i = 1; i < argc; ++i) {
if (PARAM_IS(L"--main")) {
i++;
if (argc > i) {
if (!GetTokenLevelFromString(argv[i], &primary_level)) {
wprintf(L"\nAbord, Unrecognized main token level \"%s\"", argv[i]);
PrintUsage(app_name);
return -1;
}
}
} else if (PARAM_IS(L"--init")) {
i++;
if (argc > i) {
if (!GetTokenLevelFromString(argv[i], &impersonation_level)) {
wprintf(L"\nAbord, Unrecognized init token level \"%s\"", argv[i]);
PrintUsage(app_name);
return -1;
}
}
} else if (PARAM_IS(L"--job")) {
i++;
if (argc > i) {
if (!GetJobLevelFromString(argv[i], &job_level)) {
wprintf(L"\nAbord, Unrecognized job security level \"%s\"", argv[i]);
PrintUsage(app_name);
return -1;
}
}
} else {
if (command_line.GetLength()) {
command_line += L' ';
}
command_line += argv[i];
}
}
if (!command_line.GetLength()) {
wprintf(L"\nAbord, No command line specified");
PrintUsage(app_name);
return -1;
}
wprintf(L"\nLaunching command line: \"%s\"\n", command_line.GetBuffer());
HANDLE job_handle;
DWORD err_code = sandbox::StartRestrictedProcessInJob(
command_line.GetBuffer(),
primary_level,
impersonation_level,
job_level,
&job_handle);
if (ERROR_SUCCESS != err_code) {
wprintf(L"\nAbord, Error %d while launching command line.", err_code);
return -1;
}
wprintf(L"\nPress any key to continue.");
while(!_kbhit()) {
Sleep(100);
}
::CloseHandle(job_handle);
return 0;
}
|