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

libct/init_linux: retry chdir to fix EACCES (regression in rc93) #2894

Merged
merged 1 commit into from
Apr 7, 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
26 changes: 22 additions & 4 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ func finalizeNamespace(config *initConfig) error {
return errors.Wrap(err, "close exec fds")
}

// we only do chdir if it's specified
doChdir := config.Cwd != ""
if doChdir {
// First, attempt the chdir before setting up the user.
// This could allow us to access a directory that the user running runc can access
// but the container user cannot.
err := unix.Chdir(config.Cwd)
switch {
case err == nil:
doChdir = false
case os.IsPermission(err):
// If we hit an EPERM, we should attempt again after setting up user.
Copy link
Contributor

Choose a reason for hiding this comment

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

this needs to mention EACCES not EPERM.

// This will allow us to successfully chdir if the container user has access
// to the directory, but the user running runc does not.
// This is useful in cases where the cwd is also a volume that's been chowned to the container user.
default:
return fmt.Errorf("chdir to cwd (%q) set in config.json failed: %v", config.Cwd, err)
}
}

caps := &configs.Capabilities{}
if config.Capabilities != nil {
caps = config.Capabilities
Expand All @@ -152,10 +172,8 @@ func finalizeNamespace(config *initConfig) error {
if err := setupUser(config); err != nil {
return errors.Wrap(err, "setup user")
}
// Change working directory AFTER the user has been set up.
// Otherwise, if the cwd is also a volume that's been chowned to the container user (and not the user running runc),
// this command will EPERM.
if config.Cwd != "" {
// Change working directory AFTER the user has been set up, if we haven't done it yet.
if doChdir {
if err := unix.Chdir(config.Cwd); err != nil {
return fmt.Errorf("chdir to cwd (%q) set in config.json failed: %v", config.Cwd, err)
}
Expand Down
18 changes: 17 additions & 1 deletion tests/integration/cwd.bats
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function teardown() {

# Verify a cwd owned by the container user can be chdir'd to,
# even if runc doesn't have the privilege to do so.
@test "runc create sets up user before chdir to cwd" {
@test "runc create sets up user before chdir to cwd if needed" {
requires rootless rootless_idmap

# Some setup for this test (AUX_DIR and AUX_UID) is done
Expand All @@ -56,3 +56,19 @@ function teardown() {
runc run test_busybox
[ "$status" -eq 0 ]
}

# Verify a cwd not owned by the container user can be chdir'd to,
# if runc does have the privilege to do so.
@test "runc create can chdir if runc has access" {
requires root

mkdir -p rootfs/home/nonroot
chmod 700 rootfs/home/nonroot

update_config ' .process.cwd = "/root"
| .process.user.uid = 42
| .process.args |= ["ls", "/tmp"]'

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