blob: 2cf47258ef55176292a89497c514379a81885951 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
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)
}
|