]>
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" | |
da9fc98d | 28 | #include "strv.h" |
731e10f3 | 29 | #include "unaligned.h" |
0153028a | 30 | |
3b3154df | 31 | static bool allow_object_pid(const struct ucred *ucred) { |
968f3196 ZJS |
32 | return ucred && ucred->uid == 0; |
33 | } | |
34 | ||
4b29a7f4 ZJS |
35 | static void server_process_entry_meta( |
36 | const char *p, size_t l, | |
37 | const struct ucred *ucred, | |
38 | int *priority, | |
39 | char **identifier, | |
40 | char **message, | |
41 | pid_t *object_pid) { | |
42 | ||
43 | /* We need to determine the priority of this entry for the rate limiting logic */ | |
44 | ||
45 | if (l == 10 && | |
46 | startswith(p, "PRIORITY=") && | |
47 | p[9] >= '0' && p[9] <= '9') | |
48 | *priority = (*priority & LOG_FACMASK) | (p[9] - '0'); | |
49 | ||
50 | else if (l == 17 && | |
51 | startswith(p, "SYSLOG_FACILITY=") && | |
52 | p[16] >= '0' && p[16] <= '9') | |
53 | *priority = (*priority & LOG_PRIMASK) | ((p[16] - '0') << 3); | |
54 | ||
55 | else if (l == 18 && | |
56 | startswith(p, "SYSLOG_FACILITY=") && | |
57 | p[16] >= '0' && p[16] <= '9' && | |
58 | p[17] >= '0' && p[17] <= '9') | |
59 | *priority = (*priority & LOG_PRIMASK) | (((p[16] - '0')*10 + (p[17] - '0')) << 3); | |
60 | ||
61 | else if (l >= 19 && | |
62 | startswith(p, "SYSLOG_IDENTIFIER=")) { | |
63 | char *t; | |
64 | ||
65 | t = strndup(p + 18, l - 18); | |
66 | if (t) { | |
67 | free(*identifier); | |
68 | *identifier = t; | |
69 | } | |
70 | ||
71 | } else if (l >= 8 && | |
72 | startswith(p, "MESSAGE=")) { | |
73 | char *t; | |
74 | ||
75 | t = strndup(p + 8, l - 8); | |
76 | if (t) { | |
77 | free(*message); | |
78 | *message = t; | |
79 | } | |
80 | ||
fbd0b64f LP |
81 | } else if (l > STRLEN("OBJECT_PID=") && |
82 | l < STRLEN("OBJECT_PID=") + DECIMAL_STR_MAX(pid_t) && | |
4b29a7f4 ZJS |
83 | startswith(p, "OBJECT_PID=") && |
84 | allow_object_pid(ucred)) { | |
85 | char buf[DECIMAL_STR_MAX(pid_t)]; | |
fbd0b64f LP |
86 | memcpy(buf, p + STRLEN("OBJECT_PID="), |
87 | l - STRLEN("OBJECT_PID=")); | |
88 | buf[l-STRLEN("OBJECT_PID=")] = '\0'; | |
4b29a7f4 ZJS |
89 | |
90 | (void) parse_pid(buf, object_pid); | |
91 | } | |
92 | } | |
93 | ||
68944f19 | 94 | static int server_process_entry( |
0153028a | 95 | Server *s, |
68944f19 | 96 | const void *buffer, size_t *remaining, |
22e3a02b | 97 | ClientContext *context, |
3b3154df LP |
98 | const struct ucred *ucred, |
99 | const struct timeval *tv, | |
0153028a LP |
100 | const char *label, size_t label_len) { |
101 | ||
d3070fbd LP |
102 | /* Process a single entry from a native message. Returns 0 if nothing special happened and the message |
103 | * processing should continue, and a negative or positive value otherwise. | |
68944f19 ZJS |
104 | * |
105 | * Note that *remaining is altered on both success and failure. */ | |
106 | ||
d3070fbd LP |
107 | size_t n = 0, j, tn = (size_t) -1, m = 0, entry_size = 0; |
108 | char *identifier = NULL, *message = NULL; | |
0153028a | 109 | struct iovec *iovec = NULL; |
0153028a | 110 | int priority = LOG_INFO; |
968f3196 | 111 | pid_t object_pid = 0; |
d3070fbd | 112 | const char *p; |
68944f19 | 113 | int r = 0; |
0153028a LP |
114 | |
115 | p = buffer; | |
0153028a | 116 | |
68944f19 | 117 | while (*remaining > 0) { |
0153028a LP |
118 | const char *e, *q; |
119 | ||
68944f19 | 120 | e = memchr(p, '\n', *remaining); |
0153028a LP |
121 | |
122 | if (!e) { | |
123 | /* Trailing noise, let's ignore it, and flush what we collected */ | |
124 | log_debug("Received message with trailing noise, ignoring."); | |
68944f19 | 125 | r = 1; /* finish processing of the message */ |
0153028a LP |
126 | break; |
127 | } | |
128 | ||
129 | if (e == p) { | |
130 | /* Entry separator */ | |
68944f19 ZJS |
131 | *remaining -= 1; |
132 | break; | |
0153028a LP |
133 | } |
134 | ||
4c701096 | 135 | if (IN_SET(*p, '.', '#')) { |
0153028a LP |
136 | /* Ignore control commands for now, and |
137 | * comments too. */ | |
68944f19 | 138 | *remaining -= (e - p) + 1; |
0153028a LP |
139 | p = e + 1; |
140 | continue; | |
141 | } | |
142 | ||
143 | /* A property follows */ | |
144 | ||
12a717f8 | 145 | /* n existing properties, 1 new, +1 for _TRANSPORT */ |
d3070fbd LP |
146 | if (!GREEDY_REALLOC(iovec, m, |
147 | n + 2 + | |
148 | N_IOVEC_META_FIELDS + N_IOVEC_OBJECT_FIELDS + | |
149 | client_context_extra_fields_n_iovec(context))) { | |
68944f19 | 150 | r = log_oom(); |
968f3196 | 151 | break; |
0153028a LP |
152 | } |
153 | ||
154 | q = memchr(p, '=', e - p); | |
155 | if (q) { | |
53978b98 | 156 | if (journal_field_valid(p, q - p, false)) { |
0153028a LP |
157 | size_t l; |
158 | ||
159 | l = e - p; | |
160 | ||
dde26374 LP |
161 | /* If the field name starts with an underscore, skip the variable, since that indicates |
162 | * a trusted field */ | |
163 | iovec[n++] = IOVEC_MAKE((char*) p, l); | |
68944f19 | 164 | entry_size += l; |
0153028a | 165 | |
4b29a7f4 ZJS |
166 | server_process_entry_meta(p, l, ucred, |
167 | &priority, | |
168 | &identifier, | |
169 | &message, | |
170 | &object_pid); | |
0153028a LP |
171 | } |
172 | ||
68944f19 | 173 | *remaining -= (e - p) + 1; |
0153028a LP |
174 | p = e + 1; |
175 | continue; | |
176 | } else { | |
0153028a LP |
177 | uint64_t l; |
178 | char *k; | |
179 | ||
68944f19 | 180 | if (*remaining < e - p + 1 + sizeof(uint64_t) + 1) { |
0153028a LP |
181 | log_debug("Failed to parse message, ignoring."); |
182 | break; | |
183 | } | |
184 | ||
731e10f3 | 185 | l = unaligned_read_le64(e + 1); |
0153028a | 186 | |
505b6a61 | 187 | if (l > DATA_SIZE_MAX) { |
fa1c4b51 | 188 | log_debug("Received binary data block of %"PRIu64" bytes is too large, ignoring.", l); |
505b6a61 LP |
189 | break; |
190 | } | |
191 | ||
68944f19 | 192 | if ((uint64_t) *remaining < e - p + 1 + sizeof(uint64_t) + l + 1 || |
0153028a LP |
193 | e[1+sizeof(uint64_t)+l] != '\n') { |
194 | log_debug("Failed to parse message, ignoring."); | |
195 | break; | |
196 | } | |
197 | ||
198 | k = malloc((e - p) + 1 + l); | |
199 | if (!k) { | |
200 | log_oom(); | |
201 | break; | |
202 | } | |
203 | ||
204 | memcpy(k, p, e - p); | |
205 | k[e - p] = '='; | |
206 | memcpy(k + (e - p) + 1, e + 1 + sizeof(uint64_t), l); | |
207 | ||
53978b98 | 208 | if (journal_field_valid(p, e - p, false)) { |
5cfa2c3d | 209 | iovec[n] = IOVEC_MAKE(k, (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, | |
21acb27b | 280 | const char *buffer, size_t buffer_size, |
68944f19 ZJS |
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 | ||
da9fc98d | 340 | e = PATH_STARTSWITH_SET(k, "/dev/shm/", "/tmp/", "/var/tmp/"); |
1dfa7e79 LP |
341 | if (!e) { |
342 | log_error("Received file outside of allowed directories. Refusing."); | |
343 | return; | |
344 | } | |
345 | ||
ae6c3cc0 | 346 | if (!filename_is_valid(e)) { |
1dfa7e79 LP |
347 | log_error("Received file in subdirectory of allowed directories. Refusing."); |
348 | return; | |
349 | } | |
350 | } | |
351 | ||
0153028a | 352 | if (fstat(fd, &st) < 0) { |
56f64d95 | 353 | log_error_errno(errno, "Failed to stat passed file, ignoring: %m"); |
0153028a LP |
354 | return; |
355 | } | |
356 | ||
357 | if (!S_ISREG(st.st_mode)) { | |
358 | log_error("File passed is not regular. Ignoring."); | |
359 | return; | |
360 | } | |
361 | ||
362 | if (st.st_size <= 0) | |
363 | return; | |
364 | ||
365 | if (st.st_size > ENTRY_SIZE_MAX) { | |
366 | log_error("File passed too large. Ignoring."); | |
367 | return; | |
368 | } | |
369 | ||
c79e98ea LP |
370 | if (sealed) { |
371 | void *p; | |
372 | size_t ps; | |
373 | ||
374 | /* The file is sealed, we can just map it and use it. */ | |
0153028a | 375 | |
c79e98ea LP |
376 | ps = PAGE_ALIGN(st.st_size); |
377 | p = mmap(NULL, ps, PROT_READ, MAP_PRIVATE, fd, 0); | |
378 | if (p == MAP_FAILED) { | |
56f64d95 | 379 | log_error_errno(errno, "Failed to map memfd, ignoring: %m"); |
c79e98ea LP |
380 | return; |
381 | } | |
382 | ||
383 | server_process_native_message(s, p, st.st_size, ucred, tv, label, label_len); | |
384 | assert_se(munmap(p, ps) >= 0); | |
385 | } else { | |
386 | _cleanup_free_ void *p = NULL; | |
1e603a48 | 387 | struct statvfs vfs; |
c79e98ea LP |
388 | ssize_t n; |
389 | ||
1e603a48 LP |
390 | if (fstatvfs(fd, &vfs) < 0) { |
391 | log_error_errno(errno, "Failed to stat file system of passed file, ignoring: %m"); | |
392 | return; | |
393 | } | |
394 | ||
395 | /* Refuse operating on file systems that have | |
396 | * mandatory locking enabled, see: | |
397 | * | |
398 | * https://github.com/systemd/systemd/issues/1822 | |
399 | */ | |
400 | if (vfs.f_flag & ST_MANDLOCK) { | |
1dc52f56 | 401 | log_error("Received file descriptor from file system with mandatory locking enabled, refusing."); |
1e603a48 LP |
402 | return; |
403 | } | |
404 | ||
405 | /* Make the fd non-blocking. On regular files this has | |
406 | * the effect of bypassing mandatory locking. Of | |
407 | * course, this should normally not be necessary given | |
408 | * the check above, but let's better be safe than | |
409 | * sorry, after all NFS is pretty confusing regarding | |
410 | * file system flags, and we better don't trust it, | |
411 | * and so is SMB. */ | |
412 | r = fd_nonblock(fd, true); | |
413 | if (r < 0) { | |
414 | log_error_errno(r, "Failed to make fd non-blocking, ignoring: %m"); | |
415 | return; | |
416 | } | |
417 | ||
c79e98ea LP |
418 | /* The file is not sealed, we can't map the file here, since |
419 | * clients might then truncate it and trigger a SIGBUS for | |
420 | * us. So let's stupidly read it */ | |
421 | ||
422 | p = malloc(st.st_size); | |
423 | if (!p) { | |
424 | log_oom(); | |
425 | return; | |
426 | } | |
427 | ||
428 | n = pread(fd, p, st.st_size, 0); | |
429 | if (n < 0) | |
c3753458 | 430 | log_error_errno(errno, "Failed to read file, ignoring: %m"); |
c79e98ea LP |
431 | else if (n > 0) |
432 | server_process_native_message(s, p, n, ucred, tv, label, label_len); | |
433 | } | |
0153028a LP |
434 | } |
435 | ||
f2a3de01 | 436 | int server_open_native_socket(Server *s) { |
fc2fffe7 LP |
437 | |
438 | static const union sockaddr_union sa = { | |
439 | .un.sun_family = AF_UNIX, | |
440 | .un.sun_path = "/run/systemd/journal/socket", | |
441 | }; | |
3b3154df | 442 | int r; |
0153028a LP |
443 | |
444 | assert(s); | |
445 | ||
446 | if (s->native_fd < 0) { | |
0153028a | 447 | s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
4a62c710 MS |
448 | if (s->native_fd < 0) |
449 | return log_error_errno(errno, "socket() failed: %m"); | |
0153028a | 450 | |
155b6876 | 451 | (void) sockaddr_un_unlink(&sa.un); |
0153028a | 452 | |
fc2fffe7 | 453 | r = bind(s->native_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); |
4a62c710 MS |
454 | if (r < 0) |
455 | return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path); | |
0153028a | 456 | |
4a61c3e5 | 457 | (void) chmod(sa.un.sun_path, 0666); |
0153028a | 458 | } else |
48440643 | 459 | (void) fd_nonblock(s->native_fd, true); |
0153028a | 460 | |
2ff48e98 | 461 | r = setsockopt_int(s->native_fd, SOL_SOCKET, SO_PASSCRED, true); |
4a62c710 | 462 | if (r < 0) |
2ff48e98 | 463 | return log_error_errno(r, "SO_PASSCRED failed: %m"); |
0153028a | 464 | |
349cc4a5 | 465 | #if HAVE_SELINUX |
6d395665 | 466 | if (mac_selinux_use()) { |
2ff48e98 | 467 | r = setsockopt_int(s->native_fd, SOL_SOCKET, SO_PASSSEC, true); |
d682b3a7 | 468 | if (r < 0) |
2ff48e98 | 469 | log_warning_errno(r, "SO_PASSSEC failed: %m"); |
d682b3a7 | 470 | } |
0153028a LP |
471 | #endif |
472 | ||
2ff48e98 | 473 | r = setsockopt_int(s->native_fd, SOL_SOCKET, SO_TIMESTAMP, true); |
4a62c710 | 474 | if (r < 0) |
2ff48e98 | 475 | return log_error_errno(r, "SO_TIMESTAMP failed: %m"); |
0153028a | 476 | |
8531ae70 | 477 | r = sd_event_add_io(s->event, &s->native_event_source, s->native_fd, EPOLLIN, server_process_datagram, s); |
23bbb0de MS |
478 | if (r < 0) |
479 | return log_error_errno(r, "Failed to add native server fd to event loop: %m"); | |
0153028a | 480 | |
48cef295 VC |
481 | r = sd_event_source_set_priority(s->native_event_source, SD_EVENT_PRIORITY_NORMAL+5); |
482 | if (r < 0) | |
483 | return log_error_errno(r, "Failed to adjust native event source priority: %m"); | |
484 | ||
0153028a LP |
485 | return 0; |
486 | } |