timeout: handle child processes that use PTRACE_TRACEME

* m4/jm-macros.m4 (coreutils_MACROS): Check for ptrace.
* src/timeout.c [HAVE_PTRACE && (__linux__ || __ANDROID__)]: Include
sys/ptrace.h.
(resume_command): New function.
(main): Call it if the monitored command has been stopped.
* tests/timeout/ptrace-traceme.sh: New file.
* tests/local.mk (all_tests): Add the new test case.
* NEWS: Mention the improvement.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-AKDIHN0Y.

Co-authored-by: Pádraig Brady <P@draigBrady.com>
This commit is contained in:
Collin FunkandPádraig Brady committed 2026-11-13 20:12:25 -08:00
1 parent 247c2a04f8
commit 94b24850da
5 files changed
+128 -3

No files matched your search

+4
View File
@@ -372,6 +372,10 @@ GNU coreutils NEWS -*- outline -*-
output to the same file. This is consistent with the existing bounded
operation when tailing based from the end of a file.
'timeout' no longer leaves commands running, even with --kill-after,
in the unusual case where they turn 'timeout' into their tracer
using ptrace with the PTRACE_TRACEME option.
'wc -m' counts ASCII up to 14x faster, and UTF-8 up to 7x faster.
Commands that support hardware acceleration like 'cksum' and 'wc',
+1
View File
@@ -67,6 +67,7 @@ AC_DEFUN([coreutils_MACROS],
fallocate
fchown
fchmod
ptrace
setgroups
sethostname
siginterrupt
+43 -3
View File
@@ -52,6 +52,9 @@
#if HAVE_PRCTL
# include <sys/prctl.h>
#endif
#if HAVE_PTRACE && (defined __linux__ || defined __ANDROID__)
# include <sys/ptrace.h>
#endif
#include <sys/wait.h>
#include "system.h"
@@ -106,6 +109,35 @@ static struct option const long_options[] =
{NULL, 0, NULL, 0}
};
/* Resume the monitored command that was stopped with signal SIG.
This applies to child processes on Linux who use PTRACE_TRACEME
to turn us into their tracer.
Return true if we should continue waiting for the command. */
static bool
handle_ptrace_stop (MAYBE_UNUSED int sig)
{
#if HAVE_PTRACE && (defined __linux__ || defined __ANDROID__)
siginfo_t si;
/* If stopped by SIGTRAP, it may be because the monitored command made a
successful call to execve(2). If that is not the case, or if SIG is
not SIGTRAP, we want to send the signal to the monitored command.
XXX: Note we drop the TRAP signal in the very unlikely edge case
where a command is sending TRAP to itself and using PTRACE_TRACEME. */
if (sig == SIGTRAP
&& ptrace (PTRACE_GETSIGINFO, monitored_pid, NULL, &si) == 0
&& si.si_code == SI_USER && si.si_pid == monitored_pid)
sig = 0;
/* Restart the monitored command, send signal SIG, and detach from it. */
(void) ptrace (PTRACE_DETACH, monitored_pid, NULL, sig);
return true;
#else
return false;
#endif
}
/* Start the timeout after which we'll receive a SIGALRM.
Round DURATION up to the next representable value.
Treat out-of-range values as if they were maximal,
@@ -686,10 +718,18 @@ main (int argc, char **argv)
/* Note signals remain blocked in parent here, to ensure
we don't cleanup() after waitpid() reaps the child,
to avoid sending signals to a possibly different process. */
to avoid sending signals to a possibly different process.
while ((wait_result = waitpid (monitored_pid, &status, WNOHANG)) == 0)
sigsuspend (&orig_set); /* Wait with cleanup signals unblocked. */
Note we only get stopped status for traced processes,
as we're not using WUNTRACED. */
while ((wait_result = waitpid (monitored_pid, &status, WNOHANG)) == 0
|| (0 < wait_result && WIFSTOPPED (status)
&& handle_ptrace_stop (WSTOPSIG (status))))
{
if (wait_result == 0)
sigsuspend (&orig_set); /* Wait with cleanup signals unblocked. */
}
if (wait_result < 0)
{
+1
View File
@@ -537,6 +537,7 @@ all_tests = \
tests/test/test-file.sh \
tests/misc/time-style.sh \
tests/timeout/init-parent.sh \
tests/timeout/ptrace-traceme.sh \
tests/timeout/timeout.sh \
tests/timeout/timeout-blocked.pl \
tests/timeout/timeout-group.sh \
+79
View File
@@ -0,0 +1,79 @@
#!/bin/sh
# Test the behavior of 'timeout' with a child that uses PTRACE_TRACEME.
# Copyright (C) 2026 Free Software Foundation, Inc.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ timeout
require_gcc_shared_
grep '^#define HAVE_PTRACE 1' $CONFIG_HEADER > /dev/null \
|| skip_ 'ptrace is not supported on this system'
# Set up a shared library to use PTRACE_TRACEME upon loading.
cat > k.c <<'EOF' || framework_failure_
#include <sys/ptrace.h>
#include <stddef.h>
static void __attribute__ ((__constructor__))
traceme (void)
{
ptrace (PTRACE_TRACEME, 0, NULL, NULL);
}
EOF
gcc_shared_ k.c k.so || skip_ 'failed to build ptrace shared lib'
# Make sure we can use it.
timeout --kill-after=1 10 env LD_PRELOAD="$LD_PRELOAD":./k.so \
grep '^TracerPid:.[1-9][0-9]*$' /proc/self/status \
> /dev/null \
|| skip_ 'Unable to use PTRACE_TRACEME'
# Test that the signal terminates the program.
# coreutils-9.12 and earlier would print "unknown status" here.
returns_ 124 timeout .1 env LD_PRELOAD="$LD_PRELOAD":./k.so \
sleep 10 >out 2>err || fail=1
compare /dev/null out || fail=1
compare /dev/null err || fail=1
# The KILL signal should be sent if the timeout signal is ignored.
# URG is ignored by default, avoiding a race to install SIG_IGN.
# coreutils-9.12 and earlier would exit with a status of 124, but with
# the command left running.
returns_ 137 timeout --foreground --signal=URG --kill-after .1 .1 \
env LD_PRELOAD="$LD_PRELOAD":./k.so \
sleep 10 >out 2>err || fail=1
compare /dev/null out || fail=1
compare /dev/null err || fail=1
# Signals that were not sent by 'timeout' should be delivered to the
# child process.
# coreutils-9.12 and earlier would exit with a status of 1, but with the
# command left running.
returns_ 3 timeout 10 env LD_PRELOAD="$LD_PRELOAD":./k.so \
"$SHELL" -c 'trap "exit 3" USR1; kill -USR1 "$$"; exit 2' \
>out 2>err || fail=1
compare /dev/null out || fail=1
compare /dev/null err || fail=1
# Make sure a TRAP, when caused by a traced exec, does not
# kill the child process.
returns_ 3 timeout 10 env LD_PRELOAD="$LD_PRELOAD":./k.so \
"$SHELL" -c 'exec "$SHELL" -c "exit 3"' >out 2>err || fail=1
compare /dev/null out || fail=1
compare /dev/null err || fail=1
Exit $fail