Skip to content

Commit c114e99

Browse files
romank-msftkees
authored andcommitted
coredump: Standartize and fix logging
The coredump code does not log the process ID and the comm consistently, logs unescaped comm when it does log it, and does not always use the ratelimited logging. That makes it harder to analyze logs and puts the system at the risk of spamming the system log incase something crashes many times over and over again. Fix that by logging TGID and comm (escaped) consistently and using the ratelimited logging always. Signed-off-by: Roman Kisel <[email protected]> Tested-by: Allen Pais <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent de9c2c6 commit c114e99

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

fs/coredump.c

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
586586
struct subprocess_info *sub_info;
587587

588588
if (ispipe < 0) {
589-
printk(KERN_WARNING "format_corename failed\n");
590-
printk(KERN_WARNING "Aborting core\n");
589+
coredump_report_failure("format_corename failed, aborting core");
591590
goto fail_unlock;
592591
}
593592

@@ -607,27 +606,21 @@ void do_coredump(const kernel_siginfo_t *siginfo)
607606
* right pid if a thread in a multi-threaded
608607
* core_pattern process dies.
609608
*/
610-
printk(KERN_WARNING
611-
"Process %d(%s) has RLIMIT_CORE set to 1\n",
612-
task_tgid_vnr(current), current->comm);
613-
printk(KERN_WARNING "Aborting core\n");
609+
coredump_report_failure("RLIMIT_CORE is set to 1, aborting core");
614610
goto fail_unlock;
615611
}
616612
cprm.limit = RLIM_INFINITY;
617613

618614
dump_count = atomic_inc_return(&core_dump_count);
619615
if (core_pipe_limit && (core_pipe_limit < dump_count)) {
620-
printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
621-
task_tgid_vnr(current), current->comm);
622-
printk(KERN_WARNING "Skipping core dump\n");
616+
coredump_report_failure("over core_pipe_limit, skipping core dump");
623617
goto fail_dropcount;
624618
}
625619

626620
helper_argv = kmalloc_array(argc + 1, sizeof(*helper_argv),
627621
GFP_KERNEL);
628622
if (!helper_argv) {
629-
printk(KERN_WARNING "%s failed to allocate memory\n",
630-
__func__);
623+
coredump_report_failure("%s failed to allocate memory", __func__);
631624
goto fail_dropcount;
632625
}
633626
for (argi = 0; argi < argc; argi++)
@@ -644,8 +637,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
644637

645638
kfree(helper_argv);
646639
if (retval) {
647-
printk(KERN_INFO "Core dump to |%s pipe failed\n",
648-
cn.corename);
640+
coredump_report_failure("|%s pipe failed", cn.corename);
649641
goto close_fail;
650642
}
651643
} else {
@@ -658,10 +650,8 @@ void do_coredump(const kernel_siginfo_t *siginfo)
658650
goto fail_unlock;
659651

660652
if (need_suid_safe && cn.corename[0] != '/') {
661-
printk(KERN_WARNING "Pid %d(%s) can only dump core "\
662-
"to fully qualified path!\n",
663-
task_tgid_vnr(current), current->comm);
664-
printk(KERN_WARNING "Skipping core dump\n");
653+
coredump_report_failure(
654+
"this process can only dump core to a fully qualified path, skipping core dump");
665655
goto fail_unlock;
666656
}
667657

@@ -730,13 +720,13 @@ void do_coredump(const kernel_siginfo_t *siginfo)
730720
idmap = file_mnt_idmap(cprm.file);
731721
if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode),
732722
current_fsuid())) {
733-
pr_info_ratelimited("Core dump to %s aborted: cannot preserve file owner\n",
734-
cn.corename);
723+
coredump_report_failure("Core dump to %s aborted: "
724+
"cannot preserve file owner", cn.corename);
735725
goto close_fail;
736726
}
737727
if ((inode->i_mode & 0677) != 0600) {
738-
pr_info_ratelimited("Core dump to %s aborted: cannot preserve file permissions\n",
739-
cn.corename);
728+
coredump_report_failure("Core dump to %s aborted: "
729+
"cannot preserve file permissions", cn.corename);
740730
goto close_fail;
741731
}
742732
if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
@@ -757,7 +747,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
757747
* have this set to NULL.
758748
*/
759749
if (!cprm.file) {
760-
pr_info("Core dump to |%s disabled\n", cn.corename);
750+
coredump_report_failure("Core dump to |%s disabled", cn.corename);
761751
goto close_fail;
762752
}
763753
if (!dump_vma_snapshot(&cprm))
@@ -983,11 +973,10 @@ void validate_coredump_safety(void)
983973
{
984974
if (suid_dumpable == SUID_DUMP_ROOT &&
985975
core_pattern[0] != '/' && core_pattern[0] != '|') {
986-
pr_warn(
987-
"Unsafe core_pattern used with fs.suid_dumpable=2.\n"
988-
"Pipe handler or fully qualified core dump path required.\n"
989-
"Set kernel.core_pattern before fs.suid_dumpable.\n"
990-
);
976+
977+
coredump_report_failure("Unsafe core_pattern used with fs.suid_dumpable=2: "
978+
"pipe handler or fully qualified core dump path required. "
979+
"Set kernel.core_pattern before fs.suid_dumpable.");
991980
}
992981
}
993982

include/linux/coredump.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,30 @@ extern int dump_align(struct coredump_params *cprm, int align);
4343
int dump_user_range(struct coredump_params *cprm, unsigned long start,
4444
unsigned long len);
4545
extern void do_coredump(const kernel_siginfo_t *siginfo);
46+
47+
/*
48+
* Logging for the coredump code, ratelimited.
49+
* The TGID and comm fields are added to the message.
50+
*/
51+
52+
#define __COREDUMP_PRINTK(Level, Format, ...) \
53+
do { \
54+
char comm[TASK_COMM_LEN]; \
55+
\
56+
get_task_comm(comm, current); \
57+
printk_ratelimited(Level "coredump: %d(%*pE): " Format "\n", \
58+
task_tgid_vnr(current), (int)strlen(comm), comm, ##__VA_ARGS__); \
59+
} while (0) \
60+
61+
#define coredump_report(fmt, ...) __COREDUMP_PRINTK(KERN_INFO, fmt, ##__VA_ARGS__)
62+
#define coredump_report_failure(fmt, ...) __COREDUMP_PRINTK(KERN_WARNING, fmt, ##__VA_ARGS__)
63+
4664
#else
4765
static inline void do_coredump(const kernel_siginfo_t *siginfo) {}
66+
67+
#define coredump_report(...)
68+
#define coredump_report_failure(...)
69+
4870
#endif
4971

5072
#if defined(CONFIG_COREDUMP) && defined(CONFIG_SYSCTL)

0 commit comments

Comments
 (0)