]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald-native.c
2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/epoll.h>
23 #include <sys/statvfs.h>
26 #include "alloc-util.h"
30 #include "journal-importer.h"
31 #include "journal-util.h"
32 #include "journald-console.h"
33 #include "journald-kmsg.h"
34 #include "journald-native.h"
35 #include "journald-server.h"
36 #include "journald-syslog.h"
37 #include "journald-wall.h"
38 #include "memfd-util.h"
39 #include "parse-util.h"
40 #include "path-util.h"
41 #include "process-util.h"
42 #include "selinux-util.h"
43 #include "socket-util.h"
44 #include "string-util.h"
45 #include "unaligned.h"
47 static bool allow_object_pid(const struct ucred
*ucred
) {
48 return ucred
&& ucred
->uid
== 0;
51 static void server_process_entry_meta(
52 const char *p
, size_t l
,
53 const struct ucred
*ucred
,
59 /* We need to determine the priority of this entry for the rate limiting logic */
62 startswith(p
, "PRIORITY=") &&
63 p
[9] >= '0' && p
[9] <= '9')
64 *priority
= (*priority
& LOG_FACMASK
) | (p
[9] - '0');
67 startswith(p
, "SYSLOG_FACILITY=") &&
68 p
[16] >= '0' && p
[16] <= '9')
69 *priority
= (*priority
& LOG_PRIMASK
) | ((p
[16] - '0') << 3);
72 startswith(p
, "SYSLOG_FACILITY=") &&
73 p
[16] >= '0' && p
[16] <= '9' &&
74 p
[17] >= '0' && p
[17] <= '9')
75 *priority
= (*priority
& LOG_PRIMASK
) | (((p
[16] - '0')*10 + (p
[17] - '0')) << 3);
78 startswith(p
, "SYSLOG_IDENTIFIER=")) {
81 t
= strndup(p
+ 18, l
- 18);
88 startswith(p
, "MESSAGE=")) {
91 t
= strndup(p
+ 8, l
- 8);
97 } else if (l
> strlen("OBJECT_PID=") &&
98 l
< strlen("OBJECT_PID=") + DECIMAL_STR_MAX(pid_t
) &&
99 startswith(p
, "OBJECT_PID=") &&
100 allow_object_pid(ucred
)) {
101 char buf
[DECIMAL_STR_MAX(pid_t
)];
102 memcpy(buf
, p
+ strlen("OBJECT_PID="), l
- strlen("OBJECT_PID="));
103 buf
[l
-strlen("OBJECT_PID=")] = '\0';
105 (void) parse_pid(buf
, object_pid
);
109 static int server_process_entry(
111 const void *buffer
, size_t *remaining
,
112 ClientContext
*context
,
113 const struct ucred
*ucred
,
114 const struct timeval
*tv
,
115 const char *label
, size_t label_len
) {
117 /* Process a single entry from a native message. Returns 0 if nothing special happened and the message
118 * processing should continue, and a negative or positive value otherwise.
120 * Note that *remaining is altered on both success and failure. */
122 size_t n
= 0, j
, tn
= (size_t) -1, m
= 0, entry_size
= 0;
123 char *identifier
= NULL
, *message
= NULL
;
124 struct iovec
*iovec
= NULL
;
125 int priority
= LOG_INFO
;
126 pid_t object_pid
= 0;
132 while (*remaining
> 0) {
135 e
= memchr(p
, '\n', *remaining
);
138 /* Trailing noise, let's ignore it, and flush what we collected */
139 log_debug("Received message with trailing noise, ignoring.");
140 r
= 1; /* finish processing of the message */
145 /* Entry separator */
150 if (IN_SET(*p
, '.', '#')) {
151 /* Ignore control commands for now, and
153 *remaining
-= (e
- p
) + 1;
158 /* A property follows */
160 /* n existing properties, 1 new, +1 for _TRANSPORT */
161 if (!GREEDY_REALLOC(iovec
, m
,
163 N_IOVEC_META_FIELDS
+ N_IOVEC_OBJECT_FIELDS
+
164 client_context_extra_fields_n_iovec(context
))) {
169 q
= memchr(p
, '=', e
- p
);
171 if (journal_field_valid(p
, q
- p
, false)) {
176 /* If the field name starts with an underscore, skip the variable, since that indicates
178 iovec
[n
++] = IOVEC_MAKE((char*) p
, l
);
181 server_process_entry_meta(p
, l
, ucred
,
188 *remaining
-= (e
- p
) + 1;
195 if (*remaining
< e
- p
+ 1 + sizeof(uint64_t) + 1) {
196 log_debug("Failed to parse message, ignoring.");
200 l
= unaligned_read_le64(e
+ 1);
202 if (l
> DATA_SIZE_MAX
) {
203 log_debug("Received binary data block of %"PRIu64
" bytes is too large, ignoring.", l
);
207 if ((uint64_t) *remaining
< e
- p
+ 1 + sizeof(uint64_t) + l
+ 1 ||
208 e
[1+sizeof(uint64_t)+l
] != '\n') {
209 log_debug("Failed to parse message, ignoring.");
213 k
= malloc((e
- p
) + 1 + l
);
221 memcpy(k
+ (e
- p
) + 1, e
+ 1 + sizeof(uint64_t), l
);
223 if (journal_field_valid(p
, e
- p
, false)) {
224 iovec
[n
].iov_base
= k
;
225 iovec
[n
].iov_len
= (e
- p
) + 1 + l
;
226 entry_size
+= iovec
[n
].iov_len
;
229 server_process_entry_meta(k
, (e
- p
) + 1 + l
, ucred
,
237 *remaining
-= (e
- p
) + 1 + sizeof(uint64_t) + l
+ 1;
238 p
= e
+ 1 + sizeof(uint64_t) + l
+ 1;
247 if (!client_context_test_priority(context
, priority
)) {
253 iovec
[tn
] = IOVEC_MAKE_STRING("_TRANSPORT=journal");
254 entry_size
+= strlen("_TRANSPORT=journal");
256 if (entry_size
+ n
+ 1 > ENTRY_SIZE_MAX
) { /* data + separators + trailer */
257 log_debug("Entry is too big with %zu properties and %zu bytes, ignoring.", n
, entry_size
);
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 void *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, since it didn't fit in a
338 /* If it's a memfd, check if it is sealed. If so, we can just
339 * use map it and use it, and do not need to copy the data
341 sealed
= memfd_get_sealed(fd
) > 0;
343 if (!sealed
&& (!ucred
|| ucred
->uid
!= 0)) {
344 _cleanup_free_
char *sl
= NULL
, *k
= NULL
;
347 /* If this is not a sealed memfd, and the peer is unknown or
348 * unprivileged, then verify the path. */
350 if (asprintf(&sl
, "/proc/self/fd/%i", fd
) < 0) {
355 r
= readlink_malloc(sl
, &k
);
357 log_error_errno(r
, "readlink(%s) failed: %m", sl
);
361 e
= path_startswith(k
, "/dev/shm/");
363 e
= path_startswith(k
, "/tmp/");
365 e
= path_startswith(k
, "/var/tmp/");
367 log_error("Received file outside of allowed directories. Refusing.");
371 if (!filename_is_valid(e
)) {
372 log_error("Received file in subdirectory of allowed directories. Refusing.");
377 if (fstat(fd
, &st
) < 0) {
378 log_error_errno(errno
, "Failed to stat passed file, ignoring: %m");
382 if (!S_ISREG(st
.st_mode
)) {
383 log_error("File passed is not regular. Ignoring.");
390 if (st
.st_size
> ENTRY_SIZE_MAX
) {
391 log_error("File passed too large. Ignoring.");
399 /* The file is sealed, we can just map it and use it. */
401 ps
= PAGE_ALIGN(st
.st_size
);
402 p
= mmap(NULL
, ps
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
403 if (p
== MAP_FAILED
) {
404 log_error_errno(errno
, "Failed to map memfd, ignoring: %m");
408 server_process_native_message(s
, p
, st
.st_size
, ucred
, tv
, label
, label_len
);
409 assert_se(munmap(p
, ps
) >= 0);
411 _cleanup_free_
void *p
= NULL
;
415 if (fstatvfs(fd
, &vfs
) < 0) {
416 log_error_errno(errno
, "Failed to stat file system of passed file, ignoring: %m");
420 /* Refuse operating on file systems that have
421 * mandatory locking enabled, see:
423 * https://github.com/systemd/systemd/issues/1822
425 if (vfs
.f_flag
& ST_MANDLOCK
) {
426 log_error("Received file descriptor from file system with mandatory locking enable, refusing.");
430 /* Make the fd non-blocking. On regular files this has
431 * the effect of bypassing mandatory locking. Of
432 * course, this should normally not be necessary given
433 * the check above, but let's better be safe than
434 * sorry, after all NFS is pretty confusing regarding
435 * file system flags, and we better don't trust it,
437 r
= fd_nonblock(fd
, true);
439 log_error_errno(r
, "Failed to make fd non-blocking, ignoring: %m");
443 /* The file is not sealed, we can't map the file here, since
444 * clients might then truncate it and trigger a SIGBUS for
445 * us. So let's stupidly read it */
447 p
= malloc(st
.st_size
);
453 n
= pread(fd
, p
, st
.st_size
, 0);
455 log_error_errno(errno
, "Failed to read file, ignoring: %m");
457 server_process_native_message(s
, p
, n
, ucred
, tv
, label
, label_len
);
461 int server_open_native_socket(Server
*s
) {
463 static const union sockaddr_union sa
= {
464 .un
.sun_family
= AF_UNIX
,
465 .un
.sun_path
= "/run/systemd/journal/socket",
467 static const int one
= 1;
472 if (s
->native_fd
< 0) {
473 s
->native_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
474 if (s
->native_fd
< 0)
475 return log_error_errno(errno
, "socket() failed: %m");
477 (void) unlink(sa
.un
.sun_path
);
479 r
= bind(s
->native_fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
481 return log_error_errno(errno
, "bind(%s) failed: %m", sa
.un
.sun_path
);
483 (void) chmod(sa
.un
.sun_path
, 0666);
485 fd_nonblock(s
->native_fd
, 1);
487 r
= setsockopt(s
->native_fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
));
489 return log_error_errno(errno
, "SO_PASSCRED failed: %m");
492 if (mac_selinux_use()) {
493 r
= setsockopt(s
->native_fd
, SOL_SOCKET
, SO_PASSSEC
, &one
, sizeof(one
));
495 log_warning_errno(errno
, "SO_PASSSEC failed: %m");
499 r
= setsockopt(s
->native_fd
, SOL_SOCKET
, SO_TIMESTAMP
, &one
, sizeof(one
));
501 return log_error_errno(errno
, "SO_TIMESTAMP failed: %m");
503 r
= sd_event_add_io(s
->event
, &s
->native_event_source
, s
->native_fd
, EPOLLIN
, server_process_datagram
, s
);
505 return log_error_errno(r
, "Failed to add native server fd to event loop: %m");
507 r
= sd_event_source_set_priority(s
->native_event_source
, SD_EVENT_PRIORITY_NORMAL
+5);
509 return log_error_errno(r
, "Failed to adjust native event source priority: %m");