[go: nahoru, domu]

blob: a123a13eaf8d6a6697928204dc09cda6beb9d94e [file] [log] [blame]
rvargas@google.com4f1f3d02010-01-27 19:20:421// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commita814a8d52008-07-26 22:41:284
nsylvain@chromium.org6e9d7502008-09-28 01:20:205#ifndef SANDBOX_SRC_WIN_UTILS_H_
6#define SANDBOX_SRC_WIN_UTILS_H_
initial.commita814a8d52008-07-26 22:41:287
8#include <windows.h>
avi74a1875f2015-12-22 02:10:039#include <stddef.h>
initial.commita814a8d52008-07-26 22:41:2810#include <string>
brettw@chromium.org5672e5d2013-12-12 21:36:5011
avi74a1875f2015-12-22 02:10:0312#include "base/macros.h"
brettw@chromium.org5672e5d2013-12-12 21:36:5013#include "base/strings/string16.h"
forshaweca72db2016-05-05 20:11:1414#include "sandbox/win/src/nt_internals.h"
initial.commita814a8d52008-07-26 22:41:2815
16namespace sandbox {
17
18// Prefix for path used by NT calls.
19const wchar_t kNTPrefix[] = L"\\??\\";
20const size_t kNTPrefixLen = arraysize(kNTPrefix) - 1;
21
rvargas0e49d022015-04-01 19:20:0122const wchar_t kNTDevicePrefix[] = L"\\Device\\";
23const size_t kNTDevicePrefixLen = arraysize(kNTDevicePrefix) - 1;
cpu@chromium.orga7cab942010-08-07 03:10:1224
initial.commita814a8d52008-07-26 22:41:2825// Automatically acquires and releases a lock when the object is
26// is destroyed.
27class 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.commita814a8d52008-07-26 22:41:2840 CRITICAL_SECTION *lock_;
rvargas@google.com4f1f3d02010-01-27 19:20:4241 DISALLOW_IMPLICIT_CONSTRUCTORS(AutoLock);
initial.commita814a8d52008-07-26 22:41:2842};
43
44// Basic implementation of a singleton which calls the destructor
45// when the exe is shutting down or the DLL is being unloaded.
46template <typename Derived>
47class 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
rvargasb8fd7392015-07-27 23:53:4771// false and argument is not modified.
Penny MacNeil0a29c322017-08-09 17:16:4172// - If passing in a short native device path (\Device\HarddiskVolumeX\path~1),
73// a drive letter string (c:\) must also be provided.
74bool ConvertToLongPath(base::string16* path,
75 const base::string16* drive_letter = nullptr);
initial.commita814a8d52008-07-26 22:41:2876
rvargasb8fd7392015-07-27 23:53:4777// 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.commita814a8d52008-07-26 22:41:2880// 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.
rvargasb8fd7392015-07-27 23:53:4782DWORD IsReparsePoint(const base::string16& full_path);
initial.commita814a8d52008-07-26 22:41:2883
rvargas@google.com4f1f3d02010-01-27 19:20:4284// Returns true if the handle corresponds to the object pointed by this path.
85bool SameObject(HANDLE handle, const wchar_t* full_path);
86
cpu@chromium.orga7cab942010-08-07 03:10:1287// Resolves a handle to an nt path. Returns true if the handle can be resolved.
brettw@chromium.org5672e5d2013-12-12 21:36:5088bool GetPathFromHandle(HANDLE handle, base::string16* path);
initial.commita814a8d52008-07-26 22:41:2889
cpu@chromium.orga7cab942010-08-07 03:10:1290// Resolves a win32 path to an nt path using GetPathFromHandle. The path must
91// exist. Returs true if the translation was succesful.
brettw@chromium.org5672e5d2013-12-12 21:36:5092bool GetNtPathFromWin32Path(const base::string16& path,
93 base::string16* nt_path);
cpu@chromium.orga7cab942010-08-07 03:10:1294
initial.commita814a8d52008-07-26 22:41:2895// 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.org5672e5d2013-12-12 21:36:5098HKEY GetReservedKeyFromName(const base::string16& name);
initial.commita814a8d52008-07-26 22:41:2899
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.org5672e5d2013-12-12 21:36:50104bool ResolveRegistryName(base::string16 name, base::string16* resolved_name);
initial.commita814a8d52008-07-26 22:41:28105
rvargas@google.com0483cf182010-02-11 22:20:31106// 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.
109bool WriteProtectedChildMemory(HANDLE child_process, void* address,
110 const void* buffer, size_t length);
111
forshaw14976c12014-12-18 11:54:31112// Returns true if the provided path points to a pipe.
113bool IsPipe(const base::string16& path);
114
forshaweca72db2016-05-05 20:11:14115// Converts a NTSTATUS code to a Win32 error code.
116DWORD GetLastErrorFromNtStatus(NTSTATUS status);
117
forshawcd115ae2016-09-08 20:25:46118// Returns the address of the main exe module in memory taking in account
James Forshawf3980052017-08-15 09:29:46119// address space layout randomization. This uses the process' PEB to extract
120// the base address. This should only be called on new, suspended processes.
forshawcd115ae2016-09-08 20:25:46121void* GetProcessBaseAddress(HANDLE process);
122
initial.commita814a8d52008-07-26 22:41:28123} // 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.
127void ResolveNTFunctionPtr(const char* name, void* ptr);
128
nsylvain@chromium.org6e9d7502008-09-28 01:20:20129#endif // SANDBOX_SRC_WIN_UTILS_H_