Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp2957260iob; Sun, 1 May 2022 02:42:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyX1dZi4F8Ot01dHsfmQw6fyeGt4mxjRQ6+NUR0Y52tyD1GT3fVs0nOY7j1KqTha3SMMEAq X-Received: by 2002:a17:902:e74a:b0:15e:9811:11da with SMTP id p10-20020a170902e74a00b0015e981111damr3296260plf.130.1651398131729; Sun, 01 May 2022 02:42:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651398131; cv=none; d=google.com; s=arc-20160816; b=Rb5kNVoyoE1RnZA8U3LZZrRxUb2E7m4W9Uu/SRPE9AM59BRQH0OQI7mdNtkO6G5/B9 F8/8JblhcfRTTOo+cL9fVPmLAPs12g5BozBbrVZMIxb3PCI+FRfJ2Ixau1kRcEa3h8yo +CqV4OIKM3U9dxGDvYLjmijEFe45sET8MiH3tHCsmpUZNkrwbX5LCWP+seEmcta7uIMl X3Bii6EpJDiYsb6M3M4x2xt+sjKS7+7Jxk9Dt6g3qucIzeCkXKL8kBLHtmoUnPATyp7f TzcPLUQt6iK4NCNTw3xJ/rTnZLq1KCENyNklaURnaQRBfS5Ins/OddyDjEk+PBT4z9jg yjkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:cc:to:from; bh=mlOUzK00UFdUD7rhZmErTxCQL/kRc3smfP5Vpy1MOlI=; b=y2E3V7+gT5TyYXt9JrRqJVt08Q61bODVcxRTybDhcmepNkXqyc6nsY8enB7m7rSPOE 7SpsoM7Y5IWWQySabJw0Gw95GLGzuwPlN7R/YdrpMT4q3HSlmjhu3aCvfIVkndQe7naB g38OOV40+EyQ+WKxxmmHIv5uTZrWO/fI7vaEY7JiD5iFzFo6cMcDlE4XehWEPKy24y2B O5AM0B8rBT01t+m/FN3bX2Mqr34IOhQkZXNbj5FLvq7lw/4H8pNCYUEdRK+WTKtq2yML mDytI926mHCSdLTqC/uoKDs8kqTRNGhdr7Ofr7Ch+vnqeoA2baJchotNLB3VYFegB+v7 7Bow== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id be9-20020a170902aa0900b00153b2d165ecsi10792537plb.500.2022.05.01.02.41.57; Sun, 01 May 2022 02:42:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1381264AbiD2WKr (ORCPT + 99 others); Fri, 29 Apr 2022 18:10:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1381167AbiD2WKo (ORCPT ); Fri, 29 Apr 2022 18:10:44 -0400 Received: from out02.mta.xmission.com (out02.mta.xmission.com [166.70.13.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 144B5DBD0E; Fri, 29 Apr 2022 15:07:25 -0700 (PDT) Received: from in02.mta.xmission.com ([166.70.13.52]:55002) by out02.mta.xmission.com with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1nkYUT-00AIMf-B9; Fri, 29 Apr 2022 15:49:29 -0600 Received: from ip68-227-174-4.om.om.cox.net ([68.227.174.4]:36464 helo=localhost.localdomain) by in02.mta.xmission.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1nkYUS-007RIp-6h; Fri, 29 Apr 2022 15:49:28 -0600 From: "Eric W. Biederman" To: linux-kernel@vger.kernel.org Cc: rjw@rjwysocki.net, Oleg Nesterov , mingo@kernel.org, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, mgorman@suse.de, bigeasy@linutronix.de, Will Deacon , tj@kernel.org, linux-pm@vger.kernel.org, Peter Zijlstra , Richard Weinberger , Anton Ivanov , Johannes Berg , linux-um@lists.infradead.org, Chris Zankel , Max Filippov , linux-xtensa@linux-xtensa.org, Kees Cook , Jann Horn , linux-ia64@vger.kernel.org, "Eric W. Biederman" Date: Fri, 29 Apr 2022 16:48:36 -0500 Message-Id: <20220429214837.386518-11-ebiederm@xmission.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <87k0b7v9yk.fsf_-_@email.froward.int.ebiederm.org> References: <87k0b7v9yk.fsf_-_@email.froward.int.ebiederm.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-XM-SPF: eid=1nkYUS-007RIp-6h;;;mid=<20220429214837.386518-11-ebiederm@xmission.com>;;;hst=in02.mta.xmission.com;;;ip=68.227.174.4;;;frm=ebiederm@xmission.com;;;spf=softfail X-XM-AID: U2FsdGVkX18Rvz6pahoBBVqRiRjYubwQ90GavkWSLh0= X-SA-Exim-Connect-IP: 68.227.174.4 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Virus: No X-Spam-DCC: XMission; sa01 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ****;linux-kernel@vger.kernel.org X-Spam-Relay-Country: X-Spam-Timing: total 525 ms - load_scoreonly_sql: 0.04 (0.0%), signal_user_changed: 4.8 (0.9%), b_tie_ro: 3.3 (0.6%), parse: 1.35 (0.3%), extract_message_metadata: 12 (2.2%), get_uri_detail_list: 3.3 (0.6%), tests_pri_-1000: 11 (2.1%), tests_pri_-950: 0.98 (0.2%), tests_pri_-900: 0.82 (0.2%), tests_pri_-90: 112 (21.4%), check_bayes: 111 (21.1%), b_tokenize: 8 (1.6%), b_tok_get_all: 7 (1.3%), b_comp_prob: 2.1 (0.4%), b_tok_touch_all: 90 (17.2%), b_finish: 0.81 (0.2%), tests_pri_0: 371 (70.7%), check_dkim_signature: 0.43 (0.1%), check_dkim_adsp: 1.76 (0.3%), poll_dns_idle: 0.45 (0.1%), tests_pri_10: 1.79 (0.3%), tests_pri_500: 6 (1.1%), rewrite_mail: 0.00 (0.0%) Subject: [PATCH v2 11/12] ptrace: Always call schedule in ptrace_stop X-SA-Exim-Version: 4.2.1 (built Sat, 08 Feb 2020 21:53:50 +0000) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Stop testing for !current->ptrace and setting __state to TASK_RUNNING. The code in __ptrace_unlink wakes up the child with ptrace_signal_wake_up which will set __state to TASK_RUNNING. This leaves the only thing ptrace_stop needs to do is to send the signals. Make the signals sending conditional upon current->ptrace so that the correct signals are sent to the parent. After that call schedule and let the fact that __state == TASK_RUNNING keep the code from sleeping in schedule. Now that it is easy to see that ptrace_stop always sleeps in ptrace_stop after ptrace_freeze_trace succeeds modify ptrace_check_attach to warn if wait_task_inactive fails. Signed-off-by: "Eric W. Biederman" --- kernel/ptrace.c | 14 +++------- kernel/signal.c | 68 ++++++++++++++++++------------------------------- 2 files changed, 28 insertions(+), 54 deletions(-) diff --git a/kernel/ptrace.c b/kernel/ptrace.c index d80222251f60..c1afebd2e8f3 100644 --- a/kernel/ptrace.c +++ b/kernel/ptrace.c @@ -261,17 +261,9 @@ static int ptrace_check_attach(struct task_struct *child, bool ignore_state) } read_unlock(&tasklist_lock); - if (!ret && !ignore_state) { - if (!wait_task_inactive(child, __TASK_TRACED)) { - /* - * This can only happen if may_ptrace_stop() fails and - * ptrace_stop() changes ->state back to TASK_RUNNING, - * so we should not worry about leaking __TASK_TRACED. - */ - WARN_ON(READ_ONCE(child->__state) == __TASK_TRACED); - ret = -ESRCH; - } - } + if (!ret && !ignore_state && + WARN_ON_ONCE(!wait_task_inactive(child, __TASK_TRACED))) + ret = -ESRCH; return ret; } diff --git a/kernel/signal.c b/kernel/signal.c index 7cb27a27290a..4cae3f47f664 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -2255,51 +2255,33 @@ static int ptrace_stop(int exit_code, int why, int clear_code, spin_unlock_irq(¤t->sighand->siglock); read_lock(&tasklist_lock); - if (likely(current->ptrace)) { - /* - * Notify parents of the stop. - * - * While ptraced, there are two parents - the ptracer and - * the real_parent of the group_leader. The ptracer should - * know about every stop while the real parent is only - * interested in the completion of group stop. The states - * for the two don't interact with each other. Notify - * separately unless they're gonna be duplicates. - */ + /* + * Notify parents of the stop. + * + * While ptraced, there are two parents - the ptracer and + * the real_parent of the group_leader. The ptracer should + * know about every stop while the real parent is only + * interested in the completion of group stop. The states + * for the two don't interact with each other. Notify + * separately unless they're gonna be duplicates. + */ + if (current->ptrace) do_notify_parent_cldstop(current, true, why); - if (gstop_done && ptrace_reparented(current)) - do_notify_parent_cldstop(current, false, why); - - /* - * Don't want to allow preemption here, because - * sys_ptrace() needs this task to be inactive. - * - * XXX: implement read_unlock_no_resched(). - */ - preempt_disable(); - read_unlock(&tasklist_lock); - cgroup_enter_frozen(); - preempt_enable_no_resched(); - freezable_schedule(); - cgroup_leave_frozen(true); - } else { - /* - * By the time we got the lock, our tracer went away. - * Don't drop the lock yet, another tracer may come. - * - * If @gstop_done, the ptracer went away between group stop - * completion and here. During detach, it would have set - * JOBCTL_STOP_PENDING on us and we'll re-enter - * TASK_STOPPED in do_signal_stop() on return, so notifying - * the real parent of the group stop completion is enough. - */ - if (gstop_done) - do_notify_parent_cldstop(current, false, why); + if (gstop_done && (!current->ptrace || ptrace_reparented(current))) + do_notify_parent_cldstop(current, false, why); - /* tasklist protects us from ptrace_freeze_traced() */ - __set_current_state(TASK_RUNNING); - read_unlock(&tasklist_lock); - } + /* + * Don't want to allow preemption here, because + * sys_ptrace() needs this task to be inactive. + * + * XXX: implement read_unlock_no_resched(). + */ + preempt_disable(); + read_unlock(&tasklist_lock); + cgroup_enter_frozen(); + preempt_enable_no_resched(); + freezable_schedule(); + cgroup_leave_frozen(true); /* * We are back. Now reacquire the siglock before touching -- 2.35.3