2014-02-20 08:50:10 +08:00
|
|
|
// +build linux
|
|
|
|
|
2015-02-10 05:16:43 +08:00
|
|
|
package libcontainer
|
2014-02-19 08:56:11 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-02-17 18:20:06 +08:00
|
|
|
"io"
|
2015-02-10 05:16:43 +08:00
|
|
|
"io/ioutil"
|
2014-04-29 18:41:44 +08:00
|
|
|
"os"
|
2015-04-02 20:31:47 +08:00
|
|
|
"os/exec"
|
2015-04-23 10:17:30 +08:00
|
|
|
"path"
|
2014-04-29 18:41:44 +08:00
|
|
|
"path/filepath"
|
2015-02-27 05:08:15 +08:00
|
|
|
"strings"
|
2015-02-10 06:42:21 +08:00
|
|
|
"time"
|
2014-04-29 18:41:44 +08:00
|
|
|
|
2019-09-24 04:45:45 +08:00
|
|
|
securejoin "github.com/cyphar/filepath-securejoin"
|
2020-03-17 00:08:11 +08:00
|
|
|
"github.com/moby/sys/mountinfo"
|
2016-05-27 03:36:54 +08:00
|
|
|
"github.com/mrunalp/fileutils"
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2016-01-09 02:19:00 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/system"
|
2016-01-22 07:27:20 +08:00
|
|
|
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
|
2017-03-23 08:21:19 +08:00
|
|
|
"github.com/opencontainers/selinux/go-selinux/label"
|
2017-05-10 05:38:27 +08:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2014-02-19 08:56:11 +08:00
|
|
|
)
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
const defaultMountFlags = unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV
|
2014-02-19 08:56:11 +08:00
|
|
|
|
2016-04-12 16:12:23 +08:00
|
|
|
// needsSetupDev returns true if /dev needs to be set up.
|
2016-04-08 13:01:39 +08:00
|
|
|
func needsSetupDev(config *configs.Config) bool {
|
|
|
|
for _, m := range config.Mounts {
|
2016-04-25 22:15:17 +08:00
|
|
|
if m.Device == "bind" && libcontainerUtils.CleanPath(m.Destination) == "/dev" {
|
2016-04-08 13:01:39 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:29:34 +08:00
|
|
|
// prepareRootfs sets up the devices, mount points, and filesystems for use
|
2017-05-06 07:59:03 +08:00
|
|
|
// inside a new mount namespace. It doesn't set anything as ro. You must call
|
|
|
|
// finalizeRootfs after this function to finish setting up the rootfs.
|
2017-10-04 16:07:58 +08:00
|
|
|
func prepareRootfs(pipe io.ReadWriter, iConfig *initConfig) (err error) {
|
|
|
|
config := iConfig.Config
|
2015-02-01 11:56:27 +08:00
|
|
|
if err := prepareRoot(config); err != nil {
|
2016-04-19 02:37:26 +08:00
|
|
|
return newSystemErrorWithCause(err, "preparing rootfs")
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2015-07-08 05:46:44 +08:00
|
|
|
|
2016-11-14 20:54:17 +08:00
|
|
|
hasCgroupns := config.Namespaces.Contains(configs.NEWCGROUP)
|
2018-08-15 06:50:04 +08:00
|
|
|
setupDev := needsSetupDev(config)
|
2015-03-18 02:03:28 +08:00
|
|
|
for _, m := range config.Mounts {
|
2015-04-02 20:31:47 +08:00
|
|
|
for _, precmd := range m.PremountCmds {
|
|
|
|
if err := mountCmd(precmd); err != nil {
|
2016-04-19 02:37:26 +08:00
|
|
|
return newSystemErrorWithCause(err, "running premount command")
|
2015-04-02 20:31:47 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-14 20:54:17 +08:00
|
|
|
if err := mountToRootfs(m, config.Rootfs, config.MountLabel, hasCgroupns); err != nil {
|
2020-05-03 08:59:46 +08:00
|
|
|
return newSystemErrorWithCausef(err, "mounting %q to rootfs at %q", m.Source, m.Destination)
|
2014-08-27 16:51:52 +08:00
|
|
|
}
|
2015-04-02 20:31:47 +08:00
|
|
|
|
|
|
|
for _, postcmd := range m.PostmountCmds {
|
|
|
|
if err := mountCmd(postcmd); err != nil {
|
2016-04-19 02:37:26 +08:00
|
|
|
return newSystemErrorWithCause(err, "running postmount command")
|
2015-04-02 20:31:47 +08:00
|
|
|
}
|
|
|
|
}
|
2014-04-11 07:03:52 +08:00
|
|
|
}
|
2016-06-03 23:29:34 +08:00
|
|
|
|
2015-10-21 16:06:26 +08:00
|
|
|
if setupDev {
|
2015-07-08 05:46:44 +08:00
|
|
|
if err := createDevices(config); err != nil {
|
2016-04-19 02:37:26 +08:00
|
|
|
return newSystemErrorWithCause(err, "creating device nodes")
|
2015-07-08 05:46:44 +08:00
|
|
|
}
|
2016-06-03 23:29:34 +08:00
|
|
|
if err := setupPtmx(config); err != nil {
|
2016-04-19 02:37:26 +08:00
|
|
|
return newSystemErrorWithCause(err, "setting up ptmx")
|
2015-07-08 05:46:44 +08:00
|
|
|
}
|
|
|
|
if err := setupDevSymlinks(config.Rootfs); err != nil {
|
2016-04-19 02:37:26 +08:00
|
|
|
return newSystemErrorWithCause(err, "setting up /dev symlinks")
|
2015-07-08 05:46:44 +08:00
|
|
|
}
|
2014-05-12 20:41:07 +08:00
|
|
|
}
|
2016-06-03 23:29:34 +08:00
|
|
|
|
2016-02-17 18:20:06 +08:00
|
|
|
// Signal the parent to run the pre-start hooks.
|
|
|
|
// The hooks are run after the mounts are setup, but before we switch to the new
|
|
|
|
// root, so that the old root is still available in the hooks for any mount
|
|
|
|
// manipulations.
|
2017-10-04 16:07:58 +08:00
|
|
|
// Note that iConfig.Cwd is not guaranteed to exist here.
|
2016-02-17 18:20:06 +08:00
|
|
|
if err := syncParentHooks(pipe); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-03 23:29:34 +08:00
|
|
|
|
|
|
|
// The reason these operations are done here rather than in finalizeRootfs
|
|
|
|
// is because the console-handling code gets quite sticky if we have to set
|
|
|
|
// up the console before doing the pivot_root(2). This is because the
|
|
|
|
// Console API has to also work with the ExecIn case, which means that the
|
|
|
|
// API must be able to deal with being inside as well as outside the
|
|
|
|
// container. It's just cleaner to do this here (at the expense of the
|
|
|
|
// operation not being perfectly split).
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Chdir(config.Rootfs); err != nil {
|
2016-04-19 02:37:26 +08:00
|
|
|
return newSystemErrorWithCausef(err, "changing dir to %q", config.Rootfs)
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2016-06-03 23:29:34 +08:00
|
|
|
|
2020-02-08 06:17:51 +08:00
|
|
|
s := iConfig.SpecState
|
|
|
|
s.Pid = unix.Getpid()
|
|
|
|
s.Status = configs.Creating
|
|
|
|
if err := iConfig.Config.Hooks[configs.CreateContainer].RunHooks(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
if config.NoPivotRoot {
|
2015-02-04 09:44:58 +08:00
|
|
|
err = msMoveRoot(config.Rootfs)
|
2018-01-26 00:36:37 +08:00
|
|
|
} else if config.Namespaces.Contains(configs.NEWNS) {
|
2016-10-19 05:52:10 +08:00
|
|
|
err = pivotRoot(config.Rootfs)
|
2018-01-26 00:36:37 +08:00
|
|
|
} else {
|
2020-07-15 16:22:07 +08:00
|
|
|
err = chroot()
|
2014-04-11 23:06:56 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2016-04-19 02:37:26 +08:00
|
|
|
return newSystemErrorWithCause(err, "jailing process inside rootfs")
|
2014-03-07 11:30:52 +08:00
|
|
|
}
|
2016-06-03 23:29:34 +08:00
|
|
|
|
2015-10-21 16:06:26 +08:00
|
|
|
if setupDev {
|
2015-09-21 17:50:29 +08:00
|
|
|
if err := reOpenDevNull(); err != nil {
|
2016-04-19 02:37:26 +08:00
|
|
|
return newSystemErrorWithCause(err, "reopening /dev/null inside container")
|
2015-07-08 05:46:44 +08:00
|
|
|
}
|
2015-05-01 02:03:07 +08:00
|
|
|
}
|
2016-06-03 23:29:34 +08:00
|
|
|
|
2017-10-04 16:07:58 +08:00
|
|
|
if cwd := iConfig.Cwd; cwd != "" {
|
|
|
|
// Note that spec.Process.Cwd can contain unclean value like "../../../../foo/bar...".
|
|
|
|
// However, we are safe to call MkDirAll directly because we are in the jail here.
|
|
|
|
if err := os.MkdirAll(cwd, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:29:34 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-06 07:59:03 +08:00
|
|
|
// finalizeRootfs sets anything to ro if necessary. You must call
|
|
|
|
// prepareRootfs first.
|
2016-06-03 23:29:34 +08:00
|
|
|
func finalizeRootfs(config *configs.Config) (err error) {
|
2016-10-12 07:22:48 +08:00
|
|
|
// remount dev as ro if specified
|
2016-02-24 05:56:01 +08:00
|
|
|
for _, m := range config.Mounts {
|
2016-04-25 22:15:17 +08:00
|
|
|
if libcontainerUtils.CleanPath(m.Destination) == "/dev" {
|
2017-05-10 05:38:27 +08:00
|
|
|
if m.Flags&unix.MS_RDONLY == unix.MS_RDONLY {
|
Split the code for remounting mount points and mounting paths.
A remount of a mount point must include all the current flags or
these will be cleared:
```
The mountflags and data arguments should match the values used in the
original mount() call, except for those parameters that are being
deliberately changed.
```
The current code does not do this; the bug manifests in the specified
flags for `/dev` being lost on remount read only at present. As we
need to specify flags, split the code path for this from remounting
paths which are not mount points, as these can only inherit the
existing flags of the path, and these cannot be changed.
In the bind case, remove extra flags from the bind remount. A bind
mount can only be remounted read only, no other flags can be set,
all other flags are inherited from the parent. From the man page:
```
Since Linux 2.6.26, this flag can also be used to make an existing
bind mount read-only by specifying mountflags as:
MS_REMOUNT | MS_BIND | MS_RDONLY
Note that only the MS_RDONLY setting of the bind mount can be changed
in this manner.
```
MS_REC can only be set on the original bind, so move this. See note
in man page on bind mounts:
```
The remaining bits in the mountflags argument are also ignored, with
the exception of MS_REC.
```
Signed-off-by: Justin Cormack <justin.cormack@docker.com>
2016-12-11 10:25:02 +08:00
|
|
|
if err := remountReadonly(m); err != nil {
|
2016-04-19 02:37:26 +08:00
|
|
|
return newSystemErrorWithCausef(err, "remounting %q as readonly", m.Destination)
|
2016-02-24 05:56:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-06-03 23:29:34 +08:00
|
|
|
|
2016-02-24 05:56:01 +08:00
|
|
|
// set rootfs ( / ) as readonly
|
2015-02-04 09:44:58 +08:00
|
|
|
if config.Readonlyfs {
|
2015-02-01 11:56:27 +08:00
|
|
|
if err := setReadonly(); err != nil {
|
2016-04-19 02:37:26 +08:00
|
|
|
return newSystemErrorWithCause(err, "setting rootfs as readonly")
|
2014-03-21 22:17:17 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-03 23:29:34 +08:00
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
unix.Umask(0022)
|
2014-03-07 11:30:52 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-21 04:12:09 +08:00
|
|
|
// /tmp has to be mounted as private to allow MS_MOVE to work in all situations
|
|
|
|
func prepareTmp(topTmpDir string) (string, error) {
|
|
|
|
tmpdir, err := ioutil.TempDir(topTmpDir, "runctop")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := unix.Mount(tmpdir, tmpdir, "bind", unix.MS_BIND, ""); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := unix.Mount("", tmpdir, "", uintptr(unix.MS_PRIVATE), ""); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return tmpdir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupTmp(tmpdir string) error {
|
|
|
|
unix.Unmount(tmpdir, 0)
|
|
|
|
return os.RemoveAll(tmpdir)
|
|
|
|
}
|
|
|
|
|
2015-04-02 20:31:47 +08:00
|
|
|
func mountCmd(cmd configs.Command) error {
|
|
|
|
command := exec.Command(cmd.Path, cmd.Args[:]...)
|
|
|
|
command.Env = cmd.Env
|
|
|
|
command.Dir = cmd.Dir
|
|
|
|
if out, err := command.CombinedOutput(); err != nil {
|
2015-04-17 04:55:12 +08:00
|
|
|
return fmt.Errorf("%#v failed: %s: %v", cmd, string(out), err)
|
2015-04-02 20:31:47 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-31 00:02:35 +08:00
|
|
|
func prepareBindMount(m *configs.Mount, rootfs string) error {
|
|
|
|
stat, err := os.Stat(m.Source)
|
|
|
|
if err != nil {
|
|
|
|
// error out if the source of a bind mount does not exist as we will be
|
|
|
|
// unable to bind anything to it.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// ensure that the destination of the bind mount is resolved of symlinks at mount time because
|
|
|
|
// any previous mounts can invalidate the next mount's destination.
|
|
|
|
// this can happen when a user specifies mounts within other mounts to cause breakouts or other
|
|
|
|
// evil stuff to try to escape the container's rootfs.
|
|
|
|
var dest string
|
|
|
|
if dest, err = securejoin.SecureJoin(rootfs, m.Destination); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-24 04:45:45 +08:00
|
|
|
if err := checkProcMount(rootfs, dest, m.Source); err != nil {
|
2019-01-31 00:02:35 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// update the mount with the correct dest after symlinks are resolved.
|
|
|
|
m.Destination = dest
|
|
|
|
if err := createIfNotExists(dest, stat.IsDir()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 22:38:33 +08:00
|
|
|
func mountCgroupV1(m *configs.Mount, rootfs, mountLabel string, enableCgroupns bool) error {
|
|
|
|
binds, err := getCgroupMounts(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var merged []string
|
|
|
|
for _, b := range binds {
|
|
|
|
ss := filepath.Base(b.Destination)
|
|
|
|
if strings.Contains(ss, ",") {
|
|
|
|
merged = append(merged, ss)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmpfs := &configs.Mount{
|
|
|
|
Source: "tmpfs",
|
|
|
|
Device: "tmpfs",
|
|
|
|
Destination: m.Destination,
|
|
|
|
Flags: defaultMountFlags,
|
|
|
|
Data: "mode=755",
|
|
|
|
PropagationFlags: m.PropagationFlags,
|
|
|
|
}
|
|
|
|
if err := mountToRootfs(tmpfs, rootfs, mountLabel, enableCgroupns); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, b := range binds {
|
|
|
|
if enableCgroupns {
|
|
|
|
subsystemPath := filepath.Join(rootfs, b.Destination)
|
|
|
|
if err := os.MkdirAll(subsystemPath, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
flags := defaultMountFlags
|
|
|
|
if m.Flags&unix.MS_RDONLY != 0 {
|
|
|
|
flags = flags | unix.MS_RDONLY
|
|
|
|
}
|
|
|
|
cgroupmount := &configs.Mount{
|
|
|
|
Source: "cgroup",
|
2020-04-21 09:06:59 +08:00
|
|
|
Device: "cgroup", // this is actually fstype
|
2019-09-06 22:38:33 +08:00
|
|
|
Destination: subsystemPath,
|
|
|
|
Flags: flags,
|
|
|
|
Data: filepath.Base(subsystemPath),
|
|
|
|
}
|
|
|
|
if err := mountNewCgroup(cgroupmount); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := mountToRootfs(b, rootfs, mountLabel, enableCgroupns); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, mc := range merged {
|
|
|
|
for _, ss := range strings.Split(mc, ",") {
|
|
|
|
// symlink(2) is very dumb, it will just shove the path into
|
|
|
|
// the link and doesn't do any checks or relative path
|
|
|
|
// conversion. Also, don't error out if the cgroup already exists.
|
|
|
|
if err := os.Symlink(mc, filepath.Join(rootfs, m.Destination, ss)); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mountCgroupV2(m *configs.Mount, rootfs, mountLabel string, enableCgroupns bool) error {
|
|
|
|
cgroupPath, err := securejoin.SecureJoin(rootfs, m.Destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(cgroupPath, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-27 22:09:41 +08:00
|
|
|
if err := unix.Mount(m.Source, cgroupPath, "cgroup2", uintptr(m.Flags), m.Data); err != nil {
|
|
|
|
// when we are in UserNS but CgroupNS is not unshared, we cannot mount cgroup2 (#2158)
|
|
|
|
if err == unix.EPERM || err == unix.EBUSY {
|
|
|
|
return unix.Mount("/sys/fs/cgroup", cgroupPath, "", uintptr(m.Flags)|unix.MS_BIND, "")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-09-06 22:38:33 +08:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:54:17 +08:00
|
|
|
func mountToRootfs(m *configs.Mount, rootfs, mountLabel string, enableCgroupns bool) error {
|
2015-02-13 08:23:05 +08:00
|
|
|
var (
|
2015-02-27 05:08:15 +08:00
|
|
|
dest = m.Destination
|
2015-02-13 08:23:05 +08:00
|
|
|
)
|
2015-02-27 05:08:15 +08:00
|
|
|
if !strings.HasPrefix(dest, rootfs) {
|
|
|
|
dest = filepath.Join(rootfs, dest)
|
|
|
|
}
|
|
|
|
|
2015-02-13 08:23:05 +08:00
|
|
|
switch m.Device {
|
2015-04-07 11:12:59 +08:00
|
|
|
case "proc", "sysfs":
|
2019-12-21 20:40:17 +08:00
|
|
|
// If the destination already exists and is not a directory, we bail
|
|
|
|
// out This is to avoid mounting through a symlink or similar -- which
|
|
|
|
// has been a "fun" attack scenario in the past.
|
|
|
|
// TODO: This won't be necessary once we switch to libpathrs and we can
|
|
|
|
// stop all of these symlink-exchange attacks.
|
|
|
|
if fi, err := os.Lstat(dest); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if fi.Mode()&os.ModeDir == 0 {
|
|
|
|
return fmt.Errorf("filesystem %q must be mounted on ordinary directory", m.Device)
|
|
|
|
}
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is
returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-30 09:01:41 +08:00
|
|
|
if err := os.MkdirAll(dest, 0755); err != nil {
|
2015-02-13 08:23:05 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-09-27 18:52:24 +08:00
|
|
|
// Selinux kernels do not support labeling of /proc or /sys
|
|
|
|
return mountPropagate(m, rootfs, "")
|
2015-04-07 11:12:59 +08:00
|
|
|
case "mqueue":
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is
returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-30 09:01:41 +08:00
|
|
|
if err := os.MkdirAll(dest, 0755); err != nil {
|
2015-04-07 11:12:59 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-09-25 00:43:12 +08:00
|
|
|
if err := mountPropagate(m, rootfs, mountLabel); err != nil {
|
2015-09-28 23:02:37 +08:00
|
|
|
// older kernels do not support labeling of /dev/mqueue
|
|
|
|
if err := mountPropagate(m, rootfs, ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-25 04:14:57 +08:00
|
|
|
return label.SetFileLabel(dest, mountLabel)
|
2015-04-07 11:12:59 +08:00
|
|
|
}
|
2016-03-25 04:14:57 +08:00
|
|
|
return nil
|
2015-03-11 23:12:55 +08:00
|
|
|
case "tmpfs":
|
2016-05-27 03:36:54 +08:00
|
|
|
copyUp := m.Extensions&configs.EXT_COPYUP == configs.EXT_COPYUP
|
|
|
|
tmpDir := ""
|
2015-03-11 23:12:55 +08:00
|
|
|
stat, err := os.Stat(dest)
|
|
|
|
if err != nil {
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is
returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-30 09:01:41 +08:00
|
|
|
if err := os.MkdirAll(dest, 0755); err != nil {
|
2015-03-11 23:12:55 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 03:36:54 +08:00
|
|
|
if copyUp {
|
2018-08-21 04:12:09 +08:00
|
|
|
tmpdir, err := prepareTmp("/tmp")
|
|
|
|
if err != nil {
|
|
|
|
return newSystemErrorWithCause(err, "tmpcopyup: failed to setup tmpdir")
|
|
|
|
}
|
|
|
|
defer cleanupTmp(tmpdir)
|
|
|
|
tmpDir, err = ioutil.TempDir(tmpdir, "runctmpdir")
|
2016-05-27 03:36:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return newSystemErrorWithCause(err, "tmpcopyup: failed to create tmpdir")
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
m.Destination = tmpDir
|
|
|
|
}
|
2015-09-25 00:43:12 +08:00
|
|
|
if err := mountPropagate(m, rootfs, mountLabel); err != nil {
|
2015-03-11 23:12:55 +08:00
|
|
|
return err
|
|
|
|
}
|
2016-05-27 03:36:54 +08:00
|
|
|
if copyUp {
|
|
|
|
if err := fileutils.CopyDirectory(dest, tmpDir); err != nil {
|
|
|
|
errMsg := fmt.Errorf("tmpcopyup: failed to copy %s to %s: %v", dest, tmpDir, err)
|
2017-05-10 05:38:27 +08:00
|
|
|
if err1 := unix.Unmount(tmpDir, unix.MNT_DETACH); err1 != nil {
|
2016-05-27 03:36:54 +08:00
|
|
|
return newSystemErrorWithCausef(err1, "tmpcopyup: %v: failed to unmount", errMsg)
|
|
|
|
}
|
|
|
|
return errMsg
|
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Mount(tmpDir, dest, "", unix.MS_MOVE, ""); err != nil {
|
2016-05-27 03:36:54 +08:00
|
|
|
errMsg := fmt.Errorf("tmpcopyup: failed to move mount %s to %s: %v", tmpDir, dest, err)
|
2017-05-10 05:38:27 +08:00
|
|
|
if err1 := unix.Unmount(tmpDir, unix.MNT_DETACH); err1 != nil {
|
2016-05-27 03:36:54 +08:00
|
|
|
return newSystemErrorWithCausef(err1, "tmpcopyup: %v: failed to unmount", errMsg)
|
|
|
|
}
|
|
|
|
return errMsg
|
|
|
|
}
|
|
|
|
}
|
2015-03-11 23:12:55 +08:00
|
|
|
if stat != nil {
|
|
|
|
if err = os.Chmod(dest, stat.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2015-02-13 08:23:05 +08:00
|
|
|
case "bind":
|
2019-01-31 00:02:35 +08:00
|
|
|
if err := prepareBindMount(m, rootfs); err != nil {
|
2015-02-10 06:22:52 +08:00
|
|
|
return err
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2015-09-25 00:43:12 +08:00
|
|
|
if err := mountPropagate(m, rootfs, mountLabel); err != nil {
|
2015-02-10 06:22:52 +08:00
|
|
|
return err
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2015-08-31 11:10:34 +08:00
|
|
|
// bind mount won't change mount options, we need remount to make mount options effective.
|
2015-09-29 22:53:21 +08:00
|
|
|
// first check that we have non-default options required before attempting a remount
|
2017-05-10 05:38:27 +08:00
|
|
|
if m.Flags&^(unix.MS_REC|unix.MS_REMOUNT|unix.MS_BIND) != 0 {
|
2015-09-29 22:53:21 +08:00
|
|
|
// only remount if unique mount options are set
|
|
|
|
if err := remount(m, rootfs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-13 08:23:05 +08:00
|
|
|
}
|
2015-09-29 22:53:21 +08:00
|
|
|
|
2015-02-13 08:23:05 +08:00
|
|
|
if m.Relabel != "" {
|
2015-07-31 04:36:35 +08:00
|
|
|
if err := label.Validate(m.Relabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
shared := label.IsShared(m.Relabel)
|
|
|
|
if err := label.Relabel(m.Source, mountLabel, shared); err != nil {
|
2015-02-13 08:23:05 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-04-18 08:41:11 +08:00
|
|
|
case "cgroup":
|
2019-09-06 22:38:33 +08:00
|
|
|
if cgroups.IsCgroup2UnifiedMode() {
|
libct/mountToRootfs: rm useless code
To make a bind mount read-only, it needs to be remounted. This is what
the code removed does, but it is not needed here.
We have to deal with three cases here:
1. cgroup v2 unified mode. In this case the mount is real mount with
fstype=cgroup2, and there is no need to have a bind mount on top,
as we pass readonly flag to the mount as is.
2. cgroup v1 + cgroupns (enableCgroupns == true). In this case the
"mount" is in fact a set of real mounts with fstype=cgroup, and
they are all performed in mountCgroupV1, with readonly flag
added if needed.
3. cgroup v1 as is (enableCgroupns == false). In this case
mountCgroupV1() calls mountToRootfs() again with an argument
from the list obtained from getCgroupMounts(), i.e. a bind
mount with the same flags as the original mount has (plus
unix.MS_BIND | unix.MS_REC), and mountToRootfs() does remounting
(under the case "bind":).
So, the code which this patch is removing is not needed -- it
essentially does nothing in case 3 above (since the bind mount
is already remounted readonly), and in cases 1 and 2 it
creates an unneeded extra bind mount on top of a real one (or set of
real ones).
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-04-24 07:04:18 +08:00
|
|
|
return mountCgroupV2(m, rootfs, mountLabel, enableCgroupns)
|
2015-07-21 23:56:37 +08:00
|
|
|
}
|
libct/mountToRootfs: rm useless code
To make a bind mount read-only, it needs to be remounted. This is what
the code removed does, but it is not needed here.
We have to deal with three cases here:
1. cgroup v2 unified mode. In this case the mount is real mount with
fstype=cgroup2, and there is no need to have a bind mount on top,
as we pass readonly flag to the mount as is.
2. cgroup v1 + cgroupns (enableCgroupns == true). In this case the
"mount" is in fact a set of real mounts with fstype=cgroup, and
they are all performed in mountCgroupV1, with readonly flag
added if needed.
3. cgroup v1 as is (enableCgroupns == false). In this case
mountCgroupV1() calls mountToRootfs() again with an argument
from the list obtained from getCgroupMounts(), i.e. a bind
mount with the same flags as the original mount has (plus
unix.MS_BIND | unix.MS_REC), and mountToRootfs() does remounting
(under the case "bind":).
So, the code which this patch is removing is not needed -- it
essentially does nothing in case 3 above (since the bind mount
is already remounted readonly), and in cases 1 and 2 it
creates an unneeded extra bind mount on top of a real one (or set of
real ones).
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-04-24 07:04:18 +08:00
|
|
|
return mountCgroupV1(m, rootfs, mountLabel, enableCgroupns)
|
2015-02-13 08:23:05 +08:00
|
|
|
default:
|
2017-02-07 00:53:23 +08:00
|
|
|
// ensure that the destination of the mount is resolved of symlinks at mount time because
|
|
|
|
// any previous mounts can invalidate the next mount's destination.
|
|
|
|
// this can happen when a user specifies mounts within other mounts to cause breakouts or other
|
|
|
|
// evil stuff to try to escape the container's rootfs.
|
|
|
|
var err error
|
2017-10-27 21:30:30 +08:00
|
|
|
if dest, err = securejoin.SecureJoin(rootfs, m.Destination); err != nil {
|
2017-02-07 00:53:23 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-09-24 04:45:45 +08:00
|
|
|
if err := checkProcMount(rootfs, dest, m.Source); err != nil {
|
2017-02-07 00:53:23 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// update the mount with the correct dest after symlinks are resolved.
|
|
|
|
m.Destination = dest
|
2016-02-27 07:21:33 +08:00
|
|
|
if err := os.MkdirAll(dest, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return mountPropagate(m, rootfs, mountLabel)
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 02:25:22 +08:00
|
|
|
func getCgroupMounts(m *configs.Mount) ([]*configs.Mount, error) {
|
2016-09-16 01:24:39 +08:00
|
|
|
mounts, err := cgroups.GetCgroupMounts(false)
|
2015-07-21 02:25:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-10 16:58:08 +08:00
|
|
|
cgroupPaths, err := cgroups.ParseCgroupFile("/proc/self/cgroup")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-21 02:25:22 +08:00
|
|
|
var binds []*configs.Mount
|
|
|
|
|
|
|
|
for _, mm := range mounts {
|
2016-04-26 00:19:39 +08:00
|
|
|
dir, err := mm.GetOwnCgroup(cgroupPaths)
|
2015-07-21 02:25:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
relDir, err := filepath.Rel(mm.Root, dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
binds = append(binds, &configs.Mount{
|
libcontainer: Allow passing mount propagation flags
Right now if one passes a mount propagation flag in spec file, it
does not take effect. For example, try following in spec json file.
{
"type": "bind",
"source": "/root/mnt-source",
"destination": "/root/mnt-dest",
"options": "rbind,shared"
}
One would expect that /root/mnt-dest will be shared inside the container
but that's not the case.
#findmnt -o TARGET,PROPAGATION
`-/root/mnt-dest private
Reason being that propagation flags can't be passed in along with other
regular flags. They need to be passed in a separate call to mount syscall.
That too, one propagation flag at a time. (from mount man page).
Hence, store propagation flags separately in a slice and apply these
in that order after the mount call wherever appropriate. This allows
user to control the propagation property of mount point inside
the container.
Storing them separately also solves another problem where recursive flag
(syscall.MS_REC) can get mixed up. For example, options "rbind,private"
and "bind,rprivate" will be same and there will be no way to differentiate
between these if all the flags are stored in a single integer.
This patch would allow one to pass propagation flags "[r]shared,[r]slave,
[r]private,[r]unbindable" in spec file as per mount property.
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
2015-09-17 03:53:23 +08:00
|
|
|
Device: "bind",
|
|
|
|
Source: filepath.Join(mm.Mountpoint, relDir),
|
Do not create cgroup dir name from combining subsystems
On some systems, when we mount some cgroup subsystems into
a same mountpoint, the name sequence of mount options and
cgroup directory name can not be the same.
For example, the mount option is cpuacct,cpu, but
mountpoint name is /sys/fs/cgroup/cpu,cpuacct. In current
runc, we set mount destination name from combining
subsystems, which comes from mount option from
/proc/self/mountinfo, so in my case the name would be
/sys/fs/cgroup/cpuacct,cpu, which is differernt from
host, and will break some applications.
Fix it by using directory name from host mountpoint.
Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
2017-01-11 15:27:58 +08:00
|
|
|
Destination: filepath.Join(m.Destination, filepath.Base(mm.Mountpoint)),
|
2017-05-10 05:38:27 +08:00
|
|
|
Flags: unix.MS_BIND | unix.MS_REC | m.Flags,
|
libcontainer: Allow passing mount propagation flags
Right now if one passes a mount propagation flag in spec file, it
does not take effect. For example, try following in spec json file.
{
"type": "bind",
"source": "/root/mnt-source",
"destination": "/root/mnt-dest",
"options": "rbind,shared"
}
One would expect that /root/mnt-dest will be shared inside the container
but that's not the case.
#findmnt -o TARGET,PROPAGATION
`-/root/mnt-dest private
Reason being that propagation flags can't be passed in along with other
regular flags. They need to be passed in a separate call to mount syscall.
That too, one propagation flag at a time. (from mount man page).
Hence, store propagation flags separately in a slice and apply these
in that order after the mount call wherever appropriate. This allows
user to control the propagation property of mount point inside
the container.
Storing them separately also solves another problem where recursive flag
(syscall.MS_REC) can get mixed up. For example, options "rbind,private"
and "bind,rprivate" will be same and there will be no way to differentiate
between these if all the flags are stored in a single integer.
This patch would allow one to pass propagation flags "[r]shared,[r]slave,
[r]private,[r]unbindable" in spec file as per mount property.
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
2015-09-17 03:53:23 +08:00
|
|
|
PropagationFlags: m.PropagationFlags,
|
2015-07-21 02:25:22 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return binds, nil
|
|
|
|
}
|
|
|
|
|
2019-09-24 04:45:45 +08:00
|
|
|
// checkProcMount checks to ensure that the mount destination is not over the top of /proc.
|
2015-04-22 04:46:27 +08:00
|
|
|
// dest is required to be an abs path and have any symlinks resolved before calling this function.
|
2019-09-24 04:45:45 +08:00
|
|
|
//
|
|
|
|
// if source is nil, don't stat the filesystem. This is used for restore of a checkpoint.
|
|
|
|
func checkProcMount(rootfs, dest, source string) error {
|
|
|
|
const procPath = "/proc"
|
2016-01-06 14:48:40 +08:00
|
|
|
// White list, it should be sub directories of invalid destinations
|
|
|
|
validDestinations := []string{
|
|
|
|
// These entries can be bind mounted by files emulated by fuse,
|
|
|
|
// so commands like top, free displays stats in container.
|
|
|
|
"/proc/cpuinfo",
|
|
|
|
"/proc/diskstats",
|
|
|
|
"/proc/meminfo",
|
2016-02-16 15:59:27 +08:00
|
|
|
"/proc/stat",
|
2016-08-15 07:32:39 +08:00
|
|
|
"/proc/swaps",
|
|
|
|
"/proc/uptime",
|
2018-08-30 21:23:35 +08:00
|
|
|
"/proc/loadavg",
|
2016-02-16 15:59:27 +08:00
|
|
|
"/proc/net/dev",
|
2016-01-06 14:48:40 +08:00
|
|
|
}
|
|
|
|
for _, valid := range validDestinations {
|
|
|
|
path, err := filepath.Rel(filepath.Join(rootfs, valid), dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if path == "." {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 04:45:45 +08:00
|
|
|
path, err := filepath.Rel(filepath.Join(rootfs, procPath), dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// pass if the mount path is located outside of /proc
|
|
|
|
if strings.HasPrefix(path, "..") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if path == "." {
|
|
|
|
// an empty source is pasted on restore
|
|
|
|
if source == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// only allow a mount on-top of proc if it's source is "proc"
|
|
|
|
isproc, err := isProc(source)
|
2015-05-01 03:39:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-24 04:45:45 +08:00
|
|
|
// pass if the mount is happening on top of /proc and the source of
|
|
|
|
// the mount is a proc filesystem
|
|
|
|
if isproc {
|
|
|
|
return nil
|
2015-04-22 04:46:27 +08:00
|
|
|
}
|
2019-09-24 04:45:45 +08:00
|
|
|
return fmt.Errorf("%q cannot be mounted because it is not of type proc", dest)
|
2015-04-22 04:46:27 +08:00
|
|
|
}
|
2019-09-24 04:45:45 +08:00
|
|
|
return fmt.Errorf("%q cannot be mounted because it is inside /proc", dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isProc(path string) (bool, error) {
|
|
|
|
var s unix.Statfs_t
|
|
|
|
if err := unix.Statfs(path, &s); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return s.Type == unix.PROC_SUPER_MAGIC, nil
|
2015-04-22 04:46:27 +08:00
|
|
|
}
|
|
|
|
|
2014-05-12 20:41:07 +08:00
|
|
|
func setupDevSymlinks(rootfs string) error {
|
|
|
|
var links = [][2]string{
|
|
|
|
{"/proc/self/fd", "/dev/fd"},
|
|
|
|
{"/proc/self/fd/0", "/dev/stdin"},
|
|
|
|
{"/proc/self/fd/1", "/dev/stdout"},
|
|
|
|
{"/proc/self/fd/2", "/dev/stderr"},
|
|
|
|
}
|
|
|
|
// kcore support can be toggled with CONFIG_PROC_KCORE; only create a symlink
|
|
|
|
// in /dev if it exists in /proc.
|
|
|
|
if _, err := os.Stat("/proc/kcore"); err == nil {
|
2016-02-15 20:22:19 +08:00
|
|
|
links = append(links, [2]string{"/proc/kcore", "/dev/core"})
|
2014-05-12 20:41:07 +08:00
|
|
|
}
|
|
|
|
for _, link := range links {
|
|
|
|
var (
|
|
|
|
src = link[0]
|
|
|
|
dst = filepath.Join(rootfs, link[1])
|
|
|
|
)
|
|
|
|
if err := os.Symlink(src, dst); err != nil && !os.IsExist(err) {
|
|
|
|
return fmt.Errorf("symlink %s %s %s", src, dst, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:03:07 +08:00
|
|
|
// If stdin, stdout, and/or stderr are pointing to `/dev/null` in the parent's rootfs
|
|
|
|
// this method will make them point to `/dev/null` in this container's rootfs. This
|
|
|
|
// needs to be called after we chroot/pivot into the container's rootfs so that any
|
|
|
|
// symlinks are resolved locally.
|
2015-09-21 17:50:29 +08:00
|
|
|
func reOpenDevNull() error {
|
2017-05-10 05:38:27 +08:00
|
|
|
var stat, devNullStat unix.Stat_t
|
2015-09-30 16:05:49 +08:00
|
|
|
file, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
|
2014-07-17 02:33:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to open /dev/null - %s", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Fstat(int(file.Fd()), &devNullStat); err != nil {
|
2015-02-13 08:23:05 +08:00
|
|
|
return err
|
2014-07-17 02:33:43 +08:00
|
|
|
}
|
|
|
|
for fd := 0; fd < 3; fd++ {
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Fstat(fd, &stat); err != nil {
|
2015-02-13 08:23:05 +08:00
|
|
|
return err
|
2014-07-17 02:33:43 +08:00
|
|
|
}
|
|
|
|
if stat.Rdev == devNullStat.Rdev {
|
|
|
|
// Close and re-open the fd.
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Dup3(int(file.Fd()), fd, 0); err != nil {
|
2015-02-13 08:23:05 +08:00
|
|
|
return err
|
2014-07-17 02:33:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-01 11:56:27 +08:00
|
|
|
|
|
|
|
// Create the device nodes in the container.
|
2015-02-04 09:44:58 +08:00
|
|
|
func createDevices(config *configs.Config) error {
|
2016-01-09 02:19:00 +08:00
|
|
|
useBindMount := system.RunningInUserNS() || config.Namespaces.Contains(configs.NEWUSER)
|
2017-05-10 05:38:27 +08:00
|
|
|
oldMask := unix.Umask(0000)
|
2015-02-04 09:44:58 +08:00
|
|
|
for _, node := range config.Devices {
|
2015-03-25 08:19:12 +08:00
|
|
|
// containers running in a user namespace are not allowed to mknod
|
|
|
|
// devices so we can just bind mount it from the host.
|
2016-01-09 02:19:00 +08:00
|
|
|
if err := createDeviceNode(config.Rootfs, node, useBindMount); err != nil {
|
2017-05-10 05:38:27 +08:00
|
|
|
unix.Umask(oldMask)
|
2015-02-01 11:56:27 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
unix.Umask(oldMask)
|
2015-02-01 11:56:27 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:30:41 +08:00
|
|
|
func bindMountDeviceNode(dest string, node *configs.Device) error {
|
|
|
|
f, err := os.Create(dest)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if f != nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
return unix.Mount(node.Path, dest, "bind", unix.MS_BIND, "")
|
2015-10-12 06:30:41 +08:00
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
// Creates the device node in the rootfs of the container.
|
2015-03-25 08:19:12 +08:00
|
|
|
func createDeviceNode(rootfs string, node *configs.Device, bind bool) error {
|
2020-05-04 20:39:37 +08:00
|
|
|
if node.Path == "" {
|
|
|
|
// The node only exists for cgroup reasons, ignore it here.
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-18 13:50:43 +08:00
|
|
|
dest := filepath.Join(rootfs, node.Path)
|
|
|
|
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
|
2015-02-01 11:56:27 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-03-25 08:19:12 +08:00
|
|
|
if bind {
|
2015-10-12 06:30:41 +08:00
|
|
|
return bindMountDeviceNode(dest, node)
|
2015-02-18 13:37:02 +08:00
|
|
|
}
|
2015-03-25 08:19:12 +08:00
|
|
|
if err := mknodDevice(dest, node); err != nil {
|
|
|
|
if os.IsExist(err) {
|
|
|
|
return nil
|
2015-10-12 06:30:41 +08:00
|
|
|
} else if os.IsPermission(err) {
|
|
|
|
return bindMountDeviceNode(dest, node)
|
2015-03-25 08:19:12 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2015-02-18 13:37:02 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mknodDevice(dest string, node *configs.Device) error {
|
2015-02-01 11:56:27 +08:00
|
|
|
fileMode := node.FileMode
|
|
|
|
switch node.Type {
|
2020-05-07 11:59:36 +08:00
|
|
|
case configs.BlockDevice:
|
2017-05-10 05:38:27 +08:00
|
|
|
fileMode |= unix.S_IFBLK
|
2020-05-07 11:59:36 +08:00
|
|
|
case configs.CharDevice:
|
|
|
|
fileMode |= unix.S_IFCHR
|
|
|
|
case configs.FifoDevice:
|
2017-05-10 05:38:27 +08:00
|
|
|
fileMode |= unix.S_IFIFO
|
2015-02-01 11:56:27 +08:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("%c is not a valid device type for device %s", node.Type, node.Path)
|
|
|
|
}
|
2020-05-07 11:59:36 +08:00
|
|
|
dev, err := node.Mkdev()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := unix.Mknod(dest, uint32(fileMode), int(dev)); err != nil {
|
2015-02-18 13:37:02 +08:00
|
|
|
return err
|
2015-02-01 11:56:27 +08:00
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
return unix.Chown(dest, int(node.Uid), int(node.Gid))
|
2015-02-01 11:56:27 +08:00
|
|
|
}
|
|
|
|
|
2015-10-02 05:03:02 +08:00
|
|
|
// Get the parent mount point of directory passed in as argument. Also return
|
|
|
|
// optional fields.
|
|
|
|
func getParentMount(rootfs string) (string, string, error) {
|
2020-03-13 08:32:25 +08:00
|
|
|
mi, err := mountinfo.GetMounts(mountinfo.ParentsFilter(rootfs))
|
2015-10-02 05:03:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2020-03-13 08:32:25 +08:00
|
|
|
if len(mi) < 1 {
|
|
|
|
return "", "", fmt.Errorf("could not find parent mount of %s", rootfs)
|
2015-10-02 05:03:02 +08:00
|
|
|
}
|
|
|
|
|
2020-03-13 08:32:25 +08:00
|
|
|
// find the longest mount point
|
|
|
|
var idx, maxlen int
|
|
|
|
for i := range mi {
|
|
|
|
if len(mi[i].Mountpoint) > maxlen {
|
|
|
|
maxlen = len(mi[i].Mountpoint)
|
|
|
|
idx = i
|
2015-10-02 05:03:02 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-13 08:32:25 +08:00
|
|
|
return mi[idx].Mountpoint, mi[idx].Optional, nil
|
2015-10-02 05:03:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make parent mount private if it was shared
|
2016-02-29 16:22:45 +08:00
|
|
|
func rootfsParentMountPrivate(rootfs string) error {
|
2015-10-02 05:03:02 +08:00
|
|
|
sharedMount := false
|
|
|
|
|
2016-02-29 16:22:45 +08:00
|
|
|
parentMount, optionalOpts, err := getParentMount(rootfs)
|
2015-10-02 05:03:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
optsSplit := strings.Split(optionalOpts, " ")
|
|
|
|
for _, opt := range optsSplit {
|
|
|
|
if strings.HasPrefix(opt, "shared:") {
|
|
|
|
sharedMount = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make parent mount PRIVATE if it was shared. It is needed for two
|
|
|
|
// reasons. First of all pivot_root() will fail if parent mount is
|
|
|
|
// shared. Secondly when we bind mount rootfs it will propagate to
|
|
|
|
// parent namespace and we don't want that to happen.
|
|
|
|
if sharedMount {
|
2017-05-10 05:38:27 +08:00
|
|
|
return unix.Mount("", parentMount, "", unix.MS_PRIVATE, "")
|
2015-10-02 05:03:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func prepareRoot(config *configs.Config) error {
|
2017-05-10 05:38:27 +08:00
|
|
|
flag := unix.MS_SLAVE | unix.MS_REC
|
2015-10-02 05:03:02 +08:00
|
|
|
if config.RootPropagation != 0 {
|
|
|
|
flag = config.RootPropagation
|
2015-02-01 11:56:27 +08:00
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Mount("", "/", "", uintptr(flag), ""); err != nil {
|
2015-02-01 11:56:27 +08:00
|
|
|
return err
|
|
|
|
}
|
2016-10-25 23:15:11 +08:00
|
|
|
|
|
|
|
// Make parent mount private to make sure following bind mount does
|
|
|
|
// not propagate in other namespaces. Also it will help with kernel
|
|
|
|
// check pass in pivot_root. (IS_SHARED(new_mnt->mnt_parent))
|
|
|
|
if err := rootfsParentMountPrivate(config.Rootfs); err != nil {
|
|
|
|
return err
|
2015-10-02 05:03:02 +08:00
|
|
|
}
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
return unix.Mount(config.Rootfs, config.Rootfs, "bind", unix.MS_BIND|unix.MS_REC, "")
|
2015-02-01 11:56:27 +08:00
|
|
|
}
|
2015-02-10 05:16:43 +08:00
|
|
|
|
|
|
|
func setReadonly() error {
|
2017-05-10 05:38:27 +08:00
|
|
|
return unix.Mount("/", "/", "bind", unix.MS_BIND|unix.MS_REMOUNT|unix.MS_RDONLY|unix.MS_REC, "")
|
2015-02-10 05:16:43 +08:00
|
|
|
}
|
|
|
|
|
2016-06-03 23:29:34 +08:00
|
|
|
func setupPtmx(config *configs.Config) error {
|
2015-02-10 05:16:43 +08:00
|
|
|
ptmx := filepath.Join(config.Rootfs, "dev/ptmx")
|
|
|
|
if err := os.Remove(ptmx); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Symlink("pts/ptmx", ptmx); err != nil {
|
|
|
|
return fmt.Errorf("symlink dev ptmx %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-19 05:52:10 +08:00
|
|
|
// pivotRoot will call pivot_root such that rootfs becomes the new root
|
|
|
|
// filesystem, and everything else is cleaned up.
|
|
|
|
func pivotRoot(rootfs string) error {
|
|
|
|
// While the documentation may claim otherwise, pivot_root(".", ".") is
|
|
|
|
// actually valid. What this results in is / being the new root but
|
|
|
|
// /proc/self/cwd being the old root. Since we can play around with the cwd
|
|
|
|
// with pivot_root this allows us to pivot without creating directories in
|
|
|
|
// the rootfs. Shout-outs to the LXC developers for giving us this idea.
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
oldroot, err := unix.Open("/", unix.O_DIRECTORY|unix.O_RDONLY, 0)
|
2016-10-19 05:52:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-02-10 05:16:43 +08:00
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
defer unix.Close(oldroot)
|
2016-10-19 05:52:10 +08:00
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
newroot, err := unix.Open(rootfs, unix.O_DIRECTORY|unix.O_RDONLY, 0)
|
2015-02-10 05:16:43 +08:00
|
|
|
if err != nil {
|
2016-10-19 05:52:10 +08:00
|
|
|
return err
|
2015-02-10 05:16:43 +08:00
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
defer unix.Close(newroot)
|
2016-10-19 05:52:10 +08:00
|
|
|
|
|
|
|
// Change to the new root so that the pivot_root actually acts on it.
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Fchdir(newroot); err != nil {
|
2016-10-19 05:52:10 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.PivotRoot(".", "."); err != nil {
|
2016-10-25 23:15:11 +08:00
|
|
|
return fmt.Errorf("pivot_root %s", err)
|
2015-02-10 05:16:43 +08:00
|
|
|
}
|
2016-10-19 05:52:10 +08:00
|
|
|
|
|
|
|
// Currently our "." is oldroot (according to the current kernel code).
|
|
|
|
// However, purely for safety, we will fchdir(oldroot) since there isn't
|
|
|
|
// really any guarantee from the kernel what /proc/self/cwd will be after a
|
|
|
|
// pivot_root(2).
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Fchdir(oldroot); err != nil {
|
2016-10-19 05:52:10 +08:00
|
|
|
return err
|
2015-02-10 05:16:43 +08:00
|
|
|
}
|
2015-10-02 05:03:02 +08:00
|
|
|
|
2017-06-28 23:20:23 +08:00
|
|
|
// Make oldroot rslave to make sure our unmounts don't propagate to the
|
|
|
|
// host (and thus bork the machine). We don't use rprivate because this is
|
|
|
|
// known to cause issues due to races where we still have a reference to a
|
|
|
|
// mount while a process in the host namespace are trying to operate on
|
|
|
|
// something they think has no mounts (devicemapper in particular).
|
|
|
|
if err := unix.Mount("", ".", "", unix.MS_SLAVE|unix.MS_REC, ""); err != nil {
|
2016-10-19 05:52:10 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Preform the unmount. MNT_DETACH allows us to unmount /proc/self/cwd.
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Unmount(".", unix.MNT_DETACH); err != nil {
|
2015-10-02 05:03:02 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-19 05:52:10 +08:00
|
|
|
// Switch back to our shiny new root.
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Chdir("/"); err != nil {
|
2016-10-19 05:52:10 +08:00
|
|
|
return fmt.Errorf("chdir / %s", err)
|
2015-02-10 05:16:43 +08:00
|
|
|
}
|
2016-02-24 19:31:07 +08:00
|
|
|
return nil
|
2015-02-10 05:16:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func msMoveRoot(rootfs string) error {
|
2020-03-13 09:14:39 +08:00
|
|
|
mountinfos, err := mountinfo.GetMounts(func(info *mountinfo.Info) (skip, stop bool) {
|
|
|
|
skip = false
|
|
|
|
stop = false
|
|
|
|
// Collect every sysfs and proc file systems, except those under the container rootfs
|
|
|
|
if (info.Fstype != "proc" && info.Fstype != "sysfs") || strings.HasPrefix(info.Mountpoint, rootfs) {
|
|
|
|
skip = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
2019-01-12 04:53:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range mountinfos {
|
2020-03-13 09:11:07 +08:00
|
|
|
p := info.Mountpoint
|
2019-01-12 04:53:45 +08:00
|
|
|
// Be sure umount events are not propagated to the host.
|
|
|
|
if err := unix.Mount("", p, "", unix.MS_SLAVE|unix.MS_REC, ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := unix.Unmount(p, unix.MNT_DETACH); err != nil {
|
|
|
|
if err != unix.EINVAL && err != unix.EPERM {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
// If we have not privileges for umounting (e.g. rootless), then
|
|
|
|
// cover the path.
|
|
|
|
if err := unix.Mount("tmpfs", p, "tmpfs", 0, ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Mount(rootfs, "/", "", unix.MS_MOVE, ""); err != nil {
|
2015-02-10 05:16:43 +08:00
|
|
|
return err
|
|
|
|
}
|
2020-07-15 16:22:07 +08:00
|
|
|
return chroot()
|
2018-01-26 00:36:37 +08:00
|
|
|
}
|
|
|
|
|
2020-07-15 16:22:07 +08:00
|
|
|
func chroot() error {
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Chroot("."); err != nil {
|
2015-02-10 05:16:43 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
return unix.Chdir("/")
|
2015-02-10 05:16:43 +08:00
|
|
|
}
|
2015-02-10 06:22:52 +08:00
|
|
|
|
2015-02-10 06:42:21 +08:00
|
|
|
// createIfNotExists creates a file or a directory only if it does not already exist.
|
2015-02-10 06:22:52 +08:00
|
|
|
func createIfNotExists(path string, isDir bool) error {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if isDir {
|
2015-02-10 06:42:21 +08:00
|
|
|
return os.MkdirAll(path, 0755)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
Split the code for remounting mount points and mounting paths.
A remount of a mount point must include all the current flags or
these will be cleared:
```
The mountflags and data arguments should match the values used in the
original mount() call, except for those parameters that are being
deliberately changed.
```
The current code does not do this; the bug manifests in the specified
flags for `/dev` being lost on remount read only at present. As we
need to specify flags, split the code path for this from remounting
paths which are not mount points, as these can only inherit the
existing flags of the path, and these cannot be changed.
In the bind case, remove extra flags from the bind remount. A bind
mount can only be remounted read only, no other flags can be set,
all other flags are inherited from the parent. From the man page:
```
Since Linux 2.6.26, this flag can also be used to make an existing
bind mount read-only by specifying mountflags as:
MS_REMOUNT | MS_BIND | MS_RDONLY
Note that only the MS_RDONLY setting of the bind mount can be changed
in this manner.
```
MS_REC can only be set on the original bind, so move this. See note
in man page on bind mounts:
```
The remaining bits in the mountflags argument are also ignored, with
the exception of MS_REC.
```
Signed-off-by: Justin Cormack <justin.cormack@docker.com>
2016-12-11 10:25:02 +08:00
|
|
|
// readonlyPath will make a path read only.
|
|
|
|
func readonlyPath(path string) error {
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Mount(path, path, "", unix.MS_BIND|unix.MS_REC, ""); err != nil {
|
Split the code for remounting mount points and mounting paths.
A remount of a mount point must include all the current flags or
these will be cleared:
```
The mountflags and data arguments should match the values used in the
original mount() call, except for those parameters that are being
deliberately changed.
```
The current code does not do this; the bug manifests in the specified
flags for `/dev` being lost on remount read only at present. As we
need to specify flags, split the code path for this from remounting
paths which are not mount points, as these can only inherit the
existing flags of the path, and these cannot be changed.
In the bind case, remove extra flags from the bind remount. A bind
mount can only be remounted read only, no other flags can be set,
all other flags are inherited from the parent. From the man page:
```
Since Linux 2.6.26, this flag can also be used to make an existing
bind mount read-only by specifying mountflags as:
MS_REMOUNT | MS_BIND | MS_RDONLY
Note that only the MS_RDONLY setting of the bind mount can be changed
in this manner.
```
MS_REC can only be set on the original bind, so move this. See note
in man page on bind mounts:
```
The remaining bits in the mountflags argument are also ignored, with
the exception of MS_REC.
```
Signed-off-by: Justin Cormack <justin.cormack@docker.com>
2016-12-11 10:25:02 +08:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
return unix.Mount(path, path, "", unix.MS_BIND|unix.MS_REMOUNT|unix.MS_RDONLY|unix.MS_REC, "")
|
Split the code for remounting mount points and mounting paths.
A remount of a mount point must include all the current flags or
these will be cleared:
```
The mountflags and data arguments should match the values used in the
original mount() call, except for those parameters that are being
deliberately changed.
```
The current code does not do this; the bug manifests in the specified
flags for `/dev` being lost on remount read only at present. As we
need to specify flags, split the code path for this from remounting
paths which are not mount points, as these can only inherit the
existing flags of the path, and these cannot be changed.
In the bind case, remove extra flags from the bind remount. A bind
mount can only be remounted read only, no other flags can be set,
all other flags are inherited from the parent. From the man page:
```
Since Linux 2.6.26, this flag can also be used to make an existing
bind mount read-only by specifying mountflags as:
MS_REMOUNT | MS_BIND | MS_RDONLY
Note that only the MS_RDONLY setting of the bind mount can be changed
in this manner.
```
MS_REC can only be set on the original bind, so move this. See note
in man page on bind mounts:
```
The remaining bits in the mountflags argument are also ignored, with
the exception of MS_REC.
```
Signed-off-by: Justin Cormack <justin.cormack@docker.com>
2016-12-11 10:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// remountReadonly will remount an existing mount point and ensure that it is read-only.
|
|
|
|
func remountReadonly(m *configs.Mount) error {
|
|
|
|
var (
|
|
|
|
dest = m.Destination
|
|
|
|
flags = m.Flags
|
|
|
|
)
|
2015-02-10 06:42:21 +08:00
|
|
|
for i := 0; i < 5; i++ {
|
2017-08-25 06:20:19 +08:00
|
|
|
// There is a special case in the kernel for
|
|
|
|
// MS_REMOUNT | MS_BIND, which allows us to change only the
|
|
|
|
// flags even as an unprivileged user (i.e. user namespace)
|
|
|
|
// assuming we don't drop any security related flags (nodev,
|
|
|
|
// nosuid, etc.). So, let's use that case so that we can do
|
|
|
|
// this re-mount without failing in a userns.
|
|
|
|
flags |= unix.MS_REMOUNT | unix.MS_BIND | unix.MS_RDONLY
|
|
|
|
if err := unix.Mount("", dest, "", uintptr(flags), ""); err != nil {
|
2015-02-10 06:42:21 +08:00
|
|
|
switch err {
|
2017-05-10 05:38:27 +08:00
|
|
|
case unix.EBUSY:
|
2015-02-10 06:42:21 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
return err
|
2015-02-10 06:22:52 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-10 06:42:21 +08:00
|
|
|
return nil
|
|
|
|
}
|
Split the code for remounting mount points and mounting paths.
A remount of a mount point must include all the current flags or
these will be cleared:
```
The mountflags and data arguments should match the values used in the
original mount() call, except for those parameters that are being
deliberately changed.
```
The current code does not do this; the bug manifests in the specified
flags for `/dev` being lost on remount read only at present. As we
need to specify flags, split the code path for this from remounting
paths which are not mount points, as these can only inherit the
existing flags of the path, and these cannot be changed.
In the bind case, remove extra flags from the bind remount. A bind
mount can only be remounted read only, no other flags can be set,
all other flags are inherited from the parent. From the man page:
```
Since Linux 2.6.26, this flag can also be used to make an existing
bind mount read-only by specifying mountflags as:
MS_REMOUNT | MS_BIND | MS_RDONLY
Note that only the MS_RDONLY setting of the bind mount can be changed
in this manner.
```
MS_REC can only be set on the original bind, so move this. See note
in man page on bind mounts:
```
The remaining bits in the mountflags argument are also ignored, with
the exception of MS_REC.
```
Signed-off-by: Justin Cormack <justin.cormack@docker.com>
2016-12-11 10:25:02 +08:00
|
|
|
return fmt.Errorf("unable to mount %s as readonly max retries reached", dest)
|
2015-02-10 06:42:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-23 15:02:10 +08:00
|
|
|
// maskPath masks the top of the specified path inside a container to avoid
|
|
|
|
// security issues from processes reading information from non-namespace aware
|
|
|
|
// mounts ( proc/kcore ).
|
|
|
|
// For files, maskPath bind mounts /dev/null over the top of the specified path.
|
|
|
|
// For directories, maskPath mounts read-only tmpfs over the top of the specified path.
|
2018-03-10 03:29:06 +08:00
|
|
|
func maskPath(path string, mountLabel string) error {
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Mount("/dev/null", path, "", unix.MS_BIND, ""); err != nil && !os.IsNotExist(err) {
|
|
|
|
if err == unix.ENOTDIR {
|
2018-03-10 03:29:06 +08:00
|
|
|
return unix.Mount("tmpfs", path, "tmpfs", unix.MS_RDONLY, label.FormatMountLabel("", mountLabel))
|
2016-09-23 15:02:10 +08:00
|
|
|
}
|
2015-02-13 08:23:05 +08:00
|
|
|
return err
|
2015-02-10 06:22:52 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-23 10:17:30 +08:00
|
|
|
|
|
|
|
// writeSystemProperty writes the value to a path under /proc/sys as determined from the key.
|
|
|
|
// For e.g. net.ipv4.ip_forward translated to /proc/sys/net/ipv4/ip_forward.
|
|
|
|
func writeSystemProperty(key, value string) error {
|
|
|
|
keyPath := strings.Replace(key, ".", "/", -1)
|
|
|
|
return ioutil.WriteFile(path.Join("/proc/sys", keyPath), []byte(value), 0644)
|
|
|
|
}
|
2015-09-25 00:43:12 +08:00
|
|
|
|
|
|
|
func remount(m *configs.Mount, rootfs string) error {
|
|
|
|
var (
|
|
|
|
dest = m.Destination
|
|
|
|
)
|
|
|
|
if !strings.HasPrefix(dest, rootfs) {
|
|
|
|
dest = filepath.Join(rootfs, dest)
|
|
|
|
}
|
2018-10-14 03:14:03 +08:00
|
|
|
return unix.Mount(m.Source, dest, m.Device, uintptr(m.Flags|unix.MS_REMOUNT), "")
|
2015-09-25 00:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do the mount operation followed by additional mounts required to take care
|
|
|
|
// of propagation flags.
|
|
|
|
func mountPropagate(m *configs.Mount, rootfs string, mountLabel string) error {
|
|
|
|
var (
|
2016-02-24 05:56:01 +08:00
|
|
|
dest = m.Destination
|
|
|
|
data = label.FormatMountLabel(m.Data, mountLabel)
|
|
|
|
flags = m.Flags
|
2015-09-25 00:43:12 +08:00
|
|
|
)
|
2016-04-25 22:15:17 +08:00
|
|
|
if libcontainerUtils.CleanPath(dest) == "/dev" {
|
2017-05-10 05:38:27 +08:00
|
|
|
flags &= ^unix.MS_RDONLY
|
2016-02-24 05:56:01 +08:00
|
|
|
}
|
2016-05-27 03:36:54 +08:00
|
|
|
|
|
|
|
copyUp := m.Extensions&configs.EXT_COPYUP == configs.EXT_COPYUP
|
|
|
|
if !(copyUp || strings.HasPrefix(dest, rootfs)) {
|
2015-09-25 00:43:12 +08:00
|
|
|
dest = filepath.Join(rootfs, dest)
|
|
|
|
}
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Mount(m.Source, dest, m.Device, uintptr(flags), data); err != nil {
|
2015-09-25 00:43:12 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pflag := range m.PropagationFlags {
|
2017-05-10 05:38:27 +08:00
|
|
|
if err := unix.Mount("", dest, "", uintptr(pflag), ""); err != nil {
|
2015-09-25 00:43:12 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-14 20:54:17 +08:00
|
|
|
|
|
|
|
func mountNewCgroup(m *configs.Mount) error {
|
|
|
|
var (
|
|
|
|
data = m.Data
|
|
|
|
source = m.Source
|
|
|
|
)
|
|
|
|
if data == "systemd" {
|
|
|
|
data = cgroups.CgroupNamePrefix + data
|
|
|
|
source = "systemd"
|
|
|
|
}
|
|
|
|
if err := unix.Mount(source, m.Destination, m.Device, uintptr(m.Flags), data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|