Skip to content

Commit 5e477eb

Browse files
Ericson2314cidkidnix
authored andcommitted
Test nested sandboxing, and make nicer error
We were bedeviled by sandboxing issues when working on the layered store. The problem ended up being that when we have nested nix builds, and the inner store is inside the build dir (e.g. store is `/build/nix-test/$name/store`, build dir is `/build`) bind mounts clobber each other and store paths cannot be found. After thoroughly cleaning up `local-derivation-goal.cc`, we might be able to make that work. But that is a lot of work. For now, we just fail earlier with a proper error message. Finally, test this: nested sandboxing without the problematic store dir should work, and with should fail with the expected error message.
1 parent bbc08a1 commit 5e477eb

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

src/libstore/build/local-derivation-goal.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@ void LocalDerivationGoal::startBuilder()
594594
else
595595
dirsInChroot[i.substr(0, p)] = {i.substr(p + 1), optional};
596596
}
597+
if (hasPrefix(worker.store.storeDir, tmpDirInSandbox))
598+
{
599+
throw Error("Inside the sandbox, `sandbox-build-dir` must not contain `store?store`");
600+
}
597601
dirsInChroot[tmpDirInSandbox] = tmpDir;
598602

599603
/* Add the closure of store paths to the chroot. */

tests/local.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ nix_tests = \
138138
path-from-hash-part.sh \
139139
test-libstoreconsumer.sh \
140140
toString-path.sh \
141-
read-only-store.sh
141+
read-only-store.sh \
142+
nested-sandboxing.sh
142143

143144
ifeq ($(HAVE_LIBCPUID), 1)
144145
nix_tests += compute-levels.sh

tests/nested-sandboxing.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
source common.sh
2+
[[ -d /nix/store ]] || skipTest "running this test without Nix's deps being drawn from /nix/store is not yet supported"
3+
4+
requireSandboxSupport
5+
6+
source ./nested-sandboxing/command.sh
7+
8+
expectStderr 100 runNixBuild badStoreUrl 2 | grepQuiet '`sandbox-build-dir` must not contain'
9+
10+
runNixBuild goodStoreUrl 5

tests/nested-sandboxing/command.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export NIX_BIN_DIR=$(dirname $(type -p nix))
2+
# TODO Get Nix and its closure more flexibly
3+
export EXTRA_SANDBOX="/nix/store $(dirname $NIX_BIN_DIR)"
4+
5+
badStoreUrl () {
6+
local count=$1
7+
echo $TEST_ROOT/store-$count
8+
}
9+
10+
goodStoreUrl () {
11+
local count=$1
12+
echo $("badStoreUrl" "$count")?store=/foo-$count
13+
}
14+
15+
# The non-standard sandbox-build-dir helps ensure that we get the same behavior
16+
# whether this test is being run in a derivation as part of the nix build or
17+
# being manually run by a developer outside a derivation
18+
runNixBuild () {
19+
local storeFun=$1
20+
local count=$2
21+
nix-build \
22+
--no-substitute --no-out-link \
23+
--store "$("$storeFun" "$count")" \
24+
--extra-sandbox-paths "$EXTRA_SANDBOX" \
25+
./nested-sandboxing/runner.nix \
26+
--arg count "$((count - 1))" \
27+
--argstr storeFun "$storeFun" \
28+
--sandbox-build-dir /build-non-standard
29+
}

tests/nested-sandboxing/runner.nix

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{ count, storeFun }:
2+
3+
with import ../config.nix;
4+
5+
mkDerivation {
6+
name = "simple";
7+
busybox = builtins.getEnv "busybox";
8+
EXTRA_SANDBOX = builtins.getEnv "EXTRA_SANDBOX";
9+
buildCommand = if count == 0 then ''
10+
echo Deep enough! > $out
11+
'' else ''
12+
cp -r ${../common} ./common
13+
cp ${../common.sh} ./common.sh
14+
cp ${../config.nix} ./config.nix
15+
cp -r ${./.} ./nested-sandboxing
16+
17+
export PATH=${builtins.getEnv "NIX_BIN_DIR"}:$PATH
18+
19+
source common.sh
20+
source ./nested-sandboxing/command.sh
21+
22+
runNixBuild ${storeFun} ${toString count} >> $out
23+
'';
24+
}

0 commit comments

Comments
 (0)