From 7d3efead61a3ce9bcc585098dbe84b691f01c8f7 Mon Sep 17 00:00:00 2001 From: Alex David Date: Sun, 17 Aug 2025 16:05:20 -0700 Subject: [PATCH] Only expose runtime closure by default Instead of binding the entire `/nix/store`. This behavior can be changed by overriding `basePermissions`. --- lib/base-permissions.nix | 2 +- lib/combinators.nix | 107 +++++++++++++++++++++++++++++++++++++++ lib/jail.nix | 4 ++ tests/test-cases.nix | 45 ++++++++++++++++ 4 files changed, 157 insertions(+), 1 deletion(-) diff --git a/lib/base-permissions.nix b/lib/base-permissions.nix index 99efa57..f03d37c 100644 --- a/lib/base-permissions.nix +++ b/lib/base-permissions.nix @@ -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 ] diff --git a/lib/combinators.nix b/lib/combinators.nix index 256f1ea..459eadf 100644 --- a/lib/combinators.nix +++ b/lib/combinators.nix @@ -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 = '' diff --git a/lib/jail.nix b/lib/jail.nix index 0ecc523..8007f85 100644 --- a/lib/jail.nix +++ b/lib/jail.nix @@ -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: diff --git a/tests/test-cases.nix b/tests/test-cases.nix index 479fe82..15f57bb 100644 --- a/tests/test-cases.nix +++ b/tests/test-cases.nix @@ -12,6 +12,51 @@ in { jail-without-flake = jail-nix-without-flake.init pkgs; in assertStdout (jail-without-flake "hello" pkgs.hello []) "Hello, world!"; + "it only exposes the nix store paths that are part of the runtime closure" = let + mkDep = name: let + buildtime = pkgs.runCommand "${name}-buildtime" {} "touch $out"; + runtime = pkgs.runCommand "${name}-runtime" {} "touch $out"; + parent = pkgs.runCommand "entry-dep" {} '' + echo ${buildtime} + echo ${runtime} > $out + ''; + in { inherit buildtime runtime parent; }; + deps = { + add-pkg-deps = mkDep "add-pkg-deps"; + argv = mkDep "argv"; + entry = mkDep "entry"; + unrelated = sh "echo this dep is unrelated"; + }; + checkExists = jail + "check-exists" + (sh '' + # ${deps.entry.parent} + if [ -e "$1" ]; then + echo "$1 file exists" + else + echo "$1 file doesn't exist" + fi + '') + (c: [ + (c.add-pkg-deps [ deps.add-pkg-deps.parent ]) + (c.set-argv [ (c.noescape "\"$1\"") deps.argv.parent ]) + ]); + in + # Dependencies that should be in the jail + map (dep: assertStdout (sh "${lib.getExe checkExists} ${dep}") "${dep} file exists") [ + deps.add-pkg-deps.runtime + deps.argv.runtime + deps.entry.runtime + ] + ++ + # Dependencies that shouldn't be in the jail + map (dep: assertStdout (sh "${lib.getExe checkExists} ${dep}") "${dep} file doesn't exist") [ + deps.add-pkg-deps.buildtime + deps.argv.buildtime + deps.entry.buildtime + deps.unrelated + ]; + "it allows overriding base permissions shared across all jails" = let jail' = jail-nix.lib.extend { inherit pkgs;