From nobody Mon Dec 15 23:27:41 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 6DF53205502; Fri, 16 May 2025 11:25:52 +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=1747394752; cv=none; b=p7EdDWvIO2Nwt4zoRo7ie/uD4nBijyvLfTKXrghQGSm0bi1aW69uzfaGPk1yEOxNV24jSglSiN0095qb5TFnGfccC0SPB/KsQhvKViSl5JCAuYr7aVUgoB6vy3kfew4+TVgbD+YtFbjg0E5y/NOnN9acxhEntx8AZyYfYm/L5c8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747394752; c=relaxed/simple; bh=aEsNIF9y6JxcvOwyyzzXZ1H3HPtUSep+0f3agKGpmcE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nciB7KmLkuYvWKdeGkWCbDiNlildFOdD5S2Sb8Mmh7/K/Z4PW2ZbGTAylPSGJ8fe2YUg5mmbGn/mnlFr9BYbohwp8VXDPoNOcOpZLiNr0rZYuFMy5Mkn3OAyER4bSvIUe+TYW4g0kqC6/by4f6Pn709RWQDR4SRjjjrb1IC1vPA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pAJEYftS; 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="pAJEYftS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98753C4CEE4; Fri, 16 May 2025 11:25:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747394752; bh=aEsNIF9y6JxcvOwyyzzXZ1H3HPtUSep+0f3agKGpmcE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=pAJEYftSqS3wkN3BRYgU3SIPkSLTPQNiSALKSkPtRAypqJ4ukWIB5If3C4d+RLXCd ehiy2COgUCbUcdzPaT6slmiIIe5sslmzE2TLRGT9dDbChRZ7AK+rJS5ekSscvIzuPu tJbUXmEfWnZywVf1+MCRmhD5zP9eMNJnFErqgTOSTseNgsvbwvgfbG0tP1pB9zh0MD hKFDqowhqAAizpbkxuHMD7fORZ/POBpbaTeIp7/TfbdWcSU5LwIXvuG+Pk90H6EPdT FxQyiWNShNZBgEuLitSC6C7MioiSjv6sv6wn8aREMWYwIMxhRvFDTnpFnWp4zhx6NP XpClbpocZ7aww== From: Christian Brauner Date: Fri, 16 May 2025 13:25:28 +0200 Subject: [PATCH v8 1/9] coredump: massage format_corename() 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: <20250516-work-coredump-socket-v8-1-664f3caf2516@kernel.org> References: <20250516-work-coredump-socket-v8-0-664f3caf2516@kernel.org> In-Reply-To: <20250516-work-coredump-socket-v8-0-664f3caf2516@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 , Serge Hallyn X-Mailer: b4 0.15-dev-6f78e X-Developer-Signature: v=1; a=openpgp-sha256; l=4920; i=brauner@kernel.org; h=from:subject:message-id; bh=aEsNIF9y6JxcvOwyyzzXZ1H3HPtUSep+0f3agKGpmcE=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWSoK2zNll65uP+fdZW3UFZK/p59S8oP58Rbcn4XiLvP+ PS7maduRykLgxgXg6yYIotDu0m43HKeis1GmRowc1iZQIYwcHEKwERuBjIy7FjvxCGluG+Z5wan 3rA1kYHqrQ5TwgNdzsWnKxx4YbZFlJHh0Mwfd4QPzuY8MmOys0n/h8nCR9+e/v6vKNlHY86UlZ4 zOAA= 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. Acked-by: Serge Hallyn Acked-by: Luca Boccassi Reviewed-by: Jann Horn Reviewed-by: Alexander Mikhalitsyn Signed-off-by: Christian Brauner --- fs/coredump.c | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index d740a0411266..45725465c299 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,9 @@ static int format_corename(struct core_name *cn, struc= t 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) { - err =3D cn_printf(cn, ".%d", task_tgid_vnr(current)); - if (err) - return err; - } - return ispipe; + if (cn->core_type =3D=3D COREDUMP_FILE && !pid_in_pattern && core_uses_pi= d) + return cn_printf(cn, ".%d", task_tgid_vnr(current)); + return 0; } =20 static int zap_process(struct signal_struct *signal, int exit_code) @@ -583,7 +589,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 +637,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 +699,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 +827,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