From nobody Thu Dec 18 03:54:50 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 42D5928C016; Wed, 7 May 2025 16:13:50 +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=1746634431; cv=none; b=nTogXGQcfvE+ITMIB0APiUSi8E3m7yZWR16gepIN4XHYYGk9XJvaCPnIJP1ZVjMPuDhaBekyEWqZ+9DocdougzK86kpAjDaubsWkkoLDeg10slKHaMDxZ0hBiiT/zYVCHCHw38ZSmk8fJt+lK2iOFFBWWu1GcJbhqYyRmDxekB8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746634431; c=relaxed/simple; bh=d48rlIeb55kv9LHwHtfILNbBpVSvS+cV2//Rf2p6cRQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VqE1gg1TL9FaFzgvreVs3n+EeftuV/mUp1BaJfci2wItj/9rAuACax6IXyOe0wvOKyAh2k7DEJxkUInvcWa6eK9HcQjUFU3jhySM4VVeCK/Bfvevt7NdtalLaPG12SF5bfRsdg8Vl7aKMgxWAnbde6KCsgZIsVkvXAawmeLOELg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GeUbK3MX; 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="GeUbK3MX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88DBCC4CEEE; Wed, 7 May 2025 16:13:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746634430; bh=d48rlIeb55kv9LHwHtfILNbBpVSvS+cV2//Rf2p6cRQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=GeUbK3MXb57Fun9p3AUHSHga6ExOjFIt54ht8tzr09TM3In1UkMtMy33bZSzqYJ/N Dwt2Lr6Ya1mrLegSWf7uzyBjN29xn1p45EhFdYMyBeMADvq6Hqsb4XcdcYITaCxXCR Oz/MV7TXRQivpE0JFahV69estdvmmzNKGXfT4iiwBZFSJL8lm3zz1H0Tm7aJWQ0GFL zopJCxt+dVtS6J3yatI5Bzpkt7E2n0Zm555/w6s1TpoSOmJvQPMBd9VUlKYU9jrkmc wgFpsc1XlDROiCoVsLInNqvEXqMg1l+oDjNTTL8o4bhZftsr138tJeQdLqF8GodEaU lba7lpiparDXQ== From: Christian Brauner Date: Wed, 07 May 2025 18:13:34 +0200 Subject: [PATCH v4 01/11] 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: <20250507-work-coredump-socket-v4-1-af0ef317b2d0@kernel.org> References: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> In-Reply-To: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> To: Kuniyuki Iwashima , linux-fsdevel@vger.kernel.org, Jann Horn 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, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=4656; i=brauner@kernel.org; h=from:subject:message-id; bh=d48rlIeb55kv9LHwHtfILNbBpVSvS+cV2//Rf2p6cRQ=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWRIt211E7gt+pehsXuziatNfvZ+R8e3Ny7/fh0/0bL+J PuN/k69jlIWBjEuBlkxRRaHdpNwueU8FZuNMjVg5rAygQxh4OIUgIlM4WRk2HfLLW2n6toS7tjw 2SZ/M/ni7vz8mLlj9eRHMpNf3k3odWf4K/3mc7TNpozd3VwlJY+2uS0SdX2Zwp2/TeLyTcmvYlc yuAE= 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..281320ea351f 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 Thu Dec 18 03:54:50 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 D0C1E28B7FA; Wed, 7 May 2025 16:13:55 +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=1746634437; cv=none; b=mdEB8TNLpc1OQ81cQ3pkkpnR7MOdAKJI1jNpPJ6Tpo7a+0Ny+uSWI1kMSrsaNAgMvn4PTiDXXDgUQJv22KUE51JEeqx+F7WgYkB8qLWGi8YJlyL4p/XHqtnKu+Rndwas2vKZ6Y/IptexbZe+yNYj8nDBDlgFT2aQY++Sszu0bjw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746634437; c=relaxed/simple; bh=fEpmDQLWLttpCZqCdebailDPLzOF/JIhp5x6zzfJN4U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=vGtbO+GPhb7NRYzzOhXFaak0bocOnH4zZX3oOpoch50Z+JDnEIZcQXpbQP/xDGMIyz1xO2/2yGSybF5Y2JOYbIgyuKmeS/XfmxC2FQ98yypld5pTaNIgAhFkKuT4Tx2FiPfwKbMuSCE40xNw0+GU+YMM2OBZp+cKlCUWlQIh5GU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=T9wb1YsN; 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="T9wb1YsN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D5E5C4CEE2; Wed, 7 May 2025 16:13:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746634435; bh=fEpmDQLWLttpCZqCdebailDPLzOF/JIhp5x6zzfJN4U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=T9wb1YsNd+CmcorZNNFfVSCWuLnbYPxwlk8NpmGEsCiIVKGlt+DuCNeE04DzOssDz UpWWy027Q6rjh0OQktjX7Uf00Po9Wwt6hGrlcpY0Sx6mcAgfxvz/KUWX/5135H1kpc FGmb7+B1ke4feR07wafyumYuKuCB0r6V7rUYj6ulPSM2nCdBJqjDDSLg4IlzJFnInj 4uzGLpd9FbY0hUfvokj7WCIdAIbOPvwWHxKPi51JWUbXviCx5dzGtN/ZDpRhRn+JVT tZIjurJtihDBnBeh62C23jn9aOKFOUX7AFbuNmJ4/0tzi5UD4/+vYh7giPe/8bf9CJ dIMNAJWdmvNmg== From: Christian Brauner Date: Wed, 07 May 2025 18:13:35 +0200 Subject: [PATCH v4 02/11] 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: <20250507-work-coredump-socket-v4-2-af0ef317b2d0@kernel.org> References: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> In-Reply-To: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> To: Kuniyuki Iwashima , linux-fsdevel@vger.kernel.org, Jann Horn 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, 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=fEpmDQLWLttpCZqCdebailDPLzOF/JIhp5x6zzfJN4U=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWRIt219cMiUxez4DjNHwS3du7307jLXZVUVuR6509eWs iedf691RykLgxgXg6yYIotDu0m43HKeis1GmRowc1iZQIYwcHEKwETybjMy/D+geDvhh8lF5U6n FL4n0tKtO+4mu3zmWP9rW1zblUrD2Qx/RXqedq9065vJJefZvrXeJ+qoeuqfJg/9qMybuoEfT/b xAQA= 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 281320ea351f..41491dbfafdf 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 Thu Dec 18 03:54:50 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 56DFD28C86A; Wed, 7 May 2025 16:13:59 +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=1746634440; cv=none; b=MTcrIcIjDnA5F9FJKl16zO1sKPHunrlbFDQfjL3KhDN3sZwNYUJOYAu9zBSI5HxwUFmfIsjPdI6UnhsoT12LaK5xtmkRZOhhwY0aIMZ5quuvgSVAdnMxsxFfOvB6lT8InxcEnGpXbxKQkKFmkZHV/HeSPTGAncJan0mCA43wbYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746634440; c=relaxed/simple; bh=/Y2E1rjGOsMzeWWjGsal4y5faQxb4D6kQkcZ+mAHVGY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZW77zqv8TUSDraow5UD0yergJvYE0jXOUkUFUOwfwQBzs7O6r1Tw9/vhL5uixsKktQmHD4Rk87gQ0ocYwxmsW9dcMyQWOq+Eq7DlUa0cr6DdRLoKn3HW7dlHpS93vDzW7EhhaWbLLue3lW2nAcrQdm7CgHkojpk8j7rrLZe9pgM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZOcEQHlT; 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="ZOcEQHlT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B55B6C4CEEB; Wed, 7 May 2025 16:13:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746634439; bh=/Y2E1rjGOsMzeWWjGsal4y5faQxb4D6kQkcZ+mAHVGY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ZOcEQHlTSBJ57/R/gnonojDb0eoIlgMszVLsjJdAwU0LCVxfmSAiW/cHAh7Jx5JWz 1JScWCdEAKeV2925F2CFhdEIz1GfgF5zl1nG2neF/6qSDnKs4SKJCXb47YV8K2JMIM /9rdEVpPFilKSZYufGBrBd3XKduKzSQsbysn4MFDY/WDBPVLpe2jU2xx3ElGS4nExf LoVoB8XtpljduPfazX9hYd8JesZkejBOd/s6QFie+IS8A17hkNwQQYxiuVJsALaPbH joRCnkAAP3C0aYIxq1QkPXhfqHiXLQg52wuHWZqBYc8K7Wv/t5Mk043RFfXqzk26h0 d1uSIxhniVKGw== From: Christian Brauner Date: Wed, 07 May 2025 18:13:36 +0200 Subject: [PATCH v4 03/11] 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: <20250507-work-coredump-socket-v4-3-af0ef317b2d0@kernel.org> References: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> In-Reply-To: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> To: Kuniyuki Iwashima , linux-fsdevel@vger.kernel.org, Jann Horn 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, 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=/Y2E1rjGOsMzeWWjGsal4y5faQxb4D6kQkcZ+mAHVGY=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWRIt20t+q3iltlgu3sLu/Iu15nrZWt/8m8Knfo6vWYZt 8HeMyE6HaUsDGJcDLJiiiwO7Sbhcst5KjYbZWrAzGFlAhnCwMUpABPJrGD4pzhJWPnC1n0h0nYf pl6qezXHzM53ksB9O8V4WeuIzATxw4wM29JT7xwvmqj+4oVjf+18zea/Hv86s6ucd6/Q0s6R8lj HBQA= 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 41491dbfafdf..b2eda7b176e4 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 Thu Dec 18 03:54:50 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 B872B28B7FD; Wed, 7 May 2025 16:14:04 +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=1746634444; cv=none; b=DZmg4Qdvyq8zc+QphwJpO+HR4yUAQwKejvMJIJZg05bQ8KFLsIcLefbZ1kt9c5Fu4GIQvvYgcWdR2lyKg7/QDP1uFMIvmnXwGkKaM0UHxEm6zbEucH/HSqBNsRzkTLRB7pWcZwYwxbU4TTSpx/MSDjWhdmU9jLnHYXxOPDfDf+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746634444; c=relaxed/simple; bh=6J3+jrH8mLatCxA+26NtJ7WY3BN4ZJ2WICN0zEMs5IE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IxxFBpdcqP/Cfx2c1e9MRYRyXUDelSFV+Dr5ybjAaxniLhOiH+2ihNovyD8AvAHCVL3ZLal550Uaf13oP7p1Tca5HU/d7epPWlqLPX14ynstVkdrLSMS34j/tUSUNTnNQqlTeLRSzk16Gw9HVtJsxWBYNceov/yZNALb+53IwcQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=o2QrKJDu; 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="o2QrKJDu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CF4AC4CEE2; Wed, 7 May 2025 16:14:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746634444; bh=6J3+jrH8mLatCxA+26NtJ7WY3BN4ZJ2WICN0zEMs5IE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=o2QrKJDuR/ZGZkE/LfJdLcIIS/yYUihQhuNTFg1fw55MATOaDR8PpJ0oT/B28/XSa gOBoMZWAGblA+Sx2z92HTcJ9G8/YIq0mD7OiiA2oEuUHKXlpj1vfcobf/3va2cSe01 9MIBg6eAvTnQygya/vNThN3PMpU9TcsVyZO9accKgFUHEfIGZ0ywD7M/5QV3C/bDrw 3F927yjPfVHdGpPmSP1YkbLNzOlQYTFERIrUkgYBZ5HB02i5pVBftkMopf98erf/nr 686U8WsIZgoTUP7VCJuqrAIZu0IzzPK+ZWgxvYhUNPbP+QWyYcxBvFXWR7ZS7T+/d1 mw8tplQHM1XWQ== From: Christian Brauner Date: Wed, 07 May 2025 18:13:37 +0200 Subject: [PATCH v4 04/11] net: reserve prefix 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: <20250507-work-coredump-socket-v4-4-af0ef317b2d0@kernel.org> References: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> In-Reply-To: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> To: Kuniyuki Iwashima , linux-fsdevel@vger.kernel.org, Jann Horn 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, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=4000; i=brauner@kernel.org; h=from:subject:message-id; bh=6J3+jrH8mLatCxA+26NtJ7WY3BN4ZJ2WICN0zEMs5IE=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWRIt2394+H4NHb1vOXct9kWnu++5NtrouDsvL/X8PwHq 4ufF/Ws6ShlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZhIcRTDH15Ne2e1idPfhvMV a/Ynffx1bVKYL9OHn4sOaf/8Nm32Sm6G/4WavWJNrEvf9/cFT9yhJ775wu5EocmF2VeMJ/IJ6ab x8QIA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Add the reserved "linuxafsk/" prefix for AF_UNIX sockets and require CAP_NET_ADMIN in the owning user namespace of the network namespace to bind it. This will be used in next patches to support the coredump socket but is a generally useful concept. The collision risk is so low that we can just start using it. Userspace must already be prepared to retry if a given abstract address isn't usable anyway. Signed-off-by: Christian Brauner --- include/uapi/linux/un.h | 2 ++ net/unix/af_unix.c | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/un.h b/include/uapi/linux/un.h index 0ad59dc8b686..bbd5ad508dfa 100644 --- a/include/uapi/linux/un.h +++ b/include/uapi/linux/un.h @@ -5,6 +5,8 @@ #include =20 #define UNIX_PATH_MAX 108 +/* reserved AF_UNIX socket namespace. */ +#define UNIX_SOCKET_NAMESPACE "linuxafsk/" =20 struct sockaddr_un { __kernel_sa_family_t sun_family; /* AF_UNIX */ diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 472f8aa9ea15..148d008862e7 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -114,6 +114,13 @@ static atomic_long_t unix_nr_socks; static struct hlist_head bsd_socket_buckets[UNIX_HASH_SIZE / 2]; static spinlock_t bsd_socket_locks[UNIX_HASH_SIZE / 2]; =20 +static const struct sockaddr_un linuxafsk_addr =3D { + .sun_family =3D AF_UNIX, + .sun_path =3D "\0"UNIX_SOCKET_NAMESPACE, +}; + +#define UNIX_SOCKET_NAMESPACE_ADDR_LEN (offsetof(struct sockaddr_un, sun_p= ath) + sizeof(UNIX_SOCKET_NAMESPACE)) + /* SMP locking strategy: * hash table is protected with spinlock. * each socket state is protected by separate spinlock. @@ -436,6 +443,30 @@ static struct sock *__unix_find_socket_byname(struct n= et *net, return NULL; } =20 +static int unix_may_bind_name(struct net *net, struct sockaddr_un *sunname, + int len, unsigned int hash) +{ + struct sock *s; + + s =3D __unix_find_socket_byname(net, sunname, len, hash); + if (s) + return -EADDRINUSE; + + /* + * Check whether this is our reserved prefix and if so ensure + * that only privileged processes can bind it. + */ + if (UNIX_SOCKET_NAMESPACE_ADDR_LEN <=3D len && + !memcmp(&linuxafsk_addr, sunname, UNIX_SOCKET_NAMESPACE_ADDR_LEN)) { + /* Don't bind the namespace itself. */ + if (UNIX_SOCKET_NAMESPACE_ADDR_LEN =3D=3D len) + return -ECONNREFUSED; + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) + return -ECONNREFUSED; + } + return 0; +} + static inline struct sock *unix_find_socket_byname(struct net *net, struct sockaddr_un *sunname, int len, unsigned int hash) @@ -1258,10 +1289,10 @@ static int unix_autobind(struct sock *sk) new_hash =3D unix_abstract_hash(addr->name, addr->len, sk->sk_type); unix_table_double_lock(net, old_hash, new_hash); =20 - if (__unix_find_socket_byname(net, addr->name, addr->len, new_hash)) { + if (unix_may_bind_name(net, addr->name, addr->len, new_hash)) { unix_table_double_unlock(net, old_hash, new_hash); =20 - /* __unix_find_socket_byname() may take long time if many names + /* unix_may_bind_name() may take long time if many names * are already in use. */ cond_resched(); @@ -1379,7 +1410,8 @@ static int unix_bind_abstract(struct sock *sk, struct= sockaddr_un *sunaddr, new_hash =3D unix_abstract_hash(addr->name, addr->len, sk->sk_type); unix_table_double_lock(net, old_hash, new_hash); =20 - if (__unix_find_socket_byname(net, addr->name, addr->len, new_hash)) + err =3D unix_may_bind_name(net, addr->name, addr->len, new_hash); + if (err) goto out_spin; =20 __unix_set_addr_hash(net, sk, addr, new_hash); @@ -1389,7 +1421,6 @@ static int unix_bind_abstract(struct sock *sk, struct= sockaddr_un *sunaddr, =20 out_spin: unix_table_double_unlock(net, old_hash, new_hash); - err =3D -EADDRINUSE; out_mutex: mutex_unlock(&u->bindlock); out: --=20 2.47.2 From nobody Thu Dec 18 03:54:50 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 2223F28C01B; Wed, 7 May 2025 16:14:09 +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=1746634449; cv=none; b=kW3n5HFyWCw1PFE/LKrbFfKPOWTmZOVChTwBersP+10m8Skr+AH1VRmjCFEIawKJOKx0rszHxqFj1hWH8q2KzwEWoGkDzB0Fb3C3qfo3BRpYVkmGSvf2ZZJfZnleBOK5Y1iC2GQh5jmKcW8WOm79Gdh39xgoDxzYR/FsblWalbg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746634449; c=relaxed/simple; bh=0RLUTzuUFlIkCKVPUuX4brkaHnhZiXmkvL7Waxw0x2M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cE7AVs7uC5q8xkp4wsepSmLKpWUSR1PmjVUzsYMT0So/eDb0ASRoMklfxmSnf1S3iopTfulZO8gQnn5k+KccN0wvS+GznPV3ysg5qZHXJDpqySwqun4YyB5eSDZSOVtcdhcLod3IXGhGrgzIcCQU0RoefZjRaWgQwo/JE+BZklE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Y3UKAIzT; 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="Y3UKAIzT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7CEDC4CEE9; Wed, 7 May 2025 16:14:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746634449; bh=0RLUTzuUFlIkCKVPUuX4brkaHnhZiXmkvL7Waxw0x2M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Y3UKAIzT6yJI+TxWIv7tI87+xSZwvZ/GfGi6YM0Zr1nDLoJJgpKPIImfXJzr9h1KZ p0OCD5iDFpl8FWzs6zm63Q3uPyagclu9FTu4ALMsbofe4qbPQAZp59mcwDhG+SEN0M KxH6scjOSZXxW2KeIAEtzDBGJuQAbstXMqjqwdmk6xIqIkOTP3k26DhsO87A9DUL+Z bROITB/QJMwfvmcWasDOPH3THCXc/2MjNYWuRY0pPHx1ZI5SSZrid/WGNOVaacrXzY lONKYWXHYMDONQpuxSsQyC0uMqDCsit13PJkgNekZPzVQfkWmibjPBGmHgzZIYBheG KNexysVdSTWdg== From: Christian Brauner Date: Wed, 07 May 2025 18:13:38 +0200 Subject: [PATCH v4 05/11] 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: <20250507-work-coredump-socket-v4-5-af0ef317b2d0@kernel.org> References: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> In-Reply-To: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> To: Kuniyuki Iwashima , linux-fsdevel@vger.kernel.org, Jann Horn 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, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=12783; i=brauner@kernel.org; h=from:subject:message-id; bh=0RLUTzuUFlIkCKVPUuX4brkaHnhZiXmkvL7Waxw0x2M=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWRIt22dnRe5Obs8OO8566f1htd1JZQFIxlnZlr2v51je /fvXe7zHaUsDGJcDLJiiiwO7Sbhcst5KjYbZWrAzGFlAhnCwMUpABPZup7hn5LMf4HEp/GzTp0x mqbbnVQ7qd3Q08GU76OZ78+5aV/yLBl+Mrr4vDz9OPFwp7Ga2G//6+vr5GLeHGQJnfww1ubNvcv XWQA= 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: @linuxafsk/coredump_socket The "@" at the beginning indicates to the kernel that the abstract AF_UNIX coredump socket will be used to process coredumps. The coredump socket uses the fixed address "linuxafsk/coredump.socket" for now. The coredump socket is located in the initial network namespace. To bind the coredump socket userspace must hold CAP_SYS_ADMIN in the initial user namespace. Listening and reading can happen from whatever unprivileged context is necessary to safely process coredumps. When a task coredumps it opens a client socket in the initial network namespace and connects to the coredump socket. - The coredump server should use 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 socket directly. - The pidfd for the crashing task will contain information how the task coredumps. The PIDFD_GET_INFO ioctl gained a new flag PIDFD_INFO_COREDUMP which can be used to retreive the coredump information. If the coredump gets a new coredump client connection the kernel guarantees that PIDFD_INFO_COREDUMP information is available. Currently the following information is provided in the new @coredump_mask extension to struct pidfd_info: * 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. * IDFD_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. - Since unix_stream_connect() runs bpf programs during connect it's possible to even redirect or multiplex coredumps to other sockets. - The coredump server should mark itself as non-dumpable. To capture coredumps for the coredump server itself a bpf program should be run at connect to redirect it to another socket in userspace. This can be useful for debugging crashing coredump servers. - A container coredump server in a separate network namespace can simply bind to linuxafsk/coredump.socket and systemd-coredump fowards coredumps to the container. - Fwiw, one idea is to handle coredumps via per-user/session coredump servers that run 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. 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. 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 just keep a worker pool. This is easy to test: (a) coredump processing (we're using socat): > cat coredump_socket.sh #!/bin/bash set -x sudo bash -c "echo '@linuxafsk/coredump.socket' > /proc/sys/kernel/core= _pattern" sudo socat --statistics abstract-listen:linuxafsk/coredump.socket,fork = FILE:core_file,create,append,trunc (b) trigger a coredump: user1@localhost:~/data/scripts$ cat crash.c #include #include int main(int argc, char *argv[]) { fprintf(stderr, "%u\n", (1 / 0)); _exit(0); } Signed-off-by: Christian Brauner --- fs/coredump.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++= ---- 1 file changed, 128 insertions(+), 8 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index b2eda7b176e4..d61e15d855d2 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,34 @@ static int format_corename(struct core_name *cn, struc= t coredump_params *cprm, ++pat_ptr; if (!(*pat_ptr)) return -ENOMEM; + break; + } + case COREDUMP_SOCK: { + err =3D cn_printf(cn, "%s", pat_ptr); + if (err) + return err; + + /* + * We can potentially allow this to be changed later but + * I currently see no reason to. + */ + if (strcmp(cn->corename, "@linuxafsk/coredump.socket")) + return -EINVAL; + + /* + * 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 +429,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 @@ -583,6 +628,17 @@ static int umh_coredump_setup(struct subprocess_info *= info, struct cred *new) return 0; } =20 +#ifdef CONFIG_UNIX +static const struct sockaddr_un coredump_unix_socket =3D { + .sun_family =3D AF_UNIX, + .sun_path =3D "\0linuxafsk/coredump.socket", +}; +/* Without trailing NUL byte. */ +#define COREDUMP_UNIX_SOCKET_ADDR_SIZE \ + (offsetof(struct sockaddr_un, sun_path) + \ + sizeof("\0linuxafsk/coredump.socket") - 1) +#endif + void do_coredump(const kernel_siginfo_t *siginfo) { struct core_state core_state; @@ -801,6 +857,45 @@ void do_coredump(const kernel_siginfo_t *siginfo) } break; } + case COREDUMP_SOCK: { +#ifdef CONFIG_UNIX + struct file *file __free(fput) =3D NULL; + struct socket *socket; + + /* + * 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 *)(&coredump_unix_socket), + COREDUMP_UNIX_SOCKET_ADDR_SIZE, O_NONBLOCK); + 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 +933,33 @@ 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: { + char buf[1]; + /* + * 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, buf, 1, NULL); + break; + } + default: + break; + } + } + close_fail: if (cprm.file) filp_close(cprm.file, NULL); @@ -1069,7 +1189,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. " --=20 2.47.2 From nobody Thu Dec 18 03:54:50 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 BADB428DB5A; Wed, 7 May 2025 16:14:13 +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=1746634453; cv=none; b=R7Hrj5ahkBh2sXSk/8HuYEfNW50Drn0qGChwR5pu8qxgaayYSdHPcgrR3/RPwzAHiJ+muRi4wWAKc1MVz99tGKX4IuueASQOHy6cpD+noZ9erEpvbP4wLwhdH1C6EzILFOhLYhh0f6bwvnBpWy7A0vMo/v+oiKF1c9l2vAeFLtk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746634453; c=relaxed/simple; bh=zXr47MrQD+Vjun97zqt40Q7EjbQ2mE5MbGB6SQwusWw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=j5NYp/p4JEGM1NgU4fQuWTSn2aOpjIyP8M5O1GquHuClhZDe9N8XM0uSchLwVTSl2NvRdDMrhzq6/Hanhin6e0sDNL5sYaQqkLhlMjYAzKZlEmQo9wCjJIyMBcDD+QhnVl7jQyRJFX0buwjE/dIo2xkD2b9o9D8JK9LZMY7LyQE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=i+kH5eYe; 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="i+kH5eYe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67B23C4CEF0; Wed, 7 May 2025 16:14:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746634453; bh=zXr47MrQD+Vjun97zqt40Q7EjbQ2mE5MbGB6SQwusWw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=i+kH5eYem4nf1I3H3VgsaR4HySrOCoUzNfxThdOofk56Z1ASNkpmpQwkN+OoCUdEI 8YQcFnlFlRRAdMGGJlN32P4nD2KDJt71Gn6ExovEiCqrkUNdKiCgpt2W7LRwZSvJ7u Xsppg0qGbkKGhDjl0J+i4pq/lE3b4a+gi5c2ektEqszXdZAJNdgOjb1GLOSZ7qvp+T vTWaZ51rUdBMMw/cLFd7HwRjWq7m4AIAjh8BnDYHbagZvWj25bxptnVpUjkIOp3YIP v9zOZU+K9onLOKMvfOiq5VyIqtRgGJkjXhXN+MfYpnO+yVy7TDChUOT7UazXPkHxxM c5n8klxGGFTAw== From: Christian Brauner Date: Wed, 07 May 2025 18:13:39 +0200 Subject: [PATCH v4 06/11] 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: <20250507-work-coredump-socket-v4-6-af0ef317b2d0@kernel.org> References: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> In-Reply-To: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> To: Kuniyuki Iwashima , linux-fsdevel@vger.kernel.org, Jann Horn 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, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1280; i=brauner@kernel.org; h=from:subject:message-id; bh=zXr47MrQD+Vjun97zqt40Q7EjbQ2mE5MbGB6SQwusWw=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWRIt219u/fdS+9ViW8m/szMu/BOKPT1TJeiNY+y14rdm ZCWtJK9sKOUhUGMi0FWTJHFod0kXG45T8Vmo0wNmDmsTCBDGLg4BWAiiZUM/1M4FqwzMDG9JrLb 3Iztz2TbZQatX/RmL+V24z4fK7xu/XuGf9asZ8UXzj+cmKYpZbV6d+b0afHZ3k16hZHBnAKaso3 MTAA= 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 | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index d61e15d855d2..0f00f77be988 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -1200,10 +1200,21 @@ void validate_coredump_safety(void) 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]; =20 - if (!error) - validate_coredump_safety(); + 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 (core_pattern[0] =3D=3D '@' && strcmp(core_pattern, "@linuxafsk/coredu= mp.socket")) { + strscpy(core_pattern, old_core_pattern, retval + 1); + return -EINVAL; + } + + validate_coredump_safety(); return error; } =20 --=20 2.47.2 From nobody Thu Dec 18 03:54:50 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 9877E28C2A2; Wed, 7 May 2025 16:14: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=1746634458; cv=none; b=KS9GQCVyG0WxlJNONCF7wc2kqpLy/G7IZbsz/2P/x+7j3/orA750917PgUcXcPFXMDTAHaf7gaszPZzUH+gRiN2kGU6fgIcsOEdoDtFoWMqPeSN6ZFnMIle52V0sif/SqNyNWXqFAnfi0XpdQeIZPkafHchgxxoTHhNSEP8VDnk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746634458; c=relaxed/simple; bh=6nqvwh3i7pYcCmLUo6yNb/a7vH5qP7uGXblBD1xyfls=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kDaBq7uqRnkoVBbYmuK7jubg4sZp5pHCQoPc2XH6w/7vO1Q9Py95w1saUNIabYf1fe7ilsFQ+oyLWWYHPcbKznIkCw/PCnJJN7FpJEcOqYB9ZgENTUHN/KJdT+bOOKDdGujb4Y6HqeYWf2eFNgKMPc68N8xAyIZzw+0r62oJhpo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E7MJjfZj; 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="E7MJjfZj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 15883C4CEE9; Wed, 7 May 2025 16:14:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746634458; bh=6nqvwh3i7pYcCmLUo6yNb/a7vH5qP7uGXblBD1xyfls=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=E7MJjfZj2G9psWsfoa0aPSW9JH5YpD7kwjtfcqAJ0StXJXGbDQnaHtbWd1MRrhlLT r7lrLrOMyzvctTNYdF+MF6xGoms/IYvKA95I5hprvaHcCOLMWBdU+XZ3I8rqhzQUJO 4S+E4pEQuZaswwIP3fqrG4mZfJg3NiGBEAGOmJpmezAi6BIyQB7RgsRL9C8hc1RgBr KCv6d8Ntcrn8IF8M7HwF2An1dECuICrH0x8Lo1Jgp74ll9XBgFY4sHuUs67TcCxIrl j3aMEoTOLhP1W4T1wuYV6HrWfxzhT3ZDfxcAtEhFeKUj1P+pWCR3w7kzKXccGIjHOt a32YsRAUKSlzw== From: Christian Brauner Date: Wed, 07 May 2025 18:13:40 +0200 Subject: [PATCH v4 07/11] 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: <20250507-work-coredump-socket-v4-7-af0ef317b2d0@kernel.org> References: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> In-Reply-To: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> To: Kuniyuki Iwashima , linux-fsdevel@vger.kernel.org, Jann Horn 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, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1139; i=brauner@kernel.org; h=from:subject:message-id; bh=6nqvwh3i7pYcCmLUo6yNb/a7vH5qP7uGXblBD1xyfls=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWRIt21lecp9yPXGI9FQh/2GrRWHmZcrsQVol2mWcLs0r nVxNmztKGVhEONikBVTZHFoNwmXW85TsdkoUwNmDisTyBAGLk4BmAjXK0aGM7eCgq+u/+AS8Uta Yc279EabJW+WFIuEL3vL5rDVQGbvQkaGl6rGkUsE87evevdKpuXIYj4pn0DtsMu7tY+ttOhZ6N/ LAAA= 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 0f00f77be988..e1e6f02e0ed7 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -1220,6 +1220,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 + "\nlinuxafsk/coredump.socket" +#endif +}; =20 static const struct ctl_table coredump_sysctls[] =3D { { @@ -1263,6 +1269,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 Thu Dec 18 03:54:50 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 377DD28C2A2; Wed, 7 May 2025 16:14: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=1746634463; cv=none; b=QNJrYm8FPFWpHlWTHFBzVvZeXRhJnGI5c9beUA1YWhPBybNMJPpOo14YLwkRC7rCD358iaslZAC4l4BilUs6w5odRoUhpnKjmBFKrXsqRR5oDJ+K3s+qp52jf+MmPRIvkcma9lii2hWZviD4sH7RBiOkbWB9obsB6lCuJkKyafg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746634463; c=relaxed/simple; bh=NYTbVvw3ZuT5alPL+WDMuYmSB/1HpQbdNb1rA9c0OX8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ss1xYlJ71wV27aHjLMRfcvyESt0GYUm9Rv7sQVM6Kl6X/iZcpchTr5rsvgHyE1GCfr5F5tEqil24fp8Pihq0702HDbyprQY+BBNBjSmXw2q9rNBgyQJZcA5gFyf2o8v+cRuJfjwbTVgr2/YL5kd1vro9tcp1F/NbKoQABISISrw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WxNHRlyv; 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="WxNHRlyv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9126C4CEE2; Wed, 7 May 2025 16:14:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746634463; bh=NYTbVvw3ZuT5alPL+WDMuYmSB/1HpQbdNb1rA9c0OX8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=WxNHRlyvdLfI1FNQKDABcH2YogdaieciSKxk2VORAZPcYfrpR+Vtw/kgCv/SVo/lJ 1PdxHIQclsv+zv1MEpbRC3TPmqv9ohC+BC+sAjKpmlSR1KBspD2iZ5pZ6MB15rRePQ OgRfFLPO26LIM2RBhyNLNWcdd3xPHz8q9Fd8vS0rp3Sym4SddNoaCzDFhYSHhJAzwO Uz/u1EXotf9eHHGbPQdnFs+xho3ttkiraXAUKbHww7B+LFXSfgDX0hrE9t6DfqbAY9 e9hZq5afYoK8tVvpKcsQ1EbjPD8QD8sinDCWrwScdNmk48rG60+3HHJabldi+2ebhe fPXlsdoEI7i5w== From: Christian Brauner Date: Wed, 07 May 2025 18:13:41 +0200 Subject: [PATCH v4 08/11] 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: <20250507-work-coredump-socket-v4-8-af0ef317b2d0@kernel.org> References: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> In-Reply-To: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> To: Kuniyuki Iwashima , linux-fsdevel@vger.kernel.org, Jann Horn 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, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=7883; i=brauner@kernel.org; h=from:subject:message-id; bh=NYTbVvw3ZuT5alPL+WDMuYmSB/1HpQbdNb1rA9c0OX8=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWRIt20N6z45ITHngq3pIimjXXdFzRhzEqcdnHdh8/nwc zs3Z29y6ihlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZjIiUZGhtsfDa8ulX237lNu 7XSX9J/9L34tnjRNQFSmJ/jXiZRls/0ZGf7fe8o/UWCrW5nEDFOfAPlV1S0b5Xw9ne5rNCYzds9 4wQQA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Let userspace know that the task coredumped and whether it was dumped as root or as regular user. The latter is needed so that access permissions to the executable are correctly handled. I don't think this requires any additional privileges checks. The missing exposure of the dumpability attribute of a given task is an issue we should fix given that we already expose whether a task is coredumping or not. Signed-off-by: Christian Brauner --- fs/coredump.c | 36 ++++++++++++++++++++++++++++++ fs/pidfs.c | 55 ++++++++++++++++++++++++++++++++++++++++++= ++++ include/linux/pidfs.h | 3 +++ include/uapi/linux/pidfd.h | 16 ++++++++++++++ 4 files changed, 110 insertions(+) diff --git a/fs/coredump.c b/fs/coredump.c index e1e6f02e0ed7..ddff1854988f 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -46,7 +46,9 @@ #include #include #include +#include #include +#include #include #include =20 @@ -599,6 +601,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 @@ -879,15 +883,47 @@ 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 *)(&coredump_unix_socket), COREDUMP_UNIX_SOCKET_ADDR_SIZE, O_NONBLOCK); + + /* + * ... 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..8c4d83fb115b 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,7 @@ static struct kmem_cache *pidfs_cachep __ro_after_init; struct pidfs_exit_info { __u64 cgroupid; __s32 exit_code; + __u32 coredump_mask; }; =20 struct pidfs_inode { @@ -240,6 +242,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 +298,11 @@ 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; + kinfo.coredump_mask =3D READ_ONCE(pidfs_i(inode)->__pei.coredump_mask); + } + task =3D get_pid_task(pid, PIDTYPE_PID); if (!task) { /* @@ -296,6 +319,13 @@ 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) + 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 +589,31 @@ void pidfs_exit(struct task_struct *tsk) } } =20 +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 stashed_dentry_get(&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); + /* Fwiw, this cannot be the last reference. */ + dput(dentry); +} + static struct vfsmount *pidfs_mnt __ro_after_init; =20 /* diff --git a/include/linux/pidfs.h b/include/linux/pidfs.h index 2676890c4d0d..f7729b9371bc 100644 --- a/include/linux/pidfs.h +++ b/include/linux/pidfs.h @@ -2,11 +2,14 @@ #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); +void pidfs_coredump(const struct coredump_params *cprm); 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..84ac709f560c 100644 --- a/include/uapi/linux/pidfd.h +++ b/include/uapi/linux/pidfd.h @@ -25,9 +25,23 @@ #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_SUID_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. + */ +#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 +106,8 @@ struct pidfd_info { __u32 fsuid; __u32 fsgid; __s32 exit_code; + __u32 coredump_mask; + __u32 __spare1; }; =20 #define PIDFS_IOCTL_MAGIC 0xFF --=20 2.47.2 From nobody Thu Dec 18 03:54:50 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 B01C228C2A2; Wed, 7 May 2025 16:14:27 +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=1746634467; cv=none; b=d0JKkVhUXJrBpnI+5FInHDZaU1Z3gcLTTGPTqVVxEGV1qiumAu3gZlopzLBYPlE0mxGQ6v2O07kqSxc6eO2NMadPgm0PzkQlr+7STbpIP+QSXrcZF2O+WNhfW/JuAYOyz6SyhYyMm7VRoLqswJ97p6a/u8sGkeSH8+iacAMwVOU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746634467; c=relaxed/simple; bh=u6UBg0rIcAFolpvGo5OrOssHZiALZ/tfKtbvsSEz8Fk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WDzcEpvEX9MQzfyksqq/74aWBShtxPOGOBfUcjCDtT0+AI7DVAp0JdmN6EQK3z4G2svwvWoZuozF5v/VV80q8RHUToRr+AUfqp8kDL0R58cGmoz4iO0rKjpjxi2oHCtw3ZAq4evxijhTI6tdfpCXVxxlKPq/bUawgKKEtE+VnQg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iSHPiCPx; 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="iSHPiCPx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F7CDC4CEE9; Wed, 7 May 2025 16:14:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746634467; bh=u6UBg0rIcAFolpvGo5OrOssHZiALZ/tfKtbvsSEz8Fk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=iSHPiCPxez04iAp6vBQCTmqYdJ7fpd8TJ+m0v1egxGzmTRK/MkHrcZyJhKyyGLGBe 4E7ZQP/ZIejhVtwsfzGhswWekj1srPZXKRHTfDoWpWwBOHSbr+piDRFDxpVGf+Unrr ufLyrbODLrUGs6xSGoKGE57M8FlFt3E6XuY0+e4l2Qv4pRs2PuiL1q7IZkk2NdZKkH 3OYFCdqtW2hbSVylbae9AqOhL8Soysmg7d1eby60MzgsoJIZJcXE6PTvOHo4xbZMZ2 vqP0mz6bCa1cSpUYM+k0tZ10wSi4exMN41YK55ahJ8A+WfxsgQlYw3YDAqhzq3Fecp K2ojlWtqbSKrw== From: Christian Brauner Date: Wed, 07 May 2025 18:13:42 +0200 Subject: [PATCH v4 09/11] pidfs, coredump: allow to verify coredump connection 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: <20250507-work-coredump-socket-v4-9-af0ef317b2d0@kernel.org> References: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> In-Reply-To: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> To: Kuniyuki Iwashima , linux-fsdevel@vger.kernel.org, Jann Horn 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, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=6339; i=brauner@kernel.org; h=from:subject:message-id; bh=u6UBg0rIcAFolpvGo5OrOssHZiALZ/tfKtbvsSEz8Fk=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWRIt20tM/ngPEf8qOK3OfLZMXdvZRio8PszPXOJaZ+ao b5jp3xyRykLgxgXg6yYIotDu0m43HKeis1GmRowc1iZQIYwcHEKwESe/2Bk6E/bcz4lzOeG2N4d 0boNz/Ji/n1N89G6uPxbzdXIBWGbExj+F7g7tkqeq9T6XXa2y8NUx+ZLkmtAxLRMNmuZKwWpndx MAA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 When a coredump connection is initiated use the socket cookie as the coredump cookie and store it in the pidfd. The receiver can now easily authenticate that the connection is coming from the kernel. Unless the coredump server expects to handle connection from non-crashing task it can validate that the connection has been made from 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 coredump info is available. Signed-off-by: Christian Brauner --- fs/coredump.c | 3 ++- fs/pidfs.c | 20 +++++++++++++++++++- include/linux/net.h | 1 + include/linux/pidfs.h | 1 + include/uapi/linux/pidfd.h | 1 + net/unix/af_unix.c | 7 +++++++ 6 files changed, 31 insertions(+), 2 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index ddff1854988f..cfb7a3459785 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -907,7 +907,8 @@ void do_coredump(const kernel_siginfo_t *siginfo) */ retval =3D kernel_connect(socket, (struct sockaddr *)(&coredump_unix_socket), - COREDUMP_UNIX_SOCKET_ADDR_SIZE, O_NONBLOCK); + COREDUMP_UNIX_SOCKET_ADDR_SIZE, O_NONBLOCK | + SOCK_COREDUMP); =20 /* * ... So we can safely put our pidfs reference now... diff --git a/fs/pidfs.c b/fs/pidfs.c index 8c4d83fb115b..7ff1e7923f19 100644 --- a/fs/pidfs.c +++ b/fs/pidfs.c @@ -35,6 +35,7 @@ struct pidfs_exit_info { __u64 cgroupid; __s32 exit_code; __u32 coredump_mask; + __u64 coredump_cookie; }; =20 struct pidfs_inode { @@ -300,6 +301,7 @@ 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; + 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); } =20 @@ -321,8 +323,10 @@ static long pidfd_info(struct file *file, unsigned int= cmd, unsigned long arg) =20 if (!(kinfo.mask & PIDFD_INFO_COREDUMP)) { task_lock(task); - if (task->mm) + if (task->mm) { + 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); } =20 @@ -589,6 +593,20 @@ void pidfs_exit(struct task_struct *tsk) } } =20 +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; + smp_store_release(&exit_info->coredump_cookie, coredump_cookie); +} + void pidfs_coredump(const struct coredump_params *cprm) { struct pid *pid =3D cprm->pid; diff --git a/include/linux/net.h b/include/linux/net.h index 0ff950eecc6b..005f1e52e7f1 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_TRUNC =20 #endif /* ARCH_HAS_SOCKET_TYPES */ =20 diff --git a/include/linux/pidfs.h b/include/linux/pidfs.h index f7729b9371bc..5875037be272 100644 --- a/include/linux/pidfs.h +++ b/include/linux/pidfs.h @@ -10,6 +10,7 @@ void pidfs_add_pid(struct pid *pid); void pidfs_remove_pid(struct pid *pid); void pidfs_exit(struct task_struct *tsk); void pidfs_coredump(const struct coredump_params *cprm); +void pidfs_coredump_cookie(struct pid *pid, u64 coredump_cookie); 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 84ac709f560c..f46819a02d23 100644 --- a/include/uapi/linux/pidfd.h +++ b/include/uapi/linux/pidfd.h @@ -108,6 +108,7 @@ struct pidfd_info { __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 148d008862e7..45e7a6363939 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -101,6 +101,7 @@ #include #include #include +#include #include #include #include @@ -771,6 +772,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 @@ -806,6 +808,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; } @@ -1717,6 +1721,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 Thu Dec 18 03:54:50 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 3F5C728C2A2; Wed, 7 May 2025 16:14:32 +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=1746634472; cv=none; b=E4o+2gUeSuVBCX5WR1f+yjutiDcnRbznrqnHtqL+4Y0P9apmWyfIruzv0WC+hhx/PIp3BHPfWytr/IvuDM8Ou1MCtAFtpQ3ehj73uhqaDwFGWbmDtBeZaGOXyQ8CrsSir1YRMVKgzsro21J9Y1Pp61VElaMaBDdZONQrDyt06Rc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746634472; c=relaxed/simple; bh=WF46UsPN75VDjMGLBPpKqGhF8yXGNp/YIhfAlM9Ro+U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VDx1d+bXxeRD4UgcZcUbLQxd3+Whne4FCVzjIDWUNiwUVVa3MIxbqLSeLlM1SadYvWt2iVgnHAAAieYquGsB0ZoPJnXx96GLxyv6pPw/RwgZw+58oUON3p9fp546cgg5pdg3pBbRFx7BfnSU/yRrU/TrjN3FJfjo3ZDD5MR0lxw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jYyhooju; 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="jYyhooju" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0BD14C4CEE2; Wed, 7 May 2025 16:14:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746634472; bh=WF46UsPN75VDjMGLBPpKqGhF8yXGNp/YIhfAlM9Ro+U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jYyhoojuZvjc6J0fxC0rspK0cIQ6FIPpH5yY+D1FgzLecppO01eBFvndlyexV1jAX 5GaeyH0o7/iWy8teld83G4AIZLQI/DJ3VUyBcCgkZJKcUI5hBqVcxgGnBW9mWjHp/8 l3qztuUQW+q4YJMvNMCyvbtsd3uNGBVngHX+0DfVAs0MPRT2pOlY0cNKcM4V+3v5UZ nwm1nl8TUuoc9x5CbJ6tHKUqOWwZGHfgZ5D9ZtmbMLBe+u04SomXtYixaAXCblQAbq 49R5ZPPqoE59GoysOvzH90kFWbxQ9v/S++JI2cpjfMogIjkEC2Xws7QlLyEyJqSRSN eqyZi017Ha26g== From: Christian Brauner Date: Wed, 07 May 2025 18:13:43 +0200 Subject: [PATCH v4 10/11] 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: <20250507-work-coredump-socket-v4-10-af0ef317b2d0@kernel.org> References: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> In-Reply-To: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> To: Kuniyuki Iwashima , linux-fsdevel@vger.kernel.org, Jann Horn 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, 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=owGbwMvMwCU28Zj0gdSKO4sYT6slMWRIt20NnzthRW3HvJ0rshpTkvlmHnItWXHzoPOlJY4lb p+bmPV3dpSyMIhxMciKKbI4tJuEyy3nqdhslKkBM4eVCWQIAxenAEzEQ5mRYVfAzCLFWRdds37s 35ix58bHqdpe3Dp9y4Jm3ezY2muy1IuRYYpY4+L7JiudPC8fDkzeZJTnv/L9Bo4LyyRyv2/fa7c ngQ8A 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 Thu Dec 18 03:54:50 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 3903128C2D2; Wed, 7 May 2025 16:14:36 +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=1746634477; cv=none; b=jnCcl2wghm6I75d8JELT+Z8l2cccm6EmxQYe6d63c4jYWQiUoutdKi11dCUkNaxgrkpZQFXZLqUwtGUd3PKD/+n+0OuREQLd/tPrnETwKRGiAdVtGZYm881oPYaBJm/XbovG02trHFDjaUa1kE4cywSedFeiYiLxXlmNs2L447M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746634477; c=relaxed/simple; bh=mMQMwLxi2MCFyRld7ql6OOPUv4ZPktnYFa2FfvFYeVA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=o8giI3/SC8/99UnuueudPj6VixY/z4ilH6Opg/at8NtUxzBeere/y0fIV7LuZzhw/nFLyXnSPQy2g6vyLwJIyeJdO+Ah7VhTOVlor0CdG/gm9moHOKdGjYobyWaEIcVSjdL7dDOT4WXpA3Gmbk1za5df0ueu+Gn47VrsUFTQvdY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lv9u8LxM; 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="lv9u8LxM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8EE70C4CEE2; Wed, 7 May 2025 16:14:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746634476; bh=mMQMwLxi2MCFyRld7ql6OOPUv4ZPktnYFa2FfvFYeVA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=lv9u8LxMkKUcqbP+6wfitDelliIHX6l+NpKakqtT6CWKYqmMuHDE1z+5eCX3b1qHB p8wkA8sjyZ+YJuvX9juivSdIUpky43LcjQC4cR6UIyj43jtJ/DwrfD2BK7+wbb6Wio jIucYMirqzEaECmfck1t8HWA2yxZ/UhuCHMUG96okp01+fAo/w/zflR75hUmQDnXDk /a1FAS+ur8rzXnksfDbeXc+A7g79qFaBjzCHgC/XbvJBxPmmobCuM/WiYtbPL1MqUW s11UZfBTMKQxaYKox2FKi/LReWnJePshNSdyGa3DyEG4a6AMKj6+HifEtA/NKNb6Nb 2nZqg7S5VpUxA== From: Christian Brauner Date: Wed, 07 May 2025 18:13:44 +0200 Subject: [PATCH v4 11/11] 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: <20250507-work-coredump-socket-v4-11-af0ef317b2d0@kernel.org> References: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> In-Reply-To: <20250507-work-coredump-socket-v4-0-af0ef317b2d0@kernel.org> To: Kuniyuki Iwashima , linux-fsdevel@vger.kernel.org, Jann Horn 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, Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=9262; i=brauner@kernel.org; h=from:subject:message-id; bh=mMQMwLxi2MCFyRld7ql6OOPUv4ZPktnYFa2FfvFYeVA=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWRIt219NXVSxeau3O3e+61X9izbf1SF63yvf7UV09J+L omC32/2dZSyMIhxMciKKbI4tJuEyy3nqdhslKkBM4eVCWQIAxenAEykYg4jw+6j//+9cdxsvWjb xDbuUo4T71XUDWPd067lG59kTJa1z2X4K3jy0bG2iX5VcY92v5E1Yzr6Zf6W2WHWLKKVuzkyUmS FuQE= 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 | 273 ++++++++++++++++++= +++- 1 file changed, 272 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/coredump/stackdump_test.c b/tools/test= ing/selftests/coredump/stackdump_test.c index fe3c728cd6be..a86f4ba0a367 100644 --- a/tools/testing/selftests/coredump/stackdump_test.c +++ b/tools/testing/selftests/coredump/stackdump_test.c @@ -5,10 +5,15 @@ #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 +40,7 @@ static void crashing_child(void) FIXTURE(coredump) { char original_core_pattern[256]; + pid_t pid_coredump_server; }; =20 FIXTURE_SETUP(coredump) @@ -44,6 +50,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 +68,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 +166,263 @@ 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); + + file =3D fopen("/proc/sys/kernel/core_pattern", "w"); + ASSERT_NE(NULL, file); + + ret =3D fprintf(file, "@linuxafsk/coredump.socket"); + ASSERT_EQ(ret, strlen("@linuxafsk/coredump.socket")); + ASSERT_EQ(fclose(file), 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_socket, fd_coredump, fd_peer_pidfd, fd_core_file; + __u64 peer_cookie; + socklen_t fd_peer_pidfd_len, peer_cookie_len; + static const struct sockaddr_un coredump_sk =3D { + .sun_family =3D AF_UNIX, + .sun_path =3D "\0linuxafsk/coredump.socket", + }; + static const size_t coredump_sk_len =3D + offsetof(struct sockaddr_un, sun_path) + + sizeof("linuxafsk/coredump.socket"); /* +1 for leading NUL */ + + close(ipc_sockets[0]); + + fd_socket =3D socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (fd_socket < 0) + _exit(EXIT_FAILURE); + + ret =3D bind(fd_socket, (const struct sockaddr *)&coredump_sk, coredump_= sk_len); + if (ret < 0) { + fprintf(stderr, "Failed to bind coredump socket\n"); + close(fd_socket); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + ret =3D listen(fd_socket, 1); + if (ret < 0) { + fprintf(stderr, "Failed to listen on coredump socket\n"); + close(fd_socket); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + if (write_nointr(ipc_sockets[1], "1", 1) < 0) { + close(fd_socket); + close(ipc_sockets[1]); + _exit(EXIT_FAILURE); + } + + close(ipc_sockets[1]); + + fd_coredump =3D accept4(fd_socket, NULL, NULL, SOCK_CLOEXEC); + if (fd_coredump < 0) { + fprintf(stderr, "Failed to accept coredump socket connection\n"); + close(fd_socket); + _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_socket); + _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_socket); + _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_socket); + 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_socket); + 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_socket); + close(fd_peer_pidfd); + _exit(EXIT_FAILURE); + } + + if (!info.coredump_cookie) { + fprintf(stderr, "Missing coredump cookie\n"); + close(fd_coredump); + close(fd_socket); + 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_socket); + 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_socket); + 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_socket); + 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_socket); + close(fd_peer_pidfd); + close(fd_core_file); + _exit(EXIT_FAILURE); + } + } + + close(fd_coredump); + close(fd_socket); + 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_econnrefused) +{ + int fd_socket; + static const struct sockaddr_un linuxafsk =3D { + .sun_family =3D AF_UNIX, + .sun_path =3D "\0linuxafsk/", + }; + static const size_t linuxafsk_len =3D + offsetof(struct sockaddr_un, sun_path) + + sizeof("linuxafsk/"); /* +1 for leading NUL */ + + fd_socket =3D socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + ASSERT_GT(fd_socket, 0); + + ASSERT_NE(bind(fd_socket, (const struct sockaddr *)&linuxafsk, linuxafsk_= len), 0); + ASSERT_EQ(errno, ECONNREFUSED); + EXPECT_EQ(close(fd_socket), 0); +} + +TEST_F(coredump, socket_econnrefused_privilege) +{ + int fd_socket; + static const struct sockaddr_un linuxafsk =3D { + .sun_family =3D AF_UNIX, + .sun_path =3D "\0linuxafsk/nope", + }; + static const size_t linuxafsk_len =3D + offsetof(struct sockaddr_un, sun_path) + + sizeof("linuxafsk/nope"); /* +1 for leading NUL */ + + ASSERT_EQ(seteuid(1234), 0); + + fd_socket =3D socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + ASSERT_GT(fd_socket, 0); + + ASSERT_NE(bind(fd_socket, (const struct sockaddr *)&linuxafsk, linuxafsk_= len), 0); + ASSERT_EQ(errno, ECONNREFUSED); + EXPECT_EQ(close(fd_socket), 0); + + ASSERT_EQ(seteuid(0), 0); +} + TEST_HARNESS_MAIN --=20 2.47.2