mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-05 08:02:20 -08:00
Since 133489986 (#22076), the session monitor keeps a terminated
leader unreaped until its SIGKILL deadline so its PID and process
group can't be reused. But it also withheld the exit notification
until then, so every canceled monitored session waited out the full
HUP escalation (5s by default) after its shell had already exited.
Report the exit as soon as the leader dies, still reaping only after
SIGKILL. On Linux, read the zombie's wait status with waitid(WNOWAIT),
not from /proc/<pid>/stat, whose exit_code reads as 0 when the monitor
may not ptrace the child, such as the root login process of a non-root
user's PTY session. On macOS and FreeBSD, get it from a kqueue
EVFILT_PROC NOTE_EXIT event registered at spawn and drained when the
monitor sweeps on SIGCHLD. OpenBSD keeps the old behavior until it can
be tested there.
That same commit took TestSessionMonitorCancellation, which sets a 30s
escalation, from 0.1s to 60s, making it one of our slowest tests.
Split it into pipe and PTY top-level tests so the test runner can
schedule them independently, and they now take 2s each on Linux and
macOS. Add TestMonitorTerminateReportsExitEarly, which also runs on
FreeBSD, and TestZombieWaitStatus for the Linux siginfo decoding.
Updates #deflake
Change-Id: I6ccdff33a8d4e4c96a84a27c0dbb64ac74954f47
Signed-off-by: Brad Fitzpatrick <[email protected]>
128 lines
3.8 KiB
Go
128 lines
3.8 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build linux && !android
|
|
|
|
package tailssh
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"math"
|
|
"runtime"
|
|
"strings"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// monChildAttr returns process attributes for a session child.
|
|
// With ctty, the child starts a session with fd 0 as its controlling terminal.
|
|
// Otherwise it starts a process group.
|
|
// Pdeathsig is unset so the child can survive monitor exit.
|
|
func monChildAttr(ctty bool) *syscall.SysProcAttr {
|
|
return &syscall.SysProcAttr{
|
|
Setpgid: !ctty,
|
|
Setsid: ctty,
|
|
Setctty: ctty,
|
|
}
|
|
}
|
|
|
|
// scrubFds marks every open descriptor from 3 up close-on-exec, so the
|
|
// monitor cannot leak an inherited or late-created descriptor into a
|
|
// child it execs. close_range(CLOSE_RANGE_CLOEXEC) does this in one
|
|
// atomic syscall on kernels 5.11 and later; older kernels fall back to
|
|
// a /proc/self/fd enumeration.
|
|
func scrubFds() error {
|
|
if err := unix.CloseRange(3, math.MaxUint32, unix.CLOSE_RANGE_CLOEXEC); err == nil {
|
|
return nil
|
|
}
|
|
return scrubFdsByScan("/proc/self/fd")
|
|
}
|
|
|
|
// fwdChildAttr returns process attributes for a forwarding child.
|
|
// The child runs with ma's credentials in its own process group.
|
|
// Pdeathsig sends SIGHUP when the creating thread exits. See golang/go#27505.
|
|
func fwdChildAttr(ma monitorArgs) *syscall.SysProcAttr {
|
|
return &syscall.SysProcAttr{
|
|
Credential: &syscall.Credential{
|
|
Uid: ma.uid,
|
|
Gid: ma.gid,
|
|
Groups: fwdCredGroups(ma.gid, ma.gids),
|
|
},
|
|
Setpgid: true,
|
|
Pdeathsig: unix.SIGHUP,
|
|
}
|
|
}
|
|
|
|
// exitWatcher does nothing on Linux, where [zombieWaitStatus] reads an
|
|
// unreaped child's wait status directly.
|
|
type exitWatcher struct{}
|
|
|
|
func (*exitWatcher) watch(pid int) {}
|
|
func (*exitWatcher) drain(children map[int]*monChild) {}
|
|
func (*exitWatcher) close() {}
|
|
|
|
// zombieWaitStatus returns the wait status of child pid if it has exited
|
|
// but is not yet reaped, without reaping it, using waitid with WNOWAIT.
|
|
// The pid cannot be reused while the zombie remains unreaped.
|
|
//
|
|
// It doesn't read /proc/<pid>/stat's exit_code field, which reads as 0
|
|
// unless the caller may ptrace the child. A non-root monitor's PTY
|
|
// leader, login, runs as root.
|
|
func zombieWaitStatus(pid int) (ws unix.WaitStatus, ok bool) {
|
|
var info unix.Siginfo
|
|
for {
|
|
err := unix.Waitid(unix.P_PID, pid, &info, unix.WEXITED|unix.WNOWAIT|unix.WNOHANG, nil)
|
|
if err == unix.EINTR {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
break
|
|
}
|
|
return siginfoWaitStatus(&info)
|
|
}
|
|
|
|
// The kernel's siginfo_t is 128 bytes on every Linux architecture.
|
|
var _ [128]byte = [unsafe.Sizeof(unix.Siginfo{})]byte{}
|
|
|
|
// siginfoWaitStatus converts a waitid result to a wait status. It reports
|
|
// false if no child had exited, which WNOHANG reports with si_pid zero.
|
|
//
|
|
// [unix.Siginfo] omits the SIGCHLD fields, so read them at their fixed
|
|
// offsets: after the si_signo, si_errno, and si_code ints (si_code second
|
|
// on MIPS), the union is pointer-aligned and starts with si_pid, si_uid,
|
|
// and si_status.
|
|
func siginfoWaitStatus(info *unix.Siginfo) (ws unix.WaitStatus, ok bool) {
|
|
b := (*[128]byte)(unsafe.Pointer(info))
|
|
codeOff := 8
|
|
if strings.HasPrefix(runtime.GOARCH, "mips") {
|
|
codeOff = 4
|
|
}
|
|
unionOff := 12
|
|
if unsafe.Sizeof(uintptr(0)) == 8 {
|
|
unionOff = 16
|
|
}
|
|
get := func(off int) int32 { return int32(binary.NativeEndian.Uint32(b[off:])) }
|
|
if get(unionOff) == 0 {
|
|
return 0, false
|
|
}
|
|
status := uint32(get(unionOff + 8))
|
|
const (
|
|
cldExited = 1 // CLD_EXITED
|
|
cldKilled = 2 // CLD_KILLED
|
|
cldDumped = 3 // CLD_DUMPED
|
|
)
|
|
switch get(codeOff) {
|
|
case cldExited:
|
|
return unix.WaitStatus(status&0xff) << 8, true
|
|
case cldKilled:
|
|
return unix.WaitStatus(status & 0x7f), true
|
|
case cldDumped:
|
|
return unix.WaitStatus(status&0x7f | 0x80), true
|
|
}
|
|
return 0, false
|
|
}
|