Browse code

Mount over dev and only copy allowed nodes in Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/04/11 23:30:09
Showing 2 changed files
... ...
@@ -34,6 +34,8 @@ func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Container
34 34
 		if err := d.setPrivileged(container); err != nil {
35 35
 			return nil, err
36 36
 		}
37
+	} else {
38
+		container.Mounts = append(container.Mounts, libcontainer.Mount{Type: "devtmpfs"})
37 39
 	}
38 40
 	if err := d.setupCgroups(container, c); err != nil {
39 41
 		return nil, err
... ...
@@ -47,14 +47,14 @@ func setupNewMountNamespace(rootfs, console string, container *libcontainer.Cont
47 47
 	if err := setupBindmounts(rootfs, container.Mounts); err != nil {
48 48
 		return fmt.Errorf("bind mounts %s", err)
49 49
 	}
50
+	if err := copyDevNodes(rootfs); err != nil {
51
+		return fmt.Errorf("copy dev nodes %s", err)
52
+	}
50 53
 	if restrictionPath := container.Context["restriction_path"]; restrictionPath != "" {
51 54
 		if err := restrict.Restrict(rootfs, restrictionPath); err != nil {
52 55
 			return fmt.Errorf("restrict %s", err)
53 56
 		}
54 57
 	}
55
-	if err := copyDevNodes(rootfs); err != nil {
56
-		return fmt.Errorf("copy dev nodes %s", err)
57
-	}
58 58
 	if err := setupPtmx(rootfs, console, container.Context["mount_label"]); err != nil {
59 59
 		return err
60 60
 	}
... ...
@@ -273,12 +273,20 @@ func setupBindmounts(rootfs string, bindMounts libcontainer.Mounts) error {
273 273
 }
274 274
 
275 275
 func newSystemMounts(rootfs, mountLabel string, mounts libcontainer.Mounts) []mount {
276
-	systemMounts := []mount{
277
-		{source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags},
276
+	devMounts := []mount{
278 277
 		{source: "shm", path: filepath.Join(rootfs, "dev", "shm"), device: "tmpfs", flags: defaultMountFlags, data: label.FormatMountLabel("mode=1777,size=65536k", mountLabel)},
279 278
 		{source: "devpts", path: filepath.Join(rootfs, "dev", "pts"), device: "devpts", flags: syscall.MS_NOSUID | syscall.MS_NOEXEC, data: label.FormatMountLabel("newinstance,ptmxmode=0666,mode=620,gid=5", mountLabel)},
280 279
 	}
281 280
 
281
+	systemMounts := []mount{
282
+		{source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags},
283
+	}
284
+
285
+	if len(mounts.OfType("devtmpfs")) == 1 {
286
+		systemMounts = append(systemMounts, mount{source: "tmpfs", path: filepath.Join(rootfs, "dev"), device: "tmpfs", flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME, data: "mode=755"})
287
+	}
288
+	systemMounts = append(systemMounts, devMounts...)
289
+
282 290
 	if len(mounts.OfType("sysfs")) == 1 {
283 291
 		systemMounts = append(systemMounts, mount{source: "sysfs", path: filepath.Join(rootfs, "sys"), device: "sysfs", flags: defaultMountFlags})
284 292
 	}