linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Serge E. Hallyn" <serge@hallyn.com>
To: Christian Brauner <brauner@kernel.org>
Cc: linux-fsdevel@vger.kernel.org, "Jann Horn" <jannh@google.com>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Kuniyuki Iwashima" <kuniyu@amazon.com>,
	"Eric Dumazet" <edumazet@google.com>,
	"Oleg Nesterov" <oleg@redhat.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Daan De Meyer" <daan.j.demeyer@gmail.com>,
	"David Rheinsberg" <david@readahead.eu>,
	"Jakub Kicinski" <kuba@kernel.org>, "Jan Kara" <jack@suse.cz>,
	"Lennart Poettering" <lennart@poettering.net>,
	"Luca Boccassi" <bluca@debian.org>, "Mike Yuan" <me@yhndnzj.com>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Simon Horman" <horms@kernel.org>,
	"Zbigniew Jędrzejewski-Szmek" <zbyszek@in.waw.pl>,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	"Alexander Mikhalitsyn" <alexander@mihalicyn.com>
Subject: Re: [PATCH v7 1/9] coredump: massage format_corname()
Date: Thu, 15 May 2025 08:36:06 -0500	[thread overview]
Message-ID: <20250515133606.GA740869@mail.hallyn.com> (raw)
In-Reply-To: <20250515-work-coredump-socket-v7-1-0a1329496c31@kernel.org>

On Thu, May 15, 2025 at 12:03:34AM +0200, Christian Brauner wrote:
> 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 <brauner@kernel.org>

Not my wheelhouse, but this is a nice cleanup.

Acked-by: Serge Hallyn <serge@hallyn.com>

