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