Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3523632pxj; Mon, 24 May 2021 08:36:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRpENZwaFQANUiMs0GSqbEDkgHdKtsNFjyGdQSXnM7gXkxcQzzlCtEzDLH+ZP+7E+rRnRF X-Received: by 2002:a02:83c2:: with SMTP id j2mr25094169jah.6.1621870613687; Mon, 24 May 2021 08:36:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621870613; cv=none; d=google.com; s=arc-20160816; b=Rvhk2IU4XW0S/LV99g6Y0YQOGl7FxiozQvkmFrzCiKVJYLVKiEl2qQAfqyziKmZsUg kf63vib24wVQMPhFSS5YjxxBoH6PPe6HeXvsCW90ht3XmD+lfDgaum9Om2B0GfkyvtAI /HRM9Z8e86IVo0ruFyfPSB+oHv3FA0YCH/pKBFZ+yN/qGfIQMK1W3pyWTa2iYON6WIHP cAaQu6UrCecJ+jdL6rGsMPyXdrSkIfx9B7x6sEr8PobwHXWNyjamzfK2aZLvH9LLY/wr McQA3L3VCIqneW6fRsX0Faax3DctWPIeAeeI09XKyjFI8iEZYi5Hunso4cZmtuk2Mr/O U73w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HjE1k9LOV7gxDgeBe/he+NNT/twzBCLVz1evlEj56Xo=; b=GKnnziDhVCOcg1X7+0BbGr4mRACzhI4CE3PUnIi2usoM7J8XdjLO+SuPi+dDd6Zjln v4AITa7zY9MDLxM27ukD25CyGpTvKtKeiaHFKaSOvtKXp5lbkb75ojWdX24TnWd3K5ZA seyKcdBeyXNTkuai2H0qT0/LenfDVPvWlgnH+kn0qCoqsfNLouzMYGIj64tHRS/t7kEl 1bvFO2LiCWS8j8RKyAKRoSqpT5dd2dO5JBuXo9MC3L9RVtbShn56WvPMaDbyOALTcbgU Kb9hx1QcnAGVL06X1tO3xGd6bBz6GZlHFk2M7XD4hcVUyqkkeNN5pOkIrRL4xICz6s/F cvBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jDJoqPsz; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z18si5698476ilm.35.2021.05.24.08.36.40; Mon, 24 May 2021 08:36:53 -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=@linuxfoundation.org header.s=korg header.b=jDJoqPsz; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232614AbhEXPhZ (ORCPT + 99 others); Mon, 24 May 2021 11:37:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:50500 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233883AbhEXPec (ORCPT ); Mon, 24 May 2021 11:34:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 68E4E613F6; Mon, 24 May 2021 15:31:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621870309; bh=BHmX+3MSs8DrwrmdsxqilJN75gfvTJiFcyAeYjNUK7s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jDJoqPszxVtGJmTnEfiF3mzHlYgfohCIEUJ/7G8Ojy/3Nr1yUYNV4OAJI4sxeG9UO nL/z68QmwbN90ftFoLku72TVtQQKnneBnjSZpdppcVYHAmT17rnEHNxncShjq3Hd3f 7Kl++6cP2q5e9RmGHOdZjp6aWZ9F/e6iiadlWxRU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Oleg Nesterov , Simon Marchi , "Eric W. Biederman" , Pedro Alves , Jan Kratochvil , Linus Torvalds , Sasha Levin Subject: [PATCH 4.9 04/36] ptrace: make ptrace() fail if the tracee changed its pid unexpectedly Date: Mon, 24 May 2021 17:24:49 +0200 Message-Id: <20210524152324.308628167@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210524152324.158146731@linuxfoundation.org> References: <20210524152324.158146731@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Oleg Nesterov [ Upstream commit dbb5afad100a828c97e012c6106566d99f041db6 ] 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 Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- kernel/ptrace.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/kernel/ptrace.c b/kernel/ptrace.c index ea3370e205fb..4f10223bc7b0 100644 --- a/kernel/ptrace.c +++ b/kernel/ptrace.c @@ -159,6 +159,21 @@ void __ptrace_unlink(struct task_struct *child) spin_unlock(&child->sighand->siglock); } +static bool looks_like_a_spurious_pid(struct task_struct *task) +{ + if (task->exit_code != ((PTRACE_EVENT_EXEC << 8) | SIGTRAP)) + return false; + + if (task_pid_vnr(task) == 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) { @@ -169,7 +184,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.30.2