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

Fix a regression when killing and deleting a container with shared(host) pid namespace #4048

Closed
wants to merge 5 commits into from
Closed
Changes from 2 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
37 changes: 31 additions & 6 deletions libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,6 @@ func (c *Container) Signal(s os.Signal) error {
if err != nil {
return err
}
// To avoid a PID reuse attack, don't kill non-running container.
switch status {
case Running, Created, Paused:
default:
return ErrNotRunning
}

// When a container has its own PID namespace, inside it the init PID
// is 1, and thus it is handled specially by the kernel. In particular,
Expand All @@ -383,8 +377,19 @@ func (c *Container) Signal(s os.Signal) error {
// OTOH, if PID namespace is shared, we should kill all pids to avoid
// leftover processes.
if s == unix.SIGKILL && !c.config.Namespaces.IsPrivate(configs.NEWPID) {
if pids, err := c.cgroupManager.GetAllPids(); c.ignoreCgroupError(err) != nil {
return err
} else if len(pids) == 0 {
return ErrNotRunning
}
err = signalAllProcesses(c.cgroupManager, unix.SIGKILL)
} else {
// To avoid a PID reuse attack, don't kill non-running container.
switch status {
case Running, Created, Paused:
default:
return ErrNotRunning
}
err = c.initProcess.signal(s)
}
if err != nil {
Expand Down Expand Up @@ -868,6 +873,26 @@ func (c *Container) newInitConfig(process *Process) *initConfig {
func (c *Container) Destroy() error {
c.m.Lock()
defer c.m.Unlock()
if !c.config.Namespaces.IsPrivate(configs.NEWPID) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am very much against doing this. Kill should do all the killing, and Destroy should remove the files. This was all mixed up before, but I got it untangled in #3825 (alas, with a couple of regressions which you have reported in #4047).

The alternative to doing this, without mixing the kill and destroy again, is this: 7de61c4 (PTAL 🙏🏻)

Copy link
Member Author

Choose a reason for hiding this comment

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

But I think we can't force users to use runc kill before runc delete if the container is in stopped state.

const retries = 10
for i := 0; i < retries; i++ {
pids, err := c.cgroupManager.GetAllPids()
if c.ignoreCgroupError(err) != nil {
return err
}
if len(pids) > 0 {
if err := signalAllProcesses(c.cgroupManager, unix.SIGKILL); c.ignoreCgroupError(err) != nil {
return err
}
if i == retries-1 {
return errors.New("some processes are still running in the container")
}
time.Sleep(100 * time.Millisecond)
} else {
break
}
}
}
return c.state.destroy()
}

Expand Down