2014-07-15 07:46:51 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2018-01-13 15:39:28 +08:00
|
|
|
"os"
|
2014-07-15 07:46:51 +08:00
|
|
|
"os/exec"
|
2020-05-04 21:53:33 +08:00
|
|
|
"sync"
|
2014-07-15 07:55:49 +08:00
|
|
|
"unsafe"
|
2017-05-10 05:38:27 +08:00
|
|
|
|
2018-01-16 14:11:56 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/user"
|
2017-05-10 05:38:27 +08:00
|
|
|
"golang.org/x/sys/unix"
|
2014-07-15 07:46:51 +08:00
|
|
|
)
|
|
|
|
|
2015-02-07 04:48:57 +08:00
|
|
|
type ParentDeathSignal int
|
|
|
|
|
|
|
|
func (p ParentDeathSignal) Restore() error {
|
|
|
|
if p == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
current, err := GetParentDeathSignal()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p == current {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p.Set()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p ParentDeathSignal) Set() error {
|
|
|
|
return SetParentDeathSignal(uintptr(p))
|
|
|
|
}
|
|
|
|
|
2014-07-15 07:46:51 +08:00
|
|
|
func Execv(cmd string, args []string, env []string) error {
|
|
|
|
name, err := exec.LookPath(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:05:10 +08:00
|
|
|
return unix.Exec(name, args, env)
|
2014-07-15 07:46:51 +08:00
|
|
|
}
|
2014-07-15 07:55:49 +08:00
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
func Prlimit(pid, resource int, limit unix.Rlimit) error {
|
|
|
|
_, _, err := unix.RawSyscall6(unix.SYS_PRLIMIT64, uintptr(pid), uintptr(resource), uintptr(unsafe.Pointer(&limit)), uintptr(unsafe.Pointer(&limit)), 0, 0)
|
2016-03-25 23:03:30 +08:00
|
|
|
if err != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-07 04:48:57 +08:00
|
|
|
func SetParentDeathSignal(sig uintptr) error {
|
2017-07-03 20:05:28 +08:00
|
|
|
if err := unix.Prctl(unix.PR_SET_PDEATHSIG, sig, 0, 0, 0); err != nil {
|
2014-07-15 07:55:49 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-07 04:48:57 +08:00
|
|
|
func GetParentDeathSignal() (ParentDeathSignal, error) {
|
2014-07-15 07:55:49 +08:00
|
|
|
var sig int
|
2017-07-03 20:05:28 +08:00
|
|
|
if err := unix.Prctl(unix.PR_GET_PDEATHSIG, uintptr(unsafe.Pointer(&sig)), 0, 0, 0); err != nil {
|
2014-07-15 07:55:49 +08:00
|
|
|
return -1, err
|
|
|
|
}
|
2015-02-07 04:48:57 +08:00
|
|
|
return ParentDeathSignal(sig), nil
|
2014-07-15 07:55:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetKeepCaps() error {
|
2017-07-03 20:05:28 +08:00
|
|
|
if err := unix.Prctl(unix.PR_SET_KEEPCAPS, 1, 0, 0, 0); err != nil {
|
2014-07-15 07:55:49 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClearKeepCaps() error {
|
2017-07-03 20:05:28 +08:00
|
|
|
if err := unix.Prctl(unix.PR_SET_KEEPCAPS, 0, 0, 0, 0); err != nil {
|
2014-07-15 07:55:49 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Setctty() error {
|
2017-07-03 20:05:36 +08:00
|
|
|
if err := unix.IoctlSetInt(0, unix.TIOCSCTTY, 0); err != nil {
|
2014-07-15 07:55:49 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-09 02:19:00 +08:00
|
|
|
|
2020-05-04 21:53:33 +08:00
|
|
|
var (
|
|
|
|
inUserNS bool
|
|
|
|
nsOnce sync.Once
|
|
|
|
)
|
|
|
|
|
2016-04-12 16:12:23 +08:00
|
|
|
// RunningInUserNS detects whether we are currently running in a user namespace.
|
2018-01-16 14:11:56 +08:00
|
|
|
// Originally copied from github.com/lxc/lxd/shared/util.go
|
2016-01-09 02:19:00 +08:00
|
|
|
func RunningInUserNS() bool {
|
2020-05-04 21:53:33 +08:00
|
|
|
nsOnce.Do(func() {
|
|
|
|
uidmap, err := user.CurrentProcessUIDMap()
|
|
|
|
if err != nil {
|
|
|
|
// This kernel-provided file only exists if user namespaces are supported
|
|
|
|
return
|
|
|
|
}
|
|
|
|
inUserNS = UIDMapInUserNS(uidmap)
|
|
|
|
})
|
|
|
|
return inUserNS
|
2018-01-16 14:11:56 +08:00
|
|
|
}
|
2016-01-09 02:19:00 +08:00
|
|
|
|
2018-01-16 14:11:56 +08:00
|
|
|
func UIDMapInUserNS(uidmap []user.IDMap) bool {
|
2016-01-09 02:19:00 +08:00
|
|
|
/*
|
|
|
|
* We assume we are in the initial user namespace if we have a full
|
|
|
|
* range - 4294967295 uids starting at uid 0.
|
|
|
|
*/
|
2018-01-16 14:11:56 +08:00
|
|
|
if len(uidmap) == 1 && uidmap[0].ID == 0 && uidmap[0].ParentID == 0 && uidmap[0].Count == 4294967295 {
|
2016-01-09 02:19:00 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2016-02-16 19:55:26 +08:00
|
|
|
|
2018-01-13 15:39:28 +08:00
|
|
|
// GetParentNSeuid returns the euid within the parent user namespace
|
2018-06-15 01:59:28 +08:00
|
|
|
func GetParentNSeuid() int64 {
|
|
|
|
euid := int64(os.Geteuid())
|
2018-01-13 15:39:28 +08:00
|
|
|
uidmap, err := user.CurrentProcessUIDMap()
|
|
|
|
if err != nil {
|
|
|
|
// This kernel-provided file only exists if user namespaces are supported
|
|
|
|
return euid
|
|
|
|
}
|
|
|
|
for _, um := range uidmap {
|
|
|
|
if um.ID <= euid && euid <= um.ID+um.Count-1 {
|
|
|
|
return um.ParentID + euid - um.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return euid
|
|
|
|
}
|
|
|
|
|
2016-03-15 06:46:08 +08:00
|
|
|
// SetSubreaper sets the value i as the subreaper setting for the calling process
|
|
|
|
func SetSubreaper(i int) error {
|
2020-04-23 09:45:40 +08:00
|
|
|
return unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, uintptr(i), 0, 0, 0)
|
2016-02-16 19:55:26 +08:00
|
|
|
}
|
2017-12-15 02:12:32 +08:00
|
|
|
|
|
|
|
// GetSubreaper returns the subreaper setting for the calling process
|
|
|
|
func GetSubreaper() (int, error) {
|
|
|
|
var i uintptr
|
|
|
|
|
|
|
|
if err := unix.Prctl(unix.PR_GET_CHILD_SUBREAPER, uintptr(unsafe.Pointer(&i)), 0, 0, 0); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(i), nil
|
|
|
|
}
|