Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3700215pxj; Tue, 11 May 2021 09:58:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzPoRzntmewJ6sFxKdSFR9Dl9if7ZrXNECzZldQm568XI5313zcAn9dE+P2X4o9HuYM+Eh9 X-Received: by 2002:a17:907:990f:: with SMTP id ka15mr25531334ejc.132.1620752318307; Tue, 11 May 2021 09:58:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620752318; cv=none; d=google.com; s=arc-20160816; b=gWjIQJN0+bpygY+mgTAN3x9s6a4wcTJXcZAgMx7khz1kASxDlq+P+8dRdH7XED1ynp 8JtlK6tmT6NdRyTBBtwm4VqSM22Kzx+AiwjLCUbV/onYbUC/pInJxE91USYQrBzZEPX/ X3oIFE3bEQx3g+MyEWqr6E/IUoTkKv1kqkdk66K6hBiefLYhK0Cd5tzap7MOWd+K5dRE II0dnAV42O8FZUU4Uyt6NWe0OUgGY8O3sLDk1oSNcOJ0TlKrZ080lVhTMDtvr+aZJvN8 NZ5tb9C611lM4xaB3vxjUs6kvcq3iM8kYh0KJ4j5/A8yIO38l2nT3kmKjVJyBzpfMxUV YmgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:content-disposition:mime-version :message-id:subject:cc:to:from:date:dkim-signature; bh=+Hqn4DDA9smOSapKsY68DCe6CLiWVB/h93X9znormXA=; b=Hz9H1P1sdgZZ/BCaHsul95GP4KfbZchXC8TKCqZOdzg+y9zYcnl3YwMxjBQCJzLt9L ZE1HcGGfro22dZGSumheYD+vo8onGAwJW3yR8hd5QoPu8XnBe1wCyCbYnwfwgTdXl07c JUhvSYpVH4DuR9FO6+2ys/TNRph0CrKj+LPTR2jp9lUhh0NhpGWnWEzCKjYH6UaD0SPo mf0C48Ght/iDY6tiePDvNq6xlfGbRg5BUVtwYrIOvd7krmjwaqoeP4y7u/+cIivPOI/t W9S24aeObiEhMmGuWoKCGCQaeazo/EQJhLYsbQHyfuqNYG9Aew6aLsxHfbd93j0vuG/8 2U9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=B05OGYVc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bo12si2655753ejb.317.2021.05.11.09.58.13; Tue, 11 May 2021 09:58:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=B05OGYVc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231407AbhEKQ6C (ORCPT + 99 others); Tue, 11 May 2021 12:58:02 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35998 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230315AbhEKQ6B (ORCPT ); Tue, 11 May 2021 12:58:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620752214; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=+Hqn4DDA9smOSapKsY68DCe6CLiWVB/h93X9znormXA=; b=B05OGYVcFQoGI7aX4RxmYjEuhllgk+1KXkHHsvcS5RfUh+Vx9SEnv1FPaCkiWGNU3OHrC0 IrCMzwhPeTd9iCCG1ec8u2B/5TPSK/cwA+VnqdxJiWjNgsTuUj6MVybS7fB+DXbdSS1CAk VhbZHWU9Ed9WzkBQpIh0SKk7BZzHLRI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-546-b1WzxQLeOhOwEhx5W93XLA-1; Tue, 11 May 2021 12:56:52 -0400 X-MC-Unique: b1WzxQLeOhOwEhx5W93XLA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 85133800D55; Tue, 11 May 2021 16:56:51 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.40.195.185]) by smtp.corp.redhat.com (Postfix) with SMTP id 02BED1893C; Tue, 11 May 2021 16:56:28 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Tue, 11 May 2021 18:56:50 +0200 (CEST) Date: Tue, 11 May 2021 18:56:26 +0200 From: Oleg Nesterov To: Andrew Morton Cc: "Eric W. Biederman" , Eugene Syromiatnikov , Jan Kratochvil , Linus Torvalds , Mathieu Desnoyers , Michael Kerrisk , Pedro Alves , Simon Marchi , linux-kernel@vger.kernel.org Subject: [PATCH RESEND2] ptrace: make ptrace() fail if the tracee changed its pid unexpectedly Message-ID: <20210511165626.GA13720@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 #include #include #include #include #include #include #include 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 Reported-by: Simon Marchi Acked-by: "Eric W. Biederman" Acked-by: Pedro Alves Acked-by: Simon Marchi Acked-by: Jan Kratochvil --- kernel/ptrace.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/kernel/ptrace.c b/kernel/ptrace.c index 43d6179508d6..1037251ae4a5 100644 --- a/kernel/ptrace.c +++ b/kernel/ptrace.c @@ -169,6 +169,27 @@ void __ptrace_unlink(struct task_struct *child) spin_unlock(&child->sighand->siglock); } +static bool looks_like_a_spurious_pid(struct task_struct *task) +{ + int pid; + + if (task->exit_code != ((PTRACE_EVENT_EXEC << 8) | SIGTRAP)) + return false; + + rcu_read_lock(); + pid = task_pid_nr_ns(task, task_active_pid_ns(task->parent)); + rcu_read_unlock(); + + if (pid == task->ptrace_message) + return false; + /* + * The tracee changed its pid but the PTRACE_EVENT_EXEC event + * was not wait()'ed, most probably debugger targets the old + * leader which was destroyed in de_thread(). + */ + return true; +} + /* Ensure that nothing can wake it up, even SIGKILL */ static bool ptrace_freeze_traced(struct task_struct *task) { @@ -179,7 +200,8 @@ static bool ptrace_freeze_traced(struct task_struct *task) return ret; spin_lock_irq(&task->sighand->siglock); - if (task_is_traced(task) && !__fatal_signal_pending(task)) { + if (task_is_traced(task) && !looks_like_a_spurious_pid(task) && + !__fatal_signal_pending(task)) { task->state = __TASK_TRACED; ret = true; } -- 2.25.1.362.g51ebf55