All writing

Running sandbox-exec and SwiftPM inside a macOS sandbox

I’ve been putting the pre-commit checks of an iOS side project of mine (a reading app) under sandbox-exec, with a generated profile per check that limits which repository files it can read, so that a green result can be reused for as long as those files don’t change. The profile is there for that reuse and isn’t meant to contain hostile code, and most checks didn’t notice it. Two kinds did, the self-tests of the sandbox code itself and anything that runs SwiftPM, and both came down to a rule of macOS sandboxing that I couldn’t find written down anywhere, so I’m writing it down here.

TLDR: a process inside a sandbox-exec profile can apply another sandbox only when the new profile is identical to the one it’s in. Anything else fails with exit 71 and sandbox_apply: Operation not permitted. SwiftPM sandboxes its manifest evaluation, so it trips on this, but only when the manifest cache misses. Passing --disable-sandbox to SwiftPM gets around it, at the cost of SwiftPM’s own protection (more on that below), and tests that call sandbox-exec themselves have to run outside the sandbox, so it pays to split out only those cases.

(man sandbox-exec says the command is deprecated, but it’s still there on macOS 26 and SwiftPM still uses it.)

Nesting sandbox-exec

Let’s start with two profiles that both allow everything, apart from reading one directory each:

; outer.sb
(version 1)
(allow default)
(deny file-read-data (subpath "/Users/Shared/nope"))
; inner.sb
(version 1)
(allow default)
(deny file-read-data (subpath "/Users/Shared/other"))

and a third one, allow.sb, which is only (version 1) and (allow default). Now we run sandbox-exec inside sandbox-exec:

$ sandbox-exec -f outer.sb sandbox-exec -f outer.sb /usr/bin/true; echo $?
0
$ sandbox-exec -f outer.sb sandbox-exec -f inner.sb /usr/bin/true; echo $?
sandbox-exec: sandbox_apply: Operation not permitted
71
$ sandbox-exec -f outer.sb sandbox-exec -f allow.sb /usr/bin/true; echo $?
sandbox-exec: sandbox_apply: Operation not permitted
71
$ sandbox-exec -f allow.sb sandbox-exec -f outer.sb /usr/bin/true; echo $?
sandbox-exec: sandbox_apply: Operation not permitted
71

So the same profile twice is fine (a copy of outer.sb under another name works too, so it’s the content that has to match), and every other pair fails, in either direction. A looser inner profile fails like any other, and so does any inner profile under an outer one that allows everything, so the two profiles have to match, and which one allows more doesn’t come into it. A profile that only reports instead of denying, with (allow file-read-data (with report) …), follows the same rule, since it’s just another profile. These runs are on macOS 26.

Only an identical profile nests, so probe with two different ones.

My first probe of this used the same profile inside and out, which is the one pair that works, so the first version of the rule I wrote down was wrong. It’s also an awkward failure to diagnose, because when something sandboxed fails we look in the sandbox log for a Sandbox: <process>(<pid>) deny(1) … record, and in my runs there was nothing there for this one, which makes sense, since the profile was never applied and so nothing was denied. Exit 71 is just EX_OSERR, which doesn’t point at sandboxing either.

SwiftPM sandboxes its manifest

To find out what a package contains, SwiftPM compiles Package.swift into a small program and runs it, and on macOS it runs that program under a sandbox of its own, through /usr/bin/sandbox-exec. So SwiftPM is a nested sandbox too, and under an outer profile we get this (paths trimmed):

$ sandbox-exec -f outer.sb swift package --manifest-cache none dump-package
error: 'pkg': Invalid manifest (compiled with: ["…/swiftc", … "…/pkg/Package.swift", "-o", "…/pkg-manifest"])
sandbox-exec: sandbox_apply: Operation not permitted
error: invalid manifests at [<AbsolutePath:"…/pkg">]

The “compiled with” part is how SwiftPM reports any manifest failure, so the line that tells us what happened is the sandbox-exec one. swift package describe and swift build fail the same way. Note the --manifest-cache none in that command, since SwiftPM caches the evaluated manifest, and on a cache hit it doesn’t run Package.swift at all, so there’s no nested sandbox to refuse. The failure only happens on a miss, which means a single passing run under a sandbox proves nothing.

The environment is part of that cache’s key, which matters for how we probe it. Calling swift straight from sandbox-exec leaves _ (the variable the shell sets to the path of the command it runs) pointing at sandbox-exec, which no run outside the sandbox has, so those calls always miss. Calling it from a script inside the sandbox, the way a check would, we can see the cache at work with a tiny package:

$ echo '// touch' >> Package.swift
$ sandbox-exec -f outer.sb bash -c 'swift package dump-package' >/dev/null; echo $?
1     # the manifest changed, so the cache misses
$ bash -c 'swift package dump-package' >/dev/null; echo $?
0     # an unsandboxed run fills the cache
$ sandbox-exec -f outer.sb bash -c 'swift package dump-package' >/dev/null; echo $?
0     # now it's a hit, so nothing nests
$ sandbox-exec -f outer.sb bash -c 'FOO=1 swift package dump-package' >/dev/null; echo $?
1     # one more environment variable, and it misses again

