startup_information_helper.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2020 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_STARTUP_INFORMATION_HELPER_H_
  5. #define SANDBOX_WIN_SRC_STARTUP_INFORMATION_HELPER_H_
  6. #include <Windows.h>
  7. #include <vector>
  8. #include "base/memory/scoped_refptr.h"
  9. #include "base/win/startup_information.h"
  10. #include "base/win/windows_version.h"
  11. #include "sandbox/win/src/app_container_base.h"
  12. #include "sandbox/win/src/process_mitigations.h"
  13. #include "sandbox/win/src/security_capabilities.h"
  14. namespace sandbox {
  15. using base::win::StartupInformation;
  16. // Wraps base::win::StartupInformation and allows some querying of what is
  17. // set. This is specialized for the dance between
  18. // BrokerServices::SpawnTarget() and TargetProcess::Create().
  19. class StartupInformationHelper {
  20. public:
  21. StartupInformationHelper();
  22. ~StartupInformationHelper();
  23. // Adds flags to the |dwFlags| field.
  24. void UpdateFlags(DWORD flags);
  25. // Sets |lpDesktop|. If |desktop| is empty, sets as nullptr.
  26. void SetDesktop(std::wstring desktop);
  27. // Creates PROC_THREAD_ATTRIBUTE_MITIGATION_POLICY based on |flags|.
  28. void SetMitigations(MitigationFlags flags);
  29. // Creates PROC_THREAD_ATTRIBUTE_CHILD_PROCESS_POLICY if |restrict| is true.
  30. void SetRestrictChildProcessCreation(bool restrict);
  31. // Sets stdout and stderr handles. Also allows these to be inherited into
  32. // the child process. Should be called once only.
  33. void SetStdHandles(HANDLE stdout_handle, HANDLE stderr_handle);
  34. // Ignores duplicate or invalid handles.
  35. void AddInheritedHandle(HANDLE handle);
  36. // Create PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES and
  37. // PROC_THREAD_ATTRIBUTE_ALL_APPLICATION_PACKAGES_POLICY
  38. // based on |container|. |container| should be valid.
  39. void SetAppContainer(scoped_refptr<AppContainer> container);
  40. // Creates PROC_THREAD_ATTRIBUTE_JOB_LIST with |job_handle|. Not valid before
  41. // Windows 10.
  42. void AddJobToAssociate(HANDLE job_handle);
  43. // Will one or more jobs be associated via the wrapped StartupInformation.
  44. bool HasJobsToAssociate() { return !job_handle_list_.empty(); }
  45. // Have handles been provided for secure inheritance?
  46. bool ShouldInheritHandles() { return inherit_handles_; }
  47. // Compiles fields into PROC_THREAD_ attributes and populates startup
  48. // information. Must be called before GetStartupInformation().
  49. bool BuildStartupInformation();
  50. // Gets wrapped object, valid once BuildStartupInformation() has been called.
  51. base::win::StartupInformation* GetStartupInformation() {
  52. return &startup_info_;
  53. }
  54. private:
  55. void operator=(const StartupInformationHelper&) = delete;
  56. StartupInformationHelper(const StartupInformationHelper&) = delete;
  57. int CountAttributes();
  58. // Fields that are not passed into CreateProcessAsUserW().
  59. scoped_refptr<AppContainer> app_container_;
  60. bool restrict_child_process_creation_ = false;
  61. HANDLE stdout_handle_ = INVALID_HANDLE_VALUE;
  62. HANDLE stderr_handle_ = INVALID_HANDLE_VALUE;
  63. bool inherit_handles_ = false;
  64. size_t mitigations_size_ = 0;
  65. // startup_info_.startup_info() is passed to CreateProcessAsUserW().
  66. StartupInformation startup_info_;
  67. // These need to have the same lifetime as startup_info_.startup_info();
  68. std::wstring desktop_;
  69. DWORD64 mitigations_[2]{};
  70. COMPONENT_FILTER component_filter_{};
  71. DWORD child_process_creation_ = 0;
  72. DWORD all_applications_package_policy_ = 0;
  73. std::vector<HANDLE> inherited_handle_list_;
  74. std::vector<HANDLE> job_handle_list_;
  75. std::unique_ptr<SecurityCapabilities> security_capabilities_;
  76. };
  77. } // namespace sandbox
  78. #endif // SANDBOX_WIN_SRC_STARTUP_INFORMATION_HELPER_H_