Browse code

Merge pull request #5903 from alexlarsson/writable-proc

Make /proc writable, but not /proc/sys and /proc/sysrq-trigger

Victor Marmol authored on 2014/05/20 04:21:15
Showing 2 changed files
... ...
@@ -81,7 +81,7 @@ func Init(container *libcontainer.Container, uncleanRootfs, consolePath string,
81 81
 		return fmt.Errorf("set process label %s", err)
82 82
 	}
83 83
 	if container.Context["restrictions"] != "" {
84
-		if err := restrict.Restrict("proc", "sys"); err != nil {
84
+		if err := restrict.Restrict("proc/sys", "proc/sysrq-trigger", "proc/irq", "proc/bus", "sys"); err != nil {
85 85
 			return err
86 86
 		}
87 87
 	}
... ...
@@ -10,12 +10,31 @@ import (
10 10
 	"github.com/dotcloud/docker/pkg/system"
11 11
 )
12 12
 
13
+const defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV
14
+
15
+func mountReadonly(path string) error {
16
+	if err := system.Mount("", path, "", syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil {
17
+		if err == syscall.EINVAL {
18
+			// Probably not a mountpoint, use bind-mount
19
+			if err := system.Mount(path, path, "", syscall.MS_BIND, ""); err != nil {
20
+				return err
21
+			}
22
+			if err := system.Mount(path, path, "", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC|defaultMountFlags, ""); err != nil {
23
+				return err
24
+			}
25
+		} else {
26
+			return err
27
+		}
28
+	}
29
+	return nil
30
+}
31
+
13 32
 // This has to be called while the container still has CAP_SYS_ADMIN (to be able to perform mounts).
14 33
 // However, afterwards, CAP_SYS_ADMIN should be dropped (otherwise the user will be able to revert those changes).
15 34
 func Restrict(mounts ...string) error {
16 35
 	// remount proc and sys as readonly
17 36
 	for _, dest := range mounts {
18
-		if err := system.Mount("", dest, "", syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil {
37
+		if err := mountReadonly(dest); err != nil {
19 38
 			return fmt.Errorf("unable to remount %s readonly: %s", dest, err)
20 39
 		}
21 40
 	}