Skip to content

Commit 5a0a03e

Browse files
committed
Merge pull request moby#5922 from crosbymichael/host-dev-priv
Mount /dev in tmpfs for privileged containers
2 parents 2fec15c + f042c3c commit 5a0a03e

File tree

8 files changed

+142
-44
lines changed

8 files changed

+142
-44
lines changed

daemon/execdriver/native/create.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/dotcloud/docker/daemon/execdriver/native/template"
1111
"github.com/dotcloud/docker/pkg/apparmor"
1212
"github.com/dotcloud/docker/pkg/libcontainer"
13+
"github.com/dotcloud/docker/pkg/libcontainer/mount/nodes"
1314
)
1415

1516
// createContainer populates and configures the container type with the
@@ -34,8 +35,6 @@ func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Container
3435
if err := d.setPrivileged(container); err != nil {
3536
return nil, err
3637
}
37-
} else {
38-
container.Mounts = append(container.Mounts, libcontainer.Mount{Type: "devtmpfs"})
3938
}
4039
if err := d.setupCgroups(container, c); err != nil {
4140
return nil, err
@@ -97,12 +96,17 @@ func (d *driver) createNetwork(container *libcontainer.Container, c *execdriver.
9796
return nil
9897
}
9998

100-
func (d *driver) setPrivileged(container *libcontainer.Container) error {
99+
func (d *driver) setPrivileged(container *libcontainer.Container) (err error) {
101100
container.Capabilities = libcontainer.GetAllCapabilities()
102101
container.Cgroups.DeviceAccess = true
103102

104103
delete(container.Context, "restrictions")
105104

105+
container.OptionalDeviceNodes = nil
106+
if container.RequiredDeviceNodes, err = nodes.GetHostDeviceNodes(); err != nil {
107+
return err
108+
}
109+
106110
if apparmor.IsEnabled() {
107111
container.Context["apparmor_profile"] = "unconfined"
108112
}

daemon/execdriver/native/template/default_template.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/dotcloud/docker/pkg/apparmor"
55
"github.com/dotcloud/docker/pkg/libcontainer"
66
"github.com/dotcloud/docker/pkg/libcontainer/cgroups"
7+
"github.com/dotcloud/docker/pkg/libcontainer/mount/nodes"
78
)
89

910
// New returns the docker default configuration for libcontainer
@@ -32,7 +33,9 @@ func New() *libcontainer.Container {
3233
Parent: "docker",
3334
DeviceAccess: false,
3435
},
35-
Context: libcontainer.Context{},
36+
Context: libcontainer.Context{},
37+
RequiredDeviceNodes: nodes.DefaultNodes,
38+
OptionalDeviceNodes: []string{"fuse"},
3639
}
3740
if apparmor.IsEnabled() {
3841
container.Context["apparmor_profile"] = "docker-default"

pkg/libcontainer/container.go

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,90 @@ import (
44
"github.com/dotcloud/docker/pkg/libcontainer/cgroups"
55
)
66

7-
// Context is a generic key value pair that allows
8-
// arbatrary data to be sent
7+
// Context is a generic key value pair that allows arbatrary data to be sent
98
type Context map[string]string
109

11-
// Container defines configuration options for how a
12-
// container is setup inside a directory and how a process should be executed
10+
// Container defines configuration options for executing a process inside a contained environment
1311
type Container struct {
14-
Hostname string `json:"hostname,omitempty"` // hostname
15-
ReadonlyFs bool `json:"readonly_fs,omitempty"` // set the containers rootfs as readonly
16-
NoPivotRoot bool `json:"no_pivot_root,omitempty"` // this can be enabled if you are running in ramdisk
17-
User string `json:"user,omitempty"` // user to execute the process as
18-
WorkingDir string `json:"working_dir,omitempty"` // current working directory
19-
Env []string `json:"environment,omitempty"` // environment to set
20-
Tty bool `json:"tty,omitempty"` // setup a proper tty or not
21-
Namespaces map[string]bool `json:"namespaces,omitempty"` // namespaces to apply
22-
Capabilities []string `json:"capabilities,omitempty"` // capabilities given to the container
23-
Networks []*Network `json:"networks,omitempty"` // nil for host's network stack
24-
Cgroups *cgroups.Cgroup `json:"cgroups,omitempty"` // cgroups
25-
Context Context `json:"context,omitempty"` // generic context for specific options (apparmor, selinux)
26-
Mounts Mounts `json:"mounts,omitempty"`
12+
// Hostname optionally sets the container's hostname if provided
13+
Hostname string `json:"hostname,omitempty"`
14+
15+
// ReadonlyFs will remount the container's rootfs as readonly where only externally mounted
16+
// bind mounts are writtable
17+
ReadonlyFs bool `json:"readonly_fs,omitempty"`
18+
19+
// NoPivotRoot will use MS_MOVE and a chroot to jail the process into the container's rootfs
20+
// This is a common option when the container is running in ramdisk
21+
NoPivotRoot bool `json:"no_pivot_root,omitempty"`
22+
23+
// User will set the uid and gid of the executing process running inside the container
24+
User string `json:"user,omitempty"`
25+
26+
// WorkingDir will change the processes current working directory inside the container's rootfs
27+
WorkingDir string `json:"working_dir,omitempty"`
28+
29+
// Env will populate the processes environment with the provided values
30+
// Any values from the parent processes will be cleared before the values
31+
// provided in Env are provided to the process
32+
Env []string `json:"environment,omitempty"`
33+
34+
// Tty when true will allocate a pty slave on the host for access by the container's process
35+
// and ensure that it is mounted inside the container's rootfs
36+
Tty bool `json:"tty,omitempty"`
37+
38+
// Namespaces specifies the container's namespaces that it should setup when cloning the init process
39+
// If a namespace is not provided that namespace is shared from the container's parent process
40+
Namespaces map[string]bool `json:"namespaces,omitempty"`
41+
42+
// Capabilities specify the capabilities to keep when executing the process inside the container
43+
// All capbilities not specified will be dropped from the processes capability mask
44+
Capabilities []string `json:"capabilities,omitempty"`
45+
46+
// Networks specifies the container's network setup to be created
47+
Networks []*Network `json:"networks,omitempty"`
48+
49+
// Cgroups specifies specific cgroup settings for the various subsystems that the container is
50+
// placed into to limit the resources the container has available
51+
Cgroups *cgroups.Cgroup `json:"cgroups,omitempty"`
52+
53+
// Context is a generic key value format that allows for additional settings to be passed
54+
// on the container's creation
55+
// This is commonly used to specify apparmor profiles, selinux labels, and different restrictions
56+
// placed on the container's processes
57+
Context Context `json:"context,omitempty"`
58+
59+
// Mounts specify additional source and destination paths that will be mounted inside the container's
60+
// rootfs and mount namespace if specified
61+
Mounts Mounts `json:"mounts,omitempty"`
62+
63+
// RequiredDeviceNodes are a list of device nodes that will be mknod into the container's rootfs at /dev
64+
// If the host system does not support the device that the container requests an error is returned
65+
RequiredDeviceNodes []string `json:"required_device_nodes,omitempty"`
66+
67+
// OptionalDeviceNodes are a list of device nodes that will be mknod into the container's rootfs at /dev
68+
// If the host system does not support the device that the container requests the error is ignored
69+
OptionalDeviceNodes []string `json:"optional_device_nodes,omitempty"`
2770
}
2871

2972
// Network defines configuration for a container's networking stack
3073
//
3174
// The network configuration can be omited from a container causing the
3275
// container to be setup with the host's networking stack
3376
type Network struct {
34-
Type string `json:"type,omitempty"` // type of networking to setup i.e. veth, macvlan, etc
35-
Context Context `json:"context,omitempty"` // generic context for type specific networking options
36-
Address string `json:"address,omitempty"`
37-
Gateway string `json:"gateway,omitempty"`
38-
Mtu int `json:"mtu,omitempty"`
77+
// Type sets the networks type, commonly veth and loopback
78+
Type string `json:"type,omitempty"`
79+
80+
// Context is a generic key value format for setting additional options that are specific to
81+
// the network type
82+
Context Context `json:"context,omitempty"`
83+
84+
// Address contains the IP and mask to set on the network interface
85+
Address string `json:"address,omitempty"`
86+
87+
// Gateway sets the gateway address that is used as the default for the interface
88+
Gateway string `json:"gateway,omitempty"`
89+
90+
// Mtu sets the mtu value for the interface and will be mirrored on both the host and
91+
// container's interfaces if a pair is created, specifically in the case of type veth
92+
Mtu int `json:"mtu,omitempty"`
3993
}

pkg/libcontainer/container.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,13 @@
4343
{
4444
"type": "devtmpfs"
4545
}
46+
],
47+
"required_device_nodes": [
48+
"null",
49+
"zero",
50+
"full",
51+
"random",
52+
"urandom",
53+
"tty"
4654
]
4755
}