So a check that runs SwiftPM under a sandbox passes for as long as an earlier run in the same environment filled the cache, and fails after a manifest edit or any change to that environment, which looks a lot like a flaky test. pre-commit, for one, sets PRE_COMMIT=1 for every hook it runs, so a swift build from your terminal doesn’t fill the cache a hook reads (a hook’s environment trips SwiftPM in other ways too, which I wrote about in running swift build in a git hook from a linked worktree).

—disable-sandbox, and what it gives up

SwiftPM has a flag for this, which its help describes as “Disable the sandbox when executing subprocesses”:

$ sandbox-exec -f outer.sb swift package --manifest-cache none --disable-sandbox dump-package >/dev/null; echo $?
0

With SwiftPM’s own sandbox off there’s nothing left to nest. We pass it on every SwiftPM call, not only when we know there’s an outer sandbox, so a check runs the same commands sandboxed or not and a result earned under the sandbox describes the unsandboxed run as well.

SwiftPM’s own sandbox starts from (deny default), lets the manifest read files, and blocks writing outside temporary and cache folders and nearly all network access. The flag turns that off for every manifest the command loads, dependencies included, and for plugins too, since those are also subprocesses SwiftPM sandboxes. An outer profile like mine, which only limits what the check reads, replaces none of it. A manifest that tried to open a network connection gets blocked by SwiftPM’s default sandbox and connects fine with --disable-sandbox under outer.sb. So if we pass the flag, either the outer profile should deny network and writes outside the folders the check needs, or we should only do it for packages whose manifests and plugins we already trust.

Self-tests that call sandbox-exec

The project has two self-tests that call sandbox-exec on purpose, one for the profile generator (it compiles every generated profile and checks that a read outside the allowed set really is denied) and one for the code that decides when a result can be reused, where a single soundness case runs sandbox-exec. Those cases have to apply profiles that differ from the one the check runs under, so the only place they can run is outside the sandbox. Running both self-tests whole and unsandboxed would have worked, but then nothing in them could ever be reused, even though most of the second one never touches sandbox-exec at all.

So each self-test got a --cases plain|kernel switch, where the kernel group holds only the cases that call sandbox-exec, both groups run when the switch is omitted, and anything else after it is a usage error. A case either returns early from its function in the group it doesn’t belong to, or sits under an if for its group, which in a cut-down version looks like this:

# plain: checking a pattern needs no sandbox
refuse() {
	[[ "$CASES" != kernel ]] || return 0
	…
}

# kernel: the profile has to compile under the real sandbox-exec
if [[ "$CASES" != plain ]]; then
	sandbox-exec -f "$profile" /usr/bin/true || fail "profile did not compile"
fi

Then each self-test becomes two checks over the same files, one that runs inside the sandbox with --cases plain, and one that runs outside it with --cases kernel. For the second self-test that moves almost all of its time into the part that can be reused (on a busy machine its sandbox-exec cases took 2-3 s and the rest a minute or two). The profile generator’s self-test is the other way round, since most of its cases compile a profile, so the split buys little there.

The split is only as good as its grouping, since a plain case that nests would fail under the sandbox, and a check of the grouping is only worth something if it can fail on a wrong one (the same idea as in writing checks that can fail on the bug they guard against). The direct way to check it is to run each group under an outer profile (any profile the kernel cases don’t apply themselves), where the plain group has to pass and the kernel group has to fail:

sandbox-exec -f outer.sb ./self-test.sh --cases plain    # passes
sandbox-exec -f outer.sb ./self-test.sh --cases kernel   # fails, sandbox_apply

One case can still slip past that, a plain case that expects sandbox-exec to fail (a “this read must be denied” case, say), because exit 71 looks like a denial to it. A fake sandbox-exec first on PATH that records every call catches it, as long as the call goes through PATH:

stub="$(mktemp -d)"
trap 'rm -rf -- "$stub"' EXIT
printf '#!/bin/sh\necho "$*" >>"%s/calls"\nexit 1\n' "$stub" >"$stub/sandbox-exec"
chmod +x "$stub/sandbox-exec"
PATH="$stub:$PATH" ./self-test.sh --cases plain && [ ! -s "$stub/calls" ]   # passes, no calls
PATH="$stub:$PATH" ./self-test.sh --cases kernel; [ -s "$stub/calls" ]       # called the stub

The kernel half of that is what shows the stub was on the PATH the script sees, since without it an empty call log after the plain run could just mean the stub was never found. The stub only sees calls made by name, so it misses SwiftPM, which calls /usr/bin/sandbox-exec directly. The run under an outer profile catches SwiftPM only on a manifest-cache miss, so it’s best run on a cold cache, and with --disable-sandbox on every SwiftPM call a plain case can’t nest through SwiftPM anyway.

Checking a setup like this

Before trusting a sandboxed check that passes, it’s worth probing nesting with two different profiles, and SwiftPM with --manifest-cache none from the environment the check really runs in, since a cache filled from your terminal hides the failure. If a check has to run SwiftPM inside the sandbox, --disable-sandbox on every call works, as long as the outer profile takes over the network and write limits that SwiftPM’s own sandbox had. For tests that exist to call sandbox-exec, we split out only those cases and check the split by running the plain half under an outer profile.

Email address copied [email protected]