-
Notifications
You must be signed in to change notification settings - Fork 56
userns: detect initial namespace by inode #240
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,12 @@ import ( | |
| "fmt" | ||
| "os" | ||
| "sync" | ||
| "syscall" | ||
| ) | ||
|
|
||
| // See PROC_USER_INIT_INO in https://github.com/torvalds/linux/blob/v7.1/include/uapi/linux/nsfs.h#L50. | ||
| const procUserInitIno = 0xEFFFFFFD | ||
|
|
||
| var inUserNS = sync.OnceValue(runningInUserNS) | ||
|
|
||
| // runningInUserNS detects whether we are currently running in a user namespace. | ||
|
|
@@ -17,6 +21,15 @@ var inUserNS = sync.OnceValue(runningInUserNS) | |
| // [libcontainer/runc]: https://github.com/opencontainers/runc/blob/3778ae603c706494fd1e2c2faf83b406e38d687d/libcontainer/userns/userns_linux.go#L12-L49 | ||
| // [lcx/incus]: https://github.com/lxc/incus/blob/e45085dd42f826b3c8c3228e9733c0b6f998eafe/shared/util.go#L678-L700 | ||
| func runningInUserNS() bool { | ||
| var st syscall.Stat_t | ||
| if err := syscall.Stat("/proc/self/ns/user", &st); err == nil { | ||
| return st.Ino != procUserInitIno | ||
| } else if !os.IsNotExist(err) { | ||
| return false | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should still fall back to the legacy method?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I can do this... but i think this might be the wrong way of doing it... consider thisL If the file "/proc/self/ns/user" is there, it means a modern kernel, so if stat fails for any other reason than "file does not exists" it mens that you are still on a modern kernel but something unexpected is wrong with /proc, permissions, mount state, or namespace access, its safer to assume that you are not in the init namespace than to fallback in the old criteria (specially if you plan to remove this line once older kernel are not supported) ... |
||
| } | ||
|
|
||
| // TODO: Remove this fallback once Linux kernels older than 3.8 are no | ||
| // longer supported. | ||
| file, err := os.Open("/proc/self/uid_map") | ||
| if err != nil { | ||
| // This kernel-provided file only exists if user namespaces are supported. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.