Skip to content

Support build users on unprivileged users with subuid/subgid#15183

Merged
xokdvium merged 1 commit into
NixOS:masterfrom
obsidiansystems:newuidmap
Feb 11, 2026
Merged

Support build users on unprivileged users with subuid/subgid#15183
xokdvium merged 1 commit into
NixOS:masterfrom
obsidiansystems:newuidmap

Conversation

@artemist
Copy link
Copy Markdown
Member

@artemist artemist commented Feb 9, 2026

Motivation

Some users, especially on non-NixOS systems, may want to run an unprivileged daemon, so as to reduce the possible escalation if the daemon is exploited. Currently, doing so would prevent the daemon from using build users for isolating builds, making it substantially less secure.

However, with unprivileged user namespaces it is possible to become what looks like root, but without gaining any actual new permissions. With this and the newuidmap and newgidmap executables available on base linux we can allow the nix daemon to run builds as statically allocated build users without running as actual root.

Context

I have attempted this several other ways, including with nsresourced (#15103), but they all faced issues. Allocating uids with a call to newuidmap and newgidmap appears the most likely to work, as we can map both the daemon user and all allocated build users into the same namespace.

The least invasive change appeared to be to wrap the entire nix daemon so it thinks it's running as root. In that case it can use the existing auto-allocate-uids logic without any changes to core nix.

This is a very early draft and will need quite a few changes before I consider it ready.


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions Bot added the store Issues and pull requests concerning the Nix store label Feb 9, 2026
Comment thread src/nswrapper/nswrapper.cc Outdated
@artemist artemist force-pushed the newuidmap branch 2 times, most recently from e1401af to 80fb324 Compare February 10, 2026 16:55
Comment thread meson.build
@artemist
Copy link
Copy Markdown
Member Author

One thing I'm unsure about is getting nswrapper to build only on linux. The build infrastructure seems to assume that every subproject is built for all systems, and I'm not sure if I will break anything by changing that. I'd also need to change that assumption in a few places, but that shouldn't be too bad.

@artemist
Copy link
Copy Markdown
Member Author

I am somewhat surprised that macos tests built, I guess we don't have any testing running the full meson build.

I just pushed a new change that makes it only attempt to build nswrapper on linux

@artemist artemist marked this pull request as ready for review February 10, 2026 20:11
@artemist artemist requested a review from edolstra as a code owner February 10, 2026 20:11
Comment thread src/nswrapper/nswrapper.cc Outdated
Comment thread src/nswrapper/nswrapper.cc Outdated
@artemist artemist force-pushed the newuidmap branch 2 times, most recently from 56ff9e1 to 0dab126 Compare February 11, 2026 20:10
@artemist
Copy link
Copy Markdown
Member Author

Seems that the issue with running unshare on the parent was because I linked to something creating a thread in a static initializer during my testing. Now that i'm only linking to libstore I can run unshare in the parent. This lets me remove the wait

throw SysError("dropping groups");

// We have to mount a new devpts, otherwise we won't be able to chown ptses for build users
if (mount("none", "/dev/pts", "devpts", 0, "mode=0620") < 0)
Copy link
Copy Markdown
Contributor

@xokdvium xokdvium Feb 11, 2026

Choose a reason for hiding this comment

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

This is intended to use the hosts /dev/pts or a fresh one?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is intended to be the same as the host, but I don't believe it's strictly necessary. AIUI if i were mounting a new instance I would want to symlink /dev/ptmx to /dev/pts/ptmx, which would require mounting a new /dev. That /dev wouldn't have all the symlinks generated by systemd-udevd which nix might want to use, e.g. when impurely passing in devices. I'm not sure if that's entirely true though.

Comment thread src/nswrapper/nswrapper.cc Outdated
Comment thread src/nswrapper/nswrapper.cc Outdated
Comment thread src/nswrapper/nswrapper.cc
Comment on lines +21 to +22
if (argc < 4)
throw UsageError("Usage: %1% first_uid num_uids command [args...]", argv[0]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we factor this out to print on error? Ideally we'd have some helper function for creating libexec wrappers more concisely and print the usage message on all UsageError cases.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We already have nix::Args, but it's somewhat complicated to use outside of the existing command structure.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah Args is super complex, and I hope to move it out of libutil --- it is far too opinionated for that.

Comment on lines +24 to +25
auto baseExtra = parseUid(argv[1]);
auto numExtra = parseUid(argv[2]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should there be some checks that the uids don't step on system uid ranges?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We could add additional checks (Maybe that the id is above 1 << 16?), but newuidmap or newgidmap will fail if the ids aren't already in /etc/subuid or /etc/subgid, which requires manual administrator intervention.

Copy link
Copy Markdown
Member

@Ericson2314 Ericson2314 Feb 11, 2026

Choose a reason for hiding this comment

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

Yeah it is new*map's responsibility not do a bad thing, not ours. The setuid binaries have to be hardened to things.

@artemist artemist force-pushed the newuidmap branch 2 times, most recently from e70dfd7 to bfc0c12 Compare February 11, 2026 21:04
Comment thread src/nswrapper/nswrapper.cc Outdated
Comment thread src/nswrapper/package.nix Outdated
Comment thread tests/nixos/functional/unprivileged-daemon.nix Outdated
nix-nswrapper allows running nix in its own user namespace,
believing it is root and with access to build users for sandboxing
with auto-allocate-uids, while it is actually unprivileged.

It is used to wrap nix, and an example of its use has been
added to the unprivileged daemon functional tests.

Running it does not require any elevated privileges,
only uids and gids allocated in /etc/sub{uid,gid}
@xokdvium xokdvium enabled auto-merge February 11, 2026 21:59
@xokdvium xokdvium added this pull request to the merge queue Feb 11, 2026
Merged via the queue into NixOS:master with commit 3a60a04 Feb 11, 2026
14 checks passed
@artemist artemist deleted the newuidmap branch February 12, 2026 13:57
brittonr pushed a commit to brittonr/nix that referenced this pull request Apr 1, 2026
Support build users on unprivileged users with subuid/subgid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

store Issues and pull requests concerning the Nix store

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants