target_process.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (c) 2012 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. #ifndef SANDBOX_WIN_SRC_TARGET_PROCESS_H_
  5. #define SANDBOX_WIN_SRC_TARGET_PROCESS_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <vector>
  10. #include "base/memory/free_deleter.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/win/scoped_handle.h"
  13. #include "base/win/scoped_process_information.h"
  14. #include "base/win/sid.h"
  15. #include "base/win/windows_types.h"
  16. #include "sandbox/win/src/sandbox_types.h"
  17. namespace sandbox {
  18. class Dispatcher;
  19. class SharedMemIPCServer;
  20. class Sid;
  21. class ThreadPool;
  22. class StartupInformationHelper;
  23. // TargetProcess models a target instance (child process). Objects of this
  24. // class are owned by the Policy used to create them.
  25. class TargetProcess {
  26. public:
  27. TargetProcess() = delete;
  28. // The constructor takes ownership of |initial_token| and |lockdown_token|
  29. TargetProcess(base::win::ScopedHandle initial_token,
  30. base::win::ScopedHandle lockdown_token,
  31. HANDLE job,
  32. ThreadPool* thread_pool,
  33. const std::vector<base::win::Sid>& impersonation_capabilities);
  34. TargetProcess(const TargetProcess&) = delete;
  35. TargetProcess& operator=(const TargetProcess&) = delete;
  36. ~TargetProcess();
  37. // Creates the new target process. The process is created suspended.
  38. ResultCode Create(const wchar_t* exe_path,
  39. const wchar_t* command_line,
  40. std::unique_ptr<StartupInformationHelper> startup_info,
  41. base::win::ScopedProcessInformation* target_info,
  42. DWORD* win_error);
  43. // Assign a new lowbox token to the process post creation. The process
  44. // must still be in its initial suspended state, however this still
  45. // might fail in the presence of third-party software.
  46. ResultCode AssignLowBoxToken(const base::win::ScopedHandle& token);
  47. // Destroys the target process.
  48. void Terminate();
  49. // Creates the IPC objects such as the BrokerDispatcher and the
  50. // IPC server. The IPC server uses the services of the thread_pool.
  51. ResultCode Init(Dispatcher* ipc_dispatcher,
  52. void* policy,
  53. uint32_t shared_IPC_size,
  54. uint32_t shared_policy_size,
  55. DWORD* win_error);
  56. // Returns the handle to the target process.
  57. HANDLE Process() const { return sandbox_process_info_.process_handle(); }
  58. // Returns the handle to the job object that the target process belongs to.
  59. HANDLE Job() const { return job_; }
  60. // Returns the address of the target main exe. This is used by the
  61. // interceptions framework.
  62. HMODULE MainModule() const {
  63. return reinterpret_cast<HMODULE>(base_address_);
  64. }
  65. // Returns the name of the executable.
  66. const wchar_t* Name() const { return exe_name_.get(); }
  67. // Returns the process id.
  68. DWORD ProcessId() const { return sandbox_process_info_.process_id(); }
  69. // Returns the handle to the main thread.
  70. HANDLE MainThread() const { return sandbox_process_info_.thread_handle(); }
  71. // Transfers variable at |address| of |size| bytes from broker to target.
  72. ResultCode TransferVariable(const char* name,
  73. const void* address,
  74. size_t size);
  75. // Creates a mock TargetProcess used for testing interceptions.
  76. static std::unique_ptr<TargetProcess> MakeTargetProcessForTesting(
  77. HANDLE process,
  78. HMODULE base_address);
  79. private:
  80. // Verify the target process looks the same as the broker process.
  81. ResultCode VerifySentinels();
  82. // Details of the target process.
  83. base::win::ScopedProcessInformation sandbox_process_info_;
  84. // The token associated with the process. It provides the core of the
  85. // sbox security.
  86. base::win::ScopedHandle lockdown_token_;
  87. // The token given to the initial thread so that the target process can
  88. // start. It has more powers than the lockdown_token.
  89. base::win::ScopedHandle initial_token_;
  90. // Kernel handle to the shared memory used by the IPC server.
  91. base::win::ScopedHandle shared_section_;
  92. // Job object containing the target process. This is used during
  93. // process creation prior to Windows 10 and to identify the process in
  94. // broker_services.cc.
  95. HANDLE job_;
  96. // Reference to the IPC subsystem.
  97. std::unique_ptr<SharedMemIPCServer> ipc_server_;
  98. // Provides the threads used by the IPC. This class does not own this pointer.
  99. raw_ptr<ThreadPool> thread_pool_;
  100. // Base address of the main executable
  101. //
  102. // `base_address_` is not a raw_ptr<void>, because pointer to address in
  103. // another process could be confused as a pointer to PartitionMalloc memory,
  104. // causing ref-counting mismatch. See also https://crbug.com/1173374.
  105. RAW_PTR_EXCLUSION void* base_address_;
  106. // Full name of the target executable.
  107. std::unique_ptr<wchar_t, base::FreeDeleter> exe_name_;
  108. /// List of capability sids for use when impersonating in an AC process.
  109. std::vector<base::win::Sid> impersonation_capabilities_;
  110. };
  111. } // namespace sandbox
  112. #endif // SANDBOX_WIN_SRC_TARGET_PROCESS_H_