Skip to content

Commit 19d696e

Browse files
committed
merge branch 'pr-3276'
Kir Kolyshkin (2): runc run: fix ro /dev test/int/mount.bats: refer to github issue LGTMs: thaJeztah cyphar Closes #3276
2 parents 0e79754 + b247cd3 commit 19d696e

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

libcontainer/rootfs_linux.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,16 @@ func prepareRootfs(pipe io.ReadWriter, iConfig *initConfig, mountFds []int) (err
164164
// finalizeRootfs sets anything to ro if necessary. You must call
165165
// prepareRootfs first.
166166
func finalizeRootfs(config *configs.Config) (err error) {
167-
// remount dev as ro if specified
167+
// All tmpfs mounts and /dev were previously mounted as rw
168+
// by mountPropagate. Remount them read-only as requested.
168169
for _, m := range config.Mounts {
169-
if utils.CleanPath(m.Destination) == "/dev" {
170-
if m.Flags&unix.MS_RDONLY == unix.MS_RDONLY {
171-
if err := remountReadonly(m); err != nil {
172-
return err
173-
}
170+
if m.Flags&unix.MS_RDONLY != unix.MS_RDONLY {
171+
continue
172+
}
173+
if m.Device == "tmpfs" || utils.CleanPath(m.Destination) == "/dev" {
174+
if err := remountReadonly(m); err != nil {
175+
return err
174176
}
175-
break
176177
}
177178
}
178179

@@ -452,12 +453,6 @@ func mountToRootfs(m *configs.Mount, c *mountConfig) error {
452453
return err
453454
}
454455
}
455-
// Initially mounted rw in mountPropagate, remount to ro if flag set.
456-
if m.Flags&unix.MS_RDONLY != 0 {
457-
if err := remount(m, rootfs, mountFd); err != nil {
458-
return err
459-
}
460-
}
461456
return nil
462457
case "bind":
463458
if err := prepareBindMount(m, rootfs, mountFd); err != nil {
@@ -1092,10 +1087,10 @@ func mountPropagate(m *configs.Mount, rootfs string, mountLabel string, mountFd
10921087
flags = m.Flags
10931088
)
10941089
// Delay mounting the filesystem read-only if we need to do further
1095-
// operations on it. We need to set up files in "/dev" and tmpfs mounts may
1096-
// need to be chmod-ed after mounting. The mount will be remounted ro later
1097-
// in finalizeRootfs() if necessary.
1098-
if utils.CleanPath(m.Destination) == "/dev" || m.Device == "tmpfs" {
1090+
// operations on it. We need to set up files in "/dev", and other tmpfs
1091+
// mounts may need to be chmod-ed after mounting. These mounts will be
1092+
// remounted ro later in finalizeRootfs(), if necessary.
1093+
if m.Device == "tmpfs" || utils.CleanPath(m.Destination) == "/dev" {
10991094
flags &= ^unix.MS_RDONLY
11001095
}
11011096

tests/integration/mounts.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function teardown() {
2323
[[ "${lines[0]}" == *'/tmp/bind/config.json'* ]]
2424
}
2525

26+
# https://github.com/opencontainers/runc/issues/2246
2627
@test "runc run [ro tmpfs mount]" {
2728
update_config ' .mounts += [{
2829
source: "tmpfs",
@@ -37,6 +38,16 @@ function teardown() {
3738
[[ "${lines[0]}" == *'ro,'* ]]
3839
}
3940

41+
# https://github.com/opencontainers/runc/issues/3248
42+
@test "runc run [ro /dev mount]" {
43+
update_config ' .mounts |= map((select(.destination == "/dev") | .options += ["ro"]) // .)
44+
| .process.args |= ["grep", "^tmpfs /dev", "/proc/mounts"]'
45+
46+
runc run test_busybox
47+
[ "$status" -eq 0 ]
48+
[[ "${lines[0]}" == *'ro,'* ]]
49+
}
50+
4051
# https://github.com/opencontainers/runc/issues/2683
4152
@test "runc run [tmpfs mount with absolute symlink]" {
4253
# in container, /conf -> /real/conf

0 commit comments

Comments
 (0)