Skip to content

Commit 66eb2a3

Browse files
author
Tycho Andersen
committed
fix --read-only containers under --userns-remap
The documentation here: https://docs.docker.com/engine/security/userns-remap/#user-namespace-known-limitations says that readonly containers can't be used with user namespaces do to some kernel restriction. In fact, there is a special case in the kernel to be able to do stuff like this, so let's use it. This takes us from: ubuntu@docker:~$ docker run -it --read-only ubuntu docker: Error response from daemon: oci runtime error: container_linux.go:262: starting container process caused "process_linux.go:339: container init caused \"rootfs_linux.go:125: remounting \\\"/dev\\\" as readonly caused \\\"operation not permitted\\\"\"". to: ubuntu@docker:~$ docker-runc --version runc version 1.0.0-rc4+dev commit: ae29480-dirty spec: 1.0.0 ubuntu@docker:~$ docker run -it --read-only ubuntu root@181e2acb909a:/# touch foo touch: cannot touch 'foo': Read-only file system Signed-off-by: Tycho Andersen <[email protected]>
1 parent ae29480 commit 66eb2a3

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

libcontainer/rootfs_linux.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,14 @@ func remountReadonly(m *configs.Mount) error {
733733
flags = m.Flags
734734
)
735735
for i := 0; i < 5; i++ {
736-
if err := unix.Mount("", dest, "", uintptr(flags|unix.MS_REMOUNT|unix.MS_RDONLY), ""); err != nil {
736+
// There is a special case in the kernel for
737+
// MS_REMOUNT | MS_BIND, which allows us to change only the
738+
// flags even as an unprivileged user (i.e. user namespace)
739+
// assuming we don't drop any security related flags (nodev,
740+
// nosuid, etc.). So, let's use that case so that we can do
741+
// this re-mount without failing in a userns.
742+
flags |= unix.MS_REMOUNT | unix.MS_BIND | unix.MS_RDONLY
743+
if err := unix.Mount("", dest, "", uintptr(flags), ""); err != nil {
737744
switch err {
738745
case unix.EBUSY:
739746
time.Sleep(100 * time.Millisecond)

0 commit comments

Comments
 (0)