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