sandbox: cache kernel.cap_last_cap value
Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
2dd49c437c
commit
8b69bcd215
@ -1,37 +0,0 @@
|
||||
package sandbox
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
ofUid int
|
||||
ofGid int
|
||||
ofOnce sync.Once
|
||||
)
|
||||
|
||||
const (
|
||||
ofUidPath = "/proc/sys/kernel/overflowuid"
|
||||
ofGidPath = "/proc/sys/kernel/overflowgid"
|
||||
)
|
||||
|
||||
func mustReadOverflow() {
|
||||
if v, err := os.ReadFile(ofUidPath); err != nil {
|
||||
log.Fatalf("cannot read %q: %v", ofUidPath, err)
|
||||
} else if ofUid, err = strconv.Atoi(string(bytes.TrimSpace(v))); err != nil {
|
||||
log.Fatalf("cannot interpret %q: %v", ofUidPath, err)
|
||||
}
|
||||
|
||||
if v, err := os.ReadFile(ofGidPath); err != nil {
|
||||
log.Fatalf("cannot read %q: %v", ofGidPath, err)
|
||||
} else if ofGid, err = strconv.Atoi(string(bytes.TrimSpace(v))); err != nil {
|
||||
log.Fatalf("cannot interpret %q: %v", ofGidPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
func OverflowUid() int { ofOnce.Do(mustReadOverflow); return ofUid }
|
||||
func OverflowGid() int { ofOnce.Do(mustReadOverflow); return ofGid }
|
47
sandbox/sysctl.go
Normal file
47
sandbox/sysctl.go
Normal file
@ -0,0 +1,47 @@
|
||||
package sandbox
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
kernelOverflowuid int
|
||||
kernelOverflowgid int
|
||||
kernelCapLastCap int
|
||||
|
||||
sysctlOnce sync.Once
|
||||
)
|
||||
|
||||
const (
|
||||
kernelOverflowuidPath = "/proc/sys/kernel/overflowuid"
|
||||
kernelOverflowgidPath = "/proc/sys/kernel/overflowgid"
|
||||
kernelCapLastCapPath = "/proc/sys/kernel/cap_last_cap"
|
||||
)
|
||||
|
||||
func mustReadSysctl() {
|
||||
if v, err := os.ReadFile(kernelOverflowuidPath); err != nil {
|
||||
log.Fatalf("cannot read %q: %v", kernelOverflowuidPath, err)
|
||||
} else if kernelOverflowuid, err = strconv.Atoi(string(bytes.TrimSpace(v))); err != nil {
|
||||
log.Fatalf("cannot interpret %q: %v", kernelOverflowuidPath, err)
|
||||
}
|
||||
|
||||
if v, err := os.ReadFile(kernelOverflowgidPath); err != nil {
|
||||
log.Fatalf("cannot read %q: %v", kernelOverflowgidPath, err)
|
||||
} else if kernelOverflowgid, err = strconv.Atoi(string(bytes.TrimSpace(v))); err != nil {
|
||||
log.Fatalf("cannot interpret %q: %v", kernelOverflowgidPath, err)
|
||||
}
|
||||
|
||||
if v, err := os.ReadFile(kernelCapLastCapPath); err != nil {
|
||||
log.Fatalf("cannot read %q: %v", kernelCapLastCapPath, err)
|
||||
} else if kernelCapLastCap, err = strconv.Atoi(string(bytes.TrimSpace(v))); err != nil {
|
||||
log.Fatalf("cannot interpret %q: %v", kernelCapLastCapPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
func OverflowUid() int { sysctlOnce.Do(mustReadSysctl); return kernelOverflowuid }
|
||||
func OverflowGid() int { sysctlOnce.Do(mustReadSysctl); return kernelOverflowgid }
|
||||
func LastCap() uintptr { sysctlOnce.Do(mustReadSysctl); return uintptr(kernelCapLastCap) }
|
Loading…
Reference in New Issue
Block a user