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