Skip to content

Commit

Permalink
libct: Signal: honor RootlessCgroups
Browse files Browse the repository at this point in the history
`signalAllProcesses()` depends on the cgroup and is expected to fail
when runc is running in rootless without an access to the cgroup.

Fix issue 4394

Signed-off-by: Akihiro Suda <[email protected]>
  • Loading branch information
AkihiroSuda committed Sep 4, 2024
1 parent 961b803 commit f35ae2c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,18 @@ func (c *Container) Signal(s os.Signal) error {
// leftover processes. Handle this special case here.
if s == unix.SIGKILL && !c.config.Namespaces.IsPrivate(configs.NEWPID) {
if err := signalAllProcesses(c.cgroupManager, unix.SIGKILL); err != nil {
if c.config.RootlessCgroups { // may not have an access to cgroup
return c.signal(s)
}
return fmt.Errorf("unable to kill all processes: %w", err)
}
return nil
}

return c.signal(s)
}

func (c *Container) signal(s os.Signal) error {
// To avoid a PID reuse attack, don't kill non-running container.
if !c.hasInit() {
return ErrNotRunning
Expand Down
1 change: 1 addition & 0 deletions libcontainer/state_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func destroy(c *Container) error {
// and destroy is supposed to remove all the container resources, we need
// to kill those processes here.
if !c.config.Namespaces.IsPrivate(configs.NEWPID) {
// Likely to fail when c.config.RootlessCgroups is true
_ = signalAllProcesses(c.cgroupManager, unix.SIGKILL)
}
if err := c.cgroupManager.Destroy(); err != nil {
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/kill.bats
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,27 @@ test_host_pidns_kill() {
test_host_pidns_kill
unset KILL_INIT
}

# https://github.com/opencontainers/runc/issues/4394 (cgroup v1, rootless)
@test "kill KILL [shared pidns]" {
update_config '.process.args = ["sleep", "infinity"]'

runc run -d --console-socket "$CONSOLE_SOCKET" target_ctr
[ "$status" -eq 0 ]
testcontainer target_ctr running
target_pid="$(__runc state target_ctr | jq .pid)"
update_config '.linux.namespaces |= map(if .type == "user" or .type == "pid" then (.path = "/proc/'"$target_pid"'/ns/" + .type) else . end) | del(.linux.uidMappings) | del(.linux.gidMappings)'

runc run -d --console-socket "$CONSOLE_SOCKET" attached_ctr
testcontainer attached_ctr running
[ "$status" -eq 0 ]

runc kill attached_ctr 9
[ "$status" -eq 0 ]

runc delete --force attached_ctr
[ "$status" -eq 0 ]

runc delete --force target_ctr
[ "$status" -eq 0 ]
}

0 comments on commit f35ae2c

Please sign in to comment.