]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald-native.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <sys/statvfs.h>
8 #include "alloc-util.h"
9 #include "errno-util.h"
11 #include "format-util.h"
12 #include "iovec-util.h"
13 #include "journal-importer.h"
14 #include "journal-internal.h"
15 #include "journald-client.h"
16 #include "journald-console.h"
17 #include "journald-context.h"
18 #include "journald-kmsg.h"
19 #include "journald-manager.h"
20 #include "journald-native.h"
21 #include "journald-syslog.h"
22 #include "journald-wall.h"
24 #include "log-ratelimit.h"
25 #include "memfd-util.h"
26 #include "memory-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "selinux-util.h"
31 #include "socket-util.h"
32 #include "stat-util.h"
33 #include "string-util.h"
34 #include "unaligned.h"
36 static bool allow_object_pid(const struct ucred
*ucred
) {
37 return ucred
&& ucred
->uid
== 0;
40 static void manager_process_entry_meta(
41 const char *p
, size_t l
,
42 const struct ucred
*ucred
,
48 /* We need to determine the priority of this entry for the rate limiting logic */
51 startswith(p
, "PRIORITY=") &&
52 p
[9] >= '0' && p
[9] <= '9')
53 *priority
= (*priority
& LOG_FACMASK
) | (p
[9] - '0');
56 startswith(p
, "SYSLOG_FACILITY=") &&
57 p
[16] >= '0' && p
[16] <= '9')
58 *priority
= LOG_PRI(*priority
) | ((p
[16] - '0') << 3);
61 startswith(p
, "SYSLOG_FACILITY=") &&
62 p
[16] >= '0' && p
[16] <= '9' &&
63 p
[17] >= '0' && p
[17] <= '9')
64 *priority
= LOG_PRI(*priority
) | (((p
[16] - '0')*10 + (p
[17] - '0')) << 3);
67 startswith(p
, "SYSLOG_IDENTIFIER=")) {
70 t
= memdup_suffix0(p
+ 18, l
- 18);
72 free_and_replace(*identifier
, t
);
75 startswith(p
, "MESSAGE=")) {
78 t
= memdup_suffix0(p
+ 8, l
- 8);
80 free_and_replace(*message
, t
);
82 } else if (l
> STRLEN("OBJECT_PID=") &&
83 l
< STRLEN("OBJECT_PID=") + DECIMAL_STR_MAX(pid_t
) &&
84 startswith(p
, "OBJECT_PID=") &&
85 allow_object_pid(ucred
)) {
86 char buf
[DECIMAL_STR_MAX(pid_t
)];
87 memcpy(buf
, p
+ STRLEN("OBJECT_PID="),
88 l
- STRLEN("OBJECT_PID="));
89 buf
[l
-STRLEN("OBJECT_PID=")] = '\0';
91 (void) parse_pid(buf
, object_pid
);
95 static int manager_process_entry(
97 const void *buffer
, size_t *remaining
,
98 ClientContext
*context
,
99 const struct ucred
*ucred
,
100 const struct timeval
*tv
,
101 const char *label
, size_t label_len
) {
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.
106 * Note that *remaining is altered on both success and failure. */
108 size_t n
= 0, j
, tn
= SIZE_MAX
, entry_size
= 0;
109 char *identifier
= NULL
, *message
= NULL
;
110 struct iovec
*iovec
= NULL
;
111 int priority
= LOG_INFO
;
112 pid_t object_pid
= 0;
118 while (*remaining
> 0) {
121 e
= memchr(p
, '\n', *remaining
);
124 /* Trailing noise, let's ignore it, and flush what we collected */
125 log_debug("Received message with trailing noise, ignoring.");
126 break; /* finish processing of the message */
130 /* Entry separator */
135 if (IN_SET(*p
, '.', '#')) {
136 /* Ignore control commands for now, and comments too. */
137 *remaining
-= (e
- p
) + 1;
142 /* A property follows */
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.");
148 /* n existing properties, 1 new, +1 for _TRANSPORT */
149 if (!GREEDY_REALLOC(iovec
,
151 N_IOVEC_META_FIELDS
+ N_IOVEC_OBJECT_FIELDS
+
152 client_context_extra_fields_n_iovec(context
))) {
157 q
= memchr(p
, '=', e
- p
);
159 if (journal_field_valid(p
, q
- p
, false)) {
163 if (l
> DATA_SIZE_MAX
) {
164 log_debug("Received text block of %zu bytes is too large, ignoring entry.", l
);
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);
174 /* If the field name starts with an underscore, skip the variable, since that indicates
176 iovec
[n
++] = IOVEC_MAKE((char*) p
, l
);
179 manager_process_entry_meta(p
, l
, ucred
,
186 *remaining
-= (e
- p
) + 1;
193 if (*remaining
< e
- p
+ 1 + sizeof(uint64_t) + 1) {
194 log_debug("Failed to parse message, ignoring.");
198 l
= unaligned_read_le64(e
+ 1);
199 if (l
> DATA_SIZE_MAX
) {
200 log_debug("Received binary data block of %"PRIu64
" bytes is too large, ignoring entry.", l
);
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);
211 if ((uint64_t) *remaining
< e
- p
+ 1 + sizeof(uint64_t) + l
+ 1 ||
212 e
[1+sizeof(uint64_t)+l
] != '\n') {
213 log_debug("Failed to parse message, ignoring.");
225 memcpy(k
+ (e
- p
) + 1, e
+ 1 + sizeof(uint64_t), l
);
227 if (journal_field_valid(p
, e
- p
, false)) {
228 iovec
[n
] = IOVEC_MAKE(k
, (e
- p
) + 1 + l
);
229 entry_size
+= iovec
[n
].iov_len
;
232 manager_process_entry_meta(k
, (e
- p
) + 1 + l
, ucred
,
240 *remaining
-= (e
- p
) + 1 + sizeof(uint64_t) + l
+ 1;
241 p
= e
+ 1 + sizeof(uint64_t) + l
+ 1;
249 iovec
[tn
] = IOVEC_MAKE_STRING("_TRANSPORT=journal");
250 entry_size
+= STRLEN("_TRANSPORT=journal");
252 if (entry_size
+ n
+ 1 > ENTRY_SIZE_MAX
) { /* data + separators + trailer */
253 log_debug("Entry is too big with %zu properties and %zu bytes, ignoring.", n
, entry_size
);
257 r
= 0; /* Success, we read the message. */
259 if (!client_context_test_priority(context
, priority
))
263 /* Ensure message is not NULL, otherwise strlen(message) would crash. This check needs to
264 * be here until manager_process_entry() is able to process messages containing \0 characters,
265 * as we would have access to the actual size of message. */
266 r
= client_context_check_keep_log(context
, message
, strlen(message
));
270 if (m
->forward_to_syslog
)
271 manager_forward_syslog(m
, syslog_fixup_facility(priority
), identifier
, message
, ucred
, tv
);
273 if (m
->forward_to_kmsg
)
274 manager_forward_kmsg(m
, priority
, identifier
, message
, ucred
);
276 if (m
->forward_to_console
)
277 manager_forward_console(m
, priority
, identifier
, message
, ucred
);
279 if (m
->forward_to_wall
)
280 manager_forward_wall(m
, priority
, identifier
, message
, ucred
);
283 manager_dispatch_message(m
, iovec
, n
, MALLOC_ELEMENTSOF(iovec
), context
, tv
, priority
, object_pid
);
286 for (j
= 0; j
< n
; j
++) {
290 if (iovec
[j
].iov_base
< buffer
||
291 (const char*) iovec
[j
].iov_base
>= p
+ *remaining
)
292 free(iovec
[j
].iov_base
);
302 void manager_process_native_message(
304 const char *buffer
, size_t buffer_size
,
305 const struct ucred
*ucred
,
306 const struct timeval
*tv
,
307 const char *label
, size_t label_len
) {
309 size_t remaining
= buffer_size
;
310 ClientContext
*context
= NULL
;
314 assert(buffer
|| buffer_size
== 0);
316 if (ucred
&& pid_is_valid(ucred
->pid
)) {
317 r
= client_context_get(m
, ucred
->pid
, ucred
, label
, label_len
, NULL
, &context
);
319 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
320 "Failed to retrieve credentials for PID " PID_FMT
", ignoring: %m",
325 r
= manager_process_entry(m
,
326 (const uint8_t*) buffer
+ (buffer_size
- remaining
), &remaining
,
327 context
, ucred
, tv
, label
, label_len
);
331 int manager_process_native_file(
334 const struct ucred
*ucred
,
335 const struct timeval
*tv
,
336 const char *label
, size_t label_len
) {
342 /* Data is in the passed fd, probably it didn't fit in a datagram. */
347 if (fstat(fd
, &st
) < 0)
348 return log_ratelimit_error_errno(errno
, JOURNAL_LOG_RATELIMIT
,
349 "Failed to stat passed file: %m");
351 r
= stat_verify_regular(&st
);
353 return log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
,
354 "File passed is not regular, ignoring message: %m");
359 r
= fd_verify_safe_flags(fd
);
361 return log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
,
362 "Unexpected flags of passed memory fd, ignoring message.");
364 return log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
,
365 "Failed to get flags of passed file: %m");
367 /* If it's a memfd, check if it is sealed. If so, we can just mmap it and use it, and do not need to
368 * copy the data out. */
369 sealed
= memfd_get_sealed(fd
) > 0;
371 if (!sealed
&& (!ucred
|| ucred
->uid
!= 0)) {
372 _cleanup_free_
char *k
= NULL
;
375 /* If this is not a sealed memfd, and the peer is unknown or unprivileged, then verify the
378 r
= fd_get_path(fd
, &k
);
380 return log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
,
381 "Failed to get path of passed fd: %m");
383 e
= PATH_STARTSWITH_SET(k
, "/dev/shm/", "/tmp/", "/var/tmp/");
385 return log_ratelimit_error_errno(SYNTHETIC_ERRNO(EPERM
), JOURNAL_LOG_RATELIMIT
,
386 "Received file outside of allowed directories, refusing.");
388 if (!filename_is_valid(e
))
389 return log_ratelimit_error_errno(SYNTHETIC_ERRNO(EPERM
), JOURNAL_LOG_RATELIMIT
,
390 "Received file in subdirectory of allowed directories, refusing.");
393 /* When !sealed, set a lower memory limit. We have to read the file, effectively doubling memory
395 if (st
.st_size
> ENTRY_SIZE_MAX
/ (sealed
? 1 : 2))
396 return log_ratelimit_error_errno(SYNTHETIC_ERRNO(EFBIG
), JOURNAL_LOG_RATELIMIT
,
397 "File passed too large (%"PRIu64
" bytes), refusing.",
398 (uint64_t) st
.st_size
);
404 /* The file is sealed, we can just map it and use it. */
406 ps
= PAGE_ALIGN(st
.st_size
);
407 assert(ps
< SIZE_MAX
);
408 p
= mmap(NULL
, ps
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
410 return log_ratelimit_error_errno(errno
, JOURNAL_LOG_RATELIMIT
,
411 "Failed to map memfd: %m");
413 manager_process_native_message(m
, p
, st
.st_size
, ucred
, tv
, label
, label_len
);
414 assert_se(munmap(p
, ps
) >= 0);
419 _cleanup_free_
void *p
= NULL
;
423 if (fstatvfs(fd
, &vfs
) < 0)
424 return log_ratelimit_error_errno(errno
, JOURNAL_LOG_RATELIMIT
,
425 "Failed to stat file system of passed file: %m");
427 /* Refuse operating on file systems that have mandatory locking enabled.
428 * See also: https://github.com/systemd/systemd/issues/1822 */
429 if (FLAGS_SET(vfs
.f_flag
, ST_MANDLOCK
))
430 return log_ratelimit_error_errno(SYNTHETIC_ERRNO(EPERM
), JOURNAL_LOG_RATELIMIT
,
431 "Received file descriptor from file system with mandatory locking enabled, not processing it.");
433 /* Make the fd non-blocking. On regular files this has the effect of bypassing mandatory
434 * locking. Of course, this should normally not be necessary given the check above, but let's
435 * better be safe than sorry, after all NFS is pretty confusing regarding file system flags,
436 * and we better don't trust it, and so is SMB. */
437 r
= fd_nonblock(fd
, true);
439 return log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
,
440 "Failed to make fd non-blocking: %m");
442 /* The file is not sealed, we can't map the file here, since clients might then truncate it
443 * and trigger a SIGBUS for us. So let's stupidly read it. */
445 p
= malloc(st
.st_size
);
449 n
= pread(fd
, p
, st
.st_size
, 0);
451 return log_ratelimit_error_errno(errno
, JOURNAL_LOG_RATELIMIT
,
452 "Failed to read file: %m");
454 manager_process_native_message(m
, p
, n
, ucred
, tv
, label
, label_len
);
459 int manager_open_native_socket(Manager
*m
, const char *native_socket
) {
463 assert(native_socket
);
465 if (m
->native_fd
< 0) {
466 union sockaddr_union sa
;
469 r
= sockaddr_un_set_path(&sa
.un
, native_socket
);
471 return log_error_errno(r
, "Unable to use namespace path %s for AF_UNIX socket: %m", native_socket
);
474 m
->native_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
475 if (m
->native_fd
< 0)
476 return log_error_errno(errno
, "socket() failed: %m");
478 (void) sockaddr_un_unlink(&sa
.un
);
480 r
= bind(m
->native_fd
, &sa
.sa
, sa_len
);
482 return log_error_errno(errno
, "bind(%s) failed: %m", sa
.un
.sun_path
);
484 (void) chmod(sa
.un
.sun_path
, 0666);
486 (void) fd_nonblock(m
->native_fd
, true);
488 r
= setsockopt_int(m
->native_fd
, SOL_SOCKET
, SO_PASSCRED
, true);
490 return log_error_errno(r
, "SO_PASSCRED failed: %m");
492 if (mac_selinux_use()) {
493 r
= setsockopt_int(m
->native_fd
, SOL_SOCKET
, SO_PASSSEC
, true);
495 log_full_errno(ERRNO_IS_NEG_NOT_SUPPORTED(r
) ? LOG_DEBUG
: LOG_WARNING
, r
, "SO_PASSSEC failed, ignoring: %m");
498 r
= setsockopt_int(m
->native_fd
, SOL_SOCKET
, SO_TIMESTAMP
, true);
500 return log_error_errno(r
, "SO_TIMESTAMP failed: %m");
502 r
= sd_event_add_io(m
->event
, &m
->native_event_source
, m
->native_fd
, EPOLLIN
, manager_process_datagram
, m
);
504 return log_error_errno(r
, "Failed to add native manager fd to event loop: %m");
506 r
= sd_event_source_set_priority(m
->native_event_source
, SD_EVENT_PRIORITY_NORMAL
+5);
508 return log_error_errno(r
, "Failed to adjust native event source priority: %m");