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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! The platform sandbox backends, macOS `sandbox-exec` and Linux `bubblewrap`: each
//! confines a command for a [`SandboxPolicy`] and reports what it delivered as `Enforcement`.
//! The policy follows the call's `ExecProfile` (`tools::bash::sandbox`): Session is the
//! session's; Unattended adds `deny_ipc_escapes`; Strict (`SandboxPolicy::strict`) cuts the
//! network, writes only `TMPDIR`, reads only its roots (`ReadScope::Only`), env `Minimal`.
//!
//! - **macOS**: `sandbox-exec -p <profile> <program> <args…>`. The profile is
//! TinyScheme denying writes outside the allowed subtrees; exec is not
//! restricted; under `ReadScope::Only` reads are denied but under each root.
//! Network defaults to open; `DenyAll` / `Allowlist` both use
//! `(deny network*)`. A profile names a remote host only as `*` or
//! `localhost`, so the allowlist re-allows localhost when it names it and
//! no domain at all; the domains it names are reported unmet through
//! `Enforcement`, as on Linux. `Proxy` re-allows only the proxy's ports on
//! `localhost` (which covers `127.0.0.1` and `::1`); a proxy off this
//! machine can be held only to its port, on any host, and is reported
//! unmet. Name resolution goes through `mDNSResponder`'s unix socket, which
//! `(deny network*)` covers (measured on macOS 26).
//! - **Linux**: `bwrap --ro-bind / / --bind <writable> <writable> … <program>`
//! (`ReadScope::Only`: a read-only bind per root). No bwrap: no constraint. `DenyAll` uses
//! `--unshare-net`; bwrap has no domain-level filter, so `Allowlist`
//! degrades to the same whole-network cut rather than silently opening up —
//! failure goes toward the safer side, and says so through `Enforcement`.
//! `Proxy` cuts the network too, then binds a unix socket into the
//! namespace that this process forwards to the proxy (`egress::relay`),
//! and a `socat` started inside listens on the namespace's
//! `127.0.0.1:<proxy port>` and connects to it. Without `socat` in the
//! system directories the network stays cut, reported unmet.
//! Each command gets a PID namespace of its own, so the pids it sees (`$!`
//! among them) are not the host's, and a later command does not see what an
//! earlier one left running. What it left running outlives it, as on macOS.
//! - **Windows**: no backend.
//!
//! # What a confined command may write
//!
//! The working directory, the policy's `additional_writable` (the caller puts
//! the session's own `TMPDIR` there), `/dev`, and on macOS the user's
//! `DARWIN_USER_TEMP_DIR`, which `mktemp(1)` uses whatever `TMPDIR` says. On
//! Linux `/tmp` is a private tmpfs that goes away with the command. Every path
//! is resolved through its symlinks before a rule is written for it.
//!
//! Inside those, what the engine or git later acts on outside the sandbox is
//! read-only — where the policy names a `worktree_git` (the working
//! directory is a linked worktree), `.git` below means its git directory,
//! never one found by following the worktree's links: the
//! project's `.git`, `.agix` and `.claude` entries themselves,
//! `.git/{config,config.worktree,commondir}` and `.git/hooks/`, the hooks directory
//! `core.hooksPath` names and the files `.git/config` includes, the same in
//! the git directory of every submodule under `.git/modules/` (nested ones
//! included, and the directories leading down to them), the
//! instance's own project directory but its working entries —
//! `state/scratchpad/` and `state/memory/`, all the gate leaves out of
//! configuration there (`base::paths::ConfigRootAt::writable_state_entry`) —
//! with its configuration (`base::paths::ENGINE_CONFIG_*`: settings, MCP,
//! hooks, commands, agents, skills, plugins, rules, scenes, policy, scripts,
//! output styles), what in `state/` the engine exports, forks and resumes
//! from (`base::paths::PROJECT_STATE_ENGINE_DIRS`: saved tool results,
//! history) and the embedder's `state/<product id>/` read-only even inside a
//! writable root the policy names there, all of a foreign configuration root
//! (`.claude`, and `.agix` under a renamed layout), and in either the
//! writable roots the policy names inside it stay writable, the project's skills (`.agents/skills/`,
//! `base::paths::PROJECT_ENGINE_CONFIG_DIRS`, and the `.agents` entry above
//! it), the control files of each linked worktree's entry under
//! `.git/worktrees/` (`base::paths::GIT_WORKTREE_CONTROL_FILES`), and the
//! `.git` links of the engine's worktrees (in the project's worktrees
//! directories, `.wkts/` unless the settings name another —
//! `base::paths::SensitivePaths::worktree_dirs_in`) — the last two for a
//! worktree made after the command started as well, so that what it leaves
//! running cannot rewrite those of a sub-agent's worktree made later: macOS
//! by pattern, with the worktrees directory and each worktree directly in it
//! held in place, Linux by holding `.git/worktrees/` (made, empty, where it
//! is absent) and the worktrees directory read-only with the worktrees
//! already in it opened again. A command therefore cannot add a worktree of
//! its own, nor move one aside to rewrite its link. Under
//! `only_additional_writable`, the engine's own git, which adds them, is held
//! only to the ones that exist.
//! A command in a linked worktree writes of its git directory only what
//! `additional_writable` names (`base::paths::WorktreeGit::writable`): the
//! object store but its `objects/info/`, its own entry under `worktrees/`
//! with an absent control file there shut too (on Linux by an empty file),
//! and its branch's ref, lock and reflog. On Linux the ref is opened as the
//! directory it is in — git renames `<ref>.lock` over it, which a bind of the
//! file refuses — with the other entries there read-only again, and the git
//! directory is bound read-only beneath, so that `/tmp` does not hide it.
//! Of the state root only `scratchpad/`, `memory/` and those of
//! `additional_writable` under `tmp/` (a session's own `TMPDIR`) stay
//! writable. A project that lives in the state root lifts that fence; the
//! state root is then guarded as the `.agix` it is — its configuration files
//! and directories are read-only, but for one the project itself is in —
//! and the settings and definitions of the root and every scene stay
//! read-only either way. On Linux the own project directory and each
//! foreign root is read-only as a whole and the own one's existing working
//! entries are opened again, so a new entry directly in either cannot be
//! created; `.agents/skills/` is read-only. A foreign root, `.agents` or
//! `.agents/skills` that is not there (or is an empty directory) is shut
//! under an empty read-only tmpfs, and the mount point bwrap makes for it is
//! removed again once no sandboxed command's namespace lives in the project
//! — not when bwrap exits, which can be before what the command left behind
//! (`linux::bwrap_stdio_outside`); the own directory's stays, as the
//! engine's own.
//!
//! A policy with `only_additional_writable` leaves out the working directory,
//! the state root's working entries and `DARWIN_USER_TEMP_DIR`: only
//! `additional_writable` and `/dev` are writable, the read-only entries above
//! still apply inside them. The engine's own git runs under such a policy in
//! every session whose commands are sandboxed (`base::engine_exec`), so a
//! filter driver or other program a repository's configuration names runs
//! confined, writing at most the git directory and the worktrees directory
//! when it creates a worktree, and only the session's `TMPDIR` otherwise.
//!
//! # What a confined command may not read
//!
//! The policy's `deny_read` (the credential stores in the user's home, by
//! default) less its `allow_read`, with the targets of the links in a
//! denied directory that lead out of it; its `deny_read_exact` (what the
//! session's `Read` deny rules name) whatever `allow_read` says, each path
//! as named, no link in it followed; and whatever the policy says, the
//! credentials of `base::paths` (`CREDENTIALS`, placed on disk by
//! `SensitivePaths::credential_locations`): the state root's settings files
//! and MCP OAuth tokens, its own and every scene's, the daemons' socket
//! tokens under `daemon/auth/`, and the project's `.agix/settings.local.json`
//! and `.agix/mcp.local.json`. The state root's are also those of each of
//! the policy's `foreign_state_roots` — another instance's on this machine,
//! such as `~/.agix` seen from an embedder's `data_root` — none of which a
//! command can write in either, unless the working directory is inside it.
//! macOS writes a pattern for each; Linux covers the state roots and the
//! project's `.agix` with the mounts of `mask::mask_plan`, which a credential
//! written later does not climb out of, except where escape path 6 says; a
//! foreign state root's tmpfs is read-only.
//!
//! On Linux a denied directory is shut under an empty read-only tmpfs, so a
//! command can neither read nor write in it; macOS denies reads only.
//!
//! A policy whose `read` is `ReadScope::Only` narrows that further
//! (`read_scope`).
//!
//! # What a confined command may not connect to
//!
//! The daemon it runs under: a command connected to the daemon's socket would
//! be its host, free to add an MCP server that runs outside the sandbox, trust
//! its own project or answer its own permission prompts. So under every
//! policy, network open or not, a command cannot connect to a unix socket in
//! the state root (unless the project lives there), in a foreign state root,
//! or to one this process serves ([`fence_socket`], wherever `--socket` put
//! it). macOS refuses the
//! connect, by resolved path; Linux mounts `/dev/null` over the socket — over
//! each one in a state root and its scene directories, not the directories
//! themselves, which stay readable. macOS also refuses this process's TCP
//! and WebSocket ports ([`fence_port`]); on Linux only `--unshare-net` cuts
//! them off, and the egress relay will not forward to one. Under `Proxy` both
//! backends set the command's `HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY` and
//! `NO_PROXY`, in both cases, over what it inherits. The daemon refuses its own descendants on the unix socket as
//! well, which is what holds where no backend runs.
//!
//! **Behind the `sandbox` feature: off here, on in assembly and the daemon.** What it
//! constrains is the commands the *model* runs, which is defence in depth on a
//! machine whose owner is sitting at it and a boundary only where the agent
//! acts for someone else. Without the feature the backends are not compiled,
//! [`PlatformSandbox`] says so, and `sandbox.require` refuses every
//! command rather than running it unconstrained — which is the same promise the
//! enforcement report always made, kept one step earlier.
//!
//! # Known escape paths
//!
//! This is a lightweight write restriction, not a security boundary. Known
//! ways out:
//!
//! 1. **Writable `/dev`**: the macOS profile allows `file-write*` to `/dev`,
//! so a process can `mknod` a raw disk device and bypass file ACLs.
//! 2. **Inherited environment**: `LD_PRELOAD` / `DYLD_INSERT_LIBRARIES` are
//! not cleared, so a dynamic library can be injected.
//! 3. **Absent git files on Linux, for a git outside the sandbox**: a mount
//! cannot stand in for a file that does not exist without leaving an empty
//! one behind, which git refuses to read. So a project with no `.git` yet
//! can be given one, and a repository with no `commondir` can be given one,
//! with whatever configuration and filter drivers the command likes. The
//! engine's own git runs inside the sandbox, so it carries none of that out
//! unconfined; a git the user runs in the project afterwards does. macOS
//! denies both.
//! 4. **Another process acting for the command**: the sandbox follows the
//! command's own process tree, not what it asks a system service to do.
//! On macOS, measured against `sandbox-exec`: `open` has LaunchServices
//! start an app outside the sandbox, `defaults write` has `cfprefsd` write
//! any preference domain, and an Apple event reaches an app that runs
//! unconfined (Finder itself refuses events from a sandboxed sender, other
//! apps need not). launchd refuses `launchctl submit` / `bootstrap` from a
//! sandboxed caller. A policy with `deny_ipc_escapes` — every unattended
//! session's — denies `lsopen`, `user-preference-write` and
//! `appleevent-send`, the keychain through `com.apple.SecurityServer`
//! (what `security find-generic-password` asks), and the sockets of the
//! user's tmux and screen sessions. On Linux the same policy adds
//! `--unshare-ipc` and hides the session bus and the systemd user
//! manager's socket under `$XDG_RUNTIME_DIR` (and the bus path
//! `DBUS_SESSION_BUS_ADDRESS` names), so `systemd-run --user` has nothing
//! to talk to, and screen's socket directory (tmux's is in `/tmp`, private
//! already); an abstract-socket bus is reachable unless the network is cut
//! too. A multiplexer on a socket of its own choosing (`tmux -S`) is not
//! found. With the network open, any local
//! service listening on a socket the command can reach (sshd, a container
//! daemon) is a way out under every policy.
//! 5. **A nested repository planted in the project**: the guard protects the
//! project's own `.git`, but a command can `git init` a repository in a
//! subdirectory, write its `.git/config` (`core.fsmonitor` and the like)
//! and `git add` it into the project's index as a gitlink. The user's own
//! `git` in the project afterwards, outside the sandbox, enters it and runs
//! what its configuration names. Neither backend stops this: refusing to
//! create a `.git` entry would break `git init`, `git clone` and every
//! package manager that checks out a git dependency, and a repository built
//! elsewhere and moved in would evade it anyway. The engine's own `git`
//! carries `-c core.fsmonitor=false` and is unaffected. Above the backends,
//! the runtime looks for one after every command not held to the strict
//! profile (`tools::nested_repos`; a background command when its task
//! ends): an undeclared gitlink whose configuration names a program has its
//! `.git` moved aside unless the person attending keeps it. What it does
//! not catch is what a command starts that outlives it and plants one
//! later.
//! 6. **A credential written later directly into a writable directory, on
//! Linux**: a `/dev/null` bind is pinned to the file there at wrap time, so
//! a key file the engine creates afterwards, or rename-writes over the old
//! one, is readable by what a command left running. A tmpfs over the
//! directory would close this, and swallow every new entry the project
//! makes in it. That is the state root's own key files, and `daemon/auth`
//! while it does not exist, when the project is the state root; a scene's
//! key files, and its `mcp/oauth` while it does not exist, when the
//! project is that scene directory; and the key files of a project whose
//! `.agix` is a symbolic link. What lies outside the project, the
//! configuration directories a command cannot write anyway and a real
//! `.agix` directory are masked whole. macOS matches its denials by path
//! and has no such gap.
//! 7. **A `deny_read` entry inside a writable directory, on macOS**: matched
//! by path, so a command that may write the directory renames the file,
//! or a directory above it, and reads it under the new name. The
//! `Read(...)` deny rules in `deny_read_exact` mostly name files in the
//! project, which is writable. On Linux the entry is a mount, which a
//! rename does not carry off.
//!
//! Not among them, measured on bubblewrap 0.8: `/proc` is mounted afresh for
//! the command's PID namespace, so it shows neither the host's processes nor
//! their `fd` and `root`; a command inherits no descriptor of the daemon's but
//! its stdio, the daemon opening every one close-on-exec; and it has no
//! controlling terminal to push input into through `/dev/tty`, since
//! `LocalProcess` starts it in a session of its own.
pub use ;
use ;
pub use ;
/// The system directories programs load from: what a command reads to
/// start at all, and what `ReadScope::Only` lets it read whatever its roots.
const SYSTEM_READ_ROOTS: & = &;
const SYSTEM_READ_ROOTS: & = &;
const SYSTEM_READ_ROOTS: & = &;
/// Where a confined command may write besides its working directory and the
/// policy's `additional_writable`: device nodes, and on macOS the per-user
/// temporary directory, where mktemp(1) creates its file whatever `TMPDIR`
/// says — without it every script calling a bare `mktemp` fails. That one is
/// left out when only the named roots are writable.
/// Pick the platform backend and wrap the command.
///
/// Never fails: no available backend means no constraint, reported honestly
/// through [`Enforcement`] rather than by refusing here. Whether an
/// unconstrained run is acceptable is the caller's decision, not this
/// function's.
/// The environment a confined program starts with, as far as the sandbox can
/// tell: what its spec sets, over what `scope` lets it inherit from the
/// daemon's.
/// [`wrap`] for any program, not only a shell command.
/// The first path of the call that is not UTF-8. Profiles and bwrap
/// arguments are text: such a path would be written lossily, as one that
/// does not exist.
/// A command nothing wrapped, and the reason in words the operator can act on.
/// macOS sandbox-exec 的 TinyScheme 字符串里 `\` 和 `"` 要转义。
///
/// 只有 macOS 的配置生成器调用它,但函数本身和平台无关——它的测试在哪个平台上
/// 都该跑,所以这里没有跟着调用方一起 `cfg` 掉,而是只在别的平台上豁免"未使用"。
/// Where the terminal multiplexers this user runs listen. Typing into one of
/// their sessions runs a command in a shell outside the sandbox.
/// A shell command as a process spec. The one place `bash -c` is spelled, so
/// that every backend wraps the same thing.
/// The platform backends behind the contract.
///
/// Holds nothing: which backend applies is a fact about the machine, decided
/// per call by `cfg`, and the policy arrives as an argument.
;
/// 一行可读的沙盒可用性描述,给 `/doctor` 用。
/// macOS:恒"available: sandbox-exec"(系统自带)。
/// Linux:bwrap 在 PATH 时 available;否则报 unavailable + 提示装。
/// 其它平台:unavailable + 平台名。