nixpkgs icon indicating copy to clipboard operation
nixpkgs copied to clipboard

bwrap: Unexpected capabilities but not setuid when buildFHSEnv apps run under pam_systemd-based sessions

Open WillPower3309 opened this issue 5 months ago • 2 comments

Nixpkgs version

  • Unstable (26.11)

Describe the bug

When systemd starts a service with both User= and PAMName=, it calls pam_open_session() before setuid(). The pam_systemd.so module sets PR_SET_KEEPCAPS while still root, which preserves the full permitted capability set across the subsequent setuid(user). When bwrap's acquire_privs() → has_caps() sees non-zero permitted caps, it errors — this check exists to catch misconfigured setcap binaries, but PR_SET_KEEPCAPS from PAM triggers it spuriously.

Evidence:

  • runuser -u user (no pam_systemd) → bwrap works, capget yields permitted=0
  • PAMName=cage (includes pam_systemd) → cage process shows CapInh != 0 (KEEPCAPS was active), bwrap fails
  • Error source: bubblewrap.c:889-894:
else if (real_uid != 0 && has_caps ())
    die ("Unexpected capabilities but not setuid, old file caps config?");

Steps to reproduce

The following config is enough to reproduce:

    services.cage = {
      inherit user;
      enable = true;
      program = "${pkgs.plex-htpc}/bin/plex-htpc";
    };

Expected behaviour

The service works!

Screenshots

No response

Relevant log output

Failure log:

Jun 18 18:35:13 tv cage[1393]: bwrap: Unexpected capabilities but not setuid, old file caps config?

Additional context

Fix options:

  1. bubblewrap: Change has_caps() to ignore capabilities that came from PR_SET_KEEPCAPS (only reject file-set capabilities via setcap)
  2. pam_systemd: Don't call PR_SET_KEEPCAPS when the caller is a systemd service that will setuid() — the capability preservation is unintentional here
  3. buildFHSEnv: Set --unshare-user by default to clear capabilities inside a new user namespace

Workaround:

{ config, pkgs, lib, ... }: {
  security.wrappers.bwrap = {
    setuid = true;
    owner = "root";
    group = "root";
    source = "${pkgs.bubblewrap}/bin/bwrap";
  };
  nixpkgs.overlays = [
    (final: prev: {
      bubblewrap = prev.bubblewrap.overrideAttrs (old: {
        mesonFlags = (old.mesonFlags or [ ]) ++ [ "-Dsupport_setuid=true" ];
      });
      buildFHSEnv = prev.buildFHSEnv.override {
        bubblewrap = "/run/wrappers";
      };
    })
  ];
}

This issue was RCed with AI

System metadata

$ nix-shell -p nix-info --run "nix-info -m"
warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels' does not exist, ignoring
these 7 paths will be fetched (1.7 MiB download, 15.0 MiB unpacked):
  /nix/store/kyz3mm5snbb8998kbkm28jps1phk9509-file-5.47
  /nix/store/zmvllxxx62iys7vpyg020rni3v29bcxi-gnu-config-2024-01-01
  /nix/store/vlq7nnw39j7rwk0pp68w1fcwzpxahm9h-gnumake-4.4.1
  /nix/store/k5akwnrn9x2afaj2va7g4a2zpdim8l43-gnutar-1.35
  /nix/store/ilblcn1dkvzghcr2yk3av6jxn5rk1iqw-patchelf-0.15.2
  /nix/store/jci7gw90lh2vdjaxkb6pzf9xp4v08wzs-stdenv-linux
  /nix/store/xknj6c33cc197s60ry0i69vdkmaizrs1-update-autotools-gnu-config-scripts-hook
copying path '/nix/store/zmvllxxx62iys7vpyg020rni3v29bcxi-gnu-config-2024-01-01' from 'https://cache.nixos.org'...
copying path '/nix/store/kyz3mm5snbb8998kbkm28jps1phk9509-file-5.47' from 'https://cache.nixos.org'...
copying path '/nix/store/k5akwnrn9x2afaj2va7g4a2zpdim8l43-gnutar-1.35' from 'https://cache.nixos.org'...
copying path '/nix/store/vlq7nnw39j7rwk0pp68w1fcwzpxahm9h-gnumake-4.4.1' from 'https://cache.nixos.org'...
copying path '/nix/store/ilblcn1dkvzghcr2yk3av6jxn5rk1iqw-patchelf-0.15.2' from 'https://cache.nixos.org'...
copying path '/nix/store/xknj6c33cc197s60ry0i69vdkmaizrs1-update-autotools-gnu-config-scripts-hook' from 'https://cache.nixos.org'...
copying path '/nix/store/jci7gw90lh2vdjaxkb6pzf9xp4v08wzs-stdenv-linux' from 'https://cache.nixos.org'...
 - system: `"x86_64-linux"`
 - host os: `Linux 6.18.35, NixOS, 26.11 (Zokor), 26.11.20260616.3e41b24`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.34.7`
 - nixpkgs: `/nix/store/j2r11kxv91yl5xqppy3vy84klwxjbz1i-source`

Notify maintainers


Note for maintainers: Please tag this issue in your pull request description. (i.e. Resolves #ISSUE.)

I assert that this issue is relevant for Nixpkgs

Is this issue important to you?

Add a :+1: reaction to issues you find important.

WillPower3309 avatar Jun 18 '26 23:06 WillPower3309

Seems like the same RC as #523427

WillPower3309 avatar Jun 18 '26 23:06 WillPower3309

i believe ive encounted the same issue on Fedora IoT 44 (OS-Tree) when trying to setup a cage-based kiosk

MandoCoding avatar Oct 06 '26 12:10 MandoCoding