Skip to content

SUID wrappers (i.e. sudo) do not work in FHSEnvs #69338

Open

Description

@JohnAZoidberg

Describe the bug
Running sudo inside the environment of buildFHSUserenv.env does not work.

To Reproduce
Steps to reproduce the behavior:

  1. Create shell.nix with
{ pkgs ? import <nixpkgs> {} }:

(pkgs.buildFHSUserEnv {
  name = "sudo-env";
  targetPkgs = pkgs: with pkgs; [ sudo ];
  runScript = "bash";
}).env
  1. Run nix-shell
  2. Run sudo whoami

Expected behavior
sudo whoami should print root

Actual behaviour

$ sudo whoami
sudo: /nix/store/v6l2sacryfr88yqq0pq7sia8wfgm9q31-wrapper.c:203: main: Assertion `!(st.st_mode & S_ISUID) || (st.st_uid == geteuid())' failed.
Aborted

Additional context
I think this is caused by the owner of sudo being nobody:nogroup:

# Inside FHS env
$ ls -l /run/wrappers/bin/sudo
-r-s--x--x 1 nobody nogroup 17704 Sep 22 13:08 /run/wrappers/bin/sudo
# Outside FHS env
$ ls -l /run/wrappers/bin/sudo
-r-s--x--x 1 root root 17704 Sep 22 15:08 /run/wrappers/bin/sudo

Metadata

  • system: "x86_64-linux"
  • host os: Linux 5.3.0, NixOS, 20.03pre193781.d484f2b7fc0 (Markhor)
  • multi-user?: yes
  • sandbox: no
  • version: nix-env (Nix) 2.3
  • nixpkgs: /nix/store/2p5njn4s0i93s9i8pwg276ibb15ynpyn-foo

which is this unstable channel nixos-20.03pre193781.d484f2b7fc0

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute: buildFHSUserEnv

cc @abbradar

Activity

  1. added
    0.kind: bug
    on Sep 24, 2019
  2. matthewbauer commented on Sep 28, 2019

    @matthewbauer
    Member

    This looks like any program with the SUID bit fails?

  3. abbradar commented on Oct 1, 2019

    @abbradar
    Member

    Yeah, that's an implication of us using mount namespaces inside buildFHSUserEnv. Unfortunately I don't think much could be done about it: the security check that the wrapper does avoids a scenario when you craft a malicious namespace where say /etc/passwd is mounted to a file you control, and gain user privileges via sudo.

  4. changed the title [-]sudo does not work in buildFHSUserenv.env[/-] [+]SUID wrappedrs do not work in buildFHSUserenv.env[/+] on Oct 6, 2019
  5. JohnAZoidberg commented on Oct 6, 2019

    @JohnAZoidberg
    MemberAuthor

    Yes, you're right. All SUID wrapped programs fail.

  6. cawilliamson commented on Dec 17, 2019

    @cawilliamson
    Member

    This is a really major blocker for me because I'm trying to package VMware Workstation 15 for NixOS (nixpkgs) and so far I only see two options:

    1.) Use an FHS environment to install VMware in to meaning to need to patch the binaries - blocked by this issue.

    2.) Use patchelf to patch the binaries - this then generates a large binary and patchelf complains it cannot patch it due to a max filesize limit so basically I can't win either way!

    Any ideas on how I can work around this in a way which can be pushed to nixpkgs? (i.e. not hacking random scripts and things because I obviously can't share that outside of my own system. :))

    cc @abbradar - any ideas about how I can address this? Completely blocked at the moment.

  7. changed the title [-]SUID wrappedrs do not work in buildFHSUserenv.env[/-] [+]SUID wrappers do not work in buildFHSUserenv.env[/+] on Jan 1, 2020
  8. stale commented on Jun 30, 2020

    @stale

    Thank you for your contributions.

    This has been automatically marked as stale because it has had no activity for 180 days.

    If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.

    Here are suggestions that might help resolve this more quickly:

    1. Search for maintainers and people that previously touched the related code and @ mention them in a comment.
    2. Ask on the NixOS Discourse.
    3. Ask on the #nixos channel on irc.freenode.net.
  9. added
    2.status: stale
    on Jun 30, 2020
  10. Atemu commented on Jul 10, 2020

    @Atemu
    Member

    Still important to me.

  11. removed
    2.status: stale
    on Jul 10, 2020
  12. Atemu commented on Jul 30, 2020

    @Atemu
    Member

    Could this be solved by "registering" the FHSUserenvs which are allowed to run SUID wrappers using a mechanism that requires root privileges?
    That's how SUID wrappers work outside of chrootenvs, right?

  13. setupminimal commented on Aug 1, 2020

    @setupminimal

    This is important to me too - it's necessary to get fuse working inside a FHS User Env. I'll keep this issue open while I look for alternatives, and report back if I find a workaround.

  14. noonien commented on Aug 26, 2020

    @noonien

    This is also important to me. Some programs don't work without FHS, and, some of those require access to root-only resources.

  15. eglaysher commented on Sep 15, 2020

    @eglaysher

    This is important to me since it's in the blocking path for Steam VR stuff.

  16. 5 remaining items

  17. Atemu commented on Aug 10, 2023

    @Atemu
    Member

    Just realised that this is, in fact, not a duplicate as the other issue was about dropping privileges when already privileged rather than gaining privileges as is the case here.

    Copying my idea from over there here:


    I've had a look into how Flatpak solves this issue and it seems like most apps use Polkit to authenticate. SteamVR (the main reason I'm here) uses it too.

    AFAIUI, it's an IPC-based privilege escalation mechanism which enables it to work in containers. It needs a privileged daemon on the host of course though.

    Perhaps we could build a sudo substitute on top of polkit?

  18. reopened this on Aug 10, 2023
  19. nixos-discourse commented on Jun 4, 2024

    @nixos-discourse
  20. Atemu commented on Jun 4, 2024

    @Atemu
    Member

    Perhaps we could build a sudo substitute on top of polkit?

    This will actually exists soon: Systemd 256 adds support for run0. You should be able to manually create your own "run0" using systemd-run already though if anyone wants to experiment.

  21. nixos-discourse commented on Jun 4, 2024

    @nixos-discourse
  22. Atemu commented on Jun 4, 2024

    @Atemu
    Member

    Okay, so apparently polkit (via pkexec) still requires SUID and will immediately exit with pkexec must be setuid root if it doesn't have SUID.

    But somehow this must be working within flatpak and friends?

    Perhaps it's safe to allow SUID in any case on polkit because it does its own internal userns-aware checking using a privileged daemon that cannot be fooled by a malicious userns? Figuring this out feels like the logical next step.

  23. zarelit commented on Jul 14, 2024

    @zarelit
    Member

    I'm linking this other issue on documenting suid wrappers because it's actually not clear how to proceed when packaging something that needs to access a suid binary, especially when this is inside any kind of jail

  24. yajo commented on Apr 22, 2025

    @yajo
    Contributor

    nix-shell -E '{pkgs ? import {} }: (pkgs.buildFHSUserEnv { name = "test"; targetPkgs = _: [];}).env'

    FWIW you can use sudo before entering that shell, and then you'll be root within it. However, you still won't be able to run sudo. Not sure if it helps:

     yajo  sudo nix-shell -E '{pkgs ? import <nixpkgs> {} }: (pkgs.buildFHSUserEnv { name = "test"; targetPkgs = pkgs: (with pkgs; [ sudo ]);}).env'
    
    [root@iceland:/home/yajo]# whoami
    root
    
    [root@iceland:/home/yajo]# sudo whoami
    sudo: unable to initialize PAM: Error crítico: cancelación inmediata
    
    [root@iceland:/home/yajo]# 
    exit
  25. farnoy commented on Sep 21, 2025

    @farnoy
    Member

    Has anyone figured out a replacement with systemd-run/run0? I wanted to create a policy so that commands with a prefix:

    systemd-run --scope -p AllowedCPUs=0-7 --slice=whatever.slice -- chrt -f 1 sudo -Eu $USER ...
    

    Would be auto-accepted by polkit (I think that should be secure except for the denial of service if you misconfigure AllowedCPUs? maybe needs hardcoded binary paths to /run/current-system/sw/bin?), but systemd-run uses the generic org.freedesktop.systemd1.manage-units policy which just exposes the unit name and verb - start in this case. So there's no way to match on the prefix and validate the specific command being run as far as I can see.

  26. nixos-discourse commented on Oct 29, 2025

    @nixos-discourse
  27. Atemu commented on Mar 8, 2026

    @Atemu
    Member

    Tried using run0 in an fhsenv and it threw:

    Failed to start transient service unit: Access denied as the requested operation requires interactive authentication. However, interactive authentication has not been enabled by the calling program.

    This appears to be related to pam and/or login not being done the way it should? systemd/systemd#37149

    Further context I have found:

    microsoft/WSL#9213 (cc @K900)
    https://gitlab.archlinux.org/archlinux/archlinux-wsl/-/issues/13

  28. changed the title [-]SUID wrappers do not work in buildFHSUserenv.env[/-] [+]SUID wrappers (i.e. `sudo`) do not work in FHSEnvs[/+] on Mar 8, 2026
  29. added
    2.status: stale
    on Sep 5, 2026
  30. added
    6.topic: architecture
    on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bug2.status: stale6.topic: architecture

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions