Skip to content

Commit 5acf50a

Browse files
edolstraMic92
authored andcommitted
Disallow the build directory having world-writable parents
1 parent 88b7db1 commit 5acf50a

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/libstore/unix/build/derivation-builder.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,18 @@ static void handleChildException(bool sendException)
698698
}
699699
}
700700

701+
static bool checkNotWorldWritable(std::filesystem::path path)
702+
{
703+
while (true) {
704+
auto st = lstat(path);
705+
if (st.st_mode & S_IWOTH)
706+
return false;
707+
if (path == path.parent_path()) break;
708+
path = path.parent_path();
709+
}
710+
return true;
711+
}
712+
701713
void DerivationBuilderImpl::startBuilder()
702714
{
703715
/* Make sure that no other processes are executing under the
@@ -729,6 +741,9 @@ void DerivationBuilderImpl::startBuilder()
729741

730742
createDirs(buildDir);
731743

744+
if (buildUser && !checkNotWorldWritable(buildDir))
745+
throw Error("Path %s or a parent directory is world-writable or a symlink. That's not allowed for security.", buildDir);
746+
732747
/* Create a temporary directory where the build will take
733748
place. */
734749
topTmpDir = createTempDir(buildDir, "nix-build-" + std::string(drvPath.name()), 0700);

tests/nixos/chroot-store.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,9 @@ in
4141
4242
# Test that /nix/store is available via an overlayfs mount.
4343
machine.succeed("nix shell --store /tmp/nix ${pkgA} --command cowsay foo >&2")
44+
45+
# Building in /tmp should fail for security reasons.
46+
err = machine.fail("nix build --offline --store /tmp/nix --expr 'builtins.derivation { name = \"foo\"; system = \"x86_64-linux\"; builder = \"/foo\"; }' 2>&1")
47+
assert "is world-writable" in err
4448
'';
4549
}

0 commit comments

Comments
 (0)