1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <sys/statvfs.h>
12 #include <elfutils/libdwfl.h>
15 #include "sd-daemon.h"
16 #include "sd-journal.h"
18 #include "sd-messages.h"
21 #include "alloc-util.h"
22 #include "bus-error.h"
23 #include "capability-util.h"
24 #include "cgroup-util.h"
26 #include "conf-parser.h"
28 #include "coredump-vacuum.h"
29 #include "dirent-util.h"
35 #include "journal-importer.h"
38 #include "main-func.h"
39 #include "memory-util.h"
40 #include "mkdir-label.h"
41 #include "parse-util.h"
42 #include "process-util.h"
43 #include "signal-util.h"
44 #include "socket-util.h"
46 #include "stacktrace.h"
47 #include "stat-util.h"
48 #include "string-table.h"
49 #include "string-util.h"
51 #include "sync-util.h"
52 #include "tmpfile-util.h"
53 #include "uid-alloc-range.h"
54 #include "user-util.h"
56 /* The maximum size up to which we process coredumps */
57 #define PROCESS_SIZE_MAX ((uint64_t) (2LLU*1024LLU*1024LLU*1024LLU))
59 /* The maximum size up to which we leave the coredump around on disk */
60 #define EXTERNAL_SIZE_MAX PROCESS_SIZE_MAX
62 /* The maximum size up to which we store the coredump in the journal */
63 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
64 #define JOURNAL_SIZE_MAX ((size_t) (767LU*1024LU*1024LU))
66 /* oss-fuzz limits memory usage. */
67 #define JOURNAL_SIZE_MAX ((size_t) (10LU*1024LU*1024LU))
70 /* When checking for available memory and setting lower limits, don't
71 * go below 4MB for writing core files to storage. */
72 #define PROCESS_SIZE_MIN (4U*1024U*1024U)
74 /* Make sure to not make this larger than the maximum journal entry
75 * size. See DATA_SIZE_MAX in journal-importer.h. */
76 assert_cc(JOURNAL_SIZE_MAX
<= DATA_SIZE_MAX
);
79 /* We use these as array indexes for our process metadata cache.
81 * The first indices of the cache stores the same metadata as the ones passed by
82 * the kernel via argv[], ie the strings array passed by the kernel according to
83 * our pattern defined in /proc/sys/kernel/core_pattern (see man:core(5)). */
85 META_ARGV_PID
, /* %P: as seen in the initial pid namespace */
86 META_ARGV_UID
, /* %u: as seen in the initial user namespace */
87 META_ARGV_GID
, /* %g: as seen in the initial user namespace */
88 META_ARGV_SIGNAL
, /* %s: number of signal causing dump */
89 META_ARGV_TIMESTAMP
, /* %t: time of dump, expressed as seconds since the Epoch (we expand this to µs granularity) */
90 META_ARGV_RLIMIT
, /* %c: core file size soft resource limit */
91 META_ARGV_HOSTNAME
, /* %h: hostname */
94 /* The following indexes are cached for a couple of special fields we use (and
95 * thereby need to be retrieved quickly) for naming coredump files, and attaching
96 * xattrs. Unlike the previous ones they are retrieved from the runtime
99 META_COMM
= _META_ARGV_MAX
,
102 /* The rest are similar to the previous ones except that we won't fail if one of
103 * them is missing. */
105 META_EXE
= _META_MANDATORY_MAX
,
110 static const char * const meta_field_names
[_META_MAX
] = {
111 [META_ARGV_PID
] = "COREDUMP_PID=",
112 [META_ARGV_UID
] = "COREDUMP_UID=",
113 [META_ARGV_GID
] = "COREDUMP_GID=",
114 [META_ARGV_SIGNAL
] = "COREDUMP_SIGNAL=",
115 [META_ARGV_TIMESTAMP
] = "COREDUMP_TIMESTAMP=",
116 [META_ARGV_RLIMIT
] = "COREDUMP_RLIMIT=",
117 [META_ARGV_HOSTNAME
] = "COREDUMP_HOSTNAME=",
118 [META_COMM
] = "COREDUMP_COMM=",
119 [META_EXE
] = "COREDUMP_EXE=",
120 [META_UNIT
] = "COREDUMP_UNIT=",
123 typedef struct Context
{
124 const char *meta
[_META_MAX
];
130 typedef enum CoredumpStorage
{
131 COREDUMP_STORAGE_NONE
,
132 COREDUMP_STORAGE_EXTERNAL
,
133 COREDUMP_STORAGE_JOURNAL
,
134 _COREDUMP_STORAGE_MAX
,
135 _COREDUMP_STORAGE_INVALID
= -EINVAL
,
138 static const char* const coredump_storage_table
[_COREDUMP_STORAGE_MAX
] = {
139 [COREDUMP_STORAGE_NONE
] = "none",
140 [COREDUMP_STORAGE_EXTERNAL
] = "external",
141 [COREDUMP_STORAGE_JOURNAL
] = "journal",
144 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(coredump_storage
, CoredumpStorage
);
145 static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage
, coredump_storage
, CoredumpStorage
, "Failed to parse storage setting");
147 static CoredumpStorage arg_storage
= COREDUMP_STORAGE_EXTERNAL
;
148 static bool arg_compress
= true;
149 static uint64_t arg_process_size_max
= PROCESS_SIZE_MAX
;
150 static uint64_t arg_external_size_max
= EXTERNAL_SIZE_MAX
;
151 static uint64_t arg_journal_size_max
= JOURNAL_SIZE_MAX
;
152 static uint64_t arg_keep_free
= UINT64_MAX
;
153 static uint64_t arg_max_use
= UINT64_MAX
;
155 static int parse_config(void) {
156 static const ConfigTableItem items
[] = {
157 { "Coredump", "Storage", config_parse_coredump_storage
, 0, &arg_storage
},
158 { "Coredump", "Compress", config_parse_bool
, 0, &arg_compress
},
159 { "Coredump", "ProcessSizeMax", config_parse_iec_uint64
, 0, &arg_process_size_max
},
160 { "Coredump", "ExternalSizeMax", config_parse_iec_uint64_infinity
, 0, &arg_external_size_max
},
161 { "Coredump", "JournalSizeMax", config_parse_iec_size
, 0, &arg_journal_size_max
},
162 { "Coredump", "KeepFree", config_parse_iec_uint64
, 0, &arg_keep_free
},
163 { "Coredump", "MaxUse", config_parse_iec_uint64
, 0, &arg_max_use
},
167 return config_parse_many_nulstr(
168 PKGSYSCONFDIR
"/coredump.conf",
169 CONF_PATHS_NULSTR("systemd/coredump.conf.d"),
171 config_item_table_lookup
, items
,
177 static uint64_t storage_size_max(void) {
178 if (arg_storage
== COREDUMP_STORAGE_EXTERNAL
)
179 return arg_external_size_max
;
180 if (arg_storage
== COREDUMP_STORAGE_JOURNAL
)
181 return arg_journal_size_max
;
182 assert(arg_storage
== COREDUMP_STORAGE_NONE
);
186 static int fix_acl(int fd
, uid_t uid
) {
192 assert(uid_is_valid(uid
));
194 if (uid_is_system(uid
) || uid_is_dynamic(uid
) || uid
== UID_NOBODY
)
197 /* Make sure normal users can read (but not write or delete) their own coredumps */
198 r
= fd_add_uid_acl_permission(fd
, uid
, ACL_READ
);
200 return log_error_errno(r
, "Failed to adjust ACL of the coredump: %m");
206 static int fix_xattr(int fd
, const Context
*context
) {
208 static const char * const xattrs
[_META_MAX
] = {
209 [META_ARGV_PID
] = "user.coredump.pid",
210 [META_ARGV_UID
] = "user.coredump.uid",
211 [META_ARGV_GID
] = "user.coredump.gid",
212 [META_ARGV_SIGNAL
] = "user.coredump.signal",
213 [META_ARGV_TIMESTAMP
] = "user.coredump.timestamp",
214 [META_ARGV_RLIMIT
] = "user.coredump.rlimit",
215 [META_ARGV_HOSTNAME
] = "user.coredump.hostname",
216 [META_COMM
] = "user.coredump.comm",
217 [META_EXE
] = "user.coredump.exe",
224 /* Attach some metadata to coredumps via extended
225 * attributes. Just because we can. */
227 for (unsigned i
= 0; i
< _META_MAX
; i
++) {
230 if (isempty(context
->meta
[i
]) || !xattrs
[i
])
233 k
= fsetxattr(fd
, xattrs
[i
], context
->meta
[i
], strlen(context
->meta
[i
]), XATTR_CREATE
);
241 #define filename_escape(s) xescape((s), "./ ")
243 static const char *coredump_tmpfile_name(const char *s
) {
244 return s
? s
: "(unnamed temporary file)";
247 static int fix_permissions(
249 const char *filename
,
251 const Context
*context
,
260 /* Ignore errors on these */
261 (void) fchmod(fd
, 0640);
262 (void) fix_acl(fd
, uid
);
263 (void) fix_xattr(fd
, context
);
267 return log_error_errno(r
, "Failed to sync coredump %s: %m", coredump_tmpfile_name(filename
));
269 r
= link_tmpfile(fd
, filename
, target
);
271 return log_error_errno(r
, "Failed to move coredump %s into place: %m", target
);
276 static int maybe_remove_external_coredump(const char *filename
, uint64_t size
) {
278 /* Returns 1 if might remove, 0 if will not remove, < 0 on error. */
280 if (arg_storage
== COREDUMP_STORAGE_EXTERNAL
&&
281 size
<= arg_external_size_max
)
287 if (unlink(filename
) < 0 && errno
!= ENOENT
)
288 return log_error_errno(errno
, "Failed to unlink %s: %m", filename
);
293 static int make_filename(const Context
*context
, char **ret
) {
294 _cleanup_free_
char *c
= NULL
, *u
= NULL
, *p
= NULL
, *t
= NULL
;
295 sd_id128_t boot
= {};
300 c
= filename_escape(context
->meta
[META_COMM
]);
304 u
= filename_escape(context
->meta
[META_ARGV_UID
]);
308 r
= sd_id128_get_boot(&boot
);
312 p
= filename_escape(context
->meta
[META_ARGV_PID
]);
316 t
= filename_escape(context
->meta
[META_ARGV_TIMESTAMP
]);
321 "/var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR
".%s.%s",
324 SD_ID128_FORMAT_VAL(boot
),
332 static int save_external_coredump(
333 const Context
*context
,
339 uint64_t *ret_compressed_size
,
340 bool *ret_truncated
) {
342 _cleanup_(unlink_and_freep
) char *tmp
= NULL
;
343 _cleanup_free_
char *fn
= NULL
;
344 _cleanup_close_
int fd
= -1;
345 uint64_t rlimit
, process_limit
, max_size
;
346 bool truncated
, storage_on_tmpfs
;
352 assert(ret_filename
);
356 assert(ret_compressed_size
);
357 assert(ret_truncated
);
359 r
= parse_uid(context
->meta
[META_ARGV_UID
], &uid
);
361 return log_error_errno(r
, "Failed to parse UID: %m");
363 r
= safe_atou64(context
->meta
[META_ARGV_RLIMIT
], &rlimit
);
365 return log_error_errno(r
, "Failed to parse resource limit '%s': %m",
366 context
->meta
[META_ARGV_RLIMIT
]);
367 if (rlimit
< page_size())
368 /* Is coredumping disabled? Then don't bother saving/processing the
369 * coredump. Anything below PAGE_SIZE cannot give a readable coredump
370 * (the kernel uses ELF_EXEC_PAGESIZE which is not easily accessible, but
371 * is usually the same as PAGE_SIZE. */
372 return log_info_errno(SYNTHETIC_ERRNO(EBADSLT
),
373 "Resource limits disable core dumping for process %s (%s).",
374 context
->meta
[META_ARGV_PID
], context
->meta
[META_COMM
]);
376 process_limit
= MAX(arg_process_size_max
, storage_size_max());
377 if (process_limit
== 0)
378 return log_debug_errno(SYNTHETIC_ERRNO(EBADSLT
),
379 "Limits for coredump processing and storage are both 0, not dumping core.");
381 /* Never store more than the process configured, or than we actually shall keep or process */
382 max_size
= MIN(rlimit
, process_limit
);
384 r
= make_filename(context
, &fn
);
386 return log_error_errno(r
, "Failed to determine coredump file name: %m");
388 (void) mkdir_parents_label(fn
, 0755);
390 fd
= open_tmpfile_linkable(fn
, O_RDWR
|O_CLOEXEC
, &tmp
);
392 return log_error_errno(fd
, "Failed to create temporary file for coredump %s: %m", fn
);
394 /* If storage is on tmpfs, the kernel oomd might kill us if there's MemoryMax set on
395 * the service or the slice it belongs to. This is common on low-resources systems,
396 * to avoid crashing processes to take away too many system resources.
397 * Check the cgroup settings, and set max_size to a bit less than half of the
398 * available memory left to the process.
399 * Then, attempt to write the core file uncompressed first - if the write gets
400 * interrupted, we know we won't be able to write it all, so instead compress what
401 * was written so far, delete the uncompressed truncated core, and then continue
402 * compressing from STDIN. Given the compressed core cannot be larger than the
403 * uncompressed one, and 1KB for metadata is accounted for in the calculation, we
404 * should be able to at least store the full compressed core file. */
406 storage_on_tmpfs
= fd_is_temporary_fs(fd
) > 0;
407 if (storage_on_tmpfs
&& arg_compress
) {
408 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
409 uint64_t cgroup_limit
= UINT64_MAX
;
412 /* If we can't get the cgroup limit, just ignore it, but don't fail,
413 * try anyway with the config settings. */
414 r
= sd_bus_default_system(&bus
);
416 log_info_errno(r
, "Failed to connect to system bus, skipping MemoryAvailable check: %m");
418 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
420 r
= sd_bus_get_property_trivial(
422 "org.freedesktop.systemd1",
423 "/org/freedesktop/systemd1/unit/self",
424 "org.freedesktop.systemd1.Service",
430 "Failed to query MemoryAvailable for current unit, "
431 "falling back to static config settings: %s",
432 bus_error_message(&error
, r
));
435 max_size
= MIN(cgroup_limit
, max_size
);
436 max_size
= LESS_BY(max_size
, 1024U) / 2; /* Account for 1KB metadata overhead for compressing */
437 max_size
= MAX(PROCESS_SIZE_MIN
, max_size
); /* Impose a lower minimum */
439 /* tmpfs might get full quickly, so check the available space too.
440 * But don't worry about errors here, failing to access the storage
441 * location will be better logged when writing to it. */
442 if (statvfs("/var/lib/systemd/coredump/", &sv
) >= 0)
443 max_size
= MIN((uint64_t)sv
.f_frsize
* (uint64_t)sv
.f_bfree
, max_size
);
445 log_debug("Limiting core file size to %" PRIu64
" bytes due to cgroup memory limits.", max_size
);
448 r
= copy_bytes(input_fd
, fd
, max_size
, 0);
450 return log_error_errno(r
, "Cannot store coredump of %s (%s): %m",
451 context
->meta
[META_ARGV_PID
], context
->meta
[META_COMM
]);
456 _cleanup_(unlink_and_freep
) char *tmp_compressed
= NULL
;
457 _cleanup_free_
char *fn_compressed
= NULL
;
458 _cleanup_close_
int fd_compressed
= -1;
459 uint64_t uncompressed_size
= 0;
461 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1)
462 return log_error_errno(errno
, "Failed to seek on coredump %s: %m", fn
);
464 fn_compressed
= strjoin(fn
, COMPRESSED_EXT
);
468 fd_compressed
= open_tmpfile_linkable(fn_compressed
, O_RDWR
|O_CLOEXEC
, &tmp_compressed
);
469 if (fd_compressed
< 0)
470 return log_error_errno(fd_compressed
, "Failed to create temporary file for coredump %s: %m", fn_compressed
);
472 r
= compress_stream(fd
, fd_compressed
, max_size
, &uncompressed_size
);
474 return log_error_errno(r
, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed
));
476 if (truncated
&& storage_on_tmpfs
) {
477 uint64_t partial_uncompressed_size
= 0;
479 /* Uncompressed write was truncated and we are writing to tmpfs: delete
480 * the uncompressed core, and compress the remaining part from STDIN. */
482 tmp
= unlink_and_free(tmp
);
485 r
= compress_stream(input_fd
, fd_compressed
, max_size
, &partial_uncompressed_size
);
487 return log_error_errno(r
, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed
));
488 uncompressed_size
+= partial_uncompressed_size
;
491 r
= fix_permissions(fd_compressed
, tmp_compressed
, fn_compressed
, context
, uid
);
495 if (fstat(fd_compressed
, &st
) < 0)
496 return log_error_errno(errno
,
497 "Failed to fstat core file %s: %m",
498 coredump_tmpfile_name(tmp_compressed
));
500 *ret_filename
= TAKE_PTR(fn_compressed
); /* compressed */
501 *ret_node_fd
= TAKE_FD(fd_compressed
); /* compressed */
502 *ret_compressed_size
= (uint64_t) st
.st_size
; /* compressed */
503 *ret_data_fd
= TAKE_FD(fd
);
504 *ret_size
= uncompressed_size
;
505 *ret_truncated
= truncated
;
506 tmp_compressed
= mfree(tmp_compressed
);
514 LOG_MESSAGE("Core file was truncated to %zu bytes.", max_size
),
515 "SIZE_LIMIT=%zu", max_size
,
516 "MESSAGE_ID=" SD_MESSAGE_TRUNCATED_CORE_STR
);
518 r
= fix_permissions(fd
, tmp
, fn
, context
, uid
);
520 return log_error_errno(r
, "Failed to fix permissions and finalize coredump %s into %s: %m", coredump_tmpfile_name(tmp
), fn
);
522 if (fstat(fd
, &st
) < 0)
523 return log_error_errno(errno
, "Failed to fstat core file %s: %m", coredump_tmpfile_name(tmp
));
525 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1)
526 return log_error_errno(errno
, "Failed to seek on coredump %s: %m", fn
);
528 *ret_filename
= TAKE_PTR(fn
);
529 *ret_data_fd
= TAKE_FD(fd
);
530 *ret_size
= (uint64_t) st
.st_size
;
531 *ret_truncated
= truncated
;
536 static int allocate_journal_field(int fd
, size_t size
, char **ret
, size_t *ret_size
) {
537 _cleanup_free_
char *field
= NULL
;
544 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1)
545 return log_warning_errno(errno
, "Failed to seek: %m");
547 field
= malloc(9 + size
);
549 log_warning("Failed to allocate memory for coredump, coredump will not be stored.");
553 memcpy(field
, "COREDUMP=", 9);
555 n
= read(fd
, field
+ 9, size
);
557 return log_error_errno((int) n
, "Failed to read core data: %m");
558 if ((size_t) n
< size
)
559 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
560 "Core data too short.");
562 *ret
= TAKE_PTR(field
);
563 *ret_size
= size
+ 9;
568 /* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines:
582 static int compose_open_fds(pid_t pid
, char **open_fds
) {
583 _cleanup_closedir_
DIR *proc_fd_dir
= NULL
;
584 _cleanup_close_
int proc_fdinfo_fd
= -1;
585 _cleanup_free_
char *buffer
= NULL
;
586 _cleanup_fclose_
FILE *stream
= NULL
;
587 const char *fddelim
= "", *path
;
588 struct dirent
*dent
= NULL
;
593 assert(open_fds
!= NULL
);
595 path
= procfs_file_alloca(pid
, "fd");
596 proc_fd_dir
= opendir(path
);
600 proc_fdinfo_fd
= openat(dirfd(proc_fd_dir
), "../fdinfo", O_DIRECTORY
|O_NOFOLLOW
|O_CLOEXEC
|O_PATH
);
601 if (proc_fdinfo_fd
< 0)
604 stream
= open_memstream_unlocked(&buffer
, &size
);
608 FOREACH_DIRENT(dent
, proc_fd_dir
, return -errno
) {
609 _cleanup_fclose_
FILE *fdinfo
= NULL
;
610 _cleanup_free_
char *fdname
= NULL
;
611 _cleanup_close_
int fd
= -1;
613 r
= readlinkat_malloc(dirfd(proc_fd_dir
), dent
->d_name
, &fdname
);
617 fprintf(stream
, "%s%s:%s\n", fddelim
, dent
->d_name
, fdname
);
620 /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */
621 fd
= openat(proc_fdinfo_fd
, dent
->d_name
, O_NOFOLLOW
|O_CLOEXEC
|O_RDONLY
);
625 fdinfo
= take_fdopen(&fd
, "r");
630 _cleanup_free_
char *line
= NULL
;
632 r
= read_line(fdinfo
, LONG_LINE_MAX
, &line
);
644 stream
= safe_fclose(stream
);
649 *open_fds
= TAKE_PTR(buffer
);
654 static int get_process_ns(pid_t pid
, const char *namespace, ino_t
*ns
) {
657 _cleanup_close_
int proc_ns_dir_fd
= -1;
659 p
= procfs_file_alloca(pid
, "ns");
661 proc_ns_dir_fd
= open(p
, O_DIRECTORY
| O_CLOEXEC
| O_RDONLY
);
662 if (proc_ns_dir_fd
< 0)
665 if (fstatat(proc_ns_dir_fd
, namespace, &stbuf
, /* flags */0) < 0)
672 static int get_mount_namespace_leader(pid_t pid
, pid_t
*ret
) {
676 r
= get_process_ns(pid
, "mnt", &proc_mntns
);
684 r
= get_process_ppid(pid
, &ppid
);
685 if (r
== -EADDRNOTAVAIL
) /* Reached the top (i.e. typically PID 1, but could also be a process
686 * whose parent is not in our pidns) */
691 r
= get_process_ns(ppid
, "mnt", &parent_mntns
);
695 if (proc_mntns
!= parent_mntns
) {
704 /* Returns 1 if the parent was found.
705 * Returns 0 if there is not a process we can call the pid's
706 * container parent (the pid's process isn't 'containerized').
707 * Returns a negative number on errors.
709 static int get_process_container_parent_cmdline(pid_t pid
, char** cmdline
) {
712 const char *proc_root_path
;
713 struct stat root_stat
, proc_root_stat
;
715 /* To compare inodes of / and /proc/[pid]/root */
716 if (stat("/", &root_stat
) < 0)
719 proc_root_path
= procfs_file_alloca(pid
, "root");
720 if (stat(proc_root_path
, &proc_root_stat
) < 0)
723 /* The process uses system root. */
724 if (proc_root_stat
.st_ino
== root_stat
.st_ino
) {
729 r
= get_mount_namespace_leader(pid
, &container_pid
);
733 r
= get_process_cmdline(container_pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
, cmdline
);
740 static int change_uid_gid(const Context
*context
) {
745 r
= parse_uid(context
->meta
[META_ARGV_UID
], &uid
);
749 if (uid_is_system(uid
)) {
750 const char *user
= "systemd-coredump";
752 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
, 0);
754 log_warning_errno(r
, "Cannot resolve %s user. Proceeding to dump core as root: %m", user
);
758 r
= parse_gid(context
->meta
[META_ARGV_GID
], &gid
);
763 return drop_privileges(uid
, gid
, 0);
766 static int submit_coredump(
768 struct iovec_wrapper
*iovw
,
771 _cleanup_(json_variant_unrefp
) JsonVariant
*json_metadata
= NULL
;
772 _cleanup_close_
int coredump_fd
= -1, coredump_node_fd
= -1;
773 _cleanup_free_
char *filename
= NULL
, *coredump_data
= NULL
;
774 _cleanup_free_
char *stacktrace
= NULL
;
776 const char *module_name
;
777 uint64_t coredump_size
= UINT64_MAX
, coredump_compressed_size
= UINT64_MAX
;
778 bool truncated
= false;
779 JsonVariant
*module_json
;
783 assert(input_fd
>= 0);
785 /* Vacuum before we write anything again */
786 (void) coredump_vacuum(-1, arg_keep_free
, arg_max_use
);
788 /* Always stream the coredump to disk, if that's possible */
789 r
= save_external_coredump(context
, input_fd
,
790 &filename
, &coredump_node_fd
, &coredump_fd
,
791 &coredump_size
, &coredump_compressed_size
, &truncated
);
793 /* Skip whole core dumping part */
796 /* If we don't want to keep the coredump on disk, remove it now, as later on we
797 * will lack the privileges for it. However, we keep the fd to it, so that we can
798 * still process it and log it. */
799 r
= maybe_remove_external_coredump(filename
, coredump_node_fd
>= 0 ? coredump_compressed_size
: coredump_size
);
803 (void) iovw_put_string_field(iovw
, "COREDUMP_FILENAME=", filename
);
805 } else if (arg_storage
== COREDUMP_STORAGE_EXTERNAL
)
806 log_info("The core will not be stored: size %"PRIu64
" is greater than %"PRIu64
" (the configured maximum)",
807 coredump_node_fd
>= 0 ? coredump_compressed_size
: coredump_size
, arg_external_size_max
);
809 /* Vacuum again, but exclude the coredump we just created */
810 (void) coredump_vacuum(coredump_node_fd
>= 0 ? coredump_node_fd
: coredump_fd
, arg_keep_free
, arg_max_use
);
812 /* Now, let's drop privileges to become the user who owns the segfaulted process
813 * and allocate the coredump memory under the user's uid. This also ensures that
814 * the credentials journald will see are the ones of the coredumping user, thus
815 * making sure the user gets access to the core dump. Let's also get rid of all
816 * capabilities, if we run as root, we won't need them anymore. */
817 r
= change_uid_gid(context
);
819 return log_error_errno(r
, "Failed to drop privileges: %m");
822 /* Try to get a stack trace if we can */
823 if (coredump_size
> arg_process_size_max
) {
824 log_debug("Not generating stack trace: core size %"PRIu64
" is greater "
825 "than %"PRIu64
" (the configured maximum)",
826 coredump_size
, arg_process_size_max
);
827 } else if (coredump_fd
>= 0)
828 (void) parse_elf_object(coredump_fd
,
829 context
->meta
[META_EXE
],
830 /* fork_disable_dump= */endswith(context
->meta
[META_EXE
], "systemd-coredump"), /* avoid loops */
836 core_message
= strjoina("Process ", context
->meta
[META_ARGV_PID
],
837 " (", context
->meta
[META_COMM
], ") of user ",
838 context
->meta
[META_ARGV_UID
], " dumped core.",
839 context
->is_journald
&& filename
? "\nCoredump diverted to " : NULL
,
840 context
->is_journald
&& filename
? filename
: NULL
);
842 core_message
= strjoina(core_message
, stacktrace
? "\n\n" : NULL
, stacktrace
);
844 if (context
->is_journald
) {
845 /* We cannot log to the journal, so just print the message.
846 * The target was set previously to something safe. */
847 log_dispatch(LOG_ERR
, 0, core_message
);
851 (void) iovw_put_string_field(iovw
, "MESSAGE=", core_message
);
854 (void) iovw_put_string_field(iovw
, "COREDUMP_TRUNCATED=", "1");
856 /* If we managed to parse any ELF metadata (build-id, ELF package meta),
857 * attach it as journal metadata. */
859 _cleanup_free_
char *formatted_json
= NULL
;
861 r
= json_variant_format(json_metadata
, 0, &formatted_json
);
863 return log_error_errno(r
, "Failed to format JSON package metadata: %m");
865 (void) iovw_put_string_field(iovw
, "COREDUMP_PACKAGE_JSON=", formatted_json
);
868 JSON_VARIANT_OBJECT_FOREACH(module_name
, module_json
, json_metadata
) {
869 JsonVariant
*package_name
, *package_version
;
871 /* We only add structured fields for the 'main' ELF module */
872 if (!path_equal_filename(module_name
, context
->meta
[META_EXE
]))
875 package_name
= json_variant_by_key(module_json
, "name");
877 (void) iovw_put_string_field(iovw
, "COREDUMP_PACKAGE_NAME=", json_variant_string(package_name
));
879 package_version
= json_variant_by_key(module_json
, "version");
881 (void) iovw_put_string_field(iovw
, "COREDUMP_PACKAGE_VERSION=", json_variant_string(package_version
));
884 /* Optionally store the entire coredump in the journal */
885 if (arg_storage
== COREDUMP_STORAGE_JOURNAL
&& coredump_fd
>= 0) {
886 if (coredump_size
<= arg_journal_size_max
) {
889 /* Store the coredump itself in the journal */
891 r
= allocate_journal_field(coredump_fd
, (size_t) coredump_size
, &coredump_data
, &sz
);
893 if (iovw_put(iovw
, coredump_data
, sz
) >= 0)
894 TAKE_PTR(coredump_data
);
896 log_warning_errno(r
, "Failed to attach the core to the journal entry: %m");
898 log_info("The core will not be stored: size %"PRIu64
" is greater than %"PRIu64
" (the configured maximum)",
899 coredump_size
, arg_journal_size_max
);
902 r
= sd_journal_sendv(iovw
->iovec
, iovw
->count
);
904 return log_error_errno(r
, "Failed to log coredump: %m");
909 static int save_context(Context
*context
, const struct iovec_wrapper
*iovw
) {
916 assert(iovw
->count
>= _META_ARGV_MAX
);
918 /* The context does not allocate any memory on its own */
920 for (size_t n
= 0; n
< iovw
->count
; n
++) {
921 struct iovec
*iovec
= iovw
->iovec
+ n
;
923 for (size_t i
= 0; i
< ELEMENTSOF(meta_field_names
); i
++) {
926 /* Note that these strings are NUL terminated, because we made sure that a
927 * trailing NUL byte is in the buffer, though not included in the iov_len
928 * count (see process_socket() and gather_pid_metadata_*()) */
929 assert(((char*) iovec
->iov_base
)[iovec
->iov_len
] == 0);
931 p
= startswith(iovec
->iov_base
, meta_field_names
[i
]);
933 context
->meta
[i
] = p
;
940 if (!context
->meta
[META_ARGV_PID
])
941 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
942 "Failed to find the PID of crashing process");
944 r
= parse_pid(context
->meta
[META_ARGV_PID
], &context
->pid
);
946 return log_error_errno(r
, "Failed to parse PID \"%s\": %m", context
->meta
[META_ARGV_PID
]);
948 unit
= context
->meta
[META_UNIT
];
949 context
->is_pid1
= streq(context
->meta
[META_ARGV_PID
], "1") || streq_ptr(unit
, SPECIAL_INIT_SCOPE
);
950 context
->is_journald
= streq_ptr(unit
, SPECIAL_JOURNALD_SERVICE
);
955 static int process_socket(int fd
) {
956 _cleanup_close_
int input_fd
= -1;
957 Context context
= {};
958 struct iovec_wrapper iovw
= {};
966 log_debug("Processing coredump received on stdin...");
969 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control
;
971 .msg_control
= &control
,
972 .msg_controllen
= sizeof(control
),
978 l
= next_datagram_size_fd(fd
);
980 r
= log_error_errno(l
, "Failed to determine datagram size to read: %m");
985 iovec
.iov_base
= malloc(l
+ 1);
986 if (!iovec
.iov_base
) {
993 n
= recvmsg_safe(fd
, &mh
, MSG_CMSG_CLOEXEC
);
995 free(iovec
.iov_base
);
996 r
= log_error_errno(n
, "Failed to receive datagram: %m");
1000 /* The final zero-length datagram carries the file descriptor and tells us
1001 * that we're done. */
1003 struct cmsghdr
*found
;
1005 free(iovec
.iov_base
);
1007 found
= cmsg_find(&mh
, SOL_SOCKET
, SCM_RIGHTS
, CMSG_LEN(sizeof(int)));
1009 cmsg_close_all(&mh
);
1010 r
= log_error_errno(SYNTHETIC_ERRNO(EBADMSG
),
1011 "Coredump file descriptor missing.");
1015 assert(input_fd
< 0);
1016 input_fd
= *(int*) CMSG_DATA(found
);
1019 cmsg_close_all(&mh
);
1021 /* Add trailing NUL byte, in case these are strings */
1022 ((char*) iovec
.iov_base
)[n
] = 0;
1023 iovec
.iov_len
= (size_t) n
;
1025 r
= iovw_put(&iovw
, iovec
.iov_base
, iovec
.iov_len
);
1030 /* Make sure we got all data we really need */
1031 assert(input_fd
>= 0);
1033 r
= save_context(&context
, &iovw
);
1037 /* Make sure we received at least all fields we need. */
1038 for (int i
= 0; i
< _META_MANDATORY_MAX
; i
++)
1039 if (!context
.meta
[i
]) {
1040 r
= log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1041 "A mandatory argument (%i) has not been sent, aborting.",
1046 r
= submit_coredump(&context
, &iovw
, input_fd
);
1049 iovw_free_contents(&iovw
, true);
1053 static int send_iovec(const struct iovec_wrapper
*iovw
, int input_fd
) {
1055 static const union sockaddr_union sa
= {
1056 .un
.sun_family
= AF_UNIX
,
1057 .un
.sun_path
= "/run/systemd/coredump",
1059 _cleanup_close_
int fd
= -1;
1063 assert(input_fd
>= 0);
1065 fd
= socket(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0);
1067 return log_error_errno(errno
, "Failed to create coredump socket: %m");
1069 if (connect(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0)
1070 return log_error_errno(errno
, "Failed to connect to coredump service: %m");
1072 for (size_t i
= 0; i
< iovw
->count
; i
++) {
1073 struct msghdr mh
= {
1074 .msg_iov
= iovw
->iovec
+ i
,
1077 struct iovec copy
[2];
1080 if (sendmsg(fd
, &mh
, MSG_NOSIGNAL
) >= 0)
1083 if (errno
== EMSGSIZE
&& mh
.msg_iov
[0].iov_len
> 0) {
1084 /* This field didn't fit? That's a pity. Given that this is
1085 * just metadata, let's truncate the field at half, and try
1086 * again. We append three dots, in order to show that this is
1089 if (mh
.msg_iov
!= copy
) {
1090 /* We don't want to modify the caller's iovec, hence
1091 * let's create our own array, consisting of two new
1092 * iovecs, where the first is a (truncated) copy of
1093 * what we want to send, and the second one contains
1094 * the trailing dots. */
1095 copy
[0] = iovw
->iovec
[i
];
1096 copy
[1] = IOVEC_MAKE(((char[]){'.', '.', '.'}), 3);
1102 copy
[0].iov_len
/= 2; /* halve it, and try again */
1106 return log_error_errno(errno
, "Failed to send coredump datagram: %m");
1110 r
= send_one_fd(fd
, input_fd
, 0);
1112 return log_error_errno(r
, "Failed to send coredump fd: %m");
1117 static int gather_pid_metadata_from_argv(
1118 struct iovec_wrapper
*iovw
,
1120 int argc
, char **argv
) {
1122 _cleanup_free_
char *free_timestamp
= NULL
;
1126 /* We gather all metadata that were passed via argv[] into an array of iovecs that
1127 * we'll forward to the socket unit */
1129 if (argc
< _META_ARGV_MAX
)
1130 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1131 "Not enough arguments passed by the kernel (%i, expected %i).",
1132 argc
, _META_ARGV_MAX
);
1134 for (int i
= 0; i
< _META_ARGV_MAX
; i
++) {
1140 case META_ARGV_TIMESTAMP
:
1141 /* The journal fields contain the timestamp padded with six
1142 * zeroes, so that the kernel-supplied 1s granularity timestamps
1143 * becomes 1µs granularity, i.e. the granularity systemd usually
1145 t
= free_timestamp
= strjoin(argv
[i
], "000000");
1150 case META_ARGV_SIGNAL
:
1151 /* For signal, record its pretty name too */
1152 if (safe_atoi(argv
[i
], &signo
) >= 0 && SIGNAL_VALID(signo
))
1153 (void) iovw_put_string_field(iovw
, "COREDUMP_SIGNAL_NAME=SIG",
1154 signal_to_string(signo
));
1161 r
= iovw_put_string_field(iovw
, meta_field_names
[i
], t
);
1166 /* Cache some of the process metadata we collected so far and that we'll need to
1168 return save_context(context
, iovw
);
1171 static int gather_pid_metadata(struct iovec_wrapper
*iovw
, Context
*context
) {
1178 /* Note that if we fail on oom later on, we do not roll-back changes to the iovec
1179 * structure. (It remains valid, with the first iovec fields initialized.) */
1183 /* The following is mandatory */
1184 r
= get_process_comm(pid
, &t
);
1186 return log_error_errno(r
, "Failed to get COMM: %m");
1188 r
= iovw_put_string_field_free(iovw
, "COREDUMP_COMM=", t
);
1192 /* The following are optional but we used them if present */
1193 r
= get_process_exe(pid
, &t
);
1195 r
= iovw_put_string_field_free(iovw
, "COREDUMP_EXE=", t
);
1197 log_warning_errno(r
, "Failed to get EXE, ignoring: %m");
1199 if (cg_pid_get_unit(pid
, &t
) >= 0)
1200 (void) iovw_put_string_field_free(iovw
, "COREDUMP_UNIT=", t
);
1202 /* The next are optional */
1203 if (cg_pid_get_user_unit(pid
, &t
) >= 0)
1204 (void) iovw_put_string_field_free(iovw
, "COREDUMP_USER_UNIT=", t
);
1206 if (sd_pid_get_session(pid
, &t
) >= 0)
1207 (void) iovw_put_string_field_free(iovw
, "COREDUMP_SESSION=", t
);
1209 if (sd_pid_get_owner_uid(pid
, &owner_uid
) >= 0) {
1210 r
= asprintf(&t
, UID_FMT
, owner_uid
);
1212 (void) iovw_put_string_field_free(iovw
, "COREDUMP_OWNER_UID=", t
);
1215 if (sd_pid_get_slice(pid
, &t
) >= 0)
1216 (void) iovw_put_string_field_free(iovw
, "COREDUMP_SLICE=", t
);
1218 if (get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_QUOTE_POSIX
, &t
) >= 0)
1219 (void) iovw_put_string_field_free(iovw
, "COREDUMP_CMDLINE=", t
);
1221 if (cg_pid_get_path_shifted(pid
, NULL
, &t
) >= 0)
1222 (void) iovw_put_string_field_free(iovw
, "COREDUMP_CGROUP=", t
);
1224 if (compose_open_fds(pid
, &t
) >= 0)
1225 (void) iovw_put_string_field_free(iovw
, "COREDUMP_OPEN_FDS=", t
);
1227 p
= procfs_file_alloca(pid
, "status");
1228 if (read_full_virtual_file(p
, &t
, NULL
) >= 0)
1229 (void) iovw_put_string_field_free(iovw
, "COREDUMP_PROC_STATUS=", t
);
1231 p
= procfs_file_alloca(pid
, "maps");
1232 if (read_full_virtual_file(p
, &t
, NULL
) >= 0)
1233 (void) iovw_put_string_field_free(iovw
, "COREDUMP_PROC_MAPS=", t
);
1235 p
= procfs_file_alloca(pid
, "limits");
1236 if (read_full_virtual_file(p
, &t
, NULL
) >= 0)
1237 (void) iovw_put_string_field_free(iovw
, "COREDUMP_PROC_LIMITS=", t
);
1239 p
= procfs_file_alloca(pid
, "cgroup");
1240 if (read_full_virtual_file(p
, &t
, NULL
) >=0)
1241 (void) iovw_put_string_field_free(iovw
, "COREDUMP_PROC_CGROUP=", t
);
1243 p
= procfs_file_alloca(pid
, "mountinfo");
1244 if (read_full_virtual_file(p
, &t
, NULL
) >=0)
1245 (void) iovw_put_string_field_free(iovw
, "COREDUMP_PROC_MOUNTINFO=", t
);
1247 if (get_process_cwd(pid
, &t
) >= 0)
1248 (void) iovw_put_string_field_free(iovw
, "COREDUMP_CWD=", t
);
1250 if (get_process_root(pid
, &t
) >= 0) {
1251 bool proc_self_root_is_slash
;
1253 proc_self_root_is_slash
= strcmp(t
, "/") == 0;
1255 (void) iovw_put_string_field_free(iovw
, "COREDUMP_ROOT=", t
);
1257 /* If the process' root is "/", then there is a chance it has
1258 * mounted own root and hence being containerized. */
1259 if (proc_self_root_is_slash
&& get_process_container_parent_cmdline(pid
, &t
) > 0)
1260 (void) iovw_put_string_field_free(iovw
, "COREDUMP_CONTAINER_CMDLINE=", t
);
1263 if (get_process_environ(pid
, &t
) >= 0)
1264 (void) iovw_put_string_field_free(iovw
, "COREDUMP_ENVIRON=", t
);
1266 /* we successfully acquired all metadata */
1267 return save_context(context
, iovw
);
1270 static int process_kernel(int argc
, char* argv
[]) {
1271 Context context
= {};
1272 struct iovec_wrapper
*iovw
;
1275 log_debug("Processing coredump received from the kernel...");
1281 (void) iovw_put_string_field(iovw
, "MESSAGE_ID=", SD_MESSAGE_COREDUMP_STR
);
1282 (void) iovw_put_string_field(iovw
, "PRIORITY=", STRINGIFY(LOG_CRIT
));
1284 /* Collect all process metadata passed by the kernel through argv[] */
1285 r
= gather_pid_metadata_from_argv(iovw
, &context
, argc
- 1, argv
+ 1);
1289 /* Collect the rest of the process metadata retrieved from the runtime */
1290 r
= gather_pid_metadata(iovw
, &context
);
1294 if (!context
.is_journald
) {
1295 /* OK, now we know it's not the journal, hence we can make use of it now. */
1296 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG
);
1300 /* If this is PID 1 disable coredump collection, we'll unlikely be able to process
1303 * FIXME: maybe we should disable coredumps generation from the beginning and
1304 * re-enable it only when we know it's either safe (ie we're not running OOM) or
1305 * it's not pid1 ? */
1306 if (context
.is_pid1
) {
1307 log_notice("Due to PID 1 having crashed coredump collection will now be turned off.");
1308 disable_coredumps();
1311 if (context
.is_journald
|| context
.is_pid1
)
1312 r
= submit_coredump(&context
, iovw
, STDIN_FILENO
);
1314 r
= send_iovec(iovw
, STDIN_FILENO
);
1317 iovw
= iovw_free_free(iovw
);
1321 static int process_backtrace(int argc
, char *argv
[]) {
1322 Context context
= {};
1323 struct iovec_wrapper
*iovw
;
1326 _cleanup_(journal_importer_cleanup
) JournalImporter importer
= JOURNAL_IMPORTER_INIT(STDIN_FILENO
);
1328 log_debug("Processing backtrace on stdin...");
1334 (void) iovw_put_string_field(iovw
, "MESSAGE_ID=", SD_MESSAGE_BACKTRACE_STR
);
1335 (void) iovw_put_string_field(iovw
, "PRIORITY=", STRINGIFY(LOG_CRIT
));
1337 /* Collect all process metadata from argv[] by making sure to skip the
1338 * '--backtrace' option */
1339 r
= gather_pid_metadata_from_argv(iovw
, &context
, argc
- 2, argv
+ 2);
1343 /* Collect the rest of the process metadata retrieved from the runtime */
1344 r
= gather_pid_metadata(iovw
, &context
);
1349 r
= journal_importer_process_data(&importer
);
1351 log_error_errno(r
, "Failed to parse journal entry on stdin: %m");
1354 if (r
== 1 || /* complete entry */
1355 journal_importer_eof(&importer
)) /* end of data */
1359 if (journal_importer_eof(&importer
)) {
1360 log_warning("Did not receive a full journal entry on stdin, ignoring message sent by reporter");
1362 message
= strjoina("Process ", context
.meta
[META_ARGV_PID
],
1363 " (", context
.meta
[META_COMM
], ")"
1364 " of user ", context
.meta
[META_ARGV_UID
],
1365 " failed with ", context
.meta
[META_ARGV_SIGNAL
]);
1367 r
= iovw_put_string_field(iovw
, "MESSAGE=", message
);
1371 /* The imported iovecs are not supposed to be freed by us so let's store
1372 * them at the end of the array so we can skip them while freeing the
1374 for (size_t i
= 0; i
< importer
.iovw
.count
; i
++) {
1375 struct iovec
*iovec
= importer
.iovw
.iovec
+ i
;
1377 iovw_put(iovw
, iovec
->iov_base
, iovec
->iov_len
);
1381 r
= sd_journal_sendv(iovw
->iovec
, iovw
->count
);
1383 log_error_errno(r
, "Failed to log backtrace: %m");
1386 iovw
->count
-= importer
.iovw
.count
;
1387 iovw
= iovw_free_free(iovw
);
1391 static int run(int argc
, char *argv
[]) {
1394 /* First, log to a safe place, since we don't know what crashed and it might
1395 * be journald which we'd rather not log to then. */
1397 log_set_target(LOG_TARGET_KMSG
);
1400 /* Make sure we never enter a loop */
1401 (void) prctl(PR_SET_DUMPABLE
, 0);
1403 /* Ignore all parse errors */
1404 (void) parse_config();
1406 log_debug("Selected storage '%s'.", coredump_storage_to_string(arg_storage
));
1407 log_debug("Selected compression %s.", yes_no(arg_compress
));
1409 r
= sd_listen_fds(false);
1411 return log_error_errno(r
, "Failed to determine the number of file descriptors: %m");
1413 /* If we got an fd passed, we are running in coredumpd mode. Otherwise we
1414 * are invoked from the kernel as coredump handler. */
1416 if (streq_ptr(argv
[1], "--backtrace"))
1417 return process_backtrace(argc
, argv
);
1419 return process_kernel(argc
, argv
);
1421 return process_socket(SD_LISTEN_FDS_START
);
1423 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1424 "Received unexpected number of file descriptors.");
1427 DEFINE_MAIN_FUNCTION(run
);