From 94b24850da2f534f18de3ee19e79448594096435 Mon Sep 17 00:00:00 2001 From: Collin Funk Date: Thu, 12 Nov 2026 21:51:05 -0800 Subject: [PATCH] timeout: handle child processes that use PTRACE_TRACEME MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- NEWS | 4 ++ m4/jm-macros.m4 | 1 + src/timeout.c | 46 +++++++++++++++++-- tests/local.mk | 1 + tests/timeout/ptrace-traceme.sh | 79 +++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+), 3 deletions(-) create mode 100755 tests/timeout/ptrace-traceme.sh diff --git a/NEWS b/NEWS index 9ca266031..d890729f4 100644 --- a/NEWS +++ b/NEWS @@ -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', diff --git a/m4/jm-macros.m4 b/m4/jm-macros.m4 index fb00f9faf..5a4567c65 100644 --- a/m4/jm-macros.m4 +++ b/m4/jm-macros.m4 @@ -67,6 +67,7 @@ AC_DEFUN([coreutils_MACROS], fallocate fchown fchmod + ptrace setgroups sethostname siginterrupt diff --git a/src/timeout.c b/src/timeout.c index bc6e97b18..e2d1ccdca 100644 --- a/src/timeout.c +++ b/src/timeout.c @@ -52,6 +52,9 @@ #if HAVE_PRCTL # include #endif +#if HAVE_PTRACE && (defined __linux__ || defined __ANDROID__) +# include +#endif #include #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) { diff --git a/tests/local.mk b/tests/local.mk index 19a8ece5f..c87ad70ce 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -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 \ diff --git a/tests/timeout/ptrace-traceme.sh b/tests/timeout/ptrace-traceme.sh new file mode 100755 index 000000000..a92fbbfd2 --- /dev/null +++ b/tests/timeout/ptrace-traceme.sh @@ -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 . + +. "${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 +#include + +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