Only expose runtime closure by default

Instead of binding the entire `/nix/store`. This behavior can be changed
by overriding `basePermissions`.
This commit is contained in:
Alex David 2025-08-17 16:05:20 -07:00
commit 7d3efead61
4 changed files with 157 additions and 1 deletions

View file

@ -6,10 +6,10 @@ pkgs: let helpers = import ./helpers.nix pkgs; in combinators: with combinators;
(unsafe-add-raw-args "--clearenv")
(unsafe-add-raw-args "--die-with-parent")
(add-pkg-deps [ pkgs.coreutils ])
(readonly "/nix/store")
(readonly "/bin/sh")
(fwd-env "LANG")
(fwd-env "HOME")
(fwd-env "TERM")
bind-nix-store-runtime-closure
fake-passwd
]

View file

@ -84,6 +84,47 @@ in rec {
;
};
defer = {
sig = "Permission -> Permission";
doc = ''
Runs the passed permission after all other permissions.
A `Permission` is just a `State -> State` function. For many
permissions order does not matter since they touch different parts of
state, or they add bubblewrap args that don't affect each other.
However, some more complicated permissions may need to operate on the
final state. By calling `defer`, this permission will be applied after
all other base and per-jail permissions.
This is useful when writing your own combinators, specifically ones
that operate on state modified by other permissions. This is especially
useful if your combinator is called in `basePermissions` since those
are applied before any of the per-jail permissions are.
Example:
```nix
let
print-hostname = state:
jail.combinators.add-runtime "echo 'Hostname is: ''${state.hostname}'"
state;
in jail "test" pkgs.hello (combinators: with combinators; [
# This will print 'Hostname is: jail':
print-hostname
# This will print 'Hostname is: foo'
(defer print-hostname)
(set-hostname "foo")
])
```
'';
__functor = _:
combinator: state:
state // { deferredPermissions = state.deferredPermissions ++ [ combinator ]; }
;
};
include-once = {
sig = "String -> Permission -> Permission";
doc = ''
@ -553,6 +594,72 @@ in rec {
;
};
bind-nix-store-runtime-closure = {
sig = "Permission";
includedInBasePermissions = true;
doc = ''
Binds all `/nix/store` paths in the runtime closure of the jailed
application.
If you don't have any sensitive nix store paths, you may consider just
bind mounting `/nix/store` inside the jail instead.
For example, the jail defined by
```nix
# Note that this combinator is included in base permissions, so it does
# not need to be provided:
let listNixStore = pkgs.writeShellScriptBin "list-nix-store" "ls -l /nix/store";
in jail "list-nix-store" listNixStore [];
```
will print something like
```
total 52
dr-xr-xr-x 3 65534 65534 4096 Jan 1 1970 2dx846w0q80307z72r9jxai4xlj9ghb2-list-nix-store
dr-xr-xr-x 4 65534 65534 4096 Jan 1 1970 3mi59bgj22xx29dyss7jhmx3sgznd85m-acl-2.3.2
dr-xr-xr-x 3 65534 65534 4096 Jan 1 1970 6hqzbvz50bm87hcj4qfn51gh7arxj8a6-gcc-14.2.1.20250322-libgcc
dr-xr-xr-x 4 65534 65534 4096 Jan 1 1970 6nkqdqzpa75514lhglgnjs5k4dklw4sb-libidn2-2.3.8
dr-xr-xr-x 4 65534 65534 4096 Jan 1 1970 7c0v0kbrrdc2cqgisi78jdqxn73n3401-gcc-14.2.1.20250322-lib
dr-xr-xr-x 4 65534 65534 4096 Jan 1 1970 87fck6hm17chxjq7badb11mq036zbyv9-coreutils-9.7
dr-xr-xr-x 5 65534 65534 4096 Jan 1 1970 8syylmkvnn7lg2nar9fddpp5izb4gh56-attr-2.5.2
dr-xr-xr-x 6 65534 65534 4096 Jan 1 1970 cg9s562sa33k78m63njfn1rw47dp9z0i-glibc-2.40-66
dr-xr-xr-x 3 65534 65534 4096 Jan 1 1970 nzg6zqsijbv7yc95wlfcdswx6bg69srq-gmp-with-cxx-6.3.0
-r--r--r-- 1 65534 65534 145 Jan 1 1970 vylji4sibxm3mr3hync4zcmpmgbv09az-list-nix-store-runtime-closure
dr-xr-xr-x 4 65534 65534 4096 Jan 1 1970 xy4jjgw87sbgwylm5kn047d9gkbhsr9x-bash-5.2p37
dr-xr-xr-x 3 65534 65534 4096 Jan 1 1970 yypqcvqhnv8y4zpicgxdigp3giq81gzb-libunistring-1.3
dr-xr-xr-x 3 65534 65534 4096 Jan 1 1970 za53jjhjl1xajv3y1zpjvr9mh4w0c1ay-xgcc-14.2.1.20250322-libgcc
```
rather than your entire nix store.
'';
__functor = _:
include-once "bind-nix-store-runtime-closure" (defer (state:
let
runtimeClosure = pkgs.writeText "${state.name}-runtime-closure" ''
${pkgs.lib.concatStringsSep "\n" state.path}
${state.argv}
${state.entry}
'';
bindArgs = pkgs.runCommand "${state.name}-runtime-closure-bind-args"
{
__structuredAttrs = true;
exportReferencesGraph.runtime = runtimeClosure;
nativeBuildInputs = [ pkgs.jq ];
}
''
while read -r DEP; do
printf '%s\0' --ro-bind "$DEP" "$DEP" >> $out
done <<< "$(jq -r '.runtime | map(.path) | sort | .[]' "$NIX_ATTRS_JSON_FILE")"
'';
in
compose [
# Use `--args` and a file descriptor here in case the runtime
# closure is huge, to avoid running into argv size limitations
(add-runtime "exec {RUNTIME_CLOSURE_BIND_ARGS_FD}<${bindArgs}")
(unsafe-add-raw-args "--args \"$RUNTIME_CLOSURE_BIND_ARGS_FD\"")
] state
))
;
};
gui = {
sig = "Permission";
doc = ''

View file

@ -33,6 +33,7 @@ in {
namespaces = {};
includedOnce = []; # See include-once combinator
cleanup = []; # See cleanup combinator
deferredPermissions = []; # See defer combinator
};
in lib.pipe initialState (
# Permissions shared by all invocations of jail
@ -41,6 +42,9 @@ in {
# Permissions for this specific jail
++ (normalizePermissionsToList permissions)
# Permissions wrapped in `defer` combinator
++ [(s: builtinCombinators.compose s.deferredPermissions s)]
# Finalize everything remaining in state into bwrap args
++ (with builtinCombinators; [
(s: