diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2019-04-11 08:30:31 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-11 08:30:31 -0700 |
commit | b281c34b317ff6f84757b590905c5ef5981863e0 (patch) | |
tree | 1ca9c59c49e42c3b4d1687e0ced9d6639cc83a7c /libpod | |
parent | 4596c39655f7ff5e741adbc97aaa49bb3a9d453e (diff) | |
parent | 2c9c40dc82141d3876d08fa5421f380b975a387b (diff) | |
download | podman-b281c34b317ff6f84757b590905c5ef5981863e0.tar.gz podman-b281c34b317ff6f84757b590905c5ef5981863e0.tar.bz2 podman-b281c34b317ff6f84757b590905c5ef5981863e0.zip |
Merge pull request #2899 from giuseppe/prevent-sys-fs-kernel-paths-in-userns
userns: prevent /sys/kernel/* paths in the container
Diffstat (limited to 'libpod')
-rw-r--r-- | libpod/oci_linux.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/libpod/oci_linux.go b/libpod/oci_linux.go index 8c0abad80..01f7c3649 100644 --- a/libpod/oci_linux.go +++ b/libpod/oci_linux.go @@ -3,15 +3,20 @@ package libpod import ( + "fmt" "os" "os/exec" "path/filepath" + "runtime" "strings" "syscall" "github.com/containerd/cgroups" + "github.com/containers/libpod/pkg/rootless" "github.com/containers/libpod/utils" + pmount "github.com/containers/storage/pkg/mount" spec "github.com/opencontainers/runtime-spec/specs-go" + "github.com/pkg/errors" "github.com/sirupsen/logrus" "golang.org/x/sys/unix" ) @@ -91,6 +96,54 @@ func (r *OCIRuntime) createContainer(ctr *Container, cgroupParent string, restor return err } } + + // if we are running a non privileged container, be sure to umount some kernel paths so they are not + // bind mounted inside the container at all. + if !ctr.config.Privileged && !rootless.IsRootless() { + ch := make(chan error) + go func() { + runtime.LockOSThread() + err := func() error { + fd, err := os.Open(fmt.Sprintf("/proc/%d/task/%d/ns/mnt", os.Getpid(), unix.Gettid())) + if err != nil { + return err + } + defer fd.Close() + + // create a new mountns on the current thread + if err = unix.Unshare(unix.CLONE_NEWNS); err != nil { + return err + } + defer unix.Setns(int(fd.Fd()), unix.CLONE_NEWNS) + + // don't spread our mounts around. We are setting only /sys to be slave + // so that the cleanup process is still able to umount the storage and the + // changes are propagated to the host. + err = unix.Mount("/sys", "/sys", "none", unix.MS_REC|unix.MS_SLAVE, "") + if err != nil { + return errors.Wrapf(err, "cannot make /sys slave") + } + + mounts, err := pmount.GetMounts() + if err != nil { + return err + } + for _, m := range mounts { + if !strings.HasPrefix(m.Mountpoint, "/sys/kernel") { + continue + } + err = unix.Unmount(m.Mountpoint, 0) + if err != nil { + return errors.Wrapf(err, "cannot unmount %s", m.Mountpoint) + } + } + return r.createOCIContainer(ctr, cgroupParent, restoreOptions) + }() + ch <- err + }() + err := <-ch + return err + } } return r.createOCIContainer(ctr, cgroupParent, restoreOptions) } |