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 // Returns true if |path| starts with "\??\" and returns a path without that | |
39 // component. | |
40 bool IsNTPath(const base::string16& path, base::string16* trimmed_path ) { | |
41 if ((path.size() < sandbox::kNTPrefixLen) || | |
42 (0 != path.compare(0, sandbox::kNTPrefixLen, sandbox::kNTPrefix))) { | |
43 *trimmed_path = path; | |
44 return false; | |
45 } | |
46 | |
47 *trimmed_path = path.substr(sandbox::kNTPrefixLen); | |
48 return true; | |
49 } | |
50 | |
51 // Returns true if |path| starts with "\Device\" and returns a path without that | |
52 // component. | |
53 bool IsDevicePath(const base::string16& path, base::string16* trimmed_path ) { | |
cpu_(ooo_6.6-7.5)
2015/02/13 19:27:37
name: TrimDevicePath() ?
and above TrimNtPath() ?
rvargas (doing something else)
2015/02/13 19:43:05
Not so sure. I want the "main" function to be the
cpu_(ooo_6.6-7.5)
2015/02/17 21:59:45
Acknowledged.
| |
54 if ((path.size() < sandbox::kNTDevicePrefixLen) || | |
55 (0 != _wcsnicmp(path.c_str(), sandbox::kNTDevicePrefix, | |
56 sandbox::kNTDevicePrefixLen))) { | |
57 *trimmed_path = path; | |
58 return false; | |
59 } | |
60 | |
61 *trimmed_path = path.substr(sandbox::kNTDevicePrefixLen); | |
62 return true; | |
63 } | |
64 | |
65 bool StartsWithDriveLetter(const base::string16& path) { | |
66 if (path.size() < 3) | |
67 return false; | |
68 | |
69 if (path[1] != L':' || path[2] != L'\\') | |
70 return false; | |
71 | |
cpu_(ooo_6.6-7.5)
2015/02/13 19:27:37
I think the thing below is
http://en.cppreference.
rvargas (doing something else)
2015/02/13 19:43:05
That is locale dependent and a drive letter is not
cpu_(ooo_6.6-7.5)
2015/02/17 21:59:45
Acknowledged.
| |
72 return (path[0] >= 'a' && path[0] <= 'z') || | |
73 (path[0] >= 'A' && path[0] <= 'Z'); | |
74 } | |
75 | |
76 const wchar_t kNTDotPrefix[] = L"\\\\.\\"; | |
77 const size_t kNTDotPrefixLen = arraysize(kNTDotPrefix) - 1; | |
78 | |
79 // Removes "\\\\.\\" from the path. | |
80 void RemoveImpliedDevice(base::string16* path) { | |
81 if (0 == path->compare(0, kNTDotPrefixLen, kNTDotPrefix)) | |
82 *path = path->substr(kNTDotPrefixLen); | |
83 } | |
84 | |
37 } // namespace | 85 } // namespace |
38 | 86 |
39 namespace sandbox { | 87 namespace sandbox { |
40 | 88 |
41 // Returns true if the provided path points to a pipe. | 89 // Returns true if the provided path points to a pipe. |
42 bool IsPipe(const base::string16& path) { | 90 bool IsPipe(const base::string16& path) { |
43 base::string16 lower_path = base::StringToLowerASCII(path); | |
cpu_(ooo_6.6-7.5)
2015/02/13 19:27:37
without this helper do we still need to include st
| |
44 size_t start = 0; | 91 size_t start = 0; |
45 if (0 == lower_path.compare(0, sandbox::kNTPrefixLen, sandbox::kNTPrefix)) | 92 if (0 == path.compare(0, sandbox::kNTPrefixLen, sandbox::kNTPrefix)) |
46 start = sandbox::kNTPrefixLen; | 93 start = sandbox::kNTPrefixLen; |
47 | 94 |
48 const wchar_t kPipe[] = L"pipe\\"; | 95 const wchar_t kPipe[] = L"pipe\\"; |
49 return (0 == lower_path.compare(start, arraysize(kPipe) - 1, kPipe)); | 96 if (path.size() < start + arraysize(kPipe) - 1) |
97 return false; | |
98 | |
99 return (0 == _wcsnicmp(path.c_str() + start, kPipe, arraysize(kPipe) - 1)); | |
50 } | 100 } |
51 | 101 |
52 HKEY GetReservedKeyFromName(const base::string16& name) { | 102 HKEY GetReservedKeyFromName(const base::string16& name) { |
53 for (size_t i = 0; i < arraysize(kKnownKey); ++i) { | 103 for (size_t i = 0; i < arraysize(kKnownKey); ++i) { |
54 if (name == kKnownKey[i].name) | 104 if (name == kKnownKey[i].name) |
55 return kKnownKey[i].key; | 105 return kKnownKey[i].key; |
56 } | 106 } |
57 | 107 |
58 return NULL; | 108 return NULL; |
59 } | 109 } |
(...skipping 15 matching lines...) Expand all Loading... | |
75 return false; | 125 return false; |
76 | 126 |
77 *resolved_name += name.substr(wcslen(kKnownKey[i].name)); | 127 *resolved_name += name.substr(wcslen(kKnownKey[i].name)); |
78 return true; | 128 return true; |
79 } | 129 } |
80 } | 130 } |
81 | 131 |
82 return false; | 132 return false; |
83 } | 133 } |
84 | 134 |
135 // |full_path| can have any of the following forms: | |
136 // \??\c:\some\foo\bar | |
137 // \Device\HarddiskVolume0\some\foo\bar | |
138 // \??\HarddiskVolume0\some\foo\bar | |
139 // For OSes before Win7, anything but the first form will be rejected. | |
85 DWORD IsReparsePoint(const base::string16& full_path, bool* result) { | 140 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. | 141 // Check if it's a pipe. We can't query the attributes of a pipe. |
93 if (IsPipe(path)) { | 142 if (IsPipe(full_path)) { |
94 *result = FALSE; | 143 *result = FALSE; |
95 return ERROR_SUCCESS; | 144 return ERROR_SUCCESS; |
96 } | 145 } |
97 | 146 |
147 base::string16 path; | |
148 bool nt_path = IsNTPath(full_path, &path); | |
149 bool has_drive = StartsWithDriveLetter(path); | |
150 bool is_device_path = IsDevicePath(path, &path); | |
151 | |
152 if (!has_drive && !is_device_path && !nt_path) | |
153 return ERROR_INVALID_NAME; | |
154 | |
155 bool added_implied_device = false; | |
156 if (!has_drive) { | |
157 if (base::win::GetVersion() >= base::win::VERSION_WIN7) { | |
158 path = base::string16(kNTDotPrefix) + path; | |
159 added_implied_device = true; | |
160 } else { | |
161 return ERROR_INVALID_NAME; | |
162 } | |
163 } | |
164 | |
98 base::string16::size_type last_pos = base::string16::npos; | 165 base::string16::size_type last_pos = base::string16::npos; |
166 bool passed_once = false; | |
99 | 167 |
100 do { | 168 do { |
101 path = path.substr(0, last_pos); | 169 path = path.substr(0, last_pos); |
102 | 170 |
103 DWORD attributes = ::GetFileAttributes(path.c_str()); | 171 DWORD attributes = ::GetFileAttributes(path.c_str()); |
104 if (INVALID_FILE_ATTRIBUTES == attributes) { | 172 if (INVALID_FILE_ATTRIBUTES == attributes) { |
105 DWORD error = ::GetLastError(); | 173 DWORD error = ::GetLastError(); |
106 if (error != ERROR_FILE_NOT_FOUND && | 174 if (error != ERROR_FILE_NOT_FOUND && |
107 error != ERROR_PATH_NOT_FOUND && | 175 error != ERROR_PATH_NOT_FOUND && |
108 error != ERROR_INVALID_NAME) { | 176 error != ERROR_INVALID_NAME) { |
109 // Unexpected error. | 177 // Unexpected error. |
178 if (passed_once && added_implied_device && | |
179 path.rfind(L'\\') == kNTDotPrefixLen - 1) { | |
cpu_(ooo_6.6-7.5)
2015/02/17 21:59:45
I get nervous when I see && == and - in a line wit
rvargas (doing something else)
2015/02/17 22:59:29
Done.
| |
180 break; | |
181 } | |
110 NOTREACHED_NT(); | 182 NOTREACHED_NT(); |
111 return error; | 183 return error; |
112 } | 184 } |
113 } else if (FILE_ATTRIBUTE_REPARSE_POINT & attributes) { | 185 } else if (FILE_ATTRIBUTE_REPARSE_POINT & attributes) { |
114 // This is a reparse point. | 186 // This is a reparse point. |
115 *result = true; | 187 *result = true; |
116 return ERROR_SUCCESS; | 188 return ERROR_SUCCESS; |
117 } | 189 } |
118 | 190 |
191 passed_once = true; | |
119 last_pos = path.rfind(L'\\'); | 192 last_pos = path.rfind(L'\\'); |
120 } while (last_pos > 2); // Skip root dir. | 193 } while (last_pos > 2); // Skip root dir. |
121 | 194 |
122 *result = false; | 195 *result = false; |
123 return ERROR_SUCCESS; | 196 return ERROR_SUCCESS; |
124 } | 197 } |
125 | 198 |
126 // We get a |full_path| of the form \??\c:\some\foo\bar, and the name that | 199 // 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. | 200 // we'll get from |handle| will be \device\harddiskvolume1\some\foo\bar. |
128 bool SameObject(HANDLE handle, const wchar_t* full_path) { | 201 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. | 202 // Check if it's a pipe. |
133 if (IsPipe(path)) | 203 if (IsPipe(full_path)) |
134 return true; | 204 return true; |
cpu_(ooo_6.6-7.5)
2015/02/17 21:59:46
two objects are the same if one of them is a pipe.
rvargas (doing something else)
2015/02/17 22:59:29
Open to suggestions... Of course, the reasoning wa
| |
135 | 205 |
136 base::string16 actual_path; | 206 base::string16 actual_path; |
137 if (!GetPathFromHandle(handle, &actual_path)) | 207 if (!GetPathFromHandle(handle, &actual_path)) |
138 return false; | 208 return false; |
139 | 209 |
210 base::string16 path(full_path); | |
211 DCHECK_NT(!path.empty()); | |
212 | |
140 // This may end with a backslash. | 213 // This may end with a backslash. |
141 const wchar_t kBackslash = '\\'; | 214 const wchar_t kBackslash = '\\'; |
142 if (path[path.length() - 1] == kBackslash) | 215 if (path[path.length() - 1] == kBackslash) |
143 path = path.substr(0, path.length() - 1); | 216 path = path.substr(0, path.length() - 1); |
144 | 217 |
145 // Perfect match (case-insesitive check). | 218 // Perfect match (case-insesitive check). |
146 if (0 == _wcsicmp(actual_path.c_str(), path.c_str())) | 219 if (0 == _wcsicmp(actual_path.c_str(), path.c_str())) |
147 return true; | 220 return true; |
148 | 221 |
149 // Look for the drive letter. | 222 bool nt_path = IsNTPath(path, &path); |
150 size_t colon_pos = path.find(L':'); | 223 bool has_drive = StartsWithDriveLetter(path); |
151 if (colon_pos == 0 || colon_pos == base::string16::npos) | |
152 return false; | |
153 | 224 |
154 // Only one character for the drive. | 225 if (!has_drive && nt_path) { |
155 if (colon_pos > 1 && path[colon_pos - 2] != kBackslash) | 226 base::string16 simple_actual_path; |
227 if (!IsDevicePath(actual_path, &simple_actual_path)) | |
228 return false; | |
229 | |
230 // Perfect match (case-insesitive check). | |
231 return (0 == _wcsicmp(simple_actual_path.c_str(), path.c_str())); | |
232 } | |
233 | |
234 if (!has_drive) | |
156 return false; | 235 return false; |
157 | 236 |
158 // We only need 3 chars, but let's alloc a buffer for four. | 237 // We only need 3 chars, but let's alloc a buffer for four. |
159 wchar_t drive[4] = {0}; | 238 wchar_t drive[4] = {0}; |
160 wchar_t vol_name[MAX_PATH]; | 239 wchar_t vol_name[MAX_PATH]; |
161 memcpy(drive, &path[colon_pos - 1], 2 * sizeof(*drive)); | 240 memcpy(drive, &path[0], 2 * sizeof(*drive)); |
162 | 241 |
163 // We'll get a double null terminated string. | 242 // We'll get a double null terminated string. |
164 DWORD vol_length = ::QueryDosDeviceW(drive, vol_name, MAX_PATH); | 243 DWORD vol_length = ::QueryDosDeviceW(drive, vol_name, MAX_PATH); |
165 if (vol_length < 2 || vol_length == MAX_PATH) | 244 if (vol_length < 2 || vol_length == MAX_PATH) |
166 return false; | 245 return false; |
167 | 246 |
168 // Ignore the nulls at the end. | 247 // Ignore the nulls at the end. |
169 vol_length = static_cast<DWORD>(wcslen(vol_name)); | 248 vol_length = static_cast<DWORD>(wcslen(vol_name)); |
170 | 249 |
171 // The two paths should be the same length. | 250 // The two paths should be the same length. |
172 if (vol_length + path.size() - (colon_pos + 1) != actual_path.size()) | 251 if (vol_length + path.size() - 2 != actual_path.size()) |
173 return false; | 252 return false; |
174 | 253 |
175 // Check up to the drive letter. | 254 // Check up to the drive letter. |
176 if (0 != _wcsnicmp(actual_path.c_str(), vol_name, vol_length)) | 255 if (0 != _wcsnicmp(actual_path.c_str(), vol_name, vol_length)) |
177 return false; | 256 return false; |
178 | 257 |
179 // Check the path after the drive letter. | 258 // Check the path after the drive letter. |
180 if (0 != _wcsicmp(&actual_path[vol_length], &path[colon_pos + 1])) | 259 if (0 != _wcsicmp(&actual_path[vol_length], &path[2])) |
181 return false; | 260 return false; |
182 | 261 |
183 return true; | 262 return true; |
184 } | 263 } |
cpu_(ooo_6.6-7.5)
2015/02/17 21:59:45
if I might suggest, I think we need a helper here
rvargas (doing something else)
2015/02/17 22:59:29
Not sure the result is better, but done.
| |
185 | 264 |
265 // Paths like \Device\HarddiskVolume0\some\foo\bar are assumed to be already | |
266 // expanded. | |
186 bool ConvertToLongPath(const base::string16& short_path, | 267 bool ConvertToLongPath(const base::string16& short_path, |
187 base::string16* long_path) { | 268 base::string16* long_path) { |
188 // Check if the path is a NT path. | 269 // Check if the path is a NT path. |
189 bool is_nt_path = false; | 270 base::string16 path; |
190 base::string16 path = short_path; | 271 bool is_nt_path = IsNTPath(short_path, &path); |
191 if (0 == path.compare(0, kNTPrefixLen, kNTPrefix)) { | 272 if (IsDevicePath(path, &path)) |
192 path = path.substr(kNTPrefixLen); | 273 return false; |
cpu_(ooo_6.6-7.5)
2015/02/17 21:59:46
can lines 272 and 273 be moved above 269 ?
rvargas (doing something else)
2015/02/17 22:59:29
Done.
| |
193 is_nt_path = true; | 274 |
275 bool added_implied_device = false; | |
276 if (!StartsWithDriveLetter(path) && is_nt_path && | |
277 base::win::GetVersion() >= base::win::VERSION_WIN7) { | |
278 // This workaround does NOT work for before win7. The policy cannot have | |
279 // short names in that case. | |
280 path = base::string16(kNTDotPrefix) + path; | |
281 added_implied_device = true; | |
194 } | 282 } |
195 | 283 |
196 DWORD size = MAX_PATH; | 284 DWORD size = MAX_PATH; |
197 scoped_ptr<wchar_t[]> long_path_buf(new wchar_t[size]); | 285 scoped_ptr<wchar_t[]> long_path_buf(new wchar_t[size]); |
198 | 286 |
199 DWORD return_value = ::GetLongPathName(path.c_str(), long_path_buf.get(), | 287 DWORD return_value = ::GetLongPathName(path.c_str(), long_path_buf.get(), |
200 size); | 288 size); |
201 while (return_value >= size) { | 289 while (return_value >= size) { |
202 size *= 2; | 290 size *= 2; |
203 long_path_buf.reset(new wchar_t[size]); | 291 long_path_buf.reset(new wchar_t[size]); |
(...skipping 15 matching lines...) Expand all Loading... | |
219 return false; | 307 return false; |
220 | 308 |
221 // Ok, it worked. Let's reset the return value. | 309 // Ok, it worked. Let's reset the return value. |
222 path = begin + end; | 310 path = begin + end; |
223 return_value = 1; | 311 return_value = 1; |
224 } else if (0 != return_value) { | 312 } else if (0 != return_value) { |
225 path = long_path_buf.get(); | 313 path = long_path_buf.get(); |
226 } | 314 } |
227 | 315 |
228 if (return_value != 0) { | 316 if (return_value != 0) { |
317 if (added_implied_device) | |
318 RemoveImpliedDevice(&path); | |
319 | |
229 if (is_nt_path) { | 320 if (is_nt_path) { |
230 *long_path = kNTPrefix; | 321 *long_path = kNTPrefix; |
231 *long_path += path; | 322 *long_path += path; |
232 } else { | 323 } else { |
233 *long_path = path; | 324 *long_path = path; |
234 } | 325 } |
235 | 326 |
236 return true; | 327 return true; |
237 } | 328 } |
238 | 329 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 ::InterlockedCompareExchangePointer( | 405 ::InterlockedCompareExchangePointer( |
315 reinterpret_cast<PVOID volatile*>(&ntdll), ntdll_local, NULL); | 406 reinterpret_cast<PVOID volatile*>(&ntdll), ntdll_local, NULL); |
316 | 407 |
317 } | 408 } |
318 | 409 |
319 CHECK_NT(ntdll); | 410 CHECK_NT(ntdll); |
320 FARPROC* function_ptr = reinterpret_cast<FARPROC*>(ptr); | 411 FARPROC* function_ptr = reinterpret_cast<FARPROC*>(ptr); |
321 *function_ptr = ::GetProcAddress(ntdll, name); | 412 *function_ptr = ::GetProcAddress(ntdll, name); |
322 CHECK_NT(*function_ptr); | 413 CHECK_NT(*function_ptr); |
323 } | 414 } |
OLD | NEW |