#include "misc.hh" #include "store-api.hh" #include "local-store.hh" #include "globals.hh" namespace nix { Derivation derivationFromPath(StoreAPI & store, const Path & drvPath) { assertStorePath(drvPath); store.ensurePath(drvPath); return readDerivation(drvPath); } void computeFSClosure(StoreAPI & store, const Path & path, PathSet & paths, bool flipDirection, bool includeOutputs, bool includeDerivers) { if (paths.find(path) != paths.end()) return; paths.insert(path); PathSet edges; if (flipDirection) { store.queryReferrers(path, edges); if (includeOutputs) { PathSet derivers = store.queryValidDerivers(path); foreach (PathSet::iterator, i, derivers) edges.insert(*i); } if (includeDerivers && isDerivation(path)) { PathSet outputs = store.queryDerivationOutputs(path); foreach (PathSet::iterator, i, outputs) if (store.isValidPath(*i) && store.queryDeriver(*i) == path) edges.insert(*i); } } else { store.queryReferences(path, edges); if (includeOutputs && isDerivation(path)) { PathSet outputs = store.queryDerivationOutputs(path); foreach (PathSet::iterator, i, outputs) if (store.isValidPath(*i)) edges.insert(*i); } if (includeDerivers) { Path deriver = store.queryDeriver(path); if (store.isValidPath(deriver)) edges.insert(deriver); } } foreach (PathSet::iterator, i, edges) computeFSClosure(store, *i, paths, flipDirection, includeOutputs, includeDerivers); } Path findOutput(const Derivation & drv, string id) { foreach (DerivationOutputs::const_iterator, i, drv.outputs) if (i->first == id) return i->second.path; throw Error(format("derivation has no output `%1%'") % id); } static void dfsVisit(StoreAPI & store, const PathSet & paths, const Path & path, PathSet & visited, Paths & sorted, PathSet & parents) { if (parents.find(path) != parents.end()) throw BuildError(format("cycle detected in the references of `%1%'") % path); if (visited.find(path) != visited.end()) return; visited.insert(path); parents.insert(path); PathSet references; if (store.isValidPath(path)) store.queryReferences(path, references); foreach (PathSet::iterator, i, references) /* Don't traverse into paths that don't exist. That can happen due to substitutes for non-existent paths. */ if (*i != path && paths.find(*i) != paths.end()) dfsVisit(store, paths, *i, visited, sorted, parents); sorted.push_front(path); parents.erase(path); } Paths topoSortPaths(StoreAPI & store, const PathSet & paths) { Paths sorted; PathSet visited, parents; foreach (PathSet::const_iterator, i, paths) dfsVisit(store, paths, *i, visited, sorted, parents); return sorted; } } span='3' class='logmsg'> This is a followup to 8f4ffb3fae133bb21d7991e97c2f19a7108b1143. Commit 8f4ffb3fae133bb21d7991e97c2f19a7108b1143 fell short in two ways: (1) it didn’t have any effet for fixed-output derivations performed in a chroot, which is the case for all of them except those using “builtin:download” and “builtin:git-download”, and (2) it did not preserve ownership when copying, leading to “suspicious ownership or permission […] rejecting this build output” errors. * nix/libstore/build.cc (DerivationGoal::buildDone): Account for ‘chrootRootDir’ when copying ‘drv.outputs’. * nix/libutil/util.cc (copyFileRecursively): Add ‘fchown’ and ‘fchownat’ calls to preserve file ownership; this is necessary for chrooted fixed-output derivation builds. * nix/libutil/util.hh: Update comment. Change-Id: Ib59f040e98fed59d1af81d724b874b592cbef156 2024-03-11daemon: Protect against FD escape when building fixed-output derivations ↵Ludovic Courtès (CVE-2024-27297). This fixes a security issue (CVE-2024-27297) whereby a fixed-output derivation build process could open a writable file descriptor to its output, send it to some outside process for instance over an abstract AF_UNIX socket, which would then allow said process to modify the file in the store after it has been marked as “valid”. Vulnerability discovered by puck <https://github.com/puckipedia>. Nix security advisory: https://github.com/NixOS/nix/security/advisories/GHSA-2ffj-w4mj-pg37 Nix fix: https://github.com/NixOS/nix/commit/244f3eee0bbc7f11e9b383a15ed7368e2c4becc9 * nix/libutil/util.cc (readDirectory): Add variants that take a DIR* and a file descriptor. Rewrite the ‘Path’ variant accordingly. (copyFile, copyFileRecursively): New functions. * nix/libutil/util.hh (copyFileRecursively): New declaration. * nix/libstore/build.cc (DerivationGoal::buildDone): When ‘fixedOutput’ is true, call ‘copyFileRecursively’ followed by ‘rename’ on each output. Change-Id: I7952d41093eed26e123e38c14a4c1424be1ce1c4 Reported-by: Picnoir <picnoir@alternativebit.fr>, Théophane Hufschmitt <theophane.hufschmitt@tweag.io> Change-Id: Idb5f2757f35af86b032a9851cecb19b70227bd88