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

Unified 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: Created 5 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sandbox/win/src/win_utils.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sandbox/win/src/win_utils.cc
diff --git a/sandbox/win/src/win_utils.cc b/sandbox/win/src/win_utils.cc
index 12f818926b4bdb642efc658644b6b9e75ca19e22..6b0f747e3a06e9d7f90f5bc4fd9b5c89d25b49c3 100644
--- a/sandbox/win/src/win_utils.cc
+++ b/sandbox/win/src/win_utils.cc
@@ -9,6 +9,7 @@
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_util.h"
#include "base/win/pe_image.h"
+#include "base/win/windows_version.h"
#include "sandbox/win/src/internal_types.h"
#include "sandbox/win/src/nt_internals.h"
#include "sandbox/win/src/sandbox_nt_util.h"
@@ -34,19 +35,68 @@ const KnownReservedKey kKnownKey[] = {
{ L"HKEY_DYN_DATA", HKEY_DYN_DATA}
};
+// Returns true if |path| starts with "\??\" and returns a path without that
+// component.
+bool IsNTPath(const base::string16& path, base::string16* trimmed_path ) {
+ if ((path.size() < sandbox::kNTPrefixLen) ||
+ (0 != path.compare(0, sandbox::kNTPrefixLen, sandbox::kNTPrefix))) {
+ *trimmed_path = path;
+ return false;
+ }
+
+ *trimmed_path = path.substr(sandbox::kNTPrefixLen);
+ return true;
+}
+
+// Returns true if |path| starts with "\Device\" and returns a path without that
+// component.
+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.
+ if ((path.size() < sandbox::kNTDevicePrefixLen) ||
+ (0 != _wcsnicmp(path.c_str(), sandbox::kNTDevicePrefix,
+ sandbox::kNTDevicePrefixLen))) {
+ *trimmed_path = path;
+ return false;
+ }
+
+ *trimmed_path = path.substr(sandbox::kNTDevicePrefixLen);
+ return true;
+}
+
+bool StartsWithDriveLetter(const base::string16& path) {
+ if (path.size() < 3)
+ return false;
+
+ if (path[1] != L':' || path[2] != L'\\')
+ return false;
+
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.
+ return (path[0] >= 'a' && path[0] <= 'z') ||
+ (path[0] >= 'A' && path[0] <= 'Z');
+}
+
+const wchar_t kNTDotPrefix[] = L"\\\\.\\";
+const size_t kNTDotPrefixLen = arraysize(kNTDotPrefix) - 1;
+
+// Removes "\\\\.\\" from the path.
+void RemoveImpliedDevice(base::string16* path) {
+ if (0 == path->compare(0, kNTDotPrefixLen, kNTDotPrefix))
+ *path = path->substr(kNTDotPrefixLen);
+}
+
} // namespace
namespace sandbox {
// Returns true if the provided path points to a pipe.
bool IsPipe(const base::string16& path) {
- 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
size_t start = 0;
- if (0 == lower_path.compare(0, sandbox::kNTPrefixLen, sandbox::kNTPrefix))
+ if (0 == path.compare(0, sandbox::kNTPrefixLen, sandbox::kNTPrefix))
start = sandbox::kNTPrefixLen;
const wchar_t kPipe[] = L"pipe\\";
- return (0 == lower_path.compare(start, arraysize(kPipe) - 1, kPipe));
+ if (path.size() < start + arraysize(kPipe) - 1)
+ return false;
+
+ return (0 == _wcsnicmp(path.c_str() + start, kPipe, arraysize(kPipe) - 1));
}
HKEY GetReservedKeyFromName(const base::string16& name) {
@@ -82,20 +132,38 @@ bool ResolveRegistryName(base::string16 name, base::string16* resolved_name) {
return false;
}
+// |full_path| can have any of the following forms:
+// \??\c:\some\foo\bar
+// \Device\HarddiskVolume0\some\foo\bar
+// \??\HarddiskVolume0\some\foo\bar
+// For OSes before Win7, anything but the first form will be rejected.
DWORD IsReparsePoint(const base::string16& full_path, bool* result) {
- base::string16 path = full_path;
-
- // Remove the nt prefix.
- if (0 == path.compare(0, kNTPrefixLen, kNTPrefix))
- path = path.substr(kNTPrefixLen);
-
// Check if it's a pipe. We can't query the attributes of a pipe.
- if (IsPipe(path)) {
+ if (IsPipe(full_path)) {
*result = FALSE;
return ERROR_SUCCESS;
}
+ base::string16 path;
+ bool nt_path = IsNTPath(full_path, &path);
+ bool has_drive = StartsWithDriveLetter(path);
+ bool is_device_path = IsDevicePath(path, &path);
+
+ if (!has_drive && !is_device_path && !nt_path)
+ return ERROR_INVALID_NAME;
+
+ bool added_implied_device = false;
+ if (!has_drive) {
+ if (base::win::GetVersion() >= base::win::VERSION_WIN7) {
+ path = base::string16(kNTDotPrefix) + path;
+ added_implied_device = true;
+ } else {
+ return ERROR_INVALID_NAME;
+ }
+ }
+
base::string16::size_type last_pos = base::string16::npos;
+ bool passed_once = false;
do {
path = path.substr(0, last_pos);
@@ -107,6 +175,10 @@ DWORD IsReparsePoint(const base::string16& full_path, bool* result) {
error != ERROR_PATH_NOT_FOUND &&
error != ERROR_INVALID_NAME) {
// Unexpected error.
+ if (passed_once && added_implied_device &&
+ 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.
+ break;
+ }
NOTREACHED_NT();
return error;
}
@@ -116,6 +188,7 @@ DWORD IsReparsePoint(const base::string16& full_path, bool* result) {
return ERROR_SUCCESS;
}
+ passed_once = true;
last_pos = path.rfind(L'\\');
} while (last_pos > 2); // Skip root dir.
@@ -123,20 +196,20 @@ DWORD IsReparsePoint(const base::string16& full_path, bool* result) {
return ERROR_SUCCESS;
}
-// We get a |full_path| of the form \??\c:\some\foo\bar, and the name that
+// We get a |full_path| of the forms accepted by IsReparsePoint(), and the name
// we'll get from |handle| will be \device\harddiskvolume1\some\foo\bar.
bool SameObject(HANDLE handle, const wchar_t* full_path) {
- base::string16 path(full_path);
- DCHECK_NT(!path.empty());
-
// Check if it's a pipe.
- if (IsPipe(path))
+ if (IsPipe(full_path))
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
base::string16 actual_path;
if (!GetPathFromHandle(handle, &actual_path))
return false;
+ base::string16 path(full_path);
+ DCHECK_NT(!path.empty());
+
// This may end with a backslash.
const wchar_t kBackslash = '\\';
if (path[path.length() - 1] == kBackslash)
@@ -146,19 +219,25 @@ bool SameObject(HANDLE handle, const wchar_t* full_path) {
if (0 == _wcsicmp(actual_path.c_str(), path.c_str()))
return true;
- // Look for the drive letter.
- size_t colon_pos = path.find(L':');
- if (colon_pos == 0 || colon_pos == base::string16::npos)
- return false;
+ bool nt_path = IsNTPath(path, &path);
+ bool has_drive = StartsWithDriveLetter(path);
+
+ if (!has_drive && nt_path) {
+ base::string16 simple_actual_path;
+ if (!IsDevicePath(actual_path, &simple_actual_path))
+ return false;
- // Only one character for the drive.
- if (colon_pos > 1 && path[colon_pos - 2] != kBackslash)
+ // Perfect match (case-insesitive check).
+ return (0 == _wcsicmp(simple_actual_path.c_str(), path.c_str()));
+ }
+
+ if (!has_drive)
return false;
// We only need 3 chars, but let's alloc a buffer for four.
wchar_t drive[4] = {0};
wchar_t vol_name[MAX_PATH];
- memcpy(drive, &path[colon_pos - 1], 2 * sizeof(*drive));
+ memcpy(drive, &path[0], 2 * sizeof(*drive));
// We'll get a double null terminated string.
DWORD vol_length = ::QueryDosDeviceW(drive, vol_name, MAX_PATH);
@@ -169,7 +248,7 @@ bool SameObject(HANDLE handle, const wchar_t* full_path) {
vol_length = static_cast<DWORD>(wcslen(vol_name));
// The two paths should be the same length.
- if (vol_length + path.size() - (colon_pos + 1) != actual_path.size())
+ if (vol_length + path.size() - 2 != actual_path.size())
return false;
// Check up to the drive letter.
@@ -177,20 +256,29 @@ bool SameObject(HANDLE handle, const wchar_t* full_path) {
return false;
// Check the path after the drive letter.
- if (0 != _wcsicmp(&actual_path[vol_length], &path[colon_pos + 1]))
+ if (0 != _wcsicmp(&actual_path[vol_length], &path[2]))
return false;
return true;
}
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.
+// Paths like \Device\HarddiskVolume0\some\foo\bar are assumed to be already
+// expanded.
bool ConvertToLongPath(const base::string16& short_path,
base::string16* long_path) {
// Check if the path is a NT path.
- bool is_nt_path = false;
- base::string16 path = short_path;
- if (0 == path.compare(0, kNTPrefixLen, kNTPrefix)) {
- path = path.substr(kNTPrefixLen);
- is_nt_path = true;
+ base::string16 path;
+ bool is_nt_path = IsNTPath(short_path, &path);
+ if (IsDevicePath(path, &path))
+ 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.
+
+ bool added_implied_device = false;
+ if (!StartsWithDriveLetter(path) && is_nt_path &&
+ base::win::GetVersion() >= base::win::VERSION_WIN7) {
+ // This workaround does NOT work for before win7. The policy cannot have
+ // short names in that case.
+ path = base::string16(kNTDotPrefix) + path;
+ added_implied_device = true;
}
DWORD size = MAX_PATH;
@@ -226,6 +314,9 @@ bool ConvertToLongPath(const base::string16& short_path,
}
if (return_value != 0) {
+ if (added_implied_device)
+ RemoveImpliedDevice(&path);
+
if (is_nt_path) {
*long_path = kNTPrefix;
*long_path += path;
« 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