rvargas@google.com | 4f1f3d0 | 2010-01-27 19:20:42 | [diff] [blame] | 1 | // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 4 | |
nsylvain@chromium.org | 6e9d750 | 2008-09-28 01:20:20 | [diff] [blame] | 5 | #ifndef SANDBOX_SRC_WIN_UTILS_H_ |
| 6 | #define SANDBOX_SRC_WIN_UTILS_H_ |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 7 | |
| 8 | #include <windows.h> |
avi | 74a1875f | 2015-12-22 02:10:03 | [diff] [blame] | 9 | #include <stddef.h> |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 10 | #include <string> |
brettw@chromium.org | 5672e5d | 2013-12-12 21:36:50 | [diff] [blame] | 11 | |
avi | 74a1875f | 2015-12-22 02:10:03 | [diff] [blame] | 12 | #include "base/macros.h" |
brettw@chromium.org | 5672e5d | 2013-12-12 21:36:50 | [diff] [blame] | 13 | #include "base/strings/string16.h" |
forshaw | eca72db | 2016-05-05 20:11:14 | [diff] [blame] | 14 | #include "sandbox/win/src/nt_internals.h" |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 15 | |
| 16 | namespace sandbox { |
| 17 | |
| 18 | // Prefix for path used by NT calls. |
| 19 | const wchar_t kNTPrefix[] = L"\\??\\"; |
| 20 | const size_t kNTPrefixLen = arraysize(kNTPrefix) - 1; |
| 21 | |
rvargas | 0e49d02 | 2015-04-01 19:20:01 | [diff] [blame] | 22 | const wchar_t kNTDevicePrefix[] = L"\\Device\\"; |
| 23 | const size_t kNTDevicePrefixLen = arraysize(kNTDevicePrefix) - 1; |
cpu@chromium.org | a7cab94 | 2010-08-07 03:10:12 | [diff] [blame] | 24 | |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 25 | // Automatically acquires and releases a lock when the object is |
| 26 | // is destroyed. |
| 27 | class AutoLock { |
| 28 | public: |
| 29 | // Acquires the lock. |
| 30 | explicit AutoLock(CRITICAL_SECTION *lock) : lock_(lock) { |
| 31 | ::EnterCriticalSection(lock); |
| 32 | }; |
| 33 | |
| 34 | // Releases the lock; |
| 35 | ~AutoLock() { |
| 36 | ::LeaveCriticalSection(lock_); |
| 37 | }; |
| 38 | |
| 39 | private: |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 40 | CRITICAL_SECTION *lock_; |
rvargas@google.com | 4f1f3d0 | 2010-01-27 19:20:42 | [diff] [blame] | 41 | DISALLOW_IMPLICIT_CONSTRUCTORS(AutoLock); |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | // Basic implementation of a singleton which calls the destructor |
| 45 | // when the exe is shutting down or the DLL is being unloaded. |
| 46 | template <typename Derived> |
| 47 | class SingletonBase { |
| 48 | public: |
| 49 | static Derived* GetInstance() { |
| 50 | static Derived* instance = NULL; |
| 51 | if (NULL == instance) { |
| 52 | instance = new Derived(); |
| 53 | // Microsoft CRT extension. In an exe this this called after |
| 54 | // winmain returns, in a dll is called in DLL_PROCESS_DETACH |
| 55 | _onexit(OnExit); |
| 56 | } |
| 57 | return instance; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | // this is the function that gets called by the CRT when the |
| 62 | // process is shutting down. |
| 63 | static int __cdecl OnExit() { |
| 64 | delete GetInstance(); |
| 65 | return 0; |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | // Convert a short path (C:\path~1 or \\??\\c:\path~1) to the long version of |
| 70 | // the path. If the path is not a valid filesystem path, the function returns |
rvargas | b8fd739 | 2015-07-27 23:53:47 | [diff] [blame] | 71 | // false and argument is not modified. |
Penny MacNeil | 0a29c32 | 2017-08-09 17:16:41 | [diff] [blame] | 72 | // - If passing in a short native device path (\Device\HarddiskVolumeX\path~1), |
| 73 | // a drive letter string (c:\) must also be provided. |
| 74 | bool ConvertToLongPath(base::string16* path, |
| 75 | const base::string16* drive_letter = nullptr); |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 76 | |
rvargas | b8fd739 | 2015-07-27 23:53:47 | [diff] [blame] | 77 | // Returns ERROR_SUCCESS if the path contains a reparse point, |
| 78 | // ERROR_NOT_A_REPARSE_POINT if there's no reparse point in this path, or an |
| 79 | // error code when the function fails. |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 80 | // This function is not smart. It looks for each element in the path and |
| 81 | // returns true if any of them is a reparse point. |
rvargas | b8fd739 | 2015-07-27 23:53:47 | [diff] [blame] | 82 | DWORD IsReparsePoint(const base::string16& full_path); |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 83 | |
rvargas@google.com | 4f1f3d0 | 2010-01-27 19:20:42 | [diff] [blame] | 84 | // Returns true if the handle corresponds to the object pointed by this path. |
| 85 | bool SameObject(HANDLE handle, const wchar_t* full_path); |
| 86 | |
cpu@chromium.org | a7cab94 | 2010-08-07 03:10:12 | [diff] [blame] | 87 | // Resolves a handle to an nt path. Returns true if the handle can be resolved. |
brettw@chromium.org | 5672e5d | 2013-12-12 21:36:50 | [diff] [blame] | 88 | bool GetPathFromHandle(HANDLE handle, base::string16* path); |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 89 | |
cpu@chromium.org | a7cab94 | 2010-08-07 03:10:12 | [diff] [blame] | 90 | // Resolves a win32 path to an nt path using GetPathFromHandle. The path must |
| 91 | // exist. Returs true if the translation was succesful. |
brettw@chromium.org | 5672e5d | 2013-12-12 21:36:50 | [diff] [blame] | 92 | bool GetNtPathFromWin32Path(const base::string16& path, |
| 93 | base::string16* nt_path); |
cpu@chromium.org | a7cab94 | 2010-08-07 03:10:12 | [diff] [blame] | 94 | |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 95 | // Translates a reserved key name to its handle. |
| 96 | // For example "HKEY_LOCAL_MACHINE" returns HKEY_LOCAL_MACHINE. |
| 97 | // Returns NULL if the name does not represent any reserved key name. |
brettw@chromium.org | 5672e5d | 2013-12-12 21:36:50 | [diff] [blame] | 98 | HKEY GetReservedKeyFromName(const base::string16& name); |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 99 | |
| 100 | // Resolves a user-readable registry path to a system-readable registry path. |
| 101 | // For example, HKEY_LOCAL_MACHINE\\Software\\microsoft is translated to |
| 102 | // \\registry\\machine\\software\\microsoft. Returns false if the path |
| 103 | // cannot be resolved. |
brettw@chromium.org | 5672e5d | 2013-12-12 21:36:50 | [diff] [blame] | 104 | bool ResolveRegistryName(base::string16 name, base::string16* resolved_name); |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 105 | |
rvargas@google.com | 0483cf18 | 2010-02-11 22:20:31 | [diff] [blame] | 106 | // Writes |length| bytes from the provided |buffer| into the address space of |
| 107 | // |child_process|, at the specified |address|, preserving the original write |
| 108 | // protection attributes. Returns true on success. |
| 109 | bool WriteProtectedChildMemory(HANDLE child_process, void* address, |
| 110 | const void* buffer, size_t length); |
| 111 | |
forshaw | 14976c1 | 2014-12-18 11:54:31 | [diff] [blame] | 112 | // Returns true if the provided path points to a pipe. |
| 113 | bool IsPipe(const base::string16& path); |
| 114 | |
forshaw | eca72db | 2016-05-05 20:11:14 | [diff] [blame] | 115 | // Converts a NTSTATUS code to a Win32 error code. |
| 116 | DWORD GetLastErrorFromNtStatus(NTSTATUS status); |
| 117 | |
forshaw | cd115ae | 2016-09-08 20:25:46 | [diff] [blame] | 118 | // Returns the address of the main exe module in memory taking in account |
James Forshaw | f398005 | 2017-08-15 09:29:46 | [diff] [blame^] | 119 | // address space layout randomization. This uses the process' PEB to extract |
| 120 | // the base address. This should only be called on new, suspended processes. |
forshaw | cd115ae | 2016-09-08 20:25:46 | [diff] [blame] | 121 | void* GetProcessBaseAddress(HANDLE process); |
| 122 | |
initial.commit | a814a8d5 | 2008-07-26 22:41:28 | [diff] [blame] | 123 | } // namespace sandbox |
| 124 | |
| 125 | // Resolves a function name in NTDLL to a function pointer. The second parameter |
| 126 | // is a pointer to the function pointer. |
| 127 | void ResolveNTFunctionPtr(const char* name, void* ptr); |
| 128 | |
nsylvain@chromium.org | 6e9d750 | 2008-09-28 01:20:20 | [diff] [blame] | 129 | #endif // SANDBOX_SRC_WIN_UTILS_H_ |