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