]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/coredump/coredump.c
coredump: Fix format string type mismatch
[thirdparty/systemd.git] / src / coredump / coredump.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <sys/prctl.h>
6 #include <sys/statvfs.h>
7 #include <sys/xattr.h>
8 #include <unistd.h>
9
10 #include "sd-daemon.h"
11 #include "sd-journal.h"
12 #include "sd-login.h"
13 #include "sd-messages.h"
14
15 #include "acl-util.h"
16 #include "alloc-util.h"
17 #include "bus-error.h"
18 #include "capability-util.h"
19 #include "cgroup-util.h"
20 #include "compress.h"
21 #include "conf-parser.h"
22 #include "copy.h"
23 #include "coredump-vacuum.h"
24 #include "dirent-util.h"
25 #include "elf-util.h"
26 #include "escape.h"
27 #include "fd-util.h"
28 #include "fileio.h"
29 #include "fs-util.h"
30 #include "io-util.h"
31 #include "journal-importer.h"
32 #include "log.h"
33 #include "macro.h"
34 #include "main-func.h"
35 #include "memory-util.h"
36 #include "mkdir-label.h"
37 #include "parse-util.h"
38 #include "process-util.h"
39 #include "signal-util.h"
40 #include "socket-util.h"
41 #include "special.h"
42 #include "stat-util.h"
43 #include "string-table.h"
44 #include "string-util.h"
45 #include "strv.h"
46 #include "sync-util.h"
47 #include "tmpfile-util.h"
48 #include "uid-alloc-range.h"
49 #include "user-util.h"
50
51 /* The maximum size up to which we process coredumps. We use 1G on 32bit systems, and 32G on 64bit systems */
52 #if __SIZEOF_POINTER__ == 4
53 #define PROCESS_SIZE_MAX ((uint64_t) (1LLU*1024LLU*1024LLU*1024LLU))
54 #elif __SIZEOF_POINTER__ == 8
55 #define PROCESS_SIZE_MAX ((uint64_t) (32LLU*1024LLU*1024LLU*1024LLU))
56 #else
57 #error "Unexpected pointer size"
58 #endif
59
60 /* The maximum size up to which we leave the coredump around on disk */
61 #define EXTERNAL_SIZE_MAX PROCESS_SIZE_MAX
62
63 /* The maximum size up to which we store the coredump in the journal */
64 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
65 #define JOURNAL_SIZE_MAX ((size_t) (767LU*1024LU*1024LU))
66 #else
67 /* oss-fuzz limits memory usage. */
68 #define JOURNAL_SIZE_MAX ((size_t) (10LU*1024LU*1024LU))
69 #endif
70
71 /* When checking for available memory and setting lower limits, don't
72 * go below 4MB for writing core files to storage. */
73 #define PROCESS_SIZE_MIN (4U*1024U*1024U)
74
75 /* Make sure to not make this larger than the maximum journal entry
76 * size. See DATA_SIZE_MAX in journal-importer.h. */
77 assert_cc(JOURNAL_SIZE_MAX <= DATA_SIZE_MAX);
78
79 enum {
80 /* We use these as array indexes for our process metadata cache.
81 *
82 * The first indices of the cache stores the same metadata as the ones passed by
83 * the kernel via argv[], ie the strings array passed by the kernel according to
84 * our pattern defined in /proc/sys/kernel/core_pattern (see man:core(5)). */
85
86 META_ARGV_PID, /* %P: as seen in the initial pid namespace */
87 META_ARGV_UID, /* %u: as seen in the initial user namespace */
88 META_ARGV_GID, /* %g: as seen in the initial user namespace */
89 META_ARGV_SIGNAL, /* %s: number of signal causing dump */
90 META_ARGV_TIMESTAMP, /* %t: time of dump, expressed as seconds since the Epoch (we expand this to µs granularity) */
91 META_ARGV_RLIMIT, /* %c: core file size soft resource limit */
92 META_ARGV_HOSTNAME, /* %h: hostname */
93 _META_ARGV_MAX,
94
95 /* The following indexes are cached for a couple of special fields we use (and
96 * thereby need to be retrieved quickly) for naming coredump files, and attaching
97 * xattrs. Unlike the previous ones they are retrieved from the runtime
98 * environment. */
99
100 META_COMM = _META_ARGV_MAX,
101 _META_MANDATORY_MAX,
102
103 /* The rest are similar to the previous ones except that we won't fail if one of
104 * them is missing. */
105
106 META_EXE = _META_MANDATORY_MAX,
107 META_UNIT,
108 _META_MAX
109 };
110
111 static const char * const meta_field_names[_META_MAX] = {
112 [META_ARGV_PID] = "COREDUMP_PID=",
113 [META_ARGV_UID] = "COREDUMP_UID=",
114 [META_ARGV_GID] = "COREDUMP_GID=",
115 [META_ARGV_SIGNAL] = "COREDUMP_SIGNAL=",
116 [META_ARGV_TIMESTAMP] = "COREDUMP_TIMESTAMP=",
117 [META_ARGV_RLIMIT] = "COREDUMP_RLIMIT=",
118 [META_ARGV_HOSTNAME] = "COREDUMP_HOSTNAME=",
119 [META_COMM] = "COREDUMP_COMM=",
120 [META_EXE] = "COREDUMP_EXE=",
121 [META_UNIT] = "COREDUMP_UNIT=",
122 };
123
124 typedef struct Context {
125 const char *meta[_META_MAX];
126 pid_t pid;
127 bool is_pid1;
128 bool is_journald;
129 } Context;
130
131 typedef enum CoredumpStorage {
132 COREDUMP_STORAGE_NONE,
133 COREDUMP_STORAGE_EXTERNAL,
134 COREDUMP_STORAGE_JOURNAL,
135 _COREDUMP_STORAGE_MAX,
136 _COREDUMP_STORAGE_INVALID = -EINVAL,
137 } CoredumpStorage;
138
139 static const char* const coredump_storage_table[_COREDUMP_STORAGE_MAX] = {
140 [COREDUMP_STORAGE_NONE] = "none",
141 [COREDUMP_STORAGE_EXTERNAL] = "external",
142 [COREDUMP_STORAGE_JOURNAL] = "journal",
143 };
144
145 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(coredump_storage, CoredumpStorage);
146 static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting");
147
148 static CoredumpStorage arg_storage = COREDUMP_STORAGE_EXTERNAL;
149 static bool arg_compress = true;
150 static uint64_t arg_process_size_max = PROCESS_SIZE_MAX;
151 static uint64_t arg_external_size_max = EXTERNAL_SIZE_MAX;
152 static uint64_t arg_journal_size_max = JOURNAL_SIZE_MAX;
153 static uint64_t arg_keep_free = UINT64_MAX;
154 static uint64_t arg_max_use = UINT64_MAX;
155
156 static int parse_config(void) {
157 static const ConfigTableItem items[] = {
158 { "Coredump", "Storage", config_parse_coredump_storage, 0, &arg_storage },
159 { "Coredump", "Compress", config_parse_bool, 0, &arg_compress },
160 { "Coredump", "ProcessSizeMax", config_parse_iec_uint64, 0, &arg_process_size_max },
161 { "Coredump", "ExternalSizeMax", config_parse_iec_uint64_infinity, 0, &arg_external_size_max },
162 { "Coredump", "JournalSizeMax", config_parse_iec_size, 0, &arg_journal_size_max },
163 { "Coredump", "KeepFree", config_parse_iec_uint64, 0, &arg_keep_free },
164 { "Coredump", "MaxUse", config_parse_iec_uint64, 0, &arg_max_use },
165 {}
166 };
167
168 return config_parse_many_nulstr(
169 PKGSYSCONFDIR "/coredump.conf",
170 CONF_PATHS_NULSTR("systemd/coredump.conf.d"),
171 "Coredump\0",
172 config_item_table_lookup, items,
173 CONFIG_PARSE_WARN,
174 NULL,
175 NULL);
176 }
177
178 static uint64_t storage_size_max(void) {
179 if (arg_storage == COREDUMP_STORAGE_EXTERNAL)
180 return arg_external_size_max;
181 if (arg_storage == COREDUMP_STORAGE_JOURNAL)
182 return arg_journal_size_max;
183 assert(arg_storage == COREDUMP_STORAGE_NONE);
184 return 0;
185 }
186
187 static int fix_acl(int fd, uid_t uid) {
188
189 #if HAVE_ACL
190 int r;
191
192 assert(fd >= 0);
193 assert(uid_is_valid(uid));
194
195 if (uid_is_system(uid) || uid_is_dynamic(uid) || uid == UID_NOBODY)
196 return 0;
197
198 /* Make sure normal users can read (but not write or delete) their own coredumps */
199 r = fd_add_uid_acl_permission(fd, uid, ACL_READ);
200 if (r < 0)
201 return log_error_errno(r, "Failed to adjust ACL of the coredump: %m");
202 #endif
203
204 return 0;
205 }
206
207 static int fix_xattr(int fd, const Context *context) {
208
209 static const char * const xattrs[_META_MAX] = {
210 [META_ARGV_PID] = "user.coredump.pid",
211 [META_ARGV_UID] = "user.coredump.uid",
212 [META_ARGV_GID] = "user.coredump.gid",
213 [META_ARGV_SIGNAL] = "user.coredump.signal",
214 [META_ARGV_TIMESTAMP] = "user.coredump.timestamp",
215 [META_ARGV_RLIMIT] = "user.coredump.rlimit",
216 [META_ARGV_HOSTNAME] = "user.coredump.hostname",
217 [META_COMM] = "user.coredump.comm",
218 [META_EXE] = "user.coredump.exe",
219 };
220
221 int r = 0;
222
223 assert(fd >= 0);
224
225 /* Attach some metadata to coredumps via extended
226 * attributes. Just because we can. */
227
228 for (unsigned i = 0; i < _META_MAX; i++) {
229 int k;
230
231 if (isempty(context->meta[i]) || !xattrs[i])
232 continue;
233
234 k = fsetxattr(fd, xattrs[i], context->meta[i], strlen(context->meta[i]), XATTR_CREATE);
235 if (k < 0 && r == 0)
236 r = -errno;
237 }
238
239 return r;
240 }
241
242 #define filename_escape(s) xescape((s), "./ ")
243
244 static const char *coredump_tmpfile_name(const char *s) {
245 return s ? s : "(unnamed temporary file)";
246 }
247
248 static int fix_permissions(
249 int fd,
250 const char *filename,
251 const char *target,
252 const Context *context,
253 uid_t uid) {
254
255 int r;
256
257 assert(fd >= 0);
258 assert(target);
259 assert(context);
260
261 /* Ignore errors on these */
262 (void) fchmod(fd, 0640);
263 (void) fix_acl(fd, uid);
264 (void) fix_xattr(fd, context);
265
266 r = fsync_full(fd);
267 if (r < 0)
268 return log_error_errno(r, "Failed to sync coredump %s: %m", coredump_tmpfile_name(filename));
269
270 r = link_tmpfile(fd, filename, target);
271 if (r < 0)
272 return log_error_errno(r, "Failed to move coredump %s into place: %m", target);
273
274 return 0;
275 }
276
277 static int maybe_remove_external_coredump(const char *filename, uint64_t size) {
278
279 /* Returns 1 if might remove, 0 if will not remove, < 0 on error. */
280
281 if (arg_storage == COREDUMP_STORAGE_EXTERNAL &&
282 size <= arg_external_size_max)
283 return 0;
284
285 if (!filename)
286 return 1;
287
288 if (unlink(filename) < 0 && errno != ENOENT)
289 return log_error_errno(errno, "Failed to unlink %s: %m", filename);
290
291 return 1;
292 }
293
294 static int make_filename(const Context *context, char **ret) {
295 _cleanup_free_ char *c = NULL, *u = NULL, *p = NULL, *t = NULL;
296 sd_id128_t boot = {};
297 int r;
298
299 assert(context);
300
301 c = filename_escape(context->meta[META_COMM]);
302 if (!c)
303 return -ENOMEM;
304
305 u = filename_escape(context->meta[META_ARGV_UID]);
306 if (!u)
307 return -ENOMEM;
308
309 r = sd_id128_get_boot(&boot);
310 if (r < 0)
311 return r;
312
313 p = filename_escape(context->meta[META_ARGV_PID]);
314 if (!p)
315 return -ENOMEM;
316
317 t = filename_escape(context->meta[META_ARGV_TIMESTAMP]);
318 if (!t)
319 return -ENOMEM;
320
321 if (asprintf(ret,
322 "/var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR ".%s.%s",
323 c,
324 u,
325 SD_ID128_FORMAT_VAL(boot),
326 p,
327 t) < 0)
328 return -ENOMEM;
329
330 return 0;
331 }
332
333 static int save_external_coredump(
334 const Context *context,
335 int input_fd,
336 char **ret_filename,
337 int *ret_node_fd,
338 int *ret_data_fd,
339 uint64_t *ret_size,
340 uint64_t *ret_compressed_size,
341 bool *ret_truncated) {
342
343 _cleanup_(unlink_and_freep) char *tmp = NULL;
344 _cleanup_free_ char *fn = NULL;
345 _cleanup_close_ int fd = -1;
346 uint64_t rlimit, process_limit, max_size;
347 bool truncated, storage_on_tmpfs;
348 struct stat st;
349 uid_t uid;
350 int r;
351
352 assert(context);
353 assert(ret_filename);
354 assert(ret_node_fd);
355 assert(ret_data_fd);
356 assert(ret_size);
357 assert(ret_compressed_size);
358 assert(ret_truncated);
359
360 r = parse_uid(context->meta[META_ARGV_UID], &uid);
361 if (r < 0)
362 return log_error_errno(r, "Failed to parse UID: %m");
363
364 r = safe_atou64(context->meta[META_ARGV_RLIMIT], &rlimit);
365 if (r < 0)
366 return log_error_errno(r, "Failed to parse resource limit '%s': %m",
367 context->meta[META_ARGV_RLIMIT]);
368 if (rlimit < page_size())
369 /* Is coredumping disabled? Then don't bother saving/processing the
370 * coredump. Anything below PAGE_SIZE cannot give a readable coredump
371 * (the kernel uses ELF_EXEC_PAGESIZE which is not easily accessible, but
372 * is usually the same as PAGE_SIZE. */
373 return log_info_errno(SYNTHETIC_ERRNO(EBADSLT),
374 "Resource limits disable core dumping for process %s (%s).",
375 context->meta[META_ARGV_PID], context->meta[META_COMM]);
376
377 process_limit = MAX(arg_process_size_max, storage_size_max());
378 if (process_limit == 0)
379 return log_debug_errno(SYNTHETIC_ERRNO(EBADSLT),
380 "Limits for coredump processing and storage are both 0, not dumping core.");
381
382 /* Never store more than the process configured, or than we actually shall keep or process */
383 max_size = MIN(rlimit, process_limit);
384
385 r = make_filename(context, &fn);
386 if (r < 0)
387 return log_error_errno(r, "Failed to determine coredump file name: %m");
388
389 (void) mkdir_parents_label(fn, 0755);
390
391 fd = open_tmpfile_linkable(fn, O_RDWR|O_CLOEXEC, &tmp);
392 if (fd < 0)
393 return log_error_errno(fd, "Failed to create temporary file for coredump %s: %m", fn);
394
395 /* If storage is on tmpfs, the kernel oomd might kill us if there's MemoryMax set on
396 * the service or the slice it belongs to. This is common on low-resources systems,
397 * to avoid crashing processes to take away too many system resources.
398 * Check the cgroup settings, and set max_size to a bit less than half of the
399 * available memory left to the process.
400 * Then, attempt to write the core file uncompressed first - if the write gets
401 * interrupted, we know we won't be able to write it all, so instead compress what
402 * was written so far, delete the uncompressed truncated core, and then continue
403 * compressing from STDIN. Given the compressed core cannot be larger than the
404 * uncompressed one, and 1KB for metadata is accounted for in the calculation, we
405 * should be able to at least store the full compressed core file. */
406
407 storage_on_tmpfs = fd_is_temporary_fs(fd) > 0;
408 if (storage_on_tmpfs && arg_compress) {
409 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
410 uint64_t cgroup_limit = UINT64_MAX;
411 struct statvfs sv;
412
413 /* If we can't get the cgroup limit, just ignore it, but don't fail,
414 * try anyway with the config settings. */
415 r = sd_bus_default_system(&bus);
416 if (r < 0)
417 log_info_errno(r, "Failed to connect to system bus, skipping MemoryAvailable check: %m");
418 else {
419 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
420
421 r = sd_bus_get_property_trivial(
422 bus,
423 "org.freedesktop.systemd1",
424 "/org/freedesktop/systemd1/unit/self",
425 "org.freedesktop.systemd1.Service",
426 "MemoryAvailable",
427 &error,
428 't', &cgroup_limit);
429 if (r < 0)
430 log_warning_errno(r,
431 "Failed to query MemoryAvailable for current unit, "
432 "falling back to static config settings: %s",
433 bus_error_message(&error, r));
434 }
435
436 max_size = MIN(cgroup_limit, max_size);
437 max_size = LESS_BY(max_size, 1024U) / 2; /* Account for 1KB metadata overhead for compressing */
438 max_size = MAX(PROCESS_SIZE_MIN, max_size); /* Impose a lower minimum */
439
440 /* tmpfs might get full quickly, so check the available space too.
441 * But don't worry about errors here, failing to access the storage
442 * location will be better logged when writing to it. */
443 if (statvfs("/var/lib/systemd/coredump/", &sv) >= 0)
444 max_size = MIN((uint64_t)sv.f_frsize * (uint64_t)sv.f_bfree, max_size);
445
446 log_debug("Limiting core file size to %" PRIu64 " bytes due to cgroup memory limits.", max_size);
447 }
448
449 r = copy_bytes(input_fd, fd, max_size, 0);
450 if (r < 0)
451 return log_error_errno(r, "Cannot store coredump of %s (%s): %m",
452 context->meta[META_ARGV_PID], context->meta[META_COMM]);
453 truncated = r == 1;
454
455 #if HAVE_COMPRESSION
456 if (arg_compress) {
457 _cleanup_(unlink_and_freep) char *tmp_compressed = NULL;
458 _cleanup_free_ char *fn_compressed = NULL;
459 _cleanup_close_ int fd_compressed = -1;
460 uint64_t uncompressed_size = 0;
461
462 if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
463 return log_error_errno(errno, "Failed to seek on coredump %s: %m", fn);
464
465 fn_compressed = strjoin(fn, default_compression_extension());
466 if (!fn_compressed)
467 return log_oom();
468
469 fd_compressed = open_tmpfile_linkable(fn_compressed, O_RDWR|O_CLOEXEC, &tmp_compressed);
470 if (fd_compressed < 0)
471 return log_error_errno(fd_compressed, "Failed to create temporary file for coredump %s: %m", fn_compressed);
472
473 r = compress_stream(fd, fd_compressed, max_size, &uncompressed_size);
474 if (r < 0)
475 return log_error_errno(r, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed));
476
477 if (truncated && storage_on_tmpfs) {
478 uint64_t partial_uncompressed_size = 0;
479
480 /* Uncompressed write was truncated and we are writing to tmpfs: delete
481 * the uncompressed core, and compress the remaining part from STDIN. */
482
483 tmp = unlink_and_free(tmp);
484 fd = safe_close(fd);
485
486 r = compress_stream(input_fd, fd_compressed, max_size, &partial_uncompressed_size);
487 if (r < 0)
488 return log_error_errno(r, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed));
489 uncompressed_size += partial_uncompressed_size;
490 }
491
492 r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, context, uid);
493 if (r < 0)
494 return r;
495
496 if (fstat(fd_compressed, &st) < 0)
497 return log_error_errno(errno,
498 "Failed to fstat core file %s: %m",
499 coredump_tmpfile_name(tmp_compressed));
500
501 *ret_filename = TAKE_PTR(fn_compressed); /* compressed */
502 *ret_node_fd = TAKE_FD(fd_compressed); /* compressed */
503 *ret_compressed_size = (uint64_t) st.st_size; /* compressed */
504 *ret_data_fd = TAKE_FD(fd);
505 *ret_size = uncompressed_size;
506 *ret_truncated = truncated;
507 tmp_compressed = mfree(tmp_compressed);
508
509 return 0;
510 }
511 #endif
512
513 if (truncated)
514 log_struct(LOG_INFO,
515 LOG_MESSAGE("Core file was truncated to %"PRIu64" bytes.", max_size),
516 "SIZE_LIMIT=%"PRIu64, max_size,
517 "MESSAGE_ID=" SD_MESSAGE_TRUNCATED_CORE_STR);
518
519 r = fix_permissions(fd, tmp, fn, context, uid);
520 if (r < 0)
521 return log_error_errno(r, "Failed to fix permissions and finalize coredump %s into %s: %m", coredump_tmpfile_name(tmp), fn);
522
523 if (fstat(fd, &st) < 0)
524 return log_error_errno(errno, "Failed to fstat core file %s: %m", coredump_tmpfile_name(tmp));
525
526 if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
527 return log_error_errno(errno, "Failed to seek on coredump %s: %m", fn);
528
529 *ret_filename = TAKE_PTR(fn);
530 *ret_data_fd = TAKE_FD(fd);
531 *ret_size = (uint64_t) st.st_size;
532 *ret_truncated = truncated;
533
534 return 0;
535 }
536
537 static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_size) {
538 _cleanup_free_ char *field = NULL;
539 ssize_t n;
540
541 assert(fd >= 0);
542 assert(ret);
543 assert(ret_size);
544
545 if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
546 return log_warning_errno(errno, "Failed to seek: %m");
547
548 field = malloc(9 + size);
549 if (!field) {
550 log_warning("Failed to allocate memory for coredump, coredump will not be stored.");
551 return -ENOMEM;
552 }
553
554 memcpy(field, "COREDUMP=", 9);
555
556 n = read(fd, field + 9, size);
557 if (n < 0)
558 return log_error_errno((int) n, "Failed to read core data: %m");
559 if ((size_t) n < size)
560 return log_error_errno(SYNTHETIC_ERRNO(EIO),
561 "Core data too short.");
562
563 *ret = TAKE_PTR(field);
564 *ret_size = size + 9;
565
566 return 0;
567 }
568
569 /* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines:
570 * 0:/dev/pts/23
571 * pos: 0
572 * flags: 0100002
573 *
574 * 1:/dev/pts/23
575 * pos: 0
576 * flags: 0100002
577 *
578 * 2:/dev/pts/23
579 * pos: 0
580 * flags: 0100002
581 * EOF
582 */
583 static int compose_open_fds(pid_t pid, char **open_fds) {
584 _cleanup_closedir_ DIR *proc_fd_dir = NULL;
585 _cleanup_close_ int proc_fdinfo_fd = -1;
586 _cleanup_free_ char *buffer = NULL;
587 _cleanup_fclose_ FILE *stream = NULL;
588 const char *fddelim = "", *path;
589 size_t size = 0;
590 int r;
591
592 assert(pid >= 0);
593 assert(open_fds != NULL);
594
595 path = procfs_file_alloca(pid, "fd");
596 proc_fd_dir = opendir(path);
597 if (!proc_fd_dir)
598 return -errno;
599
600 proc_fdinfo_fd = openat(dirfd(proc_fd_dir), "../fdinfo", O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC|O_PATH);
601 if (proc_fdinfo_fd < 0)
602 return -errno;
603
604 stream = open_memstream_unlocked(&buffer, &size);
605 if (!stream)
606 return -ENOMEM;
607
608 FOREACH_DIRENT(de, proc_fd_dir, return -errno) {
609 _cleanup_fclose_ FILE *fdinfo = NULL;
610 _cleanup_free_ char *fdname = NULL;
611 _cleanup_close_ int fd = -1;
612
613 r = readlinkat_malloc(dirfd(proc_fd_dir), de->d_name, &fdname);
614 if (r < 0)
615 return r;
616
617 fprintf(stream, "%s%s:%s\n", fddelim, de->d_name, fdname);
618 fddelim = "\n";
619
620 /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */
621 fd = openat(proc_fdinfo_fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY);
622 if (fd < 0)
623 continue;
624
625 fdinfo = take_fdopen(&fd, "r");
626 if (!fdinfo)
627 continue;
628
629 for (;;) {
630 _cleanup_free_ char *line = NULL;
631
632 r = read_line(fdinfo, LONG_LINE_MAX, &line);
633 if (r < 0)
634 return r;
635 if (r == 0)
636 break;
637
638 fputs(line, stream);
639 fputc('\n', stream);
640 }
641 }
642
643 errno = 0;
644 stream = safe_fclose(stream);
645
646 if (errno > 0)
647 return -errno;
648
649 *open_fds = TAKE_PTR(buffer);
650
651 return 0;
652 }
653
654 static int get_process_ns(pid_t pid, const char *namespace, ino_t *ns) {
655 const char *p;
656 struct stat stbuf;
657 _cleanup_close_ int proc_ns_dir_fd = -1;
658
659 p = procfs_file_alloca(pid, "ns");
660
661 proc_ns_dir_fd = open(p, O_DIRECTORY | O_CLOEXEC | O_RDONLY);
662 if (proc_ns_dir_fd < 0)
663 return -errno;
664
665 if (fstatat(proc_ns_dir_fd, namespace, &stbuf, /* flags */0) < 0)
666 return -errno;
667
668 *ns = stbuf.st_ino;
669 return 0;
670 }
671
672 static int get_mount_namespace_leader(pid_t pid, pid_t *ret) {
673 ino_t proc_mntns;
674 int r;
675
676 r = get_process_ns(pid, "mnt", &proc_mntns);
677 if (r < 0)
678 return r;
679
680 for (;;) {
681 ino_t parent_mntns;
682 pid_t ppid;
683
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) */
687 return -ENOENT;
688 if (r < 0)
689 return r;
690
691 r = get_process_ns(ppid, "mnt", &parent_mntns);
692 if (r < 0)
693 return r;
694
695 if (proc_mntns != parent_mntns) {
696 *ret = ppid;
697 return 0;
698 }
699
700 pid = ppid;
701 }
702 }
703
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.
708 */
709 static int get_process_container_parent_cmdline(pid_t pid, char** cmdline) {
710 pid_t container_pid;
711 const char *proc_root_path;
712 struct stat root_stat, proc_root_stat;
713 int r;
714
715 /* To compare inodes of / and /proc/[pid]/root */
716 if (stat("/", &root_stat) < 0)
717 return -errno;
718
719 proc_root_path = procfs_file_alloca(pid, "root");
720 if (stat(proc_root_path, &proc_root_stat) < 0)
721 return -errno;
722
723 /* The process uses system root. */
724 if (stat_inode_same(&proc_root_stat, &root_stat)) {
725 *cmdline = NULL;
726 return 0;
727 }
728
729 r = get_mount_namespace_leader(pid, &container_pid);
730 if (r < 0)
731 return r;
732
733 r = get_process_cmdline(container_pid, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, cmdline);
734 if (r < 0)
735 return r;
736
737 return 1;
738 }
739
740 static int change_uid_gid(const Context *context) {
741 uid_t uid;
742 gid_t gid;
743 int r;
744
745 r = parse_uid(context->meta[META_ARGV_UID], &uid);
746 if (r < 0)
747 return r;
748
749 if (uid_is_system(uid)) {
750 const char *user = "systemd-coredump";
751
752 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
753 if (r < 0) {
754 log_warning_errno(r, "Cannot resolve %s user. Proceeding to dump core as root: %m", user);
755 uid = gid = 0;
756 }
757 } else {
758 r = parse_gid(context->meta[META_ARGV_GID], &gid);
759 if (r < 0)
760 return r;
761 }
762
763 return drop_privileges(uid, gid, 0);
764 }
765
766 static int submit_coredump(
767 Context *context,
768 struct iovec_wrapper *iovw,
769 int input_fd) {
770
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;
775 char *core_message;
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;
780 int r;
781
782 assert(context);
783 assert(iovw);
784 assert(input_fd >= 0);
785
786 /* Vacuum before we write anything again */
787 (void) coredump_vacuum(-1, arg_keep_free, arg_max_use);
788
789 /* Always stream the coredump to disk, if that's possible */
790 r = save_external_coredump(context, input_fd,
791 &filename, &coredump_node_fd, &coredump_fd,
792 &coredump_size, &coredump_compressed_size, &truncated);
793 if (r < 0)
794 /* Skip whole core dumping part */
795 goto log;
796
797 /* If we don't want to keep the coredump on disk, remove it now, as later on we
798 * will lack the privileges for it. However, we keep the fd to it, so that we can
799 * still process it and log it. */
800 r = maybe_remove_external_coredump(filename, coredump_node_fd >= 0 ? coredump_compressed_size : coredump_size);
801 if (r < 0)
802 return r;
803 if (r == 0)
804 (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);
808
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);
811
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);
818 if (r < 0)
819 return log_error_errno(r, "Failed to drop privileges: %m");
820
821 /* Try to get a stack trace if we can */
822 if (coredump_size > arg_process_size_max)
823 log_debug("Not generating stack trace: core size %"PRIu64" is greater "
824 "than %"PRIu64" (the configured maximum)",
825 coredump_size, arg_process_size_max);
826 else if (coredump_fd >= 0) {
827 bool skip = startswith(context->meta[META_COMM], "systemd-coredum"); /* COMM is 16 bytes usually */
828
829 (void) parse_elf_object(coredump_fd,
830 context->meta[META_EXE],
831 /* fork_disable_dump= */ skip, /* avoid loops */
832 &stacktrace,
833 &json_metadata);
834 }
835
836 log:
837 core_message = strjoina("Process ", context->meta[META_ARGV_PID],
838 " (", context->meta[META_COMM], ") of user ",
839 context->meta[META_ARGV_UID], " dumped core.",
840 context->is_journald && filename ? "\nCoredump diverted to " : NULL,
841 context->is_journald && filename ? filename : NULL);
842
843 core_message = strjoina(core_message, stacktrace ? "\n\n" : NULL, stacktrace);
844
845 if (context->is_journald) {
846 /* We cannot log to the journal, so just print the message.
847 * The target was set previously to something safe. */
848 log_dispatch(LOG_ERR, 0, core_message);
849 return 0;
850 }
851
852 (void) iovw_put_string_field(iovw, "MESSAGE=", core_message);
853
854 if (truncated)
855 (void) iovw_put_string_field(iovw, "COREDUMP_TRUNCATED=", "1");
856
857 /* If we managed to parse any ELF metadata (build-id, ELF package meta),
858 * attach it as journal metadata. */
859 if (json_metadata) {
860 _cleanup_free_ char *formatted_json = NULL;
861
862 r = json_variant_format(json_metadata, 0, &formatted_json);
863 if (r < 0)
864 return log_error_errno(r, "Failed to format JSON package metadata: %m");
865
866 (void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_JSON=", formatted_json);
867 }
868
869 /* In the unlikely scenario that context->meta[META_EXE] is not available,
870 * let's avoid guessing the module name and skip the loop. */
871 if (context->meta[META_EXE])
872 JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, json_metadata) {
873 JsonVariant *t;
874
875 /* We only add structured fields for the 'main' ELF module, and only if we can identify it. */
876 if (!path_equal_filename(module_name, context->meta[META_EXE]))
877 continue;
878
879 t = json_variant_by_key(module_json, "name");
880 if (t)
881 (void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_NAME=", json_variant_string(t));
882
883 t = json_variant_by_key(module_json, "version");
884 if (t)
885 (void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_VERSION=", json_variant_string(t));
886 }
887
888 /* Optionally store the entire coredump in the journal */
889 if (arg_storage == COREDUMP_STORAGE_JOURNAL && coredump_fd >= 0) {
890 if (coredump_size <= arg_journal_size_max) {
891 size_t sz = 0;
892
893 /* Store the coredump itself in the journal */
894
895 r = allocate_journal_field(coredump_fd, (size_t) coredump_size, &coredump_data, &sz);
896 if (r >= 0) {
897 if (iovw_put(iovw, coredump_data, sz) >= 0)
898 TAKE_PTR(coredump_data);
899 } else
900 log_warning_errno(r, "Failed to attach the core to the journal entry: %m");
901 } else
902 log_info("The core will not be stored: size %"PRIu64" is greater than %"PRIu64" (the configured maximum)",
903 coredump_size, arg_journal_size_max);
904 }
905
906 r = sd_journal_sendv(iovw->iovec, iovw->count);
907 if (r < 0)
908 return log_error_errno(r, "Failed to log coredump: %m");
909
910 return 0;
911 }
912
913 static int save_context(Context *context, const struct iovec_wrapper *iovw) {
914 unsigned count = 0;
915 const char *unit;
916 int r;
917
918 assert(context);
919 assert(iovw);
920 assert(iovw->count >= _META_ARGV_MAX);
921
922 /* The context does not allocate any memory on its own */
923
924 for (size_t n = 0; n < iovw->count; n++) {
925 struct iovec *iovec = iovw->iovec + n;
926
927 for (size_t i = 0; i < ELEMENTSOF(meta_field_names); i++) {
928 char *p;
929
930 /* Note that these strings are NUL terminated, because we made sure that a
931 * trailing NUL byte is in the buffer, though not included in the iov_len
932 * count (see process_socket() and gather_pid_metadata_*()) */
933 assert(((char*) iovec->iov_base)[iovec->iov_len] == 0);
934
935 p = startswith(iovec->iov_base, meta_field_names[i]);
936 if (p) {
937 context->meta[i] = p;
938 count++;
939 break;
940 }
941 }
942 }
943
944 if (!context->meta[META_ARGV_PID])
945 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
946 "Failed to find the PID of crashing process");
947
948 r = parse_pid(context->meta[META_ARGV_PID], &context->pid);
949 if (r < 0)
950 return log_error_errno(r, "Failed to parse PID \"%s\": %m", context->meta[META_ARGV_PID]);
951
952 unit = context->meta[META_UNIT];
953 context->is_pid1 = streq(context->meta[META_ARGV_PID], "1") || streq_ptr(unit, SPECIAL_INIT_SCOPE);
954 context->is_journald = streq_ptr(unit, SPECIAL_JOURNALD_SERVICE);
955
956 return 0;
957 }
958
959 static int process_socket(int fd) {
960 _cleanup_close_ int input_fd = -1;
961 Context context = {};
962 struct iovec_wrapper iovw = {};
963 struct iovec iovec;
964 int r;
965
966 assert(fd >= 0);
967
968 log_setup();
969
970 log_debug("Processing coredump received on stdin...");
971
972 for (;;) {
973 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control;
974 struct msghdr mh = {
975 .msg_control = &control,
976 .msg_controllen = sizeof(control),
977 .msg_iovlen = 1,
978 };
979 ssize_t n;
980 ssize_t l;
981
982 l = next_datagram_size_fd(fd);
983 if (l < 0) {
984 r = log_error_errno(l, "Failed to determine datagram size to read: %m");
985 goto finish;
986 }
987
988 iovec.iov_len = l;
989 iovec.iov_base = malloc(l + 1);
990 if (!iovec.iov_base) {
991 r = log_oom();
992 goto finish;
993 }
994
995 mh.msg_iov = &iovec;
996
997 n = recvmsg_safe(fd, &mh, MSG_CMSG_CLOEXEC);
998 if (n < 0) {
999 free(iovec.iov_base);
1000 r = log_error_errno(n, "Failed to receive datagram: %m");
1001 goto finish;
1002 }
1003
1004 /* The final zero-length datagram carries the file descriptor and tells us
1005 * that we're done. */
1006 if (n == 0) {
1007 struct cmsghdr *found;
1008
1009 free(iovec.iov_base);
1010
1011 found = cmsg_find(&mh, SOL_SOCKET, SCM_RIGHTS, CMSG_LEN(sizeof(int)));
1012 if (!found) {
1013 cmsg_close_all(&mh);
1014 r = log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
1015 "Coredump file descriptor missing.");
1016 goto finish;
1017 }
1018
1019 assert(input_fd < 0);
1020 input_fd = *(int*) CMSG_DATA(found);
1021 break;
1022 } else
1023 cmsg_close_all(&mh);
1024
1025 /* Add trailing NUL byte, in case these are strings */
1026 ((char*) iovec.iov_base)[n] = 0;
1027 iovec.iov_len = (size_t) n;
1028
1029 r = iovw_put(&iovw, iovec.iov_base, iovec.iov_len);
1030 if (r < 0)
1031 goto finish;
1032 }
1033
1034 /* Make sure we got all data we really need */
1035 assert(input_fd >= 0);
1036
1037 r = save_context(&context, &iovw);
1038 if (r < 0)
1039 goto finish;
1040
1041 /* Make sure we received at least all fields we need. */
1042 for (int i = 0; i < _META_MANDATORY_MAX; i++)
1043 if (!context.meta[i]) {
1044 r = log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1045 "A mandatory argument (%i) has not been sent, aborting.",
1046 i);
1047 goto finish;
1048 }
1049
1050 r = submit_coredump(&context, &iovw, input_fd);
1051
1052 finish:
1053 iovw_free_contents(&iovw, true);
1054 return r;
1055 }
1056
1057 static int send_iovec(const struct iovec_wrapper *iovw, int input_fd) {
1058 _cleanup_close_ int fd = -1;
1059 int r;
1060
1061 assert(iovw);
1062 assert(input_fd >= 0);
1063
1064 fd = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
1065 if (fd < 0)
1066 return log_error_errno(errno, "Failed to create coredump socket: %m");
1067
1068 r = connect_unix_path(fd, AT_FDCWD, "/run/systemd/coredump");
1069 if (r < 0)
1070 return log_error_errno(r, "Failed to connect to coredump service: %m");
1071
1072 for (size_t i = 0; i < iovw->count; i++) {
1073 struct msghdr mh = {
1074 .msg_iov = iovw->iovec + i,
1075 .msg_iovlen = 1,
1076 };
1077 struct iovec copy[2];
1078
1079 for (;;) {
1080 if (sendmsg(fd, &mh, MSG_NOSIGNAL) >= 0)
1081 break;
1082
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
1087 * truncated. */
1088
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);
1097
1098 mh.msg_iov = copy;
1099 mh.msg_iovlen = 2;
1100 }
1101
1102 copy[0].iov_len /= 2; /* halve it, and try again */
1103 continue;
1104 }
1105
1106 return log_error_errno(errno, "Failed to send coredump datagram: %m");
1107 }
1108 }
1109
1110 r = send_one_fd(fd, input_fd, 0);
1111 if (r < 0)
1112 return log_error_errno(r, "Failed to send coredump fd: %m");
1113
1114 return 0;
1115 }
1116
1117 static int gather_pid_metadata_from_argv(
1118 struct iovec_wrapper *iovw,
1119 Context *context,
1120 int argc, char **argv) {
1121
1122 _cleanup_free_ char *free_timestamp = NULL;
1123 int r, signo;
1124 char *t;
1125
1126 /* We gather all metadata that were passed via argv[] into an array of iovecs that
1127 * we'll forward to the socket unit */
1128
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);
1133
1134 for (int i = 0; i < _META_ARGV_MAX; i++) {
1135
1136 t = argv[i];
1137
1138 switch (i) {
1139
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
1144 * operates in. */
1145 t = free_timestamp = strjoin(argv[i], "000000");
1146 if (!t)
1147 return log_oom();
1148 break;
1149
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));
1155 break;
1156
1157 default:
1158 break;
1159 }
1160
1161 r = iovw_put_string_field(iovw, meta_field_names[i], t);
1162 if (r < 0)
1163 return r;
1164 }
1165
1166 /* Cache some of the process metadata we collected so far and that we'll need to
1167 * access soon */
1168 return save_context(context, iovw);
1169 }
1170
1171 static int gather_pid_metadata(struct iovec_wrapper *iovw, Context *context) {
1172 uid_t owner_uid;
1173 pid_t pid;
1174 char *t;
1175 const char *p;
1176 int r;
1177
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.) */
1180
1181 pid = context->pid;
1182
1183 /* The following is mandatory */
1184 r = get_process_comm(pid, &t);
1185 if (r < 0)
1186 return log_error_errno(r, "Failed to get COMM: %m");
1187
1188 r = iovw_put_string_field_free(iovw, "COREDUMP_COMM=", t);
1189 if (r < 0)
1190 return r;
1191
1192 /* The following are optional, but we use them if present. */
1193 r = get_process_exe(pid, &t);
1194 if (r >= 0)
1195 r = iovw_put_string_field_free(iovw, "COREDUMP_EXE=", t);
1196 if (r < 0)
1197 log_warning_errno(r, "Failed to get EXE, ignoring: %m");
1198
1199 if (cg_pid_get_unit(pid, &t) >= 0)
1200 (void) iovw_put_string_field_free(iovw, "COREDUMP_UNIT=", t);
1201
1202 if (cg_pid_get_user_unit(pid, &t) >= 0)
1203 (void) iovw_put_string_field_free(iovw, "COREDUMP_USER_UNIT=", t);
1204
1205 if (sd_pid_get_session(pid, &t) >= 0)
1206 (void) iovw_put_string_field_free(iovw, "COREDUMP_SESSION=", t);
1207
1208 if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) {
1209 r = asprintf(&t, UID_FMT, owner_uid);
1210 if (r > 0)
1211 (void) iovw_put_string_field_free(iovw, "COREDUMP_OWNER_UID=", t);
1212 }
1213
1214 if (sd_pid_get_slice(pid, &t) >= 0)
1215 (void) iovw_put_string_field_free(iovw, "COREDUMP_SLICE=", t);
1216
1217 if (get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, &t) >= 0)
1218 (void) iovw_put_string_field_free(iovw, "COREDUMP_CMDLINE=", t);
1219
1220 if (cg_pid_get_path_shifted(pid, NULL, &t) >= 0)
1221 (void) iovw_put_string_field_free(iovw, "COREDUMP_CGROUP=", t);
1222
1223 if (compose_open_fds(pid, &t) >= 0)
1224 (void) iovw_put_string_field_free(iovw, "COREDUMP_OPEN_FDS=", t);
1225
1226 p = procfs_file_alloca(pid, "status");
1227 if (read_full_virtual_file(p, &t, NULL) >= 0)
1228 (void) iovw_put_string_field_free(iovw, "COREDUMP_PROC_STATUS=", t);
1229
1230 p = procfs_file_alloca(pid, "maps");
1231 if (read_full_virtual_file(p, &t, NULL) >= 0)
1232 (void) iovw_put_string_field_free(iovw, "COREDUMP_PROC_MAPS=", t);
1233
1234 p = procfs_file_alloca(pid, "limits");
1235 if (read_full_virtual_file(p, &t, NULL) >= 0)
1236 (void) iovw_put_string_field_free(iovw, "COREDUMP_PROC_LIMITS=", t);
1237
1238 p = procfs_file_alloca(pid, "cgroup");
1239 if (read_full_virtual_file(p, &t, NULL) >=0)
1240 (void) iovw_put_string_field_free(iovw, "COREDUMP_PROC_CGROUP=", t);
1241
1242 p = procfs_file_alloca(pid, "mountinfo");
1243 if (read_full_virtual_file(p, &t, NULL) >=0)
1244 (void) iovw_put_string_field_free(iovw, "COREDUMP_PROC_MOUNTINFO=", t);
1245
1246 if (get_process_cwd(pid, &t) >= 0)
1247 (void) iovw_put_string_field_free(iovw, "COREDUMP_CWD=", t);
1248
1249 if (get_process_root(pid, &t) >= 0) {
1250 bool proc_self_root_is_slash;
1251
1252 proc_self_root_is_slash = strcmp(t, "/") == 0;
1253
1254 (void) iovw_put_string_field_free(iovw, "COREDUMP_ROOT=", t);
1255
1256 /* If the process' root is "/", then there is a chance it has
1257 * mounted own root and hence being containerized. */
1258 if (proc_self_root_is_slash && get_process_container_parent_cmdline(pid, &t) > 0)
1259 (void) iovw_put_string_field_free(iovw, "COREDUMP_CONTAINER_CMDLINE=", t);
1260 }
1261
1262 if (get_process_environ(pid, &t) >= 0)
1263 (void) iovw_put_string_field_free(iovw, "COREDUMP_ENVIRON=", t);
1264
1265 /* we successfully acquired all metadata */
1266 return save_context(context, iovw);
1267 }
1268
1269 static int process_kernel(int argc, char* argv[]) {
1270 Context context = {};
1271 struct iovec_wrapper *iovw;
1272 int r;
1273
1274 log_debug("Processing coredump received from the kernel...");
1275
1276 iovw = iovw_new();
1277 if (!iovw)
1278 return log_oom();
1279
1280 (void) iovw_put_string_field(iovw, "MESSAGE_ID=", SD_MESSAGE_COREDUMP_STR);
1281 (void) iovw_put_string_field(iovw, "PRIORITY=", STRINGIFY(LOG_CRIT));
1282
1283 /* Collect all process metadata passed by the kernel through argv[] */
1284 r = gather_pid_metadata_from_argv(iovw, &context, argc - 1, argv + 1);
1285 if (r < 0)
1286 goto finish;
1287
1288 /* Collect the rest of the process metadata retrieved from the runtime */
1289 r = gather_pid_metadata(iovw, &context);
1290 if (r < 0)
1291 goto finish;
1292
1293 if (!context.is_journald) {
1294 /* OK, now we know it's not the journal, hence we can make use of it now. */
1295 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
1296 log_open();
1297 }
1298
1299 /* If this is PID 1 disable coredump collection, we'll unlikely be able to process
1300 * it later on.
1301 *
1302 * FIXME: maybe we should disable coredumps generation from the beginning and
1303 * re-enable it only when we know it's either safe (ie we're not running OOM) or
1304 * it's not pid1 ? */
1305 if (context.is_pid1) {
1306 log_notice("Due to PID 1 having crashed coredump collection will now be turned off.");
1307 disable_coredumps();
1308 }
1309
1310 if (context.is_journald || context.is_pid1)
1311 r = submit_coredump(&context, iovw, STDIN_FILENO);
1312 else
1313 r = send_iovec(iovw, STDIN_FILENO);
1314
1315 finish:
1316 iovw = iovw_free_free(iovw);
1317 return r;
1318 }
1319
1320 static int process_backtrace(int argc, char *argv[]) {
1321 Context context = {};
1322 struct iovec_wrapper *iovw;
1323 char *message;
1324 int r;
1325 _cleanup_(journal_importer_cleanup) JournalImporter importer = JOURNAL_IMPORTER_INIT(STDIN_FILENO);
1326
1327 log_debug("Processing backtrace on stdin...");
1328
1329 iovw = iovw_new();
1330 if (!iovw)
1331 return log_oom();
1332
1333 (void) iovw_put_string_field(iovw, "MESSAGE_ID=", SD_MESSAGE_BACKTRACE_STR);
1334 (void) iovw_put_string_field(iovw, "PRIORITY=", STRINGIFY(LOG_CRIT));
1335
1336 /* Collect all process metadata from argv[] by making sure to skip the
1337 * '--backtrace' option */
1338 r = gather_pid_metadata_from_argv(iovw, &context, argc - 2, argv + 2);
1339 if (r < 0)
1340 goto finish;
1341
1342 /* Collect the rest of the process metadata retrieved from the runtime */
1343 r = gather_pid_metadata(iovw, &context);
1344 if (r < 0)
1345 goto finish;
1346
1347 for (;;) {
1348 r = journal_importer_process_data(&importer);
1349 if (r < 0) {
1350 log_error_errno(r, "Failed to parse journal entry on stdin: %m");
1351 goto finish;
1352 }
1353 if (r == 1 || /* complete entry */
1354 journal_importer_eof(&importer)) /* end of data */
1355 break;
1356 }
1357
1358 if (journal_importer_eof(&importer)) {
1359 log_warning("Did not receive a full journal entry on stdin, ignoring message sent by reporter");
1360
1361 message = strjoina("Process ", context.meta[META_ARGV_PID],
1362 " (", context.meta[META_COMM], ")"
1363 " of user ", context.meta[META_ARGV_UID],
1364 " failed with ", context.meta[META_ARGV_SIGNAL]);
1365
1366 r = iovw_put_string_field(iovw, "MESSAGE=", message);
1367 if (r < 0)
1368 return r;
1369 } else {
1370 /* The imported iovecs are not supposed to be freed by us so let's store
1371 * them at the end of the array so we can skip them while freeing the
1372 * rest. */
1373 for (size_t i = 0; i < importer.iovw.count; i++) {
1374 struct iovec *iovec = importer.iovw.iovec + i;
1375
1376 iovw_put(iovw, iovec->iov_base, iovec->iov_len);
1377 }
1378 }
1379
1380 r = sd_journal_sendv(iovw->iovec, iovw->count);
1381 if (r < 0)
1382 log_error_errno(r, "Failed to log backtrace: %m");
1383
1384 finish:
1385 iovw->count -= importer.iovw.count;
1386 iovw = iovw_free_free(iovw);
1387 return r;
1388 }
1389
1390 static int run(int argc, char *argv[]) {
1391 int r;
1392
1393 /* First, log to a safe place, since we don't know what crashed and it might
1394 * be journald which we'd rather not log to then. */
1395
1396 log_set_target(LOG_TARGET_KMSG);
1397 log_open();
1398
1399 /* Make sure we never enter a loop */
1400 (void) prctl(PR_SET_DUMPABLE, 0);
1401
1402 /* Ignore all parse errors */
1403 (void) parse_config();
1404
1405 log_debug("Selected storage '%s'.", coredump_storage_to_string(arg_storage));
1406 log_debug("Selected compression %s.", yes_no(arg_compress));
1407
1408 r = sd_listen_fds(false);
1409 if (r < 0)
1410 return log_error_errno(r, "Failed to determine the number of file descriptors: %m");
1411
1412 /* If we got an fd passed, we are running in coredumpd mode. Otherwise we
1413 * are invoked from the kernel as coredump handler. */
1414 if (r == 0) {
1415 if (streq_ptr(argv[1], "--backtrace"))
1416 return process_backtrace(argc, argv);
1417 else
1418 return process_kernel(argc, argv);
1419 } else if (r == 1)
1420 return process_socket(SD_LISTEN_FDS_START);
1421
1422 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1423 "Received unexpected number of file descriptors.");
1424 }
1425
1426 DEFINE_MAIN_FUNCTION(run);