This commit is contained in:
Noam Raphael 2025-04-13 18:40:35 +08:00 committed by GitHub
commit fc21d387a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 0 deletions

View File

@ -2114,6 +2114,9 @@ void LocalDerivationGoal::runChild()
if (rmdir("real-root") == -1)
throw SysError("cannot remove real-root directory");
// Make build root read-only, so `mkdir /homeless-shelter` would fail.
chmod_("/", 0555);
/* Switch to the sandbox uid/gid in the user namespace,
which corresponds to the build user or calling user in
the parent namespace. */

View File

@ -153,6 +153,11 @@ nix build --impure -f multiple-outputs.nix --json e --no-link \
(.outputs | keys == ["a_a", "b"]))
'
# Make sure that `mkdir $HOME` fails with a "Permission denied" or "Operation not permitted" error
out="$(nix build -f mkdir-home-failing.nix -L 2>&1)" && status=0 || status=$?
test "$status" = 1
<<<"$out" grepQuiet -E "Permission denied" || <<<"$out" grepQuiet -E "Operation not permitted"
# Make sure that `--stdin` works and does not apply any defaults
printf "" | nix build --no-link --stdin --json | jq --exit-status '. == []'
printf "%s\n" "$drv^*" | nix build --no-link --stdin --json | jq --exit-status '.[0]|has("drvPath")'

View File

@ -0,0 +1,8 @@
with import ./config.nix;
mkDerivation {
name = "mkdir-home-no-permission";
builder = builtins.toFile "builder.sh"
''
mkdir $HOME
'';
}