> ---
>  fs/coredump.c | 41 ++++++++++++++++++++++++-----------------
>  1 file changed, 24 insertions(+), 17 deletions(-)
> 
> diff --git a/fs/coredump.c b/fs/coredump.c
> index d740a0411266..368751d98781 100644
> --- a/fs/coredump.c
> +++ b/fs/coredump.c
> @@ -76,9 +76,15 @@ static char core_pattern[CORENAME_MAX_SIZE] = "core";
>  static int core_name_size = CORENAME_MAX_SIZE;
>  unsigned int core_file_note_size_limit = CORE_FILE_NOTE_SIZE_DEFAULT;
>  
> +enum coredump_type_t {
> +	COREDUMP_FILE = 1,
> +	COREDUMP_PIPE = 2,
> +};
> +
>  struct core_name {
>  	char *corename;
>  	int used, size;
> +	enum coredump_type_t core_type;
>  };
>  
>  static int expand_corename(struct core_name *cn, int size)
> @@ -218,18 +224,21 @@ static int format_corename(struct core_name *cn, struct coredump_params *cprm,
>  {
>  	const struct cred *cred = current_cred();
>  	const char *pat_ptr = core_pattern;
> -	int ispipe = (*pat_ptr == '|');
>  	bool was_space = false;
>  	int pid_in_pattern = 0;
>  	int err = 0;
>  
>  	cn->used = 0;
>  	cn->corename = NULL;
> +	if (*pat_ptr == '|')
> +		cn->core_type = COREDUMP_PIPE;
> +	else
> +		cn->core_type = COREDUMP_FILE;
>  	if (expand_corename(cn, core_name_size))
>  		return -ENOMEM;
>  	cn->corename[0] = '\0';
>  
> -	if (ispipe) {
> +	if (cn->core_type == COREDUMP_PIPE) {
>  		int argvs = sizeof(core_pattern) / 2;
>  		(*argv) = 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 == COREDUMP_PIPE) {
>  			if (isspace(*pat_ptr)) {
>  				if (cn->used != 0)
>  					was_space = 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 != COREDUMP_PIPE)
>  					break;
>  
>  				/*
> @@ -384,12 +393,12 @@ static int format_corename(struct core_name *cn, struct 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 == COREDUMP_PIPE) && !pid_in_pattern && core_uses_pid) {
>  		err = cn_printf(cn, ".%d", task_tgid_vnr(current));
>  		if (err)
>  			return err;
>  	}
> -	return ispipe;
> +	return 0;
>  }
>  
>  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 = 0;
> -	int ispipe;
>  	size_t *argv = NULL;
>  	int argc = 0;
>  	/* require nonrelative corefile path and be extra careful */
> @@ -632,19 +640,18 @@ void do_coredump(const kernel_siginfo_t *siginfo)
>  
>  	old_cred = override_creds(cred);
>  
> -	ispipe = format_corename(&cn, &cprm, &argv, &argc);
> +	retval = format_corename(&cn, &cprm, &argv, &argc);
> +	if (retval < 0) {
> +		coredump_report_failure("format_corename failed, aborting core");
> +		goto fail_unlock;
> +	}
>  
> -	if (ispipe) {
> +	if (cn.core_type == COREDUMP_PIPE) {
>  		int argi;
>  		int dump_count;
>  		char **helper_argv;
>  		struct subprocess_info *sub_info;
>  
> -		if (ispipe < 0) {
> -			coredump_report_failure("format_corename failed, aborting core");
> -			goto fail_unlock;
> -		}
> -
>  		if (cprm.limit == 1) {
>  			/* See umh_coredump_setup() which sets RLIMIT_CORE = 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 == COREDUMP_FILE) {
>  		struct mnt_idmap *idmap;
>  		struct inode *inode;
>  		int open_flags = 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 == 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 == COREDUMP_PIPE)
>  		atomic_dec(&core_dump_count);
>  fail_unlock:
>  	kfree(argv);
> 
> -- 
> 2.47.2
> 

  parent reply	other threads:[~2025-05-15 13:36 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-14 22:03 [PATCH v7 0/9] coredump: add coredump socket Christian Brauner
2025-05-14 22:03 ` [PATCH v7 1/9] coredump: massage format_corname() Christian Brauner
2025-05-15 13:19   ` Alexander Mikhalitsyn
2025-05-15 13:36   ` Serge E. Hallyn [this message]
2025-05-15 20:52   ` Jann Horn
2025-05-14 22:03 ` [PATCH v7 2/9] coredump: massage do_coredump() Christian Brauner
2025-05-15 13:21   ` Alexander Mikhalitsyn
2025-05-15 20:52   ` Jann Horn
2025-05-14 22:03 ` [PATCH v7 3/9] coredump: reflow dump helpers a little Christian Brauner
2025-05-15 13:22   ` Alexander Mikhalitsyn
2025-05-15 20:53   ` Jann Horn
2025-05-14 22:03 ` [PATCH v7 4/9] coredump: add coredump socket Christian Brauner
2025-05-15 13:47   ` Alexander Mikhalitsyn
2025-05-16  8:30     ` Christian Brauner
2025-05-15 17:00   ` Kuniyuki Iwashima
2025-05-15 20:52     ` Jann Horn
2025-05-15 21:04       ` Kuniyuki Iwashima
2025-05-16 10:14     ` Christian Brauner
2025-05-15 20:54   ` Jann Horn
2025-05-15 21:15     ` Kuniyuki Iwashima
2025-05-16 10:09     ` Christian Brauner
2025-05-16 10:20       ` Christian Brauner
2025-05-14 22:03 ` [PATCH v7 5/9] pidfs, coredump: add PIDFD_INFO_COREDUMP Christian Brauner
2025-05-15 14:08   ` Alexander Mikhalitsyn
2025-05-15 20:56   ` Jann Horn
2025-05-15 21:37     ` Jann Horn
2025-05-16 10:34     ` Christian Brauner
2025-05-16 14:26       ` Jann Horn
2025-05-14 22:03 ` [PATCH v7 6/9] coredump: show supported coredump modes Christian Brauner
2025-05-15 13:56   ` Alexander Mikhalitsyn
2025-05-15 20:56   ` Jann Horn
2025-05-14 22:03 ` [PATCH v7 7/9] coredump: validate socket name as it is written Christian Brauner
2025-05-15 14:03   ` Alexander Mikhalitsyn
2025-05-15 20:56   ` Jann Horn
2025-05-16  9:54     ` Christian Brauner
2025-05-16 13:29       ` Christian Brauner
2025-05-14 22:03 ` [PATCH v7 8/9] selftests/pidfd: add PIDFD_INFO_COREDUMP infrastructure Christian Brauner
2025-05-15 14:35   ` Alexander Mikhalitsyn
2025-05-14 22:03 ` [PATCH v7 9/9] selftests/coredump: add tests for AF_UNIX coredumps Christian Brauner
2025-05-15 14:37   ` Alexander Mikhalitsyn
2025-05-14 22:38 ` [PATCH v7 0/9] coredump: add coredump socket Luca Boccassi
2025-05-15  9:17 ` Christian Brauner
2025-05-15  9:26 ` Lennart Poettering

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250515133606.GA740869@mail.hallyn.com \
    --to=serge@hallyn.com \
    --cc=alexander@mihalicyn.com \
    --cc=bluca@debian.org \
    --cc=brauner@kernel.org \
    --cc=daan.j.demeyer@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=david@readahead.eu \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@amazon.com \
    --cc=lennart@poettering.net \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=me@yhndnzj.com \
    --cc=netdev@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=pabeni@redhat.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=zbyszek@in.waw.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).