]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
0153028a | 2 | |
4871690d | 3 | #include <stddef.h> |
0153028a | 4 | #include <sys/epoll.h> |
c79e98ea | 5 | #include <sys/mman.h> |
1e603a48 | 6 | #include <sys/statvfs.h> |
07630cea | 7 | #include <unistd.h> |
0153028a | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
3ffd4af2 | 10 | #include "fd-util.h" |
f4f15635 | 11 | #include "fs-util.h" |
afc5dbf3 | 12 | #include "io-util.h" |
b18453ed | 13 | #include "journal-importer.h" |
53978b98 | 14 | #include "journal-util.h" |
0153028a | 15 | #include "journald-console.h" |
07630cea | 16 | #include "journald-kmsg.h" |
3ffd4af2 | 17 | #include "journald-native.h" |
07630cea | 18 | #include "journald-server.h" |
0153028a | 19 | #include "journald-syslog.h" |
40b71e89 | 20 | #include "journald-wall.h" |
a09abc4a | 21 | #include "memfd-util.h" |
6bedfcbb | 22 | #include "parse-util.h" |
07630cea | 23 | #include "path-util.h" |
22e3a02b | 24 | #include "process-util.h" |
07630cea LP |
25 | #include "selinux-util.h" |
26 | #include "socket-util.h" | |
27 | #include "string-util.h" | |
731e10f3 | 28 | #include "unaligned.h" |
0153028a | 29 | |
3b3154df | 30 | static bool allow_object_pid(const struct ucred *ucred) { |
968f3196 ZJS |
31 | return ucred && ucred->uid == 0; |
32 | } | |
33 | ||
4b29a7f4 ZJS |
34 | static void server_process_entry_meta( |
35 | const char *p, size_t l, | |
36 | const struct ucred *ucred, | |
37 | int *priority, | |
38 | char **identifier, | |
39 | char **message, | |
40 | pid_t *object_pid) { | |
41 | ||
42 | /* We need to determine the priority of this entry for the rate limiting logic */ | |
43 | ||
44 | if (l == 10 && | |
45 | startswith(p, "PRIORITY=") && | |
46 | p[9] >= '0' && p[9] <= '9') | |
47 | *priority = (*priority & LOG_FACMASK) | (p[9] - '0'); | |
48 | ||
49 | else if (l == 17 && | |
50 | startswith(p, "SYSLOG_FACILITY=") && | |
51 | p[16] >= '0' && p[16] <= '9') | |
52 | *priority = (*priority & LOG_PRIMASK) | ((p[16] - '0') << 3); | |
53 | ||
54 | else if (l == 18 && | |
55 | startswith(p, "SYSLOG_FACILITY=") && | |
56 | p[16] >= '0' && p[16] <= '9' && | |
57 | p[17] >= '0' && p[17] <= '9') | |
58 | *priority = (*priority & LOG_PRIMASK) | (((p[16] - '0')*10 + (p[17] - '0')) << 3); | |
59 | ||
60 | else if (l >= 19 && | |
61 | startswith(p, "SYSLOG_IDENTIFIER=")) { | |
62 | char *t; | |
63 | ||
64 | t = strndup(p + 18, l - 18); | |
65 | if (t) { | |
66 | free(*identifier); | |
67 | *identifier = t; | |
68 | } | |
69 | ||
70 | } else if (l >= 8 && | |
71 | startswith(p, "MESSAGE=")) { | |
72 | char *t; | |
73 | ||
74 | t = strndup(p + 8, l - 8); | |
75 | if (t) { | |
76 | free(*message); | |
77 | *message = t; | |
78 | } | |
79 | ||
fbd0b64f LP |
80 | } else if (l > STRLEN("OBJECT_PID=") && |
81 | l < STRLEN("OBJECT_PID=") + DECIMAL_STR_MAX(pid_t) && | |
4b29a7f4 ZJS |
82 | startswith(p, "OBJECT_PID=") && |
83 | allow_object_pid(ucred)) { | |
84 | char buf[DECIMAL_STR_MAX(pid_t)]; | |
fbd0b64f LP |
85 | memcpy(buf, p + STRLEN("OBJECT_PID="), |
86 | l - STRLEN("OBJECT_PID=")); | |
87 | buf[l-STRLEN("OBJECT_PID=")] = '\0'; | |
4b29a7f4 ZJS |
88 | |
89 | (void) parse_pid(buf, object_pid); | |
90 | } | |
91 | } | |
92 | ||
68944f19 | 93 | static int server_process_entry( |
0153028a | 94 | Server *s, |
68944f19 | 95 | const void *buffer, size_t *remaining, |
22e3a02b | 96 | ClientContext *context, |
3b3154df LP |
97 | const struct ucred *ucred, |
98 | const struct timeval *tv, | |
0153028a LP |
99 | const char *label, size_t label_len) { |
100 | ||
d3070fbd LP |
101 | /* Process a single entry from a native message. Returns 0 if nothing special happened and the message |
102 | * processing should continue, and a negative or positive value otherwise. | |
68944f19 ZJS |
103 | * |
104 | * Note that *remaining is altered on both success and failure. */ | |
105 | ||
d3070fbd LP |
106 | size_t n = 0, j, tn = (size_t) -1, m = 0, entry_size = 0; |
107 | char *identifier = NULL, *message = NULL; | |
0153028a | 108 | struct iovec *iovec = NULL; |
0153028a | 109 | int priority = LOG_INFO; |
968f3196 | 110 | pid_t object_pid = 0; |
d3070fbd | 111 | const char *p; |
68944f19 | 112 | int r = 0; |
0153028a LP |
113 | |
114 | p = buffer; | |
0153028a | 115 | |
68944f19 | 116 | while (*remaining > 0) { |
0153028a LP |
117 | const char *e, *q; |
118 | ||
68944f19 | 119 | e = memchr(p, '\n', *remaining); |
0153028a LP |
120 | |
121 | if (!e) { | |
122 | /* Trailing noise, let's ignore it, and flush what we collected */ | |
123 | log_debug("Received message with trailing noise, ignoring."); | |
68944f19 | 124 | r = 1; /* finish processing of the message */ |
0153028a LP |
125 | break; |
126 | } | |
127 | ||
128 | if (e == p) { | |
129 | /* Entry separator */ | |
68944f19 ZJS |
130 | *remaining -= 1; |
131 | break; | |
0153028a LP |
132 | } |
133 | ||
4c701096 | 134 | if (IN_SET(*p, '.', '#')) { |
0153028a LP |
135 | /* Ignore control commands for now, and |
136 | * comments too. */ | |
68944f19 | 137 | *remaining -= (e - p) + 1; |
0153028a LP |
138 | p = e + 1; |
139 | continue; | |
140 | } | |
141 | ||
142 | /* A property follows */ | |
143 | ||
12a717f8 | 144 | /* n existing properties, 1 new, +1 for _TRANSPORT */ |
d3070fbd LP |
145 | if (!GREEDY_REALLOC(iovec, m, |
146 | n + 2 + | |
147 | N_IOVEC_META_FIELDS + N_IOVEC_OBJECT_FIELDS + | |
148 | client_context_extra_fields_n_iovec(context))) { | |
68944f19 | 149 | r = log_oom(); |
968f3196 | 150 | break; |
0153028a LP |
151 | } |
152 | ||
153 | q = memchr(p, '=', e - p); | |
154 | if (q) { | |
53978b98 | 155 | if (journal_field_valid(p, q - p, false)) { |
0153028a LP |
156 | size_t l; |
157 | ||
158 | l = e - p; | |
159 | ||
dde26374 LP |
160 | /* If the field name starts with an underscore, skip the variable, since that indicates |
161 | * a trusted field */ | |
162 | iovec[n++] = IOVEC_MAKE((char*) p, l); | |
68944f19 | 163 | entry_size += l; |
0153028a | 164 | |
4b29a7f4 ZJS |
165 | server_process_entry_meta(p, l, ucred, |
166 | &priority, | |
167 | &identifier, | |
168 | &message, | |
169 | &object_pid); | |
0153028a LP |
170 | } |
171 | ||
68944f19 | 172 | *remaining -= (e - p) + 1; |
0153028a LP |
173 | p = e + 1; |
174 | continue; | |
175 | } else { | |
0153028a LP |
176 | uint64_t l; |
177 | char *k; | |
178 | ||
68944f19 | 179 | if (*remaining < e - p + 1 + sizeof(uint64_t) + 1) { |
0153028a LP |
180 | log_debug("Failed to parse message, ignoring."); |
181 | break; | |
182 | } | |
183 | ||
731e10f3 | 184 | l = unaligned_read_le64(e + 1); |
0153028a | 185 | |
505b6a61 | 186 | if (l > DATA_SIZE_MAX) { |
fa1c4b51 | 187 | log_debug("Received binary data block of %"PRIu64" bytes is too large, ignoring.", l); |
505b6a61 LP |
188 | break; |
189 | } | |
190 | ||
68944f19 | 191 | if ((uint64_t) *remaining < e - p + 1 + sizeof(uint64_t) + l + 1 || |
0153028a LP |
192 | e[1+sizeof(uint64_t)+l] != '\n') { |
193 | log_debug("Failed to parse message, ignoring."); | |
194 | break; | |
195 | } | |
196 | ||
197 | k = malloc((e - p) + 1 + l); | |
198 | if (!k) { | |
199 | log_oom(); | |
200 | break; | |
201 | } | |
202 | ||
203 | memcpy(k, p, e - p); | |
204 | k[e - p] = '='; | |
205 | memcpy(k + (e - p) + 1, e + 1 + sizeof(uint64_t), l); | |
206 | ||
53978b98 | 207 | if (journal_field_valid(p, e - p, false)) { |
0153028a LP |
208 | iovec[n].iov_base = k; |
209 | iovec[n].iov_len = (e - p) + 1 + l; | |
874bc134 | 210 | entry_size += iovec[n].iov_len; |
a174f94d | 211 | n++; |
4b29a7f4 ZJS |
212 | |
213 | server_process_entry_meta(k, (e - p) + 1 + l, ucred, | |
214 | &priority, | |
215 | &identifier, | |
216 | &message, | |
217 | &object_pid); | |
0153028a LP |
218 | } else |
219 | free(k); | |
220 | ||
68944f19 | 221 | *remaining -= (e - p) + 1 + sizeof(uint64_t) + l + 1; |
0153028a LP |
222 | p = e + 1 + sizeof(uint64_t) + l + 1; |
223 | } | |
224 | } | |
225 | ||
68944f19 ZJS |
226 | if (n <= 0) { |
227 | r = 1; | |
0153028a | 228 | goto finish; |
68944f19 | 229 | } |
0153028a | 230 | |
d3070fbd LP |
231 | if (!client_context_test_priority(context, priority)) { |
232 | r = 0; | |
233 | goto finish; | |
234 | } | |
235 | ||
0153028a | 236 | tn = n++; |
e6a7ec4b | 237 | iovec[tn] = IOVEC_MAKE_STRING("_TRANSPORT=journal"); |
fbd0b64f | 238 | entry_size += STRLEN("_TRANSPORT=journal"); |
874bc134 ZJS |
239 | |
240 | if (entry_size + n + 1 > ENTRY_SIZE_MAX) { /* data + separators + trailer */ | |
d3070fbd | 241 | log_debug("Entry is too big with %zu properties and %zu bytes, ignoring.", n, entry_size); |
874bc134 ZJS |
242 | goto finish; |
243 | } | |
0153028a LP |
244 | |
245 | if (message) { | |
246 | if (s->forward_to_syslog) | |
b6a20306 | 247 | server_forward_syslog(s, syslog_fixup_facility(priority), identifier, message, ucred, tv); |
0153028a LP |
248 | |
249 | if (s->forward_to_kmsg) | |
250 | server_forward_kmsg(s, priority, identifier, message, ucred); | |
251 | ||
252 | if (s->forward_to_console) | |
253 | server_forward_console(s, priority, identifier, message, ucred); | |
40b71e89 ST |
254 | |
255 | if (s->forward_to_wall) | |
256 | server_forward_wall(s, priority, identifier, message, ucred); | |
0153028a LP |
257 | } |
258 | ||
22e3a02b | 259 | server_dispatch_message(s, iovec, n, m, context, tv, priority, object_pid); |
0153028a LP |
260 | |
261 | finish: | |
262 | for (j = 0; j < n; j++) { | |
263 | if (j == tn) | |
264 | continue; | |
265 | ||
266 | if (iovec[j].iov_base < buffer || | |
68944f19 | 267 | (const char*) iovec[j].iov_base >= p + *remaining) |
0153028a LP |
268 | free(iovec[j].iov_base); |
269 | } | |
270 | ||
271 | free(iovec); | |
272 | free(identifier); | |
273 | free(message); | |
68944f19 ZJS |
274 | |
275 | return r; | |
276 | } | |
277 | ||
278 | void server_process_native_message( | |
279 | Server *s, | |
280 | const void *buffer, size_t buffer_size, | |
281 | const struct ucred *ucred, | |
282 | const struct timeval *tv, | |
283 | const char *label, size_t label_len) { | |
284 | ||
68944f19 | 285 | size_t remaining = buffer_size; |
1d3e682e | 286 | ClientContext *context = NULL; |
22e3a02b | 287 | int r; |
68944f19 ZJS |
288 | |
289 | assert(s); | |
290 | assert(buffer || buffer_size == 0); | |
291 | ||
22e3a02b LP |
292 | if (ucred && pid_is_valid(ucred->pid)) { |
293 | r = client_context_get(s, ucred->pid, ucred, label, label_len, NULL, &context); | |
294 | if (r < 0) | |
295 | log_warning_errno(r, "Failed to retrieve credentials for PID " PID_FMT ", ignoring: %m", ucred->pid); | |
296 | } | |
297 | ||
68944f19 ZJS |
298 | do { |
299 | r = server_process_entry(s, | |
300 | (const uint8_t*) buffer + (buffer_size - remaining), &remaining, | |
22e3a02b | 301 | context, ucred, tv, label, label_len); |
68944f19 | 302 | } while (r == 0); |
0153028a LP |
303 | } |
304 | ||
305 | void server_process_native_file( | |
306 | Server *s, | |
307 | int fd, | |
3b3154df LP |
308 | const struct ucred *ucred, |
309 | const struct timeval *tv, | |
0153028a LP |
310 | const char *label, size_t label_len) { |
311 | ||
312 | struct stat st; | |
c79e98ea | 313 | bool sealed; |
1dfa7e79 | 314 | int r; |
0153028a | 315 | |
c79e98ea LP |
316 | /* Data is in the passed fd, since it didn't fit in a |
317 | * datagram. */ | |
318 | ||
0153028a LP |
319 | assert(s); |
320 | assert(fd >= 0); | |
321 | ||
c79e98ea LP |
322 | /* If it's a memfd, check if it is sealed. If so, we can just |
323 | * use map it and use it, and do not need to copy the data | |
324 | * out. */ | |
73843b52 | 325 | sealed = memfd_get_sealed(fd) > 0; |
c79e98ea LP |
326 | |
327 | if (!sealed && (!ucred || ucred->uid != 0)) { | |
cf852ff6 | 328 | _cleanup_free_ char *k = NULL; |
1dfa7e79 LP |
329 | const char *e; |
330 | ||
c79e98ea LP |
331 | /* If this is not a sealed memfd, and the peer is unknown or |
332 | * unprivileged, then verify the path. */ | |
333 | ||
cf852ff6 | 334 | r = fd_get_path(fd, &k); |
1dfa7e79 | 335 | if (r < 0) { |
cf852ff6 | 336 | log_error_errno(r, "readlink(/proc/self/fd/%i) failed: %m", fd); |
1dfa7e79 LP |
337 | return; |
338 | } | |
339 | ||
340 | e = path_startswith(k, "/dev/shm/"); | |
341 | if (!e) | |
342 | e = path_startswith(k, "/tmp/"); | |
343 | if (!e) | |
344 | e = path_startswith(k, "/var/tmp/"); | |
345 | if (!e) { | |
346 | log_error("Received file outside of allowed directories. Refusing."); | |
347 | return; | |
348 | } | |
349 | ||
ae6c3cc0 | 350 | if (!filename_is_valid(e)) { |
1dfa7e79 LP |
351 | log_error("Received file in subdirectory of allowed directories. Refusing."); |
352 | return; | |
353 | } | |
354 | } | |
355 | ||
0153028a | 356 | if (fstat(fd, &st) < 0) { |
56f64d95 | 357 | log_error_errno(errno, "Failed to stat passed file, ignoring: %m"); |
0153028a LP |
358 | return; |
359 | } | |
360 | ||
361 | if (!S_ISREG(st.st_mode)) { | |
362 | log_error("File passed is not regular. Ignoring."); | |
363 | return; | |
364 | } | |
365 | ||
366 | if (st.st_size <= 0) | |
367 | return; | |
368 | ||
369 | if (st.st_size > ENTRY_SIZE_MAX) { | |
370 | log_error("File passed too large. Ignoring."); | |
371 | return; | |
372 | } | |
373 | ||
c79e98ea LP |
374 | if (sealed) { |
375 | void *p; | |
376 | size_t ps; | |
377 | ||
378 | /* The file is sealed, we can just map it and use it. */ | |
0153028a | 379 | |
c79e98ea LP |
380 | ps = PAGE_ALIGN(st.st_size); |
381 | p = mmap(NULL, ps, PROT_READ, MAP_PRIVATE, fd, 0); | |
382 | if (p == MAP_FAILED) { | |
56f64d95 | 383 | log_error_errno(errno, "Failed to map memfd, ignoring: %m"); |
c79e98ea LP |
384 | return; |
385 | } | |
386 | ||
387 | server_process_native_message(s, p, st.st_size, ucred, tv, label, label_len); | |
388 | assert_se(munmap(p, ps) >= 0); | |
389 | } else { | |
390 | _cleanup_free_ void *p = NULL; | |
1e603a48 | 391 | struct statvfs vfs; |
c79e98ea LP |
392 | ssize_t n; |
393 | ||
1e603a48 LP |
394 | if (fstatvfs(fd, &vfs) < 0) { |
395 | log_error_errno(errno, "Failed to stat file system of passed file, ignoring: %m"); | |
396 | return; | |
397 | } | |
398 | ||
399 | /* Refuse operating on file systems that have | |
400 | * mandatory locking enabled, see: | |
401 | * | |
402 | * https://github.com/systemd/systemd/issues/1822 | |
403 | */ | |
404 | if (vfs.f_flag & ST_MANDLOCK) { | |
1dc52f56 | 405 | log_error("Received file descriptor from file system with mandatory locking enabled, refusing."); |
1e603a48 LP |
406 | return; |
407 | } | |
408 | ||
409 | /* Make the fd non-blocking. On regular files this has | |
410 | * the effect of bypassing mandatory locking. Of | |
411 | * course, this should normally not be necessary given | |
412 | * the check above, but let's better be safe than | |
413 | * sorry, after all NFS is pretty confusing regarding | |
414 | * file system flags, and we better don't trust it, | |
415 | * and so is SMB. */ | |
416 | r = fd_nonblock(fd, true); | |
417 | if (r < 0) { | |
418 | log_error_errno(r, "Failed to make fd non-blocking, ignoring: %m"); | |
419 | return; | |
420 | } | |
421 | ||
c79e98ea LP |
422 | /* The file is not sealed, we can't map the file here, since |
423 | * clients might then truncate it and trigger a SIGBUS for | |
424 | * us. So let's stupidly read it */ | |
425 | ||
426 | p = malloc(st.st_size); | |
427 | if (!p) { | |
428 | log_oom(); | |
429 | return; | |
430 | } | |
431 | ||
432 | n = pread(fd, p, st.st_size, 0); | |
433 | if (n < 0) | |
c3753458 | 434 | log_error_errno(errno, "Failed to read file, ignoring: %m"); |
c79e98ea LP |
435 | else if (n > 0) |
436 | server_process_native_message(s, p, n, ucred, tv, label, label_len); | |
437 | } | |
0153028a LP |
438 | } |
439 | ||
440 | int server_open_native_socket(Server*s) { | |
fc2fffe7 LP |
441 | |
442 | static const union sockaddr_union sa = { | |
443 | .un.sun_family = AF_UNIX, | |
444 | .un.sun_path = "/run/systemd/journal/socket", | |
445 | }; | |
3b3154df LP |
446 | static const int one = 1; |
447 | int r; | |
0153028a LP |
448 | |
449 | assert(s); | |
450 | ||
451 | if (s->native_fd < 0) { | |
0153028a | 452 | s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
4a62c710 MS |
453 | if (s->native_fd < 0) |
454 | return log_error_errno(errno, "socket() failed: %m"); | |
0153028a | 455 | |
fc2fffe7 | 456 | (void) unlink(sa.un.sun_path); |
0153028a | 457 | |
fc2fffe7 | 458 | r = bind(s->native_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); |
4a62c710 MS |
459 | if (r < 0) |
460 | return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path); | |
0153028a | 461 | |
4a61c3e5 | 462 | (void) chmod(sa.un.sun_path, 0666); |
0153028a LP |
463 | } else |
464 | fd_nonblock(s->native_fd, 1); | |
465 | ||
0153028a | 466 | r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); |
4a62c710 MS |
467 | if (r < 0) |
468 | return log_error_errno(errno, "SO_PASSCRED failed: %m"); | |
0153028a | 469 | |
349cc4a5 | 470 | #if HAVE_SELINUX |
6d395665 | 471 | if (mac_selinux_use()) { |
d682b3a7 LP |
472 | r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one)); |
473 | if (r < 0) | |
56f64d95 | 474 | log_warning_errno(errno, "SO_PASSSEC failed: %m"); |
d682b3a7 | 475 | } |
0153028a LP |
476 | #endif |
477 | ||
0153028a | 478 | r = setsockopt(s->native_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one)); |
4a62c710 MS |
479 | if (r < 0) |
480 | return log_error_errno(errno, "SO_TIMESTAMP failed: %m"); | |
0153028a | 481 | |
8531ae70 | 482 | r = sd_event_add_io(s->event, &s->native_event_source, s->native_fd, EPOLLIN, server_process_datagram, s); |
23bbb0de MS |
483 | if (r < 0) |
484 | return log_error_errno(r, "Failed to add native server fd to event loop: %m"); | |
0153028a | 485 | |
48cef295 VC |
486 | r = sd_event_source_set_priority(s->native_event_source, SD_EVENT_PRIORITY_NORMAL+5); |
487 | if (r < 0) | |
488 | return log_error_errno(r, "Failed to adjust native event source priority: %m"); | |
489 | ||
0153028a LP |
490 | return 0; |
491 | } |