From nobody Wed Dec 17 08:51:12 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A0BCF25A34D; Mon, 12 May 2025 08:55:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040146; cv=none; b=bkMvH92GsIEZEO4W3Hpjh5bUh7ukCuB2HYKjUAG+ViaaLEVwbp0YpEvJE6zqhoNnXpJeLSk/9kOOxDmIHSdmrMdErHLlOvlxNLui5+ag6cM1WfrqY23TPv5WOrA8omv08cHA1Kj1F+5T/dLU5rKjeFSYKC00H7GzTmlHdgSBjiI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040146; c=relaxed/simple; bh=yJIcK7PPInODHuXyqw2gaMqbfBwseUsiKi2NQv8EWpM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nzF5a3WHibmxS9DNQMWtag1M3tvvN8A5W3dFYFBNbGbduxV86w1eBybXQV8dMMrzFPDkTZHI3QJUgXD64lHszQRSCJ9byEkJSe34sjzGL+xe2QvWsg8nSKTav8VsL90O13/viuS8I5Msz5Gag7njEH7p4j1pAwmaMQzmZv/6djQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H0oz9ObL; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="H0oz9ObL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57293C4CEE7; Mon, 12 May 2025 08:55:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747040146; bh=yJIcK7PPInODHuXyqw2gaMqbfBwseUsiKi2NQv8EWpM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=H0oz9ObL2kS0AO80QLGxNM7xbwWt+0/inC4hiEznjLbKcGKWVwCRWmipBEdWji43E we+xdJML2MyUT51+4eZ8ZeXmmKKgD5+jkVQkUMja9fDNdKenPqtA5a6IoPmCAIAOXV gnFzcHZ33I2Zxe7jyzp5u/lxJsiTKvgon1r4INajnZH13vRE1n3RDihqmE55HFx531 8/ZQLxkjwwvJ4fXtjB9r9kEH83ZPPbYi+8FrgzSt8kV6GBqnm6jTOy/XgvRxrDENAp VTcdwSiG9N4nWeDfLh+v6jeBDfrVDynizs7YtA9976mTszEs/Ta1pUhB+x3vnXpD7H AsCa8KfqKN5bQ== From: Christian Brauner Date: Mon, 12 May 2025 10:55:20 +0200 Subject: [PATCH v6 1/9] coredump: massage format_corname() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250512-work-coredump-socket-v6-1-c51bc3450727@kernel.org> References: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> In-Reply-To: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> To: linux-fsdevel@vger.kernel.org, Jann Horn , Daniel Borkmann , Kuniyuki Iwashima Cc: Eric Dumazet , Oleg Nesterov , "David S. Miller" , Alexander Viro , Daan De Meyer , David Rheinsberg , Jakub Kicinski , Jan Kara , Lennart Poettering , Luca Boccassi , Mike Yuan , Paolo Abeni , Simon Horman , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-security-module@vger.kernel.org, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=4653; i=brauner@kernel.org; h=from:subject:message-id; bh=yJIcK7PPInODHuXyqw2gaMqbfBwseUsiKi2NQv8EWpM=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWQobm9X7446Z1KYIWGw+LJpU7hm6bMTu/a3XrrSciHKv K/s/O3OjlIWBjEuBlkxRRaHdpNwueU8FZuNMjVg5rAygQxh4OIUgIl8/8rwh7cj/9HWx+8mJ6tX THJfaff6vpL649nMNnvMJMrsz7/TPMjIsK80hkX+fjwHt9z67w8rrzfXn1seP6146Xbdn+s2/vH sZAEA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 We're going to extend the coredump code in follow-up patches. Clean it up so we can do this more easily. Signed-off-by: Christian Brauner --- fs/coredump.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index d740a0411266..368751d98781 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -76,9 +76,15 @@ static char core_pattern[CORENAME_MAX_SIZE] =3D "core"; static int core_name_size =3D CORENAME_MAX_SIZE; unsigned int core_file_note_size_limit =3D CORE_FILE_NOTE_SIZE_DEFAULT; =20 +enum coredump_type_t { + COREDUMP_FILE =3D 1, + COREDUMP_PIPE =3D 2, +}; + struct core_name { char *corename; int used, size; + enum coredump_type_t core_type; }; =20 static int expand_corename(struct core_name *cn, int size) @@ -218,18 +224,21 @@ static int format_corename(struct core_name *cn, stru= ct coredump_params *cprm, { const struct cred *cred =3D current_cred(); const char *pat_ptr =3D core_pattern; - int ispipe =3D (*pat_ptr =3D=3D '|'); bool was_space =3D false; int pid_in_pattern =3D 0; int err =3D 0; =20 cn->used =3D 0; cn->corename =3D NULL; + if (*pat_ptr =3D=3D '|') + cn->core_type =3D COREDUMP_PIPE; + else + cn->core_type =3D COREDUMP_FILE; if (expand_corename(cn, core_name_size)) return -ENOMEM; cn->corename[0] =3D '\0'; =20 - if (ispipe) { + if (cn->core_type =3D=3D COREDUMP_PIPE) { int argvs =3D sizeof(core_pattern) / 2; (*argv) =3D kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL); if (!(*argv)) @@ -247,7 +256,7 @@ static int format_corename(struct core_name *cn, struct= coredump_params *cprm, * Split on spaces before doing template expansion so that * %e and %E don't get split if they have spaces in them */ - if (ispipe) { + if (cn->core_type =3D=3D COREDUMP_PIPE) { if (isspace(*pat_ptr)) { if (cn->used !=3D 0) was_space =3D true; @@ -353,7 +362,7 @@ static int format_corename(struct core_name *cn, struct= coredump_params *cprm, * Installing a pidfd only makes sense if * we actually spawn a usermode helper. */ - if (!ispipe) + if (cn->core_type !=3D COREDUMP_PIPE) break; =20 /* @@ -384,12 +393,12 @@ static int format_corename(struct core_name *cn, stru= ct coredump_params *cprm, * If core_pattern does not include a %p (as is the default) * and core_uses_pid is set, then .%pid will be appended to * the filename. Do not do this for piped commands. */ - if (!ispipe && !pid_in_pattern && core_uses_pid) { + if (!(cn->core_type =3D=3D COREDUMP_PIPE) && !pid_in_pattern && core_uses= _pid) { err =3D cn_printf(cn, ".%d", task_tgid_vnr(current)); if (err) return err; } - return ispipe; + return 0; } =20 static int zap_process(struct signal_struct *signal, int exit_code) @@ -583,7 +592,6 @@ void do_coredump(const kernel_siginfo_t *siginfo) const struct cred *old_cred; struct cred *cred; int retval =3D 0; - int ispipe; size_t *argv =3D NULL; int argc =3D 0; /* require nonrelative corefile path and be extra careful */ @@ -632,19 +640,18 @@ void do_coredump(const kernel_siginfo_t *siginfo) =20 old_cred =3D override_creds(cred); =20 - ispipe =3D format_corename(&cn, &cprm, &argv, &argc); + retval =3D format_corename(&cn, &cprm, &argv, &argc); + if (retval < 0) { + coredump_report_failure("format_corename failed, aborting core"); + goto fail_unlock; + } =20 - if (ispipe) { + if (cn.core_type =3D=3D COREDUMP_PIPE) { int argi; int dump_count; char **helper_argv; struct subprocess_info *sub_info; =20 - if (ispipe < 0) { - coredump_report_failure("format_corename failed, aborting core"); - goto fail_unlock; - } - if (cprm.limit =3D=3D 1) { /* See umh_coredump_setup() which sets RLIMIT_CORE =3D 1. * @@ -695,7 +702,7 @@ void do_coredump(const kernel_siginfo_t *siginfo) coredump_report_failure("|%s pipe failed", cn.corename); goto close_fail; } - } else { + } else if (cn.core_type =3D=3D COREDUMP_FILE) { struct mnt_idmap *idmap; struct inode *inode; int open_flags =3D O_CREAT | O_WRONLY | O_NOFOLLOW | @@ -823,13 +830,13 @@ void do_coredump(const kernel_siginfo_t *siginfo) file_end_write(cprm.file); free_vma_snapshot(&cprm); } - if (ispipe && core_pipe_limit) + if ((cn.core_type =3D=3D COREDUMP_PIPE) && core_pipe_limit) wait_for_dump_helpers(cprm.file); close_fail: if (cprm.file) filp_close(cprm.file, NULL); fail_dropcount: - if (ispipe) + if (cn.core_type =3D=3D COREDUMP_PIPE) atomic_dec(&core_dump_count); fail_unlock: kfree(argv); --=20 2.47.2 From nobody Wed Dec 17 08:51:12 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E500C25A2C1; Mon, 12 May 2025 08:55:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040152; cv=none; b=lWIbJhA5C7qIhxp4cd/mEQOfXQw8aTOLlOwLm8dtt8Ks/GCo49cdHE8mpIAGl81HRlVm4ElTDhp8Q9eSJ0wIo8uxZ2QhqZdR11/1xp9I/OfexJdZpxYXwVMCqluC3sp4wiSJPiHeQH4WLf6M23OPSjXrNmCP/KqX0miI9bTm/lM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040152; c=relaxed/simple; bh=A1ET0rfOsbnb/BsO8UCsIl2X+TJza69sCPkeWIG0Ss8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gEdhHuDO2PY6N1wFRa/8RqcVOwACluTnLdkUro38SRAc7LBekzPQ+JHxbmiCutgyZ5/IujsSsBCl9hjnOIBg4AXPPX4w7eDmIhjl+ak0V0bj1E9eAenWYW7qH7KxNdpIjKalvBJRjT208nB5EIXA6cpl9RHTeZVpEWTRlJmX+9Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=I3i81WPB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="I3i81WPB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 99426C4CEEF; Mon, 12 May 2025 08:55:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747040151; bh=A1ET0rfOsbnb/BsO8UCsIl2X+TJza69sCPkeWIG0Ss8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=I3i81WPB1UdV+yufEcuRKKWxMH5xySmzTOXqB9537sr0Z91xLH5+rR0Ynd6KzDTLd xGgdHflc4rNJfZQGoUgb4MFHTTEaJM7ISrGTu5V3S/91uFxXbninrvbJoMwQrbaNqL 1/WQdV7eAtiyuKGSrqsqdzKGfGM93gyT1PvXIe0KdZ4NHdJ92Y87OYq5zEx98NPVKt +aBZkUpAaiz8QNQgb7KOq1zhzf/Fdy/0e1m+gZNKloTybvstTeaVgwMw0IOidbTrSc 5yTORGpwc3SV5XgyT4UfYnTSgRlLltWdQNRHJDdIni2LIkHWBjD2YXIZhcZZCU0LAn bG6gtFYD9Mjog== From: Christian Brauner Date: Mon, 12 May 2025 10:55:21 +0200 Subject: [PATCH v6 2/9] coredump: massage do_coredump() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250512-work-coredump-socket-v6-2-c51bc3450727@kernel.org> References: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> In-Reply-To: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> To: linux-fsdevel@vger.kernel.org, Jann Horn , Daniel Borkmann , Kuniyuki Iwashima Cc: Eric Dumazet , Oleg Nesterov , "David S. Miller" , Alexander Viro , Daan De Meyer , David Rheinsberg , Jakub Kicinski , Jan Kara , Lennart Poettering , Luca Boccassi , Mike Yuan , Paolo Abeni , Simon Horman , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-security-module@vger.kernel.org, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=4911; i=brauner@kernel.org; h=from:subject:message-id; bh=A1ET0rfOsbnb/BsO8UCsIl2X+TJza69sCPkeWIG0Ss8=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWQobm+/oHjVWKjHJqy2b6ovj8ek+gK2mjzFgqSjG6/aN TAKrEjsKGVhEONikBVTZHFoNwmXW85TsdkoUwNmDisTyBAGLk4BmIi3BiPDMb5e00I+rqpKfmMv MbldE56Zn1/u8bHxprlMclae4XReoAqdpezzWhkfLqpTsWn/8/NqDxOfTGD+lqOtHY/yTsamsQM A X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 We're going to extend the coredump code in follow-up patches. Clean it up so we can do this more easily. Signed-off-by: Christian Brauner --- fs/coredump.c | 122 +++++++++++++++++++++++++++++++-----------------------= ---- 1 file changed, 65 insertions(+), 57 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index 368751d98781..0e97c21b35e3 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -646,63 +646,8 @@ void do_coredump(const kernel_siginfo_t *siginfo) goto fail_unlock; } =20 - if (cn.core_type =3D=3D COREDUMP_PIPE) { - int argi; - int dump_count; - char **helper_argv; - struct subprocess_info *sub_info; - - if (cprm.limit =3D=3D 1) { - /* See umh_coredump_setup() which sets RLIMIT_CORE =3D 1. - * - * Normally core limits are irrelevant to pipes, since - * we're not writing to the file system, but we use - * cprm.limit of 1 here as a special value, this is a - * consistent way to catch recursive crashes. - * We can still crash if the core_pattern binary sets - * RLIM_CORE =3D !1, but it runs as root, and can do - * lots of stupid things. - * - * Note that we use task_tgid_vnr here to grab the pid - * of the process group leader. That way we get the - * right pid if a thread in a multi-threaded - * core_pattern process dies. - */ - coredump_report_failure("RLIMIT_CORE is set to 1, aborting core"); - goto fail_unlock; - } - cprm.limit =3D RLIM_INFINITY; - - dump_count =3D atomic_inc_return(&core_dump_count); - if (core_pipe_limit && (core_pipe_limit < dump_count)) { - coredump_report_failure("over core_pipe_limit, skipping core dump"); - goto fail_dropcount; - } - - helper_argv =3D kmalloc_array(argc + 1, sizeof(*helper_argv), - GFP_KERNEL); - if (!helper_argv) { - coredump_report_failure("%s failed to allocate memory", __func__); - goto fail_dropcount; - } - for (argi =3D 0; argi < argc; argi++) - helper_argv[argi] =3D cn.corename + argv[argi]; - helper_argv[argi] =3D NULL; - - retval =3D -ENOMEM; - sub_info =3D call_usermodehelper_setup(helper_argv[0], - helper_argv, NULL, GFP_KERNEL, - umh_coredump_setup, NULL, &cprm); - if (sub_info) - retval =3D call_usermodehelper_exec(sub_info, - UMH_WAIT_EXEC); - - kfree(helper_argv); - if (retval) { - coredump_report_failure("|%s pipe failed", cn.corename); - goto close_fail; - } - } else if (cn.core_type =3D=3D COREDUMP_FILE) { + switch (cn.core_type) { + case COREDUMP_FILE: { struct mnt_idmap *idmap; struct inode *inode; int open_flags =3D O_CREAT | O_WRONLY | O_NOFOLLOW | @@ -796,6 +741,69 @@ void do_coredump(const kernel_siginfo_t *siginfo) if (do_truncate(idmap, cprm.file->f_path.dentry, 0, 0, cprm.file)) goto close_fail; + break; + } + case COREDUMP_PIPE: { + int argi; + int dump_count; + char **helper_argv; + struct subprocess_info *sub_info; + + if (cprm.limit =3D=3D 1) { + /* See umh_coredump_setup() which sets RLIMIT_CORE =3D 1. + * + * Normally core limits are irrelevant to pipes, since + * we're not writing to the file system, but we use + * cprm.limit of 1 here as a special value, this is a + * consistent way to catch recursive crashes. + * We can still crash if the core_pattern binary sets + * RLIM_CORE =3D !1, but it runs as root, and can do + * lots of stupid things. + * + * Note that we use task_tgid_vnr here to grab the pid + * of the process group leader. That way we get the + * right pid if a thread in a multi-threaded + * core_pattern process dies. + */ + coredump_report_failure("RLIMIT_CORE is set to 1, aborting core"); + goto fail_unlock; + } + cprm.limit =3D RLIM_INFINITY; + + dump_count =3D atomic_inc_return(&core_dump_count); + if (core_pipe_limit && (core_pipe_limit < dump_count)) { + coredump_report_failure("over core_pipe_limit, skipping core dump"); + goto fail_dropcount; + } + + helper_argv =3D kmalloc_array(argc + 1, sizeof(*helper_argv), + GFP_KERNEL); + if (!helper_argv) { + coredump_report_failure("%s failed to allocate memory", __func__); + goto fail_dropcount; + } + for (argi =3D 0; argi < argc; argi++) + helper_argv[argi] =3D cn.corename + argv[argi]; + helper_argv[argi] =3D NULL; + + retval =3D -ENOMEM; + sub_info =3D call_usermodehelper_setup(helper_argv[0], + helper_argv, NULL, GFP_KERNEL, + umh_coredump_setup, NULL, &cprm); + if (sub_info) + retval =3D call_usermodehelper_exec(sub_info, + UMH_WAIT_EXEC); + + kfree(helper_argv); + if (retval) { + coredump_report_failure("|%s pipe failed", cn.corename); + goto close_fail; + } + break; + } + default: + WARN_ON_ONCE(true); + goto close_fail; } =20 /* get us an unshared descriptor table; almost always a no-op */ --=20 2.47.2 From nobody Wed Dec 17 08:51:12 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB87725A2C1; Mon, 12 May 2025 08:55:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040157; cv=none; b=ZqiD+NNCjCn9RqMOCFElgPSSXpcI36xD611gndXYOkpFsM5FH52I0KlMsFn0jLQovYc/d815/iqbWzGto9O5N9/VPbbsW+bAFwoFO4eyHzwooXW1zzYc37Z489ScoxNKF0dUg+MRTNDTf3JNbzoSwT5Lap2UFuBDZAlaC6a07sA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040157; c=relaxed/simple; bh=D7Loxo9G/kcLmw4x1TO7tL5CmVqFKJ1HEu4OaNHweVk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NP6qVkOuENQyzPPhkStWKeG8G/GkMBffh5PfRqPJGdA7XMFSetRlgHi21tKa9D13ppDMOEMsNOUu9BsXKjoeSxKA6EDxc3ldSSJFl/BKdBz55XR8iQ9MWHzkDKl95L5lgBQX0pCDqy+h3fKByc3Oh7UIm2phlgrVOlwMFRG1kUA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YK+wIdZd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="YK+wIdZd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D0E04C4CEE7; Mon, 12 May 2025 08:55:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747040156; bh=D7Loxo9G/kcLmw4x1TO7tL5CmVqFKJ1HEu4OaNHweVk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=YK+wIdZdMuVMOaTh9U0gR8on6/ypY/ssKHSyL4HuyjAxNFrlOXEOlPLjtwsrg6IYe 1LvSSGTWABx4pPmSKXSo+5TtvEWPeCTmP3Y4mgxlIT2y3EXD+UuXNEU3lNz7pdE/DP 0MWaLjPyU1ftFYoXx2J8aeDmVDLTuZhvdCgnLxMwGYMH/72Zir65KTjuic+XjY4VmI XMMwrHRseITsG8UOq6ETZW84jRB+9TOL/4qilxnI3c/vrqgzC38bB9+wjGPXsC1v9d c2nRsmTePAbtVxjNZADIzYXQsbogB38/MZM7XebW5gO06jDdRCiEwhj/BncYjToeTe A5o955z6+3oBQ== From: Christian Brauner Date: Mon, 12 May 2025 10:55:22 +0200 Subject: [PATCH v6 3/9] coredump: reflow dump helpers a little Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250512-work-coredump-socket-v6-3-c51bc3450727@kernel.org> References: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> In-Reply-To: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> To: linux-fsdevel@vger.kernel.org, Jann Horn , Daniel Borkmann , Kuniyuki Iwashima Cc: Eric Dumazet , Oleg Nesterov , "David S. Miller" , Alexander Viro , Daan De Meyer , David Rheinsberg , Jakub Kicinski , Jan Kara , Lennart Poettering , Luca Boccassi , Mike Yuan , Paolo Abeni , Simon Horman , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-security-module@vger.kernel.org, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1478; i=brauner@kernel.org; h=from:subject:message-id; bh=D7Loxo9G/kcLmw4x1TO7tL5CmVqFKJ1HEu4OaNHweVk=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWQobm93XnFo595ahifW9jN01j87eMyjSvnoJOHHqjNuM XaVd1Yt6yhlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZiI9SlGhiaRK49S1qna2Z0T UFz4bcurnwuEf7P9e39giZqWyW7uCa4M/1SXTQ1lFX+kb77yzO9Yr4Mb1Nb4vBC/HfiLQ+zzkvb PizkA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 They look rather messy right now. Signed-off-by: Christian Brauner --- fs/coredump.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index 0e97c21b35e3..a70929c3585b 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -867,10 +867,9 @@ static int __dump_emit(struct coredump_params *cprm, c= onst void *addr, int nr) struct file *file =3D cprm->file; loff_t pos =3D file->f_pos; ssize_t n; + if (cprm->written + nr > cprm->limit) return 0; - - if (dump_interrupted()) return 0; n =3D __kernel_write(file, addr, nr, &pos); @@ -887,20 +886,21 @@ static int __dump_skip(struct coredump_params *cprm, = size_t nr) { static char zeroes[PAGE_SIZE]; struct file *file =3D cprm->file; + if (file->f_mode & FMODE_LSEEK) { - if (dump_interrupted() || - vfs_llseek(file, nr, SEEK_CUR) < 0) + if (dump_interrupted() || vfs_llseek(file, nr, SEEK_CUR) < 0) return 0; cprm->pos +=3D nr; return 1; - } else { - while (nr > PAGE_SIZE) { - if (!__dump_emit(cprm, zeroes, PAGE_SIZE)) - return 0; - nr -=3D PAGE_SIZE; - } - return __dump_emit(cprm, zeroes, nr); } + + while (nr > PAGE_SIZE) { + if (!__dump_emit(cprm, zeroes, PAGE_SIZE)) + return 0; + nr -=3D PAGE_SIZE; + } + + return __dump_emit(cprm, zeroes, nr); } =20 int dump_emit(struct coredump_params *cprm, const void *addr, int nr) --=20 2.47.2 From nobody Wed Dec 17 08:51:12 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6643B25A342; Mon, 12 May 2025 08:56:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040162; cv=none; b=dpMJ76uwqhjZbh6PPopK5ukvdZxpcKRQvBEEDHbp9nlms0+mRgVYXn6ICh8ohJp9+ofFi09FCN8jHLnw1Py6JCqrXGCAn6k5WhwESOcfsygwxqD4jGAagS0WpkgmL10+Fr3jTCBmYzJ6Q6Ftm6k0L2fz7fnqzXqBoCd71qd/tUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040162; c=relaxed/simple; bh=ZpqOa39wRBgmaQK3FnarDF0LgWmcV9qjAJ4w9KLR6+4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=U91VSaym8XUdIpaEHkB6r2A8P5hThReLStI1t4dH9FhFukXGuBhp1LgXwUl3Y0z9XI6gfuiTJf6orGeMN1DZRn/S7EndGiiT1PCrX8dDthZYHNpFODwDarj4gOSwhHvgq1hJMToSnla7/1qqS/oy85OUaA2c5BNA9UZHJFnUBf4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=i010sXlk; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="i010sXlk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 06D69C4CEEF; Mon, 12 May 2025 08:55:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747040161; bh=ZpqOa39wRBgmaQK3FnarDF0LgWmcV9qjAJ4w9KLR6+4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=i010sXlkR43N7sMa+oOenSf2wA2YENOmoKb2WksjGJ1YGq9WPSd6EVcZoUkHoKPQB iwxLgBpZeiBpAgnejIOlntgokHkeSMbhEedJSuztMQGTV0LuG0r4ilbtLuZS8Bqmwu dqW2hChHr0Pr+vasChdFObcVV8fVKVy4EoMYbNJjoWa9oNvwmfSgOjwDLkJncle2lG vJ795NRSck0DDMFl3Vokm21RM71j8T4q5gytnoALRHFlRZ0mwqMMjS8VNuiMyVJ+Mw BqaheCmboZDGwY8cNRBNjzHPGdR5B+tGORXBGwwRJD1deOFfHlXwlZuHuH1IkKWNR/ ousOl2qo6Q2YA== From: Christian Brauner Date: Mon, 12 May 2025 10:55:23 +0200 Subject: [PATCH v6 4/9] coredump: add coredump socket Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250512-work-coredump-socket-v6-4-c51bc3450727@kernel.org> References: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> In-Reply-To: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> To: linux-fsdevel@vger.kernel.org, Jann Horn , Daniel Borkmann , Kuniyuki Iwashima Cc: Eric Dumazet , Oleg Nesterov , "David S. Miller" , Alexander Viro , Daan De Meyer , David Rheinsberg , Jakub Kicinski , Jan Kara , Lennart Poettering , Luca Boccassi , Mike Yuan , Paolo Abeni , Simon Horman , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-security-module@vger.kernel.org, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=15291; i=brauner@kernel.org; h=from:subject:message-id; bh=ZpqOa39wRBgmaQK3FnarDF0LgWmcV9qjAJ4w9KLR6+4=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWQobm/vnXI0ruhsx5XrPm+/Time8z5U6eeUksPPXm5PC FmXbnA+pKOUhUGMi0FWTJHFod0kXG45T8Vmo0wNmDmsTCBDGLg4BWAiclcZ/oo2Fd/QW/Vr8f9w 6w+Fv45vXLDaqeKnHV/wyYs5qz+xX4hl+J+csH8d+/ZTLS8f9D9SKFD4e//V2X6jjW8nWO1+vef CtDROAA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Coredumping currently supports two modes: (1) Dumping directly into a file somewhere on the filesystem. (2) Dumping into a pipe connected to a usermode helper process spawned as a child of the system_unbound_wq or kthreadd. For simplicity I'm mostly ignoring (1). There's probably still some users of (1) out there but processing coredumps in this way can be considered adventurous especially in the face of set*id binaries. The most common option should be (2) by now. It works by allowing userspace to put a string into /proc/sys/kernel/core_pattern like: |/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h The "|" at the beginning indicates to the kernel that a pipe must be used. The path following the pipe indicator is a path to a binary that will be spawned as a usermode helper process. Any additional parameters pass information about the task that is generating the coredump to the binary that processes the coredump. In the example core_pattern shown above systemd-coredump is spawned as a usermode helper. There's various conceptual consequences of this (non-exhaustive list): - systemd-coredump is spawned with file descriptor number 0 (stdin) connected to the read-end of the pipe. All other file descriptors are closed. That specifically includes 1 (stdout) and 2 (stderr). This has already caused bugs because userspace assumed that this cannot happen (Whether or not this is a sane assumption is irrelevant.). - systemd-coredump will be spawned as a child of system_unbound_wq. So it is not a child of any userspace process and specifically not a child of PID 1. It cannot be waited upon and is in a weird hybrid upcall which are difficult for userspace to control correctly. - systemd-coredump is spawned with full kernel privileges. This necessitates all kinds of weird privilege dropping excercises in userspace to make this safe. - A new usermode helper has to be spawned for each crashing process. This series adds a new mode: (3) Dumping into an abstract AF_UNIX socket. Userspace can set /proc/sys/kernel/core_pattern to: @address SO_COOKIE The "@" at the beginning indicates to the kernel that the abstract AF_UNIX coredump socket will be used to process coredumps. The address is given by @address and must be followed by the socket cookie of the coredump listening socket. The socket cookie is used to verify the socket connection. If the coredump server restarts or crashes and someone recycles the socket address the kernel will detect that the address has been recycled as the socket cookie will have necessarily changed and refuse to connect. The coredump socket is located in the initial network namespace. When a task coredumps it opens a client socket in the initial network namespace and connects to the coredump socket. - The coredump server uses SO_PEERPIDFD to get a stable handle on the connected crashing task. The retrieved pidfd will provide a stable reference even if the crashing task gets SIGKILLed while generating the coredump. - By setting core_pipe_limit non-zero userspace can guarantee that the crashing task cannot be reaped behind it's back and thus process all necessary information in /proc/. The SO_PEERPIDFD can be used to detect whether /proc/ still refers to the same process. The core_pipe_limit isn't used to rate-limit connections to the socket. This can simply be done via AF_UNIX sockets directly. - The pidfd for the crashing task will grow new information how the task coredumps. - The coredump server should mark itself as non-dumpable. - A container coredump server in a separate network namespace can simply bind to another well-know address and systemd-coredump fowards coredumps to the container. - Coredumps could in the future also be handled via per-user/session coredump servers that run only with that users privileges. The coredump server listens on the coredump socket and accepts a new coredump connection. It then retrieves SO_PEERPIDFD for the client, inspects uid/gid and hands the accepted client to the users own coredump handler which runs with the users privileges only (It must of coure pay close attention to not forward crashing suid binaries.). The new coredump socket will allow userspace to not have to rely on usermode helpers for processing coredumps and provides a safer way to handle them instead of relying on super privileged coredumping helpers that have and continue to cause significant CVEs. This will also be significantly more lightweight since no fork()+exec() for the usermodehelper is required for each crashing process. The coredump server in userspace can e.g., just keep a worker pool. Signed-off-by: Christian Brauner Reviewed-by: Kuniyuki Iwashima --- fs/coredump.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++++= +--- include/linux/net.h | 1 + net/socket.c | 5 +- net/unix/af_unix.c | 24 ++++++--- 4 files changed, 161 insertions(+), 16 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index a70929c3585b..15e9d9a252cd 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -44,7 +44,11 @@ #include #include #include +#include +#include +#include #include +#include =20 #include #include @@ -79,6 +83,7 @@ unsigned int core_file_note_size_limit =3D CORE_FILE_NOTE= _SIZE_DEFAULT; enum coredump_type_t { COREDUMP_FILE =3D 1, COREDUMP_PIPE =3D 2, + COREDUMP_SOCK =3D 3, }; =20 struct core_name { @@ -232,13 +237,16 @@ static int format_corename(struct core_name *cn, stru= ct coredump_params *cprm, cn->corename =3D NULL; if (*pat_ptr =3D=3D '|') cn->core_type =3D COREDUMP_PIPE; + else if (*pat_ptr =3D=3D '@') + cn->core_type =3D COREDUMP_SOCK; else cn->core_type =3D COREDUMP_FILE; if (expand_corename(cn, core_name_size)) return -ENOMEM; cn->corename[0] =3D '\0'; =20 - if (cn->core_type =3D=3D COREDUMP_PIPE) { + switch (cn->core_type) { + case COREDUMP_PIPE: { int argvs =3D sizeof(core_pattern) / 2; (*argv) =3D kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL); if (!(*argv)) @@ -247,6 +255,29 @@ static int format_corename(struct core_name *cn, struc= t coredump_params *cprm, ++pat_ptr; if (!(*pat_ptr)) return -ENOMEM; + break; + } + case COREDUMP_SOCK: { + /* skip the @ */ + pat_ptr++; + err =3D cn_printf(cn, "%s", pat_ptr); + if (err) + return err; + + /* + * Currently no need to parse any other options. + * Relevant information can be retrieved from the peer + * pidfd retrievable via SO_PEERPIDFD by the receiver or + * via /proc/, using the SO_PEERPIDFD to guard + * against pid recycling when opening /proc/. + */ + return 0; + } + case COREDUMP_FILE: + break; + default: + WARN_ON_ONCE(true); + return -EINVAL; } =20 /* Repeat as long as we have more pattern to process and more output @@ -393,11 +424,20 @@ static int format_corename(struct core_name *cn, stru= ct coredump_params *cprm, * If core_pattern does not include a %p (as is the default) * and core_uses_pid is set, then .%pid will be appended to * the filename. Do not do this for piped commands. */ - if (!(cn->core_type =3D=3D COREDUMP_PIPE) && !pid_in_pattern && core_uses= _pid) { - err =3D cn_printf(cn, ".%d", task_tgid_vnr(current)); - if (err) - return err; + if (!pid_in_pattern && core_uses_pid) { + switch (cn->core_type) { + case COREDUMP_FILE: + return cn_printf(cn, ".%d", task_tgid_vnr(current)); + case COREDUMP_PIPE: + break; + case COREDUMP_SOCK: + break; + default: + WARN_ON_ONCE(true); + return -EINVAL; + } } + return 0; } =20 @@ -801,6 +841,73 @@ void do_coredump(const kernel_siginfo_t *siginfo) } break; } + case COREDUMP_SOCK: { +#ifdef CONFIG_UNIX + struct file *file __free(fput) =3D NULL; + struct sockaddr_un addr =3D { + .sun_family =3D AF_UNIX, + }; + unsigned int addr_len; + struct socket *socket; + char *p; + u64 sock_cookie; + + p =3D strchr(cn.corename, ' '); + if (!p) { + coredump_report_failure("Missing socket cookie"); + goto close_fail; + } + *p++ =3D '\0'; + + /* Leave room for the socket cookie. */ + retval =3D strscpy(addr.sun_path + 1, cn.corename, + sizeof(addr.sun_path) - sizeof(sock_cookie) - 1); + if (retval < 0) + goto close_fail; + addr_len =3D offsetof(struct sockaddr_un, sun_path) + retval + 1; + + retval =3D kstrtou64(p, 0, &sock_cookie); + if (retval || !sock_cookie) { + coredump_report_failure("Invalid socket cookie"); + goto close_fail; + } + + /* append socket cookie */ + memcpy((char *)&addr + addr_len, &sock_cookie, sizeof(sock_cookie)); + + /* + * It is possible that the userspace process which is + * supposed to handle the coredump and is listening on + * the AF_UNIX socket coredumps. Userspace should just + * mark itself non dumpable. + */ + + retval =3D sock_create_kern(&init_net, AF_UNIX, SOCK_STREAM, 0, &socket); + if (retval < 0) + goto close_fail; + + file =3D sock_alloc_file(socket, 0, NULL); + if (IS_ERR(file)) { + sock_release(socket); + goto close_fail; + } + + retval =3D kernel_connect(socket, (struct sockaddr *)(&addr), + addr_len, O_NONBLOCK | SOCK_COREDUMP); + if (retval) { + if (retval =3D=3D -EAGAIN) + coredump_report_failure("Skipping as coredump socket connection %s cou= ldn't complete immediately", cn.corename); + goto close_fail; + } + + cprm.limit =3D RLIM_INFINITY; + cprm.file =3D no_free_ptr(file); +#else + coredump_report_failure("Core dump socket support %s disabled", cn.coren= ame); + goto close_fail; +#endif + break; + } default: WARN_ON_ONCE(true); goto close_fail; @@ -838,8 +945,32 @@ void do_coredump(const kernel_siginfo_t *siginfo) file_end_write(cprm.file); free_vma_snapshot(&cprm); } - if ((cn.core_type =3D=3D COREDUMP_PIPE) && core_pipe_limit) - wait_for_dump_helpers(cprm.file); + + /* + * When core_pipe_limit is set we wait for the coredump server + * or usermodehelper to finish before exiting so it can e.g., + * inspect /proc/. + */ + if (core_pipe_limit) { + switch (cn.core_type) { + case COREDUMP_PIPE: + wait_for_dump_helpers(cprm.file); + break; + case COREDUMP_SOCK: { + /* + * We use a simple read to wait for the coredump + * processing to finish. Either the socket is + * closed or we get sent unexpected data. In + * both cases, we're done. + */ + __kernel_read(cprm.file, &(char){ 0 }, 1, NULL); + break; + } + default: + break; + } + } + close_fail: if (cprm.file) filp_close(cprm.file, NULL); @@ -1069,7 +1200,7 @@ EXPORT_SYMBOL(dump_align); void validate_coredump_safety(void) { if (suid_dumpable =3D=3D SUID_DUMP_ROOT && - core_pattern[0] !=3D '/' && core_pattern[0] !=3D '|') { + core_pattern[0] !=3D '/' && core_pattern[0] !=3D '|' && core_pattern[= 0] !=3D '@') { =20 coredump_report_failure("Unsafe core_pattern used with fs.suid_dumpable= =3D2: " "pipe handler or fully qualified core dump path required. " diff --git a/include/linux/net.h b/include/linux/net.h index 0ff950eecc6b..139c85d0f2ea 100644 --- a/include/linux/net.h +++ b/include/linux/net.h @@ -81,6 +81,7 @@ enum sock_type { #ifndef SOCK_NONBLOCK #define SOCK_NONBLOCK O_NONBLOCK #endif +#define SOCK_COREDUMP O_NOCTTY =20 #endif /* ARCH_HAS_SOCKET_TYPES */ =20 diff --git a/net/socket.c b/net/socket.c index 9a0e720f0859..d62a57a57a28 100644 --- a/net/socket.c +++ b/net/socket.c @@ -3603,7 +3603,10 @@ int kernel_connect(struct socket *sock, struct socka= ddr *addr, int addrlen, { struct sockaddr_storage address; =20 - memcpy(&address, addr, addrlen); + if (flags & SOCK_COREDUMP) + memcpy(&address, addr, addrlen + sizeof(sock->sk->sk_cookie)); + else + memcpy(&address, addr, addrlen); =20 return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr *)&address, addrlen, flags); diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 472f8aa9ea15..6b8a7863b41c 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -89,6 +89,8 @@ #include #include #include +#include +#include #include #include #include @@ -100,7 +102,6 @@ #include #include #include -#include #include #include #include @@ -1191,7 +1192,7 @@ static struct sock *unix_find_bsd(struct sockaddr_un = *sunaddr, int addr_len, =20 static struct sock *unix_find_abstract(struct net *net, struct sockaddr_un *sunaddr, - int addr_len, int type) + int addr_len, int type, int flags) { unsigned int hash =3D unix_abstract_hash(sunaddr, addr_len, type); struct dentry *dentry; @@ -1201,6 +1202,15 @@ static struct sock *unix_find_abstract(struct net *n= et, if (!sk) return ERR_PTR(-ECONNREFUSED); =20 + if (flags & SOCK_COREDUMP) { + u64 sock_cookie; + + memcpy(&sock_cookie, (char *)sunaddr + addr_len, sizeof(sock_cookie)); + DEBUG_NET_WARN_ON_ONCE(!sock_cookie); + if (sock_cookie !=3D atomic64_read(&sk->sk_cookie)) + return ERR_PTR(-ECONNREFUSED); + } + dentry =3D unix_sk(sk)->path.dentry; if (dentry) touch_atime(&unix_sk(sk)->path); @@ -1210,14 +1220,14 @@ static struct sock *unix_find_abstract(struct net *= net, =20 static struct sock *unix_find_other(struct net *net, struct sockaddr_un *sunaddr, - int addr_len, int type) + int addr_len, int type, int flags) { struct sock *sk; =20 if (sunaddr->sun_path[0]) sk =3D unix_find_bsd(sunaddr, addr_len, type); else - sk =3D unix_find_abstract(net, sunaddr, addr_len, type); + sk =3D unix_find_abstract(net, sunaddr, addr_len, type, flags); =20 return sk; } @@ -1473,7 +1483,7 @@ static int unix_dgram_connect(struct socket *sock, st= ruct sockaddr *addr, } =20 restart: - other =3D unix_find_other(sock_net(sk), sunaddr, alen, sock->type); + other =3D unix_find_other(sock_net(sk), sunaddr, alen, sock->type, 0); if (IS_ERR(other)) { err =3D PTR_ERR(other); goto out; @@ -1620,7 +1630,7 @@ static int unix_stream_connect(struct socket *sock, s= truct sockaddr *uaddr, =20 restart: /* Find listening sock. */ - other =3D unix_find_other(net, sunaddr, addr_len, sk->sk_type); + other =3D unix_find_other(net, sunaddr, addr_len, sk->sk_type, flags); if (IS_ERR(other)) { err =3D PTR_ERR(other); goto out_free_skb; @@ -2089,7 +2099,7 @@ static int unix_dgram_sendmsg(struct socket *sock, st= ruct msghdr *msg, if (msg->msg_namelen) { lookup: other =3D unix_find_other(sock_net(sk), msg->msg_name, - msg->msg_namelen, sk->sk_type); + msg->msg_namelen, sk->sk_type, 0); if (IS_ERR(other)) { err =3D PTR_ERR(other); goto out_free; --=20 2.47.2 From nobody Wed Dec 17 08:51:12 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65AB625A33D; Mon, 12 May 2025 08:56:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040167; cv=none; b=WrMIB7Hm1+xdFC/FV4QiYSKa905oTzdTNcclYj8Jk8WYwRBFg8zdXBZX/WABHQXssSUE7WAgy191EcrO2tTiF2RUssTyjELEvECq9oTPg3JsnyGA1YVR31GilUvHgDbvJlWYQtLGXzLk0RyeZjyK04EvZrIMXz0nxmlMd2T0ErU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040167; c=relaxed/simple; bh=V+jZe1H1IwILkHh/4XoMPzxVdsJcyU2xWAhKBzy9YwY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cRoCzihIFuIOEIT64XiL1WAPCTX7gaHmjMMetxmKcx0CMJF8Xg6E+1HHb7X+qvchJyRTG9NgFid8Gd0mnnLxE15Cvd7g36GfnI/K1zbz8Rd5qIY+nbsudA9fyo6Zcc44qbAPv2Jnf9PSQ9sGP+aFUgSatZYQtUxFpN2Q/03IUkI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=R2HweS1h; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="R2HweS1h" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B5FAC4CEED; Mon, 12 May 2025 08:56:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747040167; bh=V+jZe1H1IwILkHh/4XoMPzxVdsJcyU2xWAhKBzy9YwY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=R2HweS1h1oY0A14g1yvJFr4mOxDYE1MCSXoD1KfRoaCTrH0m7aoimWG+okTFl0Pac Ua1KlM1Zx9aTIUsGEi2leQWegfxuvvCM+TagYSzS0hpVPMdGtchnyPicR/MaO9ALrR 45gnkEHzLnBcqyBbjvIwGEVZRpI9U7M9iP/u7qHwUUMfr6vGREPTM9vj9BBWEmUErQ LNYGtllZnpNes9sSwb5cQ6OEp73lXZlIYLoQe0iIcgwQZKydn0tdeMYxl/RKD+2y/g 6zdKnmmPCDGU8g+A0wThOW3m+Ajz9EHWxl5a9yvcPsRStFBqLZWe3eWN6e4qGrUqnk 3/yC2lw6FjJfg== From: Christian Brauner Date: Mon, 12 May 2025 10:55:24 +0200 Subject: [PATCH v6 5/9] pidfs, coredump: add PIDFD_INFO_COREDUMP Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250512-work-coredump-socket-v6-5-c51bc3450727@kernel.org> References: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> In-Reply-To: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> To: linux-fsdevel@vger.kernel.org, Jann Horn , Daniel Borkmann , Kuniyuki Iwashima Cc: Eric Dumazet , Oleg Nesterov , "David S. Miller" , Alexander Viro , Daan De Meyer , David Rheinsberg , Jakub Kicinski , Jan Kara , Lennart Poettering , Luca Boccassi , Mike Yuan , Paolo Abeni , Simon Horman , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-security-module@vger.kernel.org, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=12198; i=brauner@kernel.org; h=from:subject:message-id; bh=V+jZe1H1IwILkHh/4XoMPzxVdsJcyU2xWAhKBzy9YwY=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWQobm9n75i75d4Oue3PLiz1itd4eefpgadzvv7Ren6Uw /TVdMvrVh2lLAxiXAyyYoosDu0m4XLLeSo2G2VqwMxhZQIZwsDFKQATWW7PyPD7+QeW+oiC2UUv tH5MjewVvM/FcqVbrkzBWWHyg3fV1vcZGTbwebZploRtkJiVmdW67sSnKwIhjzd8/5H3Io/H9OO dShYA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Extend the PIDFD_INFO_COREDUMP ioctl() with the new PIDFD_INFO_COREDUMP mask flag. This adds the fields @coredump_mask and @coredump_cookie to struct pidfd_info. When a task coredumps the kernel will provide the following information to userspace in @coredump_mask: * PIDFD_COREDUMPED is raised if the task did actually coredump. * PIDFD_COREDUMP_SKIP is raised if the task skipped coredumping (e.g., undumpable). * PIDFD_COREDUMP_USER is raised if this is a regular coredump and doesn't need special care by the coredump server. * PIDFD_COREDUMP_ROOT is raised if the generated coredump should be treated as sensitive and the coredump server should restrict to the generated coredump to sufficiently privileged users. If userspace uses the coredump socket to process coredumps it needs to be able to discern connection from the kernel from connects from userspace (e.g., Python generating it's own coredumps and forwarding them to systemd). The @coredump_cookie extension uses the SO_COOKIE of the new connection. This allows userspace to validate that the connection has been made from the kernel by a crashing task: fd_coredump =3D accept4(fd_socket, NULL, NULL, SOCK_CLOEXEC); getsockopt(fd_coredump, SOL_SOCKET, SO_PEERPIDFD, &fd_peer_pidfd, &fd_pe= er_pidfd_len); struct pidfd_info info =3D { info.mask =3D PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP, }; ioctl(pidfd, PIDFD_GET_INFO, &info); /* Refuse connections that aren't from a crashing task. */ if (!(info.mask & PIDFD_INFO_COREDUMP) || !(info.coredump_mask & PIDFD_C= OREDUMPED) ) close(fd_coredump); /* * Make sure that the coredump cookie matches the connection cookie. * If they don't it's not the coredump connection from the kernel. * We'll get another connection request in a bit. */ getsocketop(fd_coredump, SOL_SOCKET, SO_COOKIE, &peer_cookie, &peer_cook= ie_len); if (!info.coredump_cookie || (info.coredump_cookie !=3D peer_cookie)) close(fd_coredump); The kernel guarantees that by the time the connection is made the all PIDFD_INFO_COREDUMP info is available. Signed-off-by: Christian Brauner --- fs/coredump.c | 34 ++++++++++++++++++++ fs/pidfs.c | 79 ++++++++++++++++++++++++++++++++++++++++++= ++++ include/linux/pidfs.h | 10 ++++++ include/uapi/linux/pidfd.h | 22 +++++++++++++ net/unix/af_unix.c | 7 ++++ 5 files changed, 152 insertions(+) diff --git a/fs/coredump.c b/fs/coredump.c index 15e9d9a252cd..5ea0c93dd5ac 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -46,7 +46,9 @@ #include #include #include +#include #include +#include #include #include =20 @@ -594,6 +596,8 @@ static int umh_coredump_setup(struct subprocess_info *i= nfo, struct cred *new) if (IS_ERR(pidfs_file)) return PTR_ERR(pidfs_file); =20 + pidfs_coredump(cp); + /* * Usermode helpers are childen of either * system_unbound_wq or of kthreadd. So we know that @@ -892,14 +896,44 @@ void do_coredump(const kernel_siginfo_t *siginfo) goto close_fail; } =20 + /* + * Set the thread-group leader pid which is used for the + * peer credentials during connect() below. Then + * immediately register it in pidfs... + */ + cprm.pid =3D task_tgid(current); + retval =3D pidfs_register_pid(cprm.pid); + if (retval) { + sock_release(socket); + goto close_fail; + } + + /* + * ... and set the coredump information so userspace + * has it available after connect()... + */ + pidfs_coredump(&cprm); + + /* + * ... On connect() the peer credentials are recorded + * and @cprm.pid registered in pidfs... + */ retval =3D kernel_connect(socket, (struct sockaddr *)(&addr), addr_len, O_NONBLOCK | SOCK_COREDUMP); + + /* ... So we can safely put our pidfs reference now... */ + pidfs_put_pid(cprm.pid); + if (retval) { if (retval =3D=3D -EAGAIN) coredump_report_failure("Skipping as coredump socket connection %s cou= ldn't complete immediately", cn.corename); goto close_fail; } =20 + /* ... and validate that @sk_peer_pid matches @cprm.pid. */ + if (WARN_ON_ONCE(unix_peer(socket->sk)->sk_peer_pid !=3D cprm.pid)) + goto close_fail; + cprm.limit =3D RLIM_INFINITY; cprm.file =3D no_free_ptr(file); #else diff --git a/fs/pidfs.c b/fs/pidfs.c index 3b39e471840b..d7b9a0dd2db6 100644 --- a/fs/pidfs.c +++ b/fs/pidfs.c @@ -20,6 +20,7 @@ #include #include #include +#include =20 #include "internal.h" #include "mount.h" @@ -33,6 +34,8 @@ static struct kmem_cache *pidfs_cachep __ro_after_init; struct pidfs_exit_info { __u64 cgroupid; __s32 exit_code; + __u32 coredump_mask; + __u64 coredump_cookie; }; =20 struct pidfs_inode { @@ -240,6 +243,22 @@ static inline bool pid_in_current_pidns(const struct p= id *pid) return false; } =20 +static __u32 pidfs_coredump_mask(unsigned long mm_flags) +{ + switch (__get_dumpable(mm_flags)) { + case SUID_DUMP_USER: + return PIDFD_COREDUMP_USER; + case SUID_DUMP_ROOT: + return PIDFD_COREDUMP_ROOT; + case SUID_DUMP_DISABLE: + return PIDFD_COREDUMP_SKIP; + default: + WARN_ON_ONCE(true); + } + + return 0; +} + static long pidfd_info(struct file *file, unsigned int cmd, unsigned long = arg) { struct pidfd_info __user *uinfo =3D (struct pidfd_info __user *)arg; @@ -280,6 +299,13 @@ static long pidfd_info(struct file *file, unsigned int= cmd, unsigned long arg) } } =20 + if (mask & PIDFD_INFO_COREDUMP) { + kinfo.mask |=3D PIDFD_INFO_COREDUMP; + smp_rmb(); + kinfo.coredump_cookie =3D READ_ONCE(pidfs_i(inode)->__pei.coredump_cooki= e); + kinfo.coredump_mask =3D READ_ONCE(pidfs_i(inode)->__pei.coredump_mask); + } + task =3D get_pid_task(pid, PIDTYPE_PID); if (!task) { /* @@ -296,6 +322,16 @@ static long pidfd_info(struct file *file, unsigned int= cmd, unsigned long arg) if (!c) return -ESRCH; =20 + if (!(kinfo.mask & PIDFD_INFO_COREDUMP)) { + task_lock(task); + if (task->mm) { + smp_rmb(); + kinfo.coredump_cookie =3D READ_ONCE(pidfs_i(inode)->__pei.coredump_cook= ie); + kinfo.coredump_mask =3D pidfs_coredump_mask(task->mm->flags); + } + task_unlock(task); + } + /* Unconditionally return identifiers and credentials, the rest only on r= equest */ =20 user_ns =3D current_user_ns(); @@ -559,6 +595,49 @@ void pidfs_exit(struct task_struct *tsk) } } =20 +#if defined(CONFIG_COREDUMP) && defined(CONFIG_UNIX) +void pidfs_coredump_cookie(struct pid *pid, u64 coredump_cookie) +{ + struct pidfs_exit_info *exit_info; + struct dentry *dentry =3D pid->stashed; + struct inode *inode; + + if (WARN_ON_ONCE(!dentry)) + return; + + inode =3D d_inode(dentry); + exit_info =3D &pidfs_i(inode)->__pei; + /* Can't use smp_store_release() because of 32bit. */ + smp_wmb(); + WRITE_ONCE(exit_info->coredump_cookie, coredump_cookie); +} +#endif + +#ifdef CONFIG_COREDUMP +void pidfs_coredump(const struct coredump_params *cprm) +{ + struct pid *pid =3D cprm->pid; + struct pidfs_exit_info *exit_info; + struct dentry *dentry; + struct inode *inode; + __u32 coredump_mask =3D 0; + + dentry =3D pid->stashed; + if (WARN_ON_ONCE(!dentry)) + return; + + inode =3D d_inode(dentry); + exit_info =3D &pidfs_i(inode)->__pei; + /* Note how we were coredumped. */ + coredump_mask =3D pidfs_coredump_mask(cprm->mm_flags); + /* Note that we actually did coredump. */ + coredump_mask |=3D PIDFD_COREDUMPED; + /* If coredumping is set to skip we should never end up here. */ + VFS_WARN_ON_ONCE(coredump_mask & PIDFD_COREDUMP_SKIP); + smp_store_release(&exit_info->coredump_mask, coredump_mask); +} +#endif + static struct vfsmount *pidfs_mnt __ro_after_init; =20 /* diff --git a/include/linux/pidfs.h b/include/linux/pidfs.h index 2676890c4d0d..497997bc5e34 100644 --- a/include/linux/pidfs.h +++ b/include/linux/pidfs.h @@ -2,11 +2,21 @@ #ifndef _LINUX_PID_FS_H #define _LINUX_PID_FS_H =20 +struct coredump_params; + struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags); void __init pidfs_init(void); void pidfs_add_pid(struct pid *pid); void pidfs_remove_pid(struct pid *pid); void pidfs_exit(struct task_struct *tsk); +#ifdef CONFIG_COREDUMP +void pidfs_coredump(const struct coredump_params *cprm); +#endif +#if defined(CONFIG_COREDUMP) && defined(CONFIG_UNIX) +void pidfs_coredump_cookie(struct pid *pid, u64 coredump_cookie); +#elif defined(CONFIG_UNIX) +static inline void pidfs_coredump_cookie(struct pid *pid, u64 coredump_coo= kie) { } +#endif extern const struct dentry_operations pidfs_dentry_operations; int pidfs_register_pid(struct pid *pid); void pidfs_get_pid(struct pid *pid); diff --git a/include/uapi/linux/pidfd.h b/include/uapi/linux/pidfd.h index 8c1511edd0e9..69267c5ae6d0 100644 --- a/include/uapi/linux/pidfd.h +++ b/include/uapi/linux/pidfd.h @@ -25,9 +25,28 @@ #define PIDFD_INFO_CREDS (1UL << 1) /* Always returned, even if not reque= sted */ #define PIDFD_INFO_CGROUPID (1UL << 2) /* Always returned if available, e= ven if not requested */ #define PIDFD_INFO_EXIT (1UL << 3) /* Only returned if requested. */ +#define PIDFD_INFO_COREDUMP (1UL << 4) /* Only returned if requested. */ =20 #define PIDFD_INFO_SIZE_VER0 64 /* sizeof first published struct */ =20 +/* + * Values for @coredump_mask in pidfd_info. + * Only valid if PIDFD_INFO_COREDUMP is set in @mask. + * + * Note, the @PIDFD_COREDUMP_ROOT flag indicates that the generated + * coredump should be treated as sensitive and access should only be + * granted to privileged users. + * + * If the coredump AF_UNIX socket is used for processing coredumps + * @coredump_cookie will be set to the socket SO_COOKIE of the receivers + * client socket. This allows the coredump handler to detect whether an + * incoming coredump connection was initiated from the crashing task. + */ +#define PIDFD_COREDUMPED (1U << 0) /* Did crash and... */ +#define PIDFD_COREDUMP_SKIP (1U << 1) /* coredumping generation was skippe= d. */ +#define PIDFD_COREDUMP_USER (1U << 2) /* coredump was done as the user. */ +#define PIDFD_COREDUMP_ROOT (1U << 3) /* coredump was done as root. */ + /* * The concept of process and threads in userland and the kernel is a conf= using * one - within the kernel every thread is a 'task' with its own individua= l PID, @@ -92,6 +111,9 @@ struct pidfd_info { __u32 fsuid; __u32 fsgid; __s32 exit_code; + __u32 coredump_mask; + __u32 __spare1; + __u64 coredump_cookie; }; =20 #define PIDFS_IOCTL_MAGIC 0xFF diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 6b8a7863b41c..8308f74f8490 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -98,6 +98,7 @@ #include #include #include +#include #include #include #include @@ -741,6 +742,7 @@ static void unix_release_sock(struct sock *sk, int embr= ion) =20 struct unix_peercred { struct pid *peer_pid; + u64 cookie; const struct cred *peer_cred; }; =20 @@ -776,6 +778,8 @@ static void drop_peercred(struct unix_peercred *peercre= d) static inline void init_peercred(struct sock *sk, const struct unix_peercred *peercred) { + if (peercred->cookie) + pidfs_coredump_cookie(peercred->peer_pid, peercred->cookie); sk->sk_peer_pid =3D peercred->peer_pid; sk->sk_peer_cred =3D peercred->peer_cred; } @@ -1696,6 +1700,9 @@ static int unix_stream_connect(struct socket *sock, s= truct sockaddr *uaddr, unix_peer(newsk) =3D sk; newsk->sk_state =3D TCP_ESTABLISHED; newsk->sk_type =3D sk->sk_type; + /* Prepare a new socket cookie for the receiver. */ + if (flags & SOCK_COREDUMP) + peercred.cookie =3D sock_gen_cookie(newsk); init_peercred(newsk, &peercred); newu =3D unix_sk(newsk); newu->listener =3D other; --=20 2.47.2 From nobody Wed Dec 17 08:51:12 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60BE125A33D; Mon, 12 May 2025 08:56:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040173; cv=none; b=llIEpBHU4yhTHeOpLdflcoWNFbILNBt7b81NDzzdi6mLwpE3aa1a+LcOVbDPB+gOZC9eEOTLTTkK/n0U8AdjfiikULWV4JK90XsH2Qv9yoJqnejxHYFrx4xY8Wb6Yv7m5pgv1uKIBxHkPo7Q6Z4R6DSIsTYKQyHYnha4qN+PWBo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040173; c=relaxed/simple; bh=Km4WnFRvUry5Exd8fNEf7asKYTERy4VYPOic7LtuZb0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Zn9uH/dFsPd0gyb+lSYngOusQJnBKosoMzBSv7AO/ahwijIbnbIRHQvPyCF7NaOWea7xfnK4oWm672ECk7AP3eec+0fIjbiDbZMnh4j1eMWDhg0fMQcuwLE1FEVMExzuIZ4J0FvP4sfLLQtvSVfw7lOBVG52uJjWXMAl8BEh/oc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tRvNmCkM; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="tRvNmCkM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CAAFAC4CEE7; Mon, 12 May 2025 08:56:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747040172; bh=Km4WnFRvUry5Exd8fNEf7asKYTERy4VYPOic7LtuZb0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=tRvNmCkMCaX7DSgimUG1pB0JDYDuKxHOAmTsfvOgTZLezn6YSWB+qMg1XtleVwUwP ceSDhzT13Xk/xTj/PR3VfmleGsHPpVf3c6boaM7LSsifcj/phmALAa1pk3WHEV2hQ7 2jxz5sDsmjyRCbMVKBH6R8y87DRvFP9TrbjEemliVPTBFofUnPUbgIlbfRP9TIb8Hu HksRpddUGamHBVZT5iWxFP6XEOOe7H8MwZl09yFDce/sYQ1Du01o6v011DMZUWuuER 0ULh6/94mrQH0KzMZoXRLHqnEkx4y8Imys2tswsuntdk9fzGhqWuX6PVl37vIRyIgI hOafWZXvZW4Ww== From: Christian Brauner Date: Mon, 12 May 2025 10:55:25 +0200 Subject: [PATCH v6 6/9] coredump: show supported coredump modes Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250512-work-coredump-socket-v6-6-c51bc3450727@kernel.org> References: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> In-Reply-To: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> To: linux-fsdevel@vger.kernel.org, Jann Horn , Daniel Borkmann , Kuniyuki Iwashima Cc: Eric Dumazet , Oleg Nesterov , "David S. Miller" , Alexander Viro , Daan De Meyer , David Rheinsberg , Jakub Kicinski , Jan Kara , Lennart Poettering , Luca Boccassi , Mike Yuan , Paolo Abeni , Simon Horman , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-security-module@vger.kernel.org, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1120; i=brauner@kernel.org; h=from:subject:message-id; bh=Km4WnFRvUry5Exd8fNEf7asKYTERy4VYPOic7LtuZb0=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWQobm9nWKW7aOeCndrcORrxUfl6b17O6fi2/2Cu/IKvj 3ntXFnOdpSyMIhxMciKKbI4tJuEyy3nqdhslKkBM4eVCWwIF6cATEQonuG/a8GaoHlT12+ZslI2 hFVifaWr6uS6cJGVZ/aasQrwncqKZ/hn4aDitNxgc+ReOw2r78ppZ+T7nXoXMDj28zDe7Ny9pJg LAA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Allow userspace to discover what coredump modes are supported. Signed-off-by: Christian Brauner --- fs/coredump.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fs/coredump.c b/fs/coredump.c index 5ea0c93dd5ac..deee52bff6bc 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -1254,6 +1254,12 @@ static int proc_dostring_coredump(const struct ctl_t= able *table, int write, =20 static const unsigned int core_file_note_size_min =3D CORE_FILE_NOTE_SIZE_= DEFAULT; static const unsigned int core_file_note_size_max =3D CORE_FILE_NOTE_SIZE_= MAX; +static char core_modes[] =3D { + "file\npipe" +#ifdef CONFIG_UNIX + "\nsocket" +#endif +}; =20 static const struct ctl_table coredump_sysctls[] =3D { { @@ -1297,6 +1303,13 @@ static const struct ctl_table coredump_sysctls[] =3D= { .extra1 =3D SYSCTL_ZERO, .extra2 =3D SYSCTL_ONE, }, + { + .procname =3D "core_modes", + .data =3D core_modes, + .maxlen =3D sizeof(core_modes) - 1, + .mode =3D 0444, + .proc_handler =3D proc_dostring, + }, }; =20 static int __init init_fs_coredump_sysctls(void) --=20 2.47.2 From nobody Wed Dec 17 08:51:12 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E4BB725A2B8; Mon, 12 May 2025 08:56:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040180; cv=none; b=PAmKQiFmKZ6FidXRWagYomZxk5Ptd6Rmi+Tspy6RKPBzX+hxNZTltkieEmvnqVtPh9Xgy9tbKn/BFLqvJ0s62tE46r0qby/Ge++THoSQnHB6JPijhFy+dUb437Hb6t1/tKuW47cFHPEN/EUsSGhJRJmLKymJViBPJ/B0XQTxt3U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040180; c=relaxed/simple; bh=0BiGaI9QZtmJlstEuxiCKxHauy4PjCyPjPux0hQjYQA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=docWvxaoiBKN8y8PRo9QGlgCaudBGpDe3uzQxsahN4nSCha5j3y00ImyMJy4OWb3wof3stfXsrIhTYiGtiT37QMrbXR63s1ln9/wT5C518QUunB2LqqQm7Gg8Zx3XIbN8Vt3vbIiHrE4FnbFbDhnVn6P+dh0Wu4hGQO33eEf5sw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EdQtyeyb; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EdQtyeyb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 643A8C4CEE9; Mon, 12 May 2025 08:56:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747040178; bh=0BiGaI9QZtmJlstEuxiCKxHauy4PjCyPjPux0hQjYQA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=EdQtyeybwNHIadd4alM80IN3dI8TfX7rI1MrguNmxaj5Gor+BOXgdcy1FHi/3P9pg rD2tdO+ANB6qHN7uhilP1UwkltKg1W3wAvEfzoDLWLcNFDb1iPrkbyACz5sIW0DhQy 7on/P6ht/CrIzuh0UiU9fNYRTQ+dG9aqj0+tvX7a6c3WFzMzeKLMMEF925wXU09HNc 1wBpmaEnqjsSX2s+AZSJU0JpspMwVh3T92phzQGB2AoVuw6W8/WeJ8BFvy1mZhBouv TX+Y1BBqcA7AKWjGT0GOPfyEu7yfWzLxKZgqbCFhJ0iaZNoxAlgLB7EU8jYyvM433B n7aEnMV9ADBQw== From: Christian Brauner Date: Mon, 12 May 2025 10:55:26 +0200 Subject: [PATCH v6 7/9] coredump: validate socket name as it is written Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250512-work-coredump-socket-v6-7-c51bc3450727@kernel.org> References: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> In-Reply-To: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> To: linux-fsdevel@vger.kernel.org, Jann Horn , Daniel Borkmann , Kuniyuki Iwashima Cc: Eric Dumazet , Oleg Nesterov , "David S. Miller" , Alexander Viro , Daan De Meyer , David Rheinsberg , Jakub Kicinski , Jan Kara , Lennart Poettering , Luca Boccassi , Mike Yuan , Paolo Abeni , Simon Horman , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-security-module@vger.kernel.org, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1535; i=brauner@kernel.org; h=from:subject:message-id; bh=0BiGaI9QZtmJlstEuxiCKxHauy4PjCyPjPux0hQjYQA=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWQobm9PlXhpHxhTyMm1S3/G9DfizEfMC01542d+Fytlb GiasfhRRykLgxgXg6yYIotDu0m43HKeis1GmRowc1iZQIYwcHEKwETuHWf4X9s90Y9VMnpyy/nI PgURru8RxzpXByRzLhd1aJVrTXdiY/gr+Env2/IVsw/9jVHIn255fPZCGaEDtbM97h/tXhPLEXC OHQA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 In contrast to other parameters written into /proc/sys/kernel/core_pattern that never fail we can validate enabling the new AF_UNIX support. This is obviously racy as hell but it's always been that way. Signed-off-by: Christian Brauner --- fs/coredump.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index deee52bff6bc..5b9d2e063f8f 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -1242,13 +1242,36 @@ void validate_coredump_safety(void) } } =20 +static inline bool check_coredump_socket_address(void) +{ + if (core_pattern[0] !=3D '@') + return true; + + /* Leave enough space for the socket cookie. */ + if (strcspn(core_pattern, " ") >=3D (UNIX_PATH_MAX - sizeof(u64))) + return false; + + return true; +} + static int proc_dostring_coredump(const struct ctl_table *table, int write, void *buffer, size_t *lenp, loff_t *ppos) { - int error =3D proc_dostring(table, write, buffer, lenp, ppos); + int error; + ssize_t retval; + char old_core_pattern[CORENAME_MAX_SIZE]; + + retval =3D strscpy(old_core_pattern, core_pattern, CORENAME_MAX_SIZE); + + error =3D proc_dostring(table, write, buffer, lenp, ppos); + if (error) + return error; + if (!check_coredump_socket_address()) { + strscpy(core_pattern, old_core_pattern, retval + 1); + return -EINVAL; + } =20 - if (!error) - validate_coredump_safety(); + validate_coredump_safety(); return error; } =20 --=20 2.47.2 From nobody Wed Dec 17 08:51:12 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5CC4E25B1C3; Mon, 12 May 2025 08:56:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040184; cv=none; b=RiClOew/NaCL39/IKcnZ9Jl+mZjFtkdjP5Pdi73KXlH4t2Ku1SsZ5wDOVqqUPYlJsPB3SO/TWKfsmFIWTEjCFbhGzb4iZDy0+gWB/8zePP2FzU4RyKe8N8+5D63W0WHJ31KW4qnyHk+8E0xyTv2dyX40AVdvUpk6X38nETmnkTo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040184; c=relaxed/simple; bh=WF46UsPN75VDjMGLBPpKqGhF8yXGNp/YIhfAlM9Ro+U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cTuHae7QAO5XkyKQbH/5Q8w4iqMvgbu9RsD+tcVFRzfXy+balbnez50R/BBvdsDJnnBEf4KlwtNhuI78EHman3ZWvv5hEfi7hr9AirYB78pSCUUPK6yE4qfgUP7irYr4EY6X4YIt8Y5iUVeB3ddCGTwS+JQKF4wzjOsHTNW++iY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=in39wKwr; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="in39wKwr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD283C4CEF1; Mon, 12 May 2025 08:56:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747040183; bh=WF46UsPN75VDjMGLBPpKqGhF8yXGNp/YIhfAlM9Ro+U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=in39wKwrDT6YPk1FpCSb5ZqpqbNdEksHQ4v5hC0tVyawRIKAkSMQSJvB1AXMvDuja tz74DikcY0DqaqKs2TuCBJXfhFw/4ho6VyZVpGiHkCHULlJyGdimVGDKTrfVugXk/H 7CrrN+v6iE9rC4iMlyz5e8Fv/L9crPE7WI+zpoZ2vvIbnaIgNHnqEoZRCEyRGqG0nh QbwoGz0ZW2cQGSxRBsjgUR7lvbLslHQZwl6mk/AhecTQp+aVbMTWtLRYOZY9Ew0BTi uh/pfF61g02L1lPlsyyf/WArEdcA8u5c4FYxZ5B5rhJLtLt8w2Jg9olMIR3/fIrVxq tGvgSMqqes5AA== From: Christian Brauner Date: Mon, 12 May 2025 10:55:27 +0200 Subject: [PATCH v6 8/9] selftests/pidfd: add PIDFD_INFO_COREDUMP infrastructure Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250512-work-coredump-socket-v6-8-c51bc3450727@kernel.org> References: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> In-Reply-To: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> To: linux-fsdevel@vger.kernel.org, Jann Horn , Daniel Borkmann , Kuniyuki Iwashima Cc: Eric Dumazet , Oleg Nesterov , "David S. Miller" , Alexander Viro , Daan De Meyer , David Rheinsberg , Jakub Kicinski , Jan Kara , Lennart Poettering , Luca Boccassi , Mike Yuan , Paolo Abeni , Simon Horman , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-security-module@vger.kernel.org, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1381; i=brauner@kernel.org; h=from:subject:message-id; bh=WF46UsPN75VDjMGLBPpKqGhF8yXGNp/YIhfAlM9Ro+U=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWQobm8XDXLNzXrq1Z3O2mXA9/cxn61Zf4pU2+9Dwrwed y+elGvsKGVhEONikBVTZHFoNwmXW85TsdkoUwNmDisTyBAGLk4BmMjaZkaGnY1iOyOYpAwksvOf /b7w71Dl29DLvC/8FhtJRusLtf6+z/BXaqomf4B85rtvE9wz2c95b790XXL591Xl3g1Zwgt12SZ zAAA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Add PIDFD_INFO_COREDUMP infrastructure so we can use it in tests. Signed-off-by: Christian Brauner --- tools/testing/selftests/pidfd/pidfd.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tools/testing/selftests/pidfd/pidfd.h b/tools/testing/selftest= s/pidfd/pidfd.h index 55bcf81a2b9a..887c74007086 100644 --- a/tools/testing/selftests/pidfd/pidfd.h +++ b/tools/testing/selftests/pidfd/pidfd.h @@ -131,6 +131,26 @@ #define PIDFD_INFO_EXIT (1UL << 3) /* Always returned if available, even= if not requested */ #endif =20 +#ifndef PIDFD_INFO_COREDUMP +#define PIDFD_INFO_COREDUMP (1UL << 4) +#endif + +#ifndef PIDFD_COREDUMPED +#define PIDFD_COREDUMPED (1U << 0) /* Did crash and... */ +#endif + +#ifndef PIDFD_COREDUMP_SKIP +#define PIDFD_COREDUMP_SKIP (1U << 1) /* coredumping generation was skippe= d. */ +#endif + +#ifndef PIDFD_COREDUMP_USER +#define PIDFD_COREDUMP_USER (1U << 2) /* coredump was done as the user. */ +#endif + +#ifndef PIDFD_COREDUMP_ROOT +#define PIDFD_COREDUMP_ROOT (1U << 3) /* coredump was done as root. */ +#endif + #ifndef PIDFD_THREAD #define PIDFD_THREAD O_EXCL #endif @@ -150,6 +170,9 @@ struct pidfd_info { __u32 fsuid; __u32 fsgid; __s32 exit_code; + __u32 coredump_mask; + __u32 __spare1; + __u64 coredump_cookie; }; =20 /* --=20 2.47.2 From nobody Wed Dec 17 08:51:12 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 393EF25A2BA; Mon, 12 May 2025 08:56:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040189; cv=none; b=XMcFrFElDhvTFscUKj0HojrAS/m3YdY1NQDaRSScP88fkjIEqnibaVLxIVUM3RMY8co5bhfP418bc3CjhEzuFLqpfbOlXqUHDMe0bh6hN0JE3dIrE2pBxvtb6sZ8wbmODNh4Bn55XExe5kO9k5fVgYvlJHu2dE/U49PBUgdTubg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747040189; c=relaxed/simple; bh=q/v2mAlkLcg0KsGCqgczqMc5SELp2xv2/mYAT5TDFss=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dhJ9QQmb1cTwP8upfuCWktQwcSityjbxqLOPfMCsdP1mJzTC/1tGeYhUf5bHqYU/7Y/IcjPYaBT1EhC1b2b36WZK2BpIxd1cdwFFsTDOMb3XJ1zBjJMwZ5Xe2TTyXTDVVN2keVk7jUeqsi8TgwJEDYWS+MLq4gUk/PkrAGI3K4E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NodzJqK7; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NodzJqK7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 37483C4CEE9; Mon, 12 May 2025 08:56:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747040189; bh=q/v2mAlkLcg0KsGCqgczqMc5SELp2xv2/mYAT5TDFss=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=NodzJqK7fyaSDTRsaymrYOX+v1fpWq22DMFIVaIstRiYXJXa21WlyVtvLsI/3En4p oHnRw5Vm+NRL/9VmgCSW/cbzuRIgPGoh1uY5ox/n8oWe3c2KRf2dv5BIkG7R4AHxw+ YUu+IH9BOtW3STnAMJTvy/j4oy6OwAKjg7cprzzKAJZ+ZEJAsw+EUZU2rG/hI/vSk6 hlOrtsLzJvEDwxbaXU6+CHnD3SLAJMw+FwMQ9aq41Gbe82zHsywkSxklDSuEloxWJx eOfmizMhfvzupVJUyB0S2TgWhvAwKPFVp9tt9Kd1KKz/BiOwF5061gDAHuV7/QNkii /QHW1Gt9ystmw== From: Christian Brauner Date: Mon, 12 May 2025 10:55:28 +0200 Subject: [PATCH v6 9/9] selftests/coredump: add tests for AF_UNIX coredumps Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250512-work-coredump-socket-v6-9-c51bc3450727@kernel.org> References: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> In-Reply-To: <20250512-work-coredump-socket-v6-0-c51bc3450727@kernel.org> To: linux-fsdevel@vger.kernel.org, Jann Horn , Daniel Borkmann , Kuniyuki Iwashima Cc: Eric Dumazet , Oleg Nesterov , "David S. Miller" , Alexander Viro , Daan De Meyer , David Rheinsberg , Jakub Kicinski , Jan Kara , Lennart Poettering , Luca Boccassi , Mike Yuan , Paolo Abeni , Simon Horman , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-security-module@vger.kernel.org, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=29673; i=brauner@kernel.org; h=from:subject:message-id; bh=q/v2mAlkLcg0KsGCqgczqMc5SELp2xv2/mYAT5TDFss=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWQobm8//rnNuGX/V9dJmRcNHjRFaHSnNVn3qW19cG3Wf c8yD69THaUsDGJcDLJiiiwO7Sbhcst5KjYbZWrAzGFlAhnCwMUpABPRvcDI0GJwunp2jei5i9zr fRqCHoc8Orr+lPBM7/d7r1+NdZTqqGH4H67VE/2ajSFAy1XKdbvcJku/txE/LgY+07jEG+Q1beF FHgA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Add a simple test for generating coredumps via AF_UNIX sockets. Signed-off-by: Christian Brauner --- tools/testing/selftests/coredump/stackdump_test.c | 956 ++++++++++++++++++= +++- 1 file changed, 955 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/coredump/stackdump_test.c b/tools/test= ing/selftests/coredump/stackdump_test.c index fe3c728cd6be..5a74612ae42e 100644 --- a/tools/testing/selftests/coredump/stackdump_test.c +++ b/tools/testing/selftests/coredump/stackdump_test.c @@ -1,14 +1,20 @@ // SPDX-License-Identifier: GPL-2.0 =20 #include +#include #include #include #include #include +#include #include +#include +#include +#include #include =20 #include "../kselftest_harness.h" +#include "../pidfd/pidfd.h" =20 #define STACKDUMP_FILE "stack_values" #define STACKDUMP_SCRIPT "stackdump" @@ -35,6 +41,7 @@ static void crashing_child(void) FIXTURE(coredump) { char original_core_pattern[256]; + pid_t pid_coredump_server; }; =20 FIXTURE_SETUP(coredump) @@ -44,6 +51,7 @@ FIXTURE_SETUP(coredump) char *dir; int ret; =20 + self->pid_coredump_server =3D -ESRCH; file =3D fopen("/proc/sys/kernel/core_pattern", "r"); ASSERT_NE(NULL, file); =20 @@ -61,10 +69,15 @@ FIXTURE_TEARDOWN(coredump) { const char *reason; FILE *file; - int ret; + int ret, status; =20 unlink(STACKDUMP_FILE); =20 + if (self->pid_coredump_server > 0) { + kill(self->pid_coredump_server, SIGTERM); + waitpid(self->pid_coredump_server, &status, 0); + } + file =3D fopen("/proc/sys/kernel/core_pattern", "w"); if (!file) { reason =3D "Unable to open core_pattern"; @@ -154,4 +167,945 @@ TEST_F_TIMEOUT(coredump, stackdump, 120) fclose(file); } =20 +TEST_F(coredump, socket) +{ + int fd, pidfd, ret, status; + FILE *file; + pid_t pid, pid_coredump_server; + struct stat st; + char core_file[PATH_MAX]; + struct pidfd_info info =3D {}; + int ipc_sockets[2]; + char c; + + ASSERT_EQ(unshare(CLONE_NEWNS), 0); + ASSERT_EQ(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL), 0); + ASSERT_EQ(mount(NULL, "/tmp", "tmpfs", 0, NULL), 0); + + ret =3D socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets); + ASSERT_EQ(ret, 0); + + pid_coredump_server =3D fork(); + ASSERT_GE(pid_coredump_server, 0); + if (pid_coredump_server =3D=3D 0) { + int fd_server, fd_coredump, fd_peer_pidfd, fd_core_file; + __u64 peer_cookie, server_cookie; + socklen_t fd_peer_pidfd_len, peer_cookie_len, server_cookie_len; + struct sockaddr_un coredump_sk =3D { + .sun_family =3D AF_UNIX, + }; + size_t coredump_sk_len; + + close(ipc_sockets[0]); + + fd_server =3D socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (fd_server < 0) + _exit(EXIT_FAILURE); + + server_cookie_len =3D sizeof(server_cookie); + ret =3D getsockopt(fd_server, SOL_SOCKET, SO_COOKIE, + &server_cookie, &server_cookie_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve cookie for coredump socket ser= ver\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + coredump_sk_len =3D snprintf(coredump_sk.sun_path, + sizeof(coredump_sk.sun_path), + "@coredump.socket %" PRIu64, server_cookie); + if (coredump_sk_len < 0 || coredump_sk_len >=3D sizeof(coredump_sk.sun_p= ath)) { + fprintf(stderr, "Unable to create coredump socket path\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + file =3D fopen("/proc/sys/kernel/core_pattern", "w"); + if (!file) { + fprintf(stderr, "Unable to open core_pattern\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + ret =3D fprintf(file, "%s", coredump_sk.sun_path); + fclose(file); + if (ret < 0) { + fprintf(stderr, "Unable to write to core_pattern\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + coredump_sk.sun_path[0] =3D '\0'; + memcpy(coredump_sk.sun_path, "\0coredump.socket", sizeof("coredump.socke= t")); + coredump_sk_len =3D offsetof(struct sockaddr_un, sun_path) + sizeof("cor= edump.socket"); + ret =3D bind(fd_server, (const struct sockaddr *)&coredump_sk, coredump_= sk_len); + if (ret < 0) { + fprintf(stderr, "Failed to bind coredump socket\n"); + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + ret =3D listen(fd_server, 1); + if (ret < 0) { + fprintf(stderr, "Failed to listen on coredump socket\n"); + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + if (write_nointr(ipc_sockets[1], "1", 1) < 0) { + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + close(ipc_sockets[1]); + + fd_coredump =3D accept4(fd_server, NULL, NULL, SOCK_CLOEXEC); + if (fd_coredump < 0) { + fprintf(stderr, "Failed to accept coredump socket connection\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + peer_cookie_len =3D sizeof(peer_cookie); + ret =3D getsockopt(fd_coredump, SOL_SOCKET, SO_COOKIE, + &peer_cookie, &peer_cookie_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve cookie for coredump socket con= nection\n"); + close(fd_coredump); + close(fd_server); + _exit(EXIT_FAILURE); + } + + fd_peer_pidfd_len =3D sizeof(fd_peer_pidfd); + ret =3D getsockopt(fd_coredump, SOL_SOCKET, SO_PEERPIDFD, + &fd_peer_pidfd, &fd_peer_pidfd_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve peer pidfd for coredump socket= connection\n"); + close(fd_coredump); + close(fd_server); + _exit(EXIT_FAILURE); + } + + memset(&info, 0, sizeof(info)); + info.mask =3D PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP; + ret =3D ioctl(fd_peer_pidfd, PIDFD_GET_INFO, &info); + if (ret < 0) { + fprintf(stderr, "Failed to retrieve pidfd info from peer pidfd for core= dump socket connection\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (!(info.mask & PIDFD_INFO_COREDUMP)) { + fprintf(stderr, "Missing coredump information from coredumping task\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (!(info.coredump_mask & PIDFD_COREDUMPED)) { + fprintf(stderr, "Received connection from non-coredumping task\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (!info.coredump_cookie) { + fprintf(stderr, "Missing coredump cookie\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (info.coredump_cookie !=3D peer_cookie) { + fprintf(stderr, "Mismatching coredump cookies\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + fd_core_file =3D creat("/tmp/coredump.file", 0644); + if (fd_core_file < 0) { + fprintf(stderr, "Failed to create coredump file\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + for (;;) { + char buffer[4096]; + ssize_t bytes_read, bytes_write; + + bytes_read =3D read(fd_coredump, buffer, sizeof(buffer)); + if (bytes_read < 0) { + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + close(fd_core_file); + _exit(EXIT_FAILURE); + } + + if (bytes_read =3D=3D 0) + break; + + bytes_write =3D write(fd_core_file, buffer, bytes_read); + if (bytes_read !=3D bytes_write) { + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + close(fd_core_file); + _exit(EXIT_FAILURE); + } + } + + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + close(fd_core_file); + _exit(EXIT_SUCCESS); + } + self->pid_coredump_server =3D pid_coredump_server; + + EXPECT_EQ(close(ipc_sockets[1]), 0); + ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1); + EXPECT_EQ(close(ipc_sockets[0]), 0); + + pid =3D fork(); + ASSERT_GE(pid, 0); + if (pid =3D=3D 0) + crashing_child(); + + pidfd =3D sys_pidfd_open(pid, 0); + ASSERT_GE(pidfd, 0); + + waitpid(pid, &status, 0); + ASSERT_TRUE(WIFSIGNALED(status)); + ASSERT_TRUE(WCOREDUMP(status)); + + info.mask =3D PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP; + ASSERT_EQ(ioctl(pidfd, PIDFD_GET_INFO, &info), 0); + ASSERT_GT((info.mask & PIDFD_INFO_COREDUMP), 0); + ASSERT_GT((info.coredump_mask & PIDFD_COREDUMPED), 0); + + waitpid(pid_coredump_server, &status, 0); + self->pid_coredump_server =3D -ESRCH; + ASSERT_TRUE(WIFEXITED(status)); + ASSERT_EQ(WEXITSTATUS(status), 0); + + ASSERT_EQ(stat("/tmp/coredump.file", &st), 0); + ASSERT_GT(st.st_size, 0); + /* + * We should somehow validate the produced core file. + * For now just allow for visual inspection + */ + system("file /tmp/coredump.file"); +} + +TEST_F(coredump, socket_recycled_cookie) +{ + int fd, pidfd, ret, status; + FILE *file; + pid_t pid, pid_coredump_server; + struct stat st; + char core_file[PATH_MAX]; + struct pidfd_info info =3D {}; + int ipc_sockets[2]; + char c; + + ASSERT_EQ(unshare(CLONE_NEWNS), 0); + ASSERT_EQ(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL), 0); + ASSERT_EQ(mount(NULL, "/tmp", "tmpfs", 0, NULL), 0); + + ret =3D socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets); + ASSERT_EQ(ret, 0); + + pid_coredump_server =3D fork(); + ASSERT_GE(pid_coredump_server, 0); + if (pid_coredump_server =3D=3D 0) { + int fd_server, fd_coredump, fd_peer_pidfd, fd_core_file; + __u64 peer_cookie, server_cookie; + socklen_t fd_peer_pidfd_len, peer_cookie_len, server_cookie_len; + struct sockaddr_un coredump_sk =3D { + .sun_family =3D AF_UNIX, + }; + size_t coredump_sk_len; + + close(ipc_sockets[0]); + + fd_server =3D socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (fd_server < 0) + _exit(EXIT_FAILURE); + + server_cookie_len =3D sizeof(server_cookie); + ret =3D getsockopt(fd_server, SOL_SOCKET, SO_COOKIE, + &server_cookie, &server_cookie_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve cookie for coredump socket ser= ver\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + /* Write invalid socket cookie to core_pattern. */ + coredump_sk_len =3D snprintf(coredump_sk.sun_path, + sizeof(coredump_sk.sun_path), + "@coredump.socket %" PRIu64, 0); + if (coredump_sk_len < 0 || coredump_sk_len >=3D sizeof(coredump_sk.sun_p= ath)) { + fprintf(stderr, "Unable to create coredump socket path\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + file =3D fopen("/proc/sys/kernel/core_pattern", "w"); + if (!file) { + fprintf(stderr, "Unable to open core_pattern\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + ret =3D fprintf(file, "%s", coredump_sk.sun_path); + fclose(file); + if (ret < 0) { + fprintf(stderr, "Unable to write to core_pattern\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + coredump_sk.sun_path[0] =3D '\0'; + memcpy(coredump_sk.sun_path, "\0coredump.socket", sizeof("coredump.socke= t")); + coredump_sk_len =3D offsetof(struct sockaddr_un, sun_path) + sizeof("cor= edump.socket"); + ret =3D bind(fd_server, (const struct sockaddr *)&coredump_sk, coredump_= sk_len); + if (ret < 0) { + fprintf(stderr, "Failed to bind coredump socket\n"); + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + ret =3D listen(fd_server, 1); + if (ret < 0) { + fprintf(stderr, "Failed to listen on coredump socket\n"); + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + if (write_nointr(ipc_sockets[1], "1", 1) < 0) { + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + close(ipc_sockets[1]); + + fd_coredump =3D accept4(fd_server, NULL, NULL, SOCK_CLOEXEC); + if (fd_coredump < 0) { + fprintf(stderr, "Failed to accept coredump socket connection\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + peer_cookie_len =3D sizeof(peer_cookie); + ret =3D getsockopt(fd_coredump, SOL_SOCKET, SO_COOKIE, + &peer_cookie, &peer_cookie_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve cookie for coredump socket con= nection\n"); + close(fd_coredump); + close(fd_server); + _exit(EXIT_FAILURE); + } + + fd_peer_pidfd_len =3D sizeof(fd_peer_pidfd); + ret =3D getsockopt(fd_coredump, SOL_SOCKET, SO_PEERPIDFD, + &fd_peer_pidfd, &fd_peer_pidfd_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve peer pidfd for coredump socket= connection\n"); + close(fd_coredump); + close(fd_server); + _exit(EXIT_FAILURE); + } + + memset(&info, 0, sizeof(info)); + info.mask =3D PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP; + ret =3D ioctl(fd_peer_pidfd, PIDFD_GET_INFO, &info); + if (ret < 0) { + fprintf(stderr, "Failed to retrieve pidfd info from peer pidfd for core= dump socket connection\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (!(info.mask & PIDFD_INFO_COREDUMP)) { + fprintf(stderr, "Missing coredump information from coredumping task\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (!(info.coredump_mask & PIDFD_COREDUMPED)) { + fprintf(stderr, "Received connection from non-coredumping task\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (!info.coredump_cookie) { + fprintf(stderr, "Missing coredump cookie\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (info.coredump_cookie !=3D peer_cookie) { + fprintf(stderr, "Mismatching coredump cookies\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + fd_core_file =3D creat("/tmp/coredump.file", 0644); + if (fd_core_file < 0) { + fprintf(stderr, "Failed to create coredump file\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + for (;;) { + char buffer[4096]; + ssize_t bytes_read, bytes_write; + + bytes_read =3D read(fd_coredump, buffer, sizeof(buffer)); + if (bytes_read < 0) { + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + close(fd_core_file); + _exit(EXIT_FAILURE); + } + + if (bytes_read =3D=3D 0) + break; + + bytes_write =3D write(fd_core_file, buffer, bytes_read); + if (bytes_read !=3D bytes_write) { + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + close(fd_core_file); + _exit(EXIT_FAILURE); + } + } + + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + close(fd_core_file); + _exit(EXIT_SUCCESS); + } + self->pid_coredump_server =3D pid_coredump_server; + + EXPECT_EQ(close(ipc_sockets[1]), 0); + ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1); + EXPECT_EQ(close(ipc_sockets[0]), 0); + + pid =3D fork(); + ASSERT_GE(pid, 0); + if (pid =3D=3D 0) + crashing_child(); + + pidfd =3D sys_pidfd_open(pid, 0); + ASSERT_GE(pidfd, 0); + + waitpid(pid, &status, 0); + ASSERT_TRUE(WIFSIGNALED(status)); + ASSERT_FALSE(WCOREDUMP(status)); + + info.mask =3D PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP; + ASSERT_EQ(ioctl(pidfd, PIDFD_GET_INFO, &info), 0); + ASSERT_GT((info.mask & PIDFD_INFO_COREDUMP), 0); + ASSERT_EQ((info.coredump_mask & PIDFD_COREDUMPED), 0); + + ASSERT_EQ(kill(pid_coredump_server, SIGKILL), 0); + waitpid(pid_coredump_server, &status, 0); + self->pid_coredump_server =3D -ESRCH; + ASSERT_TRUE(WIFSIGNALED(status)); + ASSERT_EQ(WTERMSIG(status), SIGKILL); + + ASSERT_NE(stat("/tmp/coredump.file", &st), 0); + ASSERT_EQ(errno, ENOENT); +} + +TEST_F(coredump, socket_missing_cookie) +{ + int fd, pidfd, ret, status; + FILE *file; + pid_t pid, pid_coredump_server; + struct stat st; + char core_file[PATH_MAX]; + struct pidfd_info info =3D {}; + int ipc_sockets[2]; + char c; + + ASSERT_EQ(unshare(CLONE_NEWNS), 0); + ASSERT_EQ(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL), 0); + ASSERT_EQ(mount(NULL, "/tmp", "tmpfs", 0, NULL), 0); + + ret =3D socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets); + ASSERT_EQ(ret, 0); + + pid_coredump_server =3D fork(); + ASSERT_GE(pid_coredump_server, 0); + if (pid_coredump_server =3D=3D 0) { + int fd_server, fd_coredump, fd_peer_pidfd, fd_core_file; + __u64 peer_cookie, server_cookie; + socklen_t fd_peer_pidfd_len, peer_cookie_len, server_cookie_len; + struct sockaddr_un coredump_sk =3D { + .sun_family =3D AF_UNIX, + }; + size_t coredump_sk_len; + + close(ipc_sockets[0]); + + fd_server =3D socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (fd_server < 0) + _exit(EXIT_FAILURE); + + server_cookie_len =3D sizeof(server_cookie); + ret =3D getsockopt(fd_server, SOL_SOCKET, SO_COOKIE, + &server_cookie, &server_cookie_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve cookie for coredump socket ser= ver\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + /* Don't write socket cookie to core_pattern. */ + coredump_sk_len =3D snprintf(coredump_sk.sun_path, + sizeof(coredump_sk.sun_path), + "@coredump.socket"); + if (coredump_sk_len < 0 || coredump_sk_len >=3D sizeof(coredump_sk.sun_p= ath)) { + fprintf(stderr, "Unable to create coredump socket path\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + file =3D fopen("/proc/sys/kernel/core_pattern", "w"); + if (!file) { + fprintf(stderr, "Unable to open core_pattern\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + ret =3D fprintf(file, "%s", coredump_sk.sun_path); + fclose(file); + if (ret < 0) { + fprintf(stderr, "Unable to write to core_pattern\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + coredump_sk.sun_path[0] =3D '\0'; + memcpy(coredump_sk.sun_path, "\0coredump.socket", sizeof("coredump.socke= t")); + coredump_sk_len =3D offsetof(struct sockaddr_un, sun_path) + sizeof("cor= edump.socket"); + ret =3D bind(fd_server, (const struct sockaddr *)&coredump_sk, coredump_= sk_len); + if (ret < 0) { + fprintf(stderr, "Failed to bind coredump socket\n"); + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + ret =3D listen(fd_server, 1); + if (ret < 0) { + fprintf(stderr, "Failed to listen on coredump socket\n"); + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + if (write_nointr(ipc_sockets[1], "1", 1) < 0) { + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + close(ipc_sockets[1]); + + fd_coredump =3D accept4(fd_server, NULL, NULL, SOCK_CLOEXEC); + if (fd_coredump < 0) { + fprintf(stderr, "Failed to accept coredump socket connection\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + peer_cookie_len =3D sizeof(peer_cookie); + ret =3D getsockopt(fd_coredump, SOL_SOCKET, SO_COOKIE, + &peer_cookie, &peer_cookie_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve cookie for coredump socket con= nection\n"); + close(fd_coredump); + close(fd_server); + _exit(EXIT_FAILURE); + } + + fd_peer_pidfd_len =3D sizeof(fd_peer_pidfd); + ret =3D getsockopt(fd_coredump, SOL_SOCKET, SO_PEERPIDFD, + &fd_peer_pidfd, &fd_peer_pidfd_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve peer pidfd for coredump socket= connection\n"); + close(fd_coredump); + close(fd_server); + _exit(EXIT_FAILURE); + } + + memset(&info, 0, sizeof(info)); + info.mask =3D PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP; + ret =3D ioctl(fd_peer_pidfd, PIDFD_GET_INFO, &info); + if (ret < 0) { + fprintf(stderr, "Failed to retrieve pidfd info from peer pidfd for core= dump socket connection\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (!(info.mask & PIDFD_INFO_COREDUMP)) { + fprintf(stderr, "Missing coredump information from coredumping task\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (!(info.coredump_mask & PIDFD_COREDUMPED)) { + fprintf(stderr, "Received connection from non-coredumping task\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (!info.coredump_cookie) { + fprintf(stderr, "Missing coredump cookie\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (info.coredump_cookie !=3D peer_cookie) { + fprintf(stderr, "Mismatching coredump cookies\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + fd_core_file =3D creat("/tmp/coredump.file", 0644); + if (fd_core_file < 0) { + fprintf(stderr, "Failed to create coredump file\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + for (;;) { + char buffer[4096]; + ssize_t bytes_read, bytes_write; + + bytes_read =3D read(fd_coredump, buffer, sizeof(buffer)); + if (bytes_read < 0) { + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + close(fd_core_file); + _exit(EXIT_FAILURE); + } + + if (bytes_read =3D=3D 0) + break; + + bytes_write =3D write(fd_core_file, buffer, bytes_read); + if (bytes_read !=3D bytes_write) { + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + close(fd_core_file); + _exit(EXIT_FAILURE); + } + } + + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + close(fd_core_file); + _exit(EXIT_SUCCESS); + } + self->pid_coredump_server =3D pid_coredump_server; + + EXPECT_EQ(close(ipc_sockets[1]), 0); + ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1); + EXPECT_EQ(close(ipc_sockets[0]), 0); + + pid =3D fork(); + ASSERT_GE(pid, 0); + if (pid =3D=3D 0) + crashing_child(); + + pidfd =3D sys_pidfd_open(pid, 0); + ASSERT_GE(pidfd, 0); + + waitpid(pid, &status, 0); + ASSERT_TRUE(WIFSIGNALED(status)); + ASSERT_FALSE(WCOREDUMP(status)); + + info.mask =3D PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP; + ASSERT_EQ(ioctl(pidfd, PIDFD_GET_INFO, &info), 0); + ASSERT_GT((info.mask & PIDFD_INFO_COREDUMP), 0); + ASSERT_EQ((info.coredump_mask & PIDFD_COREDUMPED), 0); + + ASSERT_EQ(kill(pid_coredump_server, SIGKILL), 0); + waitpid(pid_coredump_server, &status, 0); + self->pid_coredump_server =3D -ESRCH; + ASSERT_TRUE(WIFSIGNALED(status)); + ASSERT_EQ(WTERMSIG(status), SIGKILL); + + ASSERT_NE(stat("/tmp/coredump.file", &st), 0); + ASSERT_EQ(errno, ENOENT); +} + +TEST_F(coredump, socket_detect_userspace_client) +{ + int fd, pidfd, ret, status; + FILE *file; + pid_t pid, pid_coredump_server; + struct stat st; + char core_file[PATH_MAX]; + struct pidfd_info info =3D {}; + int ipc_sockets[2]; + char c; + + ASSERT_EQ(unshare(CLONE_NEWNS), 0); + ASSERT_EQ(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL), 0); + ASSERT_EQ(mount(NULL, "/tmp", "tmpfs", 0, NULL), 0); + + ret =3D socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets); + ASSERT_EQ(ret, 0); + + pid_coredump_server =3D fork(); + ASSERT_GE(pid_coredump_server, 0); + if (pid_coredump_server =3D=3D 0) { + int fd_server, fd_coredump, fd_peer_pidfd, fd_core_file; + __u64 peer_cookie, server_cookie; + socklen_t fd_peer_pidfd_len, peer_cookie_len, server_cookie_len; + struct sockaddr_un coredump_sk =3D { + .sun_family =3D AF_UNIX, + }; + size_t coredump_sk_len; + + close(ipc_sockets[0]); + + fd_server =3D socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (fd_server < 0) + _exit(EXIT_FAILURE); + + server_cookie_len =3D sizeof(server_cookie); + ret =3D getsockopt(fd_server, SOL_SOCKET, SO_COOKIE, + &server_cookie, &server_cookie_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve cookie for coredump socket ser= ver\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + coredump_sk_len =3D snprintf(coredump_sk.sun_path, + sizeof(coredump_sk.sun_path), + "@coredump.socket %" PRIu64, server_cookie); + if (coredump_sk_len < 0 || coredump_sk_len >=3D sizeof(coredump_sk.sun_p= ath)) { + fprintf(stderr, "Unable to create coredump socket path\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + file =3D fopen("/proc/sys/kernel/core_pattern", "w"); + if (!file) { + fprintf(stderr, "Unable to open core_pattern\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + ret =3D fprintf(file, "%s", coredump_sk.sun_path); + fclose(file); + if (ret < 0) { + fprintf(stderr, "Unable to write to core_pattern\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + coredump_sk.sun_path[0] =3D '\0'; + memcpy(coredump_sk.sun_path, "\0coredump.socket", sizeof("coredump.socke= t")); + coredump_sk_len =3D offsetof(struct sockaddr_un, sun_path) + sizeof("cor= edump.socket"); + ret =3D bind(fd_server, (const struct sockaddr *)&coredump_sk, coredump_= sk_len); + if (ret < 0) { + fprintf(stderr, "Failed to bind coredump socket\n"); + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + ret =3D listen(fd_server, 1); + if (ret < 0) { + fprintf(stderr, "Failed to listen on coredump socket\n"); + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + if (write_nointr(ipc_sockets[1], "1", 1) < 0) { + close(fd_server); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + close(ipc_sockets[1]); + + fd_coredump =3D accept4(fd_server, NULL, NULL, SOCK_CLOEXEC); + if (fd_coredump < 0) { + fprintf(stderr, "Failed to accept coredump socket connection\n"); + close(fd_server); + _exit(EXIT_FAILURE); + } + + peer_cookie_len =3D sizeof(peer_cookie); + ret =3D getsockopt(fd_coredump, SOL_SOCKET, SO_COOKIE, + &peer_cookie, &peer_cookie_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve cookie for coredump socket con= nection\n"); + close(fd_coredump); + close(fd_server); + _exit(EXIT_FAILURE); + } + + fd_peer_pidfd_len =3D sizeof(fd_peer_pidfd); + ret =3D getsockopt(fd_coredump, SOL_SOCKET, SO_PEERPIDFD, + &fd_peer_pidfd, &fd_peer_pidfd_len); + if (ret < 0) { + fprintf(stderr, "%m - Failed to retrieve peer pidfd for coredump socket= connection\n"); + close(fd_coredump); + close(fd_server); + _exit(EXIT_FAILURE); + } + + memset(&info, 0, sizeof(info)); + info.mask =3D PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP; + ret =3D ioctl(fd_peer_pidfd, PIDFD_GET_INFO, &info); + if (ret < 0) { + fprintf(stderr, "Failed to retrieve pidfd info from peer pidfd for core= dump socket connection\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (!(info.mask & PIDFD_INFO_COREDUMP)) { + fprintf(stderr, "Missing coredump information from coredumping task\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (info.coredump_mask & PIDFD_COREDUMPED) { + fprintf(stderr, "Received unexpected connection from coredumping task\n= "); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (info.coredump_cookie) { + fprintf(stderr, "Received unexpected coredump cookie\n"); + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + close(fd_coredump); + close(fd_server); + close(fd_peer_pidfd); + close(fd_core_file); + _exit(EXIT_SUCCESS); + } + self->pid_coredump_server =3D pid_coredump_server; + + EXPECT_EQ(close(ipc_sockets[1]), 0); + ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1); + EXPECT_EQ(close(ipc_sockets[0]), 0); + + pid =3D fork(); + ASSERT_GE(pid, 0); + if (pid =3D=3D 0) { + struct sockaddr_un coredump_sk =3D { + .sun_family =3D AF_UNIX, + }; + socklen_t coredump_sk_len; + int fd_socket; + ssize_t ret; + + fd_socket =3D socket(AF_UNIX, SOCK_STREAM, 0); + if (fd_socket < 0) + _exit(EXIT_FAILURE); + + + coredump_sk.sun_path[0] =3D '\0'; + memcpy(coredump_sk.sun_path, "\0coredump.socket", sizeof("coredump.socke= t")); + coredump_sk_len =3D offsetof(struct sockaddr_un, sun_path) + sizeof("cor= edump.socket"); + + ret =3D connect(fd_socket, (const struct sockaddr *)&coredump_sk, coredu= mp_sk_len); + if (ret < 0) + _exit(EXIT_FAILURE); + + (void *)write(fd_socket, &(char){ 0 }, 1); + close(fd_socket); + _exit(EXIT_SUCCESS); + } + + pidfd =3D sys_pidfd_open(pid, 0); + ASSERT_GE(pidfd, 0); + + waitpid(pid, &status, 0); + ASSERT_TRUE(WIFEXITED(status)); + ASSERT_EQ(WEXITSTATUS(status), 0); + + info.mask =3D PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP; + ASSERT_EQ(ioctl(pidfd, PIDFD_GET_INFO, &info), 0); + ASSERT_GT((info.mask & PIDFD_INFO_COREDUMP), 0); + ASSERT_EQ((info.coredump_mask & PIDFD_COREDUMPED), 0); + + waitpid(pid_coredump_server, &status, 0); + self->pid_coredump_server =3D -ESRCH; + ASSERT_TRUE(WIFEXITED(status)); + ASSERT_EQ(WEXITSTATUS(status), 0); + + ASSERT_NE(stat("/tmp/coredump.file", &st), 0); + ASSERT_EQ(errno, ENOENT); +} + TEST_HARNESS_MAIN --=20 2.47.2