Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions libcontainer/rootfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,17 +1126,17 @@ func prepareRootInHostMountNS(config *configs.Config) error {
}

func setReadonly() error {
flags := uintptr(unix.MS_BIND | unix.MS_REMOUNT | unix.MS_RDONLY)

err := mount("", "/", "", flags, "")
if err == nil {
return nil
}
// A bind remount sets the per-mount flags to exactly those given, so
// keep the existing nosuid, nodev, noexec and nosymfollow (otherwise
// they are cleared, or the remount fails in a userns where they are
// locked). Atime flags are not passed, so the kernel preserves them.
var s unix.Statfs_t
if err := unix.Statfs("/", &s); err != nil {
return &os.PathError{Op: "statfs", Path: "/", Err: err}
}
flags |= uintptr(s.Flags)
keep := statfsToMountFlags(s) & (unix.MS_NOSUID | unix.MS_NODEV | unix.MS_NOEXEC | unix.MS_NOSYMFOLLOW)
flags := uintptr(keep | unix.MS_BIND | unix.MS_REMOUNT | unix.MS_RDONLY)

return mount("", "/", "", flags, "")
}

Expand Down
17 changes: 17 additions & 0 deletions tests/integration/mounts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function setup() {
}

function teardown() {
umount -l "$ROOT/bundle/rootfs" 2>/dev/null || true
teardown_bundle
}

Expand Down Expand Up @@ -401,3 +402,19 @@ test_mount_target() {
rm -rf rootfs/tmp/hosts
test_mount_target . /etc/hosts /tmp/hosts
}

@test "runc run [readonly rootfs keeps nosuid,nodev]" {
requires root

mount --bind rootfs rootfs
mount -o remount,bind,nosuid,nodev rootfs

update_config '.root.readonly = true'
# shellcheck disable=SC2016
update_config --arg prog '$5 == "/" {print "," $6 ","}' \
'.process.args = ["awk", $prog, "/proc/self/mountinfo"]'
run -0 runc run test_busybox
assert_output --partial ",ro,"
assert_output --partial ",nosuid,"
assert_output --partial ",nodev,"
}
Loading