job.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //! Job-object wrapper for the sandbox child.
  2. //!
  3. //! Two roles:
  4. //! 1. `JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE` containment — the sandboxed
  5. //! process tree dies with the broker.
  6. //! 2. `JOBOBJECT_BASIC_UI_RESTRICTIONS` — block clipboard, global
  7. //! atoms, system-parameter writes, display-settings changes,
  8. //! desktop switching, ExitWindows, and cross-job USER/GDI handle
  9. //! access. The bits are listed individually rather than relying
  10. //! on a hypothetical `_ALL` so the enforced surface is auditable
  11. //! from this call site.
  12. //!
  13. //! Both must be set BEFORE `AssignProcessToJobObject` so they're in
  14. //! effect from the moment the suspended child is assigned (the caller
  15. //! resumes the thread only after `assign`).
  16. use anyhow::{Context, Result};
  17. use std::ffi::c_void;
  18. use std::mem::{size_of, zeroed};
  19. use windows::Win32::Foundation::{CloseHandle, HANDLE};
  20. use windows::Win32::System::JobObjects::{
  21. AssignProcessToJobObject, CreateJobObjectW, IsProcessInJob, JOB_OBJECT_LIMIT_BREAKAWAY_OK,
  22. JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, JOB_OBJECT_UILIMIT_DESKTOP,
  23. JOB_OBJECT_UILIMIT_DISPLAYSETTINGS, JOB_OBJECT_UILIMIT_EXITWINDOWS,
  24. JOB_OBJECT_UILIMIT_GLOBALATOMS, JOB_OBJECT_UILIMIT_HANDLES, JOB_OBJECT_UILIMIT_READCLIPBOARD,
  25. JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS, JOB_OBJECT_UILIMIT_WRITECLIPBOARD,
  26. JOBOBJECT_BASIC_UI_RESTRICTIONS, JOBOBJECT_EXTENDED_LIMIT_INFORMATION,
  27. JobObjectBasicUIRestrictions, JobObjectExtendedLimitInformation, SetInformationJobObject,
  28. };
  29. /// RAII job object. `Drop` closes the handle; with
  30. /// `KILL_ON_JOB_CLOSE` set, that terminates every process still in
  31. /// the job.
  32. pub struct Job(HANDLE);
  33. impl Job {
  34. /// Create an unnamed job with kill-on-close + full UI lockdown.
  35. /// `breakaway_ok` sets `JOB_OBJECT_LIMIT_BREAKAWAY_OK` — `true`
  36. /// only on the broker→runner Job (so the runner's child can
  37. /// `CREATE_BREAKAWAY_FROM_JOB` past it onto the runner's own
  38. /// load-bearing Job); **`false` on the runner→child Job** (a
  39. /// child must NOT be able to break away from its containment).
  40. pub fn new(breakaway_ok: bool) -> Result<Self> {
  41. // Wrap the raw handle in `Self` immediately so a `?` from
  42. // either `SetInformationJobObject` below still closes it.
  43. let job = unsafe { Self(CreateJobObjectW(None, None).context("CreateJobObjectW")?) };
  44. unsafe {
  45. let mut ext: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = zeroed();
  46. ext.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
  47. if breakaway_ok {
  48. ext.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_BREAKAWAY_OK;
  49. }
  50. SetInformationJobObject(
  51. job.0,
  52. JobObjectExtendedLimitInformation,
  53. &ext as *const _ as *const c_void,
  54. size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,
  55. )
  56. .context("SetInformationJobObject(KILL_ON_JOB_CLOSE)")?;
  57. // READCLIPBOARD — block OpenClipboard for read
  58. // WRITECLIPBOARD — block SetClipboardData
  59. // HANDLES — block USER/GDI handles from outside the job
  60. // GLOBALATOMS — block GlobalAddAtom (atom-table IPC)
  61. // SYSTEMPARAMETERS — block SystemParametersInfoW(SPI_SET*)
  62. // DISPLAYSETTINGS — block ChangeDisplaySettings
  63. // DESKTOP — block SwitchDesktop / SetThreadDesktop
  64. // EXITWINDOWS — block sandbox-initiated logoff/shutdown
  65. let ui_bits = JOB_OBJECT_UILIMIT_READCLIPBOARD
  66. | JOB_OBJECT_UILIMIT_WRITECLIPBOARD
  67. | JOB_OBJECT_UILIMIT_HANDLES
  68. | JOB_OBJECT_UILIMIT_GLOBALATOMS
  69. | JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS
  70. | JOB_OBJECT_UILIMIT_DISPLAYSETTINGS
  71. | JOB_OBJECT_UILIMIT_DESKTOP
  72. | JOB_OBJECT_UILIMIT_EXITWINDOWS;
  73. let ui = JOBOBJECT_BASIC_UI_RESTRICTIONS {
  74. UIRestrictionsClass: ui_bits,
  75. };
  76. SetInformationJobObject(
  77. job.0,
  78. JobObjectBasicUIRestrictions,
  79. &ui as *const _ as *const c_void,
  80. size_of::<JOBOBJECT_BASIC_UI_RESTRICTIONS>() as u32,
  81. )
  82. .context("SetInformationJobObject(BasicUIRestrictions)")?;
  83. }
  84. Ok(job)
  85. }
  86. /// Raw job handle (for `IsProcessInJob` diagnostics).
  87. pub fn raw(&self) -> HANDLE {
  88. self.0
  89. }
  90. /// Assign a (suspended) process to the job.
  91. pub fn assign(&self, proc: HANDLE) -> Result<()> {
  92. unsafe { AssignProcessToJobObject(self.0, proc).context("AssignProcessToJobObject") }
  93. }
  94. }
  95. /// `IsProcessInJob(proc, job)` — `job = None` means "any job".
  96. ///
  97. /// On API failure returns **`true`** (conservative): the only
  98. /// non-diagnostic caller gates `CREATE_BREAKAWAY_FROM_JOB` on it, and
  99. /// breakaway is harmless when the caller is NOT in a job (the kernel
  100. /// just ignores the flag) but failing to set it when the caller IS in
  101. /// one breaks the runner→child Job assign.
  102. pub fn is_process_in_job(proc: HANDLE, job: Option<HANDLE>) -> bool {
  103. let mut r = windows::core::BOOL(0);
  104. match unsafe { IsProcessInJob(proc, job, &mut r) } {
  105. Ok(()) => r.as_bool(),
  106. Err(_) => true,
  107. }
  108. }
  109. impl Drop for Job {
  110. fn drop(&mut self) {
  111. unsafe {
  112. let _ = CloseHandle(self.0);
  113. }
  114. }
  115. }