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

Nix build processes on non-NixOS have nogroup in their supplementary groups #3245

Open
Twey opened this issue Nov 26, 2019 · 14 comments
Open

Comments

@Twey
Copy link

Twey commented Nov 26, 2019

Nix on non-NixOS seems to add nogroup to the build process's supplementary groups, even though nixbld isn't actually a member of nogroup in the sandbox.

NixOS (note the lack of 64434(nogroup) at the end of the id line):

$ nix-build -E 'with import <nixpkgs> { }; runCommand "foo" { } "id"'
these derivations will be built:
  /nix/store/cw1c5slf25gjjzpmlwdv44gwdx8kqivc-foo.drv
building '/nix/store/cw1c5slf25gjjzpmlwdv44gwdx8kqivc-foo.drv'...
uid=1000(nixbld) gid=100(nixbld) groups=100(nixbld)

Arch (thanks @paddygord):

$ nix-build -E 'with import <nixpkgs> { }; runCommand "foo" { } "id"'
these derivations will be built:
  /nix/store/scia1pcb1bpwsb026dr9vjzcr4qvc1zr-foo.drv
building '/nix/store/scia1pcb1bpwsb026dr9vjzcr4qvc1zr-foo.drv'...
uid=1000(nixbld) gid=100(nixbld) groups=100(nixbld),65534(nogroup)

Ubuntu 18.04 (thanks @tiagolam):

$ nix-build -E 'with import <nixpkgs> { }; runCommand "foo" { } "id"'
these derivations will be built:
  /nix/store/pqjsbxhs0nmgnxfz3l5yn7jdycvvsp4l-foo.drv
building '/nix/store/pqjsbxhs0nmgnxfz3l5yn7jdycvvsp4l-foo.drv'...
uid=1000(nixbld) gid=100(nixbld) groups=100(nixbld),65534(nogroup)

We're all using Nix 2.3.1.

This breaks tests for Go (e.g. go_bootstrap in nixpkgs): the Go test suite includes a test TestChown that attempts to change the group for each supplementary group given by getgroups, but this fails for nogroup. The failure looks like so:

--- FAIL: TestChown (0.00s)
        os_unix_test.go:51: gid: 100
        os_unix_test.go:63: groups:  [65534 65534 65534 65534 65534 65534 65534 100]
        os_unix_test.go:66: chown /tmp/_Go_TestChown460997763 -1 65534: chown /tmp/_Go_TestChown460997763: invalid argument
FAIL
FAIL    os      0.507s

/etc/passwd in the sandbox looks like this (on both NixOS and non-NixOS):

root:x:0:0:Nix build user:/build:/noshell
nixbld:x:1000:100:Nix build user:/build:/noshell
nobody:x:65534:65534:Nobody:/:/noshell

and /etc/group looks like this:

root:x:0:
nixbld:!:100:
nogroup:x:65534:
@zimbatm
Copy link
Member

zimbatm commented Nov 26, 2019

Can you also cat /etc/group and cat /etc/passwd in the derivation?

@Twey
Copy link
Author

Twey commented Nov 26, 2019

I edited the description to add some more information about the test failure and sandbox environment. :)

@7c6f434c
Copy link
Member

It kind of looks like before entering the sandbox there was an extra group (or maybe nixbld-outside is not the same as inside?) that is not mapped/doesn't correspond to anything (on the GID level) inside the sandbox user NS.

This is a debugging direction proposal, not a real answer. of course.

@Twey
Copy link
Author

Twey commented Nov 27, 2019

Sorry, it looks like when I edited the description most recently GitHub actually had me edit an old version, and some information was lost. I've put it back.

@7c6f434c
Copy link
Member

Are you running Nix in a single-user mode? How many groups you user id has?

@Twey
Copy link
Author

Twey commented Nov 28, 2019

No @7c6f434c, all of the above are in multi-user mode (for the non-NixOS users bog-standard Nix installs, by running the installer script from the site).

I (NixOS) have six; the Arch user has four; the Ubuntu user has nine.

@Twey
Copy link
Author

Twey commented Nov 28, 2019

(nobody is in nogroup, of course)

The Nix daemon shouldn't know anything about our user groups, though, right?

@edolstra
Copy link
Member

I can't reproduce this on Ubuntu 18.04 or 19.04.