pkg/libcontainer/container_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"encoding/json"
55
"os"
66
"testing"
7+
8+
"github.com/dotcloud/docker/pkg/libcontainer/mount/nodes"
79
)
810

911
// Checks whether the expected capability is specified in the capabilities.
10-
func hasCapability(expected string, capabilities []string) bool {
11-
for _, capability := range capabilities {
12-
if capability == expected {
12+
func contains(expected string, values []string) bool {
13+
for _, v := range values {
14+
if v == expected {
1315
return true
1416
}
1517
}
@@ -47,18 +49,25 @@ func TestContainerJsonFormat(t *testing.T) {
4749
t.Fail()
4850
}
4951

50-
if hasCapability("SYS_ADMIN", container.Capabilities) {
52+
if contains("SYS_ADMIN", container.Capabilities) {
5153
t.Log("SYS_ADMIN should not be enabled in capabilities mask")
5254
t.Fail()
5355
}
5456

55-
if !hasCapability("MKNOD", container.Capabilities) {
57+
if !contains("MKNOD", container.Capabilities) {
5658
t.Log("MKNOD should be enabled in capabilities mask")
5759
t.Fail()
5860
}
5961

60-
if hasCapability("SYS_CHROOT", container.Capabilities) {
62+
if contains("SYS_CHROOT", container.Capabilities) {
6163
t.Log("capabilities mask should not contain SYS_CHROOT")
6264
t.Fail()
6365
}
66+
67+
for _, n := range nodes.DefaultNodes {
68+
if !contains(n, container.RequiredDeviceNodes) {
69+
t.Logf("devices should contain %s", n)
70+
t.Fail()
71+
}
72+
}
6473
}

pkg/libcontainer/mount/init.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ func InitializeMountNamespace(rootfs, console string, container *libcontainer.Co
4848
if err := setupBindmounts(rootfs, container.Mounts); err != nil {
4949
return fmt.Errorf("bind mounts %s", err)
5050
}
51-
if err := nodes.CopyN(rootfs, nodes.DefaultNodes, true); err != nil {
52-
return fmt.Errorf("copy dev nodes %s", err)
51+
if err := nodes.CopyN(rootfs, container.RequiredDeviceNodes, true); err != nil {
52+
return fmt.Errorf("copy required dev nodes %s", err)
5353
}
54-
if err := nodes.CopyN(rootfs, nodes.AdditionalNodes, false); err != nil {
55-
return fmt.Errorf("copy additional dev nodes %s", err)
54+
if err := nodes.CopyN(rootfs, container.OptionalDeviceNodes, false); err != nil {
55+
return fmt.Errorf("copy optional dev nodes %s", err)
5656
}
5757
if err := SetupPtmx(rootfs, console, container.Context["mount_label"]); err != nil {
5858
return err
@@ -195,12 +195,10 @@ func newSystemMounts(rootfs, mountLabel string, mounts libcontainer.Mounts) []mo
195195
systemMounts := []mount{
196196
{source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags},
197197
{source: "sysfs", path: filepath.Join(rootfs, "sys"), device: "sysfs", flags: defaultMountFlags},
198+
{source: "tmpfs", path: filepath.Join(rootfs, "dev"), device: "tmpfs", flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME, data: label.FormatMountLabel("mode=755", mountLabel)},
198199
{source: "shm", path: filepath.Join(rootfs, "dev", "shm"), device: "tmpfs", flags: defaultMountFlags, data: label.FormatMountLabel("mode=1777,size=65536k", mountLabel)},
199200
{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)},
200201
}
201202

202-
if len(mounts.OfType("devtmpfs")) == 1 {
203-
systemMounts = append([]mount{{source: "tmpfs", path: filepath.Join(rootfs, "dev"), device: "tmpfs", flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME, data: label.FormatMountLabel("mode=755", mountLabel)}}, systemMounts...)
204-
}
205203
return systemMounts
206204
}

pkg/libcontainer/mount/nodes/nodes.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package nodes
44

55
import (
66
"fmt"
7+
"io/ioutil"
78
"os"
89
"path/filepath"
910
"syscall"
@@ -21,11 +22,6 @@ var DefaultNodes = []string{
2122
"tty",
2223
}
2324

24-
// AdditionalNodes includes nodes that are not required
25-
var AdditionalNodes = []string{
26-
"fuse",
27-
}
28-
2925
// CopyN copies the device node from the host into the rootfs
3026
func CopyN(rootfs string, nodesToCopy []string, shouldExist bool) error {
3127
oldMask := system.Umask(0000)
@@ -61,3 +57,18 @@ func Copy(rootfs, node string, shouldExist bool) error {
6157
}
6258
return nil
6359
}
60+
61+
func GetHostDeviceNodes() ([]string, error) {
62+
files, err := ioutil.ReadDir("/dev")
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
out := []string{}
68+
for _, f := range files {
69+
if f.Mode()&os.ModeDevice == os.ModeDevice {
70+
out = append(out, f.Name())
71+
}
72+
}
73+
return out, nil
74+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// +build !linux
2+
3+
package nodes
4+
5+
import "github.com/dotcloud/docker/pkg/libcontainer"
6+
7+
var DefaultNodes = []string{}
8+
9+
func GetHostDeviceNodes() ([]string, error) {
10+
return nil, libcontainer.ErrUnsupported
11+
}

0 commit comments

Comments
 (0)