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