Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp6025742pxb; Mon, 14 Feb 2022 13:24:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJxfIlbZoAfXfAUUZmJYrxhV2+WIMVF6byYEIX2ybKjDXL2ATYI9vXsggZYw7hFbhJKtuw6q X-Received: by 2002:a17:90a:e7c6:b0:1b9:1cc6:8c60 with SMTP id kb6-20020a17090ae7c600b001b91cc68c60mr741489pjb.64.1644873869928; Mon, 14 Feb 2022 13:24:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644873869; cv=none; d=google.com; s=arc-20160816; b=RIrIYSa0O4C0h7+WQZW+32wkbRFOu/E0E4kBhKCPItnjnmaB3c0i12FASvKPf3X+pE HwvY7WydcgiSBBbEzMN2M8M23V+Adp+1LAORoSAVDs2Ek7JPafoiWmIvX94yodaZEy/v y6BbEleJOD/4Shgf6uPP9h9TAoBIWFeN7cR6rfBriYyMXRROn/anGOPpNTpX6zpB+zs6 jY7Pa+4Z2tcjZV9JJbNm9L7vOHHqY+/GJL7Cm+TCggP7OaA7oaFF/yJg+wVchdzPPoCN RibnoJq75FWLqzUf7d1pNZSwKSjFRBygazn9r42dsiYpkQ+WOJ95x0CvA3GaH4WUosIT u2Kg== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=DF9WT/Vv+v/rFZpH2c7Do8/144JbYcaXb42/q6Y+A0U=; b=ax5afNtZXJ2+A3jmZbBSxYxMmQvQe8ZCILGMMBjapv/lEdokUgPeH8mUYIGf46z81A GAauV5o6hR2lojTOaDNus8a0NVstrSr6Cgnx8UF7sbKe8JN5s4IzRS5BY+1JwuyAtHh9 nZ+XkVka/58tI2wiuNtlWvVufCjEwPV63Wfrsgzm/kbRjMjhxMlkETEVxZrX/orupZTo fx6AbtAQX4f/tz1j7S6J0pblPhU7Qpj0gLhEFCAxDfKjSbtjtfglCVE0Xo93ELBN/q5O lUqCD7Ibuzuasz4atNHN/rMUoqnEvPu+eyvImVTt6BDv6PgOwUdDNDbZZVfKgaPoVnao 6geg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=QIyQthQl; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id m7si797372pgn.860.2022.02.14.13.24.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Feb 2022 13:24:29 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=QIyQthQl; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8F4E31B893C; Mon, 14 Feb 2022 12:41:55 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229538AbiBNUXB (ORCPT + 99 others); Mon, 14 Feb 2022 15:23:01 -0500 Received: from gmail-smtp-in.l.google.com ([23.128.96.19]:53562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229446AbiBNUXA (ORCPT ); Mon, 14 Feb 2022 15:23:00 -0500 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39F931E6822 for ; Mon, 14 Feb 2022 12:21:20 -0800 (PST) Received: by mail-pf1-x433.google.com with SMTP id d187so31131036pfa.10 for ; Mon, 14 Feb 2022 12:21:20 -0800 (PST) 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:content-transfer-encoding:in-reply-to; bh=DF9WT/Vv+v/rFZpH2c7Do8/144JbYcaXb42/q6Y+A0U=; b=QIyQthQljzLDrV1j/8Di/lSTC4O9i7AJ4h+RyA0Gbr4rcktlIyiBLn6GTX8uKH6LcT jHb/KGlZVkhdQXIFz3E2u6cgJcFB9mYMV6IHktSAy+pSDAun9LkQRlfDlpWDNdv8isxg OKtqc7zE/vmR6+ggIAD2GVAjrM2eJ1Mjo6GLo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=DF9WT/Vv+v/rFZpH2c7Do8/144JbYcaXb42/q6Y+A0U=; b=rLFidTy6tKJ2aN4QjuHBrTGfISAw161eaTMJLtO4I8dgFqMTmcCuzkaM/hLTd5VhgA xwr6UssqlyJwjAcBk4dG8BogpgQIeQPG9jvJmKBMqaPkrqgnvPfF6mc5k8qZHArHOiuk 0FhQnCNeIe5T5MnW8lzeyJoe+hFnY/fCrfMOOYg/9TyCcD1VL+WTcTZYqWY1pq0Qm4zr Gt0ZMBcrZ0R7Rjom1QIYYii7la3z79z9i6qkY3JwLpHQW2hpVGdpAM+sedrqVHZT/hgi wWBWEXeVzQ7JZn3K7MijE0f7Wd8y+T1JHigty5mWB3yVx8f0AOhsNApY4A+G57j0vDuj 8yJA== X-Gm-Message-State: AOAM533d5WGAfk737MKdwEpL6YQoeiKQmOgUnZQoka8lLPDa/1Bmhepn T9/rEWiee/WC8ge7+hCFGE2j7U+z2tlo9Q== X-Received: by 2002:a17:903:2281:: with SMTP id b1mr686528plh.158.1644869246223; Mon, 14 Feb 2022 12:07:26 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id lw16sm9718884pjb.51.2022.02.14.12.07.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Feb 2022 12:07:25 -0800 (PST) Date: Mon, 14 Feb 2022 12:07:25 -0800 From: Kees Cook To: Christian Brauner Cc: Andy Lutomirski , Robert =?utf-8?B?xZp3acSZY2tp?= , "Eric W. Biederman" , Jann Horn , Will Drewry , linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [RFC] Get siginfo from unreaped task Message-ID: <202202141152.6296CD7F@keescook> References: <20220213085212.cwzuqlrabpgbnbac@wittgenstein> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20220213085212.cwzuqlrabpgbnbac@wittgenstein> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Feb 13, 2022 at 09:52:12AM +0100, Christian Brauner wrote: > On Sat, Feb 12, 2022 at 06:32:08PM -0800, Andy Lutomirski wrote: > > > > > On Feb 12, 2022, at 3:24 AM, Robert Święcki wrote: > > > > > > sob., 12 lut 2022 o 05:28 Kees Cook napisał(a): > > >> > > >> Make siginfo available through PTRACE_GETSIGINFO after process death, > > >> without needing to have already used PTRACE_ATTACH. Uses 48 more bytes > > >> in task_struct, though I bet there might be somewhere else we could > > >> stash a copy of it? > > > > > > An alternative way of accessing this info could be abusing the > > > waitid() interface, with some additional, custom to Linux, flag > > > > > > waitid(P_ALL, 0, &si, __WCHILDSIGINFO); > > > > > > which would change what is put into si. > > > > > > But maybe ptrace() is better, because it's mostly incompatible with > > > other OSes anyway on the behavior/flag level, while waitd() seems to > > > be POSIX/BSD standard, even if Linux specifies some additional flags. > > > > > > > > > > I had a kind of opposite thought, which is that it would be very nice > > to be able to get all the waitid() data without reaping a process or > > even necessarily being its parent. Maybe these can be combined? A > > new waitid() option like you’re suggesting could add siginfo (and > > might need permissions). And we could have a different waitid() flag > > that says “maybe not my child, don’t reap” (and also needs > > permissions). > > > > Although the “don’t reap” thing is fundamentally racy. What a sane > > process manager actually wants is an interface to read all this info > > from a pidfd, which means it all needs to get stuck in struct pid. And > > /me briefly pops out from vacation > > Agreed and not just siginfo I would expect(?). We already came to that > conclusion when we first introduced them. > > > task_struct needs a completion or wait queue so you can actually wait > > for a pidfd to exit (unless someone already did this — I had patches a > > while back). And this would be awesome. > > Currently, you can wait for a pidfd to exit via polling and you can use > a pidfd to pass it to waitid(P_PIDFD, pidfd, ...). > > /me pops back into vacation Right, so waitid already has all the infrastructure for this, so I think adding it there makes a lot of sense. Here's what I've got: diff --git a/include/linux/sched.h b/include/linux/sched.h index f5b2be39a78c..e40789e801ef 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1178,6 +1178,7 @@ struct task_struct { #endif /* Ptrace state: */ unsigned long ptrace_message; + kernel_siginfo_t death_siginfo; kernel_siginfo_t *last_siginfo; struct task_io_accounting ioac; diff --git a/kernel/signal.c b/kernel/signal.c index 9b04631acde8..41f6ba6b7aa7 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -2825,6 +2825,10 @@ bool get_signal(struct ksignal *ksig) } fatal: + /* Allow siginfo to be queried until reaped. */ + copy_siginfo(¤t->death_siginfo, &ksig->info); + current->last_siginfo = ¤t->death_siginfo; + spin_unlock_irq(&sighand->siglock); if (unlikely(cgroup_task_frozen(current))) cgroup_leave_frozen(true); diff --git a/include/uapi/linux/wait.h b/include/uapi/linux/wait.h index 85b809fc9f11..7258cd4510ba 100644 --- a/include/uapi/linux/wait.h +++ b/include/uapi/linux/wait.h @@ -9,6 +9,7 @@ #define WCONTINUED 0x00000008 #define WNOWAIT 0x01000000 /* Don't reap, just poll status. */ +#define __WCHILDSIGINFO 0x10000000 /* Report child's siginfo. */ #define __WNOTHREAD 0x20000000 /* Don't wait on children of other threads in this group */ #define __WALL 0x40000000 /* Wait on all children, regardless of type */ #define __WCLONE 0x80000000 /* Wait only on non-SIGCHLD children */ diff --git a/kernel/exit.c b/kernel/exit.c index d54efddd378b..70ecb996cecd 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -953,6 +953,7 @@ struct waitid_info { uid_t uid; int status; int cause; + kernel_siginfo_t siginfo; }; struct wait_opts { @@ -964,7 +965,7 @@ struct wait_opts { int wo_stat; struct rusage *wo_rusage; - wait_queue_entry_t child_wait; + wait_queue_entry_t child_wait; int notask_error; }; @@ -1012,11 +1013,16 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p) int state, status; pid_t pid = task_pid_vnr(p); uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p)); - struct waitid_info *infop; + struct waitid_info *infop = wo->wo_info; if (!likely(wo->wo_flags & WEXITED)) return 0; + /* Before WNOWAIT so a copy can be extracted without reaping. */ + if (unlikely(wo->wo_flags & __WCHILDSIGINFO)) { + if (infop && p->last_siginfo) + copy_siginfo(&infop->siginfo, p->last_siginfo); + } if (unlikely(wo->wo_flags & WNOWAIT)) { status = (p->signal->flags & SIGNAL_GROUP_EXIT) ? p->signal->group_exit_code : p->exit_code; @@ -1121,7 +1127,6 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p) release_task(p); out_info: - infop = wo->wo_info; if (infop) { if ((status & 0x7f) == 0) { infop->cause = CLD_EXITED; @@ -1564,7 +1569,7 @@ static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop, unsigned int f_flags = 0; if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED| - __WNOTHREAD|__WCLONE|__WALL)) + __WNOTHREAD|__WCLONE|__WALL|__WCHILDSIGINFO)) return -EINVAL; if (!(options & (WEXITED|WSTOPPED|WCONTINUED))) return -EINVAL; @@ -1638,6 +1645,10 @@ SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *, if (!infop) return err; + /* __WCHILDSIGINFO */ + if (info->siginfo.signo) + return copy_siginfo_to_user(infop, &info->siginfo); + if (!user_write_access_begin(infop, sizeof(*infop))) return -EFAULT; @@ -1781,6 +1792,12 @@ COMPAT_SYSCALL_DEFINE5(waitid, if (!infop) return err; + /* __WCHILDSIGINFO */ + if (info->siginfo.signo) + return copy_siginfo_to_user32( + (struct compat_siginfo __user *)infop, + &info->siginfo); + if (!user_write_access_begin(infop, sizeof(*infop))) return -EFAULT; One usability question I have is: - if the process just exited normally, should it return an empty siginfo, or should it ignore __WCHILDSIGINFO? (I have it ignoring it above.) -- Kees Cook