Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: sandbox/win/src/win_utils.cc

Issue 909373004: Sandbox: Add support for file system policies that use implied device paths. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove stale header Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sandbox/win/src/win_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sandbox/win/src/win_utils.h" 5 #include "sandbox/win/src/win_utils.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 16 matching lines...) Expand all
27 { L"HKEY_CURRENT_USER", HKEY_CURRENT_USER }, 27 { L"HKEY_CURRENT_USER", HKEY_CURRENT_USER },
28 { L"HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE}, 28 { L"HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE},
29 { L"HKEY_USERS", HKEY_USERS}, 29 { L"HKEY_USERS", HKEY_USERS},
30 { L"HKEY_PERFORMANCE_DATA", HKEY_PERFORMANCE_DATA}, 30 { L"HKEY_PERFORMANCE_DATA", HKEY_PERFORMANCE_DATA},
31 { L"HKEY_PERFORMANCE_TEXT", HKEY_PERFORMANCE_TEXT}, 31 { L"HKEY_PERFORMANCE_TEXT", HKEY_PERFORMANCE_TEXT},
32 { L"HKEY_PERFORMANCE_NLSTEXT", HKEY_PERFORMANCE_NLSTEXT}, 32 { L"HKEY_PERFORMANCE_NLSTEXT", HKEY_PERFORMANCE_NLSTEXT},
33 { L"HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG}, 33 { L"HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG},
34 { L"HKEY_DYN_DATA", HKEY_DYN_DATA} 34 { L"HKEY_DYN_DATA", HKEY_DYN_DATA}
35 }; 35 };
36 36
37 // These functions perform case independent path comparisons.
38 bool EqualPath(const base::string16& first, const base::string16& second) {
39 return _wcsicmp(first.c_str(), second.c_str()) == 0;
40 }
41
42 bool EqualPath(const base::string16& first, size_t first_offset,
43 const base::string16& second, size_t second_offset) {
44 return _wcsicmp(first.c_str() + first_offset,
45 second.c_str() + second_offset) == 0;
46 }
47
48 bool EqualPath(const base::string16& first,
49 const wchar_t* second, size_t second_len) {
50 return _wcsnicmp(first.c_str(), second, second_len) == 0;
51 }
52
53 bool EqualPath(const base::string16& first, size_t first_offset,
54 const wchar_t* second, size_t second_len) {
55 return _wcsnicmp(first.c_str() + first_offset, second, second_len) == 0;
56 }
57
58 // Returns true if |path| starts with "\??\" and returns a path without that
59 // component.
60 bool IsNTPath(const base::string16& path, base::string16* trimmed_path ) {
61 if ((path.size() < sandbox::kNTPrefixLen) ||
62 (0 != path.compare(0, sandbox::kNTPrefixLen, sandbox::kNTPrefix))) {
63 *trimmed_path = path;
64 return false;
65 }
66
67 *trimmed_path = path.substr(sandbox::kNTPrefixLen);
68 return true;
69 }
70
71 // Returns true if |path| starts with "\Device\" and returns a path without that
72 // component.
73 bool IsDevicePath(const base::string16& path, base::string16* trimmed_path ) {
74 if ((path.size() < sandbox::kNTDevicePrefixLen) ||
75 (!EqualPath(path, sandbox::kNTDevicePrefix,
76 sandbox::kNTDevicePrefixLen))) {
77 *trimmed_path = path;
78 return false;
79 }
80
81 *trimmed_path = path.substr(sandbox::kNTDevicePrefixLen);
82 return true;
83 }
84
85 bool StartsWithDriveLetter(const base::string16& path) {
86 if (path.size() < 3)
87 return false;
88
89 if (path[1] != L':' || path[2] != L'\\')
90 return false;
91
92 return (path[0] >= 'a' && path[0] <= 'z') ||
93 (path[0] >= 'A' && path[0] <= 'Z');
94 }
95
96 const wchar_t kNTDotPrefix[] = L"\\\\.\\";
97 const size_t kNTDotPrefixLen = arraysize(kNTDotPrefix) - 1;
98
99 // Removes "\\\\.\\" from the path.
100 void RemoveImpliedDevice(base::string16* path) {
101 if (0 == path->compare(0, kNTDotPrefixLen, kNTDotPrefix))
102 *path = path->substr(kNTDotPrefixLen);
103 }
104
37 } // namespace 105 } // namespace
38 106
39 namespace sandbox { 107 namespace sandbox {
40 108
41 // Returns true if the provided path points to a pipe. 109 // Returns true if the provided path points to a pipe.
42 bool IsPipe(const base::string16& path) { 110 bool IsPipe(const base::string16& path) {
43 base::string16 lower_path = base::StringToLowerASCII(path);
44 size_t start = 0; 111 size_t start = 0;
45 if (0 == lower_path.compare(0, sandbox::kNTPrefixLen, sandbox::kNTPrefix)) 112 if (0 == path.compare(0, sandbox::kNTPrefixLen, sandbox::kNTPrefix))
46 start = sandbox::kNTPrefixLen; 113 start = sandbox::kNTPrefixLen;
47 114
48 const wchar_t kPipe[] = L"pipe\\"; 115 const wchar_t kPipe[] = L"pipe\\";
49 return (0 == lower_path.compare(start, arraysize(kPipe) - 1, kPipe)); 116 if (path.size() < start + arraysize(kPipe) - 1)
117 return false;
118
119 return EqualPath(path, start, kPipe, arraysize(kPipe) - 1);
50 } 120 }
51 121
52 HKEY GetReservedKeyFromName(const base::string16& name) { 122 HKEY GetReservedKeyFromName(const base::string16& name) {
53 for (size_t i = 0; i < arraysize(kKnownKey); ++i) { 123 for (size_t i = 0; i < arraysize(kKnownKey); ++i) {
54 if (name == kKnownKey[i].name) 124 if (name == kKnownKey[i].name)
55 return kKnownKey[i].key; 125 return kKnownKey[i].key;
56 } 126 }
57 127
58 return NULL; 128 return NULL;
59 } 129 }
(...skipping 15 matching lines...) Expand all
75 return false; 145 return false;
76 146
77 *resolved_name += name.substr(wcslen(kKnownKey[i].name)); 147 *resolved_name += name.substr(wcslen(kKnownKey[i].name));
78 return true; 148 return true;
79 } 149 }
80 } 150 }
81 151
82 return false; 152 return false;
83 } 153 }
84 154
155 // |full_path| can have any of the following forms:
156 // \??\c:\some\foo\bar
157 // \Device\HarddiskVolume0\some\foo\bar
158 // \??\HarddiskVolume0\some\foo\bar
85 DWORD IsReparsePoint(const base::string16& full_path, bool* result) { 159 DWORD IsReparsePoint(const base::string16& full_path, bool* result) {
86 base::string16 path = full_path;
87
88 // Remove the nt prefix.
89 if (0 == path.compare(0, kNTPrefixLen, kNTPrefix))
90 path = path.substr(kNTPrefixLen);
91
92 // Check if it's a pipe. We can't query the attributes of a pipe. 160 // Check if it's a pipe. We can't query the attributes of a pipe.
93 if (IsPipe(path)) { 161 if (IsPipe(full_path)) {
94 *result = FALSE; 162 *result = FALSE;
95 return ERROR_SUCCESS; 163 return ERROR_SUCCESS;
96 } 164 }
97 165
166 base::string16 path;
167 bool nt_path = IsNTPath(full_path, &path);
168 bool has_drive = StartsWithDriveLetter(path);
169 bool is_device_path = IsDevicePath(path, &path);
170
171 if (!has_drive && !is_device_path && !nt_path)
172 return ERROR_INVALID_NAME;
173
174 bool added_implied_device = false;
175 if (!has_drive) {
176 path = base::string16(kNTDotPrefix) + path;
177 added_implied_device = true;
178 }
179
98 base::string16::size_type last_pos = base::string16::npos; 180 base::string16::size_type last_pos = base::string16::npos;
181 bool passed_once = false;
99 182
100 do { 183 do {
101 path = path.substr(0, last_pos); 184 path = path.substr(0, last_pos);
102 185
103 DWORD attributes = ::GetFileAttributes(path.c_str()); 186 DWORD attributes = ::GetFileAttributes(path.c_str());
104 if (INVALID_FILE_ATTRIBUTES == attributes) { 187 if (INVALID_FILE_ATTRIBUTES == attributes) {
105 DWORD error = ::GetLastError(); 188 DWORD error = ::GetLastError();
106 if (error != ERROR_FILE_NOT_FOUND && 189 if (error != ERROR_FILE_NOT_FOUND &&
107 error != ERROR_PATH_NOT_FOUND && 190 error != ERROR_PATH_NOT_FOUND &&
108 error != ERROR_INVALID_NAME) { 191 error != ERROR_INVALID_NAME) {
109 // Unexpected error. 192 // Unexpected error.
193 if (passed_once && added_implied_device &&
194 (path.rfind(L'\\') == kNTDotPrefixLen - 1)) {
195 break;
196 }
110 NOTREACHED_NT(); 197 NOTREACHED_NT();
111 return error; 198 return error;
112 } 199 }
113 } else if (FILE_ATTRIBUTE_REPARSE_POINT & attributes) { 200 } else if (FILE_ATTRIBUTE_REPARSE_POINT & attributes) {
114 // This is a reparse point. 201 // This is a reparse point.
115 *result = true; 202 *result = true;
116 return ERROR_SUCCESS; 203 return ERROR_SUCCESS;
117 } 204 }
118 205
206 passed_once = true;
119 last_pos = path.rfind(L'\\'); 207 last_pos = path.rfind(L'\\');
120 } while (last_pos > 2); // Skip root dir. 208 } while (last_pos > 2); // Skip root dir.
121 209
122 *result = false; 210 *result = false;
123 return ERROR_SUCCESS; 211 return ERROR_SUCCESS;
124 } 212 }
125 213
126 // We get a |full_path| of the form \??\c:\some\foo\bar, and the name that 214 // We get a |full_path| of the forms accepted by IsReparsePoint(), and the name
127 // we'll get from |handle| will be \device\harddiskvolume1\some\foo\bar. 215 // we'll get from |handle| will be \device\harddiskvolume1\some\foo\bar.
128 bool SameObject(HANDLE handle, const wchar_t* full_path) { 216 bool SameObject(HANDLE handle, const wchar_t* full_path) {
129 base::string16 path(full_path);
130 DCHECK_NT(!path.empty());
131
132 // Check if it's a pipe. 217 // Check if it's a pipe.
133 if (IsPipe(path)) 218 if (IsPipe(full_path))
134 return true; 219 return true;
135 220
136 base::string16 actual_path; 221 base::string16 actual_path;
137 if (!GetPathFromHandle(handle, &actual_path)) 222 if (!GetPathFromHandle(handle, &actual_path))
138 return false; 223 return false;
139 224
225 base::string16 path(full_path);
226 DCHECK_NT(!path.empty());
227
140 // This may end with a backslash. 228 // This may end with a backslash.
141 const wchar_t kBackslash = '\\'; 229 const wchar_t kBackslash = '\\';
142 if (path[path.length() - 1] == kBackslash) 230 if (path[path.length() - 1] == kBackslash)
143 path = path.substr(0, path.length() - 1); 231 path = path.substr(0, path.length() - 1);
144 232
145 // Perfect match (case-insesitive check). 233 // Perfect match (case-insesitive check).
146 if (0 == _wcsicmp(actual_path.c_str(), path.c_str())) 234 if (EqualPath(actual_path, path))
147 return true; 235 return true;
148 236
149 // Look for the drive letter. 237 bool nt_path = IsNTPath(path, &path);
150 size_t colon_pos = path.find(L':'); 238 bool has_drive = StartsWithDriveLetter(path);
151 if (colon_pos == 0 || colon_pos == base::string16::npos)
152 return false;
153 239
154 // Only one character for the drive. 240 if (!has_drive && nt_path) {
155 if (colon_pos > 1 && path[colon_pos - 2] != kBackslash) 241 base::string16 simple_actual_path;
242 if (!IsDevicePath(actual_path, &simple_actual_path))
243 return false;
244
245 // Perfect match (case-insesitive check).
246 return (EqualPath(simple_actual_path, path));
247 }
248
249 if (!has_drive)
156 return false; 250 return false;
157 251
158 // We only need 3 chars, but let's alloc a buffer for four. 252 // We only need 3 chars, but let's alloc a buffer for four.
159 wchar_t drive[4] = {0}; 253 wchar_t drive[4] = {0};
160 wchar_t vol_name[MAX_PATH]; 254 wchar_t vol_name[MAX_PATH];
161 memcpy(drive, &path[colon_pos - 1], 2 * sizeof(*drive)); 255 memcpy(drive, &path[0], 2 * sizeof(*drive));
162 256
163 // We'll get a double null terminated string. 257 // We'll get a double null terminated string.
164 DWORD vol_length = ::QueryDosDeviceW(drive, vol_name, MAX_PATH); 258 DWORD vol_length = ::QueryDosDeviceW(drive, vol_name, MAX_PATH);
165 if (vol_length < 2 || vol_length == MAX_PATH) 259 if (vol_length < 2 || vol_length == MAX_PATH)
166 return false; 260 return false;
167 261
168 // Ignore the nulls at the end. 262 // Ignore the nulls at the end.
169 vol_length = static_cast<DWORD>(wcslen(vol_name)); 263 vol_length = static_cast<DWORD>(wcslen(vol_name));
170 264
171 // The two paths should be the same length. 265 // The two paths should be the same length.
172 if (vol_length + path.size() - (colon_pos + 1) != actual_path.size()) 266 if (vol_length + path.size() - 2 != actual_path.size())
173 return false; 267 return false;
174 268
175 // Check up to the drive letter. 269 // Check up to the drive letter.
176 if (0 != _wcsnicmp(actual_path.c_str(), vol_name, vol_length)) 270 if (!EqualPath(actual_path, vol_name, vol_length))
177 return false; 271 return false;
178 272
179 // Check the path after the drive letter. 273 // Check the path after the drive letter.
180 if (0 != _wcsicmp(&actual_path[vol_length], &path[colon_pos + 1])) 274 if (!EqualPath(actual_path, vol_length, path, 2))
181 return false; 275 return false;
182 276
183 return true; 277 return true;
184 } 278 }
185 279
280 // Paths like \Device\HarddiskVolume0\some\foo\bar are assumed to be already
281 // expanded.
186 bool ConvertToLongPath(const base::string16& short_path, 282 bool ConvertToLongPath(const base::string16& short_path,
187 base::string16* long_path) { 283 base::string16* long_path) {
188 // Check if the path is a NT path. 284 if (IsPipe(short_path)) {
189 bool is_nt_path = false; 285 // TODO(rvargas): Change the signature to use a single argument.
190 base::string16 path = short_path; 286 long_path->assign(short_path);
191 if (0 == path.compare(0, kNTPrefixLen, kNTPrefix)) { 287 return true;
192 path = path.substr(kNTPrefixLen); 288 }
193 is_nt_path = true; 289
290 base::string16 path;
291 if (IsDevicePath(short_path, &path))
292 return false;
293
294 bool is_nt_path = IsNTPath(path, &path);
295 bool added_implied_device = false;
296 if (!StartsWithDriveLetter(path) && is_nt_path) {
297 path = base::string16(kNTDotPrefix) + path;
298 added_implied_device = true;
194 } 299 }
195 300
196 DWORD size = MAX_PATH; 301 DWORD size = MAX_PATH;
197 scoped_ptr<wchar_t[]> long_path_buf(new wchar_t[size]); 302 scoped_ptr<wchar_t[]> long_path_buf(new wchar_t[size]);
198 303
199 DWORD return_value = ::GetLongPathName(path.c_str(), long_path_buf.get(), 304 DWORD return_value = ::GetLongPathName(path.c_str(), long_path_buf.get(),
200 size); 305 size);
201 while (return_value >= size) { 306 while (return_value >= size) {
202 size *= 2; 307 size *= 2;
203 long_path_buf.reset(new wchar_t[size]); 308 long_path_buf.reset(new wchar_t[size]);
(...skipping 15 matching lines...) Expand all
219 return false; 324 return false;
220 325
221 // Ok, it worked. Let's reset the return value. 326 // Ok, it worked. Let's reset the return value.
222 path = begin + end; 327 path = begin + end;
223 return_value = 1; 328 return_value = 1;
224 } else if (0 != return_value) { 329 } else if (0 != return_value) {
225 path = long_path_buf.get(); 330 path = long_path_buf.get();
226 } 331 }
227 332
228 if (return_value != 0) { 333 if (return_value != 0) {
334 if (added_implied_device)
335 RemoveImpliedDevice(&path);
336
229 if (is_nt_path) { 337 if (is_nt_path) {
230 *long_path = kNTPrefix; 338 *long_path = kNTPrefix;
231 *long_path += path; 339 *long_path += path;
232 } else { 340 } else {
233 *long_path = path; 341 *long_path = path;
234 } 342 }
235 343
236 return true; 344 return true;
237 } 345 }
238 346
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 ::InterlockedCompareExchangePointer( 422 ::InterlockedCompareExchangePointer(
315 reinterpret_cast<PVOID volatile*>(&ntdll), ntdll_local, NULL); 423 reinterpret_cast<PVOID volatile*>(&ntdll), ntdll_local, NULL);
316 424
317 } 425 }
318 426
319 CHECK_NT(ntdll); 427 CHECK_NT(ntdll);
320 FARPROC* function_ptr = reinterpret_cast<FARPROC*>(ptr); 428 FARPROC* function_ptr = reinterpret_cast<FARPROC*>(ptr);
321 *function_ptr = ::GetProcAddress(ntdll, name); 429 *function_ptr = ::GetProcAddress(ntdll, name);
322 CHECK_NT(*function_ptr); 430 CHECK_NT(*function_ptr);
323 } 431 }
OLDNEW
« no previous file with comments | « sandbox/win/src/win_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698