]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald-native.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <sys/statvfs.h>
9 #include "alloc-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"
29 #include "unaligned.h"
31 static bool allow_object_pid(const struct ucred
*ucred
) {
32 return ucred
&& ucred
->uid
== 0;
35 static void server_process_entry_meta(
36 const char *p
, size_t l
,
37 const struct ucred
*ucred
,
43 /* We need to determine the priority of this entry for the rate limiting logic */
46 startswith(p
, "PRIORITY=") &&
47 p
[9] >= '0' && p
[9] <= '9')
48 *priority
= (*priority
& LOG_FACMASK
) | (p
[9] - '0');
51 startswith(p
, "SYSLOG_FACILITY=") &&
52 p
[16] >= '0' && p
[16] <= '9')
53 *priority
= (*priority
& LOG_PRIMASK
) | ((p
[16] - '0') << 3);
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);
62 startswith(p
, "SYSLOG_IDENTIFIER=")) {
65 t
= strndup(p
+ 18, l
- 18);
72 startswith(p
, "MESSAGE=")) {
75 t
= strndup(p
+ 8, l
- 8);
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';
90 (void) parse_pid(buf
, object_pid
);
94 static int server_process_entry(
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
) {
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.
105 * Note that *remaining is altered on both success and failure. */
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;
117 while (*remaining
> 0) {
120 e
= memchr(p
, '\n', *remaining
);
123 /* Trailing noise, let's ignore it, and flush what we collected */
124 log_debug("Received message with trailing noise, ignoring.");
125 break; /* finish processing of the message */
129 /* Entry separator */
134 if (IN_SET(*p
, '.', '#')) {
135 /* Ignore control commands for now, and comments too. */
136 *remaining
-= (e
- p
) + 1;
141 /* A property follows */
142 if (n
> ENTRY_FIELD_COUNT_MAX
) {
143 log_debug("Received an entry that has more than " STRINGIFY(ENTRY_FIELD_COUNT_MAX
) " fields, ignoring entry.");
147 /* n existing properties, 1 new, +1 for _TRANSPORT */
148 if (!GREEDY_REALLOC(iovec
, m
,
150 N_IOVEC_META_FIELDS
+ N_IOVEC_OBJECT_FIELDS
+
151 client_context_extra_fields_n_iovec(context
))) {
156 q
= memchr(p
, '=', e
- p
);
158 if (journal_field_valid(p
, q
- p
, false)) {
162 if (l
> DATA_SIZE_MAX
) {
163 log_debug("Received text block of %zu bytes is too large, ignoring entry.", l
);
167 if (entry_size
+ l
+ n
+ 1 > ENTRY_SIZE_MAX
) { /* data + separators + trailer */
168 log_debug("Entry is too big (%zu bytes after processing %zu entries), ignoring entry.",
169 entry_size
+ l
, n
+ 1);
173 /* If the field name starts with an underscore, skip the variable, since that indicates
175 iovec
[n
++] = IOVEC_MAKE((char*) p
, l
);
178 server_process_entry_meta(p
, l
, ucred
,
185 *remaining
-= (e
- p
) + 1;
192 if (*remaining
< e
- p
+ 1 + sizeof(uint64_t) + 1) {
193 log_debug("Failed to parse message, ignoring.");
197 l
= unaligned_read_le64(e
+ 1);
198 if (l
> DATA_SIZE_MAX
) {
199 log_debug("Received binary data block of %"PRIu64
" bytes is too large, ignoring entry.", l
);
203 total
= (e
- p
) + 1 + l
;
204 if (entry_size
+ total
+ n
+ 1 > ENTRY_SIZE_MAX
) { /* data + separators + trailer */
205 log_debug("Entry is too big (%"PRIu64
"bytes after processing %zu fields), ignoring.",
206 entry_size
+ total
, n
+ 1);
210 if ((uint64_t) *remaining
< e
- p
+ 1 + sizeof(uint64_t) + l
+ 1 ||
211 e
[1+sizeof(uint64_t)+l
] != '\n') {
212 log_debug("Failed to parse message, ignoring.");
224 memcpy(k
+ (e
- p
) + 1, e
+ 1 + sizeof(uint64_t), l
);
226 if (journal_field_valid(p
, e
- p
, false)) {
227 iovec
[n
] = IOVEC_MAKE(k
, (e
- p
) + 1 + l
);
228 entry_size
+= iovec
[n
].iov_len
;
231 server_process_entry_meta(k
, (e
- p
) + 1 + l
, ucred
,
239 *remaining
-= (e
- p
) + 1 + sizeof(uint64_t) + l
+ 1;
240 p
= e
+ 1 + sizeof(uint64_t) + l
+ 1;
248 iovec
[tn
] = IOVEC_MAKE_STRING("_TRANSPORT=journal");
249 entry_size
+= STRLEN("_TRANSPORT=journal");
251 if (entry_size
+ n
+ 1 > ENTRY_SIZE_MAX
) { /* data + separators + trailer */
252 log_debug("Entry is too big with %zu properties and %zu bytes, ignoring.", n
, entry_size
);
256 r
= 0; /* Success, we read the message. */
258 if (!client_context_test_priority(context
, priority
))
262 if (s
->forward_to_syslog
)
263 server_forward_syslog(s
, syslog_fixup_facility(priority
), identifier
, message
, ucred
, tv
);
265 if (s
->forward_to_kmsg
)
266 server_forward_kmsg(s
, priority
, identifier
, message
, ucred
);
268 if (s
->forward_to_console
)
269 server_forward_console(s
, priority
, identifier
, message
, ucred
);
271 if (s
->forward_to_wall
)
272 server_forward_wall(s
, priority
, identifier
, message
, ucred
);
275 server_dispatch_message(s
, iovec
, n
, m
, context
, tv
, priority
, object_pid
);
278 for (j
= 0; j
< n
; j
++) {
282 if (iovec
[j
].iov_base
< buffer
||
283 (const char*) iovec
[j
].iov_base
>= p
+ *remaining
)
284 free(iovec
[j
].iov_base
);
294 void server_process_native_message(
296 const char *buffer
, size_t buffer_size
,
297 const struct ucred
*ucred
,
298 const struct timeval
*tv
,
299 const char *label
, size_t label_len
) {
301 size_t remaining
= buffer_size
;
302 ClientContext
*context
= NULL
;
306 assert(buffer
|| buffer_size
== 0);
308 if (ucred
&& pid_is_valid(ucred
->pid
)) {
309 r
= client_context_get(s
, ucred
->pid
, ucred
, label
, label_len
, NULL
, &context
);
311 log_warning_errno(r
, "Failed to retrieve credentials for PID " PID_FMT
", ignoring: %m", ucred
->pid
);
315 r
= server_process_entry(s
,
316 (const uint8_t*) buffer
+ (buffer_size
- remaining
), &remaining
,
317 context
, ucred
, tv
, label
, label_len
);
321 void server_process_native_file(
324 const struct ucred
*ucred
,
325 const struct timeval
*tv
,
326 const char *label
, size_t label_len
) {
332 /* Data is in the passed fd, probably it didn't fit in a datagram. */
337 /* If it's a memfd, check if it is sealed. If so, we can just
338 * mmap it and use it, and do not need to copy the data out. */
339 sealed
= memfd_get_sealed(fd
) > 0;
341 if (!sealed
&& (!ucred
|| ucred
->uid
!= 0)) {
342 _cleanup_free_
char *k
= NULL
;
345 /* If this is not a sealed memfd, and the peer is unknown or
346 * unprivileged, then verify the path. */
348 r
= fd_get_path(fd
, &k
);
350 log_error_errno(r
, "readlink(/proc/self/fd/%i) failed: %m", fd
);
354 e
= PATH_STARTSWITH_SET(k
, "/dev/shm/", "/tmp/", "/var/tmp/");
356 log_error("Received file outside of allowed directories. Refusing.");
360 if (!filename_is_valid(e
)) {
361 log_error("Received file in subdirectory of allowed directories. Refusing.");
366 if (fstat(fd
, &st
) < 0) {
367 log_error_errno(errno
, "Failed to stat passed file, ignoring: %m");
371 if (!S_ISREG(st
.st_mode
)) {
372 log_error("File passed is not regular. Ignoring.");
379 /* When !sealed, set a lower memory limit. We have to read the file,
380 * effectively doubling memory use. */
381 if (st
.st_size
> ENTRY_SIZE_MAX
/ (sealed
? 1 : 2)) {
382 log_error("File passed too large (%"PRIu64
" bytes). Ignoring.", (uint64_t) st
.st_size
);
390 /* The file is sealed, we can just map it and use it. */
392 ps
= PAGE_ALIGN(st
.st_size
);
393 p
= mmap(NULL
, ps
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
394 if (p
== MAP_FAILED
) {
395 log_error_errno(errno
, "Failed to map memfd, ignoring: %m");
399 server_process_native_message(s
, p
, st
.st_size
, ucred
, tv
, label
, label_len
);
400 assert_se(munmap(p
, ps
) >= 0);
402 _cleanup_free_
void *p
= NULL
;
406 if (fstatvfs(fd
, &vfs
) < 0) {
407 log_error_errno(errno
, "Failed to stat file system of passed file, not processing it: %m");
411 /* Refuse operating on file systems that have
412 * mandatory locking enabled, see:
414 * https://github.com/systemd/systemd/issues/1822
416 if (vfs
.f_flag
& ST_MANDLOCK
) {
417 log_error("Received file descriptor from file system with mandatory locking enabled, not processing it.");
421 /* Make the fd non-blocking. On regular files this has
422 * the effect of bypassing mandatory locking. Of
423 * course, this should normally not be necessary given
424 * the check above, but let's better be safe than
425 * sorry, after all NFS is pretty confusing regarding
426 * file system flags, and we better don't trust it,
428 r
= fd_nonblock(fd
, true);
430 log_error_errno(r
, "Failed to make fd non-blocking, not processing it: %m");
434 /* The file is not sealed, we can't map the file here, since
435 * clients might then truncate it and trigger a SIGBUS for
436 * us. So let's stupidly read it. */
438 p
= malloc(st
.st_size
);
444 n
= pread(fd
, p
, st
.st_size
, 0);
446 log_error_errno(errno
, "Failed to read file, ignoring: %m");
448 server_process_native_message(s
, p
, n
, ucred
, tv
, label
, label_len
);
452 int server_open_native_socket(Server
*s
) {
454 static const union sockaddr_union sa
= {
455 .un
.sun_family
= AF_UNIX
,
456 .un
.sun_path
= "/run/systemd/journal/socket",
462 if (s
->native_fd
< 0) {
463 s
->native_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
464 if (s
->native_fd
< 0)
465 return log_error_errno(errno
, "socket() failed: %m");
467 (void) sockaddr_un_unlink(&sa
.un
);
469 r
= bind(s
->native_fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
471 return log_error_errno(errno
, "bind(%s) failed: %m", sa
.un
.sun_path
);
473 (void) chmod(sa
.un
.sun_path
, 0666);
475 (void) fd_nonblock(s
->native_fd
, true);
477 r
= setsockopt_int(s
->native_fd
, SOL_SOCKET
, SO_PASSCRED
, true);
479 return log_error_errno(r
, "SO_PASSCRED failed: %m");
482 if (mac_selinux_use()) {
483 r
= setsockopt_int(s
->native_fd
, SOL_SOCKET
, SO_PASSSEC
, true);
485 log_warning_errno(r
, "SO_PASSSEC failed: %m");
489 r
= setsockopt_int(s
->native_fd
, SOL_SOCKET
, SO_TIMESTAMP
, true);
491 return log_error_errno(r
, "SO_TIMESTAMP failed: %m");
493 r
= sd_event_add_io(s
->event
, &s
->native_event_source
, s
->native_fd
, EPOLLIN
, server_process_datagram
, s
);
495 return log_error_errno(r
, "Failed to add native server fd to event loop: %m");
497 r
= sd_event_source_set_priority(s
->native_event_source
, SD_EVENT_PRIORITY_NORMAL
+5);
499 return log_error_errno(r
, "Failed to adjust native event source priority: %m");