]>
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 "journald-console.h"
32 #include "journald-kmsg.h"
33 #include "journald-native.h"
34 #include "journald-server.h"
35 #include "journald-syslog.h"
36 #include "journald-wall.h"
37 #include "memfd-util.h"
38 #include "parse-util.h"
39 #include "path-util.h"
40 #include "selinux-util.h"
41 #include "socket-util.h"
42 #include "string-util.h"
43 #include "unaligned.h"
45 bool valid_user_field(const char *p
, size_t l
, bool allow_protected
) {
48 /* We kinda enforce POSIX syntax recommendations for
49 environment variables here, but make a couple of additional
52 http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html */
54 /* No empty field names */
58 /* Don't allow names longer than 64 chars */
62 /* Variables starting with an underscore are protected */
63 if (!allow_protected
&& p
[0] == '_')
66 /* Don't allow digits as first character */
67 if (p
[0] >= '0' && p
[0] <= '9')
70 /* Only allow A-Z0-9 and '_' */
71 for (a
= p
; a
< p
+ l
; a
++)
72 if ((*a
< 'A' || *a
> 'Z') &&
73 (*a
< '0' || *a
> '9') &&
80 static bool allow_object_pid(const struct ucred
*ucred
) {
81 return ucred
&& ucred
->uid
== 0;
84 static void server_process_entry_meta(
85 const char *p
, size_t l
,
86 const struct ucred
*ucred
,
92 /* We need to determine the priority of this entry for the rate limiting logic */
95 startswith(p
, "PRIORITY=") &&
96 p
[9] >= '0' && p
[9] <= '9')
97 *priority
= (*priority
& LOG_FACMASK
) | (p
[9] - '0');
100 startswith(p
, "SYSLOG_FACILITY=") &&
101 p
[16] >= '0' && p
[16] <= '9')
102 *priority
= (*priority
& LOG_PRIMASK
) | ((p
[16] - '0') << 3);
105 startswith(p
, "SYSLOG_FACILITY=") &&
106 p
[16] >= '0' && p
[16] <= '9' &&
107 p
[17] >= '0' && p
[17] <= '9')
108 *priority
= (*priority
& LOG_PRIMASK
) | (((p
[16] - '0')*10 + (p
[17] - '0')) << 3);
111 startswith(p
, "SYSLOG_IDENTIFIER=")) {
114 t
= strndup(p
+ 18, l
- 18);
121 startswith(p
, "MESSAGE=")) {
124 t
= strndup(p
+ 8, l
- 8);
130 } else if (l
> strlen("OBJECT_PID=") &&
131 l
< strlen("OBJECT_PID=") + DECIMAL_STR_MAX(pid_t
) &&
132 startswith(p
, "OBJECT_PID=") &&
133 allow_object_pid(ucred
)) {
134 char buf
[DECIMAL_STR_MAX(pid_t
)];
135 memcpy(buf
, p
+ strlen("OBJECT_PID="), l
- strlen("OBJECT_PID="));
136 buf
[l
-strlen("OBJECT_PID=")] = '\0';
138 (void) parse_pid(buf
, object_pid
);
142 static int server_process_entry(
144 const void *buffer
, size_t *remaining
,
145 const struct ucred
*ucred
,
146 const struct timeval
*tv
,
147 const char *label
, size_t label_len
) {
149 /* Process a single entry from a native message.
150 * Returns 0 if nothing special happened and the message processing should continue,
151 * and a negative or positive value otherwise.
153 * Note that *remaining is altered on both success and failure. */
155 struct iovec
*iovec
= NULL
;
156 unsigned n
= 0, j
, tn
= (unsigned) -1;
158 size_t m
= 0, entry_size
= 0;
159 int priority
= LOG_INFO
;
160 char *identifier
= NULL
, *message
= NULL
;
161 pid_t object_pid
= 0;
166 while (*remaining
> 0) {
169 e
= memchr(p
, '\n', *remaining
);
172 /* Trailing noise, let's ignore it, and flush what we collected */
173 log_debug("Received message with trailing noise, ignoring.");
174 r
= 1; /* finish processing of the message */
179 /* Entry separator */
184 if (*p
== '.' || *p
== '#') {
185 /* Ignore control commands for now, and
187 *remaining
-= (e
- p
) + 1;
192 /* A property follows */
194 /* n existing properties, 1 new, +1 for _TRANSPORT */
195 if (!GREEDY_REALLOC(iovec
, m
, n
+ 2 + N_IOVEC_META_FIELDS
+ N_IOVEC_OBJECT_FIELDS
)) {
200 q
= memchr(p
, '=', e
- p
);
202 if (valid_user_field(p
, q
- p
, false)) {
207 /* If the field name starts with an
208 * underscore, skip the variable,
209 * since that indicates a trusted
211 iovec
[n
].iov_base
= (char*) p
;
212 iovec
[n
].iov_len
= l
;
216 server_process_entry_meta(p
, l
, ucred
,
223 *remaining
-= (e
- p
) + 1;
230 if (*remaining
< e
- p
+ 1 + sizeof(uint64_t) + 1) {
231 log_debug("Failed to parse message, ignoring.");
235 l
= unaligned_read_le64(e
+ 1);
237 if (l
> DATA_SIZE_MAX
) {
238 log_debug("Received binary data block of %"PRIu64
" bytes is too large, ignoring.", l
);
242 if ((uint64_t) *remaining
< e
- p
+ 1 + sizeof(uint64_t) + l
+ 1 ||
243 e
[1+sizeof(uint64_t)+l
] != '\n') {
244 log_debug("Failed to parse message, ignoring.");
248 k
= malloc((e
- p
) + 1 + l
);
256 memcpy(k
+ (e
- p
) + 1, e
+ 1 + sizeof(uint64_t), l
);
258 if (valid_user_field(p
, e
- p
, false)) {
259 iovec
[n
].iov_base
= k
;
260 iovec
[n
].iov_len
= (e
- p
) + 1 + l
;
261 entry_size
+= iovec
[n
].iov_len
;
264 server_process_entry_meta(k
, (e
- p
) + 1 + l
, ucred
,
272 *remaining
-= (e
- p
) + 1 + sizeof(uint64_t) + l
+ 1;
273 p
= e
+ 1 + sizeof(uint64_t) + l
+ 1;
283 IOVEC_SET_STRING(iovec
[tn
], "_TRANSPORT=journal");
284 entry_size
+= strlen("_TRANSPORT=journal");
286 if (entry_size
+ n
+ 1 > ENTRY_SIZE_MAX
) { /* data + separators + trailer */
287 log_debug("Entry is too big with %u properties and %zu bytes, ignoring.",
293 if (s
->forward_to_syslog
)
294 server_forward_syslog(s
, syslog_fixup_facility(priority
), identifier
, message
, ucred
, tv
);
296 if (s
->forward_to_kmsg
)
297 server_forward_kmsg(s
, priority
, identifier
, message
, ucred
);
299 if (s
->forward_to_console
)
300 server_forward_console(s
, priority
, identifier
, message
, ucred
);
302 if (s
->forward_to_wall
)
303 server_forward_wall(s
, priority
, identifier
, message
, ucred
);
306 server_dispatch_message(s
, iovec
, n
, m
, ucred
, tv
, label
, label_len
, NULL
, priority
, object_pid
);
309 for (j
= 0; j
< n
; j
++) {
313 if (iovec
[j
].iov_base
< buffer
||
314 (const char*) iovec
[j
].iov_base
>= p
+ *remaining
)
315 free(iovec
[j
].iov_base
);
325 void server_process_native_message(
327 const void *buffer
, size_t buffer_size
,
328 const struct ucred
*ucred
,
329 const struct timeval
*tv
,
330 const char *label
, size_t label_len
) {
333 size_t remaining
= buffer_size
;
336 assert(buffer
|| buffer_size
== 0);
339 r
= server_process_entry(s
,
340 (const uint8_t*) buffer
+ (buffer_size
- remaining
), &remaining
,
341 ucred
, tv
, label
, label_len
);
345 void server_process_native_file(
348 const struct ucred
*ucred
,
349 const struct timeval
*tv
,
350 const char *label
, size_t label_len
) {
356 /* Data is in the passed fd, since it didn't fit in a
362 /* If it's a memfd, check if it is sealed. If so, we can just
363 * use map it and use it, and do not need to copy the data
365 sealed
= memfd_get_sealed(fd
) > 0;
367 if (!sealed
&& (!ucred
|| ucred
->uid
!= 0)) {
368 _cleanup_free_
char *sl
= NULL
, *k
= NULL
;
371 /* If this is not a sealed memfd, and the peer is unknown or
372 * unprivileged, then verify the path. */
374 if (asprintf(&sl
, "/proc/self/fd/%i", fd
) < 0) {
379 r
= readlink_malloc(sl
, &k
);
381 log_error_errno(r
, "readlink(%s) failed: %m", sl
);
385 e
= path_startswith(k
, "/dev/shm/");
387 e
= path_startswith(k
, "/tmp/");
389 e
= path_startswith(k
, "/var/tmp/");
391 log_error("Received file outside of allowed directories. Refusing.");
395 if (!filename_is_valid(e
)) {
396 log_error("Received file in subdirectory of allowed directories. Refusing.");
401 if (fstat(fd
, &st
) < 0) {
402 log_error_errno(errno
, "Failed to stat passed file, ignoring: %m");
406 if (!S_ISREG(st
.st_mode
)) {
407 log_error("File passed is not regular. Ignoring.");
414 if (st
.st_size
> ENTRY_SIZE_MAX
) {
415 log_error("File passed too large. Ignoring.");
423 /* The file is sealed, we can just map it and use it. */
425 ps
= PAGE_ALIGN(st
.st_size
);
426 p
= mmap(NULL
, ps
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
427 if (p
== MAP_FAILED
) {
428 log_error_errno(errno
, "Failed to map memfd, ignoring: %m");
432 server_process_native_message(s
, p
, st
.st_size
, ucred
, tv
, label
, label_len
);
433 assert_se(munmap(p
, ps
) >= 0);
435 _cleanup_free_
void *p
= NULL
;
439 if (fstatvfs(fd
, &vfs
) < 0) {
440 log_error_errno(errno
, "Failed to stat file system of passed file, ignoring: %m");
444 /* Refuse operating on file systems that have
445 * mandatory locking enabled, see:
447 * https://github.com/systemd/systemd/issues/1822
449 if (vfs
.f_flag
& ST_MANDLOCK
) {
450 log_error("Received file descriptor from file system with mandatory locking enable, refusing.");
454 /* Make the fd non-blocking. On regular files this has
455 * the effect of bypassing mandatory locking. Of
456 * course, this should normally not be necessary given
457 * the check above, but let's better be safe than
458 * sorry, after all NFS is pretty confusing regarding
459 * file system flags, and we better don't trust it,
461 r
= fd_nonblock(fd
, true);
463 log_error_errno(r
, "Failed to make fd non-blocking, ignoring: %m");
467 /* The file is not sealed, we can't map the file here, since
468 * clients might then truncate it and trigger a SIGBUS for
469 * us. So let's stupidly read it */
471 p
= malloc(st
.st_size
);
477 n
= pread(fd
, p
, st
.st_size
, 0);
479 log_error_errno(errno
, "Failed to read file, ignoring: %m");
481 server_process_native_message(s
, p
, n
, ucred
, tv
, label
, label_len
);
485 int server_open_native_socket(Server
*s
) {
487 static const union sockaddr_union sa
= {
488 .un
.sun_family
= AF_UNIX
,
489 .un
.sun_path
= "/run/systemd/journal/socket",
491 static const int one
= 1;
496 if (s
->native_fd
< 0) {
497 s
->native_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
498 if (s
->native_fd
< 0)
499 return log_error_errno(errno
, "socket() failed: %m");
501 (void) unlink(sa
.un
.sun_path
);
503 r
= bind(s
->native_fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
505 return log_error_errno(errno
, "bind(%s) failed: %m", sa
.un
.sun_path
);
507 (void) chmod(sa
.un
.sun_path
, 0666);
509 fd_nonblock(s
->native_fd
, 1);
511 r
= setsockopt(s
->native_fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
));
513 return log_error_errno(errno
, "SO_PASSCRED failed: %m");
516 if (mac_selinux_use()) {
517 r
= setsockopt(s
->native_fd
, SOL_SOCKET
, SO_PASSSEC
, &one
, sizeof(one
));
519 log_warning_errno(errno
, "SO_PASSSEC failed: %m");
523 r
= setsockopt(s
->native_fd
, SOL_SOCKET
, SO_TIMESTAMP
, &one
, sizeof(one
));
525 return log_error_errno(errno
, "SO_TIMESTAMP failed: %m");
527 r
= sd_event_add_io(s
->event
, &s
->native_event_source
, s
->native_fd
, EPOLLIN
, server_process_datagram
, s
);
529 return log_error_errno(r
, "Failed to add native server fd to event loop: %m");
531 r
= sd_event_source_set_priority(s
->native_event_source
, SD_EVENT_PRIORITY_NORMAL
+5);
533 return log_error_errno(r
, "Failed to adjust native event source priority: %m");