| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <endian.h> |
| 4 |
#include <grp.h> |
| 5 |
#include <poll.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <sys/inotify.h> |
| 8 |
#include <unistd.h> |
| 9 |
|
| 10 |
#include "sd-bus.h" |
| 11 |
#include "sd-daemon.h" |
| 12 |
|
| 13 |
#include "alloc-util.h" |
| 14 |
#include "bus-internal.h" |
| 15 |
#include "bus-message.h" |
| 16 |
#include "bus-socket.h" |
| 17 |
#include "errno-util.h" |
| 18 |
#include "escape.h" |
| 19 |
#include "fd-util.h" |
| 20 |
#include "fs-util.h" |
| 21 |
#include "hexdecoct.h" |
| 22 |
#include "io-util.h" |
| 23 |
#include "iovec-util.h" |
| 24 |
#include "log.h" |
| 25 |
#include "memory-util.h" |
| 26 |
#include "path-util.h" |
| 27 |
#include "process-util.h" |
| 28 |
#include "random-util.h" |
| 29 |
#include "stdio-util.h" |
| 30 |
#include "string-util.h" |
| 31 |
#include "time-util.h" |
| 32 |
#include "user-util.h" |
| 33 |
#include "utf8.h" |
| 34 |
|
| 35 |
#define SNDBUF_SIZE (8*1024*1024) |
| 36 |
|
| 37 |
static void iovec_advance(struct iovec iov[], unsigned *idx, size_t size) { |
| 38 |
assert(idx); |
| 39 |
|
| 40 |
while (size > 0) { |
| 41 |
struct iovec *i = iov + *idx; |
| 42 |
|
| 43 |
if (i->iov_len > size) { |
| 44 |
i->iov_base = (uint8_t*) i->iov_base + size; |
| 45 |
i->iov_len -= size; |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
size -= i->iov_len; |
| 50 |
|
| 51 |
*i = IOVEC_MAKE(NULL, 0); |
| 52 |
|
| 53 |
(*idx)++; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
static int append_iovec(sd_bus_message *m, const void *p, size_t sz) { |
| 58 |
assert(m); |
| 59 |
assert(p); |
| 60 |
assert(sz > 0); |
| 61 |
|
| 62 |
m->iovec[m->n_iovec++] = IOVEC_MAKE((void*) p, sz); |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
static int bus_message_setup_iovec(sd_bus_message *m) { |
| 68 |
BusMessageBodyPart *part; |
| 69 |
unsigned n, i; |
| 70 |
int r; |
| 71 |
|
| 72 |
assert(m); |
| 73 |
assert(m->sealed); |
| 74 |
|
| 75 |
if (m->n_iovec > 0) |
| 76 |
return 0; |
| 77 |
|
| 78 |
assert(!m->iovec); |
| 79 |
|
| 80 |
n = 1 + m->n_body_parts; |
| 81 |
if (n < ELEMENTSOF(m->iovec_fixed)) |
| 82 |
m->iovec = m->iovec_fixed; |
| 83 |
else { |
| 84 |
m->iovec = new(struct iovec, n); |
| 85 |
if (!m->iovec) { |
| 86 |
r = -ENOMEM; |
| 87 |
goto fail; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
r = append_iovec(m, m->header, BUS_MESSAGE_BODY_BEGIN(m)); |
| 92 |
if (r < 0) |
| 93 |
goto fail; |
| 94 |
|
| 95 |
MESSAGE_FOREACH_PART(part, i, m) { |
| 96 |
r = bus_body_part_map(part); |
| 97 |
if (r < 0) |
| 98 |
goto fail; |
| 99 |
|
| 100 |
r = append_iovec(m, part->data, part->size); |
| 101 |
if (r < 0) |
| 102 |
goto fail; |
| 103 |
} |
| 104 |
|
| 105 |
assert(n == m->n_iovec); |
| 106 |
|
| 107 |
return 0; |
| 108 |
|
| 109 |
fail: |
| 110 |
m->poisoned = true; |
| 111 |
return r; |
| 112 |
} |
| 113 |
|
| 114 |
bool bus_socket_auth_needs_write(sd_bus *b) { |
| 115 |
|
| 116 |
unsigned i; |
| 117 |
|
| 118 |
if (b->auth_index >= ELEMENTSOF(b->auth_iovec)) |
| 119 |
return false; |
| 120 |
|
| 121 |
for (i = b->auth_index; i < ELEMENTSOF(b->auth_iovec); i++) { |
| 122 |
struct iovec *j = b->auth_iovec + i; |
| 123 |
|
| 124 |
if (j->iov_len > 0) |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
static int bus_socket_auth_verify_client(sd_bus *b) { |
| 132 |
char *l, *lines[4] = {}; |
| 133 |
sd_id128_t peer; |
| 134 |
size_t i, n; |
| 135 |
int r; |
| 136 |
|
| 137 |
assert(b); |
| 138 |
|
| 139 |
/* |
| 140 |
* We expect up to three response lines: |
| 141 |
* "DATA\r\n" (optional) |
| 142 |
* "OK <server-id>\r\n" |
| 143 |
* "AGREE_UNIX_FD\r\n" (optional) |
| 144 |
*/ |
| 145 |
|
| 146 |
n = 0; |
| 147 |
lines[n] = b->rbuffer; |
| 148 |
for (i = 0; i < 3; ++i) { |
| 149 |
l = memmem_safe(lines[n], b->rbuffer_size - (lines[n] - (char*) b->rbuffer), "\r\n", 2); |
| 150 |
if (l) |
| 151 |
lines[++n] = l + 2; |
| 152 |
else |
| 153 |
break; |
| 154 |
} |
| 155 |
|
| 156 |
/* |
| 157 |
* If we sent a non-empty initial response, then we just expect an OK |
| 158 |
* reply. We currently do this if, and only if, we picked ANONYMOUS. |
| 159 |
* If we did not send an initial response, then we expect a DATA |
| 160 |
* challenge, reply with our own DATA, and expect an OK reply. We do |
| 161 |
* this for EXTERNAL. |
| 162 |
* If FD negotiation was requested, we additionally expect |
| 163 |
* an AGREE_UNIX_FD response in all cases. |
| 164 |
*/ |
| 165 |
if (n < (b->anonymous_auth ? 1U : 2U) + !!b->accept_fd) |
| 166 |
return 0; /* wait for more data */ |
| 167 |
|
| 168 |
i = 0; |
| 169 |
|
| 170 |
/* In case of EXTERNAL, verify the first response was DATA. */ |
| 171 |
if (!b->anonymous_auth) { |
| 172 |
l = lines[i++]; |
| 173 |
if (lines[i] - l == 4 + 2) { |
| 174 |
if (memcmp(l, "DATA", 4) != 0) |
| 175 |
return -EPERM; |
| 176 |
} else if (lines[i] - l == 3 + 32 + 2) { |
| 177 |
/* |
| 178 |
* Old versions of the server-side implementation of |
| 179 |
* `sd-bus` replied with "OK <id>" to "AUTH" requests |
| 180 |
* from a client, even if the "AUTH" line did not |
| 181 |
* contain inlined arguments. Therefore, we also accept |
| 182 |
* "OK <id>" here, even though it is technically the |
| 183 |
* wrong reply. We ignore the "<id>" parameter, though, |
| 184 |
* since it has no real value. |
| 185 |
*/ |
| 186 |
if (memcmp(l, "OK ", 3) != 0) |
| 187 |
return -EPERM; |
| 188 |
} else |
| 189 |
return -EPERM; |
| 190 |
} |
| 191 |
|
| 192 |
/* Now check the OK line. */ |
| 193 |
l = lines[i++]; |
| 194 |
|
| 195 |
if (lines[i] - l != 3 + 32 + 2) |
| 196 |
return -EPERM; |
| 197 |
if (memcmp(l, "OK ", 3) != 0) |
| 198 |
return -EPERM; |
| 199 |
|
| 200 |
b->auth = b->anonymous_auth ? BUS_AUTH_ANONYMOUS : BUS_AUTH_EXTERNAL; |
| 201 |
|
| 202 |
for (unsigned j = 0; j < 32; j += 2) { |
| 203 |
int x, y; |
| 204 |
|
| 205 |
x = unhexchar(l[3 + j]); |
| 206 |
y = unhexchar(l[3 + j + 1]); |
| 207 |
|
| 208 |
if (x < 0 || y < 0) |
| 209 |
return -EINVAL; |
| 210 |
|
| 211 |
peer.bytes[j/2] = ((uint8_t) x << 4 | (uint8_t) y); |
| 212 |
} |
| 213 |
|
| 214 |
if (!sd_id128_is_null(b->server_id) && |
| 215 |
!sd_id128_equal(b->server_id, peer)) |
| 216 |
return -EPERM; |
| 217 |
|
| 218 |
b->server_id = peer; |
| 219 |
|
| 220 |
/* And possibly check the third line, too */ |
| 221 |
if (b->accept_fd) { |
| 222 |
l = lines[i++]; |
| 223 |
b->can_fds = memory_startswith(l, lines[i] - l, "AGREE_UNIX_FD"); |
| 224 |
} |
| 225 |
|
| 226 |
assert(i == n); |
| 227 |
|
| 228 |
b->rbuffer_size -= (lines[i] - (char*) b->rbuffer); |
| 229 |
memmove(b->rbuffer, lines[i], b->rbuffer_size); |
| 230 |
|
| 231 |
r = bus_start_running(b); |
| 232 |
if (r < 0) |
| 233 |
return r; |
| 234 |
|
| 235 |
return 1; |
| 236 |
} |
| 237 |
|
| 238 |
static bool line_equals(const char *s, size_t m, const char *line) { |
| 239 |
size_t l; |
| 240 |
|
| 241 |
l = strlen(line); |
| 242 |
if (l != m) |
| 243 |
return false; |
| 244 |
|
| 245 |
return memcmp(s, line, l) == 0; |
| 246 |
} |
| 247 |
|
| 248 |
static bool line_begins(const char *s, size_t m, const char *word) { |
| 249 |
const char *p; |
| 250 |
|
| 251 |
p = memory_startswith(s, m, word); |
| 252 |
return p && (p == (s + m) || *p == ' '); |
| 253 |
} |
| 254 |
|
| 255 |
static int verify_anonymous_token(sd_bus *b, const char *p, size_t l) { |
| 256 |
_cleanup_free_ char *token = NULL; |
| 257 |
size_t len; |
| 258 |
int r; |
| 259 |
|
| 260 |
if (!b->anonymous_auth) |
| 261 |
return 0; |
| 262 |
|
| 263 |
if (l <= 0) |
| 264 |
return 1; |
| 265 |
|
| 266 |
assert(p[0] == ' '); |
| 267 |
p++; l--; |
| 268 |
|
| 269 |
if (l % 2 != 0) |
| 270 |
return 0; |
| 271 |
|
| 272 |
r = unhexmem_full(p, l, /* secure= */ false, (void**) &token, &len); |
| 273 |
if (r < 0) |
| 274 |
return 0; |
| 275 |
|
| 276 |
if (memchr(token, 0, len)) |
| 277 |
return 0; |
| 278 |
|
| 279 |
return !!utf8_is_valid(token); |
| 280 |
} |
| 281 |
|
| 282 |
static int verify_external_token(sd_bus *b, const char *p, size_t l) { |
| 283 |
_cleanup_free_ char *token = NULL; |
| 284 |
size_t len; |
| 285 |
uid_t u; |
| 286 |
int r; |
| 287 |
|
| 288 |
/* We don't do any real authentication here. Instead, if |
| 289 |
* the owner of this bus wanted authentication they should have |
| 290 |
* checked SO_PEERCRED before even creating the bus object. */ |
| 291 |
|
| 292 |
if (!b->anonymous_auth && !b->ucred_valid) |
| 293 |
return 0; |
| 294 |
|
| 295 |
if (l <= 0) |
| 296 |
return 1; |
| 297 |
|
| 298 |
assert(p[0] == ' '); |
| 299 |
p++; l--; |
| 300 |
|
| 301 |
if (l % 2 != 0) |
| 302 |
return 0; |
| 303 |
|
| 304 |
r = unhexmem_full(p, l, /* secure= */ false, (void**) &token, &len); |
| 305 |
if (r < 0) |
| 306 |
return 0; |
| 307 |
|
| 308 |
if (memchr(token, 0, len)) |
| 309 |
return 0; |
| 310 |
|
| 311 |
r = parse_uid(token, &u); |
| 312 |
if (r < 0) |
| 313 |
return 0; |
| 314 |
|
| 315 |
/* We ignore the passed value if anonymous authentication is |
| 316 |
* on anyway. */ |
| 317 |
if (!b->anonymous_auth && u != b->ucred.uid) |
| 318 |
return 0; |
| 319 |
|
| 320 |
return 1; |
| 321 |
} |
| 322 |
|
| 323 |
static int bus_socket_auth_write(sd_bus *b, const char *t) { |
| 324 |
char *p; |
| 325 |
size_t l; |
| 326 |
|
| 327 |
assert(b); |
| 328 |
assert(t); |
| 329 |
|
| 330 |
/* We only make use of the first iovec */ |
| 331 |
assert(IN_SET(b->auth_index, 0, 1)); |
| 332 |
|
| 333 |
l = strlen(t); |
| 334 |
p = malloc(b->auth_iovec[0].iov_len + l); |
| 335 |
if (!p) |
| 336 |
return -ENOMEM; |
| 337 |
|
| 338 |
memcpy_safe(p, b->auth_iovec[0].iov_base, b->auth_iovec[0].iov_len); |
| 339 |
memcpy(p + b->auth_iovec[0].iov_len, t, l); |
| 340 |
|
| 341 |
b->auth_iovec[0].iov_base = p; |
| 342 |
b->auth_iovec[0].iov_len += l; |
| 343 |
|
| 344 |
free_and_replace(b->auth_buffer, p); |
| 345 |
b->auth_index = 0; |
| 346 |
return 0; |
| 347 |
} |
| 348 |
|
| 349 |
static int bus_socket_auth_write_ok(sd_bus *b) { |
| 350 |
char t[3 + 32 + 2 + 1]; |
| 351 |
|
| 352 |
assert(b); |
| 353 |
|
| 354 |
xsprintf(t, "OK " SD_ID128_FORMAT_STR "\r\n", SD_ID128_FORMAT_VAL(b->server_id)); |
| 355 |
|
| 356 |
return bus_socket_auth_write(b, t); |
| 357 |
} |
| 358 |
|
| 359 |
static int bus_socket_auth_verify_server(sd_bus *b) { |
| 360 |
char *e; |
| 361 |
const char *line; |
| 362 |
size_t l; |
| 363 |
bool processed = false; |
| 364 |
int r; |
| 365 |
|
| 366 |
assert(b); |
| 367 |
|
| 368 |
if (b->rbuffer_size < 1) |
| 369 |
return 0; |
| 370 |
|
| 371 |
/* First char must be a NUL byte */ |
| 372 |
if (*(char*) b->rbuffer != 0) |
| 373 |
return -EIO; |
| 374 |
|
| 375 |
if (b->rbuffer_size < 3) |
| 376 |
return 0; |
| 377 |
|
| 378 |
/* Begin with the first line */ |
| 379 |
if (b->auth_rbegin <= 0) |
| 380 |
b->auth_rbegin = 1; |
| 381 |
|
| 382 |
for (;;) { |
| 383 |
/* Check if line is complete */ |
| 384 |
line = (char*) b->rbuffer + b->auth_rbegin; |
| 385 |
e = memmem_safe(line, b->rbuffer_size - b->auth_rbegin, "\r\n", 2); |
| 386 |
if (!e) |
| 387 |
return processed; |
| 388 |
|
| 389 |
l = e - line; |
| 390 |
|
| 391 |
if (line_begins(line, l, "AUTH ANONYMOUS")) { |
| 392 |
|
| 393 |
r = verify_anonymous_token(b, |
| 394 |
line + strlen("AUTH ANONYMOUS"), |
| 395 |
l - strlen("AUTH ANONYMOUS")); |
| 396 |
if (r < 0) |
| 397 |
return r; |
| 398 |
if (r == 0) |
| 399 |
r = bus_socket_auth_write(b, "REJECTED\r\n"); |
| 400 |
else { |
| 401 |
b->auth = BUS_AUTH_ANONYMOUS; |
| 402 |
if (l <= strlen("AUTH ANONYMOUS")) |
| 403 |
r = bus_socket_auth_write(b, "DATA\r\n"); |
| 404 |
else |
| 405 |
r = bus_socket_auth_write_ok(b); |
| 406 |
} |
| 407 |
|
| 408 |
} else if (line_begins(line, l, "AUTH EXTERNAL")) { |
| 409 |
|
| 410 |
r = verify_external_token(b, |
| 411 |
line + strlen("AUTH EXTERNAL"), |
| 412 |
l - strlen("AUTH EXTERNAL")); |
| 413 |
if (r < 0) |
| 414 |
return r; |
| 415 |
if (r == 0) |
| 416 |
r = bus_socket_auth_write(b, "REJECTED\r\n"); |
| 417 |
else { |
| 418 |
b->auth = BUS_AUTH_EXTERNAL; |
| 419 |
if (l <= strlen("AUTH EXTERNAL")) |
| 420 |
r = bus_socket_auth_write(b, "DATA\r\n"); |
| 421 |
else |
| 422 |
r = bus_socket_auth_write_ok(b); |
| 423 |
} |
| 424 |
|
| 425 |
} else if (line_begins(line, l, "AUTH")) |
| 426 |
r = bus_socket_auth_write(b, "REJECTED EXTERNAL ANONYMOUS\r\n"); |
| 427 |
else if (line_equals(line, l, "CANCEL") || |
| 428 |
line_begins(line, l, "ERROR")) { |
| 429 |
|
| 430 |
b->auth = _BUS_AUTH_INVALID; |
| 431 |
r = bus_socket_auth_write(b, "REJECTED\r\n"); |
| 432 |
|
| 433 |
} else if (line_equals(line, l, "BEGIN")) { |
| 434 |
|
| 435 |
if (b->auth == _BUS_AUTH_INVALID) |
| 436 |
r = bus_socket_auth_write(b, "ERROR\r\n"); |
| 437 |
else { |
| 438 |
/* We can't leave from the auth phase |
| 439 |
* before we haven't written |
| 440 |
* everything queued, so let's check |
| 441 |
* that */ |
| 442 |
|
| 443 |
if (bus_socket_auth_needs_write(b)) |
| 444 |
return 1; |
| 445 |
|
| 446 |
b->rbuffer_size -= (e + 2 - (char*) b->rbuffer); |
| 447 |
memmove(b->rbuffer, e + 2, b->rbuffer_size); |
| 448 |
return bus_start_running(b); |
| 449 |
} |
| 450 |
|
| 451 |
} else if (line_begins(line, l, "DATA")) { |
| 452 |
|
| 453 |
if (b->auth == _BUS_AUTH_INVALID) |
| 454 |
r = bus_socket_auth_write(b, "ERROR\r\n"); |
| 455 |
else { |
| 456 |
if (b->auth == BUS_AUTH_ANONYMOUS) |
| 457 |
r = verify_anonymous_token(b, line + 4, l - 4); |
| 458 |
else |
| 459 |
r = verify_external_token(b, line + 4, l - 4); |
| 460 |
|
| 461 |
if (r < 0) |
| 462 |
return r; |
| 463 |
if (r == 0) { |
| 464 |
b->auth = _BUS_AUTH_INVALID; |
| 465 |
r = bus_socket_auth_write(b, "REJECTED\r\n"); |
| 466 |
} else |
| 467 |
r = bus_socket_auth_write_ok(b); |
| 468 |
} |
| 469 |
} else if (line_equals(line, l, "NEGOTIATE_UNIX_FD")) { |
| 470 |
if (b->auth == _BUS_AUTH_INVALID || !b->accept_fd) |
| 471 |
r = bus_socket_auth_write(b, "ERROR\r\n"); |
| 472 |
else { |
| 473 |
b->can_fds = true; |
| 474 |
r = bus_socket_auth_write(b, "AGREE_UNIX_FD\r\n"); |
| 475 |
} |
| 476 |
} else |
| 477 |
r = bus_socket_auth_write(b, "ERROR\r\n"); |
| 478 |
|
| 479 |
if (r < 0) |
| 480 |
return r; |
| 481 |
|
| 482 |
b->auth_rbegin = e + 2 - (char*) b->rbuffer; |
| 483 |
|
| 484 |
processed = true; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
static int bus_socket_auth_verify(sd_bus *b) { |
| 489 |
assert(b); |
| 490 |
|
| 491 |
if (b->is_server) |
| 492 |
return bus_socket_auth_verify_server(b); |
| 493 |
else |
| 494 |
return bus_socket_auth_verify_client(b); |
| 495 |
} |
| 496 |
|
| 497 |
static int bus_socket_write_auth(sd_bus *b) { |
| 498 |
ssize_t k; |
| 499 |
|
| 500 |
assert(b); |
| 501 |
assert(b->state == BUS_AUTHENTICATING); |
| 502 |
|
| 503 |
if (!bus_socket_auth_needs_write(b)) |
| 504 |
return 0; |
| 505 |
|
| 506 |
if (b->prefer_writev) |
| 507 |
k = writev(b->output_fd, b->auth_iovec + b->auth_index, ELEMENTSOF(b->auth_iovec) - b->auth_index); |
| 508 |
else { |
| 509 |
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control = {}; |
| 510 |
|
| 511 |
struct msghdr mh = { |
| 512 |
.msg_iov = b->auth_iovec + b->auth_index, |
| 513 |
.msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index, |
| 514 |
}; |
| 515 |
|
| 516 |
if (uid_is_valid(b->connect_as_uid) || gid_is_valid(b->connect_as_gid)) { |
| 517 |
|
| 518 |
/* If we shall connect under some specific UID/GID, then synthesize an |
| 519 |
* SCM_CREDENTIALS record accordingly. After all we want to adopt this UID/GID both |
| 520 |
* for SO_PEERCRED (where we have to fork()) and SCM_CREDENTIALS (where we can just |
| 521 |
* fake it via sendmsg()) */ |
| 522 |
|
| 523 |
struct ucred ucred = { |
| 524 |
.pid = getpid_cached(), |
| 525 |
.uid = uid_is_valid(b->connect_as_uid) ? b->connect_as_uid : getuid(), |
| 526 |
.gid = gid_is_valid(b->connect_as_gid) ? b->connect_as_gid : getgid(), |
| 527 |
}; |
| 528 |
|
| 529 |
mh.msg_control = &control; |
| 530 |
mh.msg_controllen = sizeof(control); |
| 531 |
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&mh); |
| 532 |
*cmsg = (struct cmsghdr) { |
| 533 |
.cmsg_level = SOL_SOCKET, |
| 534 |
.cmsg_type = SCM_CREDENTIALS, |
| 535 |
.cmsg_len = CMSG_LEN(sizeof(struct ucred)), |
| 536 |
}; |
| 537 |
|
| 538 |
memcpy(CMSG_DATA(cmsg), &ucred, sizeof(struct ucred)); |
| 539 |
} |
| 540 |
|
| 541 |
k = sendmsg(b->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); |
| 542 |
if (k < 0 && errno == ENOTSOCK) { |
| 543 |
b->prefer_writev = true; |
| 544 |
k = writev(b->output_fd, b->auth_iovec + b->auth_index, ELEMENTSOF(b->auth_iovec) - b->auth_index); |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
if (k < 0) |
| 549 |
return ERRNO_IS_TRANSIENT(errno) ? 0 : -errno; |
| 550 |
|
| 551 |
iovec_advance(b->auth_iovec, &b->auth_index, (size_t) k); |
| 552 |
|
| 553 |
/* Now crank the state machine since we might be able to make progress after writing. For example, |
| 554 |
* the server only processes "BEGIN" when the write buffer is empty. |
| 555 |
*/ |
| 556 |
return bus_socket_auth_verify(b); |
| 557 |
} |
| 558 |
|
| 559 |
static int bus_process_cmsg(sd_bus *bus, struct msghdr *mh, bool allow_fds) { |
| 560 |
_cleanup_close_ int pidfd = -EBADF; |
| 561 |
const int *fds = NULL; |
| 562 |
size_t n_fds = 0; |
| 563 |
|
| 564 |
assert(bus); |
| 565 |
assert(mh); |
| 566 |
|
| 567 |
CLEANUP_ARRAY(fds, n_fds, close_many); |
| 568 |
|
| 569 |
struct cmsghdr *cmsg; |
| 570 |
CMSG_FOREACH(cmsg, mh) |
| 571 |
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
| 572 |
assert(!fds); |
| 573 |
fds = CMSG_TYPED_DATA(cmsg, int); |
| 574 |
n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); |
| 575 |
|
| 576 |
} else if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_PIDFD) { |
| 577 |
log_debug("Got unexpected auxiliary pidfd, ignoring."); |
| 578 |
assert(pidfd < 0); |
| 579 |
pidfd = *CMSG_TYPED_DATA(cmsg, int); |
| 580 |
|
| 581 |
} else |
| 582 |
log_debug("Got unexpected auxiliary data with level=%d and type=%d, ignoring.", |
| 583 |
cmsg->cmsg_level, cmsg->cmsg_type); |
| 584 |
|
| 585 |
if (!allow_fds) { |
| 586 |
if (fds) |
| 587 |
/* Whut? We received fds during the auth protocol or so? Somebody is playing games |
| 588 |
* with us. Close them all, and fail */ |
| 589 |
return -EIO; |
| 590 |
|
| 591 |
return 0; |
| 592 |
} |
| 593 |
|
| 594 |
/* Check if we previously received fds with MSG_CTRUNC set. When the kernel truncates the fd array, |
| 595 |
* it drops all fds from the blocked one onwards - we have no way to know how many were lost or which |
| 596 |
* indexes are affected. Any fds we receive now would be appended at the wrong indexes, corrupting |
| 597 |
* the message's fd table. We must silently drop them to avoid a worse mess. Reading the message will |
| 598 |
* still fail later when the user tries to access a truncated fd. */ |
| 599 |
if (!bus->got_ctrunc) { |
| 600 |
if (!GREEDY_REALLOC(bus->fds, bus->n_fds + n_fds)) |
| 601 |
return -ENOMEM; |
| 602 |
|
| 603 |
FOREACH_ARRAY(i, fds, n_fds) |
| 604 |
bus->fds[bus->n_fds++] = fd_move_above_stdio(*i); |
| 605 |
|
| 606 |
TAKE_PTR(fds); |
| 607 |
n_fds = 0; |
| 608 |
} |
| 609 |
|
| 610 |
if (FLAGS_SET(mh->msg_flags, MSG_CTRUNC)) |
| 611 |
bus->got_ctrunc = true; |
| 612 |
|
| 613 |
return 0; |
| 614 |
} |
| 615 |
|
| 616 |
static int bus_socket_read_auth(sd_bus *b) { |
| 617 |
struct msghdr mh; |
| 618 |
struct iovec iov = {}; |
| 619 |
size_t n; |
| 620 |
ssize_t k; |
| 621 |
int r; |
| 622 |
void *p; |
| 623 |
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)) control; |
| 624 |
bool handle_cmsg = false; |
| 625 |
|
| 626 |
assert(b); |
| 627 |
assert(b->state == BUS_AUTHENTICATING); |
| 628 |
|
| 629 |
r = bus_socket_auth_verify(b); |
| 630 |
if (r != 0) |
| 631 |
return r; |
| 632 |
|
| 633 |
n = MAX(256u, b->rbuffer_size * 2); |
| 634 |
|
| 635 |
if (n > BUS_AUTH_SIZE_MAX) |
| 636 |
n = BUS_AUTH_SIZE_MAX; |
| 637 |
|
| 638 |
if (b->rbuffer_size >= n) |
| 639 |
return -ENOBUFS; |
| 640 |
|
| 641 |
p = realloc(b->rbuffer, n); |
| 642 |
if (!p) |
| 643 |
return -ENOMEM; |
| 644 |
|
| 645 |
b->rbuffer = p; |
| 646 |
|
| 647 |
iov = IOVEC_MAKE((uint8_t *)b->rbuffer + b->rbuffer_size, n - b->rbuffer_size); |
| 648 |
|
| 649 |
if (b->prefer_readv) { |
| 650 |
k = readv(b->input_fd, &iov, 1); |
| 651 |
if (k < 0) |
| 652 |
k = -errno; |
| 653 |
} else { |
| 654 |
mh = (struct msghdr) { |
| 655 |
.msg_iov = &iov, |
| 656 |
.msg_iovlen = 1, |
| 657 |
.msg_control = &control, |
| 658 |
.msg_controllen = sizeof(control), |
| 659 |
}; |
| 660 |
|
| 661 |
k = recvmsg_safe(b->input_fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); |
| 662 |
if (k == -ENOTSOCK) { |
| 663 |
b->prefer_readv = true; |
| 664 |
k = readv(b->input_fd, &iov, 1); |
| 665 |
if (k < 0) |
| 666 |
k = -errno; |
| 667 |
} else |
| 668 |
handle_cmsg = true; |
| 669 |
} |
| 670 |
if (ERRNO_IS_NEG_TRANSIENT(k)) |
| 671 |
return 0; |
| 672 |
if (k < 0) |
| 673 |
return (int) k; |
| 674 |
if (k == 0) { |
| 675 |
if (handle_cmsg) |
| 676 |
cmsg_close_all(&mh); /* paranoia, we shouldn't have gotten any fds on EOF */ |
| 677 |
return -ECONNRESET; |
| 678 |
} |
| 679 |
|
| 680 |
/* Silence static analyzers, k is bounded by iov size: n - rbuffer_size */ |
| 681 |
assert((size_t) k <= n - b->rbuffer_size); |
| 682 |
b->rbuffer_size += k; |
| 683 |
|
| 684 |
if (handle_cmsg) { |
| 685 |
r = bus_process_cmsg(b, &mh, /* allow_fds= */ false); |
| 686 |
if (r < 0) |
| 687 |
return r; |
| 688 |
} |
| 689 |
|
| 690 |
r = bus_socket_auth_verify(b); |
| 691 |
if (r != 0) |
| 692 |
return r; |
| 693 |
|
| 694 |
return 1; |
| 695 |
} |
| 696 |
|
| 697 |
void bus_socket_setup(sd_bus *b) { |
| 698 |
assert(b); |
| 699 |
|
| 700 |
/* Increase the buffers to 8 MB */ |
| 701 |
(void) fd_increase_rxbuf(b->input_fd, SNDBUF_SIZE); |
| 702 |
(void) fd_inc_sndbuf(b->output_fd, SNDBUF_SIZE); |
| 703 |
|
| 704 |
b->message_version = 1; |
| 705 |
b->message_endian = 0; |
| 706 |
} |
| 707 |
|
| 708 |
static void bus_get_peercred(sd_bus *b) { |
| 709 |
int r; |
| 710 |
|
| 711 |
assert(b); |
| 712 |
assert(!b->ucred_valid); |
| 713 |
assert(!b->label); |
| 714 |
assert(b->n_groups == SIZE_MAX); |
| 715 |
|
| 716 |
/* Get the peer for socketpair() sockets */ |
| 717 |
b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0; |
| 718 |
|
| 719 |
/* Get the SELinux context of the peer */ |
| 720 |
r = getpeersec(b->input_fd, &b->label); |
| 721 |
if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT)) |
| 722 |
log_debug_errno(r, "Failed to determine peer security context, ignoring: %m"); |
| 723 |
|
| 724 |
/* Get the list of auxiliary groups of the peer */ |
| 725 |
r = getpeergroups(b->input_fd, &b->groups); |
| 726 |
if (r >= 0) |
| 727 |
b->n_groups = (size_t) r; |
| 728 |
else if (!IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT)) |
| 729 |
log_debug_errno(r, "Failed to determine peer's group list, ignoring: %m"); |
| 730 |
|
| 731 |
r = getpeerpidfd(b->input_fd); |
| 732 |
if (r < 0) |
| 733 |
log_debug_errno(r, "Failed to determine peer pidfd, ignoring: %m"); |
| 734 |
else |
| 735 |
close_and_replace(b->pidfd, r); |
| 736 |
|
| 737 |
/* Let's query the peers socket address, it might carry information such as the peer's comm or |
| 738 |
* description string */ |
| 739 |
zero(b->sockaddr_peer); |
| 740 |
b->sockaddr_size_peer = 0; |
| 741 |
|
| 742 |
socklen_t l = sizeof(b->sockaddr_peer) - 1; /* Leave space for a NUL */ |
| 743 |
if (getpeername(b->input_fd, &b->sockaddr_peer.sa, &l) < 0) |
| 744 |
log_debug_errno(errno, "Failed to get peer's socket address, ignoring: %m"); |
| 745 |
else |
| 746 |
b->sockaddr_size_peer = l; |
| 747 |
} |
| 748 |
|
| 749 |
static int bus_socket_start_auth_client(sd_bus *b) { |
| 750 |
static const char sasl_auth_anonymous[] = { |
| 751 |
/* |
| 752 |
* We use an arbitrary trace-string for the ANONYMOUS authentication. It can be used by the |
| 753 |
* message broker to aid debugging of clients. We fully anonymize the connection and use a |
| 754 |
* static default. |
| 755 |
*/ |
| 756 |
/* HEX a n o n y m o u s */ |
| 757 |
"\0AUTH ANONYMOUS 616e6f6e796d6f7573\r\n" |
| 758 |
}; |
| 759 |
static const char sasl_auth_external[] = { |
| 760 |
"\0AUTH EXTERNAL\r\n" |
| 761 |
"DATA\r\n" |
| 762 |
}; |
| 763 |
static const char sasl_negotiate_unix_fd[] = { |
| 764 |
"NEGOTIATE_UNIX_FD\r\n" |
| 765 |
}; |
| 766 |
static const char sasl_begin[] = { |
| 767 |
"BEGIN\r\n" |
| 768 |
}; |
| 769 |
size_t i = 0; |
| 770 |
|
| 771 |
assert(b); |
| 772 |
|
| 773 |
if (b->anonymous_auth) |
| 774 |
b->auth_iovec[i++] = IOVEC_MAKE((char*) sasl_auth_anonymous, sizeof(sasl_auth_anonymous) - 1); |
| 775 |
else |
| 776 |
b->auth_iovec[i++] = IOVEC_MAKE((char*) sasl_auth_external, sizeof(sasl_auth_external) - 1); |
| 777 |
|
| 778 |
if (b->accept_fd) |
| 779 |
b->auth_iovec[i++] = IOVEC_MAKE_STRING(sasl_negotiate_unix_fd); |
| 780 |
|
| 781 |
b->auth_iovec[i++] = IOVEC_MAKE_STRING(sasl_begin); |
| 782 |
|
| 783 |
return bus_socket_write_auth(b); |
| 784 |
} |
| 785 |
|
| 786 |
int bus_socket_start_auth(sd_bus *b) { |
| 787 |
assert(b); |
| 788 |
|
| 789 |
bus_get_peercred(b); |
| 790 |
|
| 791 |
bus_set_state(b, BUS_AUTHENTICATING); |
| 792 |
b->auth_timeout = usec_add(now(CLOCK_MONOTONIC), BUS_AUTH_TIMEOUT); |
| 793 |
|
| 794 |
if (sd_is_socket(b->input_fd, AF_UNIX, 0, 0) <= 0) |
| 795 |
b->accept_fd = false; |
| 796 |
|
| 797 |
if (b->output_fd != b->input_fd) |
| 798 |
if (sd_is_socket(b->output_fd, AF_UNIX, 0, 0) <= 0) |
| 799 |
b->accept_fd = false; |
| 800 |
|
| 801 |
if (b->is_server) |
| 802 |
return bus_socket_read_auth(b); |
| 803 |
else |
| 804 |
return bus_socket_start_auth_client(b); |
| 805 |
} |
| 806 |
|
| 807 |
static int bus_socket_inotify_setup(sd_bus *b) { |
| 808 |
_cleanup_free_ int *new_watches = NULL; |
| 809 |
_cleanup_free_ char *absolute = NULL; |
| 810 |
size_t n = 0, done = 0, i; |
| 811 |
unsigned max_follow = 32; |
| 812 |
const char *p; |
| 813 |
int wd, r; |
| 814 |
|
| 815 |
assert(b); |
| 816 |
assert(b->watch_bind); |
| 817 |
assert(b->sockaddr.sa.sa_family == AF_UNIX); |
| 818 |
assert(b->sockaddr.un.sun_path[0] != 0); |
| 819 |
|
| 820 |
/* Sets up an inotify fd in case watch_bind is enabled: wait until the configured AF_UNIX file system |
| 821 |
* socket appears before connecting to it. The implemented is pretty simplistic: we just subscribe to |
| 822 |
* relevant changes to all components of the path, and every time we get an event for that we try to |
| 823 |
* reconnect again, without actually caring what precisely the event we got told us. If we still |
| 824 |
* can't connect we re-subscribe to all relevant changes of anything in the path, so that our watches |
| 825 |
* include any possibly newly created path components. */ |
| 826 |
|
| 827 |
if (b->inotify_fd < 0) { |
| 828 |
b->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); |
| 829 |
if (b->inotify_fd < 0) |
| 830 |
return -errno; |
| 831 |
|
| 832 |
b->inotify_fd = fd_move_above_stdio(b->inotify_fd); |
| 833 |
} |
| 834 |
|
| 835 |
/* Make sure the path is NUL terminated */ |
| 836 |
p = strndupa_safe(b->sockaddr.un.sun_path, |
| 837 |
sizeof(b->sockaddr.un.sun_path)); |
| 838 |
|
| 839 |
/* Make sure the path is absolute */ |
| 840 |
r = path_make_absolute_cwd(p, &absolute); |
| 841 |
if (r < 0) |
| 842 |
goto fail; |
| 843 |
|
| 844 |
/* Watch all components of the path, and don't mind any prefix that doesn't exist yet. For the |
| 845 |
* innermost directory that exists we want to know when files are created or moved into it. For all |
| 846 |
* parents of it we just care if they are removed or renamed. */ |
| 847 |
|
| 848 |
if (!GREEDY_REALLOC(new_watches, n + 1)) { |
| 849 |
r = -ENOMEM; |
| 850 |
goto fail; |
| 851 |
} |
| 852 |
|
| 853 |
/* Start with the top-level directory, which is a bit simpler than the rest, since it can't be a |
| 854 |
* symlink, and always exists */ |
| 855 |
wd = inotify_add_watch(b->inotify_fd, "/", IN_CREATE|IN_MOVED_TO); |
| 856 |
if (wd < 0) { |
| 857 |
r = log_debug_errno(errno, "Failed to add inotify watch on /: %m"); |
| 858 |
goto fail; |
| 859 |
} else |
| 860 |
new_watches[n++] = wd; |
| 861 |
|
| 862 |
for (;;) { |
| 863 |
_cleanup_free_ char *component = NULL, *prefix = NULL, *destination = NULL; |
| 864 |
size_t n_slashes, n_component; |
| 865 |
char *c = NULL; |
| 866 |
|
| 867 |
n_slashes = strspn(absolute + done, "/"); |
| 868 |
n_component = n_slashes + strcspn(absolute + done + n_slashes, "/"); |
| 869 |
|
| 870 |
if (n_component == 0) /* The end */ |
| 871 |
break; |
| 872 |
|
| 873 |
component = strndup(absolute + done, n_component); |
| 874 |
if (!component) { |
| 875 |
r = -ENOMEM; |
| 876 |
goto fail; |
| 877 |
} |
| 878 |
|
| 879 |
/* A trailing slash? That's a directory, and not a socket then */ |
| 880 |
if (path_equal(component, "/")) { |
| 881 |
r = -EISDIR; |
| 882 |
goto fail; |
| 883 |
} |
| 884 |
|
| 885 |
/* A single dot? Let's eat this up */ |
| 886 |
if (path_equal(component, "/.")) { |
| 887 |
done += n_component; |
| 888 |
continue; |
| 889 |
} |
| 890 |
|
| 891 |
prefix = strndup(absolute, done + n_component); |
| 892 |
if (!prefix) { |
| 893 |
r = -ENOMEM; |
| 894 |
goto fail; |
| 895 |
} |
| 896 |
|
| 897 |
if (!GREEDY_REALLOC(new_watches, n + 1)) { |
| 898 |
r = -ENOMEM; |
| 899 |
goto fail; |
| 900 |
} |
| 901 |
|
| 902 |
wd = inotify_add_watch(b->inotify_fd, prefix, IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_MOVED_TO|IN_DONT_FOLLOW); |
| 903 |
if (wd < 0) { |
| 904 |
if (IN_SET(errno, ENOENT, ELOOP)) |
| 905 |
break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */ |
| 906 |
|
| 907 |
r = log_debug_errno(errno, "Failed to add inotify watch on %s: %m", empty_to_root(prefix)); |
| 908 |
goto fail; |
| 909 |
} else { |
| 910 |
log_debug("Added inotify watch %i for %s on bus %s.", wd, prefix, strna(b->description)); |
| 911 |
new_watches[n++] = wd; |
| 912 |
} |
| 913 |
|
| 914 |
/* Check if this is possibly a symlink. If so, let's follow it and watch it too. */ |
| 915 |
r = readlink_malloc(prefix, &destination); |
| 916 |
if (r == -EINVAL) { /* not a symlink */ |
| 917 |
done += n_component; |
| 918 |
continue; |
| 919 |
} |
| 920 |
if (r < 0) |
| 921 |
goto fail; |
| 922 |
|
| 923 |
if (isempty(destination)) { /* Empty symlink target? Yuck! */ |
| 924 |
r = -EINVAL; |
| 925 |
goto fail; |
| 926 |
} |
| 927 |
|
| 928 |
if (max_follow <= 0) { /* Let's make sure we don't follow symlinks forever */ |
| 929 |
r = -ELOOP; |
| 930 |
goto fail; |
| 931 |
} |
| 932 |
|
| 933 |
if (path_is_absolute(destination)) { |
| 934 |
/* For absolute symlinks we build the new path and start anew */ |
| 935 |
c = strjoin(destination, absolute + done + n_component); |
| 936 |
done = 0; |
| 937 |
} else { |
| 938 |
_cleanup_free_ char *t = NULL; |
| 939 |
|
| 940 |
/* For relative symlinks we replace the last component, and try again */ |
| 941 |
t = strndup(absolute, done); |
| 942 |
if (!t) |
| 943 |
return -ENOMEM; |
| 944 |
|
| 945 |
c = strjoin(t, "/", destination, absolute + done + n_component); |
| 946 |
} |
| 947 |
if (!c) { |
| 948 |
r = -ENOMEM; |
| 949 |
goto fail; |
| 950 |
} |
| 951 |
|
| 952 |
free_and_replace(absolute, c); |
| 953 |
|
| 954 |
max_follow--; |
| 955 |
} |
| 956 |
|
| 957 |
/* And now, let's remove all watches from the previous iteration we don't need anymore */ |
| 958 |
for (i = 0; i < b->n_inotify_watches; i++) { |
| 959 |
bool found = false; |
| 960 |
size_t j; |
| 961 |
|
| 962 |
for (j = 0; j < n; j++) |
| 963 |
if (new_watches[j] == b->inotify_watches[i]) { |
| 964 |
found = true; |
| 965 |
break; |
| 966 |
} |
| 967 |
|
| 968 |
if (found) |
| 969 |
continue; |
| 970 |
|
| 971 |
(void) inotify_rm_watch(b->inotify_fd, b->inotify_watches[i]); |
| 972 |
} |
| 973 |
|
| 974 |
free_and_replace(b->inotify_watches, new_watches); |
| 975 |
b->n_inotify_watches = n; |
| 976 |
|
| 977 |
return 0; |
| 978 |
|
| 979 |
fail: |
| 980 |
bus_close_inotify_fd(b); |
| 981 |
return r; |
| 982 |
} |
| 983 |
|
| 984 |
static int bind_description(sd_bus *b, int fd, int family) { |
| 985 |
_cleanup_free_ char *bind_name = NULL, *comm = NULL; |
| 986 |
union sockaddr_union bsa; |
| 987 |
const char *d = NULL; |
| 988 |
int r; |
| 989 |
|
| 990 |
assert(b); |
| 991 |
assert(fd >= 0); |
| 992 |
|
| 993 |
/* If this is an AF_UNIX socket, let's set our client's socket address to carry the description |
| 994 |
* string for this bus connection. This is useful for debugging things, as the connection name is |
| 995 |
* visible in various socket-related tools, and can even be queried by the server side. */ |
| 996 |
|
| 997 |
if (family != AF_UNIX) |
| 998 |
return 0; |
| 999 |
|
| 1000 |
(void) sd_bus_get_description(b, &d); |
| 1001 |
|
| 1002 |
/* Generate a recognizable source address in the abstract namespace. We'll include: |
| 1003 |
* - a random 64-bit value (to avoid collisions) |
| 1004 |
* - our "comm" process name (suppressed if contains "/" to avoid parsing issues) |
| 1005 |
* - the description string of the bus connection. */ |
| 1006 |
(void) pid_get_comm(0, &comm); |
| 1007 |
if (comm && strchr(comm, '/')) |
| 1008 |
comm = mfree(comm); |
| 1009 |
|
| 1010 |
if (!d && !comm) /* skip if we don't have either field, rely on kernel autobind instead */ |
| 1011 |
return 0; |
| 1012 |
|
| 1013 |
if (asprintf(&bind_name, "@%" PRIx64 "/bus/%s/%s", random_u64(), strempty(comm), strempty(d)) < 0) |
| 1014 |
return -ENOMEM; |
| 1015 |
|
| 1016 |
strshorten(bind_name, sizeof_field(struct sockaddr_un, sun_path)); |
| 1017 |
|
| 1018 |
r = sockaddr_un_set_path(&bsa.un, bind_name); |
| 1019 |
if (r < 0) |
| 1020 |
return r; |
| 1021 |
|
| 1022 |
if (bind(fd, &bsa.sa, r) < 0) |
| 1023 |
return -errno; |
| 1024 |
|
| 1025 |
return 0; |
| 1026 |
} |
| 1027 |
|
| 1028 |
static int connect_as(int fd, const struct sockaddr *sa, socklen_t salen, uid_t uid, gid_t gid) { |
| 1029 |
_cleanup_close_pair_ int pfd[2] = EBADF_PAIR; |
| 1030 |
ssize_t n; |
| 1031 |
int r; |
| 1032 |
|
| 1033 |
/* Shortcut if we are not supposed to drop privileges */ |
| 1034 |
if (!uid_is_valid(uid) && !gid_is_valid(gid)) |
| 1035 |
return RET_NERRNO(connect(fd, sa, salen)); |
| 1036 |
|
| 1037 |
/* This changes identity to the specified uid/gid and issues connect() as that. This is useful to |
| 1038 |
* make sure SO_PEERCRED reports the selected UID/GID rather than the usual one of the caller. */ |
| 1039 |
|
| 1040 |
if (pipe2(pfd, O_CLOEXEC) < 0) |
| 1041 |
return -errno; |
| 1042 |
|
| 1043 |
r = pidref_safe_fork( |
| 1044 |
"(sd-setresuid)", |
| 1045 |
FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_WAIT, |
| 1046 |
/* ret= */ NULL); |
| 1047 |
if (r < 0) |
| 1048 |
return r; |
| 1049 |
if (r == 0) { |
| 1050 |
/* child */ |
| 1051 |
|
| 1052 |
pfd[0] = safe_close(pfd[0]); |
| 1053 |
|
| 1054 |
r = RET_NERRNO(setgroups(0, NULL)); |
| 1055 |
if (r < 0) |
| 1056 |
goto child_finish; |
| 1057 |
|
| 1058 |
if (gid_is_valid(gid)) { |
| 1059 |
r = RET_NERRNO(setresgid(gid, gid, gid)); |
| 1060 |
if (r < 0) |
| 1061 |
goto child_finish; |
| 1062 |
} |
| 1063 |
|
| 1064 |
if (uid_is_valid(uid)) { |
| 1065 |
r = RET_NERRNO(setresuid(uid, uid, uid)); |
| 1066 |
if (r < 0) |
| 1067 |
goto child_finish; |
| 1068 |
} |
| 1069 |
|
| 1070 |
r = RET_NERRNO(connect(fd, sa, salen)); |
| 1071 |
if (r < 0) |
| 1072 |
goto child_finish; |
| 1073 |
|
| 1074 |
r = 0; |
| 1075 |
|
| 1076 |
child_finish: |
| 1077 |
n = write(pfd[1], &r, sizeof(r)); |
| 1078 |
if (n != sizeof(r)) |
| 1079 |
_exit(EXIT_FAILURE); |
| 1080 |
|
| 1081 |
_exit(EXIT_SUCCESS); |
| 1082 |
} |
| 1083 |
|
| 1084 |
n = read(pfd[0], &r, sizeof(r)); |
| 1085 |
if (n != sizeof(r)) |
| 1086 |
return -EIO; |
| 1087 |
|
| 1088 |
return r; |
| 1089 |
} |
| 1090 |
|
| 1091 |
int bus_socket_connect(sd_bus *b) { |
| 1092 |
bool inotify_done = false; |
| 1093 |
int r; |
| 1094 |
|
| 1095 |
assert(b); |
| 1096 |
|
| 1097 |
for (;;) { |
| 1098 |
assert(b->input_fd < 0); |
| 1099 |
assert(b->output_fd < 0); |
| 1100 |
assert(b->sockaddr.sa.sa_family != AF_UNSPEC); |
| 1101 |
|
| 1102 |
if (DEBUG_LOGGING) { |
| 1103 |
_cleanup_free_ char *pretty = NULL; |
| 1104 |
(void) sockaddr_pretty(&b->sockaddr.sa, b->sockaddr_size, false, true, &pretty); |
| 1105 |
log_debug("sd-bus: starting bus%s%s by connecting to %s...", |
| 1106 |
b->description ? " " : "", strempty(b->description), strnull(pretty)); |
| 1107 |
} |
| 1108 |
|
| 1109 |
b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| 1110 |
if (b->input_fd < 0) |
| 1111 |
return -errno; |
| 1112 |
|
| 1113 |
r = bind_description(b, b->input_fd, b->sockaddr.sa.sa_family); |
| 1114 |
if (r < 0) |
| 1115 |
return r; |
| 1116 |
|
| 1117 |
b->input_fd = fd_move_above_stdio(b->input_fd); |
| 1118 |
|
| 1119 |
b->output_fd = b->input_fd; |
| 1120 |
bus_socket_setup(b); |
| 1121 |
|
| 1122 |
r = connect_as(b->input_fd, &b->sockaddr.sa, b->sockaddr_size, b->connect_as_uid, b->connect_as_gid); |
| 1123 |
if (r < 0) { |
| 1124 |
if (r == -EINPROGRESS) { |
| 1125 |
|
| 1126 |
/* If we have any inotify watches open, close them now, we don't need them anymore, as |
| 1127 |
* we have successfully initiated a connection */ |
| 1128 |
bus_close_inotify_fd(b); |
| 1129 |
|
| 1130 |
/* Note that very likely we are already in BUS_OPENING state here, as we enter it when |
| 1131 |
* we start parsing the address string. The only reason we set the state explicitly |
| 1132 |
* here, is to undo BUS_WATCH_BIND, in case we did the inotify magic. */ |
| 1133 |
bus_set_state(b, BUS_OPENING); |
| 1134 |
return 1; |
| 1135 |
} |
| 1136 |
|
| 1137 |
if (IN_SET(r, -ENOENT, -ECONNREFUSED) && /* ENOENT → unix socket doesn't exist at all; ECONNREFUSED → unix socket stale */ |
| 1138 |
b->watch_bind && |
| 1139 |
b->sockaddr.sa.sa_family == AF_UNIX && |
| 1140 |
b->sockaddr.un.sun_path[0] != 0) { |
| 1141 |
|
| 1142 |
/* This connection attempt failed, let's release the socket for now, and start with a |
| 1143 |
* fresh one when reconnecting. */ |
| 1144 |
bus_close_io_fds(b); |
| 1145 |
|
| 1146 |
if (inotify_done) { |
| 1147 |
/* inotify set up already, don't do it again, just return now, and remember |
| 1148 |
* that we are waiting for inotify events now. */ |
| 1149 |
bus_set_state(b, BUS_WATCH_BIND); |
| 1150 |
return 1; |
| 1151 |
} |
| 1152 |
|
| 1153 |
/* This is a file system socket, and the inotify logic is enabled. Let's create the necessary inotify fd. */ |
| 1154 |
r = bus_socket_inotify_setup(b); |
| 1155 |
if (r < 0) |
| 1156 |
return r; |
| 1157 |
|
| 1158 |
/* Let's now try to connect a second time, because in theory there's otherwise a race |
| 1159 |
* here: the socket might have been created in the time between our first connect() and |
| 1160 |
* the time we set up the inotify logic. But let's remember that we set up inotify now, |
| 1161 |
* so that we don't do the connect() more than twice. */ |
| 1162 |
inotify_done = true; |
| 1163 |
|
| 1164 |
} else |
| 1165 |
return r; |
| 1166 |
} else |
| 1167 |
break; |
| 1168 |
} |
| 1169 |
|
| 1170 |
/* Yay, established, we don't need no inotify anymore! */ |
| 1171 |
bus_close_inotify_fd(b); |
| 1172 |
|
| 1173 |
return bus_socket_start_auth(b); |
| 1174 |
} |
| 1175 |
|
| 1176 |
int bus_socket_exec(sd_bus *b) { |
| 1177 |
int s[2], r; |
| 1178 |
|
| 1179 |
assert(b); |
| 1180 |
assert(b->input_fd < 0); |
| 1181 |
assert(b->output_fd < 0); |
| 1182 |
assert(b->exec_path); |
| 1183 |
assert(!pidref_is_set(&b->busexec_pidref)); |
| 1184 |
|
| 1185 |
if (DEBUG_LOGGING) { |
| 1186 |
_cleanup_free_ char *line = NULL; |
| 1187 |
|
| 1188 |
if (b->exec_argv) |
| 1189 |
line = quote_command_line(b->exec_argv, SHELL_ESCAPE_EMPTY); |
| 1190 |
|
| 1191 |
log_debug("sd-bus: starting bus%s%s with %s%s", |
| 1192 |
b->description ? " " : "", strempty(b->description), |
| 1193 |
line ?: b->exec_path, |
| 1194 |
b->exec_argv && !line ? "…" : ""); |
| 1195 |
} |
| 1196 |
|
| 1197 |
r = socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0, s); |
| 1198 |
if (r < 0) |
| 1199 |
return -errno; |
| 1200 |
|
| 1201 |
r = pidref_safe_fork_full( |
| 1202 |
"(sd-busexec)", |
| 1203 |
(int[]) { s[1], s[1], STDERR_FILENO }, |
| 1204 |
NULL, 0, |
| 1205 |
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO|FORK_RLIMIT_NOFILE_SAFE, |
| 1206 |
&b->busexec_pidref); |
| 1207 |
if (r < 0) { |
| 1208 |
safe_close_pair(s); |
| 1209 |
return r; |
| 1210 |
} |
| 1211 |
if (r == 0) { |
| 1212 |
/* Child */ |
| 1213 |
|
| 1214 |
if (b->exec_argv) |
| 1215 |
execvp(b->exec_path, b->exec_argv); |
| 1216 |
else |
| 1217 |
execvp(b->exec_path, STRV_MAKE(b->exec_path)); |
| 1218 |
|
| 1219 |
_exit(EXIT_FAILURE); |
| 1220 |
} |
| 1221 |
|
| 1222 |
safe_close(s[1]); |
| 1223 |
b->output_fd = b->input_fd = fd_move_above_stdio(s[0]); |
| 1224 |
|
| 1225 |
bus_socket_setup(b); |
| 1226 |
|
| 1227 |
return bus_socket_start_auth(b); |
| 1228 |
} |
| 1229 |
|
| 1230 |
int bus_socket_take_fd(sd_bus *b) { |
| 1231 |
assert(b); |
| 1232 |
|
| 1233 |
bus_socket_setup(b); |
| 1234 |
|
| 1235 |
return bus_socket_start_auth(b); |
| 1236 |
} |
| 1237 |
|
| 1238 |
int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) { |
| 1239 |
struct iovec *iov; |
| 1240 |
ssize_t k; |
| 1241 |
unsigned j; |
| 1242 |
int r; |
| 1243 |
|
| 1244 |
assert(bus); |
| 1245 |
assert(m); |
| 1246 |
assert(idx); |
| 1247 |
assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)); |
| 1248 |
|
| 1249 |
if (*idx >= BUS_MESSAGE_SIZE(m)) |
| 1250 |
return 0; |
| 1251 |
|
| 1252 |
r = bus_message_setup_iovec(m); |
| 1253 |
if (r < 0) |
| 1254 |
return r; |
| 1255 |
|
| 1256 |
iov = newa(struct iovec, m->n_iovec); |
| 1257 |
memcpy_safe(iov, m->iovec, sizeof(struct iovec) * m->n_iovec); |
| 1258 |
|
| 1259 |
j = 0; |
| 1260 |
iovec_advance(iov, &j, *idx); |
| 1261 |
|
| 1262 |
if (bus->prefer_writev) |
| 1263 |
k = writev(bus->output_fd, iov, m->n_iovec); |
| 1264 |
else { |
| 1265 |
struct msghdr mh = { |
| 1266 |
.msg_iov = iov, |
| 1267 |
.msg_iovlen = m->n_iovec, |
| 1268 |
}; |
| 1269 |
|
| 1270 |
if (m->n_fds > 0 && *idx == 0) { |
| 1271 |
struct cmsghdr *control; |
| 1272 |
|
| 1273 |
mh.msg_controllen = CMSG_SPACE(sizeof(int) * m->n_fds); |
| 1274 |
mh.msg_control = alloca0(mh.msg_controllen); |
| 1275 |
control = CMSG_FIRSTHDR(&mh); |
| 1276 |
control->cmsg_len = CMSG_LEN(sizeof(int) * m->n_fds); |
| 1277 |
control->cmsg_level = SOL_SOCKET; |
| 1278 |
control->cmsg_type = SCM_RIGHTS; |
| 1279 |
memcpy(CMSG_DATA(control), m->fds, sizeof(int) * m->n_fds); |
| 1280 |
} |
| 1281 |
|
| 1282 |
k = sendmsg(bus->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); |
| 1283 |
if (k < 0 && errno == ENOTSOCK) { |
| 1284 |
bus->prefer_writev = true; |
| 1285 |
k = writev(bus->output_fd, iov, m->n_iovec); |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
if (k < 0) |
| 1290 |
return ERRNO_IS_TRANSIENT(errno) ? 0 : -errno; |
| 1291 |
|
| 1292 |
*idx += (size_t) k; |
| 1293 |
return 1; |
| 1294 |
} |
| 1295 |
|
| 1296 |
static int bus_socket_read_message_need(sd_bus *bus, size_t *need) { |
| 1297 |
uint32_t a, b; |
| 1298 |
uint8_t e; |
| 1299 |
uint64_t sum; |
| 1300 |
|
| 1301 |
assert(bus); |
| 1302 |
assert(need); |
| 1303 |
assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)); |
| 1304 |
|
| 1305 |
if (bus->rbuffer_size < sizeof(BusMessageHeader)) { |
| 1306 |
*need = sizeof(BusMessageHeader) + 8; |
| 1307 |
|
| 1308 |
/* Minimum message size: |
| 1309 |
* |
| 1310 |
* Header + |
| 1311 |
* |
| 1312 |
* Method Call: +2 string headers |
| 1313 |
* Signal: +3 string headers |
| 1314 |
* Method Error: +1 string headers |
| 1315 |
* +1 uint32 headers |
| 1316 |
* Method Reply: +1 uint32 headers |
| 1317 |
* |
| 1318 |
* A string header is at least 9 bytes |
| 1319 |
* A uint32 header is at least 8 bytes |
| 1320 |
* |
| 1321 |
* Hence the minimum message size of a valid message |
| 1322 |
* is header + 8 bytes */ |
| 1323 |
|
| 1324 |
return 0; |
| 1325 |
} |
| 1326 |
|
| 1327 |
a = ((const uint32_t*) bus->rbuffer)[1]; |
| 1328 |
b = ((const uint32_t*) bus->rbuffer)[3]; |
| 1329 |
|
| 1330 |
e = ((const uint8_t*) bus->rbuffer)[0]; |
| 1331 |
if (e == BUS_LITTLE_ENDIAN) { |
| 1332 |
a = le32toh(a); |
| 1333 |
b = le32toh(b); |
| 1334 |
} else if (e == BUS_BIG_ENDIAN) { |
| 1335 |
a = be32toh(a); |
| 1336 |
b = be32toh(b); |
| 1337 |
} else |
| 1338 |
return -EBADMSG; |
| 1339 |
|
| 1340 |
sum = (uint64_t) sizeof(BusMessageHeader) + (uint64_t) ALIGN8(b) + (uint64_t) a; |
| 1341 |
if (sum >= BUS_MESSAGE_SIZE_MAX) |
| 1342 |
return -ENOBUFS; |
| 1343 |
|
| 1344 |
*need = (size_t) sum; |
| 1345 |
return 0; |
| 1346 |
} |
| 1347 |
|
| 1348 |
static int bus_socket_make_message(sd_bus *bus, size_t size) { |
| 1349 |
sd_bus_message *t = NULL; |
| 1350 |
void *b; |
| 1351 |
int r; |
| 1352 |
|
| 1353 |
assert(bus); |
| 1354 |
assert(bus->rbuffer_size >= size); |
| 1355 |
assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)); |
| 1356 |
|
| 1357 |
r = bus_rqueue_make_room(bus); |
| 1358 |
if (r < 0) |
| 1359 |
return r; |
| 1360 |
|
| 1361 |
if (bus->rbuffer_size > size) { |
| 1362 |
b = memdup((const uint8_t*) bus->rbuffer + size, |
| 1363 |
bus->rbuffer_size - size); |
| 1364 |
if (!b) |
| 1365 |
return -ENOMEM; |
| 1366 |
} else |
| 1367 |
b = NULL; |
| 1368 |
|
| 1369 |
r = bus_message_from_malloc(bus, |
| 1370 |
bus->rbuffer, size, |
| 1371 |
bus->fds, bus->n_fds, |
| 1372 |
bus->got_ctrunc, |
| 1373 |
NULL, |
| 1374 |
&t); |
| 1375 |
if (r == -EBADMSG) { |
| 1376 |
log_debug_errno(r, "Received invalid message from connection %s, dropping.", strna(bus->description)); |
| 1377 |
free(bus->rbuffer); /* We want to drop current rbuffer and proceed with whatever remains in b */ |
| 1378 |
} else if (r < 0) { |
| 1379 |
free(b); |
| 1380 |
return r; |
| 1381 |
} |
| 1382 |
|
| 1383 |
/* rbuffer ownership was either transferred to t, or we got EBADMSG and dropped it. */ |
| 1384 |
bus->rbuffer = b; |
| 1385 |
bus->rbuffer_size -= size; |
| 1386 |
|
| 1387 |
bus->fds = NULL; |
| 1388 |
bus->n_fds = 0; |
| 1389 |
bus->got_ctrunc = false; |
| 1390 |
|
| 1391 |
if (t) { |
| 1392 |
t->read_counter = ++bus->read_counter; |
| 1393 |
bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(t, bus); |
| 1394 |
sd_bus_message_unref(t); |
| 1395 |
} |
| 1396 |
|
| 1397 |
return 1; |
| 1398 |
} |
| 1399 |
|
| 1400 |
int bus_socket_read_message(sd_bus *bus) { |
| 1401 |
size_t need; |
| 1402 |
int r; |
| 1403 |
|
| 1404 |
assert(bus); |
| 1405 |
assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)); |
| 1406 |
|
| 1407 |
r = bus_socket_read_message_need(bus, &need); |
| 1408 |
if (r < 0) |
| 1409 |
return r; |
| 1410 |
|
| 1411 |
if (bus->rbuffer_size >= need) |
| 1412 |
return bus_socket_make_message(bus, need); |
| 1413 |
|
| 1414 |
void *b = realloc(bus->rbuffer, need); |
| 1415 |
if (!b) |
| 1416 |
return -ENOMEM; |
| 1417 |
bus->rbuffer = b; |
| 1418 |
|
| 1419 |
struct iovec iov = IOVEC_MAKE((uint8_t*) bus->rbuffer + bus->rbuffer_size, need - bus->rbuffer_size); |
| 1420 |
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)) control; |
| 1421 |
struct msghdr mh; |
| 1422 |
bool handle_cmsg = false; |
| 1423 |
ssize_t k; |
| 1424 |
|
| 1425 |
if (!bus->prefer_readv) { |
| 1426 |
mh = (struct msghdr) { |
| 1427 |
.msg_iov = &iov, |
| 1428 |
.msg_iovlen = 1, |
| 1429 |
.msg_control = &control, |
| 1430 |
.msg_controllen = sizeof(control), |
| 1431 |
}; |
| 1432 |
|
| 1433 |
k = recvmsg(bus->input_fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); |
| 1434 |
if (k < 0 && errno == ENOTSOCK) |
| 1435 |
bus->prefer_readv = true; |
| 1436 |
else |
| 1437 |
handle_cmsg = true; |
| 1438 |
} |
| 1439 |
if (bus->prefer_readv) |
| 1440 |
k = readv(bus->input_fd, &iov, 1); |
| 1441 |
if (k < 0) { |
| 1442 |
if (ERRNO_IS_TRANSIENT(errno)) |
| 1443 |
return 0; |
| 1444 |
|
| 1445 |
return -errno; |
| 1446 |
} |
| 1447 |
if (k == 0) { |
| 1448 |
if (handle_cmsg) |
| 1449 |
cmsg_close_all(&mh); /* On EOF we shouldn't have gotten an fd, but let's make sure */ |
| 1450 |
return -ECONNRESET; |
| 1451 |
} |
| 1452 |
if (handle_cmsg && FLAGS_SET(mh.msg_flags, MSG_TRUNC)) { |
| 1453 |
cmsg_close_all(&mh); |
| 1454 |
return -EXFULL; |
| 1455 |
} |
| 1456 |
|
| 1457 |
/* Silence static analyzers, k is bounded by iov size: need - rbuffer_size */ |
| 1458 |
assert((size_t) k <= need - bus->rbuffer_size); |
| 1459 |
bus->rbuffer_size += k; |
| 1460 |
|
| 1461 |
if (handle_cmsg) { |
| 1462 |
r = bus_process_cmsg(bus, &mh, bus->can_fds); |
| 1463 |
if (r < 0) |
| 1464 |
return r; |
| 1465 |
} |
| 1466 |
|
| 1467 |
r = bus_socket_read_message_need(bus, &need); |
| 1468 |
if (r < 0) |
| 1469 |
return r; |
| 1470 |
|
| 1471 |
if (bus->rbuffer_size >= need) |
| 1472 |
return bus_socket_make_message(bus, need); |
| 1473 |
|
| 1474 |
return 1; |
| 1475 |
} |
| 1476 |
|
| 1477 |
int bus_socket_process_opening(sd_bus *b) { |
| 1478 |
int error = 0, events, r; |
| 1479 |
socklen_t slen = sizeof(error); |
| 1480 |
|
| 1481 |
assert(b->state == BUS_OPENING); |
| 1482 |
|
| 1483 |
events = fd_wait_for_event(b->output_fd, POLLOUT, 0); |
| 1484 |
if (ERRNO_IS_NEG_TRANSIENT(events)) |
| 1485 |
return 0; |
| 1486 |
if (events < 0) |
| 1487 |
return events; |
| 1488 |
if (!(events & (POLLOUT|POLLERR|POLLHUP))) |
| 1489 |
return 0; |
| 1490 |
|
| 1491 |
r = getsockopt(b->output_fd, SOL_SOCKET, SO_ERROR, &error, &slen); |
| 1492 |
if (r < 0) |
| 1493 |
b->last_connect_error = errno; |
| 1494 |
else if (error != 0) |
| 1495 |
b->last_connect_error = error; |
| 1496 |
else if (events & (POLLERR|POLLHUP)) |
| 1497 |
b->last_connect_error = ECONNREFUSED; |
| 1498 |
else |
| 1499 |
return bus_socket_start_auth(b); |
| 1500 |
|
| 1501 |
return bus_next_address(b); |
| 1502 |
} |
| 1503 |
|
| 1504 |
int bus_socket_process_authenticating(sd_bus *b) { |
| 1505 |
int r; |
| 1506 |
|
| 1507 |
assert(b); |
| 1508 |
assert(b->state == BUS_AUTHENTICATING); |
| 1509 |
|
| 1510 |
if (now(CLOCK_MONOTONIC) >= b->auth_timeout) |
| 1511 |
return -ETIMEDOUT; |
| 1512 |
|
| 1513 |
r = bus_socket_write_auth(b); |
| 1514 |
if (r != 0) |
| 1515 |
return r; |
| 1516 |
|
| 1517 |
return bus_socket_read_auth(b); |
| 1518 |
} |
| 1519 |
|
| 1520 |
int bus_socket_process_watch_bind(sd_bus *b) { |
| 1521 |
int r, q; |
| 1522 |
|
| 1523 |
assert(b); |
| 1524 |
assert(b->state == BUS_WATCH_BIND); |
| 1525 |
assert(b->inotify_fd >= 0); |
| 1526 |
|
| 1527 |
r = flush_fd(b->inotify_fd); |
| 1528 |
if (r <= 0) |
| 1529 |
return r; |
| 1530 |
|
| 1531 |
log_debug("Got inotify event on bus %s.", strna(b->description)); |
| 1532 |
|
| 1533 |
/* We flushed events out of the inotify fd. In that case, maybe the socket is valid now? Let's try to connect |
| 1534 |
* to it again */ |
| 1535 |
|
| 1536 |
r = bus_socket_connect(b); |
| 1537 |
if (r < 0) |
| 1538 |
return r; |
| 1539 |
|
| 1540 |
q = bus_attach_io_events(b); |
| 1541 |
if (q < 0) |
| 1542 |
return q; |
| 1543 |
|
| 1544 |
q = bus_attach_inotify_event(b); |
| 1545 |
if (q < 0) |
| 1546 |
return q; |
| 1547 |
|
| 1548 |
return r; |
| 1549 |
} |