Skip to content

Commit 0d424c6

Browse files
committed
coredump: massage format_corename()
JIRA: https://issues.redhat.com/browse/RHEL-113598 commit 727b551 Author: Christian Brauner <brauner@kernel.org> Date: Fri, 16 May 2025 13:25:28 +0200 coredump: massage format_corename() We're going to extend the coredump code in follow-up patches. Clean it up so we can do this more easily. Link: https://lore.kernel.org/20250516-work-coredump-socket-v8-1-664f3caf2516@kernel.org Acked-by: Serge Hallyn <serge@hallyn.com> Acked-by: Luca Boccassi <luca.boccassi@gmail.com> Reviewed-by: Jann Horn <jannh@google.com> Reviewed-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> Signed-off-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Waiman Long <longman@redhat.com>
1 parent db6e76e commit 0d424c6

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

fs/coredump.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,15 @@ static char core_pattern[CORENAME_MAX_SIZE] = "core";
7676
static int core_name_size = CORENAME_MAX_SIZE;
7777
unsigned int core_file_note_size_limit = CORE_FILE_NOTE_SIZE_DEFAULT;
7878

79+
enum coredump_type_t {
80+
COREDUMP_FILE = 1,
81+
COREDUMP_PIPE = 2,
82+
};
83+
7984
struct core_name {
8085
char *corename;
8186
int used, size;
87+
enum coredump_type_t core_type;
8288
};
8389

8490
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,
218224
{
219225
const struct cred *cred = current_cred();
220226
const char *pat_ptr = core_pattern;
221-
int ispipe = (*pat_ptr == '|');
222227
bool was_space = false;
223228
int pid_in_pattern = 0;
224229
int err = 0;
225230

226231
cn->used = 0;
227232
cn->corename = NULL;
233+
if (*pat_ptr == '|')
234+
cn->core_type = COREDUMP_PIPE;
235+
else
236+
cn->core_type = COREDUMP_FILE;
228237
if (expand_corename(cn, core_name_size))
229238
return -ENOMEM;
230239
cn->corename[0] = '\0';
231240

232-
if (ispipe) {
241+
if (cn->core_type == COREDUMP_PIPE) {
233242
int argvs = sizeof(core_pattern) / 2;
234243
(*argv) = kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL);
235244
if (!(*argv))
@@ -247,7 +256,7 @@ static int format_corename(struct core_name *cn, struct coredump_params *cprm,
247256
* Split on spaces before doing template expansion so that
248257
* %e and %E don't get split if they have spaces in them
249258
*/
250-
if (ispipe) {
259+
if (cn->core_type == COREDUMP_PIPE) {
251260
if (isspace(*pat_ptr)) {
252261
if (cn->used != 0)
253262
was_space = true;
@@ -353,7 +362,7 @@ static int format_corename(struct core_name *cn, struct coredump_params *cprm,
353362
* Installing a pidfd only makes sense if
354363
* we actually spawn a usermode helper.
355364
*/
356-
if (!ispipe)
365+
if (cn->core_type != COREDUMP_PIPE)
357366
break;
358367

359368
/*
@@ -384,12 +393,9 @@ static int format_corename(struct core_name *cn, struct coredump_params *cprm,
384393
* If core_pattern does not include a %p (as is the default)
385394
* and core_uses_pid is set, then .%pid will be appended to
386395
* the filename. Do not do this for piped commands. */
387-
if (!ispipe && !pid_in_pattern && core_uses_pid) {
388-
err = cn_printf(cn, ".%d", task_tgid_vnr(current));
389-
if (err)
390-
return err;
391-
}
392-
return ispipe;
396+
if (cn->core_type == COREDUMP_FILE && !pid_in_pattern && core_uses_pid)
397+
return cn_printf(cn, ".%d", task_tgid_vnr(current));
398+
return 0;
393399
}
394400

395401
static int zap_process(struct signal_struct *signal, int exit_code)
@@ -583,7 +589,6 @@ void do_coredump(const kernel_siginfo_t *siginfo)
583589
const struct cred *old_cred;
584590
struct cred *cred;
585591
int retval = 0;
586-
int ispipe;
587592
size_t *argv = NULL;
588593
int argc = 0;
589594
/* require nonrelative corefile path and be extra careful */
@@ -632,19 +637,18 @@ void do_coredump(const kernel_siginfo_t *siginfo)
632637

633638
old_cred = override_creds(cred);
634639

635-
ispipe = format_corename(&cn, &cprm, &argv, &argc);
640+
retval = format_corename(&cn, &cprm, &argv, &argc);
641+
if (retval < 0) {
642+
coredump_report_failure("format_corename failed, aborting core");
643+
goto fail_unlock;
644+
}
636645

637-
if (ispipe) {
646+
if (cn.core_type == COREDUMP_PIPE) {
638647
int argi;
639648
int dump_count;
640649
char **helper_argv;
641650
struct subprocess_info *sub_info;
642651

643-
if (ispipe < 0) {
644-
coredump_report_failure("format_corename failed, aborting core");
645-
goto fail_unlock;
646-
}
647-
648652
if (cprm.limit == 1) {
649653
/* See umh_coredump_setup() which sets RLIMIT_CORE = 1.
650654
*
@@ -695,7 +699,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
695699
coredump_report_failure("|%s pipe failed", cn.corename);
696700
goto close_fail;
697701
}
698-
} else {
702+
} else if (cn.core_type == COREDUMP_FILE) {
699703
struct mnt_idmap *idmap;
700704
struct inode *inode;
701705
int open_flags = O_CREAT | O_WRONLY | O_NOFOLLOW |
@@ -823,13 +827,13 @@ void do_coredump(const kernel_siginfo_t *siginfo)
823827
file_end_write(cprm.file);
824828
free_vma_snapshot(&cprm);
825829
}
826-
if (ispipe && core_pipe_limit)
830+
if ((cn.core_type == COREDUMP_PIPE) && core_pipe_limit)
827831
wait_for_dump_helpers(cprm.file);
828832
close_fail:
829833
if (cprm.file)
830834
filp_close(cprm.file, NULL);
831835
fail_dropcount:
832-
if (ispipe)
836+
if (cn.core_type == COREDUMP_PIPE)
833837
atomic_dec(&core_dump_count);
834838
fail_unlock:
835839
kfree(argv);

0 commit comments

Comments
 (0)