summaryrefslogtreecommitdiff
path: root/chrome_elf
diff options
context:
space:
mode:
authorTorne (Richard Coles) <torne@google.com>2014-03-21 14:25:57 +0000
committerTorne (Richard Coles) <torne@google.com>2014-03-21 14:25:57 +0000
commit23730a6e56a168d1879203e4b3819bb36e3d8f1f (patch)
tree4b72bdd217f9beda1d1b975e21c9d646a9664c48 /chrome_elf
parent6d6d338a36340632714f2aab9a266979b71c3442 (diff)
downloadchromium_org-23730a6e56a168d1879203e4b3819bb36e3d8f1f.tar.gz
Merge from Chromium at DEPS revision 258528
This commit was generated by merge_to_master.py. Change-Id: Id197cbdde2c2881915f096c0426c741416884783
Diffstat (limited to 'chrome_elf')
-rw-r--r--chrome_elf/blacklist/blacklist.cc136
-rw-r--r--chrome_elf/chrome_elf.gyp3
-rw-r--r--chrome_elf/create_file/chrome_create_file.cc20
-rw-r--r--chrome_elf/create_file/chrome_create_file_unittest.cc27
-rw-r--r--chrome_elf/ntdll_cache.cc53
-rw-r--r--chrome_elf/ntdll_cache.h6
-rw-r--r--chrome_elf/thunk_getter.cc142
-rw-r--r--chrome_elf/thunk_getter.h16
8 files changed, 264 insertions, 139 deletions
diff --git a/chrome_elf/blacklist/blacklist.cc b/chrome_elf/blacklist/blacklist.cc
index 23a06b4d34..19c7641379 100644
--- a/chrome_elf/blacklist/blacklist.cc
+++ b/chrome_elf/blacklist/blacklist.cc
@@ -11,9 +11,9 @@
#include "chrome_elf/blacklist/blacklist_interceptions.h"
#include "chrome_elf/chrome_elf_constants.h"
#include "chrome_elf/chrome_elf_util.h"
+#include "chrome_elf/thunk_getter.h"
#include "sandbox/win/src/interception_internal.h"
#include "sandbox/win/src/internal_types.h"
-#include "sandbox/win/src/sandbox_utils.h"
#include "sandbox/win/src/service_resolver.h"
// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
@@ -42,109 +42,10 @@ __declspec(allocate(".crthunk")) sandbox::ThunkData g_thunk_storage;
namespace {
-enum Version {
- VERSION_PRE_XP_SP2 = 0, // Not supported.
- VERSION_XP_SP2,
- VERSION_SERVER_2003, // Also includes XP Pro x64 and Server 2003 R2.
- VERSION_VISTA, // Also includes Windows Server 2008.
- VERSION_WIN7, // Also includes Windows Server 2008 R2.
- VERSION_WIN8, // Also includes Windows Server 2012.
- VERSION_WIN8_1,
- VERSION_WIN_LAST, // Indicates error condition.
-};
-
-// Whether a process is running under WOW64 (the wrapper that allows 32-bit
-// processes to run on 64-bit versions of Windows). This will return
-// WOW64_DISABLED for both "32-bit Chrome on 32-bit Windows" and "64-bit
-// Chrome on 64-bit Windows". WOW64_UNKNOWN means "an error occurred", e.g.
-// the process does not have sufficient access rights to determine this.
-enum WOW64Status {
- WOW64_DISABLED,
- WOW64_ENABLED,
- WOW64_UNKNOWN,
-};
-
// Record if the blacklist was successfully initialized so processes can easily
// determine if the blacklist is enabled for them.
bool g_blacklist_initialized = false;
-WOW64Status GetWOW64StatusForCurrentProcess() {
- typedef BOOL (WINAPI* IsWow64ProcessFunc)(HANDLE, PBOOL);
- IsWow64ProcessFunc is_wow64_process = reinterpret_cast<IsWow64ProcessFunc>(
- GetProcAddress(GetModuleHandle(L"kernel32.dll"), "IsWow64Process"));
- if (!is_wow64_process)
- return WOW64_DISABLED;
- BOOL is_wow64 = FALSE;
- if (!(*is_wow64_process)(GetCurrentProcess(), &is_wow64))
- return WOW64_UNKNOWN;
- return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED;
-}
-
-class OSInfo {
- public:
- struct VersionNumber {
- int major;
- int minor;
- int build;
- };
-
- struct ServicePack {
- int major;
- int minor;
- };
-
- OSInfo() {
- OSVERSIONINFOEX version_info = { sizeof(version_info) };
- GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info));
- version_number_.major = version_info.dwMajorVersion;
- version_number_.minor = version_info.dwMinorVersion;
- version_number_.build = version_info.dwBuildNumber;
- if ((version_number_.major == 5) && (version_number_.minor > 0)) {
- // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003.
- version_ = (version_number_.minor == 1) ? VERSION_XP_SP2 :
- VERSION_SERVER_2003;
- if (version_ == VERSION_XP_SP2 && version_info.wServicePackMajor < 2)
- version_ = VERSION_PRE_XP_SP2;
- } else if (version_number_.major == 6) {
- switch (version_number_.minor) {
- case 0:
- // Treat Windows Server 2008 the same as Windows Vista.
- version_ = VERSION_VISTA;
- break;
- case 1:
- // Treat Windows Server 2008 R2 the same as Windows 7.
- version_ = VERSION_WIN7;
- break;
- case 2:
- // Treat Windows Server 2012 the same as Windows 8.
- version_ = VERSION_WIN8;
- break;
- default:
- version_ = VERSION_WIN8_1;
- break;
- }
- } else if (version_number_.major > 6) {
- version_ = VERSION_WIN_LAST;
- } else {
- version_ = VERSION_PRE_XP_SP2;
- }
-
- service_pack_.major = version_info.wServicePackMajor;
- service_pack_.minor = version_info.wServicePackMinor;
- }
-
- Version version() const { return version_; }
- VersionNumber version_number() const { return version_number_; }
- ServicePack service_pack() const { return service_pack_; }
-
- private:
- Version version_;
- VersionNumber version_number_;
- ServicePack service_pack_;
-
- DISALLOW_COPY_AND_ASSIGN(OSInfo);
-};
-
// Record that the thunk setup completed succesfully and close the registry
// key handle since it is no longer needed.
void RecordSuccessfulThunkSetup(HKEY* key) {
@@ -343,17 +244,16 @@ bool Initialize(bool force) {
if (!force && !LeaveSetupBeacon())
return false;
- // Don't try blacklisting on unsupported OS versions.
- OSInfo os_info;
- if (os_info.version() <= VERSION_PRE_XP_SP2)
- return false;
-
- // Pseudo-handle, no need to close.
- HANDLE current_process = ::GetCurrentProcess();
-
// Tells the resolver to patch already patched functions.
const bool kRelaxed = true;
+ // Create a thunk via the appropriate ServiceResolver instance.
+ sandbox::ServiceResolverThunk* thunk = GetThunk(kRelaxed);
+
+ // Don't try blacklisting on unsupported OS versions.
+ if (!thunk)
+ return false;
+
// Record that we are starting the thunk setup code.
HKEY key = NULL;
DWORD disposition = 0;
@@ -378,26 +278,6 @@ bool Initialize(bool force) {
key = NULL;
}
- // Create a thunk via the appropriate ServiceResolver instance.
- sandbox::ServiceResolverThunk* thunk = NULL;
-#if defined(_WIN64)
- // Because Windows 8 and 8.1 have different stubs in 64-bit,
- // ServiceResolverThunk can handle all the formats in 64-bit (instead only
- // handling 1 like it does in 32-bit versions).
- thunk = new sandbox::ServiceResolverThunk(current_process, kRelaxed);
-#else
- if (GetWOW64StatusForCurrentProcess() == WOW64_ENABLED) {
- if (os_info.version() >= VERSION_WIN8)
- thunk = new sandbox::Wow64W8ResolverThunk(current_process, kRelaxed);
- else
- thunk = new sandbox::Wow64ResolverThunk(current_process, kRelaxed);
- } else if (os_info.version() >= VERSION_WIN8) {
- thunk = new sandbox::Win8ResolverThunk(current_process, kRelaxed);
- } else {
- thunk = new sandbox::ServiceResolverThunk(current_process, kRelaxed);
- }
-#endif
-
// Record that we have initialized the blacklist.
g_blacklist_initialized = true;
diff --git a/chrome_elf/chrome_elf.gyp b/chrome_elf/chrome_elf.gyp
index 21ab568ef1..7eeae2b4b7 100644
--- a/chrome_elf/chrome_elf.gyp
+++ b/chrome_elf/chrome_elf.gyp
@@ -125,6 +125,7 @@
'dependencies': [
'chrome_elf_common',
'../base/base.gyp:base_static',
+ '../sandbox/sandbox.gyp:sandbox',
],
},
{
@@ -151,6 +152,8 @@
'chrome_elf_types.h',
'chrome_elf_util.cc',
'chrome_elf_util.h',
+ 'thunk_getter.cc',
+ 'thunk_getter.h',
],
},
{
diff --git a/chrome_elf/create_file/chrome_create_file.cc b/chrome_elf/create_file/chrome_create_file.cc
index 59b15bd0f7..2db6f8d837 100644
--- a/chrome_elf/create_file/chrome_create_file.cc
+++ b/chrome_elf/create_file/chrome_create_file.cc
@@ -10,6 +10,7 @@
#include "chrome_elf/chrome_elf_constants.h"
#include "chrome_elf/chrome_elf_util.h"
#include "chrome_elf/ntdll_cache.h"
+#include "sandbox/win/src/interception_internal.h"
#include "sandbox/win/src/nt_internals.h"
namespace {
@@ -205,13 +206,22 @@ HANDLE CreateFileNTDLL(
if (flags_and_attributes & FILE_FLAG_OPEN_NO_RECALL)
flags |= FILE_OPEN_NO_RECALL;
- if (!g_ntdll_lookup["NtCreateFile"] ||
- !g_ntdll_lookup["RtlInitUnicodeString"]) {
+ if (!g_ntdll_lookup["RtlInitUnicodeString"])
return INVALID_HANDLE_VALUE;
- }
- NtCreateFileFunction create_file =
- reinterpret_cast<NtCreateFileFunction>(g_ntdll_lookup["NtCreateFile"]);
+ NtCreateFileFunction create_file;
+ char thunk_buffer[sizeof(sandbox::ThunkData)] = {};
+
+ if (g_nt_thunk_storage.data[0] != 0) {
+ create_file = reinterpret_cast<NtCreateFileFunction>(&g_nt_thunk_storage);
+ // Copy the thunk data to a buffer on the stack for debugging purposes.
+ memcpy(&thunk_buffer, &g_nt_thunk_storage, sizeof(sandbox::ThunkData));
+ } else if (g_ntdll_lookup["NtCreateFile"]) {
+ create_file =
+ reinterpret_cast<NtCreateFileFunction>(g_ntdll_lookup["NtCreateFile"]);
+ } else {
+ return INVALID_HANDLE_VALUE;
+ }
RtlInitUnicodeStringFunction init_unicode_string =
reinterpret_cast<RtlInitUnicodeStringFunction>(
diff --git a/chrome_elf/create_file/chrome_create_file_unittest.cc b/chrome_elf/create_file/chrome_create_file_unittest.cc
index e25b159fcb..5b77607368 100644
--- a/chrome_elf/create_file/chrome_create_file_unittest.cc
+++ b/chrome_elf/create_file/chrome_create_file_unittest.cc
@@ -20,6 +20,7 @@
#include "base/win/windows_version.h"
#include "chrome_elf/chrome_elf_constants.h"
#include "chrome_elf/ntdll_cache.h"
+#include "sandbox/win/src/interception_internal.h"
#include "sandbox/win/src/nt_internals.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
@@ -87,7 +88,22 @@ class ChromeCreateFileTest : public PlatformTest {
self_ = this;
}
+ void UnsetThunkStorage() {
+ DWORD old_protect = 0;
+ EXPECT_TRUE(::VirtualProtect(&g_nt_thunk_storage,
+ sizeof(g_nt_thunk_storage),
+ PAGE_EXECUTE_READWRITE,
+ &old_protect));
+ memset(&g_nt_thunk_storage, 0, sizeof(g_nt_thunk_storage));
+
+ EXPECT_TRUE(::VirtualProtect(&g_nt_thunk_storage,
+ sizeof(g_nt_thunk_storage),
+ PAGE_EXECUTE_READ,
+ &old_protect));
+ }
+
void RedirectNtCreateFileCalls() {
+ UnsetThunkStorage();
old_func_ptr_ =
reinterpret_cast<NtCreateFileFunction>(g_ntdll_lookup["NtCreateFile"]);
@@ -376,13 +392,14 @@ TEST_F(ChromeCreateFileTest, BypassTest) {
EXPECT_FALSE(ShouldBypass(local_junk_path.value().c_str()));
}
-TEST_F(ChromeCreateFileTest, NtCreateFileAddressCheck) {
- HMODULE ntdll_handle = ::GetModuleHandle(L"ntdll.dll");
- EXPECT_EQ(::GetProcAddress(ntdll_handle, "NtCreateFile"),
- g_ntdll_lookup["NtCreateFile"]);
+TEST_F(ChromeCreateFileTest, ReadWriteFromNtDll) {
+ UnsetThunkStorage();
+ base::FilePath file_name = temp_dir_.path().Append(L"some_file.txt");
+ DoWriteCheck(file_name, FILE_ATTRIBUTE_NORMAL, false);
+ DoReadCheck(file_name, FILE_ATTRIBUTE_NORMAL, false);
}
-TEST_F(ChromeCreateFileTest, ReadWriteFromNtDll) {
+TEST_F(ChromeCreateFileTest, ReadWriteFromThunk) {
base::FilePath file_name = temp_dir_.path().Append(L"some_file.txt");
DoWriteCheck(file_name, FILE_ATTRIBUTE_NORMAL, false);
DoReadCheck(file_name, FILE_ATTRIBUTE_NORMAL, false);
diff --git a/chrome_elf/ntdll_cache.cc b/chrome_elf/ntdll_cache.cc
index 2f4dbdf244..a0429a45ae 100644
--- a/chrome_elf/ntdll_cache.cc
+++ b/chrome_elf/ntdll_cache.cc
@@ -2,14 +2,28 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "chrome_elf/ntdll_cache.h"
+
#include <stdint.h>
#include <windows.h>
+#include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
#include "base/win/pe_image.h"
-#include "chrome_elf/ntdll_cache.h"
+#include "chrome_elf/thunk_getter.h"
+#include "sandbox/win/src/interception_internal.h"
+#include "sandbox/win/src/internal_types.h"
+#include "sandbox/win/src/service_resolver.h"
FunctionLookupTable g_ntdll_lookup;
+// Allocate storage for thunks in a page of this module to save on doing
+// an extra allocation at run time.
+#pragma section(".crthunk",read,execute)
+__declspec(allocate(".crthunk")) sandbox::ThunkData g_nt_thunk_storage;
+
+
+
namespace {
bool EnumExportsCallback(const base::win::PEImage& image,
@@ -35,4 +49,41 @@ void InitCache() {
base::win::PEImage ntdll_image(ntdll_handle);
ntdll_image.EnumExports(EnumExportsCallback, NULL);
+
+ // If ntdll has already been patched, don't copy it.
+ const bool kRelaxed = false;
+
+ // Create a thunk via the appropriate ServiceResolver instance.
+ scoped_ptr<sandbox::ServiceResolverThunk> thunk(GetThunk(kRelaxed));
+
+ if (thunk.get()) {
+ BYTE* thunk_storage = reinterpret_cast<BYTE*>(&g_nt_thunk_storage);
+
+ // Mark the thunk storage as readable and writeable, since we
+ // are ready to write to it.
+ DWORD old_protect = 0;
+ if (!::VirtualProtect(&g_nt_thunk_storage,
+ sizeof(g_nt_thunk_storage),
+ PAGE_EXECUTE_READWRITE,
+ &old_protect)) {
+ return;
+ }
+
+ size_t storage_used = 0;
+ NTSTATUS ret = thunk->CopyThunk(::GetModuleHandle(sandbox::kNtdllName),
+ "NtCreateFile",
+ thunk_storage,
+ sizeof(sandbox::ThunkData),
+ &storage_used);
+
+ if (!NT_SUCCESS(ret)) {
+ memset(&g_nt_thunk_storage, 0, sizeof(g_nt_thunk_storage));
+ }
+
+ // Ensure that the pointer to the old function can't be changed.
+ ::VirtualProtect(&g_nt_thunk_storage,
+ sizeof(g_nt_thunk_storage),
+ PAGE_EXECUTE_READ,
+ &old_protect);
+ }
}
diff --git a/chrome_elf/ntdll_cache.h b/chrome_elf/ntdll_cache.h
index 4608cf1936..5e4fb2bf7a 100644
--- a/chrome_elf/ntdll_cache.h
+++ b/chrome_elf/ntdll_cache.h
@@ -7,9 +7,15 @@
#include "chrome_elf/chrome_elf_types.h"
+namespace sandbox {
+struct ThunkData;
+}
+
// Caches the addresses of all functions exported by ntdll in |g_ntdll_lookup|.
void InitCache();
extern FunctionLookupTable g_ntdll_lookup;
+extern sandbox::ThunkData g_nt_thunk_storage;
+
#endif // CHROME_ELF_NTDLL_CACHE_H_
diff --git a/chrome_elf/thunk_getter.cc b/chrome_elf/thunk_getter.cc
new file mode 100644
index 0000000000..8421e5eecf
--- /dev/null
+++ b/chrome_elf/thunk_getter.cc
@@ -0,0 +1,142 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stdint.h>
+#include <windows.h>
+
+#include "base/basictypes.h"
+#include "sandbox/win/src/interception_internal.h"
+#include "sandbox/win/src/internal_types.h"
+#include "sandbox/win/src/sandbox_utils.h"
+#include "sandbox/win/src/service_resolver.h"
+
+namespace {
+enum Version {
+ VERSION_PRE_XP_SP2 = 0, // Not supported.
+ VERSION_XP_SP2,
+ VERSION_SERVER_2003, // Also includes XP Pro x64 and Server 2003 R2.
+ VERSION_VISTA, // Also includes Windows Server 2008.
+ VERSION_WIN7, // Also includes Windows Server 2008 R2.
+ VERSION_WIN8, // Also includes Windows Server 2012.
+ VERSION_WIN8_1,
+ VERSION_WIN_LAST, // Indicates error condition.
+};
+
+// Whether a process is running under WOW64 (the wrapper that allows 32-bit
+// processes to run on 64-bit versions of Windows). This will return
+// WOW64_DISABLED for both "32-bit Chrome on 32-bit Windows" and "64-bit
+// Chrome on 64-bit Windows". WOW64_UNKNOWN means "an error occurred", e.g.
+// the process does not have sufficient access rights to determine this.
+enum WOW64Status { WOW64_DISABLED, WOW64_ENABLED, WOW64_UNKNOWN, };
+
+WOW64Status GetWOW64StatusForCurrentProcess() {
+ typedef BOOL(WINAPI * IsWow64ProcessFunc)(HANDLE, PBOOL);
+ IsWow64ProcessFunc is_wow64_process = reinterpret_cast<IsWow64ProcessFunc>(
+ GetProcAddress(GetModuleHandle(L"kernel32.dll"), "IsWow64Process"));
+ if (!is_wow64_process)
+ return WOW64_DISABLED;
+ BOOL is_wow64 = FALSE;
+ if (!is_wow64_process(GetCurrentProcess(), &is_wow64))
+ return WOW64_UNKNOWN;
+ return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED;
+}
+
+class OSInfo {
+ public:
+ struct VersionNumber {
+ int major;
+ int minor;
+ int build;
+ };
+
+ struct ServicePack {
+ int major;
+ int minor;
+ };
+
+ OSInfo() {
+ OSVERSIONINFOEX version_info = {sizeof(version_info)};
+ GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info));
+ version_number_.major = version_info.dwMajorVersion;
+ version_number_.minor = version_info.dwMinorVersion;
+ version_number_.build = version_info.dwBuildNumber;
+ if ((version_number_.major == 5) && (version_number_.minor > 0)) {
+ // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003.
+ version_ =
+ (version_number_.minor == 1) ? VERSION_XP_SP2 : VERSION_SERVER_2003;
+ if (version_ == VERSION_XP_SP2 && version_info.wServicePackMajor < 2)
+ version_ = VERSION_PRE_XP_SP2;
+ } else if (version_number_.major == 6) {
+ switch (version_number_.minor) {
+ case 0:
+ // Treat Windows Server 2008 the same as Windows Vista.
+ version_ = VERSION_VISTA;
+ break;
+ case 1:
+ // Treat Windows Server 2008 R2 the same as Windows 7.
+ version_ = VERSION_WIN7;
+ break;
+ case 2:
+ // Treat Windows Server 2012 the same as Windows 8.
+ version_ = VERSION_WIN8;
+ break;
+ default:
+ version_ = VERSION_WIN8_1;
+ break;
+ }
+ } else if (version_number_.major > 6) {
+ version_ = VERSION_WIN_LAST;
+ } else {
+ version_ = VERSION_PRE_XP_SP2;
+ }
+
+ service_pack_.major = version_info.wServicePackMajor;
+ service_pack_.minor = version_info.wServicePackMinor;
+ }
+
+ Version version() const { return version_; }
+ VersionNumber version_number() const { return version_number_; }
+ ServicePack service_pack() const { return service_pack_; }
+
+ private:
+ Version version_;
+ VersionNumber version_number_;
+ ServicePack service_pack_;
+
+ DISALLOW_COPY_AND_ASSIGN(OSInfo);
+};
+
+} // namespace
+
+sandbox::ServiceResolverThunk* GetThunk(bool relaxed) {
+ // Create a thunk via the appropriate ServiceResolver instance.
+ sandbox::ServiceResolverThunk* thunk = NULL;
+
+ // No thunks for unsupported OS versions.
+ OSInfo os_info;
+ if (os_info.version() <= VERSION_PRE_XP_SP2)
+ return thunk;
+
+ // Pseudo-handle, no need to close.
+ HANDLE current_process = ::GetCurrentProcess();
+
+#if defined(_WIN64)
+ // ServiceResolverThunk can handle all the formats in 64-bit (instead only
+ // handling one like it does in 32-bit versions).
+ thunk = new sandbox::ServiceResolverThunk(current_process, relaxed);
+#else
+ if (GetWOW64StatusForCurrentProcess() == WOW64_ENABLED) {
+ if (os_info.version() >= VERSION_WIN8)
+ thunk = new sandbox::Wow64W8ResolverThunk(current_process, relaxed);
+ else
+ thunk = new sandbox::Wow64ResolverThunk(current_process, relaxed);
+ } else if (os_info.version() >= VERSION_WIN8) {
+ thunk = new sandbox::Win8ResolverThunk(current_process, relaxed);
+ } else {
+ thunk = new sandbox::ServiceResolverThunk(current_process, relaxed);
+ }
+#endif
+
+ return thunk;
+}
diff --git a/chrome_elf/thunk_getter.h b/chrome_elf/thunk_getter.h
new file mode 100644
index 0000000000..5bc20fe81e
--- /dev/null
+++ b/chrome_elf/thunk_getter.h
@@ -0,0 +1,16 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_ELF_THUNK_GETTER_H_
+#define CHROME_ELF_THUNK_GETTER_H_
+
+namespace sandbox {
+class ServiceResolverThunk;
+}
+
+// Creates a |ServiceResolverThunk| based on the OS version. Ownership of the
+// resulting thunk is passed to the caller.
+sandbox::ServiceResolverThunk* GetThunk(bool relaxed);
+
+#endif // CHROME_ELF_THUNK_GETTER_H_