1212#include " function-trace.hh"
1313#include " profiles.hh"
1414#include " print.hh"
15+ #include " fs-input-accessor.hh"
1516
1617#include < algorithm>
1718#include < chrono>
@@ -503,6 +504,18 @@ EvalState::EvalState(
503504 , sOutputSpecified (symbols.create(" outputSpecified" ))
504505 , repair(NoRepair)
505506 , emptyBindings(0 )
507+ , rootFS(
508+ makeFSInputAccessor (
509+ CanonPath::root,
510+ evalSettings.restrictEval || evalSettings.pureEval
511+ ? std::optional<std::set<CanonPath>>(std::set<CanonPath>())
512+ : std::nullopt,
513+ [](const CanonPath & path) -> RestrictedPathError {
514+ auto modeInformation = evalSettings.pureEval
515+ ? " in pure evaluation mode (use '--impure' to override)"
516+ : " in restricted mode" ;
517+ throw RestrictedPathError (" access to absolute path '%1%' is forbidden %2%" , path, modeInformation);
518+ }))
506519 , derivationInternal(rootPath(CanonPath(" /builtin/derivation.nix" )))
507520 , store(store)
508521 , buildStore(buildStore ? buildStore : store)
@@ -518,6 +531,8 @@ EvalState::EvalState(
518531 , baseEnv(allocEnv(128 ))
519532 , staticBaseEnv{std::make_shared<StaticEnv>(false , nullptr )}
520533{
534+ rootFS->allowPath (CanonPath::root); // FIXME
535+
521536 countCalls = getEnv (" NIX_COUNT_CALLS" ).value_or (" 0" ) != " 0" ;
522537
523538 assert (gcInitialised);
@@ -599,7 +614,7 @@ SourcePath EvalState::checkSourcePath(const SourcePath & path_)
599614 */
600615 Path abspath = canonPath (path_.path .abs ());
601616
602- if (hasPrefix (abspath, corepkgsPrefix)) return CanonPath (abspath);
617+ if (hasPrefix (abspath, corepkgsPrefix)) return rootPath ( CanonPath (abspath) );
603618
604619 for (auto & i : *allowedPaths) {
605620 if (isDirOrInDir (abspath, i)) {
@@ -617,7 +632,7 @@ SourcePath EvalState::checkSourcePath(const SourcePath & path_)
617632
618633 /* Resolve symlinks. */
619634 debug (" checking access to '%s'" , abspath);
620- SourcePath path = CanonPath (canonPath (abspath, true ));
635+ SourcePath path = rootPath ( CanonPath (canonPath (abspath, true ) ));
621636
622637 for (auto & i : *allowedPaths) {
623638 if (isDirOrInDir (path.path .abs (), i)) {
@@ -649,12 +664,12 @@ void EvalState::checkURI(const std::string & uri)
649664 /* If the URI is a path, then check it against allowedPaths as
650665 well. */
651666 if (hasPrefix (uri, " /" )) {
652- checkSourcePath (CanonPath (uri));
667+ checkSourcePath (rootPath ( CanonPath (uri) ));
653668 return ;
654669 }
655670
656671 if (hasPrefix (uri, " file://" )) {
657- checkSourcePath (CanonPath (std::string (uri, 7 )));
672+ checkSourcePath (rootPath ( CanonPath (std::string (uri, 7 ) )));
658673 return ;
659674 }
660675
@@ -950,7 +965,7 @@ void Value::mkStringMove(const char * s, const NixStringContext & context)
950965
951966void Value::mkPath (const SourcePath & path)
952967{
953- mkPath (makeImmutableString (path.path .abs ()));
968+ mkPath (&*path. accessor , makeImmutableString (path.path .abs ()));
954969}
955970
956971
@@ -2037,7 +2052,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
20372052 else if (firstType == nPath) {
20382053 if (!context.empty ())
20392054 state.error (" a string that refers to a store path cannot be appended to a path" ).atPos (pos).withFrame (env, *this ).debugThrow <EvalError>();
2040- v.mkPath (CanonPath (canonPath (str ())));
2055+ v.mkPath (state. rootPath ( CanonPath (canonPath (str () ))));
20412056 } else
20422057 v.mkStringMove (c_str (), context);
20432058}
@@ -2236,7 +2251,7 @@ BackedStringView EvalState::coerceToString(
22362251 !canonicalizePath && !copyToStore
22372252 ? // FIXME: hack to preserve path literals that end in a
22382253 // slash, as in /foo/${x}.
2239- v._path
2254+ v._path . path
22402255 : copyToStore
22412256 ? store->printStorePath (copyPathToStore (context, v.path ()))
22422257 : std::string (v.path ().path .abs ());
@@ -2329,7 +2344,7 @@ SourcePath EvalState::coerceToPath(const PosIdx pos, Value & v, NixStringContext
23292344 auto path = coerceToString (pos, v, context, errorCtx, false , false , true ).toOwned ();
23302345 if (path == " " || path[0 ] != ' /' )
23312346 error (" string '%1%' doesn't represent an absolute path" , path).withTrace (pos, errorCtx).debugThrow <EvalError>();
2332- return CanonPath (path);
2347+ return rootPath ( CanonPath (path) );
23332348}
23342349
23352350
@@ -2429,7 +2444,9 @@ bool EvalState::eqValues(Value & v1, Value & v2, const PosIdx pos, std::string_v
24292444 return v1.string_view ().compare (v2.string_view ()) == 0 ;
24302445
24312446 case nPath:
2432- return strcmp (v1._path , v2._path ) == 0 ;
2447+ return
2448+ v1._path .accessor == v2._path .accessor
2449+ && strcmp (v1._path .path , v2._path .path ) == 0 ;
24332450
24342451 case nNull:
24352452 return true ;
0 commit comments