Skip to content

userns: detect initial namespace by inode - #240

Open
aleivag wants to merge 1 commit into
moby:mainfrom
aleivag:fix-userns-detection-proc-inode
Open

userns: detect initial namespace by inode#240
aleivag wants to merge 1 commit into
moby:mainfrom
aleivag:fix-userns-detection-proc-inode

Conversation

@aleivag

@aleivag aleivag commented Jul 28, 2026

Copy link
Copy Markdown

The current uid_map heuristic treats a private user namespace with an identity uid_map as the initial user namespace. systemd 260 can create that shape with PrivateUsers=full, causing callers to believe they can perform initial-namespace-only operations such as cgroup device BPF setup.

Use /proc/self/ns/user's inode instead and compare it with the kernel's PROC_USER_INIT_INO value. This detects the initial user namespace directly instead of inferring it from the uid_map layout.

Related: #239

Comment thread userns/userns_linux.go
"syscall"
)

const procUserInitIno = 0xEFFFFFFD

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@aleivag
aleivag force-pushed the fix-userns-detection-proc-inode branch from 3a4e7f4 to 372f6db Compare July 28, 2026 18:52
The current uid_map heuristic treats a private user namespace with an identity uid_map as the initial user namespace. systemd 260 can create that shape with PrivateUsers=full, causing callers to believe they can perform initial-namespace-only operations such as cgroup device BPF setup.

Use /proc/self/ns/user's inode instead and compare it with the kernel's PROC_USER_INIT_INO value. This detects the initial user namespace directly instead of inferring it from the uid_map layout.

Related: containers/crun#2150
Signed-off-by: Alvaro Leiva Geisse <aleivag@gmail.com>
Comment thread userns/userns_linux.go Outdated

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  1. I think that TestInodeInUserNS (and a separate func inodeInUserNS) makes little sense now when the code is very simple and clean. I mean, we just do stat(2) and compare st.Ino -- this needs no unit tests.

  2. The problem is, this (predefined inode number for PROC_USER_INIT_INO) only appears in kernel v3.8 (it comes from https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=98f842e675f9), and since Go itself (since Go 1.24) requires kernel v3.2 (see https://go.dev/wiki/MinimumRequirements#linuxlinux), we do need a fallback to old code. It will still work since most people run linux >= v3.8, and no one sane will run systemd v260+ on an older kernel.

@kolyshkin

Copy link
Copy Markdown
Collaborator

since Go itself (since Go 1.24) requires kernel v3.2

Note this package (github.com/moby/sys/userns) only requires Go 1.18 which itself has a lower linux kernel version requirement. Meaning, we definitely need the fallback.

@aleivag
aleivag force-pushed the fix-userns-detection-proc-inode branch from 372f6db to 41c4209 Compare July 29, 2026 04:53

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

See my comments above

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

Successfully merging this pull request may close these issues.

3 participants