Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1688174pxv; Fri, 25 Jun 2021 20:44:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwnPPmTRwEJJo7bPu+YH5gEMK2rdxb9V0KZC40M5I/8IwFsjCBcUE3OKB41ThXr2hOYPLYD X-Received: by 2002:a92:c88d:: with SMTP id w13mr9642362ilo.170.1624679051907; Fri, 25 Jun 2021 20:44:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624679051; cv=none; d=google.com; s=arc-20160816; b=AgSvIGmcR5Dky6ANhUSOHltWvZ9Ry10I6NzNB+CiWGYLQiDvve5IMsjdj+vXRcnVXX y8I48YB+ADD+M+TCNc5V5wJjb/JJp5gDLu0ibVtU0wJVQuazmkSw+sAWdY2nnUfESic1 IzRE3LHxUM/3aXJ90LCoxFKxSlM+ADyMoFtbaXC4FjuEohcLMZgS+QDdnGuxg2tBhFJo WzSPbt2OZcAlFiLoXTutoXZ/0ylzhxBnB+ScuBo+tEscS6/1S5+AAsbsh80qCZlUrJrY tB1lYBkq2FgT0xozKqH07SluHSsi+iCqzHPsfr46T1OK9Sli9bEpZpB1zjiX3X53kvz6 ufiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=DEu/zUGEil5W1i542Jddkvc9pkER3JRysmBpbGQkYys=; b=RSKHpu6CDaFhnAuk015RnCMi8qV4ugRllqmxoqotSLfTFoq8ecXX3WzCXM8SsERdU3 bFd9YPnlJRH71eIeo+lVTDhZbUUlTM4xCtXK0Hez7pKm/AAP/hrUWlQAkEVyzJNaklGh UzSMEELSWizEJr6hdShEGo1K6odVIVvGOypAqDqcvunCXGFp/FpzlxnhZob2Ze9+swZB C+oi4g3N5aB24V8FQ5JP2O7LfOWjN9P1HZZrl3bJFu2T1lva+aaTmiThWRLUSI/AwSwn QXxDMnohhQVINxrwiC+62L6tDK5Bcn8bNiTjddJhHYqGnSlH/STE6KzpPzpuyY1GH9Na aRsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="h/QRUEAV"; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n3si8636545ioh.98.2021.06.25.20.43.59; Fri, 25 Jun 2021 20:44:11 -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=@chromium.org header.s=google header.b="h/QRUEAV"; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230002AbhFZDoc (ORCPT + 99 others); Fri, 25 Jun 2021 23:44:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229906AbhFZDob (ORCPT ); Fri, 25 Jun 2021 23:44:31 -0400 Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3DA2C061574 for ; Fri, 25 Jun 2021 20:42:08 -0700 (PDT) Received: by mail-pg1-x534.google.com with SMTP id y14so9849881pgs.12 for ; Fri, 25 Jun 2021 20:42:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=DEu/zUGEil5W1i542Jddkvc9pkER3JRysmBpbGQkYys=; b=h/QRUEAVNrUKdOz23aXgH2qZADlVHaJeFs2MojeAXLKLDp3e1LOG9MjMdVwDqHGe3B YxK6GS2Dy3kNvMeygQwfdJ9jhAyjbJcbRBbtkjoa1UtJjxJw3AoZVAV1d2dS1+yv32o4 7HJpXFhj83H1JRRR9SNpp6vFWF7KQea3GpBv8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=DEu/zUGEil5W1i542Jddkvc9pkER3JRysmBpbGQkYys=; b=ERZbc6a0amdeJjUdY/S28EmXd9IQ5eGxhKdXmXhIhiI/VAcg6YNR2JsjDMFEkUmybG g9lqCP66/SpilwgzReFB4NEdab6wvlhyRBGcYJukydmym2MBVyjV0hOT6GA9xgAFZll+ wWmyGdHKg1CwN0CaVhe3i8eJ7hlPWW/XiMIRNni5RBUIxHQeXr/GWpAK39nxdlmfdC6d I+xlRt+xMoRROjgughmtHaT/ciBfZflqKKgLZw8k2TRzp8kLGhMvCjcpc7OKjTL5n5Qh llqU+kneMOrJjTxExXrgVFiXd1zbp8QgJ89timUdQZ/3l+8zz3/3D5P5Z4IkoCZNEx1t sfyw== X-Gm-Message-State: AOAM533z/KUiug34t6MwyxJd7eQwbG/ZT8fpDh4XSiA5Qa8kQ4rZSxAA SAOdJ+7Otw99EYTuUr13pXy+ZA== X-Received: by 2002:a62:6406:0:b029:302:bb9:1511 with SMTP id y6-20020a6264060000b02903020bb91511mr13668259pfb.26.1624678928256; Fri, 25 Jun 2021 20:42:08 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id j12sm6494689pjw.52.2021.06.25.20.42.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Jun 2021 20:42:07 -0700 (PDT) Date: Fri, 25 Jun 2021 20:42:06 -0700 From: Kees Cook To: "Eric W. Biederman" Cc: Linus Torvalds , Al Viro , Michael Schmitz , linux-arch , Jens Axboe , Oleg Nesterov , Linux Kernel Mailing List , Richard Henderson , Ivan Kokshaysky , Matt Turner , alpha , Geert Uytterhoeven , linux-m68k , Arnd Bergmann , Tejun Heo Subject: Re: [PATCH 6/9] signal: Fold do_group_exit into get_signal fixing io_uring threads Message-ID: <202106252038.389B963B6F@keescook> References: <87a6njf0ia.fsf@disp2133> <87tulpbp19.fsf@disp2133> <87zgvgabw1.fsf@disp2133> <875yy3850g.fsf_-_@disp2133> <874kdn6q87.fsf_-_@disp2133> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <874kdn6q87.fsf_-_@disp2133> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 24, 2021 at 02:02:16PM -0500, Eric W. Biederman wrote: > > Forld do_group_exit into get_signal as it is the last caller. > > Move the group_exit logic above the PF_IO_WORKER exit, ensuring > that if an PF_IO_WORKER catches SIGKILL every thread in > the thread group will exit not just the the PF_IO_WORKER. > > Now that the information is easily available only set PF_SIGNALED > when it was a signal that caused the exit. > > Signed-off-by: "Eric W. Biederman" > --- > include/linux/sched/task.h | 1 - > kernel/exit.c | 31 ------------------------------- > kernel/signal.c | 35 +++++++++++++++++++++++++---------- > 3 files changed, 25 insertions(+), 42 deletions(-) > > diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h > index ef02be869cf2..45525512e3d0 100644 > --- a/include/linux/sched/task.h > +++ b/include/linux/sched/task.h > @@ -77,7 +77,6 @@ static inline void exit_thread(struct task_struct *tsk) > { > } > #endif > -extern void do_group_exit(int); > > extern void exit_files(struct task_struct *); > extern void exit_itimers(struct signal_struct *); > diff --git a/kernel/exit.c b/kernel/exit.c > index 921519d80b56..635f434122b7 100644 > --- a/kernel/exit.c > +++ b/kernel/exit.c > @@ -892,37 +892,6 @@ SYSCALL_DEFINE1(exit, int, error_code) > do_exit((error_code&0xff)<<8); > } > > -/* > - * Take down every thread in the group. This is called by fatal signals > - * as well as by sys_exit_group (below). > - */ > -void > -do_group_exit(int exit_code) > -{ > - struct signal_struct *sig = current->signal; > - > - BUG_ON(exit_code & 0x80); /* core dumps don't get here */ > - > - if (signal_group_exit(sig)) > - exit_code = sig->group_exit_code; > - else if (!thread_group_empty(current)) { > - struct sighand_struct *const sighand = current->sighand; > - > - spin_lock_irq(&sighand->siglock); > - if (signal_group_exit(sig)) > - /* Another thread got here before we took the lock. */ > - exit_code = sig->group_exit_code; > - else { > - sig->group_exit_code = exit_code; > - sig->flags = SIGNAL_GROUP_EXIT; > - zap_other_threads(current); Oh, now I see it: the "new code" in start_group_exit() is an open-coded zap_other_threads()? That wasn't clear to me, but makes sense now. > - } > - spin_unlock_irq(&sighand->siglock); > - } > - > - do_exit(exit_code); > - /* NOTREACHED */ > -} > > /* > * this kills every thread in the thread group. Note that any externally > diff --git a/kernel/signal.c b/kernel/signal.c > index c79c010ca5f3..95a076af600a 100644 > --- a/kernel/signal.c > +++ b/kernel/signal.c > @@ -2646,6 +2646,7 @@ bool get_signal(struct ksignal *ksig) > { > struct sighand_struct *sighand = current->sighand; > struct signal_struct *signal = current->signal; > + int exit_code; > int signr; > > if (unlikely(current->task_works)) > @@ -2848,8 +2849,6 @@ bool get_signal(struct ksignal *ksig) > /* > * Anything else is fatal, maybe with a core dump. > */ > - current->flags |= PF_SIGNALED; > - > if (sig_kernel_coredump(signr)) { > if (print_fatal_signals) > print_fatal_signal(ksig->info.si_signo); > @@ -2857,14 +2856,33 @@ bool get_signal(struct ksignal *ksig) > /* > * If it was able to dump core, this kills all > * other threads in the group and synchronizes with > - * their demise. If we lost the race with another > - * thread getting here, it set group_exit_code > - * first and our do_group_exit call below will use > - * that value and ignore the one we pass it. > + * their demise. If another thread makes it > + * to do_coredump first, it will set group_exit_code > + * which will be passed to do_exit. > */ > do_coredump(&ksig->info); > } > > + /* > + * Death signals, no core dump. > + */ > + exit_code = signr; > + if (signal_group_exit(signal)) { > + exit_code = signal->group_exit_code; > + } else { > + spin_lock_irq(&sighand->siglock); > + if (signal_group_exit(signal)) { > + /* Another thread got here before we took the lock. */ > + exit_code = signal->group_exit_code; > + } else { > + start_group_exit_locked(signal, exit_code); And here's the "if we didn't already do start_group_exit(), do it here". And that state is entirely captured via the SIGNAL_GROUP_EXIT flag. Cool. > + } > + spin_unlock_irq(&sighand->siglock); > + } > + > + if (exit_code & 0x7f) > + current->flags |= PF_SIGNALED; > + > /* > * PF_IO_WORKER threads will catch and exit on fatal signals > * themselves. They have cleanup that must be performed, so > @@ -2873,10 +2891,7 @@ bool get_signal(struct ksignal *ksig) > if (current->flags & PF_IO_WORKER) > goto out; > > - /* > - * Death signals, no core dump. > - */ > - do_group_exit(ksig->info.si_signo); > + do_exit(exit_code); > /* NOTREACHED */ > } > spin_unlock_irq(&sighand->siglock); > -- > 2.20.1 > -- Kees Cook