]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald-native.c
Merge pull request #11206 from cdown/cgroup_no_v1
[thirdparty/systemd.git] / src / journal / journald-native.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <stddef.h>
4 #include <sys/epoll.h>
5 #include <sys/mman.h>
6 #include <sys/statvfs.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "fd-util.h"
11 #include "fs-util.h"
12 #include "io-util.h"
13 #include "journal-importer.h"
14 #include "journal-util.h"
15 #include "journald-console.h"
16 #include "journald-kmsg.h"
17 #include "journald-native.h"
18 #include "journald-server.h"
19 #include "journald-syslog.h"
20 #include "journald-wall.h"
21 #include "memfd-util.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "process-util.h"
25 #include "selinux-util.h"
26 #include "socket-util.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "unaligned.h"
30
31 static bool allow_object_pid(const struct ucred *ucred) {
32 return ucred && ucred->uid == 0;
33 }
34
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
81 } else if (l > STRLEN("OBJECT_PID=") &&
82 l < STRLEN("OBJECT_PID=") + DECIMAL_STR_MAX(pid_t) &&
83 startswith(p, "OBJECT_PID=") &&
84 allow_object_pid(ucred)) {
85 char buf[DECIMAL_STR_MAX(pid_t)];
86 memcpy(buf, p + STRLEN("OBJECT_PID="),
87 l - STRLEN("OBJECT_PID="));
88 buf[l-STRLEN("OBJECT_PID=")] = '\0';
89
90 (void) parse_pid(buf, object_pid);
91 }
92 }
93
94 static int server_process_entry(
95 Server *s,
96 const void *buffer, size_t *remaining,
97 ClientContext *context,
98 const struct ucred *ucred,
99 const struct timeval *tv,
100 const char *label, size_t label_len) {
101
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.
104 *
105 * Note that *remaining is altered on both success and failure. */
106
107 size_t n = 0, j, tn = (size_t) -1, m = 0, entry_size = 0;
108 char *identifier = NULL, *message = NULL;
109 struct iovec *iovec = NULL;
110 int priority = LOG_INFO;
111 pid_t object_pid = 0;
112 const char *p;
113 int r = 0;
114
115 p = buffer;
116
117 while (*remaining > 0) {
118 const char *e, *q;
119
120 e = memchr(p, '\n', *remaining);
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.");
125 r = 1; /* finish processing of the message */
126 break;
127 }
128
129 if (e == p) {
130 /* Entry separator */
131 *remaining -= 1;
132 break;
133 }
134
135 if (IN_SET(*p, '.', '#')) {
136 /* Ignore control commands for now, and
137 * comments too. */
138 *remaining -= (e - p) + 1;
139 p = e + 1;
140 continue;
141 }
142
143 /* A property follows */
144
145 /* n existing properties, 1 new, +1 for _TRANSPORT */
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))) {
150 r = log_oom();
151 break;
152 }
153
154 q = memchr(p, '=', e - p);
155 if (q) {
156 if (journal_field_valid(p, q - p, false)) {
157 size_t l;
158
159 l = e - p;
160
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);
164 entry_size += l;
165
166 server_process_entry_meta(p, l, ucred,
167 &priority,
168 &identifier,
169 &message,
170 &object_pid);
171 }
172
173 *remaining -= (e - p) + 1;
174 p = e + 1;
175 continue;
176 } else {
177 uint64_t l;
178 char *k;
179
180 if (*remaining < e - p + 1 + sizeof(uint64_t) + 1) {
181 log_debug("Failed to parse message, ignoring.");
182 break;
183 }
184
185 l = unaligned_read_le64(e + 1);
186
187 if (l > DATA_SIZE_MAX) {
188 log_debug("Received binary data block of %"PRIu64" bytes is too large, ignoring.", l);
189 break;
190 }
191
192 if ((uint64_t) *remaining < e - p + 1 + sizeof(uint64_t) + l + 1 ||
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
208 if (journal_field_valid(p, e - p, false)) {
209 iovec[n] = IOVEC_MAKE(k, (e - p) + 1 + l);
210 entry_size += iovec[n].iov_len;
211 n++;
212
213 server_process_entry_meta(k, (e - p) + 1 + l, ucred,
214 &priority,
215 &identifier,
216 &message,
217 &object_pid);
218 } else
219 free(k);
220
221 *remaining -= (e - p) + 1 + sizeof(uint64_t) + l + 1;
222 p = e + 1 + sizeof(uint64_t) + l + 1;
223 }
224 }
225
226 if (n <= 0) {
227 r = 1;
228 goto finish;
229 }
230
231 if (!client_context_test_priority(context, priority)) {
232 r = 0;
233 goto finish;
234 }
235
236 tn = n++;
237 iovec[tn] = IOVEC_MAKE_STRING("_TRANSPORT=journal");
238 entry_size += STRLEN("_TRANSPORT=journal");
239
240 if (entry_size + n + 1 > ENTRY_SIZE_MAX) { /* data + separators + trailer */
241 log_debug("Entry is too big with %zu properties and %zu bytes, ignoring.", n, entry_size);
242 goto finish;
243 }
244
245 if (message) {
246 if (s->forward_to_syslog)
247 server_forward_syslog(s, syslog_fixup_facility(priority), identifier, message, ucred, tv);
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);
254
255 if (s->forward_to_wall)
256 server_forward_wall(s, priority, identifier, message, ucred);
257 }
258
259 server_dispatch_message(s, iovec, n, m, context, tv, priority, object_pid);
260
261 finish:
262 for (j = 0; j < n; j++) {
263 if (j == tn)
264 continue;
265
266 if (iovec[j].iov_base < buffer ||
267 (const char*) iovec[j].iov_base >= p + *remaining)
268 free(iovec[j].iov_base);
269 }
270
271 free(iovec);
272 free(identifier);
273 free(message);
274
275 return r;
276 }
277
278 void server_process_native_message(
279 Server *s,
280 const char *buffer, size_t buffer_size,
281 const struct ucred *ucred,
282 const struct timeval *tv,
283 const char *label, size_t label_len) {
284
285 size_t remaining = buffer_size;
286 ClientContext *context = NULL;
287 int r;
288
289 assert(s);
290 assert(buffer || buffer_size == 0);
291
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
298 do {
299 r = server_process_entry(s,
300 (const uint8_t*) buffer + (buffer_size - remaining), &remaining,
301 context, ucred, tv, label, label_len);
302 } while (r == 0);
303 }
304
305 void server_process_native_file(
306 Server *s,
307 int fd,
308 const struct ucred *ucred,
309 const struct timeval *tv,
310 const char *label, size_t label_len) {
311
312 struct stat st;
313 bool sealed;
314 int r;
315
316 /* Data is in the passed fd, since it didn't fit in a
317 * datagram. */
318
319 assert(s);
320 assert(fd >= 0);
321
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. */
325 sealed = memfd_get_sealed(fd) > 0;
326
327 if (!sealed && (!ucred || ucred->uid != 0)) {
328 _cleanup_free_ char *k = NULL;
329 const char *e;
330
331 /* If this is not a sealed memfd, and the peer is unknown or
332 * unprivileged, then verify the path. */
333
334 r = fd_get_path(fd, &k);
335 if (r < 0) {
336 log_error_errno(r, "readlink(/proc/self/fd/%i) failed: %m", fd);
337 return;
338 }
339
340 e = PATH_STARTSWITH_SET(k, "/dev/shm/", "/tmp/", "/var/tmp/");
341 if (!e) {
342 log_error("Received file outside of allowed directories. Refusing.");
343 return;
344 }
345
346 if (!filename_is_valid(e)) {
347 log_error("Received file in subdirectory of allowed directories. Refusing.");
348 return;
349 }
350 }
351
352 if (fstat(fd, &st) < 0) {
353 log_error_errno(errno, "Failed to stat passed file, ignoring: %m");
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
370 if (sealed) {
371 void *p;
372 size_t ps;
373
374 /* The file is sealed, we can just map it and use it. */
375
376 ps = PAGE_ALIGN(st.st_size);
377 p = mmap(NULL, ps, PROT_READ, MAP_PRIVATE, fd, 0);
378 if (p == MAP_FAILED) {
379 log_error_errno(errno, "Failed to map memfd, ignoring: %m");
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;
387 struct statvfs vfs;
388 ssize_t n;
389
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) {
401 log_error("Received file descriptor from file system with mandatory locking enabled, refusing.");
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
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)
430 log_error_errno(errno, "Failed to read file, ignoring: %m");
431 else if (n > 0)
432 server_process_native_message(s, p, n, ucred, tv, label, label_len);
433 }
434 }
435
436 int server_open_native_socket(Server *s) {
437
438 static const union sockaddr_union sa = {
439 .un.sun_family = AF_UNIX,
440 .un.sun_path = "/run/systemd/journal/socket",
441 };
442 int r;
443
444 assert(s);
445
446 if (s->native_fd < 0) {
447 s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
448 if (s->native_fd < 0)
449 return log_error_errno(errno, "socket() failed: %m");
450
451 (void) sockaddr_un_unlink(&sa.un);
452
453 r = bind(s->native_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
454 if (r < 0)
455 return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path);
456
457 (void) chmod(sa.un.sun_path, 0666);
458 } else
459 (void) fd_nonblock(s->native_fd, true);
460
461 r = setsockopt_int(s->native_fd, SOL_SOCKET, SO_PASSCRED, true);
462 if (r < 0)
463 return log_error_errno(r, "SO_PASSCRED failed: %m");
464
465 #if HAVE_SELINUX
466 if (mac_selinux_use()) {
467 r = setsockopt_int(s->native_fd, SOL_SOCKET, SO_PASSSEC, true);
468 if (r < 0)
469 log_warning_errno(r, "SO_PASSSEC failed: %m");
470 }
471 #endif
472
473 r = setsockopt_int(s->native_fd, SOL_SOCKET, SO_TIMESTAMP, true);
474 if (r < 0)
475 return log_error_errno(r, "SO_TIMESTAMP failed: %m");
476
477 r = sd_event_add_io(s->event, &s->native_event_source, s->native_fd, EPOLLIN, server_process_datagram, s);
478 if (r < 0)
479 return log_error_errno(r, "Failed to add native server fd to event loop: %m");
480
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
485 return 0;
486 }