From nobody Wed Dec 17 08:51:19 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 6C0DF38FA3; Fri, 9 May 2025 10:25:57 +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=1746786358; cv=none; b=ZIvcSp6hO1z23ODsm/mlTrJj4uCivmvzT4G5J3am87a43/aS150bQjsgBP+oB8SKtqplNFnKjw9IG7WJtLCJqTG9QREkbTHX6un6/fTvAqvsRs4Kfelw2QXBnJ4IFwMS+L5HI4111pxAE0J3b7Yb6pS0FCCKoNYeaD+fV3s90X8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746786358; c=relaxed/simple; bh=d48rlIeb55kv9LHwHtfILNbBpVSvS+cV2//Rf2p6cRQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dz5/Z3zUUE+E/SAE9DfkWTqYKo6w8AbUOouWyASUaeXPsmGvM3kkrMF/Q7JT5lRbjSiuVOoaReiXtFlUeqzKL+c4BQLe3XSOiVYWP21WGiiH7SSQQdMIZOf+VgCSKTM6sffwUEFsLLl57xd03LS3SMoxbzJ4FSFhdEcWQyzs9Rc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=garMvxXz; 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="garMvxXz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 750C6C4CEEF; Fri, 9 May 2025 10:25:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746786356; bh=d48rlIeb55kv9LHwHtfILNbBpVSvS+cV2//Rf2p6cRQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=garMvxXzcsMIBDL7X4t+L7oPnFrth80GMDdmAK1YjdD6rD9j4SK+D97oLDYfP7/BB JncSK8Cpn56w6TyFqLZlJiveJ1Q57JQy457KiaraJfShVd+5PCQrVCAIll4mIHaGET QB4BHrRt3SPWwDa3CxkQCXmb2orr3c3vnXHycncEhhiE3K3z5X9KK0e5qgJP8nkef3 il98bkMIiDWYg4oahJMe7eM1cmw4mGUj5Z/3UaGQ1MAW9AHjlToY87L8Qj/a7ZdrmL 4qprPmqNVUAkihstMcIMC7khXn5R6ILqTBW+gRlO6LO6jKi46j6vjwke4wEgbOrPDw LNisyz847Yehw== From: Christian Brauner Date: Fri, 09 May 2025 12:25:33 +0200 Subject: [PATCH v5 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: <20250509-work-coredump-socket-v5-1-23c5b14df1bc@kernel.org> References: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@kernel.org> In-Reply-To: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@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=4656; i=brauner@kernel.org; h=from:subject:message-id; bh=d48rlIeb55kv9LHwHtfILNbBpVSvS+cV2//Rf2p6cRQ=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTI3tCytyu9Hv9mS3PH/pTTy41/Ht/VaZB8UGJn11vtZ ubW3SftOkpZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDKEgYtTACZyTZPhv8P5b1OU5HdF5Yi8 tppStUTwZPiBlUaOFTP2z/3rZly7lJWR4eASkXdu8esT1k8WnHxKzNTV/8Hnxtf934+kMyevKCm TZQQA 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 Reviewed-by: Alexander Mikhalitsyn --- 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 Wed Dec 17 08:51:19 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 9C7A828EA75; Fri, 9 May 2025 10:26:02 +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=1746786362; cv=none; b=IZkG9h47ObFhrzHtEyvlAg1ZjTrq3rdCzXh+Jz/bGXUnxr302QW8mnxfo+tFKyvLAUYIOfpZDK+1FiR00VOcppFa0i+GDyTfVzIcyxkOALLtGkR5fYaGJVz3ApfhdoHmIVhDiQoUcCF66e/ijL/yEeoQfHvChitV6b9RXse1eO0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746786362; c=relaxed/simple; bh=fEpmDQLWLttpCZqCdebailDPLzOF/JIhp5x6zzfJN4U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NHjYAxOmF3IWgpwWTROCgni4lxrwjOj4X8x37b9mEB5bKbBv2ak0w+xx8UxFrmlvqCztaA8ocg3PFLeR3QV+8BLhYZkqDOXhEum+dZteB7LzU3xb0LEJw7ttnF68SN6eQpocgrMaEs8TH7vBMOl6nDxyP3Y2bDrol9b4QikYva4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dp24dZcW; 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="dp24dZcW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 56924C4CEE4; Fri, 9 May 2025 10:25:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746786362; bh=fEpmDQLWLttpCZqCdebailDPLzOF/JIhp5x6zzfJN4U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=dp24dZcWPkjeZxnyk8bYRARgX7gTkBncKxEo1C2mnSPT+B/FZSIImctKVNTcf93s0 ZHLdewP08iN89JQVrkwiC7xvi56OuHbrGX8MffdND1nZiCA3xOgwqkpEU7q896znYJ k3wodEDEpNHzq6M7goCMMBtEl+WWNqRCB/hdigHxmAAfjuhZ4UzJ5CvJIJAmgV43n8 H48LlkenP1hjYjKSkOXNx8W0G/f9jL/WBStxdiVTHbw/kFkiufYj+HqDSrf1nU2hU6 X1WS1IdIqGFUq+XNxj9mYkAC+WKec5WoBn441COJAWCTy6JOywPq2RG3MgAXbwjQLZ sz2vSjmgK0Nuw== From: Christian Brauner Date: Fri, 09 May 2025 12:25:34 +0200 Subject: [PATCH v5 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: <20250509-work-coredump-socket-v5-2-23c5b14df1bc@kernel.org> References: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@kernel.org> In-Reply-To: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@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=fEpmDQLWLttpCZqCdebailDPLzOF/JIhp5x6zzfJN4U=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTI3tDa4GUz4eABDr7ALeu6FeQ9Di+dd2ynqzbnAc4zu zT/z5mt31HKwiDGxSArpsji0G4SLrecp2KzUaYGzBxWJpAhDFycAjARxTkM/wuENvycL2fi0uZ7 xsJD52+Ft7RwiINIuL1dM//TN9yb3zEyrL4UczB/vt7qH+tNRSO/CEj90GP+dSBLO3dvYvqy3fG hjAA= 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 Reviewed-by: Alexander Mikhalitsyn --- 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 Wed Dec 17 08:51:19 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 C3BF128EA72; Fri, 9 May 2025 10:26: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=1746786367; cv=none; b=em9wNU2nYep0XtEpFiW/0vIAspV+AFUlQ2FQ+1AHq+tThoYZwU0YqCjr9XcPwZHJBDiqJDR3xWFsbyWn9vUXjOyOU99JEtxpdaN3oBA7WO52eUsrTqOLbRLdl7ave5kF+GSLOSmM+ZK6j4cSg99aW1gw1aCIin15FXUEAvpPols= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746786367; c=relaxed/simple; bh=/Y2E1rjGOsMzeWWjGsal4y5faQxb4D6kQkcZ+mAHVGY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WYz36LmI4gAlw0CDZvHEL6A6UMNi/QEDuxDJyfTvtb3l2pL9Acjb1y1gso6T5JfU+cbHgEiG0Suz/6rghXILPeyJA77VEyobszYhqaQebWh9RBOt5rJeyCVAOGZMm8/x93sXWsydQqa9pu++9s78luZV6TE1/kyJbvhlzB/yW3k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YXjriyq5; 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="YXjriyq5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 85418C4CEE9; Fri, 9 May 2025 10:26:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746786367; bh=/Y2E1rjGOsMzeWWjGsal4y5faQxb4D6kQkcZ+mAHVGY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=YXjriyq5q7v2aiE7bTCxzhfhBVLd3QkGFNuJlC21+J8uG0zDiUkwVyYm1nm70GaWE nBI/7GMN4qrf4NjANF6a7UD5eorq2cW0CEeEHh+x7BjnJw7b4OcAM7fiKf/Z4Fl65c ZfiA1bJ3OjiJnmyviOpW4j9rXaKogTs8O1NGjwrKvAkiFyp1VvkJivRj/XnzE3gN9a VpJzOj5EkYWykNMbMU3HQ068FOZ3aEcluY3mQCgntLaooMW4l9CDeNorxPj+7RwdfH 09F9OswKPlK65JVNy+CBtzVSkb6yGQm8FHphi+NhR46wPvoWPfg2btNPfWygJ3A+rl zdAcz4j9CNzuA== From: Christian Brauner Date: Fri, 09 May 2025 12:25:35 +0200 Subject: [PATCH v5 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: <20250509-work-coredump-socket-v5-3-23c5b14df1bc@kernel.org> References: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@kernel.org> In-Reply-To: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@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=/Y2E1rjGOsMzeWWjGsal4y5faQxb4D6kQkcZ+mAHVGY=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTI3tB6Hycp7nUu5d5U09q5ntUhRw6Ecyd8fG5/jeODd rWJaNG6jlIWBjEuBlkxRRaHdpNwueU8FZuNMjVg5rAygQxh4OIUgImk+zP8M3gQkL/9Zwn3dIa3 axvavr+991OimaP9/nlfRZmjcpqZxxgZJp9I3yOcsuZFjNDd9u6NMSwzpoTVSB5eZcwg6PT2xKE rHAA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 They look rather messy right now. Signed-off-by: Christian Brauner Reviewed-by: Alexander Mikhalitsyn --- 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 Wed Dec 17 08:51:19 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 CCFF428ECE1; Fri, 9 May 2025 10:26: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=1746786372; cv=none; b=eSHhKibMaLhahE9WkHQtZySR7xHMWbzs+15+ZqfDaPR/52jifo+TAbFJTkMS/TZsPp4BS1dn8jj2l6LGSM+6ovSwbEO2fB5vMFvHSNxaHXO9aseYq5lTwF73oeqxb+MDeZ15IB2i6VckZDMxNvaKXvG969ZOy2ZiBCy9reeGg8k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746786372; c=relaxed/simple; bh=azxZtsE6L5/MGp5HZA52bIWnlvYbF3pnUPwM2hjgfrU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CFkiLVAr8bPd/Sz7PPFfUtx1hx4T6Qcdep7ey9hiRm9FGIvJUN8bIrXGeeqpHazGP0Oh4hu87tcbNj+WVbWv+r5Y/jo9jF0diBPbZ9MrFKaYQvE2VKVGsFsXjglRZlrlhy4lEyE1CmPtjfpNsSfxaxGeD5pvdaS5q6BCRFnIJPY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PiGXqAqn; 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="PiGXqAqn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3F55C4CEEF; Fri, 9 May 2025 10:26:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746786372; bh=azxZtsE6L5/MGp5HZA52bIWnlvYbF3pnUPwM2hjgfrU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=PiGXqAqnucrYMHj8iwb1f19LQY4lKmBdjBaFBzPL/ZNMPg+C7Zc2bLYv2C3+dj32t 4Hkymy907MXwmHO1lO2W/h1KAlDGcpw0CJ72HJ+LKWbm9CHsKZCt0oWB1u0hVaL59e rczY3kwi5qOpx/6PerdA/s2+LKXNR3Nxk1gBURwjfUQlkDOoBtd0naxdZ9Tiohs3iI QGU9EqyctWQhl55J6mA+F6sS6US3qit/+ojeCB+ekWpOUQC2coJE/LWoiK7Pu3Fj0q H3yJhK0K2rCR1ifMZE87G6VTBkBZCVINW2SGvII9RqJkL27By0E3yv/g2L1jlusPmJ aNPY6ve5LPZhw== From: Christian Brauner Date: Fri, 09 May 2025 12:25:36 +0200 Subject: [PATCH v5 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: <20250509-work-coredump-socket-v5-4-23c5b14df1bc@kernel.org> References: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@kernel.org> In-Reply-To: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@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=15710; i=brauner@kernel.org; h=from:subject:message-id; bh=azxZtsE6L5/MGp5HZA52bIWnlvYbF3pnUPwM2hjgfrU=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTI3tDat8wiTCmK4Xipb2XQTPWzhpHn54lyXrWODOwyX 3CIWyK+o5SFQYyLQVZMkcWh3SRcbjlPxWajTA2YOaxMIEMYuDgF4CJpjAxnW/bMOJl7/+7hc1Hv HN9ZvjhQL9TwyvPwwQsh/Rf0pVZ+ZfjDw6PT9taIvS37a1znAfV1z1LY/Z9G7ao9X/JbRe384YW cAA== 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 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. 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 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. 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 | 155 +++++++++++++++++++++++++++++++++++++++++++= +--- include/linux/coredump.h | 14 +++++ net/unix/af_unix.c | 21 +++++-- 3 files changed, 178 insertions(+), 12 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index b2eda7b176e4..d3599d671c51 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,37 @@ 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) + +/* + * Ensure that only a process privileged over the initial network + * namespace can bind the coredump server address. Protect us against + * the coredump server crashing, or intentionally restarting and an + * unprivileged process binding the coredump server address to receive + * coredumps. + */ +int unix_may_bind_coredump_addr(struct net *net, struct sockaddr_un *sunna= me, int len) +{ + if (net !=3D &init_net) + return 0; + if (COREDUMP_UNIX_SOCKET_ADDR_SIZE !=3D len) + return 0; + if (memcmp(&coredump_unix_socket, sunname, len)) + return 0; + if (ns_capable(net->user_ns, CAP_NET_ADMIN)) + return 0; + return -ECONNREFUSED; +} +#endif + void do_coredump(const kernel_siginfo_t *siginfo) { struct core_state core_state; @@ -801,6 +877,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 +953,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){}, 1, NULL); + break; + } + default: + break; + } + } + close_fail: if (cprm.file) filp_close(cprm.file, NULL); @@ -1069,7 +1208,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/coredump.h b/include/linux/coredump.h index 76e41805b92d..aa6820df916d 100644 --- a/include/linux/coredump.h +++ b/include/linux/coredump.h @@ -7,6 +7,8 @@ #include #include =20 +struct sockaddr_un; + #ifdef CONFIG_COREDUMP struct core_vma_metadata { unsigned long start, end; @@ -76,4 +78,16 @@ extern void validate_coredump_safety(void); static inline void validate_coredump_safety(void) {} #endif =20 +#if defined(CONFIG_COREDUMP) && defined(CONFIG_UNIX) +int unix_may_bind_coredump_addr(struct net *net, struct sockaddr_un *sunna= me, + int len); +#elif defined(CONFIG_UNIX) +static inline int unix_may_bind_coredump_addr(struct net *net, + struct sockaddr_un *sunname, + int len) +{ + return 0; +} +#endif + #endif /* _LINUX_COREDUMP_H */ diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 472f8aa9ea15..05e5a4737333 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -101,6 +101,7 @@ #include #include #include +#include #include #include #include @@ -436,6 +437,18 @@ 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; + + return unix_may_bind_coredump_addr(net, sunname, len); +} + static inline struct sock *unix_find_socket_byname(struct net *net, struct sockaddr_un *sunname, int len, unsigned int hash) @@ -1258,10 +1271,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 +1392,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 +1403,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 Wed Dec 17 08:51:19 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 E230928ECFE; Fri, 9 May 2025 10:26:17 +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=1746786382; cv=none; b=UhozAjcSfAy534pbcHaclNrqmoU6CrJIvvjDguUecinQZDpRFQSRpUy2Yp1C9RcTz5G/+cZriUZTA8m3cuuPSGAGY+7FJ8EAaarOA+Rlh9pZaax3qRnYsfaVn3PxxqWgFuNkbIXqQ6xHZ3lDVYZHAVTEv9B3tPN6wooNa9eTYb0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746786382; c=relaxed/simple; bh=xatkpe2vSXcXIpbbqm020+HAgg0XaXWOakYxbQLw2kQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MrVBML3xA/jc5DhR9/bcBZKZUAFBPlSzeeD8vP28PULpWKWnzvPGe8ebye2VOEQA6JcShiHVaKOIPOVOl1IokhUf+o1xzi6WOkj1HIojnl+GWKKYk6HzR3StX8axYFUPvGTNbYqBNkHUYwiV6Y8ZW/X/lmgYwNiSugwUUB1314Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RJnFhLPQ; 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="RJnFhLPQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8ED3C4CEEE; Fri, 9 May 2025 10:26:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746786377; bh=xatkpe2vSXcXIpbbqm020+HAgg0XaXWOakYxbQLw2kQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=RJnFhLPQnjZYXr5NnPfBIHoHtAufOVNJ7QbEMNSnSPFBnTIIPq7rX021eLm0eIj5X SDPaylO+V9O7Yeym07YygDT4A5EJbdXIZLwo83tsBnkD+WL22wmlK4Y2lxBLHoS4C1 03Hw1Zv2JQYgbQyMbsIbmTVyOsA7fhWpLpWIgchahYhgH5dC9hABaaHZC7rtYbPcGq vHrVj3G62hocRcBRH9HEUUiLgwEXK3/2xg2Ln5RNz2JjnckpRDuyejXhDuva/5/Wv1 UzypoWQPhN9Q5XzQXkxLmm7V6KNokug1ttUYQyQ97IYD0H2RdzBxjcQx5onC0sPXWe qweSXF14FxBEw== From: Christian Brauner Date: Fri, 09 May 2025 12:25:37 +0200 Subject: [PATCH v5 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: <20250509-work-coredump-socket-v5-5-23c5b14df1bc@kernel.org> References: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@kernel.org> In-Reply-To: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@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=12604; i=brauner@kernel.org; h=from:subject:message-id; bh=xatkpe2vSXcXIpbbqm020+HAgg0XaXWOakYxbQLw2kQ=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTI3tDi+iv54YqGqNr5nZop58w+HPffZJgdG5fVHsbdt vH3imV7O0pZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDKEgYtTACYiFMvwz8bJ8NL9VcJ8/a/d XbR36ogqyR6O0rj7//KfT3fex5tPb2JkmL5kzdasX1u1PxtFPXw+4/W52w9Vwz6s/3wm7PSD7bI JzzgB 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 | 37 ++++++++++++++++++++++- fs/pidfs.c | 75 ++++++++++++++++++++++++++++++++++++++++++= ++++ include/linux/net.h | 1 + include/linux/pidfs.h | 10 +++++++ include/uapi/linux/pidfd.h | 22 ++++++++++++++ net/unix/af_unix.c | 7 +++++ 6 files changed, 151 insertions(+), 1 deletion(-) diff --git a/fs/coredump.c b/fs/coredump.c index d3599d671c51..ff42688ec9ac 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 @@ -899,15 +903,46 @@ 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); + COREDUMP_UNIX_SOCKET_ADDR_SIZE, 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..848a12c8f9cf 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,12 @@ 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); + } + task =3D get_pid_task(pid, PIDTYPE_PID); if (!task) { /* @@ -296,6 +321,15 @@ 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_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 +593,47 @@ 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; + smp_store_release(&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/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/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 05e5a4737333..294667b572ee 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -101,6 +101,7 @@ #include #include #include +#include #include #include #include @@ -753,6 +754,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 @@ -788,6 +790,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; } @@ -1699,6 +1703,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:19 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 0DEF4290BDF; Fri, 9 May 2025 10:26:22 +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=1746786383; cv=none; b=Jkpo9I9lbC59O0DZ7uV2V1BNkS65UouwDIJs59F3rzVIvq9Yh+CM4LhnXrVHTN4cNBHBts6Shq/Z5jpyll+NteB03i3dvXRnG7H2Bw558M1Go2XS3anx4KFD5rOWrCrmTvYaAfoVxS4qGGXQc8NB4GnBMEEsiawqsGDap3Ir8uM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746786383; c=relaxed/simple; bh=5W5KUoJ3iZpfXplEUqQaQMHWFuiUtasGtEN3sJaZv/s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=caoaYSDbDjtIJKbtocd95LaOSrCmOU3M1MjEsiMKNtTVEJJvSvU+3IGjO2PPqbO28aNSEnQSQoqsUNnab0Sm+5d93cMJeXijGaX1MFyTw98Z8f0P/t/LkJIlgECI5LIZGPm/1xYaZTN2+bq8r1S2yr92WdrHdIx55dWeai5XxzM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZKhMlGzg; 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="ZKhMlGzg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B34A6C4CEF1; Fri, 9 May 2025 10:26:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746786382; bh=5W5KUoJ3iZpfXplEUqQaQMHWFuiUtasGtEN3sJaZv/s=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ZKhMlGzgabUo0uZsKxFb5iBOQf9xGb7dKhLz9WyS9EoIzJ2/0BoEvhzAHTxpEPiBw TnPPdoA4N8uwke23kmMQRvAkk3RQbj2/yFOYFI0ZxYpFr0QX9BK2EgULPPLm3kQ9ih SH4Xaz9NQuJqH1aIad6Kz5zekafeK2NhlVuxVq8sEvinSv1abLVp8LyTD8DXmZBrhc teU1BLLL6pTIhGnl+6cRCNrR1N4ZpPlaqYTWCkOQ0wfHfJma8P2rLxqsdStmc51d2T cMXPZVIqKc6htcGHg/wDXSBaVvMXm3De9hnBUqD9BcWg7UbsA35VBG2fSp26uRaYlU lSRGeu/DjihRA== From: Christian Brauner Date: Fri, 09 May 2025 12:25:38 +0200 Subject: [PATCH v5 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: <20250509-work-coredump-socket-v5-6-23c5b14df1bc@kernel.org> References: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@kernel.org> In-Reply-To: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@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=1139; i=brauner@kernel.org; h=from:subject:message-id; bh=5W5KUoJ3iZpfXplEUqQaQMHWFuiUtasGtEN3sJaZv/s=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTI3tBSaOK8ZztrWXDmhOKTnRti57117/yy92L+/kT/s D0z3vl/6yhlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZiI2zZGhvN/0sJXa5j3ndfd I7jyg3Dx0jxV1QMNl8rMtPlzJ/qcWMvIcH1ydpAgU+u7JdwxzvOnnd98lzWnT9txl4/+KY9O0W+ buAE= 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 ff42688ec9ac..fa50a681b225 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -1263,6 +1263,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 { { @@ -1306,6 +1312,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:19 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 CF33428F520; Fri, 9 May 2025 10:26: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=1746786387; cv=none; b=OyGksnzZ8xRZMuyByP26YRAs9GLFl1sn0j6Vo1kPrKT0vnCjnqhDSmPW+VuF3rZw2RUEOXPx8LbS30KGkw7PBnqPpgPQJnzjZdNeM4Lt1LKv6FTqHj7w+9vlQiSE68hjDwDRwzCpzXn+dHJt3G6258ggQFoTUMB/g9QNpLDu0YE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746786387; c=relaxed/simple; bh=XtYAn9qxMv6rK3q0zBvwCKauIZoqUJCYXQhXC/1IcFM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RRgl/Q5SfzBF0kOpz5fT4GLfldDjR7WiGyuCvy+jP9Vl2PcaHXcjq2XD1E+sttdJSjInG21mPdHLR4t/hQZJIcrqnLkmlcs3hNe/F2oe1Y8jmKxt/TIWSavAWkcpEXZC5YVIlLu9L9fE098aYsish8KbCP2GWoiAtuWZzO+Gzn0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nS0nppIm; 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="nS0nppIm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE512C4CEE9; Fri, 9 May 2025 10:26:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746786387; bh=XtYAn9qxMv6rK3q0zBvwCKauIZoqUJCYXQhXC/1IcFM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=nS0nppImxSVITjGKUFHL/HFCSEU2yLsr7sPnSCzwevQeL3IESIxm1XdI3Ra0p7hfG 9cZhKns1Q5Ca4BinkAkD9cPZOMzuPPffkJFTiUN6qvUx+pUA+5NpdHaRN8WbioH0nU OlBUFBahfc6A2n5zF/ZOMSmEH91hqD1bR5gGm/6Thx4cUC+2mShiooJ+4eClKcs3fv zQOlV15CT9xZrDUgL8BL+0L4ArJaxUwprD+JEXyw1no3WG82ovnhpBmzVqEOM3LtZw a9bgqI5sUvTiIJgNQjp0bYEeShxUme32Ux6F7/jnMpX/RTtoEE368IGe8lIc5dCbAC Q56slUqnLfRyg== From: Christian Brauner Date: Fri, 09 May 2025 12:25:39 +0200 Subject: [PATCH v5 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: <20250509-work-coredump-socket-v5-7-23c5b14df1bc@kernel.org> References: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@kernel.org> In-Reply-To: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@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=1280; i=brauner@kernel.org; h=from:subject:message-id; bh=XtYAn9qxMv6rK3q0zBvwCKauIZoqUJCYXQhXC/1IcFM=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTI3tBqXzpXddNFvjkbaotuaTbIzJm9NOTg4tKVj5jzR JS3ssdv7yhlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZhI4wJGhsPTFqw8fmJ5Aue3 N71Wov3Op296p13jl14418VXts9Ik4fhn6HAy+TrSnN3WV25+rGY8+nqX6+/+F//YeZXpvlkclr JVkYA 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 fa50a681b225..19e776769b21 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -1254,10 +1254,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 Wed Dec 17 08:51:19 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 491E028F52D; Fri, 9 May 2025 10:26: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=1746786393; cv=none; b=H0+5yWdIpz4CIe4SkbSI3JK50cUmY0mODJeOhNVHWzzve3Edn9Fgz85GgNxGlI43geZT68ZVQn/y7HzfF5JyvWqPo6l57Pl81c8NX1rLE2rs8qe7QFuQH/ij2XqGmLRoibL6FMGo2Bk4hWThbI1V4O6Nk7AJinx5vUgXOeIcnN0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746786393; c=relaxed/simple; bh=WF46UsPN75VDjMGLBPpKqGhF8yXGNp/YIhfAlM9Ro+U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Qwo+xC9p5Q0eSjRh1V2EWgi//9q2kPS67I+82Ssxs8Wt+jAw8eOA6xNSoqHOgzE6dIFXTngPPa4WBW6wtQxe1OT4nDPSFyj5BHKwMrHM8QoMbL/AxNoZ7Xx32tpn9KDxUWV3ycyhK8mlMKt/8qa3/XMucSuM5eWUpAmtHCxmUtM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HhzX0J59; 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="HhzX0J59" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 28852C4CEE4; Fri, 9 May 2025 10:26:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746786392; bh=WF46UsPN75VDjMGLBPpKqGhF8yXGNp/YIhfAlM9Ro+U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=HhzX0J59OWpC18HzeymavGMmDQGNlaD/7TtlK11wypa63GJ/XkyH57b2HqDpo7z+4 DxvOGNswnlXqf2vFh2RcG08aMGLMF6gVE13l8PhZUuQwFdpH9U3lI6MZKJJZkdjaUn 8rSncXyy6B1bQ4VD9KSox0Ch+vJv4JFh3WhNKkpd4HB4rBMnF4eUoWOs8bBoLE2M4n 1wGDnQLJsM2BcUnuDw//hG0vKHTpfM2AlAJPXaaUt0JS/cC9ePDIFLnKxBZ3nKjaHU TlEhH/PRrydIq1ivhgjvIgU9c/WBKsuv6w0Ua6YMOqOqHIaoS3sM4mUL3jK7wdNamn xxmrWW/SiVUeg== From: Christian Brauner Date: Fri, 09 May 2025 12:25:40 +0200 Subject: [PATCH v5 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: <20250509-work-coredump-socket-v5-8-23c5b14df1bc@kernel.org> References: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@kernel.org> In-Reply-To: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@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=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTI3tAqjv+WYBK0ZX5Fxrd5Wo0NatwaxWGF9+PNDH5si OlsuXW8o5SFQYyLQVZMkcWh3SRcbjlPxWajTA2YOaxMIEMYuDgFYCJHLjH8M4y4eE6aXcFvxonY jwrMRXUdFX9cQ2rjtjl7zdx64GKOLMNf0f8Ch31PRtvx80xI29O+JvGF5otNBcvPXJsWUHz5zo4 4JgA= 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:19 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 4F24E28E57C; Fri, 9 May 2025 10:26:37 +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=1746786398; cv=none; b=YppvAfGwCUCX4gsUiyNR8esAkKFsgUFEx+Iru74qQHUxTP1OKtbfZy5zcBIeMJSuHluIpC/ku1P3YyBd+BgqsHoknkzJVEw7tZGOMbZK9aQ8vsOA/8t84PzQa02FzaCJG4Lfc3t++8fcOF0+aVxWHS/ln6qpB8/aWl90MWXW1kw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746786398; c=relaxed/simple; bh=UO8bLlP3bUupU133EuNspN2KAZgS/+F5nFcKZes45cU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gtDxu4XV2DA1s6j8YUKFmiruK2LOLi6UGYCeDeRI1BO0jdt/N4YO6TVN9EmcbMw+IZ/iAfFExuGfxjcFrBj27uMEQUxJxBc0oJuXFHyQDNQqT9HAbKnAj9YG4L/OCaHFBtfZfcWnx4EzeRCc64NP63hM9vPH2rOT6cuparLYa/Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YKgC5FPT; 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="YKgC5FPT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D610C4CEEF; Fri, 9 May 2025 10:26:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746786397; bh=UO8bLlP3bUupU133EuNspN2KAZgS/+F5nFcKZes45cU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=YKgC5FPT0WBppeiCJmEFRBJPyodLYesxoE5mgC9/S7q0+DcsRQ/YC/rUrLtC6U8iR Ov2ucJfKnUR2imwGtS77/KFG/hQPkAUFi2aHR1Xxg0y0EoMLLoSOvju1bwpdnz+/Uh Xiln5EtEg+LIBVJnukUv427p1gdApe29r91c6DKCjhr1oXz3TjQLsev0NF3LoNel8R VJqlRj9YcTel+rxAV9UqQodDjGFX0z32gOlpiWqqv5qo5hd/fAIW9bHcM4hX+2uoS5 +tDDShde9+sYFn+1CoKV2htxxPXX2DCpJNMsuXlLIexu31qLaRPzpESB0oZoOdOuMc yTWLkQk9BDNwQ== From: Christian Brauner Date: Fri, 09 May 2025 12:25:41 +0200 Subject: [PATCH v5 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: <20250509-work-coredump-socket-v5-9-23c5b14df1bc@kernel.org> References: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@kernel.org> In-Reply-To: <20250509-work-coredump-socket-v5-0-23c5b14df1bc@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=8035; i=brauner@kernel.org; h=from:subject:message-id; bh=UO8bLlP3bUupU133EuNspN2KAZgS/+F5nFcKZes45cU=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTI3tCOz9G98FZXdAIPV9lRv2+1x1dvsX84eWPb7IW5S mkT951b0VHKwiDGxSArpsji0G4SLrecp2KzUaYGzBxWJpAhDFycAjCRWTsZGW7mBDoqZbDraB72 rmS0+MBtbdx0vUr39NsGp2/zJyzXuMTI0D+xfdVk229z/l/QPsDq+TWVcapu21XdutLJwgvt3u7 4zg8A 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 | 231 ++++++++++++++++++= +++- 1 file changed, 230 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/coredump/stackdump_test.c b/tools/test= ing/selftests/coredump/stackdump_test.c index fe3c728cd6be..8423499808ff 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,221 @@ 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_HARNESS_MAIN --=20 2.47.2