Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chmod build root to make it read-only #11448

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/libstore/unix/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,9 @@ void LocalDerivationGoal::runChild()
if (rmdir("real-root") == -1)
throw SysError("cannot remove real-root directory");

// Make build root read-only, so `mkdir /homeless-shelter` would fail.
chmod_("/", 0555);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this break uid-range builds (e.g. tests/nixos/containers/systemd-nspawn.nix)? Those do expect the root to be writable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I guess it could.

I had another idea. If we actually mkdir /homeless-shelter and then make it read-only, and adjust the "home directory exists" error to be only triggered if the directory exists and is non-empty, I think it may solve the issue. Would you object to always having an empty /homeless-shelter directory in the sandbox?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should then be controlled by a derivation-provided setting like an "advanced attribute"?


/* Switch to the sandbox uid/gid in the user namespace,
which corresponds to the build user or calling user in
the parent namespace. */
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ nix build --impure -f multiple-outputs.nix --json e --no-link \
(.outputs | keys == ["a_a", "b"]))
'

# Make sure that `mkdir $HOME` fails with a "Permission denied" or "Operation not permitted" error
out="$(nix build -f mkdir-home-failing.nix -L 2>&1)" && status=0 || status=$?
test "$status" = 1
<<<"$out" grepQuiet -E "Permission denied" || <<<"$out" grepQuiet -E "Operation not permitted"

# Make sure that `--stdin` works and does not apply any defaults
printf "" | nix build --no-link --stdin --json | jq --exit-status '. == []'
printf "%s\n" "$drv^*" | nix build --no-link --stdin --json | jq --exit-status '.[0]|has("drvPath")'
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/mkdir-home-failing.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
with import ./config.nix;
mkDerivation {
name = "mkdir-home-no-permission";
builder = builtins.toFile "builder.sh"
''
mkdir $HOME
'';
}
Loading