]>
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 "memory-util.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "selinux-util.h"
27 #include "socket-util.h"
28 #include "string-util.h"
30 #include "unaligned.h"
32 static bool allow_object_pid(const struct ucred
*ucred
) {
33 return ucred
&& ucred
->uid
== 0;
36 static void server_process_entry_meta(
37 const char *p
, size_t l
,
38 const struct ucred
*ucred
,
44 /* We need to determine the priority of this entry for the rate limiting logic */
47 startswith(p
, "PRIORITY=") &&
48 p
[9] >= '0' && p
[9] <= '9')
49 *priority
= (*priority
& LOG_FACMASK
) | (p
[9] - '0');
52 startswith(p
, "SYSLOG_FACILITY=") &&
53 p
[16] >= '0' && p
[16] <= '9')
54 *priority
= (*priority
& LOG_PRIMASK
) | ((p
[16] - '0') << 3);
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);
63 startswith(p
, "SYSLOG_IDENTIFIER=")) {
66 t
= memdup_suffix0(p
+ 18, l
- 18);
73 startswith(p
, "MESSAGE=")) {
76 t
= memdup_suffix0(p
+ 8, l
- 8);
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 server_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_t) -1, m
= 0, 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
, m
,
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 server_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 server_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 if (s
->forward_to_syslog
)
264 server_forward_syslog(s
, syslog_fixup_facility(priority
), identifier
, message
, ucred
, tv
);
266 if (s
->forward_to_kmsg
)
267 server_forward_kmsg(s
, priority
, identifier
, message
, ucred
);
269 if (s
->forward_to_console
)
270 server_forward_console(s
, priority
, identifier
, message
, ucred
);
272 if (s
->forward_to_wall
)
273 server_forward_wall(s
, priority
, identifier
, message
, ucred
);
276 server_dispatch_message(s
, iovec
, n
, m
, context
, tv
, priority
, object_pid
);
279 for (j
= 0; j
< n
; j
++) {
283 if (iovec
[j
].iov_base
< buffer
||
284 (const char*) iovec
[j
].iov_base
>= p
+ *remaining
)
285 free(iovec
[j
].iov_base
);
295 void server_process_native_message(
297 const char *buffer
, size_t buffer_size
,
298 const struct ucred
*ucred
,
299 const struct timeval
*tv
,
300 const char *label
, size_t label_len
) {
302 size_t remaining
= buffer_size
;
303 ClientContext
*context
= NULL
;
307 assert(buffer
|| buffer_size
== 0);
309 if (ucred
&& pid_is_valid(ucred
->pid
)) {
310 r
= client_context_get(s
, ucred
->pid
, ucred
, label
, label_len
, NULL
, &context
);
312 log_warning_errno(r
, "Failed to retrieve credentials for PID " PID_FMT
", ignoring: %m", ucred
->pid
);
316 r
= server_process_entry(s
,
317 (const uint8_t*) buffer
+ (buffer_size
- remaining
), &remaining
,
318 context
, ucred
, tv
, label
, label_len
);
322 void server_process_native_file(
325 const struct ucred
*ucred
,
326 const struct timeval
*tv
,
327 const char *label
, size_t label_len
) {
333 /* Data is in the passed fd, probably it didn't fit in a datagram. */
338 /* If it's a memfd, check if it is sealed. If so, we can just
339 * mmap it and use it, and do not need to copy the data out. */
340 sealed
= memfd_get_sealed(fd
) > 0;
342 if (!sealed
&& (!ucred
|| ucred
->uid
!= 0)) {
343 _cleanup_free_
char *k
= NULL
;
346 /* If this is not a sealed memfd, and the peer is unknown or
347 * unprivileged, then verify the path. */
349 r
= fd_get_path(fd
, &k
);
351 log_error_errno(r
, "readlink(/proc/self/fd/%i) failed: %m", fd
);
355 e
= PATH_STARTSWITH_SET(k
, "/dev/shm/", "/tmp/", "/var/tmp/");
357 log_error("Received file outside of allowed directories. Refusing.");
361 if (!filename_is_valid(e
)) {
362 log_error("Received file in subdirectory of allowed directories. Refusing.");
367 if (fstat(fd
, &st
) < 0) {
368 log_error_errno(errno
, "Failed to stat passed file, ignoring: %m");
372 if (!S_ISREG(st
.st_mode
)) {
373 log_error("File passed is not regular. Ignoring.");
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
);
391 /* The file is sealed, we can just map it and use it. */
393 ps
= PAGE_ALIGN(st
.st_size
);
394 p
= mmap(NULL
, ps
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
395 if (p
== MAP_FAILED
) {
396 log_error_errno(errno
, "Failed to map memfd, ignoring: %m");
400 server_process_native_message(s
, p
, st
.st_size
, ucred
, tv
, label
, label_len
);
401 assert_se(munmap(p
, ps
) >= 0);
403 _cleanup_free_
void *p
= NULL
;
407 if (fstatvfs(fd
, &vfs
) < 0) {
408 log_error_errno(errno
, "Failed to stat file system of passed file, not processing it: %m");
412 /* Refuse operating on file systems that have
413 * mandatory locking enabled, see:
415 * https://github.com/systemd/systemd/issues/1822
417 if (vfs
.f_flag
& ST_MANDLOCK
) {
418 log_error("Received file descriptor from file system with mandatory locking enabled, not processing it.");
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,
429 r
= fd_nonblock(fd
, true);
431 log_error_errno(r
, "Failed to make fd non-blocking, not processing it: %m");
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
437 * us. So let's stupidly read it. */
439 p
= malloc(st
.st_size
);
445 n
= pread(fd
, p
, st
.st_size
, 0);
447 log_error_errno(errno
, "Failed to read file, ignoring: %m");
449 server_process_native_message(s
, p
, n
, ucred
, tv
, label
, label_len
);
453 int server_open_native_socket(Server
*s
) {
455 static const union sockaddr_union sa
= {
456 .un
.sun_family
= AF_UNIX
,
457 .un
.sun_path
= "/run/systemd/journal/socket",
463 if (s
->native_fd
< 0) {
464 s
->native_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
465 if (s
->native_fd
< 0)
466 return log_error_errno(errno
, "socket() failed: %m");
468 (void) sockaddr_un_unlink(&sa
.un
);
470 r
= bind(s
->native_fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
472 return log_error_errno(errno
, "bind(%s) failed: %m", sa
.un
.sun_path
);
474 (void) chmod(sa
.un
.sun_path
, 0666);
476 (void) fd_nonblock(s
->native_fd
, true);
478 r
= setsockopt_int(s
->native_fd
, SOL_SOCKET
, SO_PASSCRED
, true);
480 return log_error_errno(r
, "SO_PASSCRED failed: %m");
483 if (mac_selinux_use()) {
484 r
= setsockopt_int(s
->native_fd
, SOL_SOCKET
, SO_PASSSEC
, true);
486 log_warning_errno(r
, "SO_PASSSEC failed: %m");
490 r
= setsockopt_int(s
->native_fd
, SOL_SOCKET
, SO_TIMESTAMP
, true);
492 return log_error_errno(r
, "SO_TIMESTAMP failed: %m");
494 r
= sd_event_add_io(s
->event
, &s
->native_event_source
, s
->native_fd
, EPOLLIN
, server_process_datagram
, s
);
496 return log_error_errno(r
, "Failed to add native server fd to event loop: %m");
498 r
= sd_event_source_set_priority(s
->native_event_source
, SD_EVENT_PRIORITY_NORMAL
+5);
500 return log_error_errno(r
, "Failed to adjust native event source priority: %m");