linux-stable/kernel
Oleg Nesterov aa039ddb54 ptrace: make ptrace() fail if the tracee changed its pid unexpectedly
[ Upstream commit dbb5afad10 ]

Suppose we have 2 threads, the group-leader L and a sub-theread T,
both parked in ptrace_stop(). Debugger tries to resume both threads
and does

	ptrace(PTRACE_CONT, T);
	ptrace(PTRACE_CONT, L);

If the sub-thread T execs in between, the 2nd PTRACE_CONT doesn not
resume the old leader L, it resumes the post-exec thread T which was
actually now stopped in PTHREAD_EVENT_EXEC. In this case the
PTHREAD_EVENT_EXEC event is lost, and the tracer can't know that the
tracee changed its pid.

This patch makes ptrace() fail in this case until debugger does wait()
and consumes PTHREAD_EVENT_EXEC which reports old_pid. This affects all
ptrace requests except the "asynchronous" PTRACE_INTERRUPT/KILL.

The patch doesn't add the new PTRACE_ option to not complicate the API,
and I _hope_ this won't cause any noticeable regression:

	- If debugger uses PTRACE_O_TRACEEXEC and the thread did an exec
	  and the tracer does a ptrace request without having consumed
	  the exec event, it's 100% sure that the thread the ptracer
	  thinks it is targeting does not exist anymore, or isn't the
	  same as the one it thinks it is targeting.

	- To some degree this patch adds nothing new. In the scenario
	  above ptrace(L) can fail with -ESRCH if it is called after the
	  execing sub-thread wakes the leader up and before it "steals"
	  the leader's pid.

