Skip to content

userns.RunningInUserNS() misdetects systemd PrivateUsers=full as the initial user namespace #239

Closed

Description

@aleivag

Hi maintainers,

github.com/moby/sys/userns.RunningInUserNS() currently detects the initial user namespace by parsing /proc/self/uid_map:

// userns_linux.go
func uidMapInUserNS(uidMap string) bool {
    ...
    // As per user_namespaces(7), /proc/self/uid_map of
    // the initial user namespace shows 0 0 4294967295.
    initNS := a == 0 && b == 0 && c == 4294967295
    return !initNS
}

That assumes the only namespace that can contain 4294967295 UIDs is the initial namespace. However, uid_map describes ID mapping, not namespace identity. A non-initial user namespace can have a full identity map.

This now matters with systemd >= 260. (from: systemd/systemd@24c73c7) A service using PrivateUsers=full can run in a non-initial user namespace while still having a full identity UID/GID map:

0 0 4294967295

In that case, RunningInUserNS() returns false even though the process is in a user namespace.

We hit this through runc, which vendors moby/sys/userns. runc's cgroup-v2 device controller intentionally skips/ignores some eBPF device-controller failures when running in a user namespace:

// vendor/github.com/opencontainers/cgroups/devices/v2.go
func canSkipEBPFError(r *cgroups.Resources) bool {
    if userns.RunningInUserNS() {
        return true
    }
    ...
}

Because RunningInUserNS() returns false under PrivateUsers=full, runc does not take that userns path and instead fails nested container startup with:

docker: Error response from daemon: failed to create task for container:
failed to create shim task: OCI runtime create failed: runc create failed:
unable to start container process: error during container init:
error setting cgroup config for procHooks process:
bpf_prog_query(BPF_CGROUP_DEVICE) failed: operation not permitted

...
The more robust check seems to be comparing /proc/self/ns/user to the kernel's initial user namespace inode, PROC_USER_INIT_INO (0xEFFFFFFD), rather than parsing /proc/self/uid_map. This is what systemd does, and what i propose we do in crun in containers/crun#2150

Would you accept a patch changing RunningInUserNS() to use the namespace inode check instead of the uid_map full-range heuristic?

Activity

  1. thaJeztah commented on Jul 27, 2026

    @thaJeztah
    Member

    Thanks for reporting; contributions are definitely welcome; ISTR this code (which was extracted from runc) originally came from lxc, which also looks to still have the same logic, so probably needs to be adjusted as well? https://github.com/lxc/incus/blob/e45085dd42f826b3c8c3228e9733c0b6f998eafe/shared/util.go#L678-L700

    cc @AkihiroSuda

  2. AkihiroSuda commented on Jul 28, 2026

    @AkihiroSuda
    Member

    A service using PrivateUsers=full can run in a non-initial user namespace while still having a full identity UID/GID map

    How is this useful?

    PROC_USER_INIT_INO (0xEFFFFFFD)

    SGTM

  3. aleivag commented on Jul 28, 2026

    @aleivag
    ContributorAuthor

    How is this useful?

    when you run a unit with this setting, It removes all capabilities of the host userspace , but has full capabilities within the service's user namespace. At meta we run containers inside this types of transient units...

  4. AkihiroSuda commented on Jul 28, 2026

    @AkihiroSuda
    Member

    But why map the host root to the user-namespaced root?

  5. aleivag commented on Jul 29, 2026

    @aleivag
    ContributorAuthor

    But why map the host root to the user-namespaced root?

    Many container/runtime payloads expect to run as uid 0 and to have uid 0 capabilities for operations inside their own namespaces.

    Mapping uid 0 to uid 0 keeps file ownership, bind mounts, and root-oriented setup code working normally.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions