Skip to content

Commit

Permalink
Merge pull request #3403 from kolyshkin/1.1-exec-subcgroup
Browse files Browse the repository at this point in the history
[1.1] runc exec --cgroup: ensure the path is a sub-cgroup path
  • Loading branch information
AkihiroSuda committed Mar 8, 2022
2 parents b9460f2 + 35784a3 commit 51feb42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 10 additions & 2 deletions libcontainer/container_linux.go
Expand Up @@ -636,7 +636,11 @@ func (c *linuxContainer) newSetnsProcess(p *Process, cmd *exec.Cmd, messageSockP
// cgroup v1: using the same path for all controllers.
// cgroup v2: the only possible way.
for k := range proc.cgroupPaths {
proc.cgroupPaths[k] = path.Join(proc.cgroupPaths[k], add)
subPath := path.Join(proc.cgroupPaths[k], add)
if !strings.HasPrefix(subPath, proc.cgroupPaths[k]) {
return nil, fmt.Errorf("%s is not a sub cgroup path", add)
}
proc.cgroupPaths[k] = subPath
}
// cgroup v2: do not try to join init process's cgroup
// as a fallback (see (*setnsProcess).start).
Expand All @@ -645,7 +649,11 @@ func (c *linuxContainer) newSetnsProcess(p *Process, cmd *exec.Cmd, messageSockP
// Per-controller paths.
for ctrl, add := range p.SubCgroupPaths {
if val, ok := proc.cgroupPaths[ctrl]; ok {
proc.cgroupPaths[ctrl] = path.Join(val, add)
subPath := path.Join(val, add)
if !strings.HasPrefix(subPath, val) {
return nil, fmt.Errorf("%s is not a sub cgroup path", add)
}
proc.cgroupPaths[ctrl] = subPath
} else {
return nil, fmt.Errorf("unknown controller %s in SubCgroupPaths", ctrl)
}
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/exec.bats
Expand Up @@ -197,6 +197,11 @@ function check_exec_debug() {
__runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
testcontainer test_busybox running

# Check we can't join parent cgroup.
runc exec --cgroup ".." test_busybox cat /proc/self/cgroup
[ "$status" -ne 0 ]
[[ "$output" == *" .. is not a sub cgroup path"* ]]

# Check we can't join non-existing subcgroup.
runc exec --cgroup nonexistent test_busybox cat /proc/self/cgroup
[ "$status" -ne 0 ]
Expand Down Expand Up @@ -243,6 +248,11 @@ function check_exec_debug() {
__runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
testcontainer test_busybox running

# Check we can't join parent cgroup.
runc exec --cgroup ".." test_busybox cat /proc/self/cgroup
[ "$status" -ne 0 ]
[[ "$output" == *" .. is not a sub cgroup path"* ]]

# Check we can't join non-existing subcgroup.
runc exec --cgroup nonexistent test_busybox cat /proc/self/cgroup
[ "$status" -ne 0 ]
Expand Down

0 comments on commit 51feb42

Please sign in to comment.