From 31d30a985d99a0eef92116a22159727f5c332784 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Fri, 27 Oct 2017 00:21:41 -0700 Subject: [PATCH] Fix user mount /dev/shm size Commit 7120976d74195 ("Implement none, private, and shareable ipc modes") introduces a bug: if a user-specified mount for /dev/shm is provided, its size is overriden by value of ShmSize. A reproducer is simple: docker run --rm --mount type=tmpfs,dst=/dev/shm,tmpfs-size=100K \ alpine df /dev/shm This commit is an attempt to fix the bug, as well as optimize things a but and make the code easier to read. https://github.com/moby/moby/issues/35271 Signed-off-by: Kir Kolyshkin --- daemon/oci_linux.go | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go index 2c5d94d99b..e876e3b6ce 100644 --- a/daemon/oci_linux.go +++ b/daemon/oci_linux.go @@ -528,23 +528,35 @@ func setMounts(daemon *Daemon, s *specs.Spec, c *container.Container, mounts []c userMounts[m.Destination] = struct{}{} } - // Filter out mounts from spec - noIpc := c.HostConfig.IpcMode.IsNone() - // Filter out mounts that are overridden by user supplied mounts + // Copy all mounts from spec to defaultMounts, except for + // - mounts overriden by a user supplied mount; + // - all mounts under /dev if a user supplied /dev is present; + // - /dev/shm, in case IpcMode is none. + // While at it, also + // - set size for /dev/shm from shmsize. var defaultMounts []specs.Mount _, mountDev := userMounts["/dev"] for _, m := range s.Mounts { - // filter out /dev/shm mount if case IpcMode is none - if noIpc && m.Destination == "/dev/shm" { + if _, ok := userMounts[m.Destination]; ok { + // filter out mount overridden by a user supplied mount continue } - // filter out mount overridden by a user supplied mount - if _, ok := userMounts[m.Destination]; !ok { - if mountDev && strings.HasPrefix(m.Destination, "/dev/") { + if mountDev && strings.HasPrefix(m.Destination, "/dev/") { + // filter out everything under /dev if /dev is user-mounted + continue + } + + if m.Destination == "/dev/shm" { + if c.HostConfig.IpcMode.IsNone() { + // filter out /dev/shm for "none" IpcMode continue } - defaultMounts = append(defaultMounts, m) + // set size for /dev/shm mount from spec + sizeOpt := "size=" + strconv.FormatInt(c.HostConfig.ShmSize, 10) + m.Options = append(m.Options, sizeOpt) } + + defaultMounts = append(defaultMounts, m) } s.Mounts = defaultMounts @@ -652,14 +664,6 @@ func setMounts(daemon *Daemon, s *specs.Spec, c *container.Container, mounts []c s.Linux.MaskedPaths = nil } - // Set size for /dev/shm mount that comes from spec (IpcMode: private only) - for i, m := range s.Mounts { - if m.Destination == "/dev/shm" { - sizeOpt := "size=" + strconv.FormatInt(c.HostConfig.ShmSize, 10) - s.Mounts[i].Options = append(s.Mounts[i].Options, sizeOpt) - } - } - // TODO: until a kernel/mount solution exists for handling remount in a user namespace, // we must clear the readonly flag for the cgroups mount (@mrunalp concurs) if uidMap := daemon.idMappings.UIDs(); uidMap != nil || c.HostConfig.Privileged {