From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.3 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_PASS,USER_AGENT_MUTT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4862EC46475 for ; Tue, 23 Oct 2018 09:24:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 09EAF20665 for ; Tue, 23 Oct 2018 09:24:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 09EAF20665 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728582AbeJWRqc (ORCPT ); Tue, 23 Oct 2018 13:46:32 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40522 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727714AbeJWRqc (ORCPT ); Tue, 23 Oct 2018 13:46:32 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8B10A3084259; Tue, 23 Oct 2018 09:23:58 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.43.17.106]) by smtp.corp.redhat.com (Postfix) with SMTP id 8EF1D60BF6; Tue, 23 Oct 2018 09:23:49 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Tue, 23 Oct 2018 11:23:58 +0200 (CEST) Date: Tue, 23 Oct 2018 11:23:48 +0200 From: Oleg Nesterov To: Enke Chen Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Peter Zijlstra , Arnd Bergmann , "Eric W. Biederman" , Khalid Aziz , Kate Stewart , Helge Deller , Greg Kroah-Hartman , Al Viro , Andrew Morton , Christian Brauner , Catalin Marinas , Will Deacon , Dave Martin , Mauro Carvalho Chehab , Michal Hocko , Rik van Riel , "Kirill A. Shutemov" , Roman Gushchin , Marcos Paulo de Souza , Dominik Brodowski , Cyrill Gorcunov , Yang Shi , Jann Horn , Kees Cook , x86@kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, "Victor Kamensky (kamensky)" , xe-linux-external@cisco.com, Stefan Strogin Subject: Re: [PATCH v2] kernel/signal: Signal-based pre-coredump notification Message-ID: <20181023092348.GA14340@redhat.com> References: <458c04d8-d189-4a26-729a-bb1d1d751534@cisco.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <458c04d8-d189-4a26-729a-bb1d1d751534@cisco.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Tue, 23 Oct 2018 09:23:59 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/22, Enke Chen wrote: > > As the coredump of a process may take time, in certain time-sensitive > applications it is necessary for a parent process (e.g., a process > manager) to be notified of a child's imminent death before the coredump > so that the parent process can act sooner, such as re-spawning an > application process, or initiating a control-plane fail-over. Personally I still do not like this feature, but I won't argue. > --- a/fs/coredump.c > +++ b/fs/coredump.c > @@ -546,6 +546,7 @@ void do_coredump(const kernel_siginfo_t *siginfo) > struct cred *cred; > int retval = 0; > int ispipe; > + bool notify; > struct files_struct *displaced; > /* require nonrelative corefile path and be extra careful */ > bool need_suid_safe = false; > @@ -590,6 +591,15 @@ void do_coredump(const kernel_siginfo_t *siginfo) > if (retval < 0) > goto fail_creds; > > + /* > + * Send the pre-coredump signal to the parent if requested. > + */ > + read_lock(&tasklist_lock); > + notify = do_notify_parent_predump(current); > + read_unlock(&tasklist_lock); > + if (notify) > + cond_resched(); Hmm. I do not understand why do we need cond_resched(). And even if we need it, why we can't call it unconditionally? I'd also suggest to move read_lock/unlock(tasklist) into do_notify_parent_predump() and remove the "task_struct *tsk" argument, tsk is always current. Yes, do_notify_parent() and do_notify_parent_cldstop() are called with tasklist_lock held, but there are good reasons for that. > +static inline int valid_predump_signal(int sig) > +{ > + return (sig == SIGCHLD) || (sig == SIGUSR1) || (sig == SIGUSR2); > +} I still do not understand why do we need to restrict predump_signal. PR_SET_PREDUMP_SIG can only change the caller's ->predump_signal, so to me even PR_SET_PREDUMP_SIG(SIGKILL) is fine. And once again, SIGCHLD/SIGUSR do not queue, this means that PR_SET_PREDUMP_SIG is pointless if you have 2 or more children. > +bool do_notify_parent_predump(struct task_struct *tsk) > +{ > + struct sighand_struct *sighand; > + struct kernel_siginfo info; > + struct task_struct *parent; > + unsigned long flags; > + pid_t pid; > + int sig; > + > + parent = tsk->parent; > + sighand = parent->sighand; > + pid = task_tgid_vnr(tsk); > + > + spin_lock_irqsave(&sighand->siglock, flags); > + sig = parent->signal->predump_signal; > + if (!valid_predump_signal(sig)) { > + spin_unlock_irqrestore(&sighand->siglock, flags); > + return false; > + } Why do we need to check parent->signal->predump_signal under ->siglock? This complicates the code for no reason, afaics. > + clear_siginfo(&info); > + info.si_pid = pid; > + info.si_signo = sig; > + if (sig == SIGCHLD) > + info.si_code = CLD_PREDUMP; > + > + __group_send_sig_info(sig, &info, parent); > + __wake_up_parent(tsk, parent); Why __wake_up_parent() ? do_notify_parent() does this to wake up the parent sleeping in do_wait(), to report the event. But predump_signal has nothing to do with wait(). Now. This version sends the signal to ->parent, not ->real_parent. OK, but this means that real_parent won't be notified if its child is traced. > + case PR_SET_PREDUMP_SIG: > + if (arg3 || arg4 || arg5) > + return -EINVAL; > + > + /* 0 is valid for disabling the feature */ > + if (arg2 && !valid_predump_signal((int)arg2)) > + return -EINVAL; > + me->signal->predump_signal = (int)arg2; > + break; Again, I do not understand why do we need valid_predump_signal(). But even if we need it, I don't understand why should we check it twice. IOW, why do_notify_parent_predump() can't simply check ->predump_signal != 0? Whatever we do, PR_SET_PREDUMP_SIG should validate arg2 anyway. Who else can change ->predump_signal after that? > + case PR_GET_PREDUMP_SIG: > + if (arg3 || arg4 || arg5) > + return -EINVAL; > + error = put_user(me->signal->predump_signal, > + (int __user *)arg2); To me it would be better to simply return ->predump_signal, iow error = me->signal->predump_signal; break; but I won't insist, this is subjective and cosmetic. Oleg. From mboxrd@z Thu Jan 1 00:00:00 1970 From: Oleg Nesterov Subject: Re: [PATCH v2] kernel/signal: Signal-based pre-coredump notification Date: Tue, 23 Oct 2018 11:23:48 +0200 Message-ID: <20181023092348.GA14340@redhat.com> References: <458c04d8-d189-4a26-729a-bb1d1d751534@cisco.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Content-Disposition: inline In-Reply-To: <458c04d8-d189-4a26-729a-bb1d1d751534@cisco.com> Sender: linux-kernel-owner@vger.kernel.org To: Enke Chen Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Peter Zijlstra , Arnd Bergmann , "Eric W. Biederman" , Khalid Aziz , Kate Stewart , Helge Deller , Greg Kroah-Hartman , Al Viro , Andrew Morton , Christian Brauner , Catalin Marinas , Will Deacon , Dave Martin , Mauro Carvalho Chehab , Michal Hocko , Rik van Riel List-Id: linux-arch.vger.kernel.org On 10/22, Enke Chen wrote: > > As the coredump of a process may take time, in certain time-sensitive > applications it is necessary for a parent process (e.g., a process > manager) to be notified of a child's imminent death before the coredump > so that the parent process can act sooner, such as re-spawning an > application process, or initiating a control-plane fail-over. Personally I still do not like this feature, but I won't argue. > --- a/fs/coredump.c > +++ b/fs/coredump.c > @@ -546,6 +546,7 @@ void do_coredump(const kernel_siginfo_t *siginfo) > struct cred *cred; > int retval = 0; > int ispipe; > + bool notify; > struct files_struct *displaced; > /* require nonrelative corefile path and be extra careful */ > bool need_suid_safe = false; > @@ -590,6 +591,15 @@ void do_coredump(const kernel_siginfo_t *siginfo) > if (retval < 0) > goto fail_creds; > > + /* > + * Send the pre-coredump signal to the parent if requested. > + */ > + read_lock(&tasklist_lock); > + notify = do_notify_parent_predump(current); > + read_unlock(&tasklist_lock); > + if (notify) > + cond_resched(); Hmm. I do not understand why do we need cond_resched(). And even if we need it, why we can't call it unconditionally? I'd also suggest to move read_lock/unlock(tasklist) into do_notify_parent_predump() and remove the "task_struct *tsk" argument, tsk is always current. Yes, do_notify_parent() and do_notify_parent_cldstop() are called with tasklist_lock held, but there are good reasons for that. > +static inline int valid_predump_signal(int sig) > +{ > + return (sig == SIGCHLD) || (sig == SIGUSR1) || (sig == SIGUSR2); > +} I still do not understand why do we need to restrict predump_signal. PR_SET_PREDUMP_SIG can only change the caller's ->predump_signal, so to me even PR_SET_PREDUMP_SIG(SIGKILL) is fine. And once again, SIGCHLD/SIGUSR do not queue, this means that PR_SET_PREDUMP_SIG is pointless if you have 2 or more children. > +bool do_notify_parent_predump(struct task_struct *tsk) > +{ > + struct sighand_struct *sighand; > + struct kernel_siginfo info; > + struct task_struct *parent; > + unsigned long flags; > + pid_t pid; > + int sig; > + > + parent = tsk->parent; > + sighand = parent->sighand; > + pid = task_tgid_vnr(tsk); > + > + spin_lock_irqsave(&sighand->siglock, flags); > + sig = parent->signal->predump_signal; > + if (!valid_predump_signal(sig)) { > + spin_unlock_irqrestore(&sighand->siglock, flags); > + return false; > + } Why do we need to check parent->signal->predump_signal under ->siglock? This complicates the code for no reason, afaics. > + clear_siginfo(&info); > + info.si_pid = pid; > + info.si_signo = sig; > + if (sig == SIGCHLD) > + info.si_code = CLD_PREDUMP; > + > + __group_send_sig_info(sig, &info, parent); > + __wake_up_parent(tsk, parent); Why __wake_up_parent() ? do_notify_parent() does this to wake up the parent sleeping in do_wait(), to report the event. But predump_signal has nothing to do with wait(). Now. This version sends the signal to ->parent, not ->real_parent. OK, but this means that real_parent won't be notified if its child is traced. > + case PR_SET_PREDUMP_SIG: > + if (arg3 || arg4 || arg5) > + return -EINVAL; > + > + /* 0 is valid for disabling the feature */ > + if (arg2 && !valid_predump_signal((int)arg2)) > + return -EINVAL; > + me->signal->predump_signal = (int)arg2; > + break; Again, I do not understand why do we need valid_predump_signal(). But even if we need it, I don't understand why should we check it twice. IOW, why do_notify_parent_predump() can't simply check ->predump_signal != 0? Whatever we do, PR_SET_PREDUMP_SIG should validate arg2 anyway. Who else can change ->predump_signal after that? > + case PR_GET_PREDUMP_SIG: > + if (arg3 || arg4 || arg5) > + return -EINVAL; > + error = put_user(me->signal->predump_signal, > + (int __user *)arg2); To me it would be better to simply return ->predump_signal, iow error = me->signal->predump_signal; break; but I won't insist, this is subjective and cosmetic. Oleg.