Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runc run: fix readonly path error for rootless + host pidns #2897

Merged
merged 2 commits into from
Apr 19, 2021
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
15 changes: 13 additions & 2 deletions libcontainer/rootfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,9 +931,20 @@ func readonlyPath(path string) error {
if os.IsNotExist(err) {
return nil
}
return err
return &os.PathError{Op: "bind-mount", Path: path, Err: err}
}

var s unix.Statfs_t
if err := unix.Statfs(path, &s); err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about trying mount first and only going statfs route on EPERM, but this way the code path will rarely be hit (and thus tested), and a single statfs doesn't add too much overhead.

return &os.PathError{Op: "statfs", Path: path, Err: err}
}
return unix.Mount(path, path, "", unix.MS_BIND|unix.MS_REMOUNT|unix.MS_RDONLY|unix.MS_REC, "")
flags := uintptr(s.Flags) & (unix.MS_NOSUID | unix.MS_NODEV | unix.MS_NOEXEC)

if err := unix.Mount(path, path, "", flags|unix.MS_BIND|unix.MS_REMOUNT|unix.MS_RDONLY, ""); err != nil {
return &os.PathError{Op: "bind-mount-ro", Path: path, Err: err}
}

return nil
}

// remountReadonly will remount an existing mount point and ensure that it is read-only.
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/start_hello.bats
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,20 @@ function teardown() {

[[ "$(cat pid.txt)" =~ [0-9]+ ]]
}

# https://github.com/opencontainers/runc/pull/2897
@test "runc run [rootless with host pidns]" {
requires rootless_no_features

# Remove pid namespace, and replace /proc mount
# with a bind mount from the host.
update_config ' .linux.namespaces -= [{"type": "pid"}]
| .mounts |= map((select(.type == "proc")
| .type = "none"
| .source = "/proc"
| .options = ["rbind", "nosuid", "nodev", "noexec"]
) // .)'

runc run test_hello
[ "$status" -eq 0 ]
}