Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit df78075

Browse files
committed
Update restrictions for better handling of mounts
This also cleans up some of the left over restriction paths code from before. Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
1 parent dcc4061 commit df78075

File tree

4 files changed

+25
-53
lines changed

4 files changed

+25
-53
lines changed

mount/init.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,12 @@ func newSystemMounts(rootfs, mountLabel string, mounts libcontainer.Mounts) []mo
123123
systemMounts := []mount{
124124
{source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags},
125125
{source: "sysfs", path: filepath.Join(rootfs, "sys"), device: "sysfs", flags: defaultMountFlags},
126+
{source: "shm", path: filepath.Join(rootfs, "dev", "shm"), device: "tmpfs", flags: defaultMountFlags, data: label.FormatMountLabel("mode=1777,size=65536k", mountLabel)},
127+
{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)},
126128
}
127129

128130
if len(mounts.OfType("devtmpfs")) == 1 {
129131
systemMounts = append(systemMounts, mount{source: "tmpfs", path: filepath.Join(rootfs, "dev"), device: "tmpfs", flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME, data: label.FormatMountLabel("mode=755", mountLabel)})
130132
}
131-
systemMounts = append(systemMounts,
132-
mount{source: "shm", path: filepath.Join(rootfs, "dev", "shm"), device: "tmpfs", flags: defaultMountFlags, data: label.FormatMountLabel("mode=1777,size=65536k", mountLabel)},
133-
mount{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)},
134-
)
135-
136133
return systemMounts
137134
}

nsinit/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func Init(container *libcontainer.Container, uncleanRootfs, consolePath string,
7272

7373
runtime.LockOSThread()
7474

75-
if restrictionPath := container.Context["restriction_path"]; restrictionPath != "" {
76-
if err := restrict.Restrict("/", restrictionPath); err != nil {
75+
if container.Context["restrictions"] != "" {
76+
if err := restrict.Restrict(); err != nil {
7777
return err
7878
}
7979
}

security/restrict/restrict.go

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,42 @@ import (
1111
"github.com/dotcloud/docker/pkg/system"
1212
)
1313

14-
// "restrictions" are container paths (files, directories, whatever) that have to be masked.
15-
// maskPath is a "safe" path to be mounted over maskedPath. It can take two special values:
16-
// - if it is "", then nothing is mounted;
17-
// - if it is "EMPTY", then an empty directory is mounted instead.
18-
// If remountRO is true then the maskedPath is remounted read-only (regardless of whether a maskPath was used).
19-
type restriction struct {
20-
maskedPath string
21-
maskPath string
22-
remountRO bool
23-
}
24-
25-
var restrictions = []restriction{
26-
{"/proc", "", true},
27-
{"/sys", "", true},
28-
{"/proc/kcore", "/dev/null", false},
29-
}
30-
3114
// This has to be called while the container still has CAP_SYS_ADMIN (to be able to perform mounts).
3215
// However, afterwards, CAP_SYS_ADMIN should be dropped (otherwise the user will be able to revert those changes).
33-
// "empty" should be the path to an empty directory.
34-
func Restrict(rootfs, empty string) error {
35-
for _, restriction := range restrictions {
36-
dest := filepath.Join(rootfs, restriction.maskedPath)
37-
if restriction.maskPath != "" {
38-
var source string
39-
if restriction.maskPath == "EMPTY" {
40-
source = empty
41-
} else {
42-
source = filepath.Join(rootfs, restriction.maskPath)
43-
}
44-
if err := system.Mount(source, dest, "", syscall.MS_BIND, ""); err != nil {
45-
return fmt.Errorf("unable to bind-mount %s over %s: %s", source, dest, err)
46-
}
47-
}
48-
if restriction.remountRO {
49-
if err := system.Mount("", dest, "", syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil {
50-
return fmt.Errorf("unable to remount %s readonly: %s", dest, err)
51-
}
16+
func Restrict() error {
17+
// remount proc and sys as readonly
18+
for _, dest := range []string{"proc", "sys"} {
19+
if err := system.Mount("", dest, "", syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil {
20+
return fmt.Errorf("unable to remount %s readonly: %s", dest, err)
5221
}
5322
}
5423

24+
if err := system.Mount("/proc/kcore", "/dev/null", "", syscall.MS_BIND, ""); err != nil {
25+
return fmt.Errorf("unable to bind-mount /dev/null over /proc/kcore")
26+
}
27+
5528
// This weird trick will allow us to mount /proc read-only, while being able to use AppArmor.
5629
// This is because apparently, loading an AppArmor profile requires write access to /proc/1/attr.
5730
// So we do another mount of procfs, ensure it's write-able, and bind-mount a subset of it.
58-
tmpProcPath := filepath.Join(rootfs, ".proc")
59-
if err := os.Mkdir(tmpProcPath, 0700); err != nil {
60-
return fmt.Errorf("unable to create temporary proc mountpoint %s: %s", tmpProcPath, err)
31+
var (
32+
rwAttrPath = filepath.Join(".proc", "1", "attr")
33+
roAttrPath = filepath.Join("proc", "1", "attr")
34+
)
35+
36+
if err := os.Mkdir(".proc", 0700); err != nil {
37+
return fmt.Errorf("unable to create temporary proc mountpoint .proc: %s", err)
6138
}
62-
if err := system.Mount("proc", tmpProcPath, "proc", 0, ""); err != nil {
39+
if err := system.Mount("proc", ".proc", "proc", 0, ""); err != nil {
6340
return fmt.Errorf("unable to mount proc on temporary proc mountpoint: %s", err)
6441
}
65-
if err := system.Mount("proc", tmpProcPath, "", syscall.MS_REMOUNT, ""); err != nil {
42+
if err := system.Mount("proc", ".proc", "", syscall.MS_REMOUNT, ""); err != nil {
6643
return fmt.Errorf("unable to remount proc read-write: %s", err)
6744
}
68-
rwAttrPath := filepath.Join(rootfs, ".proc", "1", "attr")
69-
roAttrPath := filepath.Join(rootfs, "proc", "1", "attr")
7045
if err := system.Mount(rwAttrPath, roAttrPath, "", syscall.MS_BIND, ""); err != nil {
7146
return fmt.Errorf("unable to bind-mount %s on %s: %s", rwAttrPath, roAttrPath, err)
7247
}
73-
if err := system.Unmount(tmpProcPath, 0); err != nil {
48+
if err := system.Unmount(".proc", 0); err != nil {
7449
return fmt.Errorf("unable to unmount temporary proc filesystem: %s", err)
7550
}
76-
return nil
51+
return os.RemoveAll(".proc")
7752
}

security/restrict/unsupported.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ package restrict
44

55
import "fmt"
66

7-
func Restrict(rootfs, empty string) error {
7+
func Restrict() error {
88
return fmt.Errorf("not supported")
99
}

0 commit comments

Comments
 (0)