[go: nahoru, domu]

blob: e416d6cda9979bf6a8e3db9fc1cf45f95080da91 [file] [log] [blame]
Avi Drissman09875652022-09-15 20:03:191// Copyright 2006-2010 The Chromium Authors
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
Lei Zhang21255d22021-04-23 19:17:245#ifndef SANDBOX_WIN_SRC_WIN_UTILS_H_
6#define SANDBOX_WIN_SRC_WIN_UTILS_H_
initial.commita814a8d52008-07-26 22:41:287
James Forshawf928125c2021-11-19 23:43:128#include <stdlib.h>
9
James Forshaw5ac06472021-12-22 13:07:3810#include <map>
James Forshawe74d3912019-10-03 19:48:0511#include <memory>
initial.commita814a8d52008-07-26 22:41:2812#include <string>
James Forshaw5ac06472021-12-22 13:07:3813#include <vector>
brettw@chromium.org5672e5d2013-12-12 21:36:5014
James Forshawf928125c2021-11-19 23:43:1215#include "base/win/windows_types.h"
James Forshaw5ac06472021-12-22 13:07:3816#include "third_party/abseil-cpp/absl/types/optional.h"
initial.commita814a8d52008-07-26 22:41:2817
18namespace sandbox {
19
20// Prefix for path used by NT calls.
21const wchar_t kNTPrefix[] = L"\\??\\";
Daniel Chengf42ae6c2022-02-27 01:07:3422const size_t kNTPrefixLen = std::size(kNTPrefix) - 1;
initial.commita814a8d52008-07-26 22:41:2823
rvargas0e49d022015-04-01 19:20:0124const wchar_t kNTDevicePrefix[] = L"\\Device\\";
Daniel Chengf42ae6c2022-02-27 01:07:3425const size_t kNTDevicePrefixLen = std::size(kNTDevicePrefix) - 1;
cpu@chromium.orga7cab942010-08-07 03:10:1226
James Forshaw5ac06472021-12-22 13:07:3827// List of handles mapped to their kernel object type name.
28using ProcessHandleMap = std::map<std::wstring, std::vector<HANDLE>>;
29
initial.commita814a8d52008-07-26 22:41:2830// Basic implementation of a singleton which calls the destructor
31// when the exe is shutting down or the DLL is being unloaded.
32template <typename Derived>
33class SingletonBase {
34 public:
35 static Derived* GetInstance() {
Will Harris0dbd9182017-10-05 20:56:4536 static Derived* instance = nullptr;
37 if (!instance) {
initial.commita814a8d52008-07-26 22:41:2838 instance = new Derived();
39 // Microsoft CRT extension. In an exe this this called after
40 // winmain returns, in a dll is called in DLL_PROCESS_DETACH
41 _onexit(OnExit);
42 }
43 return instance;
44 }
45
46 private:
47 // this is the function that gets called by the CRT when the
48 // process is shutting down.
49 static int __cdecl OnExit() {
50 delete GetInstance();
51 return 0;
52 }
53};
54
55// Convert a short path (C:\path~1 or \\??\\c:\path~1) to the long version of
56// the path. If the path is not a valid filesystem path, the function returns
rvargasb8fd7392015-07-27 23:53:4757// false and argument is not modified.
Penny MacNeil0a29c322017-08-09 17:16:4158// - If passing in a short native device path (\Device\HarddiskVolumeX\path~1),
59// a drive letter string (c:\) must also be provided.
Jan Wilken Dörrie4e88a3c2019-11-02 22:43:2260bool ConvertToLongPath(std::wstring* path,
61 const std::wstring* drive_letter = nullptr);
initial.commita814a8d52008-07-26 22:41:2862
rvargasb8fd7392015-07-27 23:53:4763// Returns ERROR_SUCCESS if the path contains a reparse point,
64// ERROR_NOT_A_REPARSE_POINT if there's no reparse point in this path, or an
65// error code when the function fails.
initial.commita814a8d52008-07-26 22:41:2866// This function is not smart. It looks for each element in the path and
67// returns true if any of them is a reparse point.
Jan Wilken Dörrie4e88a3c2019-11-02 22:43:2268DWORD IsReparsePoint(const std::wstring& full_path);
initial.commita814a8d52008-07-26 22:41:2869
rvargas@google.com4f1f3d02010-01-27 19:20:4270// Returns true if the handle corresponds to the object pointed by this path.
71bool SameObject(HANDLE handle, const wchar_t* full_path);
72
Alex Gough4f40d662022-11-29 23:35:1373// Resolves a handle to an nt path or nullopt if the path cannot be resolved.
74absl::optional<std::wstring> GetPathFromHandle(HANDLE handle);
initial.commita814a8d52008-07-26 22:41:2875
cpu@chromium.orga7cab942010-08-07 03:10:1276// Resolves a win32 path to an nt path using GetPathFromHandle. The path must
Alex Gough4f40d662022-11-29 23:35:1377// exist. Returns the path if the translation was successful.
78absl::optional<std::wstring> GetNtPathFromWin32Path(const std::wstring& path);
cpu@chromium.orga7cab942010-08-07 03:10:1279
Alex Gough4f40d662022-11-29 23:35:1380// Resolves a handle to its type name. Returns the typename if successful.
81absl::optional<std::wstring> GetTypeNameFromHandle(HANDLE handle);
James Forshaw5a4e92c2021-12-21 10:55:1882
initial.commita814a8d52008-07-26 22:41:2883// Resolves a user-readable registry path to a system-readable registry path.
84// For example, HKEY_LOCAL_MACHINE\\Software\\microsoft is translated to
Alex Gough4f40d662022-11-29 23:35:1385// \\registry\\machine\\software\\microsoft. Returns nullopt if the path
initial.commita814a8d52008-07-26 22:41:2886// cannot be resolved.
Alex Gough4f40d662022-11-29 23:35:1387absl::optional<std::wstring> ResolveRegistryName(std::wstring name);
initial.commita814a8d52008-07-26 22:41:2888
Alex Goughe2537922019-07-08 19:38:4989// Allocates |buffer_bytes| in child (PAGE_READWRITE) and copies data
90// from |local_buffer| in this process into |child|. |remote_buffer|
91// contains the address in the chile. If a zero byte copy is
92// requested |true| is returned and no allocation or copying is
93// attempted. Returns false if allocation or copying fails. If
94// copying fails, the allocation will be reversed.
95bool CopyToChildMemory(HANDLE child,
96 const void* local_buffer,
97 size_t buffer_bytes,
98 void** remote_buffer);
99
forshaw14976c12014-12-18 11:54:31100// Returns true if the provided path points to a pipe.
Jan Wilken Dörrie4e88a3c2019-11-02 22:43:22101bool IsPipe(const std::wstring& path);
forshaw14976c12014-12-18 11:54:31102
forshaweca72db2016-05-05 20:11:14103// Converts a NTSTATUS code to a Win32 error code.
104DWORD GetLastErrorFromNtStatus(NTSTATUS status);
105
forshawcd115ae2016-09-08 20:25:46106// Returns the address of the main exe module in memory taking in account
James Forshawf3980052017-08-15 09:29:46107// address space layout randomization. This uses the process' PEB to extract
108// the base address. This should only be called on new, suspended processes.
forshawcd115ae2016-09-08 20:25:46109void* GetProcessBaseAddress(HANDLE process);
110
Alex Gough665e8752023-01-25 01:29:37111// Returns a map of handles open in the current process. The map is keyed by the
112// kernel object type name. If querying the handles fails an empty optional
113// value is returned. Note that unless all threads are suspended in the process
114// the valid handles could change between the return of the list and when you
115// use them.
James Forshaw5ac06472021-12-22 13:07:38116absl::optional<ProcessHandleMap> GetCurrentProcessHandles();
117
initial.commita814a8d52008-07-26 22:41:28118} // namespace sandbox
119
120// Resolves a function name in NTDLL to a function pointer. The second parameter
121// is a pointer to the function pointer.
122void ResolveNTFunctionPtr(const char* name, void* ptr);
123
Lei Zhang21255d22021-04-23 19:17:24124#endif // SANDBOX_WIN_SRC_WIN_UTILS_H_