Skip to content

linux: support supplementary groups with auto-allocate-uids #13314

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

SimSaladin
Copy link
Contributor

Motivation

Add a new option "supplementary-groups" that allows specifying additional groups to be mapped into the build sandbox and assigned as supplementary groups for the build user.

This makes it possible to, for example, use the "kvm" group to provide access to /dev/kvm (without giving world-write to it) even when auto-allocate-uids is enabled—something that was not previously possible. It also enables use of supplementary groups to grant sandboxed builds group-controlled access to secrets or other shared resources.

Context

Closes: #9276

What I'm not at all sure of is whether having kvm (or anything) mapped by default is sensible or not.

The code is already calling setgroups anyway, a default perhaps wouldn't really break anything for anyone, and kvm assigned by default (if it exists) might save someone from the trouble of hunting down that one piece of UDEV rule logic... or was it libvirt itself that changed /dev/kvm perms if it felt like it sometimes? Or perhaps the systemd unit... I mean, well, this is now the future where my /dev/kvm is 0666 root:kvm as a default. So I suppose then that someone finally found way to fix that "permission denied" error for everyone. Neat!

Note, kvm the system-feature is already on by default.


Add 👍 to pull requests you find important.

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

@SimSaladin SimSaladin requested a review from Ericson2314 as a code owner June 2, 2025 21:09
Example:

```nix
extra-supplementary-groups = nixbld users:10100
Copy link
Member

Choose a reason for hiding this comment

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

This should probably be a JSON option, similar to #13315 (comment).

E.g.

extra-supplementary-groups = [ {"group": "nixbld"}, {"group": "users", "gid": 10100} ]

Example:

```nix
extra-supplementary-groups = nixbld users:10100
Copy link
Member

Choose a reason for hiding this comment

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

What's the use case for adding nixbld as a supplementary group? Isn't the build user already running in the nixbld group?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, this doesn't change anything if build-users-group = nixbld and auto-allocate-uids = false.

But if (1) build-users-group was something else than nixbld, then nixbld here should be interpreted as a supplementary group (primary group being whatever the host has assigned for the builder mapped to sandbox nixbld/100). It has a GID that is inherited from the host, and it (or the sandbox-mapped GID, if mapping was added) should be present in build user's sup. groups. (Currently this case is ignored.)

Secondly, (2) when auto-allocate-uids is active the host-side GID of sandbox nixbld group is a huge generated one, so it's a totally different group (save for the identical name) from the nixbld group that is referred to here (which is, of course, the group that's called nixbld and has some well-known static GID (e.g. 30000) in the host's (usually root) namespace ... well, assuming that such a group exists of course).

Most importantly, it's a group for which one is able to grant permissions in a filesystem, bind-mount said fs into the sandbox, and so provide fs access - without 777 - for builders through group permissions. Without auto-allocate-uids, the host-side primary GID is already the permitted group, usually, and nothing more needs to be done. Otherwise however, a setting like this becomes necessary so that the permitted GID may be mapped for the builder that has only some random high UID/GID by default.

A permissioned nixbld supplementary group (in the presence of auto-allocated UIDs) is perhaps the main use of this setting.

>
> When using [`build-users-group`](#conf-build-users-group),
> supplementary groups should instead be specified directly in the
> user definitions.
Copy link
Member

Choose a reason for hiding this comment

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

I guess "user definitions" refers to /etc/group here? Wasn't entirely clear.

Also, wouldn't it make sense to apply supplementary-groups regardless of whether we're using auto-allocate-uids? The only thing that wouldn't work is the gid remapping.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's correct. I'll revise the wording.

No argument there, this is now only focused to the auto-allocate-uids case because that's the simpler situation. Build users also technically have their individual groups as well when the users are defined in the host's user database.

@SimSaladin SimSaladin force-pushed the linux-auto-allocate-supplementary-groups branch 2 times, most recently from 1e06a03 to d9c2fbe Compare July 17, 2025 14:54
@SimSaladin SimSaladin force-pushed the linux-auto-allocate-supplementary-groups branch 2 times, most recently from e1a7d5c to 87fbe0d Compare July 18, 2025 00:35
@SimSaladin
Copy link
Contributor Author

The current changeset now enables supplementary groups regardless of the auto-allocate-uids setting.

When static build users are used, additional groups that the build user is a member of are mapped automatically. (Not sure whether this should be opt-in instead? Adding additional groups for build users is kind of intentional already.)

Some of the changes here go beyond managing supplementary groups now. This is to facilitate idmapping for bind mounts as implemented here: SimSaladin@3351c65. That feature builds on top of #13315 and also this PR.

@SimSaladin SimSaladin force-pushed the linux-auto-allocate-supplementary-groups branch 2 times, most recently from 57a49fb to 1258661 Compare July 21, 2025 16:40
Add a new option "supplementary-groups" that allows specifying
additional groups to be mapped into the build sandbox and assigned as
supplementary groups for the build user.

This makes it possible to, for example, use the "kvm" group to provide
access to /dev/kvm even when auto-allocate-uids is enabled—something
that was not previously possible. It also enables use of supplementary
groups to grant sandboxed builds read-only access to secrets or other
shared resources.

Closes: NixOS#9276
Signed-off-by: Samuli Thomasson <samuli.thomasson@pm.me>
@SimSaladin SimSaladin force-pushed the linux-auto-allocate-supplementary-groups branch from 1258661 to 7fd1793 Compare August 9, 2025 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

auto-allocate-uids mounts extra-sandbox-paths as nobody:nogroup instead of nobody:nixbld
2 participants