]>
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"
28 #include "unaligned.h"
30 static bool allow_object_pid(const struct ucred
*ucred
) {
31 return ucred
&& ucred
->uid
== 0;
34 static void server_process_entry_meta(
35 const char *p
, size_t l
,
36 const struct ucred
*ucred
,
42 /* We need to determine the priority of this entry for the rate limiting logic */
45 startswith(p
, "PRIORITY=") &&
46 p
[9] >= '0' && p
[9] <= '9')
47 *priority
= (*priority
& LOG_FACMASK
) | (p
[9] - '0');
50 startswith(p
, "SYSLOG_FACILITY=") &&
51 p
[16] >= '0' && p
[16] <= '9')
52 *priority
= (*priority
& LOG_PRIMASK
) | ((p
[16] - '0') << 3);
55 startswith(p
, "SYSLOG_FACILITY=") &&
56 p
[16] >= '0' && p
[16] <= '9' &&
57 p
[17] >= '0' && p
[17] <= '9')
58 *priority
= (*priority
& LOG_PRIMASK
) | (((p
[16] - '0')*10 + (p
[17] - '0')) << 3);
61 startswith(p
, "SYSLOG_IDENTIFIER=")) {
64 t
= strndup(p
+ 18, l
- 18);
71 startswith(p
, "MESSAGE=")) {
74 t
= strndup(p
+ 8, l
- 8);
80 } else if (l
> STRLEN("OBJECT_PID=") &&
81 l
< STRLEN("OBJECT_PID=") + DECIMAL_STR_MAX(pid_t
) &&
82 startswith(p
, "OBJECT_PID=") &&
83 allow_object_pid(ucred
)) {
84 char buf
[DECIMAL_STR_MAX(pid_t
)];
85 memcpy(buf
, p
+ STRLEN("OBJECT_PID="),
86 l
- STRLEN("OBJECT_PID="));
87 buf
[l
-STRLEN("OBJECT_PID=")] = '\0';
89 (void) parse_pid(buf
, object_pid
);
93 static int server_process_entry(
95 const void *buffer
, size_t *remaining
,
96 ClientContext
*context
,
97 const struct ucred
*ucred
,
98 const struct timeval
*tv
,
99 const char *label
, size_t label_len
) {
101 /* Process a single entry from a native message. Returns 0 if nothing special happened and the message
102 * processing should continue, and a negative or positive value otherwise.
104 * Note that *remaining is altered on both success and failure. */
106 size_t n
= 0, j
, tn
= (size_t) -1, m
= 0, entry_size
= 0;
107 char *identifier
= NULL
, *message
= NULL
;
108 struct iovec
*iovec
= NULL
;
109 int priority
= LOG_INFO
;
110 pid_t object_pid
= 0;
116 while (*remaining
> 0) {
119 e
= memchr(p
, '\n', *remaining
);
122 /* Trailing noise, let's ignore it, and flush what we collected */
123 log_debug("Received message with trailing noise, ignoring.");
124 r
= 1; /* finish processing of the message */
129 /* Entry separator */
134 if (IN_SET(*p
, '.', '#')) {
135 /* Ignore control commands for now, and
137 *remaining
-= (e
- p
) + 1;
142 /* A property follows */
144 /* n existing properties, 1 new, +1 for _TRANSPORT */
145 if (!GREEDY_REALLOC(iovec
, m
,
147 N_IOVEC_META_FIELDS
+ N_IOVEC_OBJECT_FIELDS
+
148 client_context_extra_fields_n_iovec(context
))) {
153 q
= memchr(p
, '=', e
- p
);
155 if (journal_field_valid(p
, q
- p
, false)) {
160 /* If the field name starts with an underscore, skip the variable, since that indicates
162 iovec
[n
++] = IOVEC_MAKE((char*) p
, l
);
165 server_process_entry_meta(p
, l
, ucred
,
172 *remaining
-= (e
- p
) + 1;
179 if (*remaining
< e
- p
+ 1 + sizeof(uint64_t) + 1) {
180 log_debug("Failed to parse message, ignoring.");
184 l
= unaligned_read_le64(e
+ 1);
186 if (l
> DATA_SIZE_MAX
) {
187 log_debug("Received binary data block of %"PRIu64
" bytes is too large, ignoring.", l
);
191 if ((uint64_t) *remaining
< e
- p
+ 1 + sizeof(uint64_t) + l
+ 1 ||
192 e
[1+sizeof(uint64_t)+l
] != '\n') {
193 log_debug("Failed to parse message, ignoring.");
197 k
= malloc((e
- p
) + 1 + l
);
205 memcpy(k
+ (e
- p
) + 1, e
+ 1 + sizeof(uint64_t), l
);
207 if (journal_field_valid(p
, e
- p
, false)) {
208 iovec
[n
].iov_base
= k
;
209 iovec
[n
].iov_len
= (e
- p
) + 1 + l
;
210 entry_size
+= iovec
[n
].iov_len
;
213 server_process_entry_meta(k
, (e
- p
) + 1 + l
, ucred
,
221 *remaining
-= (e
- p
) + 1 + sizeof(uint64_t) + l
+ 1;
222 p
= e
+ 1 + sizeof(uint64_t) + l
+ 1;
231 if (!client_context_test_priority(context
, priority
)) {
237 iovec
[tn
] = IOVEC_MAKE_STRING("_TRANSPORT=journal");
238 entry_size
+= STRLEN("_TRANSPORT=journal");
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
);
246 if (s
->forward_to_syslog
)
247 server_forward_syslog(s
, syslog_fixup_facility(priority
), identifier
, message
, ucred
, tv
);
249 if (s
->forward_to_kmsg
)
250 server_forward_kmsg(s
, priority
, identifier
, message
, ucred
);
252 if (s
->forward_to_console
)
253 server_forward_console(s
, priority
, identifier
, message
, ucred
);
255 if (s
->forward_to_wall
)
256 server_forward_wall(s
, priority
, identifier
, message
, ucred
);
259 server_dispatch_message(s
, iovec
, n
, m
, context
, tv
, priority
, object_pid
);
262 for (j
= 0; j
< n
; j
++) {
266 if (iovec
[j
].iov_base
< buffer
||
267 (const char*) iovec
[j
].iov_base
>= p
+ *remaining
)
268 free(iovec
[j
].iov_base
);
278 void server_process_native_message(
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
) {
285 size_t remaining
= buffer_size
;
286 ClientContext
*context
= NULL
;
290 assert(buffer
|| buffer_size
== 0);
292 if (ucred
&& pid_is_valid(ucred
->pid
)) {
293 r
= client_context_get(s
, ucred
->pid
, ucred
, label
, label_len
, NULL
, &context
);
295 log_warning_errno(r
, "Failed to retrieve credentials for PID " PID_FMT
", ignoring: %m", ucred
->pid
);
299 r
= server_process_entry(s
,
300 (const uint8_t*) buffer
+ (buffer_size
- remaining
), &remaining
,
301 context
, ucred
, tv
, label
, label_len
);
305 void server_process_native_file(
308 const struct ucred
*ucred
,
309 const struct timeval
*tv
,
310 const char *label
, size_t label_len
) {
316 /* Data is in the passed fd, since it didn't fit in a
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
325 sealed
= memfd_get_sealed(fd
) > 0;
327 if (!sealed
&& (!ucred
|| ucred
->uid
!= 0)) {
328 _cleanup_free_
char *k
= NULL
;
331 /* If this is not a sealed memfd, and the peer is unknown or
332 * unprivileged, then verify the path. */
334 r
= fd_get_path(fd
, &k
);
336 log_error_errno(r
, "readlink(/proc/self/fd/%i) failed: %m", fd
);
340 e
= path_startswith(k
, "/dev/shm/");
342 e
= path_startswith(k
, "/tmp/");
344 e
= path_startswith(k
, "/var/tmp/");
346 log_error("Received file outside of allowed directories. Refusing.");
350 if (!filename_is_valid(e
)) {
351 log_error("Received file in subdirectory of allowed directories. Refusing.");
356 if (fstat(fd
, &st
) < 0) {
357 log_error_errno(errno
, "Failed to stat passed file, ignoring: %m");
361 if (!S_ISREG(st
.st_mode
)) {
362 log_error("File passed is not regular. Ignoring.");
369 if (st
.st_size
> ENTRY_SIZE_MAX
) {
370 log_error("File passed too large. Ignoring.");
378 /* The file is sealed, we can just map it and use it. */
380 ps
= PAGE_ALIGN(st
.st_size
);
381 p
= mmap(NULL
, ps
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
382 if (p
== MAP_FAILED
) {
383 log_error_errno(errno
, "Failed to map memfd, ignoring: %m");
387 server_process_native_message(s
, p
, st
.st_size
, ucred
, tv
, label
, label_len
);
388 assert_se(munmap(p
, ps
) >= 0);
390 _cleanup_free_
void *p
= NULL
;
394 if (fstatvfs(fd
, &vfs
) < 0) {
395 log_error_errno(errno
, "Failed to stat file system of passed file, ignoring: %m");
399 /* Refuse operating on file systems that have
400 * mandatory locking enabled, see:
402 * https://github.com/systemd/systemd/issues/1822
404 if (vfs
.f_flag
& ST_MANDLOCK
) {
405 log_error("Received file descriptor from file system with mandatory locking enabled, refusing.");
409 /* Make the fd non-blocking. On regular files this has
410 * the effect of bypassing mandatory locking. Of
411 * course, this should normally not be necessary given
412 * the check above, but let's better be safe than
413 * sorry, after all NFS is pretty confusing regarding
414 * file system flags, and we better don't trust it,
416 r
= fd_nonblock(fd
, true);
418 log_error_errno(r
, "Failed to make fd non-blocking, ignoring: %m");
422 /* The file is not sealed, we can't map the file here, since
423 * clients might then truncate it and trigger a SIGBUS for
424 * us. So let's stupidly read it */
426 p
= malloc(st
.st_size
);
432 n
= pread(fd
, p
, st
.st_size
, 0);
434 log_error_errno(errno
, "Failed to read file, ignoring: %m");
436 server_process_native_message(s
, p
, n
, ucred
, tv
, label
, label_len
);
440 int server_open_native_socket(Server
*s
) {
442 static const union sockaddr_union sa
= {
443 .un
.sun_family
= AF_UNIX
,
444 .un
.sun_path
= "/run/systemd/journal/socket",
450 if (s
->native_fd
< 0) {
451 s
->native_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
452 if (s
->native_fd
< 0)
453 return log_error_errno(errno
, "socket() failed: %m");
455 (void) sockaddr_un_unlink(&sa
.un
);
457 r
= bind(s
->native_fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
459 return log_error_errno(errno
, "bind(%s) failed: %m", sa
.un
.sun_path
);
461 (void) chmod(sa
.un
.sun_path
, 0666);
463 (void) fd_nonblock(s
->native_fd
, true);
465 r
= setsockopt_int(s
->native_fd
, SOL_SOCKET
, SO_PASSCRED
, true);
467 return log_error_errno(r
, "SO_PASSCRED failed: %m");
470 if (mac_selinux_use()) {
471 r
= setsockopt_int(s
->native_fd
, SOL_SOCKET
, SO_PASSSEC
, true);
473 log_warning_errno(r
, "SO_PASSSEC failed: %m");
477 r
= setsockopt_int(s
->native_fd
, SOL_SOCKET
, SO_TIMESTAMP
, true);
479 return log_error_errno(r
, "SO_TIMESTAMP failed: %m");
481 r
= sd_event_add_io(s
->event
, &s
->native_event_source
, s
->native_fd
, EPOLLIN
, server_process_datagram
, s
);
483 return log_error_errno(r
, "Failed to add native server fd to event loop: %m");
485 r
= sd_event_source_set_priority(s
->native_event_source
, SD_EVENT_PRIORITY_NORMAL
+5);
487 return log_error_errno(r
, "Failed to adjust native event source priority: %m");