Skip to content

Commit 280901e

Browse files
committedAug 13, 2013
add -insecure flag and relevant tests
1 parent 04cd0a3 commit 280901e

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed
 

‎container.go

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type Config struct {
7878
VolumesFrom string
7979
Entrypoint []string
8080
NetworkDisabled bool
81+
Privileged bool
8182
}
8283

8384
type HostConfig struct {
@@ -108,6 +109,7 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig,
108109
flMemory := cmd.Int64("m", 0, "Memory limit (in bytes)")
109110
flContainerIDFile := cmd.String("cidfile", "", "Write the container ID to the file")
110111
flNetwork := cmd.Bool("n", true, "Enable networking for this container")
112+
flPrivileged := cmd.Bool("privileged", false, "Give extended privileges to this container")
111113

112114
if capabilities != nil && *flMemory > 0 && !capabilities.MemoryLimit {
113115
//fmt.Fprintf(stdout, "WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n")
@@ -194,6 +196,7 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig,
194196
Volumes: flVolumes,
195197
VolumesFrom: *flVolumesFrom,
196198
Entrypoint: entrypoint,
199+
Privileged: *flPrivileged,
197200
}
198201
hostConfig := &HostConfig{
199202
Binds: binds,

‎container_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -1313,3 +1313,35 @@ func TestOnlyLoopbackExistsWhenUsingDisableNetworkOption(t *testing.T) {
13131313
}
13141314

13151315
}
1316+
1317+
func TestPrivilegedCanMknod(t *testing.T) {
1318+
runtime := mkRuntime(t)
1319+
defer nuke(runtime)
1320+
if output, _ := runContainer(runtime, []string{"-privileged", "_", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok"}, t); output != "ok\n" {
1321+
t.Fatal("Could not mknod into privileged container")
1322+
}
1323+
}
1324+
1325+
func TestPrivilegedCanMount(t *testing.T) {
1326+
runtime := mkRuntime(t)
1327+
defer nuke(runtime)
1328+
if output, _ := runContainer(runtime, []string{"-privileged", "_", "sh", "-c", "mount -t tmpfs none /tmp && echo ok"}, t); output != "ok\n" {
1329+
t.Fatal("Could not mount into privileged container")
1330+
}
1331+
}
1332+
1333+
func TestPrivilegedCannotMknod(t *testing.T) {
1334+
runtime := mkRuntime(t)
1335+
defer nuke(runtime)
1336+
if output, _ := runContainer(runtime, []string{"_", "sh", "-c", "mknod /tmp/sda b 8 0 || echo ok"}, t); output != "ok\n" {
1337+
t.Fatal("Could mknod into secure container")
1338+
}
1339+
}
1340+
1341+
func TestPrivilegedCannotMount(t *testing.T) {
1342+
runtime := mkRuntime(t)
1343+
defer nuke(runtime)
1344+
if output, _ := runContainer(runtime, []string{"_", "sh", "-c", "mount -t tmpfs none /tmp || echo ok"}, t); output != "ok\n" {
1345+
t.Fatal("Could mount into secure container")
1346+
}
1347+
}

‎docs/sources/commandline/command/run.rst

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-e=[]: Set environment variables
2020
-h="": Container host name
2121
-i=false: Keep stdin open even if not attached
22+
-privileged=false: Give extended privileges to this container
2223
-m=0: Memory limit (in bytes)
2324
-n=true: Enable networking for this container
2425
-p=[]: Map a network port to the container
@@ -38,3 +39,15 @@ Examples
3839
docker run -cidfile /tmp/docker_test.cid ubuntu echo "test"
3940
4041
| This will create a container and print "test" to the console. The cidfile flag makes docker attempt to create a new file and write the container ID to it. If the file exists already, docker will return an error. Docker will close this file when docker run exits.
42+
43+
.. code-block:: bash
44+
45+
docker run mount -t tmpfs none /var/spool/squid
46+
47+
| This will *not* work, because by default, most potentially dangerous kernel capabilities are dropped; including ``cap_sys_admin`` (which is required to mount filesystems). However, the ``-privileged`` flag will allow it to run:
48+
49+
.. code-block:: bash
50+
51+
docker run -privileged mount -t tmpfs none /var/spool/squid
52+
53+
| The ``-privileged`` flag gives *all* capabilities to the container, and it also lifts all the limitations enforced by the ``device`` cgroup controller. In other words, the container can then do almost everything that the host can do. This flag exists to allow special use-cases, like running Docker within Docker.

‎lxc_template.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ lxc.console = none
4040
# no controlling tty at all
4141
lxc.tty = 1
4242
43+
{{if .Config.Privileged}}
44+
lxc.cgroup.devices.allow = a
45+
{{else}}
4346
# no implicit access to devices
4447
lxc.cgroup.devices.deny = a
4548
@@ -69,7 +72,7 @@ lxc.cgroup.devices.allow = c 10:200 rwm
6972
7073
# rtc
7174
#lxc.cgroup.devices.allow = c 254:0 rwm
72-
75+
{{end}}
7376
7477
# standard mount point
7578
# WARNING: procfs is a known attack vector and should probably be disabled
@@ -95,11 +98,15 @@ lxc.mount.entry = {{$realPath}} {{$ROOTFS}}/{{$virtualPath}} none bind,{{ if ind
9598
{{end}}
9699
{{end}}
97100
101+
{{if .Config.Privileged}}
102+
# retain all capabilities; no lxc.cap.drop line
103+
{{else}}
98104
# drop linux capabilities (apply mainly to the user root in the container)
99105
# (Note: 'lxc.cap.keep' is coming soon and should replace this under the
100106
# security principle 'deny all unless explicitly permitted', see
101107
# http://sourceforge.net/mailarchive/message.php?msg_id=31054627 )
102108
lxc.cap.drop = audit_control audit_write mac_admin mac_override mknod setfcap setpcap sys_admin sys_boot sys_module sys_nice sys_pacct sys_rawio sys_resource sys_time sys_tty_config
109+
{{end}}
103110
104111
# limits
105112
{{if .Config.Memory}}

1 commit comments

Comments
 (1)

hinorashi commented on Nov 6, 2019

@hinorashi

I see no comment, so i just add one, have a nice day mate 💃

Please sign in to comment.