Skip to content

Commit 4316df8

Browse files
committed
libcontainer/system: move userns utilities to separate package
Moving these utilities to a separate package, so that consumers of this package don't have to pull in the whole "system" package. Looking at uses of these utilities (outside of runc itself); `RunningInUserNS()` is used by [various external consumers][1], so adding a "Deprecated" alias for this. [1]: https://grep.app/search?current=2&q=.RunningInUserNS Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent e7fd383 commit 4316df8

15 files changed

Lines changed: 70 additions & 57 deletions

checkpoint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
criu "github.com/checkpoint-restore/go-criu/v5/rpc"
1313
"github.com/opencontainers/runc/libcontainer"
14-
"github.com/opencontainers/runc/libcontainer/system"
14+
"github.com/opencontainers/runc/libcontainer/userns"
1515
"github.com/opencontainers/runtime-spec/specs-go"
1616
"github.com/sirupsen/logrus"
1717
"github.com/urfave/cli"
@@ -48,7 +48,7 @@ checkpointed.`,
4848
return err
4949
}
5050
// XXX: Currently this is untested with rootless containers.
51-
if os.Geteuid() != 0 || system.RunningInUserNS() {
51+
if os.Geteuid() != 0 || userns.RunningInUserNS() {
5252
logrus.Warn("runc checkpoint is untested with rootless containers")
5353
}
5454

libcontainer/cgroups/fs/devices.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
1313
"github.com/opencontainers/runc/libcontainer/configs"
1414
"github.com/opencontainers/runc/libcontainer/devices"
15-
"github.com/opencontainers/runc/libcontainer/system"
15+
"github.com/opencontainers/runc/libcontainer/userns"
1616
)
1717

1818
type DevicesGroup struct {
@@ -55,7 +55,7 @@ func buildEmulator(rules []*devices.Rule) (*cgroupdevices.Emulator, error) {
5555
}
5656

5757
func (s *DevicesGroup) Set(path string, cgroup *configs.Cgroup) error {
58-
if system.RunningInUserNS() || cgroup.SkipDevices {
58+
if userns.RunningInUserNS() || cgroup.SkipDevices {
5959
return nil
6060
}
6161

libcontainer/cgroups/fs2/devices.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/opencontainers/runc/libcontainer/cgroups/ebpf/devicefilter"
88
"github.com/opencontainers/runc/libcontainer/configs"
99
"github.com/opencontainers/runc/libcontainer/devices"
10-
"github.com/opencontainers/runc/libcontainer/system"
10+
"github.com/opencontainers/runc/libcontainer/userns"
1111

1212
"github.com/pkg/errors"
1313
"golang.org/x/sys/unix"
@@ -36,7 +36,7 @@ func canSkipEBPFError(cgroup *configs.Cgroup) bool {
3636
// have the necessary privileges to mknod(2) device inodes or access
3737
// host-level instances (though ideally we would be blocking device access
3838
// for rootless containers anyway).
39-
if system.RunningInUserNS() {
39+
if userns.RunningInUserNS() {
4040
return true
4141
}
4242

libcontainer/cgroups/systemd/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
1515
dbus "github.com/godbus/dbus/v5"
16-
"github.com/opencontainers/runc/libcontainer/system"
16+
"github.com/opencontainers/runc/libcontainer/userns"
1717
"github.com/pkg/errors"
1818
)
1919

@@ -52,7 +52,7 @@ func NewUserSystemdDbus() (*systemdDbus.Conn, error) {
5252
//
5353
// Otherwise returns os.Getuid() .
5454
func DetectUID() (int, error) {
55-
if !system.RunningInUserNS() {
55+
if !userns.RunningInUserNS() {
5656
return os.Getuid(), nil
5757
}
5858
b, err := exec.Command("busctl", "--user", "--no-pager", "status").CombinedOutput()

libcontainer/cgroups/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"time"
1717

1818
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
19-
"github.com/opencontainers/runc/libcontainer/system"
19+
"github.com/opencontainers/runc/libcontainer/userns"
2020
"github.com/sirupsen/logrus"
2121
"golang.org/x/sys/unix"
2222
)
@@ -37,7 +37,7 @@ func IsCgroup2UnifiedMode() bool {
3737
var st unix.Statfs_t
3838
err := unix.Statfs(unifiedMountpoint, &st)
3939
if err != nil {
40-
if os.IsNotExist(err) && system.RunningInUserNS() {
40+
if os.IsNotExist(err) && userns.RunningInUserNS() {
4141
// ignore the "not found" error if running in userns
4242
logrus.WithError(err).Debugf("%s missing, assuming cgroup v1", unifiedMountpoint)
4343
isUnified = false

libcontainer/rootfs_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/opencontainers/runc/libcontainer/cgroups"
2020
"github.com/opencontainers/runc/libcontainer/configs"
2121
"github.com/opencontainers/runc/libcontainer/devices"
22-
"github.com/opencontainers/runc/libcontainer/system"
22+
"github.com/opencontainers/runc/libcontainer/userns"
2323
"github.com/opencontainers/runc/libcontainer/utils"
2424
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
2525
"github.com/opencontainers/runtime-spec/specs-go"
@@ -603,7 +603,7 @@ func reOpenDevNull() error {
603603

604604
// Create the device nodes in the container.
605605
func createDevices(config *configs.Config) error {
606-
useBindMount := system.RunningInUserNS() || config.Namespaces.Contains(configs.NEWUSER)
606+
useBindMount := userns.RunningInUserNS() || config.Namespaces.Contains(configs.NEWUSER)
607607
oldMask := unix.Umask(0000)
608608
for _, node := range config.Devices {
609609

libcontainer/system/linux.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ package system
44

55
import (
66
"os/exec"
7-
"sync"
87
"unsafe"
98

10-
"github.com/opencontainers/runc/libcontainer/user"
119
"golang.org/x/sys/unix"
1210
)
1311

@@ -86,36 +84,6 @@ func Setctty() error {
8684
return nil
8785
}
8886

89-
var (
90-
inUserNS bool
91-
nsOnce sync.Once
92-
)
93-
94-
// RunningInUserNS detects whether we are currently running in a user namespace.
95-
// Originally copied from github.com/lxc/lxd/shared/util.go
96-
func RunningInUserNS() bool {
97-
nsOnce.Do(func() {
98-
uidmap, err := user.CurrentProcessUIDMap()
99-
if err != nil {
100-
// This kernel-provided file only exists if user namespaces are supported
101-
return
102-
}
103-
inUserNS = uidMapInUserNS(uidmap)
104-
})
105-
return inUserNS
106-
}
107-
108-
func uidMapInUserNS(uidmap []user.IDMap) bool {
109-
/*
110-
* We assume we are in the initial user namespace if we have a full
111-
* range - 4294967295 uids starting at uid 0.
112-
*/
113-
if len(uidmap) == 1 && uidmap[0].ID == 0 && uidmap[0].ParentID == 0 && uidmap[0].Count == 4294967295 {
114-
return false
115-
}
116-
return true
117-
}
118-
11987
// SetSubreaper sets the value i as the subreaper setting for the calling process
12088
func SetSubreaper(i int) error {
12189
return unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, uintptr(i), 0, 0, 0)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package system
2+
3+
import "github.com/opencontainers/runc/libcontainer/userns"
4+
5+
var RunningInUserNS = userns.RunningInUserNS

libcontainer/userns/userns.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package userns
2+
3+
// RunningInUserNS detects whether we are currently running in a user namespace.
4+
// Originally copied from github.com/lxc/lxd/shared/util.go
5+
var RunningInUserNS = runningInUserNS
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// +build gofuzz
22

3-
package system
3+
package userns
44

55
import (
66
"strings"

0 commit comments

Comments
 (0)