mirror of
https://github.com/NixOS/nix
synced 2025-07-07 10:11:47 +02:00
Always use the Darwin sandbox
Even with "build-use-sandbox = false", we now use sandboxing with a
permissive profile that allows everything except the creation of
setuid/setgid binaries.
Based on 85e93d7b87
.
This commit is contained in:
parent
f534627929
commit
8e298e8ad9
1 changed files with 86 additions and 78 deletions
|
@ -2538,94 +2538,93 @@ void DerivationGoal::runChild()
|
||||||
|
|
||||||
const char *builder = "invalid";
|
const char *builder = "invalid";
|
||||||
|
|
||||||
string sandboxProfile;
|
|
||||||
if (isBuiltin(*drv)) {
|
if (isBuiltin(*drv)) {
|
||||||
;
|
;
|
||||||
|
}
|
||||||
#if __APPLE__
|
#if __APPLE__
|
||||||
} else if (useChroot) {
|
else {
|
||||||
/* Lots and lots and lots of file functions freak out if they can't stat their full ancestry */
|
/* This has to appear before import statements. */
|
||||||
PathSet ancestry;
|
std::string sandboxProfile = "(version 1)\n";
|
||||||
|
|
||||||
/* We build the ancestry before adding all inputPaths to the store because we know they'll
|
if (useChroot) {
|
||||||
all have the same parents (the store), and there might be lots of inputs. This isn't
|
|
||||||
particularly efficient... I doubt it'll be a bottleneck in practice */
|
/* Lots and lots and lots of file functions freak out if they can't stat their full ancestry */
|
||||||
for (auto & i : dirsInChroot) {
|
PathSet ancestry;
|
||||||
Path cur = i.first;
|
|
||||||
|
/* We build the ancestry before adding all inputPaths to the store because we know they'll
|
||||||
|
all have the same parents (the store), and there might be lots of inputs. This isn't
|
||||||
|
particularly efficient... I doubt it'll be a bottleneck in practice */
|
||||||
|
for (auto & i : dirsInChroot) {
|
||||||
|
Path cur = i.first;
|
||||||
|
while (cur.compare("/") != 0) {
|
||||||
|
cur = dirOf(cur);
|
||||||
|
ancestry.insert(cur);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* And we want the store in there regardless of how empty dirsInChroot. We include the innermost
|
||||||
|
path component this time, since it's typically /nix/store and we care about that. */
|
||||||
|
Path cur = settings.nixStore;
|
||||||
while (cur.compare("/") != 0) {
|
while (cur.compare("/") != 0) {
|
||||||
cur = dirOf(cur);
|
|
||||||
ancestry.insert(cur);
|
ancestry.insert(cur);
|
||||||
|
cur = dirOf(cur);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* And we want the store in there regardless of how empty dirsInChroot. We include the innermost
|
/* Add all our input paths to the chroot */
|
||||||
path component this time, since it's typically /nix/store and we care about that. */
|
for (auto & i : inputPaths)
|
||||||
Path cur = settings.nixStore;
|
dirsInChroot[i] = i;
|
||||||
while (cur.compare("/") != 0) {
|
|
||||||
ancestry.insert(cur);
|
|
||||||
cur = dirOf(cur);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Add all our input paths to the chroot */
|
/* Violations will go to the syslog if you set this. Unfortunately the destination does not appear to be configurable */
|
||||||
for (auto & i : inputPaths)
|
if (settings.get("darwin-log-sandbox-violations", false)) {
|
||||||
dirsInChroot[i] = i;
|
sandboxProfile += "(deny default)\n";
|
||||||
|
} else {
|
||||||
/* This has to appear before import statements */
|
sandboxProfile += "(deny default (with no-log))\n";
|
||||||
sandboxProfile += "(version 1)\n";
|
|
||||||
|
|
||||||
/* Violations will go to the syslog if you set this. Unfortunately the destination does not appear to be configurable */
|
|
||||||
if (settings.get("darwin-log-sandbox-violations", false)) {
|
|
||||||
sandboxProfile += "(deny default)\n";
|
|
||||||
} else {
|
|
||||||
sandboxProfile += "(deny default (with no-log))\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The tmpDir in scope points at the temporary build directory for our derivation. Some packages try different mechanisms
|
|
||||||
to find temporary directories, so we want to open up a broader place for them to dump their files, if needed. */
|
|
||||||
Path globalTmpDir = canonPath(getEnv("TMPDIR", "/tmp"), true);
|
|
||||||
|
|
||||||
/* They don't like trailing slashes on subpath directives */
|
|
||||||
if (globalTmpDir.back() == '/') globalTmpDir.pop_back();
|
|
||||||
|
|
||||||
/* Our rwx outputs */
|
|
||||||
sandboxProfile += "(allow file-read* file-write* process-exec\n";
|
|
||||||
for (auto & i : missingPaths) {
|
|
||||||
sandboxProfile += (format("\t(subpath \"%1%\")\n") % i.c_str()).str();
|
|
||||||
}
|
|
||||||
sandboxProfile += ")\n";
|
|
||||||
|
|
||||||
/* Our inputs (transitive dependencies and any impurities computed above)
|
|
||||||
|
|
||||||
without file-write* allowed, access() incorrectly returns EPERM
|
|
||||||
*/
|
|
||||||
sandboxProfile += "(allow file-read* file-write* process-exec\n";
|
|
||||||
for (auto & i : dirsInChroot) {
|
|
||||||
if (i.first != i.second.source)
|
|
||||||
throw Error(format(
|
|
||||||
"can't map '%1%' to '%2%': mismatched impure paths not supported on Darwin")
|
|
||||||
% i.first % i.second.source);
|
|
||||||
|
|
||||||
string path = i.first;
|
|
||||||
struct stat st;
|
|
||||||
if (lstat(path.c_str(), &st)) {
|
|
||||||
if (i.second.optional && errno == ENOENT)
|
|
||||||
continue;
|
|
||||||
throw SysError(format("getting attributes of path ‘%1%’") % path);
|
|
||||||
}
|
}
|
||||||
if (S_ISDIR(st.st_mode))
|
|
||||||
sandboxProfile += (format("\t(subpath \"%1%\")\n") % path).str();
|
|
||||||
else
|
|
||||||
sandboxProfile += (format("\t(literal \"%1%\")\n") % path).str();
|
|
||||||
}
|
|
||||||
sandboxProfile += ")\n";
|
|
||||||
|
|
||||||
/* Allow file-read* on full directory hierarchy to self. Allows realpath() */
|
/* Our rwx outputs */
|
||||||
sandboxProfile += "(allow file-read*\n";
|
sandboxProfile += "(allow file-read* file-write* process-exec\n";
|
||||||
for (auto & i : ancestry) {
|
for (auto & i : missingPaths) {
|
||||||
sandboxProfile += (format("\t(literal \"%1%\")\n") % i.c_str()).str();
|
sandboxProfile += (format("\t(subpath \"%1%\")\n") % i.c_str()).str();
|
||||||
}
|
}
|
||||||
sandboxProfile += ")\n";
|
sandboxProfile += ")\n";
|
||||||
|
|
||||||
sandboxProfile += additionalSandboxProfile;
|
/* Our inputs (transitive dependencies and any impurities computed above)
|
||||||
|
|
||||||
|
without file-write* allowed, access() incorrectly returns EPERM
|
||||||
|
*/
|
||||||
|
sandboxProfile += "(allow file-read* file-write* process-exec\n";
|
||||||
|
for (auto & i : dirsInChroot) {
|
||||||
|
if (i.first != i.second.source)
|
||||||
|
throw Error(format(
|
||||||
|
"can't map '%1%' to '%2%': mismatched impure paths not supported on Darwin")
|
||||||
|
% i.first % i.second.source);
|
||||||
|
|
||||||
|
string path = i.first;
|
||||||
|
struct stat st;
|
||||||
|
if (lstat(path.c_str(), &st)) {
|
||||||
|
if (i.second.optional && errno == ENOENT)
|
||||||
|
continue;
|
||||||
|
throw SysError(format("getting attributes of path ‘%1%’") % path);
|
||||||
|
}
|
||||||
|
if (S_ISDIR(st.st_mode))
|
||||||
|
sandboxProfile += (format("\t(subpath \"%1%\")\n") % path).str();
|
||||||
|
else
|
||||||
|
sandboxProfile += (format("\t(literal \"%1%\")\n") % path).str();
|
||||||
|
}
|
||||||
|
sandboxProfile += ")\n";
|
||||||
|
|
||||||
|
/* Allow file-read* on full directory hierarchy to self. Allows realpath() */
|
||||||
|
sandboxProfile += "(allow file-read*\n";
|
||||||
|
for (auto & i : ancestry) {
|
||||||
|
sandboxProfile += (format("\t(literal \"%1%\")\n") % i.c_str()).str();
|
||||||
|
}
|
||||||
|
sandboxProfile += ")\n";
|
||||||
|
|
||||||
|
sandboxProfile += additionalSandboxProfile;
|
||||||
|
} else
|
||||||
|
sandboxProfile += "(allow default)\n";
|
||||||
|
|
||||||
|
sandboxProfile += "(deny file-write-setugid)\n";
|
||||||
|
|
||||||
debug("Generated sandbox profile:");
|
debug("Generated sandbox profile:");
|
||||||
debug(sandboxProfile);
|
debug(sandboxProfile);
|
||||||
|
@ -2636,6 +2635,13 @@ void DerivationGoal::runChild()
|
||||||
|
|
||||||
writeFile(sandboxFile, sandboxProfile);
|
writeFile(sandboxFile, sandboxProfile);
|
||||||
|
|
||||||
|
/* The tmpDir in scope points at the temporary build directory for our derivation. Some packages try different mechanisms
|
||||||
|
to find temporary directories, so we want to open up a broader place for them to dump their files, if needed. */
|
||||||
|
Path globalTmpDir = canonPath(getEnv("TMPDIR", "/tmp"), true);
|
||||||
|
|
||||||
|
/* They don't like trailing slashes on subpath directives */
|
||||||
|
if (globalTmpDir.back() == '/') globalTmpDir.pop_back();
|
||||||
|
|
||||||
builder = "/usr/bin/sandbox-exec";
|
builder = "/usr/bin/sandbox-exec";
|
||||||
args.push_back("sandbox-exec");
|
args.push_back("sandbox-exec");
|
||||||
args.push_back("-f");
|
args.push_back("-f");
|
||||||
|
@ -2643,12 +2649,14 @@ void DerivationGoal::runChild()
|
||||||
args.push_back("-D");
|
args.push_back("-D");
|
||||||
args.push_back("_GLOBAL_TMP_DIR=" + globalTmpDir);
|
args.push_back("_GLOBAL_TMP_DIR=" + globalTmpDir);
|
||||||
args.push_back(drv->builder);
|
args.push_back(drv->builder);
|
||||||
#endif
|
}
|
||||||
} else {
|
#else
|
||||||
|
else {
|
||||||
builder = drv->builder.c_str();
|
builder = drv->builder.c_str();
|
||||||
string builderBasename = baseNameOf(drv->builder);
|
string builderBasename = baseNameOf(drv->builder);
|
||||||
args.push_back(builderBasename);
|
args.push_back(builderBasename);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
for (auto & i : drv->args)
|
for (auto & i : drv->args)
|
||||||
args.push_back(rewriteHashes(i, rewritesToTmp));
|
args.push_back(rewriteHashes(i, rewritesToTmp));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue