1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! macOS sandbox backend — `sandbox_init(3)` profile-based isolation.
//!
//! macOS exposes per-process sandboxing through the deprecated-but-still-shipped
//! `sandbox_init` C function in `libSystem`. Calling it from `pre_exec`
//! installs a profile on the child before `execve`, equivalent to Linux's
//! `unshare`.
//!
//! # Tier mapping
//!
//! - **None** — no-op.
//! - **Hardened** — a custom SBPL profile that denies network access,
//! hides other processes' metadata (`process-info*`), blocks raw
//! `IOKit` device access (so the child can't hook the HID stack to
//! keylog the user), denies privileged Mach task-port lookups (so
//! it can't `task_for_pid` into other processes), and denies write
//! access to POSIX shared memory (a non-network exfil channel). The
//! filesystem is kept open because Hardened is intended to allow
//! typical `npm run` / `cargo build` / `wrangler deploy` workloads.
//! - **Lockdown** — `pure-computation` builtin profile. Child cannot
//! reach the network *or* write to the filesystem outside ephemeral
//! areas. The closest macOS analogue to Linux Lockdown's "private
//! mount + denied network".
//!
//! The chosen profiles are macOS's predefined string-name profiles
//! (`kSBXProfilePureComputation`) for Lockdown, and a custom SBPL
//! profile for Hardened. `sandbox_init` historically prints a deprecation
//! diagnostic at link time but the function is still present and
//! exercised by Chrome / Firefox / Apple's own apps.
use ;
use SandboxTier;
extern "C"
/// `SANDBOX_NAMED` — the `profile` argument is a predefined builtin name.
const SANDBOX_NAMED: u64 = 1;
/// `SANDBOX_RAW` — the `profile` argument is raw SBPL string.
const SANDBOX_RAW: u64 = 0;
/// macOS custom Hardened SBPL — defense-in-depth around the secret in
/// the child's environment.
///
/// - `(allow default)` — start from the unrestricted base; we only
/// *deny* specific operations rather than building an allow-list,
/// because Hardened needs to support arbitrary developer tooling
/// (npm, cargo, wrangler, …).
/// - `(deny network*)` — closes outbound TCP/UDP, the obvious
/// network-exfil path.
/// - `(deny process-info* (with no-log))` — hides other processes'
/// metadata so the child can't enumerate the system to find the
/// envseal supervisor / GUI helper. `no-log` keeps the macOS
/// `log stream` clean of denied-access spam from routine
/// `proc_listpids` calls inside libSystem.
/// - `(deny iokit-open)` — blocks raw `IOKit` device handles so the
/// child cannot, e.g., open `IOHIDSystem` to keylog the desktop
/// while the user types in another window.
/// - `(deny mach-priv-task-port)` — blocks `task_for_pid()` against
/// privileged tasks; without this a child running as the same uid
/// could attach the supervisor's address space.
/// - `(deny ipc-posix-shm-write)` — closes POSIX shared-memory writes
/// so a compromised child can't drop the secret into a `shm_open`
/// region that another local user maps to read it back.
const PROFILE_HARDENED: & = b"(version 1)
(allow default)
(deny network*)
(deny process-info* (with no-log))
(deny iokit-open)
(deny mach-priv-task-port)
(deny ipc-posix-shm-write)\0";
/// macOS predefined profile that denies network *and* file writes outside
/// ephemeral areas.
const PROFILE_PURE_COMPUTATION: & = b"pure-computation\0";
/// Apply sandbox isolation matching the requested tier.
///
/// Called between `fork()` and `exec()` via `Command::pre_exec`. Each tier
/// maps to a predefined macOS profile (see module docs).
///
/// # Errors
///
/// Returns the OS error if `sandbox_init` rejects the profile (in
/// practice, a `PermissionDenied` containing the message libSystem emits
/// into `errorbuf`).
///
/// # Safety
///
/// Must only be called from a `pre_exec` closure.
/// Whether the macOS sandbox primitive is reachable on this build.
///
/// Always `true` on macOS — `sandbox_init` is in `libSystem`, present on
/// every shipping macOS. Used by [`super::OsCapabilities`] for diagnostic
/// reporting.