| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/statvfs.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-event.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "errno-util.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "format-util.h" |
| 12 |
#include "iovec-util.h" |
| 13 |
#include "journal-internal.h" |
| 14 |
#include "journald-client.h" |
| 15 |
#include "journald-console.h" |
| 16 |
#include "journald-context.h" |
| 17 |
#include "journald-kmsg.h" |
| 18 |
#include "journald-manager.h" |
| 19 |
#include "journald-native.h" |
| 20 |
#include "journald-syslog.h" |
| 21 |
#include "journald-wall.h" |
| 22 |
#include "log.h" |
| 23 |
#include "log-ratelimit.h" |
| 24 |
#include "memfd-util.h" |
| 25 |
#include "memory-util.h" |
| 26 |
#include "parse-util.h" |
| 27 |
#include "path-util.h" |
| 28 |
#include "process-util.h" |
| 29 |
#include "selinux-util.h" |
| 30 |
#include "socket-util.h" |
| 31 |
#include "stat-util.h" |
| 32 |
#include "string-util.h" |
| 33 |
#include "unaligned.h" |
| 34 |
|
| 35 |
static bool allow_object_pid(const struct ucred *ucred) { |
| 36 |
return ucred && ucred->uid == 0; |
| 37 |
} |
| 38 |
|
| 39 |
static void manager_process_entry_meta( |
| 40 |
const char *p, size_t l, |
| 41 |
const struct ucred *ucred, |
| 42 |
int *priority, |
| 43 |
char **identifier, |
| 44 |
char **message, |
| 45 |
pid_t *object_pid) { |
| 46 |
|
| 47 |
assert(priority); |
| 48 |
assert(identifier); |
| 49 |
assert(message); |
| 50 |
|
| 51 |
/* We need to determine the priority of this entry for the rate limiting logic */ |
| 52 |
|
| 53 |
if (l == 10 && |
| 54 |
startswith(p, "PRIORITY=") && |
| 55 |
p[9] >= '0' && p[9] <= '9') |
| 56 |
*priority = (*priority & LOG_FACMASK) | (p[9] - '0'); |
| 57 |
|
| 58 |
else if (l == 17 && |
| 59 |
startswith(p, "SYSLOG_FACILITY=") && |
| 60 |
p[16] >= '0' && p[16] <= '9') |
| 61 |
*priority = LOG_PRI(*priority) | ((p[16] - '0') << 3); |
| 62 |
|
| 63 |
else if (l == 18 && |
| 64 |
startswith(p, "SYSLOG_FACILITY=") && |
| 65 |
p[16] >= '0' && p[16] <= '9' && |
| 66 |
p[17] >= '0' && p[17] <= '9') |
| 67 |
*priority = LOG_PRI(*priority) | (((p[16] - '0')*10 + (p[17] - '0')) << 3); |
| 68 |
|
| 69 |
else if (l >= 19 && |
| 70 |
startswith(p, "SYSLOG_IDENTIFIER=")) { |
| 71 |
char *t; |
| 72 |
|
| 73 |
t = memdup_suffix0(p + 18, l - 18); |
| 74 |
if (t) |
| 75 |
free_and_replace(*identifier, t); |
| 76 |
|
| 77 |
} else if (l >= 8 && |
| 78 |
startswith(p, "MESSAGE=")) { |
| 79 |
char *t; |
| 80 |
|
| 81 |
t = memdup_suffix0(p + 8, l - 8); |
| 82 |
if (t) |
| 83 |
free_and_replace(*message, t); |
| 84 |
|
| 85 |
} else if (l > STRLEN("OBJECT_PID=") && |
| 86 |
l < STRLEN("OBJECT_PID=") + DECIMAL_STR_MAX(pid_t) && |
| 87 |
startswith(p, "OBJECT_PID=") && |
| 88 |
allow_object_pid(ucred)) { |
| 89 |
char buf[DECIMAL_STR_MAX(pid_t)]; |
| 90 |
memcpy(buf, p + STRLEN("OBJECT_PID="), |
| 91 |
l - STRLEN("OBJECT_PID=")); |
| 92 |
buf[l-STRLEN("OBJECT_PID=")] = '\0'; |
| 93 |
|
| 94 |
(void) parse_pid(buf, object_pid); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
static int manager_process_entry( |
| 99 |
Manager *m, |
| 100 |
const void *buffer, size_t *remaining, |
| 101 |
ClientContext *context, |
| 102 |
const struct ucred *ucred, |
| 103 |
const struct timeval *tv, |
| 104 |
const char *label) { |
| 105 |
|
| 106 |
/* Process a single entry from a native message. Returns 0 if nothing special happened and the message |
| 107 |
* processing should continue, and a negative or positive value otherwise. |
| 108 |
* |
| 109 |
* Note that *remaining is altered on both success and failure. */ |
| 110 |
|
| 111 |
size_t n = 0, j, tn = SIZE_MAX, entry_size = 0; |
| 112 |
char *identifier = NULL, *message = NULL; |
| 113 |
struct iovec *iovec = NULL; |
| 114 |
int priority = LOG_INFO; |
| 115 |
pid_t object_pid = 0; |
| 116 |
const char *p; |
| 117 |
int r = 1; |
| 118 |
|
| 119 |
assert(remaining); |
| 120 |
|
| 121 |
p = buffer; |
| 122 |
|
| 123 |
while (*remaining > 0) { |
| 124 |
const char *e, *q; |
| 125 |
|
| 126 |
e = memchr(p, '\n', *remaining); |
| 127 |
|
| 128 |
if (!e) { |
| 129 |
/* Trailing noise, let's ignore it, and flush what we collected */ |
| 130 |
log_debug("Received message with trailing noise, ignoring."); |
| 131 |
break; /* finish processing of the message */ |
| 132 |
} |
| 133 |
|
| 134 |
if (e == p) { |
| 135 |
/* Entry separator */ |
| 136 |
*remaining -= 1; |
| 137 |
break; |
| 138 |
} |
| 139 |
|
| 140 |
if (IN_SET(*p, '.', '#')) { |
| 141 |
/* Ignore control commands for now, and comments too. */ |
| 142 |
*remaining -= (e - p) + 1; |
| 143 |
p = e + 1; |
| 144 |
continue; |
| 145 |
} |
| 146 |
|
| 147 |
/* A property follows */ |
| 148 |
if (n >= ENTRY_FIELD_COUNT_MAX) { |
| 149 |
log_debug("Received an entry that has more than " STRINGIFY(ENTRY_FIELD_COUNT_MAX) " fields, ignoring entry."); |
| 150 |
goto finish; |
| 151 |
} |
| 152 |
|
| 153 |
/* n existing properties, 1 new, +1 for _TRANSPORT */ |
| 154 |
if (!GREEDY_REALLOC(iovec, |
| 155 |
n + 2 + |
| 156 |
N_IOVEC_META_FIELDS + N_IOVEC_OBJECT_FIELDS + |
| 157 |
client_context_extra_fields_n_iovec(context))) { |
| 158 |
r = log_oom(); |
| 159 |
goto finish; |
| 160 |
} |
| 161 |
|
| 162 |
q = memchr(p, '=', e - p); |
| 163 |
if (q) { |
| 164 |
if (journal_field_valid(p, q - p, false)) { |
| 165 |
size_t l; |
| 166 |
|
| 167 |
l = e - p; |
| 168 |
if (l > DATA_SIZE_MAX) { |
| 169 |
log_debug("Received text block of %zu bytes is too large, ignoring entry.", l); |
| 170 |
goto finish; |
| 171 |
} |
| 172 |
|
| 173 |
if (entry_size + l + n + 1 > ENTRY_SIZE_MAX) { /* data + separators + trailer */ |
| 174 |
log_debug("Entry is too big (%zu bytes after processing %zu entries), ignoring entry.", |
| 175 |
entry_size + l, n + 1); |
| 176 |
goto finish; |
| 177 |
} |
| 178 |
|
| 179 |
/* If the field name starts with an underscore, skip the variable, since that indicates |
| 180 |
* a trusted field */ |
| 181 |
iovec[n++] = IOVEC_MAKE((char*) p, l); |
| 182 |
entry_size += l; |
| 183 |
|
| 184 |
manager_process_entry_meta(p, l, ucred, |
| 185 |
&priority, |
| 186 |
&identifier, |
| 187 |
&message, |
| 188 |
&object_pid); |
| 189 |
} |
| 190 |
|
| 191 |
*remaining -= (e - p) + 1; |
| 192 |
p = e + 1; |
| 193 |
continue; |
| 194 |
} else { |
| 195 |
uint64_t l, total; |
| 196 |
char *k; |
| 197 |
|
| 198 |
if (*remaining < e - p + 1 + sizeof(uint64_t) + 1) { |
| 199 |
log_debug("Failed to parse message, ignoring."); |
| 200 |
break; |
| 201 |
} |
| 202 |
|
| 203 |
l = unaligned_read_le64(e + 1); |
| 204 |
if (l > DATA_SIZE_MAX) { |
| 205 |
log_debug("Received binary data block of %"PRIu64" bytes is too large, ignoring entry.", l); |
| 206 |
goto finish; |
| 207 |
} |
| 208 |
|
| 209 |
total = (e - p) + 1 + l; |
| 210 |
if (entry_size + total + n + 1 > ENTRY_SIZE_MAX) { /* data + separators + trailer */ |
| 211 |
log_debug("Entry is too big (%"PRIu64"bytes after processing %zu fields), ignoring.", |
| 212 |
entry_size + total, n + 1); |
| 213 |
goto finish; |
| 214 |
} |
| 215 |
|
| 216 |
if ((uint64_t) *remaining < e - p + 1 + sizeof(uint64_t) + l + 1 || |
| 217 |
e[1+sizeof(uint64_t)+l] != '\n') { |
| 218 |
log_debug("Failed to parse message, ignoring."); |
| 219 |
break; |
| 220 |
} |
| 221 |
|
| 222 |
k = malloc(total); |
| 223 |
if (!k) { |
| 224 |
log_oom(); |
| 225 |
break; |
| 226 |
} |
| 227 |
|
| 228 |
memcpy(k, p, e - p); |
| 229 |
k[e - p] = '='; |
| 230 |
memcpy(k + (e - p) + 1, e + 1 + sizeof(uint64_t), l); |
| 231 |
|
| 232 |
if (journal_field_valid(p, e - p, false)) { |
| 233 |
iovec[n] = IOVEC_MAKE(k, (e - p) + 1 + l); |
| 234 |
entry_size += iovec[n].iov_len; |
| 235 |
n++; |
| 236 |
|
| 237 |
manager_process_entry_meta(k, (e - p) + 1 + l, ucred, |
| 238 |
&priority, |
| 239 |
&identifier, |
| 240 |
&message, |
| 241 |
&object_pid); |
| 242 |
} else |
| 243 |
free(k); |
| 244 |
|
| 245 |
*remaining -= (e - p) + 1 + sizeof(uint64_t) + l + 1; |
| 246 |
p = e + 1 + sizeof(uint64_t) + l + 1; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
if (n <= 0) |
| 251 |
goto finish; |
| 252 |
|
| 253 |
tn = n++; |
| 254 |
iovec[tn] = IOVEC_MAKE_STRING("_TRANSPORT=journal"); |
| 255 |
entry_size += STRLEN("_TRANSPORT=journal"); |
| 256 |
|
| 257 |
if (entry_size + n + 1 > ENTRY_SIZE_MAX) { /* data + separators + trailer */ |
| 258 |
log_debug("Entry is too big with %zu properties and %zu bytes, ignoring.", n, entry_size); |
| 259 |
goto finish; |
| 260 |
} |
| 261 |
|
| 262 |
r = 0; /* Success, we read the message. */ |
| 263 |
|
| 264 |
if (!client_context_test_priority(context, priority)) |
| 265 |
goto finish; |
| 266 |
|
| 267 |
if (message) { |
| 268 |
/* Ensure message is not NULL, otherwise strlen(message) would crash. This check needs to |
| 269 |
* be here until manager_process_entry() is able to process messages containing \0 characters, |
| 270 |
* as we would have access to the actual size of message. */ |
| 271 |
r = client_context_check_keep_log(context, message, strlen(message)); |
| 272 |
if (r <= 0) |
| 273 |
goto finish; |
| 274 |
|
| 275 |
if (m->config.forward_to_syslog) |
| 276 |
manager_forward_syslog(m, syslog_fixup_facility(priority), identifier, message, ucred, tv); |
| 277 |
|
| 278 |
if (m->config.forward_to_kmsg) |
| 279 |
manager_forward_kmsg(m, priority, identifier, message, ucred); |
| 280 |
|
| 281 |
if (m->config.forward_to_console) |
| 282 |
manager_forward_console(m, priority, identifier, message, ucred); |
| 283 |
|
| 284 |
if (m->config.forward_to_wall) |
| 285 |
manager_forward_wall(m, priority, identifier, message, ucred); |
| 286 |
} |
| 287 |
|
| 288 |
manager_dispatch_message(m, iovec, n, MALLOC_ELEMENTSOF(iovec), context, tv, priority, object_pid); |
| 289 |
|
| 290 |
finish: |
| 291 |
for (j = 0; j < n; j++) { |
| 292 |
if (j == tn) |
| 293 |
continue; |
| 294 |
|
| 295 |
if (iovec[j].iov_base < buffer || |
| 296 |
(const char*) iovec[j].iov_base >= p + *remaining) |
| 297 |
free(iovec[j].iov_base); |
| 298 |
} |
| 299 |
|
| 300 |
free(iovec); |
| 301 |
free(identifier); |
| 302 |
free(message); |
| 303 |
|
| 304 |
return r; |
| 305 |
} |
| 306 |
|
| 307 |
void manager_process_native_message( |
| 308 |
Manager *m, |
| 309 |
const char *buffer, size_t buffer_size, |
| 310 |
const struct ucred *ucred, |
| 311 |
const struct timeval *tv, |
| 312 |
const char *label) { |
| 313 |
|
| 314 |
size_t remaining = buffer_size; |
| 315 |
ClientContext *context = NULL; |
| 316 |
int r; |
| 317 |
|
| 318 |
assert(m); |
| 319 |
assert(buffer || buffer_size == 0); |
| 320 |
|
| 321 |
if (ucred && pid_is_valid(ucred->pid)) { |
| 322 |
r = client_context_get(m, ucred->pid, ucred, label, /* unit_id= */ NULL, &context); |
| 323 |
if (r < 0) |
| 324 |
log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, |
| 325 |
"Failed to retrieve credentials for PID " PID_FMT ", ignoring: %m", |
| 326 |
ucred->pid); |
| 327 |
} |
| 328 |
|
| 329 |
do { |
| 330 |
r = manager_process_entry(m, |
| 331 |
(const uint8_t*) buffer + (buffer_size - remaining), &remaining, |
| 332 |
context, ucred, tv, label); |
| 333 |
} while (r == 0); |
| 334 |
} |
| 335 |
|
| 336 |
static size_t entry_size_max_by_ucred(Manager *m, const struct ucred *ucred, const char *label) { |
| 337 |
static uint64_t entry_size_max = UINT64_MAX; |
| 338 |
static bool entry_size_max_checked = false; |
| 339 |
int r; |
| 340 |
|
| 341 |
if (entry_size_max != UINT64_MAX) |
| 342 |
return entry_size_max; |
| 343 |
if (!entry_size_max_checked) { |
| 344 |
const char *p; |
| 345 |
|
| 346 |
entry_size_max_checked = true; |
| 347 |
|
| 348 |
p = secure_getenv("SYSTEMD_JOURNAL_FD_SIZE_MAX"); |
| 349 |
if (p) { |
| 350 |
r = parse_size(p, 1024, &entry_size_max); |
| 351 |
if (r < 0) |
| 352 |
log_warning_errno(r, "Failed to parse $SYSTEMD_JOURNAL_FD_SIZE_MAX, ignoring: %m"); |
| 353 |
else |
| 354 |
return entry_size_max; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/* Check for unprivileged senders, as the default limit of 768M is quite high and the socket is |
| 359 |
* unprivileged, to avoid abuses. */ |
| 360 |
|
| 361 |
if (!ucred) |
| 362 |
return ENTRY_SIZE_UNPRIV_MAX; |
| 363 |
if (ucred->uid == 0) /* Shortcut for root senders */ |
| 364 |
return ENTRY_SIZE_MAX; |
| 365 |
|
| 366 |
/* As an exception, allow coredumps to use the old max size for backward compatibility */ |
| 367 |
if (pid_is_valid(ucred->pid)) { |
| 368 |
ClientContext *context = NULL; |
| 369 |
|
| 370 |
r = client_context_get(m, ucred->pid, ucred, label, /* unit_id= */ NULL, &context); |
| 371 |
if (r < 0) |
| 372 |
log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, |
| 373 |
"Failed to retrieve credentials for PID " PID_FMT ", ignoring: %m", |
| 374 |
ucred->pid); |
| 375 |
else if (context->unit && startswith(context->unit, "systemd-coredump@")) |
| 376 |
return ENTRY_SIZE_MAX; |
| 377 |
} |
| 378 |
|
| 379 |
return ENTRY_SIZE_UNPRIV_MAX; |
| 380 |
} |
| 381 |
|
| 382 |
int manager_process_native_file( |
| 383 |
Manager *m, |
| 384 |
int fd, |
| 385 |
const struct ucred *ucred, |
| 386 |
const struct timeval *tv, |
| 387 |
const char *label) { |
| 388 |
|
| 389 |
struct stat st; |
| 390 |
bool sealed; |
| 391 |
int r; |
| 392 |
|
| 393 |
/* Data is in the passed fd, probably it didn't fit in a datagram. */ |
| 394 |
|
| 395 |
assert(m); |
| 396 |
assert(fd >= 0); |
| 397 |
|
| 398 |
if (fstat(fd, &st) < 0) |
| 399 |
return log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT, |
| 400 |
"Failed to stat passed file: %m"); |
| 401 |
|
| 402 |
r = stat_verify_regular(&st); |
| 403 |
if (r < 0) |
| 404 |
return log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT, |
| 405 |
"File passed is not regular, ignoring message: %m"); |
| 406 |
|
| 407 |
if (st.st_size <= 0) |
| 408 |
return 0; |
| 409 |
|
| 410 |
r = fd_verify_safe_flags(fd); |
| 411 |
if (r == -EREMOTEIO) |
| 412 |
return log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT, |
| 413 |
"Unexpected flags of passed memory fd, ignoring message."); |
| 414 |
if (r < 0) |
| 415 |
return log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT, |
| 416 |
"Failed to get flags of passed file: %m"); |
| 417 |
|
| 418 |
/* If it's a memfd, check if it is sealed. If so, we can just mmap it and use it, and do not need to |
| 419 |
* copy the data out. */ |
| 420 |
sealed = memfd_get_sealed(fd) > 0; |
| 421 |
|
| 422 |
if (!sealed && (!ucred || ucred->uid != 0)) { |
| 423 |
_cleanup_free_ char *k = NULL; |
| 424 |
const char *e; |
| 425 |
|
| 426 |
/* If this is not a sealed memfd, and the peer is unknown or unprivileged, then verify the |
| 427 |
* path. */ |
| 428 |
|
| 429 |
r = fd_get_path(fd, &k); |
| 430 |
if (r < 0) |
| 431 |
return log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT, |
| 432 |
"Failed to get path of passed fd: %m"); |
| 433 |
|
| 434 |
e = PATH_STARTSWITH_SET(k, "/dev/shm/", "/tmp/", "/var/tmp/"); |
| 435 |
if (!e) |
| 436 |
return log_ratelimit_error_errno(SYNTHETIC_ERRNO(EPERM), JOURNAL_LOG_RATELIMIT, |
| 437 |
"Received file outside of allowed directories, refusing."); |
| 438 |
|
| 439 |
if (!filename_is_valid(e)) |
| 440 |
return log_ratelimit_error_errno(SYNTHETIC_ERRNO(EPERM), JOURNAL_LOG_RATELIMIT, |
| 441 |
"Received file in subdirectory of allowed directories, refusing."); |
| 442 |
} |
| 443 |
|
| 444 |
/* When !sealed, set a lower memory limit. We have to read the file, effectively doubling memory |
| 445 |
* use. */ |
| 446 |
if ((size_t) st.st_size > entry_size_max_by_ucred(m, ucred, label) / (sealed ? 1 : 2)) |
| 447 |
return log_ratelimit_error_errno(SYNTHETIC_ERRNO(EFBIG), JOURNAL_LOG_RATELIMIT, |
| 448 |
"File passed too large (%"PRIu64" bytes), refusing.", |
| 449 |
(uint64_t) st.st_size); |
| 450 |
|
| 451 |
if (sealed) { |
| 452 |
void *p; |
| 453 |
size_t ps; |
| 454 |
|
| 455 |
/* The file is sealed, we can just map it and use it. */ |
| 456 |
|
| 457 |
ps = PAGE_ALIGN(st.st_size); |
| 458 |
assert(ps < SIZE_MAX); |
| 459 |
p = mmap(NULL, ps, PROT_READ, MAP_PRIVATE, fd, 0); |
| 460 |
if (p == MAP_FAILED) |
| 461 |
return log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT, |
| 462 |
"Failed to map memfd: %m"); |
| 463 |
|
| 464 |
manager_process_native_message(m, p, st.st_size, ucred, tv, label); |
| 465 |
assert_se(munmap(p, ps) >= 0); |
| 466 |
|
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| 470 |
_cleanup_free_ void *p = NULL; |
| 471 |
struct statvfs vfs; |
| 472 |
ssize_t n; |
| 473 |
|
| 474 |
if (fstatvfs(fd, &vfs) < 0) |
| 475 |
return log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT, |
| 476 |
"Failed to stat file system of passed file: %m"); |
| 477 |
|
| 478 |
/* Refuse operating on file systems that have mandatory locking enabled. |
| 479 |
* See also: https://github.com/systemd/systemd/issues/1822 */ |
| 480 |
if (FLAGS_SET(vfs.f_flag, ST_MANDLOCK)) |
| 481 |
return log_ratelimit_error_errno(SYNTHETIC_ERRNO(EPERM), JOURNAL_LOG_RATELIMIT, |
| 482 |
"Received file descriptor from file system with mandatory locking enabled, not processing it."); |
| 483 |
|
| 484 |
/* Make the fd non-blocking. On regular files this has the effect of bypassing mandatory |
| 485 |
* locking. Of course, this should normally not be necessary given the check above, but let's |
| 486 |
* better be safe than sorry, after all NFS is pretty confusing regarding file system flags, |
| 487 |
* and we better don't trust it, and so is SMB. */ |
| 488 |
r = fd_nonblock(fd, true); |
| 489 |
if (r < 0) |
| 490 |
return log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT, |
| 491 |
"Failed to make fd non-blocking: %m"); |
| 492 |
|
| 493 |
/* The file is not sealed, we can't map the file here, since clients might then truncate it |
| 494 |
* and trigger a SIGBUS for us. So let's stupidly read it. */ |
| 495 |
|
| 496 |
p = malloc(st.st_size); |
| 497 |
if (!p) |
| 498 |
return log_oom(); |
| 499 |
|
| 500 |
n = pread(fd, p, st.st_size, 0); |
| 501 |
if (n < 0) |
| 502 |
return log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT, |
| 503 |
"Failed to read file: %m"); |
| 504 |
if (n > 0) |
| 505 |
manager_process_native_message(m, p, n, ucred, tv, label); |
| 506 |
|
| 507 |
return 0; |
| 508 |
} |
| 509 |
|
| 510 |
int manager_open_native_socket(Manager *m, const char *native_socket) { |
| 511 |
int r; |
| 512 |
|
| 513 |
assert(m); |
| 514 |
assert(native_socket); |
| 515 |
|
| 516 |
if (m->native_fd < 0) { |
| 517 |
union sockaddr_union sa; |
| 518 |
size_t sa_len; |
| 519 |
|
| 520 |
r = sockaddr_un_set_path(&sa.un, native_socket); |
| 521 |
if (r < 0) |
| 522 |
return log_error_errno(r, "Unable to use namespace path %s for AF_UNIX socket: %m", native_socket); |
| 523 |
sa_len = r; |
| 524 |
|
| 525 |
m->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| 526 |
if (m->native_fd < 0) |
| 527 |
return log_error_errno(errno, "socket() failed: %m"); |
| 528 |
|
| 529 |
(void) sockaddr_un_unlink(&sa.un); |
| 530 |
|
| 531 |
r = bind(m->native_fd, &sa.sa, sa_len); |
| 532 |
if (r < 0) |
| 533 |
return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path); |
| 534 |
|
| 535 |
(void) chmod(sa.un.sun_path, 0666); |
| 536 |
} else |
| 537 |
(void) fd_nonblock(m->native_fd, true); |
| 538 |
|
| 539 |
r = setsockopt_int(m->native_fd, SOL_SOCKET, SO_PASSCRED, true); |
| 540 |
if (r < 0) |
| 541 |
return log_error_errno(r, "SO_PASSCRED failed: %m"); |
| 542 |
|
| 543 |
if (mac_selinux_use()) { |
| 544 |
r = setsockopt_int(m->native_fd, SOL_SOCKET, SO_PASSSEC, true); |
| 545 |
if (r < 0) |
| 546 |
log_full_errno(ERRNO_IS_NEG_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r, "SO_PASSSEC failed, ignoring: %m"); |
| 547 |
} |
| 548 |
|
| 549 |
r = setsockopt_int(m->native_fd, SOL_SOCKET, SO_TIMESTAMP, true); |
| 550 |
if (r < 0) |
| 551 |
return log_error_errno(r, "SO_TIMESTAMP failed: %m"); |
| 552 |
|
| 553 |
r = sd_event_add_io(m->event, &m->native_event_source, m->native_fd, EPOLLIN, manager_process_datagram, m); |
| 554 |
if (r < 0) |
| 555 |
return log_error_errno(r, "Failed to add native manager fd to event loop: %m"); |
| 556 |
|
| 557 |
r = sd_event_source_set_priority(m->native_event_source, SD_EVENT_PRIORITY_NORMAL+5); |
| 558 |
if (r < 0) |
| 559 |
return log_error_errno(r, "Failed to adjust native event source priority: %m"); |
| 560 |
|
| 561 |
return 0; |
| 562 |
} |