From 814eaf3b77ef695ffd00b83a9db7e1ae6d494d1e Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Wed, 20 Sep 2023 19:17:45 -0700 Subject: [PATCH] systrap: set PDEATHSIG right after forking a sysmsg thread container_test:TestGoferExits fails by timeout, because there are some sysmsg threads survived sandbox process death. When we fork a new sysmsg thread, it is traced with PTRACE_O_EXITKILL, so we need to set PDEATHSIG before detaching from it. Signed-off-by: Andrei Vagin --- pkg/sentry/platform/systrap/subprocess.go | 15 ++++++++++++++- .../platform/systrap/sysmsg_thread_unsafe.go | 16 +--------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/pkg/sentry/platform/systrap/subprocess.go b/pkg/sentry/platform/systrap/subprocess.go index 4643c617d..2cafc36d6 100644 --- a/pkg/sentry/platform/systrap/subprocess.go +++ b/pkg/sentry/platform/systrap/subprocess.go @@ -228,6 +228,20 @@ func (s *subprocess) handlePtraceSyscallRequest(req any) { panic(fmt.Sprintf("error waiting for new clone: expected SIGSTOP, got %v", sig)) } + t.initRegs = ptraceThread.initRegs + // Set the parent death signal to SIGKILL. + _, err = t.syscallIgnoreInterrupt(&t.initRegs, unix.SYS_PRCTL, + arch.SyscallArgument{Value: linux.PR_SET_PDEATHSIG}, + arch.SyscallArgument{Value: uintptr(unix.SIGKILL)}, + arch.SyscallArgument{Value: 0}, + arch.SyscallArgument{Value: 0}, + arch.SyscallArgument{Value: 0}, + arch.SyscallArgument{Value: 0}, + ) + if err != nil { + panic(fmt.Sprintf("prctl: %v", err)) + } + id, ok := s.sysmsgStackPool.Get() if !ok { panic("unable to allocate a sysmsg stub thread") @@ -240,7 +254,6 @@ func (s *subprocess) handlePtraceSyscallRequest(req any) { // Detach the thread. t.detach() - t.initRegs = ptraceThread.initRegs // Return the thread. r.thread <- t diff --git a/pkg/sentry/platform/systrap/sysmsg_thread_unsafe.go b/pkg/sentry/platform/systrap/sysmsg_thread_unsafe.go index ec71034a8..59d553222 100644 --- a/pkg/sentry/platform/systrap/sysmsg_thread_unsafe.go +++ b/pkg/sentry/platform/systrap/sysmsg_thread_unsafe.go @@ -39,20 +39,6 @@ func (p *sysmsgThread) setMsg(addr uintptr) { func (p *sysmsgThread) init(sentryAddr, guestAddr uintptr) { t := p.thread - - // Set the parent death signal to SIGKILL. - _, err := t.syscallIgnoreInterrupt(&t.initRegs, unix.SYS_PRCTL, - arch.SyscallArgument{Value: linux.PR_SET_PDEATHSIG}, - arch.SyscallArgument{Value: uintptr(unix.SIGKILL)}, - arch.SyscallArgument{Value: 0}, - arch.SyscallArgument{Value: 0}, - arch.SyscallArgument{Value: 0}, - arch.SyscallArgument{Value: 0}, - ) - if err != nil { - panic(fmt.Sprintf("prctl: %v", err)) - } - // Set the sysmsg signal stack. // // sentryAddr is from the stub mapping which is mapped once and never @@ -61,7 +47,7 @@ func (p *sysmsgThread) init(sentryAddr, guestAddr uintptr) { *alt = linux.SignalStack{} alt.Addr = uint64(guestAddr) alt.Size = uint64(sysmsg.MsgOffsetFromSharedStack) - _, err = t.syscallIgnoreInterrupt(&t.initRegs, unix.SYS_SIGALTSTACK, + _, err := t.syscallIgnoreInterrupt(&t.initRegs, unix.SYS_SIGALTSTACK, arch.SyscallArgument{Value: guestAddr}, arch.SyscallArgument{Value: 0}, arch.SyscallArgument{Value: 0},