Test-case:

	#include <stdio.h>
	#include <unistd.h>
	#include <signal.h>
	#include <sys/ptrace.h>
	#include <sys/wait.h>
	#include <errno.h>
	#include <pthread.h>
	#include <assert.h>

	void *tf(void *arg)
	{
		execve("/usr/bin/true", NULL, NULL);
		assert(0);

		return NULL;
	}

	int main(void)
	{
		int leader = fork();
		if (!leader) {
			kill(getpid(), SIGSTOP);

			pthread_t th;
			pthread_create(&th, NULL, tf, NULL);
			for (;;)
				pause();

			return 0;
		}

		waitpid(leader, NULL, WSTOPPED);

		ptrace(PTRACE_SEIZE, leader, 0,
				PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXEC);
		waitpid(leader, NULL, 0);

		ptrace(PTRACE_CONT, leader, 0,0);
		waitpid(leader, NULL, 0);

		int status, thread = waitpid(-1, &status, 0);
		assert(thread > 0 && thread != leader);
		assert(status == 0x80137f);

		ptrace(PTRACE_CONT, thread, 0,0);
		/*
		 * waitid() because waitpid(leader, &status, WNOWAIT) does not
		 * report status. Why ????
		 *
		 * Why WEXITED? because we have another kernel problem connected
		 * to mt-exec.
		 */
		siginfo_t info;
		assert(waitid(P_PID, leader, &info, WSTOPPED|WEXITED|WNOWAIT) == 0);
		assert(info.si_pid == leader && info.si_status == 0x0405);

		/* OK, it sleeps in ptrace(PTRACE_EVENT_EXEC == 0x04) */
		assert(ptrace(PTRACE_CONT, leader, 0,0) == -1);
		assert(errno == ESRCH);

		assert(leader == waitpid(leader, &status, WNOHANG));
		assert(status == 0x04057f);

		assert(ptrace(PTRACE_CONT, leader, 0,0) == 0);

		return 0;
	}

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Reported-by: Simon Marchi <simon.marchi@efficios.com>
Acked-by: "Eric W. Biederman" <ebiederm@xmission.com>
Acked-by: Pedro Alves <palves@redhat.com>
Acked-by: Simon Marchi <simon.marchi@efficios.com>
Acked-by: Jan Kratochvil <jan.kratochvil@redhat.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2021-05-26 11:29:06 +02:00
..
bpf bpf: Check for integer overflow when using roundup_pow_of_two() 2021-02-23 13:59:16 +01:00
configs config: android: enable CONFIG_SECCOMP 2016-10-11 15:06:32 -07:00
debug kdb: Make memory allocations more robust 2021-03-03 17:44:31 +01:00
events perf/core: Fix race in the perf_mmap_close() function 2020-11-18 18:26:31 +01:00
gcov gcov: add support for GCC 10.1 2020-09-23 08:46:14 +02:00
irq genirq: Disable interrupts for force threaded handlers 2021-03-24 10:59:26 +01:00
livepatch
locking futex: Handle early deadlock return correctly 2021-03-30 14:41:42 +02:00
power PM: hibernate: remove the bogus call to get_gendisk() in software_resume() 2020-10-29 09:05:43 +01:00
printk printk: fix deadlock when kernel panic 2021-03-07 11:25:56 +01:00
rcu rcuperf: Fix cleanup path for invalid perf_type strings 2019-05-31 06:48:30 -07:00
sched sched/fair: handle case of task_h_load() returning 0 2020-07-22 09:10:54 +02:00
time kernel, fs: Introduce and use set_restart_fn() and arch_set_restart_data() 2021-03-24 10:59:25 +01:00
trace tracing: Restructure trace_clock_global() to never block 2021-05-22 10:40:21 +02:00
.gitignore
acct.c kernel/acct.c: fix the acct->needcheck check in check_free_space() 2018-01-10 09:29:51 +01:00
async.c kernel/async.c: revert "async: simplify lowest_in_progress()" 2018-02-17 13:21:18 +01:00
audit.c audit: fix a net reference leak in audit_list_rules_send() 2021-04-07 12:05:41 +02:00
audit.h audit: fix a net reference leak in audit_list_rules_send() 2021-04-07 12:05:41 +02:00
audit_fsnotify.c
audit_tree.c
audit_watch.c audit: CONFIG_CHANGE don't log internal bookkeeping as an event 2020-10-01 20:40:07 +02:00
auditfilter.c audit: fix a net reference leak in audit_list_rules_send() 2021-04-07 12:05:41 +02:00
auditsc.c audit: print empty EXECVE args 2019-11-28 18:28:55 +01:00
backtracetest.c
bounds.c kbuild: fix kernel/bounds.c 'W=1' warning 2018-11-13 11:16:57 -08:00
capability.c ptrace: Capture the ptracer's creds not PT_PTRACE_CAP 2017-01-06 10:40:13 +01:00
cgroup.c cgroup: add missing skcd->no_refcnt check in cgroup_sk_clone() 2020-08-21 11:01:56 +02:00
cgroup_freezer.c
cgroup_pids.c cgroup: pids: use atomic64_t for pids->limit 2019-12-21 10:42:02 +01:00
compat.c
configs.c
context_tracking.c
cpu.c kernel/cpu: add arch override for clear_tasks_mm_cpumask() mm handling 2020-12-29 13:44:50 +01:00
cpu_pm.c kernel/cpu_pm: Fix uninitted local in cpu_pm 2020-06-20 10:24:21 +02:00
cpuset.c sched/cpuset/pm: Fix cpuset vs. suspend-resume bugs 2017-10-12 11:51:25 +02:00
crash_dump.c
cred.c memcg: account security cred as well to kmemcg 2020-01-12 11:24:13 +01:00
delayacct.c
dma.c
exec_domain.c
exit.c futex: Mark the begin of futex exit explicitly 2021-02-03 23:19:49 +01:00
extable.c kernel/extable.c: mark core_kernel_text notrace 2017-07-21 07:42:21 +02:00
fork.c futex: Split futex_mm_release() for exit/exec 2021-02-03 23:19:49 +01:00
freezer.c
futex.c Revert 337f13046f ("futex: Allow FUTEX_CLOCK_REALTIME with FUTEX_WAIT op") 2021-05-22 10:40:19 +02:00
groups.c kernel: make groups_sort calling a responsibility group_info allocators 2018-01-10 09:29:52 +01:00
hung_task.c kernel: hung_task.c: disable on suspend 2019-04-20 09:07:52 +02:00
irq_work.c
jump_label.c jump_label: Invoke jump_label_test() via early_initcall() 2017-12-14 09:28:24 +01:00
kallsyms.c
kcmp.c
Kconfig.freezer
Kconfig.hz
Kconfig.locks
Kconfig.preempt
kcov.c kcov: ensure irq code sees a valid area 2018-08-03 07:55:12 +02:00
kexec.c
kexec_core.c objtool, x86: Add several functions and files to the objtool whitelist 2018-06-05 10:28:57 +02:00
kexec_file.c kernel: kexec_file: fix error return code of kexec_calculate_store_digests() 2021-05-22 10:40:31 +02:00
kexec_internal.h
kmod.c usermodehelper: reset umask to default before executing user process 2020-10-14 09:48:14 +02:00
kprobes.c kretprobe: Avoid re-registration of the same kretprobe earlier 2021-02-10 09:09:25 +01:00
ksysfs.c
kthread.c kthread_worker: prevent queuing delayed work from timer_fn when it is being canceled 2020-11-10 10:24:02 +01:00
latencytop.c
Makefile elfcore: fix building with clang 2021-02-10 09:09:25 +01:00
membarrier.c Fix: Disable sys_membarrier when nohz_full is enabled 2017-03-12 06:41:45 +01:00
memremap.c mm, devm_memremap_pages: kill mapping "System RAM" support 2019-01-13 10:03:51 +01:00
module-internal.h
module.c module: Ignore _GLOBAL_OFFSET_TABLE_ when warning for undefined symbols 2021-03-03 17:44:44 +01:00
module_signing.c
notifier.c x86/mm: split vmalloc_sync_all() 2020-04-02 17:20:26 +02:00
nsproxy.c
padata.c padata: purge get_cpu and reorder_via_wq from padata_do_serial 2020-05-27 16:41:53 +02:00
panic.c panic: ensure preemption is disabled during panic() 2019-10-17 13:42:25 -07:00
params.c
pid.c pidns: disable pid allocation if pid_ns_prepare_proc() is failed in alloc_pid() 2018-04-13 19:47:53 +02:00
pid_namespace.c signal/pid_namespace: Fix reboot_pid_ns to use send_sig not force_sig 2019-08-04 09:33:16 +02:00
profile.c
ptrace.c ptrace: make ptrace() fail if the tracee changed its pid unexpectedly 2021-05-26 11:29:06 +02:00
range.c
reboot.c reboot: fix overflow parsing reboot cpu number 2020-11-18 18:26:32 +01:00
relay.c kernel/relay.c: fix memleak on destroy relay channel 2020-08-26 10:29:03 +02:00
resource.c resource: fix integer overflow at reallocation 2018-04-24 09:34:09 +02:00
seccomp.c seccomp: Add missing return in non-void function 2021-03-03 17:44:43 +01:00
signal.c signal: Extend exec_id to 64bits 2020-04-24 07:58:54 +02:00
smp.c cpu/hotplug: Fix SMT supported evaluation 2018-08-15 18:14:53 +02:00
smpboot.c kthread/smpboot: do not park in kthread_create_on_cpu() 2016-10-11 15:06:33 -07:00
smpboot.h
softirq.c Mark HI and TASKLET softirq synchronous 2018-08-15 18:14:42 +02:00
stacktrace.c stacktrace, lockdep: Fix address, newline ugliness 2017-02-14 15:25:42 -08:00
stop_machine.c stop_machine: Use raw spinlocks 2018-08-03 07:55:24 +02:00
sys.c kernel/sys.c: avoid copying possible padding bytes in copy_to_user 2020-10-01 20:40:04 +02:00
sys_ni.c x86/pkeys: Fix pkeys build breakage for some non-x86 arches 2016-09-13 14:41:36 +02:00
sysctl.c sched/rt: Show the 'sched_rr_timeslice' SCHED_RR timeslice tuning knob in milliseconds 2020-07-09 09:35:55 +02:00
sysctl_binary.c
task_work.c
taskstats.c taskstats: fix data-race 2020-01-12 11:24:12 +01:00
test_kprobes.c
torture.c
tracepoint.c tracepoint: Do not fail unregistering a probe due to memory failure 2021-03-03 17:44:38 +01:00
tsacct.c
ucount.c kernel/ucount.c: mark user_header with kmemleak_ignore() 2017-06-17 06:41:51 +02:00
uid16.c kernel: make groups_sort calling a responsibility group_info allocators 2018-01-10 09:29:52 +01:00
up.c
user-return-notifier.c
user.c
user_namespace.c userns: move user access out of the mutex 2018-09-09 20:01:24 +02:00
utsname.c Merge branch 'nsfs-ioctls' into HEAD 2016-09-22 20:00:36 -05:00
utsname_sysctl.c sys: don't hold uts_sem while accessing userspace memory 2018-09-09 20:01:24 +02:00
watchdog.c kernel/watchdog: prevent false hardlockup on overloaded system 2017-06-17 06:41:57 +02:00
watchdog_hld.c kernel/watchdog: prevent false hardlockup on overloaded system 2017-06-17 06:41:57 +02:00
workqueue.c workqueue: Move the position of debug_work_activate() in __queue_work() 2021-04-16 11:59:08 +02:00
workqueue_internal.h workqueue: Fix NULL pointer dereference 2017-11-15 15:53:17 +01:00