Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions libcontainer/system/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package system

import (
"os"
"os/exec"
"syscall" // only for exec
"unsafe"
Expand Down Expand Up @@ -122,22 +121,6 @@ func UIDMapInUserNS(uidmap []user.IDMap) bool {
return true
}

// GetParentNSeuid returns the euid within the parent user namespace
func GetParentNSeuid() int64 {
euid := int64(os.Geteuid())
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
}

// SetSubreaper sets the value i as the subreaper setting for the calling process
func SetSubreaper(i int) error {
return unix.Prctl(PR_SET_CHILD_SUBREAPER, uintptr(i), 0, 0, 0)
Expand Down
6 changes: 0 additions & 6 deletions libcontainer/system/unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,3 @@ func RunningInUserNS() bool {
func UIDMapInUserNS(uidmap []user.IDMap) bool {
return false
}

// GetParentNSeuid returns the euid within the parent user namespace
// Always returns os.Geteuid on non-linux
func GetParentNSeuid() int {
return os.Geteuid()
}
29 changes: 24 additions & 5 deletions utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,31 @@ func isRootless(context *cli.Context) (bool, error) {
}
// nil b stands for "auto detect"
}
// Even if os.Geteuid() == 0, it might still require rootless mode,
// especially when running within userns.
// So we use system.GetParentNSeuid() here.
if os.Geteuid() != 0 {
return true, nil
}
if !system.RunningInUserNS() {
// euid == 0 in the initial namespace
return false, nil
}
// euid == 0 in userns.
// TODO: In this case, we should not enable the entire "rootless mode", but we should allow ignoring cgroup permission errors.
// See https://github.com/opencontainers/runc/issues/1837
//
// * When runc is executed via Docker-in-LXD, isRootless() returns false, because systemd in LXD does not set $USER.
// If Docker was launched manually in LXD, isRootless() still returns false, because $USER would be set to "root".
// Note that returning false is the expected behavior for Docker-in-LXD, because LXD sets up cgroups.
//
// * When runc is executed via rootless img, buildkit, and a few other tools, isRootless() returns true,
// because they don't change environment variables after unsharing the userns and mapping UID=0 to the current user.
//
// TODO(AkihiroSuda): how to support nested userns?
return system.GetParentNSeuid() != 0 || system.RunningInUserNS(), nil
// Corner case:
// * When runc is executed in Docker-in-rootless-Docker ("rootless dind"), as the root in the contaienr,
// isRootless() returns false, and unlikely to work, unless cgroups is configured.
// The dockerd in the container would need to specify `runc --rootless` explicitly in this case.
// (And user would need to launch dockerd with --rootless explicitly, probably)
u := os.Getenv("USER")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks quite fragile, would be possible to add here the check from the point 2.2 in your proposal or wouldn't it help?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry what is the point 2.2?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second bullet point in step 2:

"Switch the cgroup manager to libcontainer.RootlessCgroupfs"

We should detect cgroup availability explicitly by probably trying mkdir /sys/fs/cgroup/foo/bar or something similar. I guess the overhead is negligible.
Or just remove libcontainer.RootlessCgroupfs manager and ignore all errors.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to just close this PR and open the step2 PR now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine by me, but I think some runc maintainer should ensure it can get reviewed promptly. If that is not possible, this much simpler PR can be an intermediate workaround

return u != "" && u != "root", nil
}

func createContainer(context *cli.Context, id string, spec *specs.Spec) (libcontainer.Container, error) {
Expand Down