From 36ddd3050cc2c4d476299cfb9455f4c745ff22bf Mon Sep 17 00:00:00 2001 From: Konstantin Bogomolov Date: Fri, 7 Oct 2022 14:40:32 -0700 Subject: [PATCH] Check if ThreadGroup exists before executing JoinProcessGroup. The caller of JoinProcessGroup (in linux.Setpgid) cannot guarantee that the ThreadGroup used to make the call is valid before acquiring the TaskSet mutex. This is essentially the same same fix as in cl/381351069 It is still possible for setpgid to race with a single task exiting (i.e. a task exits in the middle of a setpgid call), but this will not cause problems since JoinProcessGroup operates on ThreadGroups. As long as the target ThreadGroup remains valid throughout the call we are ok. PiperOrigin-RevId: 479671524 --- pkg/sentry/kernel/sessions.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/sentry/kernel/sessions.go b/pkg/sentry/kernel/sessions.go index c217707ef..47536213b 100644 --- a/pkg/sentry/kernel/sessions.go +++ b/pkg/sentry/kernel/sessions.go @@ -440,6 +440,11 @@ func (tg *ThreadGroup) JoinProcessGroup(pidns *PIDNamespace, pgid ProcessGroupID pidns.owner.mu.Lock() defer pidns.owner.mu.Unlock() + // Check whether the process still exists or not. + if _, ok := pidns.tgids[tg]; !ok { + return linuxerr.ESRCH + } + // Lookup the ProcessGroup. pg := pidns.processGroups[pgid] if pg == nil {