Skip to content

Commit bc499b2

Browse files
authored
Merge pull request #8696 from NixLayeredStore/nested-sandboxing
Test nested sandboxing, and make nicer error
2 parents 6d9f1a8 + 0f7242f commit bc499b2

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-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("`sandbox-build-dir` must not contain the storeDir");
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
source common.sh
2+
# This test is run by `tests/nested-sandboxing/runner.nix` in an extra layer of sandboxing.
3+
[[ -d /nix/store ]] || skipTest "running this test without Nix's deps being drawn from /nix/store is not yet supported"
4+
5+
requireSandboxSupport
6+
7+
source ./nested-sandboxing/command.sh
8+
9+
expectStderr 100 runNixBuild badStoreUrl 2 | grepQuiet '`sandbox-build-dir` must not contain'
10+
11+
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 altitude=$1
7+
echo $TEST_ROOT/store-$altitude
8+
}
9+
10+
goodStoreUrl () {
11+
local altitude=$1
12+
echo $("badStoreUrl" "$altitude")?store=/foo-$altitude
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 altitude=$2
21+
nix-build \
22+
--no-substitute --no-out-link \
23+
--store "$("$storeFun" "$altitude")" \
24+
--extra-sandbox-paths "$EXTRA_SANDBOX" \
25+
./nested-sandboxing/runner.nix \
26+
--arg altitude "$((altitude - 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+
{ altitude, storeFun }:
2+
3+
with import ../config.nix;
4+
5+
mkDerivation {
6+
name = "nested-sandboxing";
7+
busybox = builtins.getEnv "busybox";
8+
EXTRA_SANDBOX = builtins.getEnv "EXTRA_SANDBOX";
9+
buildCommand = if altitude == 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 altitude} >> $out
23+
'';
24+
}

0 commit comments

Comments
 (0)