package container import ( "bytes" "fmt" "os" "strconv" "sync" "hakurei.app/fhs" ) // mustSysctlInt reads a sysctl path via proc, interprets it as a decimal // representation of an integer, and terminates the program if the read was // unsuccessful or the response is invalid. func mustSysctlInt(path string) (v int) { p, err := os.ReadFile(fhs.ProcSys + path) if err != nil { fmt.Println(err.Error()) os.Exit(1) } if v, err = strconv.Atoi(string(bytes.TrimSpace(p))); err != nil { fmt.Printf("invalid sysctl response %q\n", string(p)) os.Exit(1) } return } // cached sysctl responses for populateSysctl var ( kernelOverflowuid int kernelOverflowgid int kernelCapLastCap int sysctlOnce sync.Once ) // populateSysctl populates cached sysctl responses guarded by sysctlOnce. func populateSysctl() { sysctlOnce.Do(func() { kernelOverflowuid = mustSysctlInt("kernel/overflowuid") kernelOverflowgid = mustSysctlInt("kernel/overflowgid") kernelCapLastCap = mustSysctlInt("kernel/cap_last_cap") }) } // OverflowUID returns the fixed uid returned by legacy 16-bit UID system calls. // This value is also returned for unmapped UIDs in a user namespace. func OverflowUID() int { populateSysctl() return kernelOverflowuid } // OverflowGID is like [OverflowUID], but for GID instead. func OverflowGID() int { populateSysctl() return kernelOverflowgid } // lastcap returns the highest valid capability of the running kernel. Exports // CAP_LAST_CAP from the kernel. func lastcap() uintptr { populateSysctl() return uintptr(kernelCapLastCap) }