Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" |
| 11 #include "base/win/pe_image.h" | 11 #include "base/win/pe_image.h" |
| 12 #include "base/win/windows_version.h" | |
| 12 #include "sandbox/win/src/internal_types.h" | 13 #include "sandbox/win/src/internal_types.h" |
| 13 #include "sandbox/win/src/nt_internals.h" | 14 #include "sandbox/win/src/nt_internals.h" |
| 14 #include "sandbox/win/src/sandbox_nt_util.h" | 15 #include "sandbox/win/src/sandbox_nt_util.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Holds the information about a known registry key. | 19 // Holds the information about a known registry key. |
| 19 struct KnownReservedKey { | 20 struct KnownReservedKey { |
| 20 const wchar_t* name; | 21 const wchar_t* name; |
| 21 HKEY key; | 22 HKEY key; |
| 22 }; | 23 }; |
| 23 | 24 |
| 24 // Contains all the known registry key by name and by handle. | 25 // Contains all the known registry key by name and by handle. |
| 25 const KnownReservedKey kKnownKey[] = { | 26 const KnownReservedKey kKnownKey[] = { |
| 26 { L"HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT }, | 27 { L"HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT }, |
| 27 { L"HKEY_CURRENT_USER", HKEY_CURRENT_USER }, | 28 { L"HKEY_CURRENT_USER", HKEY_CURRENT_USER }, |
| 28 { L"HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE}, | 29 { L"HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE}, |
| 29 { L"HKEY_USERS", HKEY_USERS}, | 30 { L"HKEY_USERS", HKEY_USERS}, |
| 30 { L"HKEY_PERFORMANCE_DATA", HKEY_PERFORMANCE_DATA}, | 31 { L"HKEY_PERFORMANCE_DATA", HKEY_PERFORMANCE_DATA}, |
| 31 { L"HKEY_PERFORMANCE_TEXT", HKEY_PERFORMANCE_TEXT}, | 32 { L"HKEY_PERFORMANCE_TEXT", HKEY_PERFORMANCE_TEXT}, |
| 32 { L"HKEY_PERFORMANCE_NLSTEXT", HKEY_PERFORMANCE_NLSTEXT}, | 33 { L"HKEY_PERFORMANCE_NLSTEXT", HKEY_PERFORMANCE_NLSTEXT}, |
| 33 { L"HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG}, | 34 { L"HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG}, |
| 34 { L"HKEY_DYN_DATA", HKEY_DYN_DATA} | 35 { L"HKEY_DYN_DATA", HKEY_DYN_DATA} |
| 35 }; | 36 }; |
| 36 | 37 |
| 38 // These functions perform case independent path comparisons. | |
| 39 bool EqualPath(base::string16 first, base::string16 second) { | |
|
cpu_(ooo_6.6-7.5)
2015/02/17 23:55:27
by const ref? here and below?
| |
| 40 return _wcsicmp(first.c_str(), second.c_str()) == 0; | |
| 41 } | |
| 42 | |
| 43 bool EqualPath(base::string16 first, size_t first_offset, | |
| 44 base::string16 second, size_t second_offset) { | |
| 45 return _wcsicmp(first.c_str() + first_offset, | |
| 46 second.c_str() + second_offset) == 0; | |
| 47 } | |
| 48 | |
| 49 bool EqualPath(base::string16 first, const wchar_t* second, size_t second_len) { | |
| 50 return _wcsnicmp(first.c_str(), second, second_len) == 0; | |
| 51 } | |
| 52 | |
| 53 bool EqualPath(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 Loading... | |
| 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 | |
| 159 // For OSes before Win7, anything but the first form will be rejected. | |
| 85 DWORD IsReparsePoint(const base::string16& full_path, bool* result) { | 160 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. | 161 // Check if it's a pipe. We can't query the attributes of a pipe. |
| 93 if (IsPipe(path)) { | 162 if (IsPipe(full_path)) { |
| 94 *result = FALSE; | 163 *result = FALSE; |
| 95 return ERROR_SUCCESS; | 164 return ERROR_SUCCESS; |
| 96 } | 165 } |
| 97 | 166 |
| 167 base::string16 path; | |
| 168 bool nt_path = IsNTPath(full_path, &path); | |
| 169 bool has_drive = StartsWithDriveLetter(path); | |
| 170 bool is_device_path = IsDevicePath(path, &path); | |
| 171 | |
| 172 if (!has_drive && !is_device_path && !nt_path) | |
| 173 return ERROR_INVALID_NAME; | |
| 174 | |
| 175 bool added_implied_device = false; | |
| 176 if (!has_drive) { | |
| 177 if (base::win::GetVersion() >= base::win::VERSION_WIN7) { | |
| 178 path = base::string16(kNTDotPrefix) + path; | |
| 179 added_implied_device = true; | |
| 180 } else { | |
| 181 return ERROR_INVALID_NAME; | |
| 182 } | |
| 183 } | |
| 184 | |
| 98 base::string16::size_type last_pos = base::string16::npos; | 185 base::string16::size_type last_pos = base::string16::npos; |
| 186 bool passed_once = false; | |
| 99 | 187 |
| 100 do { | 188 do { |
| 101 path = path.substr(0, last_pos); | 189 path = path.substr(0, last_pos); |
| 102 | 190 |
| 103 DWORD attributes = ::GetFileAttributes(path.c_str()); | 191 DWORD attributes = ::GetFileAttributes(path.c_str()); |
| 104 if (INVALID_FILE_ATTRIBUTES == attributes) { | 192 if (INVALID_FILE_ATTRIBUTES == attributes) { |
| 105 DWORD error = ::GetLastError(); | 193 DWORD error = ::GetLastError(); |
| 106 if (error != ERROR_FILE_NOT_FOUND && | 194 if (error != ERROR_FILE_NOT_FOUND && |
| 107 error != ERROR_PATH_NOT_FOUND && | 195 error != ERROR_PATH_NOT_FOUND && |
| 108 error != ERROR_INVALID_NAME) { | 196 error != ERROR_INVALID_NAME) { |
| 109 // Unexpected error. | 197 // Unexpected error. |
| 198 if (passed_once && added_implied_device && | |
| 199 (path.rfind(L'\\') == kNTDotPrefixLen - 1)) { | |
| 200 break; | |
| 201 } | |
| 110 NOTREACHED_NT(); | 202 NOTREACHED_NT(); |
| 111 return error; | 203 return error; |
| 112 } | 204 } |
| 113 } else if (FILE_ATTRIBUTE_REPARSE_POINT & attributes) { | 205 } else if (FILE_ATTRIBUTE_REPARSE_POINT & attributes) { |
| 114 // This is a reparse point. | 206 // This is a reparse point. |
| 115 *result = true; | 207 *result = true; |
| 116 return ERROR_SUCCESS; | 208 return ERROR_SUCCESS; |
| 117 } | 209 } |
| 118 | 210 |
| 211 passed_once = true; | |
| 119 last_pos = path.rfind(L'\\'); | 212 last_pos = path.rfind(L'\\'); |
| 120 } while (last_pos > 2); // Skip root dir. | 213 } while (last_pos > 2); // Skip root dir. |
| 121 | 214 |
| 122 *result = false; | 215 *result = false; |
| 123 return ERROR_SUCCESS; | 216 return ERROR_SUCCESS; |
| 124 } | 217 } |
| 125 | 218 |
| 126 // We get a |full_path| of the form \??\c:\some\foo\bar, and the name that | 219 // 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. | 220 // we'll get from |handle| will be \device\harddiskvolume1\some\foo\bar. |
| 128 bool SameObject(HANDLE handle, const wchar_t* full_path) { | 221 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. | 222 // Check if it's a pipe. |
| 133 if (IsPipe(path)) | 223 if (IsPipe(full_path)) |
| 134 return true; | 224 return true; |
| 135 | 225 |
| 136 base::string16 actual_path; | 226 base::string16 actual_path; |
| 137 if (!GetPathFromHandle(handle, &actual_path)) | 227 if (!GetPathFromHandle(handle, &actual_path)) |
| 138 return false; | 228 return false; |
| 139 | 229 |
| 230 base::string16 path(full_path); | |
| 231 DCHECK_NT(!path.empty()); | |
| 232 | |
| 140 // This may end with a backslash. | 233 // This may end with a backslash. |
| 141 const wchar_t kBackslash = '\\'; | 234 const wchar_t kBackslash = '\\'; |
| 142 if (path[path.length() - 1] == kBackslash) | 235 if (path[path.length() - 1] == kBackslash) |
| 143 path = path.substr(0, path.length() - 1); | 236 path = path.substr(0, path.length() - 1); |
| 144 | 237 |
| 145 // Perfect match (case-insesitive check). | 238 // Perfect match (case-insesitive check). |
| 146 if (0 == _wcsicmp(actual_path.c_str(), path.c_str())) | 239 if (EqualPath(actual_path, path)) |
| 147 return true; | 240 return true; |
| 148 | 241 |
| 149 // Look for the drive letter. | 242 bool nt_path = IsNTPath(path, &path); |
| 150 size_t colon_pos = path.find(L':'); | 243 bool has_drive = StartsWithDriveLetter(path); |
| 151 if (colon_pos == 0 || colon_pos == base::string16::npos) | |
| 152 return false; | |
| 153 | 244 |
| 154 // Only one character for the drive. | 245 if (!has_drive && nt_path) { |
| 155 if (colon_pos > 1 && path[colon_pos - 2] != kBackslash) | 246 base::string16 simple_actual_path; |
| 247 if (!IsDevicePath(actual_path, &simple_actual_path)) | |
| 248 return false; | |
| 249 | |
| 250 // Perfect match (case-insesitive check). | |
| 251 return (EqualPath(simple_actual_path, path)); | |
| 252 } | |
| 253 | |
| 254 if (!has_drive) | |
| 156 return false; | 255 return false; |
| 157 | 256 |
| 158 // We only need 3 chars, but let's alloc a buffer for four. | 257 // We only need 3 chars, but let's alloc a buffer for four. |
| 159 wchar_t drive[4] = {0}; | 258 wchar_t drive[4] = {0}; |
| 160 wchar_t vol_name[MAX_PATH]; | 259 wchar_t vol_name[MAX_PATH]; |
| 161 memcpy(drive, &path[colon_pos - 1], 2 * sizeof(*drive)); | 260 memcpy(drive, &path[0], 2 * sizeof(*drive)); |
| 162 | 261 |
| 163 // We'll get a double null terminated string. | 262 // We'll get a double null terminated string. |
| 164 DWORD vol_length = ::QueryDosDeviceW(drive, vol_name, MAX_PATH); | 263 DWORD vol_length = ::QueryDosDeviceW(drive, vol_name, MAX_PATH); |
| 165 if (vol_length < 2 || vol_length == MAX_PATH) | 264 if (vol_length < 2 || vol_length == MAX_PATH) |
| 166 return false; | 265 return false; |
| 167 | 266 |
| 168 // Ignore the nulls at the end. | 267 // Ignore the nulls at the end. |
| 169 vol_length = static_cast<DWORD>(wcslen(vol_name)); | 268 vol_length = static_cast<DWORD>(wcslen(vol_name)); |
| 170 | 269 |
| 171 // The two paths should be the same length. | 270 // The two paths should be the same length. |
| 172 if (vol_length + path.size() - (colon_pos + 1) != actual_path.size()) | 271 if (vol_length + path.size() - 2 != actual_path.size()) |
| 173 return false; | 272 return false; |
| 174 | 273 |
| 175 // Check up to the drive letter. | 274 // Check up to the drive letter. |
| 176 if (0 != _wcsnicmp(actual_path.c_str(), vol_name, vol_length)) | 275 if (!EqualPath(actual_path, vol_name, vol_length)) |
| 177 return false; | 276 return false; |
| 178 | 277 |
| 179 // Check the path after the drive letter. | 278 // Check the path after the drive letter. |
| 180 if (0 != _wcsicmp(&actual_path[vol_length], &path[colon_pos + 1])) | 279 if (!EqualPath(actual_path, vol_length, path, 2)) |
| 181 return false; | 280 return false; |
| 182 | 281 |
| 183 return true; | 282 return true; |
| 184 } | 283 } |
| 185 | 284 |
| 285 // Paths like \Device\HarddiskVolume0\some\foo\bar are assumed to be already | |
| 286 // expanded. | |
| 186 bool ConvertToLongPath(const base::string16& short_path, | 287 bool ConvertToLongPath(const base::string16& short_path, |
| 187 base::string16* long_path) { | 288 base::string16* long_path) { |
| 188 // Check if the path is a NT path. | 289 base::string16 path; |
| 189 bool is_nt_path = false; | 290 if (IsDevicePath(short_path, &path)) |
| 190 base::string16 path = short_path; | 291 return false; |
| 191 if (0 == path.compare(0, kNTPrefixLen, kNTPrefix)) { | 292 |
| 192 path = path.substr(kNTPrefixLen); | 293 bool is_nt_path = IsNTPath(path, &path); |
| 193 is_nt_path = true; | 294 bool added_implied_device = false; |
| 295 if (!StartsWithDriveLetter(path) && is_nt_path && | |
| 296 base::win::GetVersion() >= base::win::VERSION_WIN7) { | |
| 297 // This workaround does NOT work for before win7. The policy cannot have | |
| 298 // short names in that case. | |
| 299 path = base::string16(kNTDotPrefix) + path; | |
| 300 added_implied_device = true; | |
| 194 } | 301 } |
| 195 | 302 |
| 196 DWORD size = MAX_PATH; | 303 DWORD size = MAX_PATH; |
| 197 scoped_ptr<wchar_t[]> long_path_buf(new wchar_t[size]); | 304 scoped_ptr<wchar_t[]> long_path_buf(new wchar_t[size]); |
| 198 | 305 |
| 199 DWORD return_value = ::GetLongPathName(path.c_str(), long_path_buf.get(), | 306 DWORD return_value = ::GetLongPathName(path.c_str(), long_path_buf.get(), |
| 200 size); | 307 size); |
| 201 while (return_value >= size) { | 308 while (return_value >= size) { |
| 202 size *= 2; | 309 size *= 2; |
| 203 long_path_buf.reset(new wchar_t[size]); | 310 long_path_buf.reset(new wchar_t[size]); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 219 return false; | 326 return false; |
| 220 | 327 |
| 221 // Ok, it worked. Let's reset the return value. | 328 // Ok, it worked. Let's reset the return value. |
| 222 path = begin + end; | 329 path = begin + end; |
| 223 return_value = 1; | 330 return_value = 1; |
| 224 } else if (0 != return_value) { | 331 } else if (0 != return_value) { |
| 225 path = long_path_buf.get(); | 332 path = long_path_buf.get(); |
| 226 } | 333 } |
| 227 | 334 |
| 228 if (return_value != 0) { | 335 if (return_value != 0) { |
| 336 if (added_implied_device) | |
| 337 RemoveImpliedDevice(&path); | |
| 338 | |
| 229 if (is_nt_path) { | 339 if (is_nt_path) { |
| 230 *long_path = kNTPrefix; | 340 *long_path = kNTPrefix; |
| 231 *long_path += path; | 341 *long_path += path; |
| 232 } else { | 342 } else { |
| 233 *long_path = path; | 343 *long_path = path; |
| 234 } | 344 } |
| 235 | 345 |
| 236 return true; | 346 return true; |
| 237 } | 347 } |
| 238 | 348 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 ::InterlockedCompareExchangePointer( | 424 ::InterlockedCompareExchangePointer( |
| 315 reinterpret_cast<PVOID volatile*>(&ntdll), ntdll_local, NULL); | 425 reinterpret_cast<PVOID volatile*>(&ntdll), ntdll_local, NULL); |
| 316 | 426 |
| 317 } | 427 } |
| 318 | 428 |
| 319 CHECK_NT(ntdll); | 429 CHECK_NT(ntdll); |
| 320 FARPROC* function_ptr = reinterpret_cast<FARPROC*>(ptr); | 430 FARPROC* function_ptr = reinterpret_cast<FARPROC*>(ptr); |
| 321 *function_ptr = ::GetProcAddress(ntdll, name); | 431 *function_ptr = ::GetProcAddress(ntdll, name); |
| 322 CHECK_NT(*function_ptr); | 432 CHECK_NT(*function_ptr); |
| 323 } | 433 } |
| OLD | NEW |