Skip to content

Commit e735791

Browse files
corherelaurazard
authored andcommitted
runc-shim: refuse to start execs after init exits
The runc task state machine prevents execs from being created after the init process has exited, but there are no guards against starting a created exec after the init process has exited. That leaves a small window for starting an exec to race our handling of the init process exiting. Normally this is not an issue in practice: the kernel will atomically kill all processes in a PID namespace when its "init" process terminates, and will not allow new processes to fork(2) into the PID namespace afterwards. Therefore the racing exec is guaranteed by the kernel to not be running after the init process terminates. On the other hand, when the container does not have a private PID namespace (i.e. the container's init process is not the "init" process of the container's PID namespace), the kernel does not automatically kill other container processes on init exit and will happily allow runc to start an exec process at any time. It is the runc shim's responsibility to clean up the container when the init process exits in this situation by killing all the container's remaining processes. Block execs from being started after the container's init process has exited to prevent the processes from leaking, and to avoid violating the task service's assumption that an exec can be running iff the init process is also running. Signed-off-by: Cory Snider <[email protected]>
1 parent 7f3bf99 commit e735791

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

cmd/containerd-shim-runc-v2/task/service.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func NewTaskService(ctx context.Context, publisher shim.Publisher, sd shutdown.S
8282
containers: make(map[string]*runc.Container),
8383
running: make(map[int][]containerProcess),
8484
pendingExecs: make(map[*runc.Container]int),
85+
execable: make(map[*runc.Container]bool),
8586
exitSubscribers: make(map[*map[int][]runcC.Exit]struct{}),
8687
}
8788
go s.processExits()
@@ -118,6 +119,12 @@ type service struct {
118119
lifecycleMu sync.Mutex
119120
running map[int][]containerProcess // pid -> running process, guarded by lifecycleMu
120121
pendingExecs map[*runc.Container]int // container -> num pending execs, guarded by lifecycleMu
122+
// container -> execs can be started, guarded by lifecycleMu.
123+
// Execs can be started if the container's init process (read: pid, not [process.Init])
124+
// has been started and not yet reaped by the shim.
125+
// Note that this flag gets updated before the container's [process.Init.Status]
126+
// is transitioned to "stopped".
127+
execable map[*runc.Container]bool
121128
// Subscriptions to exits for PIDs. Adding/deleting subscriptions and
122129
// dereferencing the subscription pointers must only be done while holding
123130
// lifecycleMu.
@@ -231,6 +238,9 @@ func (s *service) preStart(c *runc.Container) (handleStarted func(*runc.Containe
231238
Container: c,
232239
Process: p,
233240
})
241+
if init {
242+
s.execable[c] = true
243+
}
234244
s.lifecycleMu.Unlock()
235245
}
236246
}
@@ -305,6 +315,10 @@ func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (*taskAPI.
305315
if r.ExecID == "" {
306316
cinit = container
307317
} else {
318+
if !s.execable[container] {
319+
s.lifecycleMu.Unlock()
320+
return nil, errdefs.ToGRPCf(errdefs.ErrFailedPrecondition, "container %s init process is not running", container.ID)
321+
}
308322
s.pendingExecs[container]++
309323
}
310324
handleStarted, cleanup := s.preStart(cinit)
@@ -680,6 +694,9 @@ func (s *service) processExits() {
680694
var cps, skipped []containerProcess
681695
for _, cp := range s.running[e.Pid] {
682696
_, init := cp.Process.(*process.Init)
697+
if init {
698+
delete(s.execable, cp.Container)
699+
}
683700
if init && s.pendingExecs[cp.Container] != 0 {
684701
// This exit relates to a container for which we have pending execs. In
685702
// order to ensure order between execs and the init process for a given

0 commit comments

Comments
 (0)