]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | /*** | |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2012 Lennart Poettering | |
6 | ***/ | |
7 | ||
8 | #include <errno.h> | |
9 | #include <stdio.h> | |
10 | #include <stdio_ext.h> | |
11 | #include <sys/prctl.h> | |
12 | #include <sys/xattr.h> | |
13 | #include <unistd.h> | |
14 | ||
15 | #if HAVE_ELFUTILS | |
16 | #include <dwarf.h> | |
17 | #include <elfutils/libdwfl.h> | |
18 | #endif | |
19 | ||
20 | #include "sd-daemon.h" | |
21 | #include "sd-journal.h" | |
22 | #include "sd-login.h" | |
23 | #include "sd-messages.h" | |
24 | ||
25 | #include "acl-util.h" | |
26 | #include "alloc-util.h" | |
27 | #include "capability-util.h" | |
28 | #include "cgroup-util.h" | |
29 | #include "compress.h" | |
30 | #include "conf-parser.h" | |
31 | #include "copy.h" | |
32 | #include "coredump-vacuum.h" | |
33 | #include "dirent-util.h" | |
34 | #include "escape.h" | |
35 | #include "fd-util.h" | |
36 | #include "fileio.h" | |
37 | #include "fs-util.h" | |
38 | #include "io-util.h" | |
39 | #include "journal-importer.h" | |
40 | #include "log.h" | |
41 | #include "macro.h" | |
42 | #include "missing.h" | |
43 | #include "mkdir.h" | |
44 | #include "parse-util.h" | |
45 | #include "process-util.h" | |
46 | #include "signal-util.h" | |
47 | #include "socket-util.h" | |
48 | #include "special.h" | |
49 | #include "stacktrace.h" | |
50 | #include "string-table.h" | |
51 | #include "string-util.h" | |
52 | #include "strv.h" | |
53 | #include "user-util.h" | |
54 | #include "util.h" | |
55 | ||
56 | /* The maximum size up to which we process coredumps */ | |
57 | #define PROCESS_SIZE_MAX ((uint64_t) (2LLU*1024LLU*1024LLU*1024LLU)) | |
58 | ||
59 | /* The maximum size up to which we leave the coredump around on disk */ | |
60 | #define EXTERNAL_SIZE_MAX PROCESS_SIZE_MAX | |
61 | ||
62 | /* The maximum size up to which we store the coredump in the journal */ | |
63 | #define JOURNAL_SIZE_MAX ((size_t) (767LU*1024LU*1024LU)) | |
64 | ||
65 | /* Make sure to not make this larger than the maximum journal entry | |
66 | * size. See DATA_SIZE_MAX in journald-native.c. */ | |
67 | assert_cc(JOURNAL_SIZE_MAX <= DATA_SIZE_MAX); | |
68 | ||
69 | enum { | |
70 | /* We use this as array indexes for a couple of special fields we use for | |
71 | * naming coredump files, and attaching xattrs, and for indexing argv[]. | |
72 | ||
73 | * Our pattern for man:systectl(1) kernel.core_pattern is such that the | |
74 | * kernel passes fields until CONTEXT_RLIMIT as arguments in argv[]. After | |
75 | * that it gets complicated: the kernel passes "comm" as one or more fields | |
76 | * starting at index CONTEXT_COMM (in other words, full "comm" is under index | |
77 | * CONTEXT_COMM when it does not contain spaces, which is the common | |
78 | * case). This mapping is not reversible, so we prefer to retrieve "comm" | |
79 | * from /proc. We only fall back to argv[CONTEXT_COMM...] when that fails. | |
80 | * | |
81 | * In the internal context[] array, fields before CONTEXT_COMM are the | |
82 | * strings from argv[], so they should not be freed. The strings at indices | |
83 | * CONTEXT_COMM and higher are allocated by us and should be freed at the | |
84 | * end. | |
85 | */ | |
86 | CONTEXT_PID, | |
87 | CONTEXT_UID, | |
88 | CONTEXT_GID, | |
89 | CONTEXT_SIGNAL, | |
90 | CONTEXT_TIMESTAMP, | |
91 | CONTEXT_RLIMIT, | |
92 | CONTEXT_HOSTNAME, | |
93 | CONTEXT_COMM, | |
94 | CONTEXT_EXE, | |
95 | CONTEXT_UNIT, | |
96 | _CONTEXT_MAX | |
97 | }; | |
98 | ||
99 | typedef enum CoredumpStorage { | |
100 | COREDUMP_STORAGE_NONE, | |
101 | COREDUMP_STORAGE_EXTERNAL, | |
102 | COREDUMP_STORAGE_JOURNAL, | |
103 | _COREDUMP_STORAGE_MAX, | |
104 | _COREDUMP_STORAGE_INVALID = -1 | |
105 | } CoredumpStorage; | |
106 | ||
107 | static const char* const coredump_storage_table[_COREDUMP_STORAGE_MAX] = { | |
108 | [COREDUMP_STORAGE_NONE] = "none", | |
109 | [COREDUMP_STORAGE_EXTERNAL] = "external", | |
110 | [COREDUMP_STORAGE_JOURNAL] = "journal", | |
111 | }; | |
112 | ||
113 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP(coredump_storage, CoredumpStorage); | |
114 | static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting"); | |
115 | ||
116 | static CoredumpStorage arg_storage = COREDUMP_STORAGE_EXTERNAL; | |
117 | static bool arg_compress = true; | |
118 | static uint64_t arg_process_size_max = PROCESS_SIZE_MAX; | |
119 | static uint64_t arg_external_size_max = EXTERNAL_SIZE_MAX; | |
120 | static uint64_t arg_journal_size_max = JOURNAL_SIZE_MAX; | |
121 | static uint64_t arg_keep_free = (uint64_t) -1; | |
122 | static uint64_t arg_max_use = (uint64_t) -1; | |
123 | ||
124 | static int parse_config(void) { | |
125 | static const ConfigTableItem items[] = { | |
126 | { "Coredump", "Storage", config_parse_coredump_storage, 0, &arg_storage }, | |
127 | { "Coredump", "Compress", config_parse_bool, 0, &arg_compress }, | |
128 | { "Coredump", "ProcessSizeMax", config_parse_iec_uint64, 0, &arg_process_size_max }, | |
129 | { "Coredump", "ExternalSizeMax", config_parse_iec_uint64, 0, &arg_external_size_max }, | |
130 | { "Coredump", "JournalSizeMax", config_parse_iec_size, 0, &arg_journal_size_max }, | |
131 | { "Coredump", "KeepFree", config_parse_iec_uint64, 0, &arg_keep_free }, | |
132 | { "Coredump", "MaxUse", config_parse_iec_uint64, 0, &arg_max_use }, | |
133 | {} | |
134 | }; | |
135 | ||
136 | return config_parse_many_nulstr(PKGSYSCONFDIR "/coredump.conf", | |
137 | CONF_PATHS_NULSTR("systemd/coredump.conf.d"), | |
138 | "Coredump\0", | |
139 | config_item_table_lookup, items, | |
140 | CONFIG_PARSE_WARN, NULL); | |
141 | } | |
142 | ||
143 | static inline uint64_t storage_size_max(void) { | |
144 | return arg_storage == COREDUMP_STORAGE_EXTERNAL ? arg_external_size_max : arg_journal_size_max; | |
145 | } | |
146 | ||
147 | static int fix_acl(int fd, uid_t uid) { | |
148 | ||
149 | #if HAVE_ACL | |
150 | _cleanup_(acl_freep) acl_t acl = NULL; | |
151 | acl_entry_t entry; | |
152 | acl_permset_t permset; | |
153 | int r; | |
154 | ||
155 | assert(fd >= 0); | |
156 | ||
157 | if (uid_is_system(uid) || uid_is_dynamic(uid) || uid == UID_NOBODY) | |
158 | return 0; | |
159 | ||
160 | /* Make sure normal users can read (but not write or delete) | |
161 | * their own coredumps */ | |
162 | ||
163 | acl = acl_get_fd(fd); | |
164 | if (!acl) | |
165 | return log_error_errno(errno, "Failed to get ACL: %m"); | |
166 | ||
167 | if (acl_create_entry(&acl, &entry) < 0 || | |
168 | acl_set_tag_type(entry, ACL_USER) < 0 || | |
169 | acl_set_qualifier(entry, &uid) < 0) | |
170 | return log_error_errno(errno, "Failed to patch ACL: %m"); | |
171 | ||
172 | if (acl_get_permset(entry, &permset) < 0 || | |
173 | acl_add_perm(permset, ACL_READ) < 0) | |
174 | return log_warning_errno(errno, "Failed to patch ACL: %m"); | |
175 | ||
176 | r = calc_acl_mask_if_needed(&acl); | |
177 | if (r < 0) | |
178 | return log_warning_errno(r, "Failed to patch ACL: %m"); | |
179 | ||
180 | if (acl_set_fd(fd, acl) < 0) | |
181 | return log_error_errno(errno, "Failed to apply ACL: %m"); | |
182 | #endif | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
187 | static int fix_xattr(int fd, const char *context[_CONTEXT_MAX]) { | |
188 | ||
189 | static const char * const xattrs[_CONTEXT_MAX] = { | |
190 | [CONTEXT_PID] = "user.coredump.pid", | |
191 | [CONTEXT_UID] = "user.coredump.uid", | |
192 | [CONTEXT_GID] = "user.coredump.gid", | |
193 | [CONTEXT_SIGNAL] = "user.coredump.signal", | |
194 | [CONTEXT_TIMESTAMP] = "user.coredump.timestamp", | |
195 | [CONTEXT_RLIMIT] = "user.coredump.rlimit", | |
196 | [CONTEXT_HOSTNAME] = "user.coredump.hostname", | |
197 | [CONTEXT_COMM] = "user.coredump.comm", | |
198 | [CONTEXT_EXE] = "user.coredump.exe", | |
199 | }; | |
200 | ||
201 | int r = 0; | |
202 | unsigned i; | |
203 | ||
204 | assert(fd >= 0); | |
205 | ||
206 | /* Attach some metadata to coredumps via extended | |
207 | * attributes. Just because we can. */ | |
208 | ||
209 | for (i = 0; i < _CONTEXT_MAX; i++) { | |
210 | int k; | |
211 | ||
212 | if (isempty(context[i]) || !xattrs[i]) | |
213 | continue; | |
214 | ||
215 | k = fsetxattr(fd, xattrs[i], context[i], strlen(context[i]), XATTR_CREATE); | |
216 | if (k < 0 && r == 0) | |
217 | r = -errno; | |
218 | } | |
219 | ||
220 | return r; | |
221 | } | |
222 | ||
223 | #define filename_escape(s) xescape((s), "./ ") | |
224 | ||
225 | static inline const char *coredump_tmpfile_name(const char *s) { | |
226 | return s ? s : "(unnamed temporary file)"; | |
227 | } | |
228 | ||
229 | static int fix_permissions( | |
230 | int fd, | |
231 | const char *filename, | |
232 | const char *target, | |
233 | const char *context[_CONTEXT_MAX], | |
234 | uid_t uid) { | |
235 | ||
236 | int r; | |
237 | ||
238 | assert(fd >= 0); | |
239 | assert(target); | |
240 | assert(context); | |
241 | ||
242 | /* Ignore errors on these */ | |
243 | (void) fchmod(fd, 0640); | |
244 | (void) fix_acl(fd, uid); | |
245 | (void) fix_xattr(fd, context); | |
246 | ||
247 | if (fsync(fd) < 0) | |
248 | return log_error_errno(errno, "Failed to sync coredump %s: %m", coredump_tmpfile_name(filename)); | |
249 | ||
250 | (void) fsync_directory_of_file(fd); | |
251 | ||
252 | r = link_tmpfile(fd, filename, target); | |
253 | if (r < 0) | |
254 | return log_error_errno(r, "Failed to move coredump %s into place: %m", target); | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
259 | static int maybe_remove_external_coredump(const char *filename, uint64_t size) { | |
260 | ||
261 | /* Returns 1 if might remove, 0 if will not remove, < 0 on error. */ | |
262 | ||
263 | if (arg_storage == COREDUMP_STORAGE_EXTERNAL && | |
264 | size <= arg_external_size_max) | |
265 | return 0; | |
266 | ||
267 | if (!filename) | |
268 | return 1; | |
269 | ||
270 | if (unlink(filename) < 0 && errno != ENOENT) | |
271 | return log_error_errno(errno, "Failed to unlink %s: %m", filename); | |
272 | ||
273 | return 1; | |
274 | } | |
275 | ||
276 | static int make_filename(const char *context[_CONTEXT_MAX], char **ret) { | |
277 | _cleanup_free_ char *c = NULL, *u = NULL, *p = NULL, *t = NULL; | |
278 | sd_id128_t boot = {}; | |
279 | int r; | |
280 | ||
281 | assert(context); | |
282 | ||
283 | c = filename_escape(context[CONTEXT_COMM]); | |
284 | if (!c) | |
285 | return -ENOMEM; | |
286 | ||
287 | u = filename_escape(context[CONTEXT_UID]); | |
288 | if (!u) | |
289 | return -ENOMEM; | |
290 | ||
291 | r = sd_id128_get_boot(&boot); | |
292 | if (r < 0) | |
293 | return r; | |
294 | ||
295 | p = filename_escape(context[CONTEXT_PID]); | |
296 | if (!p) | |
297 | return -ENOMEM; | |
298 | ||
299 | t = filename_escape(context[CONTEXT_TIMESTAMP]); | |
300 | if (!t) | |
301 | return -ENOMEM; | |
302 | ||
303 | if (asprintf(ret, | |
304 | "/var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR ".%s.%s000000", | |
305 | c, | |
306 | u, | |
307 | SD_ID128_FORMAT_VAL(boot), | |
308 | p, | |
309 | t) < 0) | |
310 | return -ENOMEM; | |
311 | ||
312 | return 0; | |
313 | } | |
314 | ||
315 | static int save_external_coredump( | |
316 | const char *context[_CONTEXT_MAX], | |
317 | int input_fd, | |
318 | char **ret_filename, | |
319 | int *ret_node_fd, | |
320 | int *ret_data_fd, | |
321 | uint64_t *ret_size, | |
322 | bool *ret_truncated) { | |
323 | ||
324 | _cleanup_free_ char *fn = NULL, *tmp = NULL; | |
325 | _cleanup_close_ int fd = -1; | |
326 | uint64_t rlimit, max_size; | |
327 | struct stat st; | |
328 | uid_t uid; | |
329 | int r; | |
330 | ||
331 | assert(context); | |
332 | assert(ret_filename); | |
333 | assert(ret_node_fd); | |
334 | assert(ret_data_fd); | |
335 | assert(ret_size); | |
336 | ||
337 | r = parse_uid(context[CONTEXT_UID], &uid); | |
338 | if (r < 0) | |
339 | return log_error_errno(r, "Failed to parse UID: %m"); | |
340 | ||
341 | r = safe_atou64(context[CONTEXT_RLIMIT], &rlimit); | |
342 | if (r < 0) | |
343 | return log_error_errno(r, "Failed to parse resource limit: %s", context[CONTEXT_RLIMIT]); | |
344 | if (rlimit < page_size()) { | |
345 | /* Is coredumping disabled? Then don't bother saving/processing the coredump. | |
346 | * Anything below PAGE_SIZE cannot give a readable coredump (the kernel uses | |
347 | * ELF_EXEC_PAGESIZE which is not easily accessible, but is usually the same as PAGE_SIZE. */ | |
348 | log_info("Resource limits disable core dumping for process %s (%s).", | |
349 | context[CONTEXT_PID], context[CONTEXT_COMM]); | |
350 | return -EBADSLT; | |
351 | } | |
352 | ||
353 | /* Never store more than the process configured, or than we actually shall keep or process */ | |
354 | max_size = MIN(rlimit, MAX(arg_process_size_max, storage_size_max())); | |
355 | ||
356 | r = make_filename(context, &fn); | |
357 | if (r < 0) | |
358 | return log_error_errno(r, "Failed to determine coredump file name: %m"); | |
359 | ||
360 | mkdir_p_label("/var/lib/systemd/coredump", 0755); | |
361 | ||
362 | fd = open_tmpfile_linkable(fn, O_RDWR|O_CLOEXEC, &tmp); | |
363 | if (fd < 0) | |
364 | return log_error_errno(fd, "Failed to create temporary file for coredump %s: %m", fn); | |
365 | ||
366 | r = copy_bytes(input_fd, fd, max_size, 0); | |
367 | if (r < 0) { | |
368 | log_error_errno(r, "Cannot store coredump of %s (%s): %m", context[CONTEXT_PID], context[CONTEXT_COMM]); | |
369 | goto fail; | |
370 | } | |
371 | *ret_truncated = r == 1; | |
372 | if (*ret_truncated) | |
373 | log_struct(LOG_INFO, | |
374 | LOG_MESSAGE("Core file was truncated to %zu bytes.", max_size), | |
375 | "SIZE_LIMIT=%zu", max_size, | |
376 | "MESSAGE_ID=" SD_MESSAGE_TRUNCATED_CORE_STR, | |
377 | NULL); | |
378 | ||
379 | if (fstat(fd, &st) < 0) { | |
380 | log_error_errno(errno, "Failed to fstat core file %s: %m", coredump_tmpfile_name(tmp)); | |
381 | goto fail; | |
382 | } | |
383 | ||
384 | if (lseek(fd, 0, SEEK_SET) == (off_t) -1) { | |
385 | log_error_errno(errno, "Failed to seek on %s: %m", coredump_tmpfile_name(tmp)); | |
386 | goto fail; | |
387 | } | |
388 | ||
389 | #if HAVE_XZ || HAVE_LZ4 | |
390 | /* If we will remove the coredump anyway, do not compress. */ | |
391 | if (arg_compress && !maybe_remove_external_coredump(NULL, st.st_size)) { | |
392 | ||
393 | _cleanup_free_ char *fn_compressed = NULL, *tmp_compressed = NULL; | |
394 | _cleanup_close_ int fd_compressed = -1; | |
395 | ||
396 | fn_compressed = strappend(fn, COMPRESSED_EXT); | |
397 | if (!fn_compressed) { | |
398 | log_oom(); | |
399 | goto uncompressed; | |
400 | } | |
401 | ||
402 | fd_compressed = open_tmpfile_linkable(fn_compressed, O_RDWR|O_CLOEXEC, &tmp_compressed); | |
403 | if (fd_compressed < 0) { | |
404 | log_error_errno(fd_compressed, "Failed to create temporary file for coredump %s: %m", fn_compressed); | |
405 | goto uncompressed; | |
406 | } | |
407 | ||
408 | r = compress_stream(fd, fd_compressed, -1); | |
409 | if (r < 0) { | |
410 | log_error_errno(r, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed)); | |
411 | goto fail_compressed; | |
412 | } | |
413 | ||
414 | r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, context, uid); | |
415 | if (r < 0) | |
416 | goto fail_compressed; | |
417 | ||
418 | /* OK, this worked, we can get rid of the uncompressed version now */ | |
419 | if (tmp) | |
420 | unlink_noerrno(tmp); | |
421 | ||
422 | *ret_filename = TAKE_PTR(fn_compressed); /* compressed */ | |
423 | *ret_node_fd = TAKE_FD(fd_compressed); /* compressed */ | |
424 | *ret_data_fd = TAKE_FD(fd); /* uncompressed */ | |
425 | *ret_size = (uint64_t) st.st_size; /* uncompressed */ | |
426 | ||
427 | return 0; | |
428 | ||
429 | fail_compressed: | |
430 | if (tmp_compressed) | |
431 | (void) unlink(tmp_compressed); | |
432 | } | |
433 | ||
434 | uncompressed: | |
435 | #endif | |
436 | ||
437 | r = fix_permissions(fd, tmp, fn, context, uid); | |
438 | if (r < 0) | |
439 | goto fail; | |
440 | ||
441 | *ret_filename = TAKE_PTR(fn); | |
442 | *ret_data_fd = TAKE_FD(fd); | |
443 | *ret_node_fd = -1; | |
444 | *ret_size = (uint64_t) st.st_size; | |
445 | ||
446 | return 0; | |
447 | ||
448 | fail: | |
449 | if (tmp) | |
450 | (void) unlink(tmp); | |
451 | return r; | |
452 | } | |
453 | ||
454 | static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_size) { | |
455 | _cleanup_free_ char *field = NULL; | |
456 | ssize_t n; | |
457 | ||
458 | assert(fd >= 0); | |
459 | assert(ret); | |
460 | assert(ret_size); | |
461 | ||
462 | if (lseek(fd, 0, SEEK_SET) == (off_t) -1) | |
463 | return log_warning_errno(errno, "Failed to seek: %m"); | |
464 | ||
465 | field = malloc(9 + size); | |
466 | if (!field) { | |
467 | log_warning("Failed to allocate memory for coredump, coredump will not be stored."); | |
468 | return -ENOMEM; | |
469 | } | |
470 | ||
471 | memcpy(field, "COREDUMP=", 9); | |
472 | ||
473 | n = read(fd, field + 9, size); | |
474 | if (n < 0) | |
475 | return log_error_errno((int) n, "Failed to read core data: %m"); | |
476 | if ((size_t) n < size) { | |
477 | log_error("Core data too short."); | |
478 | return -EIO; | |
479 | } | |
480 | ||
481 | *ret = TAKE_PTR(field); | |
482 | *ret_size = size + 9; | |
483 | ||
484 | return 0; | |
485 | } | |
486 | ||
487 | /* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines: | |
488 | * 0:/dev/pts/23 | |
489 | * pos: 0 | |
490 | * flags: 0100002 | |
491 | * | |
492 | * 1:/dev/pts/23 | |
493 | * pos: 0 | |
494 | * flags: 0100002 | |
495 | * | |
496 | * 2:/dev/pts/23 | |
497 | * pos: 0 | |
498 | * flags: 0100002 | |
499 | * EOF | |
500 | */ | |
501 | static int compose_open_fds(pid_t pid, char **open_fds) { | |
502 | _cleanup_closedir_ DIR *proc_fd_dir = NULL; | |
503 | _cleanup_close_ int proc_fdinfo_fd = -1; | |
504 | _cleanup_free_ char *buffer = NULL; | |
505 | _cleanup_fclose_ FILE *stream = NULL; | |
506 | const char *fddelim = "", *path; | |
507 | struct dirent *dent = NULL; | |
508 | size_t size = 0; | |
509 | int r = 0; | |
510 | ||
511 | assert(pid >= 0); | |
512 | assert(open_fds != NULL); | |
513 | ||
514 | path = procfs_file_alloca(pid, "fd"); | |
515 | proc_fd_dir = opendir(path); | |
516 | if (!proc_fd_dir) | |
517 | return -errno; | |
518 | ||
519 | proc_fdinfo_fd = openat(dirfd(proc_fd_dir), "../fdinfo", O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
520 | if (proc_fdinfo_fd < 0) | |
521 | return -errno; | |
522 | ||
523 | stream = open_memstream(&buffer, &size); | |
524 | if (!stream) | |
525 | return -ENOMEM; | |
526 | ||
527 | (void) __fsetlocking(stream, FSETLOCKING_BYCALLER); | |
528 | ||
529 | FOREACH_DIRENT(dent, proc_fd_dir, return -errno) { | |
530 | _cleanup_fclose_ FILE *fdinfo = NULL; | |
531 | _cleanup_free_ char *fdname = NULL; | |
532 | char line[LINE_MAX]; | |
533 | int fd; | |
534 | ||
535 | r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname); | |
536 | if (r < 0) | |
537 | return r; | |
538 | ||
539 | fprintf(stream, "%s%s:%s\n", fddelim, dent->d_name, fdname); | |
540 | fddelim = "\n"; | |
541 | ||
542 | /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */ | |
543 | fd = openat(proc_fdinfo_fd, dent->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY); | |
544 | if (fd < 0) | |
545 | continue; | |
546 | ||
547 | fdinfo = fdopen(fd, "re"); | |
548 | if (!fdinfo) { | |
549 | safe_close(fd); | |
550 | continue; | |
551 | } | |
552 | ||
553 | FOREACH_LINE(line, fdinfo, break) { | |
554 | fputs(line, stream); | |
555 | if (!endswith(line, "\n")) | |
556 | fputc('\n', stream); | |
557 | } | |
558 | } | |
559 | ||
560 | errno = 0; | |
561 | stream = safe_fclose(stream); | |
562 | ||
563 | if (errno > 0) | |
564 | return -errno; | |
565 | ||
566 | *open_fds = TAKE_PTR(buffer); | |
567 | ||
568 | return 0; | |
569 | } | |
570 | ||
571 | static int get_process_ns(pid_t pid, const char *namespace, ino_t *ns) { | |
572 | const char *p; | |
573 | struct stat stbuf; | |
574 | _cleanup_close_ int proc_ns_dir_fd; | |
575 | ||
576 | p = procfs_file_alloca(pid, "ns"); | |
577 | ||
578 | proc_ns_dir_fd = open(p, O_DIRECTORY | O_CLOEXEC | O_RDONLY); | |
579 | if (proc_ns_dir_fd < 0) | |
580 | return -errno; | |
581 | ||
582 | if (fstatat(proc_ns_dir_fd, namespace, &stbuf, /* flags */0) < 0) | |
583 | return -errno; | |
584 | ||
585 | *ns = stbuf.st_ino; | |
586 | return 0; | |
587 | } | |
588 | ||
589 | static int get_mount_namespace_leader(pid_t pid, pid_t *container_pid) { | |
590 | pid_t cpid = pid, ppid = 0; | |
591 | ino_t proc_mntns; | |
592 | int r = 0; | |
593 | ||
594 | r = get_process_ns(pid, "mnt", &proc_mntns); | |
595 | if (r < 0) | |
596 | return r; | |
597 | ||
598 | for (;;) { | |
599 | ino_t parent_mntns; | |
600 | ||
601 | r = get_process_ppid(cpid, &ppid); | |
602 | if (r < 0) | |
603 | return r; | |
604 | ||
605 | r = get_process_ns(ppid, "mnt", &parent_mntns); | |
606 | if (r < 0) | |
607 | return r; | |
608 | ||
609 | if (proc_mntns != parent_mntns) | |
610 | break; | |
611 | ||
612 | if (ppid == 1) | |
613 | return -ENOENT; | |
614 | ||
615 | cpid = ppid; | |
616 | } | |
617 | ||
618 | *container_pid = ppid; | |
619 | return 0; | |
620 | } | |
621 | ||
622 | /* Returns 1 if the parent was found. | |
623 | * Returns 0 if there is not a process we can call the pid's | |
624 | * container parent (the pid's process isn't 'containerized'). | |
625 | * Returns a negative number on errors. | |
626 | */ | |
627 | static int get_process_container_parent_cmdline(pid_t pid, char** cmdline) { | |
628 | int r = 0; | |
629 | pid_t container_pid; | |
630 | const char *proc_root_path; | |
631 | struct stat root_stat, proc_root_stat; | |
632 | ||
633 | /* To compare inodes of / and /proc/[pid]/root */ | |
634 | if (stat("/", &root_stat) < 0) | |
635 | return -errno; | |
636 | ||
637 | proc_root_path = procfs_file_alloca(pid, "root"); | |
638 | if (stat(proc_root_path, &proc_root_stat) < 0) | |
639 | return -errno; | |
640 | ||
641 | /* The process uses system root. */ | |
642 | if (proc_root_stat.st_ino == root_stat.st_ino) { | |
643 | *cmdline = NULL; | |
644 | return 0; | |
645 | } | |
646 | ||
647 | r = get_mount_namespace_leader(pid, &container_pid); | |
648 | if (r < 0) | |
649 | return r; | |
650 | ||
651 | r = get_process_cmdline(container_pid, 0, false, cmdline); | |
652 | if (r < 0) | |
653 | return r; | |
654 | ||
655 | return 1; | |
656 | } | |
657 | ||
658 | static int change_uid_gid(const char *context[]) { | |
659 | uid_t uid; | |
660 | gid_t gid; | |
661 | int r; | |
662 | ||
663 | r = parse_uid(context[CONTEXT_UID], &uid); | |
664 | if (r < 0) | |
665 | return r; | |
666 | ||
667 | if (uid <= SYSTEM_UID_MAX) { | |
668 | const char *user = "systemd-coredump"; | |
669 | ||
670 | r = get_user_creds(&user, &uid, &gid, NULL, NULL); | |
671 | if (r < 0) { | |
672 | log_warning_errno(r, "Cannot resolve %s user. Proceeding to dump core as root: %m", user); | |
673 | uid = gid = 0; | |
674 | } | |
675 | } else { | |
676 | r = parse_gid(context[CONTEXT_GID], &gid); | |
677 | if (r < 0) | |
678 | return r; | |
679 | } | |
680 | ||
681 | return drop_privileges(uid, gid, 0); | |
682 | } | |
683 | ||
684 | static bool is_journald_crash(const char *context[_CONTEXT_MAX]) { | |
685 | assert(context); | |
686 | ||
687 | return streq_ptr(context[CONTEXT_UNIT], SPECIAL_JOURNALD_SERVICE); | |
688 | } | |
689 | ||
690 | static bool is_pid1_crash(const char *context[_CONTEXT_MAX]) { | |
691 | assert(context); | |
692 | ||
693 | return streq_ptr(context[CONTEXT_UNIT], SPECIAL_INIT_SCOPE) || | |
694 | streq_ptr(context[CONTEXT_PID], "1"); | |
695 | } | |
696 | ||
697 | #define SUBMIT_COREDUMP_FIELDS 4 | |
698 | ||
699 | static int submit_coredump( | |
700 | const char *context[_CONTEXT_MAX], | |
701 | struct iovec *iovec, | |
702 | size_t n_iovec_allocated, | |
703 | size_t n_iovec, | |
704 | int input_fd) { | |
705 | ||
706 | _cleanup_close_ int coredump_fd = -1, coredump_node_fd = -1; | |
707 | _cleanup_free_ char *core_message = NULL, *filename = NULL, *coredump_data = NULL; | |
708 | uint64_t coredump_size = UINT64_MAX; | |
709 | bool truncated = false, journald_crash; | |
710 | int r; | |
711 | ||
712 | assert(context); | |
713 | assert(iovec); | |
714 | assert(n_iovec_allocated >= n_iovec + SUBMIT_COREDUMP_FIELDS); | |
715 | assert(input_fd >= 0); | |
716 | ||
717 | journald_crash = is_journald_crash(context); | |
718 | ||
719 | /* Vacuum before we write anything again */ | |
720 | (void) coredump_vacuum(-1, arg_keep_free, arg_max_use); | |
721 | ||
722 | /* Always stream the coredump to disk, if that's possible */ | |
723 | r = save_external_coredump(context, input_fd, | |
724 | &filename, &coredump_node_fd, &coredump_fd, &coredump_size, &truncated); | |
725 | if (r < 0) | |
726 | /* Skip whole core dumping part */ | |
727 | goto log; | |
728 | ||
729 | /* If we don't want to keep the coredump on disk, remove it now, as later on we will lack the privileges for | |
730 | * it. However, we keep the fd to it, so that we can still process it and log it. */ | |
731 | r = maybe_remove_external_coredump(filename, coredump_size); | |
732 | if (r < 0) | |
733 | return r; | |
734 | if (r == 0) { | |
735 | const char *coredump_filename; | |
736 | ||
737 | coredump_filename = strjoina("COREDUMP_FILENAME=", filename); | |
738 | iovec[n_iovec++] = IOVEC_MAKE_STRING(coredump_filename); | |
739 | } else if (arg_storage == COREDUMP_STORAGE_EXTERNAL) | |
740 | log_info("The core will not be stored: size %"PRIu64" is greater than %"PRIu64" (the configured maximum)", | |
741 | coredump_size, arg_external_size_max); | |
742 | ||
743 | /* Vacuum again, but exclude the coredump we just created */ | |
744 | (void) coredump_vacuum(coredump_node_fd >= 0 ? coredump_node_fd : coredump_fd, arg_keep_free, arg_max_use); | |
745 | ||
746 | /* Now, let's drop privileges to become the user who owns the segfaulted process and allocate the coredump | |
747 | * memory under the user's uid. This also ensures that the credentials journald will see are the ones of the | |
748 | * coredumping user, thus making sure the user gets access to the core dump. Let's also get rid of all | |
749 | * capabilities, if we run as root, we won't need them anymore. */ | |
750 | r = change_uid_gid(context); | |
751 | if (r < 0) | |
752 | return log_error_errno(r, "Failed to drop privileges: %m"); | |
753 | ||
754 | #if HAVE_ELFUTILS | |
755 | /* Try to get a strack trace if we can */ | |
756 | if (coredump_size <= arg_process_size_max) { | |
757 | _cleanup_free_ char *stacktrace = NULL; | |
758 | ||
759 | r = coredump_make_stack_trace(coredump_fd, context[CONTEXT_EXE], &stacktrace); | |
760 | if (r >= 0) | |
761 | core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID], | |
762 | " (", context[CONTEXT_COMM], ") of user ", | |
763 | context[CONTEXT_UID], " dumped core.", | |
764 | journald_crash ? "\nCoredump diverted to " : "", | |
765 | journald_crash ? filename : "", | |
766 | "\n\n", stacktrace); | |
767 | else if (r == -EINVAL) | |
768 | log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno())); | |
769 | else | |
770 | log_warning_errno(r, "Failed to generate stack trace: %m"); | |
771 | } else | |
772 | log_debug("Not generating stack trace: core size %"PRIu64" is greater than %"PRIu64" (the configured maximum)", | |
773 | coredump_size, arg_process_size_max); | |
774 | ||
775 | if (!core_message) | |
776 | #endif | |
777 | log: | |
778 | core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID], | |
779 | " (", context[CONTEXT_COMM], ") of user ", | |
780 | context[CONTEXT_UID], " dumped core.", | |
781 | journald_crash ? "\nCoredump diverted to " : NULL, | |
782 | journald_crash ? filename : NULL); | |
783 | if (!core_message) | |
784 | return log_oom(); | |
785 | ||
786 | if (journald_crash) { | |
787 | /* We cannot log to the journal, so just print the MESSAGE. | |
788 | * The target was set previously to something safe. */ | |
789 | log_dispatch(LOG_ERR, 0, core_message); | |
790 | return 0; | |
791 | } | |
792 | ||
793 | iovec[n_iovec++] = IOVEC_MAKE_STRING(core_message); | |
794 | ||
795 | if (truncated) | |
796 | iovec[n_iovec++] = IOVEC_MAKE_STRING("COREDUMP_TRUNCATED=1"); | |
797 | ||
798 | /* Optionally store the entire coredump in the journal */ | |
799 | if (arg_storage == COREDUMP_STORAGE_JOURNAL) { | |
800 | if (coredump_size <= arg_journal_size_max) { | |
801 | size_t sz = 0; | |
802 | ||
803 | /* Store the coredump itself in the journal */ | |
804 | ||
805 | r = allocate_journal_field(coredump_fd, (size_t) coredump_size, &coredump_data, &sz); | |
806 | if (r >= 0) | |
807 | iovec[n_iovec++] = IOVEC_MAKE(coredump_data, sz); | |
808 | else | |
809 | log_warning_errno(r, "Failed to attach the core to the journal entry: %m"); | |
810 | } else | |
811 | log_info("The core will not be stored: size %"PRIu64" is greater than %"PRIu64" (the configured maximum)", | |
812 | coredump_size, arg_journal_size_max); | |
813 | } | |
814 | ||
815 | assert(n_iovec <= n_iovec_allocated); | |
816 | ||
817 | r = sd_journal_sendv(iovec, n_iovec); | |
818 | if (r < 0) | |
819 | return log_error_errno(r, "Failed to log coredump: %m"); | |
820 | ||
821 | return 0; | |
822 | } | |
823 | ||
824 | static void map_context_fields(const struct iovec *iovec, const char* context[]) { | |
825 | ||
826 | static const char * const context_field_names[] = { | |
827 | [CONTEXT_PID] = "COREDUMP_PID=", | |
828 | [CONTEXT_UID] = "COREDUMP_UID=", | |
829 | [CONTEXT_GID] = "COREDUMP_GID=", | |
830 | [CONTEXT_SIGNAL] = "COREDUMP_SIGNAL=", | |
831 | [CONTEXT_TIMESTAMP] = "COREDUMP_TIMESTAMP=", | |
832 | [CONTEXT_RLIMIT] = "COREDUMP_RLIMIT=", | |
833 | [CONTEXT_HOSTNAME] = "COREDUMP_HOSTNAME=", | |
834 | [CONTEXT_COMM] = "COREDUMP_COMM=", | |
835 | [CONTEXT_EXE] = "COREDUMP_EXE=", | |
836 | }; | |
837 | ||
838 | unsigned i; | |
839 | ||
840 | assert(iovec); | |
841 | assert(context); | |
842 | ||
843 | for (i = 0; i < ELEMENTSOF(context_field_names); i++) { | |
844 | size_t l; | |
845 | ||
846 | if (!context_field_names[i]) | |
847 | continue; | |
848 | ||
849 | l = strlen(context_field_names[i]); | |
850 | if (iovec->iov_len < l) | |
851 | continue; | |
852 | ||
853 | if (memcmp(iovec->iov_base, context_field_names[i], l) != 0) | |
854 | continue; | |
855 | ||
856 | /* Note that these strings are NUL terminated, because we made sure that a trailing NUL byte is in the | |
857 | * buffer, though not included in the iov_len count. (see below) */ | |
858 | context[i] = (char*) iovec->iov_base + l; | |
859 | break; | |
860 | } | |
861 | } | |
862 | ||
863 | static int process_socket(int fd) { | |
864 | _cleanup_close_ int coredump_fd = -1; | |
865 | struct iovec *iovec = NULL; | |
866 | size_t n_iovec = 0, n_allocated = 0, i, k; | |
867 | const char *context[_CONTEXT_MAX] = {}; | |
868 | int r; | |
869 | ||
870 | assert(fd >= 0); | |
871 | ||
872 | log_set_target(LOG_TARGET_AUTO); | |
873 | log_parse_environment(); | |
874 | log_open(); | |
875 | ||
876 | log_debug("Processing coredump received on stdin..."); | |
877 | ||
878 | for (;;) { | |
879 | union { | |
880 | struct cmsghdr cmsghdr; | |
881 | uint8_t buf[CMSG_SPACE(sizeof(int))]; | |
882 | } control = {}; | |
883 | struct msghdr mh = { | |
884 | .msg_control = &control, | |
885 | .msg_controllen = sizeof(control), | |
886 | .msg_iovlen = 1, | |
887 | }; | |
888 | ssize_t n; | |
889 | ssize_t l; | |
890 | ||
891 | if (!GREEDY_REALLOC(iovec, n_allocated, n_iovec + SUBMIT_COREDUMP_FIELDS)) { | |
892 | r = log_oom(); | |
893 | goto finish; | |
894 | } | |
895 | ||
896 | l = next_datagram_size_fd(fd); | |
897 | if (l < 0) { | |
898 | r = log_error_errno(l, "Failed to determine datagram size to read: %m"); | |
899 | goto finish; | |
900 | } | |
901 | ||
902 | assert(l >= 0); | |
903 | ||
904 | iovec[n_iovec].iov_len = l; | |
905 | iovec[n_iovec].iov_base = malloc(l + 1); | |
906 | if (!iovec[n_iovec].iov_base) { | |
907 | r = log_oom(); | |
908 | goto finish; | |
909 | } | |
910 | ||
911 | mh.msg_iov = iovec + n_iovec; | |
912 | ||
913 | n = recvmsg(fd, &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC); | |
914 | if (n < 0) { | |
915 | free(iovec[n_iovec].iov_base); | |
916 | r = log_error_errno(errno, "Failed to receive datagram: %m"); | |
917 | goto finish; | |
918 | } | |
919 | ||
920 | if (n == 0) { | |
921 | struct cmsghdr *cmsg, *found = NULL; | |
922 | /* The final zero-length datagram carries the file descriptor and tells us that we're done. */ | |
923 | ||
924 | free(iovec[n_iovec].iov_base); | |
925 | ||
926 | CMSG_FOREACH(cmsg, &mh) { | |
927 | if (cmsg->cmsg_level == SOL_SOCKET && | |
928 | cmsg->cmsg_type == SCM_RIGHTS && | |
929 | cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { | |
930 | assert(!found); | |
931 | found = cmsg; | |
932 | } | |
933 | } | |
934 | ||
935 | if (!found) { | |
936 | log_error("Coredump file descriptor missing."); | |
937 | r = -EBADMSG; | |
938 | goto finish; | |
939 | } | |
940 | ||
941 | assert(coredump_fd < 0); | |
942 | coredump_fd = *(int*) CMSG_DATA(found); | |
943 | break; | |
944 | } | |
945 | ||
946 | /* Add trailing NUL byte, in case these are strings */ | |
947 | ((char*) iovec[n_iovec].iov_base)[n] = 0; | |
948 | iovec[n_iovec].iov_len = (size_t) n; | |
949 | ||
950 | cmsg_close_all(&mh); | |
951 | map_context_fields(iovec + n_iovec, context); | |
952 | n_iovec++; | |
953 | } | |
954 | ||
955 | if (!GREEDY_REALLOC(iovec, n_allocated, n_iovec + SUBMIT_COREDUMP_FIELDS)) { | |
956 | r = log_oom(); | |
957 | goto finish; | |
958 | } | |
959 | ||
960 | /* Make sure we got all data we really need */ | |
961 | assert(context[CONTEXT_PID]); | |
962 | assert(context[CONTEXT_UID]); | |
963 | assert(context[CONTEXT_GID]); | |
964 | assert(context[CONTEXT_SIGNAL]); | |
965 | assert(context[CONTEXT_TIMESTAMP]); | |
966 | assert(context[CONTEXT_RLIMIT]); | |
967 | assert(context[CONTEXT_HOSTNAME]); | |
968 | assert(context[CONTEXT_COMM]); | |
969 | assert(coredump_fd >= 0); | |
970 | ||
971 | /* Small quirk: the journal fields contain the timestamp padded with six zeroes, so that the kernel-supplied 1s | |
972 | * granularity timestamps becomes 1µs granularity, i.e. the granularity systemd usually operates in. Since we | |
973 | * are reconstructing the original kernel context, we chop this off again, here. */ | |
974 | k = strlen(context[CONTEXT_TIMESTAMP]); | |
975 | if (k > 6) | |
976 | context[CONTEXT_TIMESTAMP] = strndupa(context[CONTEXT_TIMESTAMP], k - 6); | |
977 | ||
978 | r = submit_coredump(context, iovec, n_allocated, n_iovec, coredump_fd); | |
979 | ||
980 | finish: | |
981 | for (i = 0; i < n_iovec; i++) | |
982 | free(iovec[i].iov_base); | |
983 | free(iovec); | |
984 | ||
985 | return r; | |
986 | } | |
987 | ||
988 | static int send_iovec(const struct iovec iovec[], size_t n_iovec, int input_fd) { | |
989 | ||
990 | static const union sockaddr_union sa = { | |
991 | .un.sun_family = AF_UNIX, | |
992 | .un.sun_path = "/run/systemd/coredump", | |
993 | }; | |
994 | _cleanup_close_ int fd = -1; | |
995 | size_t i; | |
996 | int r; | |
997 | ||
998 | assert(iovec || n_iovec <= 0); | |
999 | assert(input_fd >= 0); | |
1000 | ||
1001 | fd = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0); | |
1002 | if (fd < 0) | |
1003 | return log_error_errno(errno, "Failed to create coredump socket: %m"); | |
1004 | ||
1005 | if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) | |
1006 | return log_error_errno(errno, "Failed to connect to coredump service: %m"); | |
1007 | ||
1008 | for (i = 0; i < n_iovec; i++) { | |
1009 | struct msghdr mh = { | |
1010 | .msg_iov = (struct iovec*) iovec + i, | |
1011 | .msg_iovlen = 1, | |
1012 | }; | |
1013 | struct iovec copy[2]; | |
1014 | ||
1015 | for (;;) { | |
1016 | if (sendmsg(fd, &mh, MSG_NOSIGNAL) >= 0) | |
1017 | break; | |
1018 | ||
1019 | if (errno == EMSGSIZE && mh.msg_iov[0].iov_len > 0) { | |
1020 | /* This field didn't fit? That's a pity. Given that this is just metadata, | |
1021 | * let's truncate the field at half, and try again. We append three dots, in | |
1022 | * order to show that this is truncated. */ | |
1023 | ||
1024 | if (mh.msg_iov != copy) { | |
1025 | /* We don't want to modify the caller's iovec, hence let's create our | |
1026 | * own array, consisting of two new iovecs, where the first is a | |
1027 | * (truncated) copy of what we want to send, and the second one | |
1028 | * contains the trailing dots. */ | |
1029 | copy[0] = iovec[i]; | |
1030 | copy[1] = (struct iovec) { | |
1031 | .iov_base = (char[]) { '.', '.', '.' }, | |
1032 | .iov_len = 3, | |
1033 | }; | |
1034 | ||
1035 | mh.msg_iov = copy; | |
1036 | mh.msg_iovlen = 2; | |
1037 | } | |
1038 | ||
1039 | copy[0].iov_len /= 2; /* halve it, and try again */ | |
1040 | continue; | |
1041 | } | |
1042 | ||
1043 | return log_error_errno(errno, "Failed to send coredump datagram: %m"); | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | r = send_one_fd(fd, input_fd, 0); | |
1048 | if (r < 0) | |
1049 | return log_error_errno(r, "Failed to send coredump fd: %m"); | |
1050 | ||
1051 | return 0; | |
1052 | } | |
1053 | ||
1054 | static char* set_iovec_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) { | |
1055 | char *x; | |
1056 | ||
1057 | x = strappend(field, value); | |
1058 | if (x) | |
1059 | iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x); | |
1060 | return x; | |
1061 | } | |
1062 | ||
1063 | static char* set_iovec_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) { | |
1064 | char *x; | |
1065 | ||
1066 | x = set_iovec_field(iovec, n_iovec, field, value); | |
1067 | free(value); | |
1068 | return x; | |
1069 | } | |
1070 | ||
1071 | static int gather_pid_metadata( | |
1072 | char* context[_CONTEXT_MAX], | |
1073 | char **comm_fallback, | |
1074 | struct iovec *iovec, size_t *n_iovec) { | |
1075 | ||
1076 | /* We need 27 empty slots in iovec! | |
1077 | * | |
1078 | * Note that if we fail on oom later on, we do not roll-back changes to the iovec structure. (It remains valid, | |
1079 | * with the first n_iovec fields initialized.) */ | |
1080 | ||
1081 | uid_t owner_uid; | |
1082 | pid_t pid; | |
1083 | char *t; | |
1084 | const char *p; | |
1085 | int r, signo; | |
1086 | ||
1087 | r = parse_pid(context[CONTEXT_PID], &pid); | |
1088 | if (r < 0) | |
1089 | return log_error_errno(r, "Failed to parse PID \"%s\": %m", context[CONTEXT_PID]); | |
1090 | ||
1091 | r = get_process_comm(pid, &context[CONTEXT_COMM]); | |
1092 | if (r < 0) { | |
1093 | log_warning_errno(r, "Failed to get COMM, falling back to the command line: %m"); | |
1094 | context[CONTEXT_COMM] = strv_join(comm_fallback, " "); | |
1095 | if (!context[CONTEXT_COMM]) | |
1096 | return log_oom(); | |
1097 | } | |
1098 | ||
1099 | r = get_process_exe(pid, &context[CONTEXT_EXE]); | |
1100 | if (r < 0) | |
1101 | log_warning_errno(r, "Failed to get EXE, ignoring: %m"); | |
1102 | ||
1103 | if (cg_pid_get_unit(pid, &context[CONTEXT_UNIT]) >= 0) { | |
1104 | if (!is_journald_crash((const char**) context)) { | |
1105 | /* OK, now we know it's not the journal, hence we can make use of it now. */ | |
1106 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); | |
1107 | log_open(); | |
1108 | } | |
1109 | ||
1110 | /* If this is PID 1 disable coredump collection, we'll unlikely be able to process it later on. */ | |
1111 | if (is_pid1_crash((const char**) context)) { | |
1112 | log_notice("Due to PID 1 having crashed coredump collection will now be turned off."); | |
1113 | disable_coredumps(); | |
1114 | } | |
1115 | ||
1116 | set_iovec_field(iovec, n_iovec, "COREDUMP_UNIT=", context[CONTEXT_UNIT]); | |
1117 | } | |
1118 | ||
1119 | if (cg_pid_get_user_unit(pid, &t) >= 0) | |
1120 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_USER_UNIT=", t); | |
1121 | ||
1122 | /* The next few are mandatory */ | |
1123 | if (!set_iovec_field(iovec, n_iovec, "COREDUMP_PID=", context[CONTEXT_PID])) | |
1124 | return log_oom(); | |
1125 | ||
1126 | if (!set_iovec_field(iovec, n_iovec, "COREDUMP_UID=", context[CONTEXT_UID])) | |
1127 | return log_oom(); | |
1128 | ||
1129 | if (!set_iovec_field(iovec, n_iovec, "COREDUMP_GID=", context[CONTEXT_GID])) | |
1130 | return log_oom(); | |
1131 | ||
1132 | if (!set_iovec_field(iovec, n_iovec, "COREDUMP_SIGNAL=", context[CONTEXT_SIGNAL])) | |
1133 | return log_oom(); | |
1134 | ||
1135 | if (!set_iovec_field(iovec, n_iovec, "COREDUMP_RLIMIT=", context[CONTEXT_RLIMIT])) | |
1136 | return log_oom(); | |
1137 | ||
1138 | if (!set_iovec_field(iovec, n_iovec, "COREDUMP_HOSTNAME=", context[CONTEXT_HOSTNAME])) | |
1139 | return log_oom(); | |
1140 | ||
1141 | if (!set_iovec_field(iovec, n_iovec, "COREDUMP_COMM=", context[CONTEXT_COMM])) | |
1142 | return log_oom(); | |
1143 | ||
1144 | if (context[CONTEXT_EXE] && | |
1145 | !set_iovec_field(iovec, n_iovec, "COREDUMP_EXE=", context[CONTEXT_EXE])) | |
1146 | return log_oom(); | |
1147 | ||
1148 | if (sd_pid_get_session(pid, &t) >= 0) | |
1149 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_SESSION=", t); | |
1150 | ||
1151 | if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) { | |
1152 | r = asprintf(&t, "COREDUMP_OWNER_UID=" UID_FMT, owner_uid); | |
1153 | if (r > 0) | |
1154 | iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(t); | |
1155 | } | |
1156 | ||
1157 | if (sd_pid_get_slice(pid, &t) >= 0) | |
1158 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_SLICE=", t); | |
1159 | ||
1160 | if (get_process_cmdline(pid, 0, false, &t) >= 0) | |
1161 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_CMDLINE=", t); | |
1162 | ||
1163 | if (cg_pid_get_path_shifted(pid, NULL, &t) >= 0) | |
1164 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_CGROUP=", t); | |
1165 | ||
1166 | if (compose_open_fds(pid, &t) >= 0) | |
1167 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_OPEN_FDS=", t); | |
1168 | ||
1169 | p = procfs_file_alloca(pid, "status"); | |
1170 | if (read_full_file(p, &t, NULL) >= 0) | |
1171 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_STATUS=", t); | |
1172 | ||
1173 | p = procfs_file_alloca(pid, "maps"); | |
1174 | if (read_full_file(p, &t, NULL) >= 0) | |
1175 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_MAPS=", t); | |
1176 | ||
1177 | p = procfs_file_alloca(pid, "limits"); | |
1178 | if (read_full_file(p, &t, NULL) >= 0) | |
1179 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_LIMITS=", t); | |
1180 | ||
1181 | p = procfs_file_alloca(pid, "cgroup"); | |
1182 | if (read_full_file(p, &t, NULL) >=0) | |
1183 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_CGROUP=", t); | |
1184 | ||
1185 | p = procfs_file_alloca(pid, "mountinfo"); | |
1186 | if (read_full_file(p, &t, NULL) >=0) | |
1187 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_MOUNTINFO=", t); | |
1188 | ||
1189 | if (get_process_cwd(pid, &t) >= 0) | |
1190 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_CWD=", t); | |
1191 | ||
1192 | if (get_process_root(pid, &t) >= 0) { | |
1193 | bool proc_self_root_is_slash; | |
1194 | ||
1195 | proc_self_root_is_slash = strcmp(t, "/") == 0; | |
1196 | ||
1197 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_ROOT=", t); | |
1198 | ||
1199 | /* If the process' root is "/", then there is a chance it has | |
1200 | * mounted own root and hence being containerized. */ | |
1201 | if (proc_self_root_is_slash && get_process_container_parent_cmdline(pid, &t) > 0) | |
1202 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_CONTAINER_CMDLINE=", t); | |
1203 | } | |
1204 | ||
1205 | if (get_process_environ(pid, &t) >= 0) | |
1206 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_ENVIRON=", t); | |
1207 | ||
1208 | t = strjoin("COREDUMP_TIMESTAMP=", context[CONTEXT_TIMESTAMP], "000000"); | |
1209 | if (t) | |
1210 | iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(t); | |
1211 | ||
1212 | if (safe_atoi(context[CONTEXT_SIGNAL], &signo) >= 0 && SIGNAL_VALID(signo)) | |
1213 | set_iovec_field(iovec, n_iovec, "COREDUMP_SIGNAL_NAME=SIG", signal_to_string(signo)); | |
1214 | ||
1215 | return 0; /* we successfully acquired all metadata */ | |
1216 | } | |
1217 | ||
1218 | static int process_kernel(int argc, char* argv[]) { | |
1219 | ||
1220 | char* context[_CONTEXT_MAX] = {}; | |
1221 | struct iovec iovec[29 + SUBMIT_COREDUMP_FIELDS]; | |
1222 | size_t i, n_iovec, n_to_free = 0; | |
1223 | int r; | |
1224 | ||
1225 | log_debug("Processing coredump received from the kernel..."); | |
1226 | ||
1227 | if (argc < CONTEXT_COMM + 1) { | |
1228 | log_error("Not enough arguments passed by the kernel (%i, expected %i).", argc - 1, CONTEXT_COMM + 1 - 1); | |
1229 | return -EINVAL; | |
1230 | } | |
1231 | ||
1232 | context[CONTEXT_PID] = argv[1 + CONTEXT_PID]; | |
1233 | context[CONTEXT_UID] = argv[1 + CONTEXT_UID]; | |
1234 | context[CONTEXT_GID] = argv[1 + CONTEXT_GID]; | |
1235 | context[CONTEXT_SIGNAL] = argv[1 + CONTEXT_SIGNAL]; | |
1236 | context[CONTEXT_TIMESTAMP] = argv[1 + CONTEXT_TIMESTAMP]; | |
1237 | context[CONTEXT_RLIMIT] = argv[1 + CONTEXT_RLIMIT]; | |
1238 | context[CONTEXT_HOSTNAME] = argv[1 + CONTEXT_HOSTNAME]; | |
1239 | ||
1240 | r = gather_pid_metadata(context, argv + 1 + CONTEXT_COMM, iovec, &n_to_free); | |
1241 | if (r < 0) | |
1242 | goto finish; | |
1243 | ||
1244 | n_iovec = n_to_free; | |
1245 | ||
1246 | iovec[n_iovec++] = IOVEC_MAKE_STRING("MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR); | |
1247 | ||
1248 | assert_cc(2 == LOG_CRIT); | |
1249 | iovec[n_iovec++] = IOVEC_MAKE_STRING("PRIORITY=2"); | |
1250 | ||
1251 | assert(n_iovec <= ELEMENTSOF(iovec)); | |
1252 | ||
1253 | if (is_journald_crash((const char**) context) || is_pid1_crash((const char**) context)) | |
1254 | r = submit_coredump((const char**) context, | |
1255 | iovec, ELEMENTSOF(iovec), n_iovec, | |
1256 | STDIN_FILENO); | |
1257 | else | |
1258 | r = send_iovec(iovec, n_iovec, STDIN_FILENO); | |
1259 | ||
1260 | finish: | |
1261 | for (i = 0; i < n_to_free; i++) | |
1262 | free(iovec[i].iov_base); | |
1263 | ||
1264 | /* Those fields are allocated by gather_pid_metadata */ | |
1265 | free(context[CONTEXT_COMM]); | |
1266 | free(context[CONTEXT_EXE]); | |
1267 | free(context[CONTEXT_UNIT]); | |
1268 | ||
1269 | return r; | |
1270 | } | |
1271 | ||
1272 | static int process_backtrace(int argc, char *argv[]) { | |
1273 | char *context[_CONTEXT_MAX] = {}; | |
1274 | _cleanup_free_ char *message = NULL; | |
1275 | _cleanup_free_ struct iovec *iovec = NULL; | |
1276 | size_t n_iovec, n_allocated, n_to_free = 0, i; | |
1277 | int r; | |
1278 | JournalImporter importer = { | |
1279 | .fd = STDIN_FILENO, | |
1280 | }; | |
1281 | ||
1282 | log_debug("Processing backtrace on stdin..."); | |
1283 | ||
1284 | if (argc < CONTEXT_COMM + 1) { | |
1285 | log_error("Not enough arguments passed (%i, expected %i).", argc - 1, CONTEXT_COMM + 1 - 1); | |
1286 | return -EINVAL; | |
1287 | } | |
1288 | ||
1289 | context[CONTEXT_PID] = argv[2 + CONTEXT_PID]; | |
1290 | context[CONTEXT_UID] = argv[2 + CONTEXT_UID]; | |
1291 | context[CONTEXT_GID] = argv[2 + CONTEXT_GID]; | |
1292 | context[CONTEXT_SIGNAL] = argv[2 + CONTEXT_SIGNAL]; | |
1293 | context[CONTEXT_TIMESTAMP] = argv[2 + CONTEXT_TIMESTAMP]; | |
1294 | context[CONTEXT_RLIMIT] = argv[2 + CONTEXT_RLIMIT]; | |
1295 | context[CONTEXT_HOSTNAME] = argv[2 + CONTEXT_HOSTNAME]; | |
1296 | ||
1297 | n_allocated = 34 + COREDUMP_STORAGE_EXTERNAL; | |
1298 | /* 26 metadata, 2 static, +unknown input, 4 storage, rounded up */ | |
1299 | iovec = new(struct iovec, n_allocated); | |
1300 | if (!iovec) | |
1301 | return log_oom(); | |
1302 | ||
1303 | r = gather_pid_metadata(context, argv + 2 + CONTEXT_COMM, iovec, &n_to_free); | |
1304 | if (r < 0) | |
1305 | goto finish; | |
1306 | if (r > 0) { | |
1307 | /* This was a special crash, and has already been processed. */ | |
1308 | r = 0; | |
1309 | goto finish; | |
1310 | } | |
1311 | n_iovec = n_to_free; | |
1312 | ||
1313 | for (;;) { | |
1314 | r = journal_importer_process_data(&importer); | |
1315 | if (r < 0) { | |
1316 | log_error_errno(r, "Failed to parse journal entry on stdin: %m"); | |
1317 | goto finish; | |
1318 | } | |
1319 | if (r == 1 || /* complete entry */ | |
1320 | journal_importer_eof(&importer)) /* end of data */ | |
1321 | break; | |
1322 | } | |
1323 | ||
1324 | if (!GREEDY_REALLOC(iovec, n_allocated, n_iovec + importer.iovw.count + 2)) | |
1325 | return log_oom(); | |
1326 | ||
1327 | if (journal_importer_eof(&importer)) { | |
1328 | log_warning("Did not receive a full journal entry on stdin, ignoring message sent by reporter"); | |
1329 | ||
1330 | message = strjoin("MESSAGE=Process ", context[CONTEXT_PID], | |
1331 | " (", context[CONTEXT_COMM], ")" | |
1332 | " of user ", context[CONTEXT_UID], | |
1333 | " failed with ", context[CONTEXT_SIGNAL]); | |
1334 | if (!message) { | |
1335 | r = log_oom(); | |
1336 | goto finish; | |
1337 | } | |
1338 | iovec[n_iovec++] = IOVEC_MAKE_STRING(message); | |
1339 | } else { | |
1340 | for (i = 0; i < importer.iovw.count; i++) | |
1341 | iovec[n_iovec++] = importer.iovw.iovec[i]; | |
1342 | } | |
1343 | ||
1344 | iovec[n_iovec++] = IOVEC_MAKE_STRING("MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR); | |
1345 | assert_cc(2 == LOG_CRIT); | |
1346 | iovec[n_iovec++] = IOVEC_MAKE_STRING("PRIORITY=2"); | |
1347 | ||
1348 | assert(n_iovec <= n_allocated); | |
1349 | ||
1350 | r = sd_journal_sendv(iovec, n_iovec); | |
1351 | if (r < 0) | |
1352 | log_error_errno(r, "Failed to log backtrace: %m"); | |
1353 | ||
1354 | finish: | |
1355 | for (i = 0; i < n_to_free; i++) | |
1356 | free(iovec[i].iov_base); | |
1357 | ||
1358 | /* Those fields are allocated by gather_pid_metadata */ | |
1359 | free(context[CONTEXT_COMM]); | |
1360 | free(context[CONTEXT_EXE]); | |
1361 | free(context[CONTEXT_UNIT]); | |
1362 | ||
1363 | return r; | |
1364 | } | |
1365 | ||
1366 | int main(int argc, char *argv[]) { | |
1367 | int r; | |
1368 | ||
1369 | /* First, log to a safe place, since we don't know what crashed and it might | |
1370 | * be journald which we'd rather not log to then. */ | |
1371 | ||
1372 | log_set_target(LOG_TARGET_KMSG); | |
1373 | log_open(); | |
1374 | ||
1375 | /* Make sure we never enter a loop */ | |
1376 | (void) prctl(PR_SET_DUMPABLE, 0); | |
1377 | ||
1378 | /* Ignore all parse errors */ | |
1379 | (void) parse_config(); | |
1380 | ||
1381 | log_debug("Selected storage '%s'.", coredump_storage_to_string(arg_storage)); | |
1382 | log_debug("Selected compression %s.", yes_no(arg_compress)); | |
1383 | ||
1384 | r = sd_listen_fds(false); | |
1385 | if (r < 0) { | |
1386 | log_error_errno(r, "Failed to determine number of file descriptor: %m"); | |
1387 | goto finish; | |
1388 | } | |
1389 | ||
1390 | /* If we got an fd passed, we are running in coredumpd mode. Otherwise we | |
1391 | * are invoked from the kernel as coredump handler. */ | |
1392 | if (r == 0) { | |
1393 | if (streq_ptr(argv[1], "--backtrace")) | |
1394 | r = process_backtrace(argc, argv); | |
1395 | else | |
1396 | r = process_kernel(argc, argv); | |
1397 | } else if (r == 1) | |
1398 | r = process_socket(SD_LISTEN_FDS_START); | |
1399 | else { | |
1400 | log_error("Received unexpected number of file descriptors."); | |
1401 | r = -EINVAL; | |
1402 | } | |
1403 | ||
1404 | finish: | |
1405 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
1406 | } |