]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journald-native.c
nss-resolve: drop the internal fallback to libnss_dns (#5945)
[thirdparty/systemd.git] / src / journal / journald-native.c
CommitLineData
0153028a
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2011 Lennart Poettering
5
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.
10
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.
15
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/>.
18***/
19
4871690d 20#include <stddef.h>
0153028a 21#include <sys/epoll.h>
c79e98ea 22#include <sys/mman.h>
1e603a48 23#include <sys/statvfs.h>
07630cea 24#include <unistd.h>
0153028a 25
b5efdb8a 26#include "alloc-util.h"
3ffd4af2 27#include "fd-util.h"
f4f15635 28#include "fs-util.h"
afc5dbf3 29#include "io-util.h"
b18453ed 30#include "journal-importer.h"
0153028a 31#include "journald-console.h"
07630cea 32#include "journald-kmsg.h"
3ffd4af2 33#include "journald-native.h"
07630cea 34#include "journald-server.h"
0153028a 35#include "journald-syslog.h"
40b71e89 36#include "journald-wall.h"
a09abc4a 37#include "memfd-util.h"
6bedfcbb 38#include "parse-util.h"
07630cea
LP
39#include "path-util.h"
40#include "selinux-util.h"
41#include "socket-util.h"
42#include "string-util.h"
0153028a 43
d18d46ec 44bool valid_user_field(const char *p, size_t l, bool allow_protected) {
0153028a
LP
45 const char *a;
46
47 /* We kinda enforce POSIX syntax recommendations for
48 environment variables here, but make a couple of additional
49 requirements.
50
51 http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html */
52
53 /* No empty field names */
54 if (l <= 0)
55 return false;
56
57 /* Don't allow names longer than 64 chars */
58 if (l > 64)
59 return false;
60
61 /* Variables starting with an underscore are protected */
d18d46ec 62 if (!allow_protected && p[0] == '_')
0153028a
LP
63 return false;
64
65 /* Don't allow digits as first character */
66 if (p[0] >= '0' && p[0] <= '9')
67 return false;
68
69 /* Only allow A-Z0-9 and '_' */
70 for (a = p; a < p + l; a++)
d18d46ec
ZJS
71 if ((*a < 'A' || *a > 'Z') &&
72 (*a < '0' || *a > '9') &&
73 *a != '_')
0153028a
LP
74 return false;
75
76 return true;
77}
78
3b3154df 79static bool allow_object_pid(const struct ucred *ucred) {
968f3196
ZJS
80 return ucred && ucred->uid == 0;
81}
82
0153028a
LP
83void server_process_native_message(
84 Server *s,
85 const void *buffer, size_t buffer_size,
3b3154df
LP
86 const struct ucred *ucred,
87 const struct timeval *tv,
0153028a
LP
88 const char *label, size_t label_len) {
89
90 struct iovec *iovec = NULL;
968f3196 91 unsigned n = 0, j, tn = (unsigned) -1;
0153028a 92 const char *p;
874bc134 93 size_t remaining, m = 0, entry_size = 0;
0153028a
LP
94 int priority = LOG_INFO;
95 char *identifier = NULL, *message = NULL;
968f3196 96 pid_t object_pid = 0;
0153028a
LP
97
98 assert(s);
99 assert(buffer || buffer_size == 0);
100
101 p = buffer;
102 remaining = buffer_size;
103
104 while (remaining > 0) {
105 const char *e, *q;
106
107 e = memchr(p, '\n', remaining);
108
109 if (!e) {
110 /* Trailing noise, let's ignore it, and flush what we collected */
111 log_debug("Received message with trailing noise, ignoring.");
112 break;
113 }
114
115 if (e == p) {
116 /* Entry separator */
874bc134
ZJS
117
118 if (entry_size + n + 1 > ENTRY_SIZE_MAX) { /* data + separators + trailer */
8c0b803b 119 log_debug("Entry is too big with %u properties and %zu bytes, ignoring.", n, entry_size);
874bc134
ZJS
120 continue;
121 }
122
968f3196 123 server_dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, NULL, priority, object_pid);
0153028a
LP
124 n = 0;
125 priority = LOG_INFO;
874bc134 126 entry_size = 0;
0153028a
LP
127
128 p++;
129 remaining--;
130 continue;
131 }
132
133 if (*p == '.' || *p == '#') {
134 /* Ignore control commands for now, and
135 * comments too. */
136 remaining -= (e - p) + 1;
137 p = e + 1;
138 continue;
139 }
140
141 /* A property follows */
142
12a717f8 143 /* n existing properties, 1 new, +1 for _TRANSPORT */
92ee6447 144 if (!GREEDY_REALLOC(iovec, m, n + 2 + N_IOVEC_META_FIELDS + N_IOVEC_OBJECT_FIELDS)) {
968f3196
ZJS
145 log_oom();
146 break;
0153028a
LP
147 }
148
149 q = memchr(p, '=', e - p);
150 if (q) {
d18d46ec 151 if (valid_user_field(p, q - p, false)) {
0153028a
LP
152 size_t l;
153
154 l = e - p;
155
156 /* If the field name starts with an
157 * underscore, skip the variable,
158 * since that indidates a trusted
159 * field */
160 iovec[n].iov_base = (char*) p;
161 iovec[n].iov_len = l;
874bc134 162 entry_size += iovec[n].iov_len;
a174f94d 163 n++;
0153028a
LP
164
165 /* We need to determine the priority
166 * of this entry for the rate limiting
167 * logic */
168 if (l == 10 &&
2a0e0692 169 startswith(p, "PRIORITY=") &&
0153028a
LP
170 p[9] >= '0' && p[9] <= '9')
171 priority = (priority & LOG_FACMASK) | (p[9] - '0');
172
173 else if (l == 17 &&
2a0e0692 174 startswith(p, "SYSLOG_FACILITY=") &&
0153028a
LP
175 p[16] >= '0' && p[16] <= '9')
176 priority = (priority & LOG_PRIMASK) | ((p[16] - '0') << 3);
177
178 else if (l == 18 &&
2a0e0692 179 startswith(p, "SYSLOG_FACILITY=") &&
0153028a
LP
180 p[16] >= '0' && p[16] <= '9' &&
181 p[17] >= '0' && p[17] <= '9')
182 priority = (priority & LOG_PRIMASK) | (((p[16] - '0')*10 + (p[17] - '0')) << 3);
183
184 else if (l >= 19 &&
2a0e0692 185 startswith(p, "SYSLOG_IDENTIFIER=")) {
0153028a
LP
186 char *t;
187
188 t = strndup(p + 18, l - 18);
189 if (t) {
190 free(identifier);
191 identifier = t;
192 }
1f6b4113 193
0153028a 194 } else if (l >= 8 &&
2a0e0692 195 startswith(p, "MESSAGE=")) {
0153028a
LP
196 char *t;
197
198 t = strndup(p + 8, l - 8);
199 if (t) {
200 free(message);
201 message = t;
202 }
1f6b4113 203
968f3196
ZJS
204 } else if (l > strlen("OBJECT_PID=") &&
205 l < strlen("OBJECT_PID=") + DECIMAL_STR_MAX(pid_t) &&
2a0e0692 206 startswith(p, "OBJECT_PID=") &&
968f3196
ZJS
207 allow_object_pid(ucred)) {
208 char buf[DECIMAL_STR_MAX(pid_t)];
209 memcpy(buf, p + strlen("OBJECT_PID="), l - strlen("OBJECT_PID="));
daf535a3 210 buf[l-strlen("OBJECT_PID=")] = '\0';
968f3196
ZJS
211
212 /* ignore error */
213 parse_pid(buf, &object_pid);
0153028a
LP
214 }
215 }
216
217 remaining -= (e - p) + 1;
218 p = e + 1;
219 continue;
220 } else {
221 le64_t l_le;
222 uint64_t l;
223 char *k;
224
225 if (remaining < e - p + 1 + sizeof(uint64_t) + 1) {
226 log_debug("Failed to parse message, ignoring.");
227 break;
228 }
229
230 memcpy(&l_le, e + 1, sizeof(uint64_t));
231 l = le64toh(l_le);
232
505b6a61 233 if (l > DATA_SIZE_MAX) {
fa1c4b51 234 log_debug("Received binary data block of %"PRIu64" bytes is too large, ignoring.", l);
505b6a61
LP
235 break;
236 }
237
238 if ((uint64_t) remaining < e - p + 1 + sizeof(uint64_t) + l + 1 ||
0153028a
LP
239 e[1+sizeof(uint64_t)+l] != '\n') {
240 log_debug("Failed to parse message, ignoring.");
241 break;
242 }
243
244 k = malloc((e - p) + 1 + l);
245 if (!k) {
246 log_oom();
247 break;
248 }
249
250 memcpy(k, p, e - p);
251 k[e - p] = '=';
252 memcpy(k + (e - p) + 1, e + 1 + sizeof(uint64_t), l);
253
d18d46ec 254 if (valid_user_field(p, e - p, false)) {
0153028a
LP
255 iovec[n].iov_base = k;
256 iovec[n].iov_len = (e - p) + 1 + l;
874bc134 257 entry_size += iovec[n].iov_len;
a174f94d 258 n++;
0153028a
LP
259 } else
260 free(k);
261
262 remaining -= (e - p) + 1 + sizeof(uint64_t) + l + 1;
263 p = e + 1 + sizeof(uint64_t) + l + 1;
264 }
265 }
266
267 if (n <= 0)
268 goto finish;
269
270 tn = n++;
271 IOVEC_SET_STRING(iovec[tn], "_TRANSPORT=journal");
874bc134
ZJS
272 entry_size += strlen("_TRANSPORT=journal");
273
274 if (entry_size + n + 1 > ENTRY_SIZE_MAX) { /* data + separators + trailer */
275 log_debug("Entry is too big with %u properties and %zu bytes, ignoring.",
276 n, entry_size);
277 goto finish;
278 }
0153028a
LP
279
280 if (message) {
281 if (s->forward_to_syslog)
b6a20306 282 server_forward_syslog(s, syslog_fixup_facility(priority), identifier, message, ucred, tv);
0153028a
LP
283
284 if (s->forward_to_kmsg)
285 server_forward_kmsg(s, priority, identifier, message, ucred);
286
287 if (s->forward_to_console)
288 server_forward_console(s, priority, identifier, message, ucred);
40b71e89
ST
289
290 if (s->forward_to_wall)
291 server_forward_wall(s, priority, identifier, message, ucred);
0153028a
LP
292 }
293
968f3196 294 server_dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, NULL, priority, object_pid);
0153028a
LP
295
296finish:
297 for (j = 0; j < n; j++) {
298 if (j == tn)
299 continue;
300
301 if (iovec[j].iov_base < buffer ||
302 (const uint8_t*) iovec[j].iov_base >= (const uint8_t*) buffer + buffer_size)
303 free(iovec[j].iov_base);
304 }
305
306 free(iovec);
307 free(identifier);
308 free(message);
309}
310
311void server_process_native_file(
312 Server *s,
313 int fd,
3b3154df
LP
314 const struct ucred *ucred,
315 const struct timeval *tv,
0153028a
LP
316 const char *label, size_t label_len) {
317
318 struct stat st;
c79e98ea 319 bool sealed;
1dfa7e79 320 int r;
0153028a 321
c79e98ea
LP
322 /* Data is in the passed fd, since it didn't fit in a
323 * datagram. */
324
0153028a
LP
325 assert(s);
326 assert(fd >= 0);
327
c79e98ea
LP
328 /* If it's a memfd, check if it is sealed. If so, we can just
329 * use map it and use it, and do not need to copy the data
330 * out. */
73843b52 331 sealed = memfd_get_sealed(fd) > 0;
c79e98ea
LP
332
333 if (!sealed && (!ucred || ucred->uid != 0)) {
1dfa7e79
LP
334 _cleanup_free_ char *sl = NULL, *k = NULL;
335 const char *e;
336
c79e98ea
LP
337 /* If this is not a sealed memfd, and the peer is unknown or
338 * unprivileged, then verify the path. */
339
1dfa7e79
LP
340 if (asprintf(&sl, "/proc/self/fd/%i", fd) < 0) {
341 log_oom();
342 return;
343 }
344
345 r = readlink_malloc(sl, &k);
346 if (r < 0) {
709f6e46 347 log_error_errno(r, "readlink(%s) failed: %m", sl);
1dfa7e79
LP
348 return;
349 }
350
351 e = path_startswith(k, "/dev/shm/");
352 if (!e)
353 e = path_startswith(k, "/tmp/");
354 if (!e)
355 e = path_startswith(k, "/var/tmp/");
356 if (!e) {
357 log_error("Received file outside of allowed directories. Refusing.");
358 return;
359 }
360
ae6c3cc0 361 if (!filename_is_valid(e)) {
1dfa7e79
LP
362 log_error("Received file in subdirectory of allowed directories. Refusing.");
363 return;
364 }
365 }
366
0153028a 367 if (fstat(fd, &st) < 0) {
56f64d95 368 log_error_errno(errno, "Failed to stat passed file, ignoring: %m");
0153028a
LP
369 return;
370 }
371
372 if (!S_ISREG(st.st_mode)) {
373 log_error("File passed is not regular. Ignoring.");
374 return;
375 }
376
377 if (st.st_size <= 0)
378 return;
379
380 if (st.st_size > ENTRY_SIZE_MAX) {
381 log_error("File passed too large. Ignoring.");
382 return;
383 }
384
c79e98ea
LP
385 if (sealed) {
386 void *p;
387 size_t ps;
388
389 /* The file is sealed, we can just map it and use it. */
0153028a 390
c79e98ea
LP
391 ps = PAGE_ALIGN(st.st_size);
392 p = mmap(NULL, ps, PROT_READ, MAP_PRIVATE, fd, 0);
393 if (p == MAP_FAILED) {
56f64d95 394 log_error_errno(errno, "Failed to map memfd, ignoring: %m");
c79e98ea
LP
395 return;
396 }
397
398 server_process_native_message(s, p, st.st_size, ucred, tv, label, label_len);
399 assert_se(munmap(p, ps) >= 0);
400 } else {
401 _cleanup_free_ void *p = NULL;
1e603a48 402 struct statvfs vfs;
c79e98ea
LP
403 ssize_t n;
404
1e603a48
LP
405 if (fstatvfs(fd, &vfs) < 0) {
406 log_error_errno(errno, "Failed to stat file system of passed file, ignoring: %m");
407 return;
408 }
409
410 /* Refuse operating on file systems that have
411 * mandatory locking enabled, see:
412 *
413 * https://github.com/systemd/systemd/issues/1822
414 */
415 if (vfs.f_flag & ST_MANDLOCK) {
416 log_error("Received file descriptor from file system with mandatory locking enable, refusing.");
417 return;
418 }
419
420 /* Make the fd non-blocking. On regular files this has
421 * the effect of bypassing mandatory locking. Of
422 * course, this should normally not be necessary given
423 * the check above, but let's better be safe than
424 * sorry, after all NFS is pretty confusing regarding
425 * file system flags, and we better don't trust it,
426 * and so is SMB. */
427 r = fd_nonblock(fd, true);
428 if (r < 0) {
429 log_error_errno(r, "Failed to make fd non-blocking, ignoring: %m");
430 return;
431 }
432
c79e98ea
LP
433 /* The file is not sealed, we can't map the file here, since
434 * clients might then truncate it and trigger a SIGBUS for
435 * us. So let's stupidly read it */
436
437 p = malloc(st.st_size);
438 if (!p) {
439 log_oom();
440 return;
441 }
442
443 n = pread(fd, p, st.st_size, 0);
444 if (n < 0)
c3753458 445 log_error_errno(errno, "Failed to read file, ignoring: %m");
c79e98ea
LP
446 else if (n > 0)
447 server_process_native_message(s, p, n, ucred, tv, label, label_len);
448 }
0153028a
LP
449}
450
451int server_open_native_socket(Server*s) {
fc2fffe7
LP
452
453 static const union sockaddr_union sa = {
454 .un.sun_family = AF_UNIX,
455 .un.sun_path = "/run/systemd/journal/socket",
456 };
3b3154df
LP
457 static const int one = 1;
458 int r;
0153028a
LP
459
460 assert(s);
461
462 if (s->native_fd < 0) {
0153028a 463 s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
4a62c710
MS
464 if (s->native_fd < 0)
465 return log_error_errno(errno, "socket() failed: %m");
0153028a 466
fc2fffe7 467 (void) unlink(sa.un.sun_path);
0153028a 468
fc2fffe7 469 r = bind(s->native_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
4a62c710
MS
470 if (r < 0)
471 return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path);
0153028a 472
4a61c3e5 473 (void) chmod(sa.un.sun_path, 0666);
0153028a
LP
474 } else
475 fd_nonblock(s->native_fd, 1);
476
0153028a 477 r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
4a62c710
MS
478 if (r < 0)
479 return log_error_errno(errno, "SO_PASSCRED failed: %m");
0153028a
LP
480
481#ifdef HAVE_SELINUX
6355e756 482 if (mac_selinux_have()) {
d682b3a7
LP
483 r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
484 if (r < 0)
56f64d95 485 log_warning_errno(errno, "SO_PASSSEC failed: %m");
d682b3a7 486 }
0153028a
LP
487#endif
488
0153028a 489 r = setsockopt(s->native_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
4a62c710
MS
490 if (r < 0)
491 return log_error_errno(errno, "SO_TIMESTAMP failed: %m");
0153028a 492
8531ae70 493 r = sd_event_add_io(s->event, &s->native_event_source, s->native_fd, EPOLLIN, server_process_datagram, s);
23bbb0de
MS
494 if (r < 0)
495 return log_error_errno(r, "Failed to add native server fd to event loop: %m");
0153028a 496
48cef295
VC
497 r = sd_event_source_set_priority(s->native_event_source, SD_EVENT_PRIORITY_NORMAL+5);
498 if (r < 0)
499 return log_error_errno(r, "Failed to adjust native event source priority: %m");
500
0153028a
LP
501 return 0;
502}