1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef SANDBOX_SRC_WIN_UTILS_H_ 6 #define SANDBOX_SRC_WIN_UTILS_H_ 7 8 #include <windows.h> 9 #include <string> 10 #include "base/basictypes.h" 11 12 namespace sandbox { 13 14 // Prefix for path used by NT calls. 15 const wchar_t kNTPrefix[] = L"\\??\\"; 16 const size_t kNTPrefixLen = arraysize(kNTPrefix) - 1; 17 18 const wchar_t kNTObjManPrefix[] = L"\\Device\\"; 19 const size_t kNTObjManPrefixLen = arraysize(kNTObjManPrefix) - 1; 20 21 // Automatically acquires and releases a lock when the object is 22 // is destroyed. 23 class AutoLock { 24 public: 25 // Acquires the lock. 26 explicit AutoLock(CRITICAL_SECTION *lock) : lock_(lock) { 27 ::EnterCriticalSection(lock); 28 }; 29 30 // Releases the lock; 31 ~AutoLock() { 32 ::LeaveCriticalSection(lock_); 33 }; 34 35 private: 36 CRITICAL_SECTION *lock_; 37 DISALLOW_IMPLICIT_CONSTRUCTORS(AutoLock); 38 }; 39 40 // Basic implementation of a singleton which calls the destructor 41 // when the exe is shutting down or the DLL is being unloaded. 42 template <typename Derived> 43 class SingletonBase { 44 public: 45 static Derived* GetInstance() { 46 static Derived* instance = NULL; 47 if (NULL == instance) { 48 instance = new Derived(); 49 // Microsoft CRT extension. In an exe this this called after 50 // winmain returns, in a dll is called in DLL_PROCESS_DETACH 51 _onexit(OnExit); 52 } 53 return instance; 54 } 55 56 private: 57 // this is the function that gets called by the CRT when the 58 // process is shutting down. 59 static int __cdecl OnExit() { 60 delete GetInstance(); 61 return 0; 62 } 63 }; 64 65 // Convert a short path (C:\path~1 or \\??\\c:\path~1) to the long version of 66 // the path. If the path is not a valid filesystem path, the function returns 67 // false and the output parameter is not modified. 68 bool ConvertToLongPath(const std::wstring& short_path, std::wstring* long_path); 69 70 // Sets result to true if the path contains a reparse point. The return value 71 // is ERROR_SUCCESS when the function succeeds or the appropriate error code 72 // when the function fails. 73 // This function is not smart. It looks for each element in the path and 74 // returns true if any of them is a reparse point. 75 DWORD IsReparsePoint(const std::wstring& full_path, bool* result); 76 77 // Returns true if the handle corresponds to the object pointed by this path. 78 bool SameObject(HANDLE handle, const wchar_t* full_path); 79 80 // Resolves a handle to an nt path. Returns true if the handle can be resolved. 81 bool GetPathFromHandle(HANDLE handle, std::wstring* path); 82 83 // Resolves a win32 path to an nt path using GetPathFromHandle. The path must 84 // exist. Returs true if the translation was succesful. 85 bool GetNtPathFromWin32Path(const std::wstring& path, std::wstring* nt_path); 86 87 // Translates a reserved key name to its handle. 88 // For example "HKEY_LOCAL_MACHINE" returns HKEY_LOCAL_MACHINE. 89 // Returns NULL if the name does not represent any reserved key name. 90 HKEY GetReservedKeyFromName(const std::wstring& name); 91 92 // Resolves a user-readable registry path to a system-readable registry path. 93 // For example, HKEY_LOCAL_MACHINE\\Software\\microsoft is translated to 94 // \\registry\\machine\\software\\microsoft. Returns false if the path 95 // cannot be resolved. 96 bool ResolveRegistryName(std::wstring name, std::wstring* resolved_name); 97 98 // Writes |length| bytes from the provided |buffer| into the address space of 99 // |child_process|, at the specified |address|, preserving the original write 100 // protection attributes. Returns true on success. 101 bool WriteProtectedChildMemory(HANDLE child_process, void* address, 102 const void* buffer, size_t length); 103 104 } // namespace sandbox 105 106 // Resolves a function name in NTDLL to a function pointer. The second parameter 107 // is a pointer to the function pointer. 108 void ResolveNTFunctionPtr(const char* name, void* ptr); 109 110 #endif // SANDBOX_SRC_WIN_UTILS_H_ 111