Note however that this is expected behaviour when using a chroot store as a non-root user. To quote from build.cc:

            /* Drop additional groups here because we can't do it
               after we've created the new user namespace.  FIXME:
               this means that if we're not root in the parent
               namespace, we can't drop additional groups; they will
               be mapped to nogroup in the child namespace. There does
               not seem to be a workaround for this. (But who can tell
               from reading user_namespaces(7)?)
               See also https://lwn.net/Articles/621612/. */
            if (getuid() == 0 && setgroups(0, 0) == -1)
                throw SysError("setgroups failed");

@7c6f434c
Copy link
Member

7c6f434c commented Dec 1, 2019

Hm, seven copies of nogroup does kind of matches one-main and six-additional groups… but if you are using Nix-daemon…

For further debugging I would consider starting some very slow build (sleep 1d or something), enumerating all the visible processes in the nix-build and nix-daemon process trees, and doing 'grep '^G' /proc/[PID]/status' for each. For bonus points, maybe do the same with all the processes visible inside the build environment…

@yshui
Copy link
Contributor

yshui commented May 14, 2020

This is because group mapping is not properly setup in a UID namespace.

@weeezes
Copy link

weeezes commented Dec 4, 2020

Thanks for the tips in this issue 👍. What I ended up doing was take my user out of all groups except its primary one just to work around this for now. Seems like there's an issue also open in the Go repository, which gave me the tip that these specific tests break because of the nogroup group. IMO seems like it's more of an issue with the tests in Go, and this just happens to trip on a bug in it.

@stale
Copy link

stale bot commented Jun 3, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Jun 3, 2021
zowoq pushed a commit to c00w/nixpkgs that referenced this issue Feb 3, 2022
zowoq pushed a commit to NixOS/nixpkgs that referenced this issue Feb 4, 2022
zowoq pushed a commit to NixOS/nixpkgs that referenced this issue Feb 4, 2022
kalbasit pushed a commit to NixOS/nixpkgs that referenced this issue May 28, 2022
Workaround for <golang/go#42525>

(Also related to <NixOS/nix#3245>)

(cherry picked from commit af3cd7c)
kalbasit pushed a commit to NixOS/nixpkgs that referenced this issue May 28, 2022
Workaround for <golang/go#42525>

(Also related to <NixOS/nix#3245>)

(cherry picked from commit a66d9c8)
@yshui
Copy link
Contributor

yshui commented Nov 28, 2022

Still hitting this after 3 years...

nix should drop the supplementary groups with setgroups, or probably setup gid map to map them to something. (see below)

@stale stale bot removed the stale label Nov 28, 2022
@yshui
Copy link
Contributor

yshui commented Nov 29, 2022

After some researching on user namespace, i think this should be closed as wontfix. In summary, for security reason, an unprivileged process is not allowed to drop groups, or setup gid mappings inside the namespace for anything other than it's primary group. (references 1 and 2)

I think the best course of action is to just warn the user when they are not building using the nix daemon.

yshui added a commit to yshui/nix that referenced this issue Nov 30, 2022
We need root permission to drop supplementary groups, and if we don't do
that, some builds can fail in user namespace, most noteably go.

Related: NixOS#3245
yshui added a commit to yshui/nix that referenced this issue Nov 30, 2022
We need root permission to drop supplementary groups, and if we don't do
that, some builds can fail in user namespace, most notably go.

Related: NixOS#3245
yshui added a commit to yshui/nix that referenced this issue Nov 30, 2022
We need root permission to drop supplementary groups, and if we don't do
that, some builds can fail in user namespace, most notably go.

Related: NixOS#3245
rkaippully pushed a commit to awakesecurity/nixpkgs that referenced this issue Jul 3, 2023
jsoo1 pushed a commit to awakesecurity/nixpkgs that referenced this issue Jul 3, 2023
yshui added a commit to yshui/nix that referenced this issue Jan 20, 2024
We need root permission to drop supplementary groups, and if we don't do
that, some builds can fail in user namespace, most notably go.

Related: NixOS#3245
yshui added a commit to yshui/nix that referenced this issue Jan 20, 2024
We need root permission to drop supplementary groups, and if we don't do
that, some builds can fail in user namespace, most notably go.

Related: NixOS#3245
yshui added a commit to yshui/nix that referenced this issue Mar 29, 2024
We need root permission to drop supplementary groups, and if we don't do
that, some builds can fail in user namespace, most notably go.

Related: NixOS#3245
yshui added a commit to yshui/nix that referenced this issue Jul 31, 2024
We need root permission to drop supplementary groups, and if we don't do
that, some builds can fail in user namespace, most notably go.

Related: NixOS#3245
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants