Skip to content

Commit eba840c

Browse files
committedMay 4, 2017
Linux sandbox: Use /build instead of /tmp as $TMPDIR
There is a security issue when a build accidentally stores its $TMPDIR in some critical place, such as an RPATH. If TMPDIR=/tmp/nix-build-..., then any user on the system can recreate that directory and inject libraries into the RPATH of programs executed by other users. Since /build probably doesn't exist (or isn't world-writable), this mitigates the issue.
1 parent 2da6a42 commit eba840c

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed
 

‎src/libstore/build.cc

+15-5
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,9 @@ int childEntry(void * arg)
16611661
}
16621662

16631663

1664+
const std::string buildDir = "/build";
1665+
1666+
16641667
void DerivationGoal::startBuilder()
16651668
{
16661669
auto f = format(
@@ -1721,7 +1724,14 @@ void DerivationGoal::startBuilder()
17211724

17221725
/* In a sandbox, for determinism, always use the same temporary
17231726
directory. */
1727+
#if __linux__
1728+
tmpDirInSandbox = useChroot ? buildDir : tmpDir;
1729+
#elif __APPLE__
1730+
// On Darwin, we canonize /tmp because its probably a symlink to /private/tmp.
17241731
tmpDirInSandbox = useChroot ? canonPath("/tmp", true) + "/nix-build-" + drvName + "-0" : tmpDir;
1732+
#else
1733+
tmpDirInSandbox = tmpDir;
1734+
#endif
17251735
chownToBuilder(tmpDir);
17261736

17271737
/* Substitute output placeholders with the actual output paths. */
@@ -1829,11 +1839,11 @@ void DerivationGoal::startBuilder()
18291839
Samba-in-QEMU. */
18301840
createDirs(chrootRootDir + "/etc");
18311841

1832-
writeFile(chrootRootDir + "/etc/passwd",
1833-
(format(
1834-
"root:x:0:0:Nix build user:/:/noshell\n"
1835-
"nixbld:x:%1%:%2%:Nix build user:/:/noshell\n"
1836-
"nobody:x:65534:65534:Nobody:/:/noshell\n") % sandboxUid % sandboxGid).str());
1842+
writeFile(chrootRootDir + "/etc/passwd", fmt(
1843+
"root:x:0:0:Nix build user:%3%:/noshell\n"
1844+
"nixbld:x:%1%:%2%:Nix build user:%3%:/noshell\n"
1845+
"nobody:x:65534:65534:Nobody:/:/noshell\n",
1846+
sandboxUid, sandboxGid, buildDir));
18371847

18381848
/* Declare the build user's group so that programs get a consistent
18391849
view of the system (e.g., "id -gn"). */

0 commit comments

Comments
 (0)
Please sign in to comment.