• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 23030009067

12 Mar 2026 09:53PM UTC coverage: 72.421% (-0.09%) from 72.513%
23030009067

push

github

bluca
portable: avoid passing through ID/version fields to LogExtraFields= when they contain control characters

Found by Claude Code Review.

Follow-up for e8114a4f8

2 of 4 new or added lines in 1 file covered. (50.0%)

1362 existing lines in 42 files now uncovered.

315393 of 435501 relevant lines covered (72.42%)

1215652.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.16
/src/basic/socket-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <linux/if.h>
5
#include <linux/if_arp.h>
6
#include <mqueue.h>
7
#include <net/if.h>
8
#include <netdb.h>
9
#include <netinet/ip.h>
10
#include <poll.h>
11
#include <stdio.h>
12
#include <sys/ioctl.h>
13
#include <unistd.h>
14

15
#include "alloc-util.h"
16
#include "errno-util.h"
17
#include "escape.h"
18
#include "fd-util.h"
19
#include "format-ifname.h"
20
#include "format-util.h"
21
#include "in-addr-util.h"
22
#include "io-util.h"
23
#include "log.h"
24
#include "memory-util.h"
25
#include "parse-util.h"
26
#include "path-util.h"
27
#include "pidref.h"
28
#include "process-util.h"
29
#include "random-util.h"
30
#include "socket-util.h"
31
#include "sparse-endian.h"
32
#include "string-table.h"
33
#include "string-util.h"
34
#include "strv.h"
35
#include "sysctl-util.h"
36

37
#if ENABLE_IDN
38
#  define IDN_FLAGS NI_IDN
39
#else
40
#  define IDN_FLAGS 0
41
#endif
42

43
static const char* const socket_address_type_table[] = {
44
        [SOCK_STREAM] =    "Stream",
45
        [SOCK_DGRAM] =     "Datagram",
46
        [SOCK_RAW] =       "Raw",
47
        [SOCK_RDM] =       "ReliableDatagram",
48
        [SOCK_SEQPACKET] = "SequentialPacket",
49
        [SOCK_DCCP] =      "DatagramCongestionControl",
50
};
51

52
DEFINE_STRING_TABLE_LOOKUP(socket_address_type, int);
×
53

54
int socket_address_verify(const SocketAddress *a, bool strict) {
10,783✔
55
        assert(a);
10,783✔
56

57
        /* With 'strict' we enforce additional sanity constraints which are not set by the standard,
58
         * but should only apply to sockets we create ourselves. */
59

60
        switch (socket_address_family(a)) {
10,783✔
61

62
        case AF_INET:
27✔
63
                if (a->size != sizeof(struct sockaddr_in))
27✔
64
                        return -EINVAL;
65

66
                if (a->sockaddr.in.sin_port == 0)
27✔
67
                        return -EINVAL;
68

69
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
27✔
70
                        return -EINVAL;
1✔
71

72
                return 0;
73

74
        case AF_INET6:
31✔
75
                if (a->size != sizeof(struct sockaddr_in6))
31✔
76
                        return -EINVAL;
77

78
                if (a->sockaddr.in6.sin6_port == 0)
31✔
79
                        return -EINVAL;
80

81
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
31✔
82
                        return -EINVAL;
×
83

84
                return 0;
85

86
        case AF_UNIX:
10,275✔
87
                if (a->size < offsetof(struct sockaddr_un, sun_path))
10,275✔
88
                        return -EINVAL;
89
                if (a->size > sizeof(struct sockaddr_un) + !strict)
10,275✔
90
                        /* If !strict, allow one extra byte, since getsockname() on Linux will append
91
                         * a NUL byte if we have path sockets that are above sun_path's full size. */
92
                        return -EINVAL;
93

94
                if (a->size > offsetof(struct sockaddr_un, sun_path) &&
10,275✔
95
                    a->sockaddr.un.sun_path[0] != 0 &&
10,275✔
96
                    strict) {
97
                        /* Only validate file system sockets here, and only in strict mode */
98
                        const char *e;
3,494✔
99

100
                        e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path));
3,494✔
101
                        if (e) {
3,494✔
102
                                /* If there's an embedded NUL byte, make sure the size of the socket address matches it */
103
                                if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1)
3,494✔
104
                                        return -EINVAL;
105
                        } else {
106
                                /* If there's no embedded NUL byte, then the size needs to match the whole
107
                                 * structure or the structure with one extra NUL byte suffixed. (Yeah, Linux is awful,
108
                                 * and considers both equivalent: getsockname() even extends sockaddr_un beyond its
109
                                 * size if the path is non NUL terminated.) */
110
                                if (!IN_SET(a->size, sizeof(a->sockaddr.un.sun_path), sizeof(a->sockaddr.un.sun_path)+1))
×
111
                                        return -EINVAL;
112
                        }
113
                }
114

115
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET))
10,275✔
116
                        return -EINVAL;
×
117

118
                return 0;
119

120
        case AF_NETLINK:
364✔
121

122
                if (a->size != sizeof(struct sockaddr_nl))
364✔
123
                        return -EINVAL;
124

125
                if (!IN_SET(a->type, 0, SOCK_RAW, SOCK_DGRAM))
364✔
126
                        return -EINVAL;
×
127

128
                return 0;
129

130
        case AF_VSOCK:
69✔
131
                if (a->size != sizeof(struct sockaddr_vm))
69✔
132
                        return -EINVAL;
133

134
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
69✔
135
                        return -EINVAL;
×
136

137
                return 0;
138

139
        default:
140
                return -EAFNOSUPPORT;
141
        }
142
}
143

144
int socket_address_print(const SocketAddress *a, char **ret) {
2,458✔
145
        int r;
2,458✔
146

147
        assert(a);
2,458✔
148
        assert(ret);
2,458✔
149

150
        r = socket_address_verify(a, false); /* We do non-strict validation, because we want to be
2,458✔
151
                                              * able to pretty-print any socket the kernel considers
152
                                              * valid. We still need to do validation to know if we
153
                                              * can meaningfully print the address. */
154
        if (r < 0)
2,458✔
155
                return r;
156

157
        if (socket_address_family(a) == AF_NETLINK) {
2,458✔
158
                _cleanup_free_ char *sfamily = NULL;
125✔
159

160
                r = netlink_family_to_string_alloc(a->protocol, &sfamily);
125✔
161
                if (r < 0)
125✔
162
                        return r;
163

164
                r = asprintf(ret, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
125✔
165
                if (r < 0)
125✔
166
                        return -ENOMEM;
167

168
                return 0;
125✔
169
        }
170

171
        return sockaddr_pretty(&a->sockaddr.sa, a->size, false, true, ret);
2,333✔
172
}
173

174
bool socket_address_can_accept(const SocketAddress *a) {
7,562✔
175
        assert(a);
7,562✔
176

177
        return
7,562✔
178
                IN_SET(a->type, SOCK_STREAM, SOCK_SEQPACKET);
7,562✔
179
}
180

181
bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
2,355✔
182
        assert(a);
2,355✔
183
        assert(b);
2,355✔
184

185
        /* Invalid addresses are unequal to all */
186
        if (socket_address_verify(a, false) < 0 ||
4,710✔
187
            socket_address_verify(b, false) < 0)
2,355✔
188
                return false;
189

190
        if (a->type != b->type)
2,354✔
191
                return false;
192

193
        if (socket_address_family(a) != socket_address_family(b))
2,353✔
194
                return false;
195

196
        switch (socket_address_family(a)) {
2,351✔
197

198
        case AF_INET:
6✔
199
                if (a->sockaddr.in.sin_addr.s_addr != b->sockaddr.in.sin_addr.s_addr)
6✔
200
                        return false;
201

202
                if (a->sockaddr.in.sin_port != b->sockaddr.in.sin_port)
5✔
203
                        return false;
1✔
204

205
                break;
206

207
        case AF_INET6:
2✔
208
                if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0)
2✔
209
                        return false;
210

211
                if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port)
2✔
212
                        return false;
×
213

214
                break;
215

216
        case AF_UNIX:
2,238✔
217
                if (a->size <= offsetof(struct sockaddr_un, sun_path) ||
2,238✔
218
                    b->size <= offsetof(struct sockaddr_un, sun_path))
2,238✔
219
                        return false;
220

221
                if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0))
2,238✔
222
                        return false;
223

224
                if (a->sockaddr.un.sun_path[0]) {
2,083✔
225
                        if (!path_equal_or_inode_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0))
2,081✔
226
                                return false;
254✔
227
                } else {
228
                        if (a->size != b->size)
2✔
229
                                return false;
230

231
                        if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
2✔
232
                                return false;
×
233
                }
234

235
                break;
236

237
        case AF_NETLINK:
86✔
238
                if (a->protocol != b->protocol)
86✔
239
                        return false;
240

241
                if (a->sockaddr.nl.nl_groups != b->sockaddr.nl.nl_groups)
86✔
242
                        return false;
1✔
243

244
                break;
245

246
        case AF_VSOCK:
19✔
247
                if (a->sockaddr.vm.svm_cid != b->sockaddr.vm.svm_cid)
19✔
248
                        return false;
249

250
                if (a->sockaddr.vm.svm_port != b->sockaddr.vm.svm_port)
18✔
251
                        return false;
1✔
252

253
                break;
254

255
        default:
256
                /* Cannot compare, so we assume the addresses are different */
257
                return false;
258
        }
259

260
        return true;
261
}
262

263
const char* socket_address_get_path(const SocketAddress *a) {
18,287✔
264
        assert(a);
18,287✔
265

266
        if (socket_address_family(a) != AF_UNIX)
18,287✔
267
                return NULL;
268

269
        if (a->sockaddr.un.sun_path[0] == 0)
17,755✔
270
                return NULL;
271

272
        /* Note that this is only safe because we know that there's an extra NUL byte after the sockaddr_un
273
         * structure. On Linux AF_UNIX file system socket addresses don't have to be NUL terminated if they take up the
274
         * full sun_path space. */
275
        assert_cc(sizeof(union sockaddr_union) >= sizeof(struct sockaddr_un)+1);
17,754✔
276
        return a->sockaddr.un.sun_path;
17,754✔
277
}
278

279
bool socket_ipv6_is_supported(void) {
150,360✔
280
        static int cached = -1;
150,360✔
281

282
        if (cached < 0) {
150,360✔
283

284
                if (access("/proc/net/if_inet6", F_OK) < 0) {
1,111✔
285

286
                        if (errno != ENOENT) {
×
287
                                log_debug_errno(errno, "Unexpected error when checking whether /proc/net/if_inet6 exists: %m");
×
288
                                return false;
×
289
                        }
290

291
                        cached = false;
×
292
                } else
293
                        cached = true;
1,111✔
294
        }
295

296
        return cached;
150,360✔
297
}
298

299
bool socket_ipv6_is_enabled(void) {
110,774✔
300
        _cleanup_free_ char *v = NULL;
110,774✔
301
        int r;
110,774✔
302

303
        /* Much like socket_ipv6_is_supported(), but also checks that the sysctl that disables IPv6 on all
304
         * interfaces isn't turned on */
305

306
        if (!socket_ipv6_is_supported())
110,774✔
307
                return false;
308

309
        r = sysctl_read_ip_property(AF_INET6, "all", "disable_ipv6", &v);
110,774✔
310
        if (r < 0) {
110,774✔
311
                log_debug_errno(r, "Unexpected error reading 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
×
312
                return true;
×
313
        }
314

315
        r = parse_boolean(v);
110,774✔
316
        if (r < 0) {
110,774✔
317
                log_debug_errno(r, "Failed to pare 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
×
318
                return true;
×
319
        }
320

321
        return !r;
110,774✔
322
}
323

324
bool socket_address_matches_fd(const SocketAddress *a, int fd) {
522✔
325
        SocketAddress b;
522✔
326
        socklen_t solen;
522✔
327

328
        assert(a);
522✔
329
        assert(fd >= 0);
522✔
330

331
        b.size = sizeof(b.sockaddr);
522✔
332
        if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0)
522✔
333
                return false;
522✔
334

335
        if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family)
522✔
336
                return false;
337

338
        solen = sizeof(b.type);
472✔
339
        if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &b.type, &solen) < 0)
472✔
340
                return false;
341

342
        if (b.type != a->type)
472✔
343
                return false;
344

345
        if (a->protocol != 0)  {
409✔
346
                solen = sizeof(b.protocol);
×
347
                if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &b.protocol, &solen) < 0)
×
348
                        return false;
349

350
                if (b.protocol != a->protocol)
×
351
                        return false;
352
        }
353

354
        return socket_address_equal(a, &b);
409✔
355
}
356

357
int sockaddr_port(const struct sockaddr *_sa, unsigned *ret_port) {
×
358
        const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
×
359

360
        /* Note, this returns the port as 'unsigned' rather than 'uint16_t', as AF_VSOCK knows larger ports */
361

362
        assert(sa);
×
363

364
        switch (sa->sa.sa_family) {
×
365

366
        case AF_INET:
×
367
                *ret_port = be16toh(sa->in.sin_port);
×
368
                return 0;
×
369

370
        case AF_INET6:
×
371
                *ret_port = be16toh(sa->in6.sin6_port);
×
372
                return 0;
×
373

374
        case AF_VSOCK:
×
375
                *ret_port = sa->vm.svm_port;
×
376
                return 0;
×
377

378
        default:
379
                return -EAFNOSUPPORT;
380
        }
381
}
382

383
const union in_addr_union *sockaddr_in_addr(const struct sockaddr *_sa) {
305✔
384
        const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
305✔
385

386
        if (!sa)
305✔
387
                return NULL;
388

389
        switch (sa->sa.sa_family) {
305✔
390

391
        case AF_INET:
263✔
392
                return (const union in_addr_union*) &sa->in.sin_addr;
263✔
393

394
        case AF_INET6:
42✔
395
                return (const union in_addr_union*) &sa->in6.sin6_addr;
42✔
396

397
        default:
398
                return NULL;
399
        }
400
}
401

402
int sockaddr_set_in_addr(
1,106✔
403
                union sockaddr_union *u,
404
                int family,
405
                const union in_addr_union *a,
406
                uint16_t port) {
407

408
        assert(u);
1,106✔
409
        assert(a);
1,106✔
410

411
        switch (family) {
1,106✔
412

413
        case AF_INET:
1,012✔
414
                u->in = (struct sockaddr_in) {
1,012✔
415
                        .sin_family = AF_INET,
416
                        .sin_addr = a->in,
1,012✔
417
                        .sin_port = htobe16(port),
1,012✔
418
                };
419

420
                return 0;
1,012✔
421

422
        case AF_INET6:
94✔
423
                u->in6 = (struct sockaddr_in6) {
94✔
424
                        .sin6_family = AF_INET6,
425
                        .sin6_addr = a->in6,
94✔
426
                        .sin6_port = htobe16(port),
94✔
427
                };
428

429
                return 0;
94✔
430

431
        default:
432
                return -EAFNOSUPPORT;
433

434
        }
435
}
436

437
int sockaddr_pretty(
10,894✔
438
                const struct sockaddr *_sa,
439
                socklen_t salen,
440
                bool translate_ipv6,
441
                bool include_port,
442
                char **ret) {
443

444
        union sockaddr_union *sa = (union sockaddr_union*) _sa;
10,894✔
445
        char *p;
10,894✔
446
        int r;
10,894✔
447

448
        assert(sa);
10,894✔
449
        assert(salen >= sizeof(sa->sa.sa_family));
10,894✔
450
        assert(ret);
10,894✔
451

452
        switch (sa->sa.sa_family) {
10,894✔
453

454
        case AF_INET: {
121✔
455
                uint32_t a;
121✔
456

457
                a = be32toh(sa->in.sin_addr.s_addr);
121✔
458

459
                if (include_port)
121✔
460
                        r = asprintf(&p,
121✔
461
                                     "%u.%u.%u.%u:%u",
462
                                     a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
463
                                     be16toh(sa->in.sin_port));
121✔
464
                else
465
                        r = asprintf(&p,
×
466
                                     "%u.%u.%u.%u",
467
                                     a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF);
468
                if (r < 0)
121✔
469
                        return -ENOMEM;
10,894✔
470
                break;
471
        }
472

473
        case AF_INET6: {
18✔
474
                static const unsigned char ipv4_prefix[] = {
18✔
475
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
476
                };
477

478
                if (translate_ipv6 &&
18✔
479
                    memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
9✔
480
                        const uint8_t *a = sa->in6.sin6_addr.s6_addr+12;
×
481
                        if (include_port)
×
482
                                r = asprintf(&p,
×
483
                                             "%u.%u.%u.%u:%u",
484
                                             a[0], a[1], a[2], a[3],
×
485
                                             be16toh(sa->in6.sin6_port));
×
486
                        else
487
                                r = asprintf(&p,
×
488
                                             "%u.%u.%u.%u",
489
                                             a[0], a[1], a[2], a[3]);
×
490
                        if (r < 0)
×
491
                                return -ENOMEM;
492
                } else {
493
                        const char *a = IN6_ADDR_TO_STRING(&sa->in6.sin6_addr);
18✔
494

495
                        if (include_port) {
18✔
496
                                if (asprintf(&p,
17✔
497
                                             "[%s]:%u%s%s",
498
                                             a,
499
                                             be16toh(sa->in6.sin6_port),
17✔
500
                                             sa->in6.sin6_scope_id != 0 ? "%" : "",
17✔
501
                                             FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX)) < 0)
17✔
502
                                        return -ENOMEM;
×
503
                        } else {
504
                                if (sa->in6.sin6_scope_id != 0)
1✔
505
                                        p = strjoin(a, "%", FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX));
×
506
                                else
507
                                        p = strdup(a);
1✔
508
                                if (!p)
1✔
509
                                        return -ENOMEM;
510
                        }
511
                }
512

513
                break;
514
        }
515

516
        case AF_UNIX:
10,733✔
517
                if (salen <= offsetof(struct sockaddr_un, sun_path) ||
10,733✔
518
                    (sa->un.sun_path[0] == 0 && salen == offsetof(struct sockaddr_un, sun_path) + 1))
10,629✔
519
                        /* The name must have at least one character (and the leading NUL does not count) */
520
                        p = strdup("<unnamed>");
106✔
521
                else {
522
                        /* Note that we calculate the path pointer here through the .un_buffer[] field, in order to
523
                         * outtrick bounds checking tools such as ubsan, which are too smart for their own good: on
524
                         * Linux the kernel may return sun_path[] data one byte longer than the declared size of the
525
                         * field. */
526
                        char *path = (char*) sa->un_buffer + offsetof(struct sockaddr_un, sun_path);
10,627✔
527
                        size_t path_len = salen - offsetof(struct sockaddr_un, sun_path);
10,627✔
528

529
                        if (path[0] == 0) {
10,627✔
530
                                /* Abstract socket. When parsing address information from, we
531
                                 * explicitly reject overly long paths and paths with embedded NULs.
532
                                 * But we might get such a socket from the outside. Let's return
533
                                 * something meaningful and printable in this case. */
534

535
                                _cleanup_free_ char *e = NULL;
8✔
536

537
                                e = cescape_length(path + 1, path_len - 1);
8✔
538
                                if (!e)
8✔
539
                                        return -ENOMEM;
×
540

541
                                p = strjoin("@", e);
8✔
542
                        } else {
543
                                if (path[path_len - 1] == '\0')
10,619✔
544
                                        /* We expect a terminating NUL and don't print it */
545
                                        path_len--;
10,618✔
546

547
                                p = cescape_length(path, path_len);
10,619✔
548
                        }
549
                }
550
                if (!p)
10,733✔
551
                        return -ENOMEM;
552

553
                break;
554

555
        case AF_VSOCK:
22✔
556
                if (include_port) {
22✔
557
                        if (sa->vm.svm_cid == VMADDR_CID_ANY)
22✔
558
                                r = asprintf(&p, "vsock::%u", sa->vm.svm_port);
19✔
559
                        else
560
                                r = asprintf(&p, "vsock:%u:%u", sa->vm.svm_cid, sa->vm.svm_port);
3✔
561
                } else
562
                        r = asprintf(&p, "vsock:%u", sa->vm.svm_cid);
×
563
                if (r < 0)
22✔
564
                        return -ENOMEM;
565
                break;
566

567
        default:
568
                return -EOPNOTSUPP;
569
        }
570

571
        *ret = p;
10,894✔
572
        return 0;
10,894✔
573
}
574

575
int getpeername_pretty(int fd, bool include_port, char **ret) {
105✔
576
        union sockaddr_union sa;
105✔
577
        socklen_t salen = sizeof(sa);
105✔
578
        int r;
105✔
579

580
        assert(fd >= 0);
105✔
581
        assert(ret);
105✔
582

583
        if (getpeername(fd, &sa.sa, &salen) < 0)
105✔
584
                return -errno;
×
585

586
        if (sa.sa.sa_family == AF_UNIX) {
105✔
587
                struct ucred ucred = UCRED_INVALID;
104✔
588

589
                /* UNIX connection sockets are anonymous, so let's use
590
                 * PID/UID as pretty credentials instead */
591

592
                r = getpeercred(fd, &ucred);
104✔
593
                if (r < 0)
104✔
594
                        return r;
104✔
595

596
                if (asprintf(ret, "PID "PID_FMT"/UID "UID_FMT, ucred.pid, ucred.uid) < 0)
104✔
597
                        return -ENOMEM;
598

599
                return 0;
104✔
600
        }
601

602
        /* For remote sockets we translate IPv6 addresses back to IPv4
603
         * if applicable, since that's nicer. */
604

605
        return sockaddr_pretty(&sa.sa, salen, true, include_port, ret);
1✔
606
}
607

608
int getsockname_pretty(int fd, char **ret) {
2✔
609
        union sockaddr_union sa;
2✔
610
        socklen_t salen = sizeof(sa);
2✔
611

612
        assert(fd >= 0);
2✔
613
        assert(ret);
2✔
614

615
        if (getsockname(fd, &sa.sa, &salen) < 0)
2✔
616
                return -errno;
×
617

618
        /* For local sockets we do not translate IPv6 addresses back
619
         * to IPv6 if applicable, since this is usually used for
620
         * listening sockets where the difference between IPv4 and
621
         * IPv6 matters. */
622

623
        return sockaddr_pretty(&sa.sa, salen, false, true, ret);
2✔
624
}
625

626
int socknameinfo_pretty(const struct sockaddr *sa, socklen_t salen, char **ret) {
×
627
        char host[NI_MAXHOST];
×
628
        int r;
×
629

630
        assert(sa);
×
631
        assert(salen >= sizeof(sa_family_t));
×
632
        assert(ret);
×
633

634
        r = getnameinfo(sa, salen, host, sizeof(host), /* service= */ NULL, /* service_len= */ 0, IDN_FLAGS);
×
635
        if (r != 0) {
×
636
                if (r == EAI_MEMORY)
×
637
                        return log_oom_debug();
×
638
                if (r == EAI_SYSTEM)
×
639
                        log_debug_errno(errno, "getnameinfo() failed, ignoring: %m");
×
640
                else
641
                        log_debug("getnameinfo() failed, ignoring: %s", gai_strerror(r));
×
642

643
                return sockaddr_pretty(sa, salen, /* translate_ipv6= */ true, /* include_port= */ true, ret);
×
644
        }
645

646
        return strdup_to(ret, host);
×
647
}
648

649
static const char* const netlink_family_table[] = {
650
        [NETLINK_ROUTE]          = "route",
651
        [NETLINK_FIREWALL]       = "firewall",
652
        [NETLINK_INET_DIAG]      = "inet-diag",
653
        [NETLINK_NFLOG]          = "nflog",
654
        [NETLINK_XFRM]           = "xfrm",
655
        [NETLINK_SELINUX]        = "selinux",
656
        [NETLINK_ISCSI]          = "iscsi",
657
        [NETLINK_AUDIT]          = "audit",
658
        [NETLINK_FIB_LOOKUP]     = "fib-lookup",
659
        [NETLINK_CONNECTOR]      = "connector",
660
        [NETLINK_NETFILTER]      = "netfilter",
661
        [NETLINK_IP6_FW]         = "ip6-fw",
662
        [NETLINK_DNRTMSG]        = "dnrtmsg",
663
        [NETLINK_KOBJECT_UEVENT] = "kobject-uevent",
664
        [NETLINK_GENERIC]        = "generic",
665
        [NETLINK_SCSITRANSPORT]  = "scsitransport",
666
        [NETLINK_ECRYPTFS]       = "ecryptfs",
667
        [NETLINK_RDMA]           = "rdma",
668
};
669

670
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX);
561✔
671

672
bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b) {
726✔
673
        assert(a);
726✔
674
        assert(b);
726✔
675

676
        if (a->sa.sa_family != b->sa.sa_family)
726✔
677
                return false;
678

679
        if (a->sa.sa_family == AF_INET)
725✔
680
                return a->in.sin_addr.s_addr == b->in.sin_addr.s_addr;
4✔
681

682
        if (a->sa.sa_family == AF_INET6)
721✔
683
                return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr)) == 0;
1✔
684

685
        if (a->sa.sa_family == AF_VSOCK)
720✔
686
                return a->vm.svm_cid == b->vm.svm_cid;
1✔
687

688
        return false;
689
}
690

691
int fd_set_sndbuf(int fd, size_t n, bool increase) {
424,659✔
692
        int r, value;
424,659✔
693
        socklen_t l = sizeof(value);
424,659✔
694

695
        if (n > INT_MAX)
424,659✔
696
                return -ERANGE;
424,659✔
697

698
        r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
424,659✔
699
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
424,659✔
700
                return 0;
701

702
        /* First, try to set the buffer size with SO_SNDBUF. */
703
        r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUF, n);
424,659✔
704
        if (r < 0)
424,659✔
705
                return r;
706

707
        /* SO_SNDBUF above may set to the kernel limit, instead of the requested size.
708
         * So, we need to check the actual buffer size here. */
709
        l = sizeof(value);
424,659✔
710
        r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
424,659✔
711
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
424,659✔
712
                return 1;
713

714
        /* If we have the privileges we will ignore the kernel limit. */
715
        r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUFFORCE, n);
423,840✔
716
        if (r < 0)
423,840✔
717
                return r;
31,493✔
718

719
        return 1;
720
}
721

722
int fd_set_rcvbuf(int fd, size_t n, bool increase) {
33,413✔
723
        int r, value;
33,413✔
724
        socklen_t l = sizeof(value);
33,413✔
725

726
        if (n > INT_MAX)
33,413✔
727
                return -ERANGE;
33,413✔
728

729
        r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
33,413✔
730
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
33,413✔
731
                return 0;
732

733
        /* First, try to set the buffer size with SO_RCVBUF. */
734
        r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUF, n);
33,412✔
735
        if (r < 0)
33,412✔
736
                return r;
737

738
        /* SO_RCVBUF above may set to the kernel limit, instead of the requested size.
739
         * So, we need to check the actual buffer size here. */
740
        l = sizeof(value);
33,412✔
741
        r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
33,412✔
742
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
33,412✔
743
                return 1;
744

745
        /* If we have the privileges we will ignore the kernel limit. */
746
        r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUFFORCE, n);
32,594✔
747
        if (r < 0)
32,594✔
748
                return r;
6,337✔
749

750
        return 1;
751
}
752

753
static const char* const ip_tos_table[] = {
754
        [IPTOS_LOWDELAY]    = "low-delay",
755
        [IPTOS_THROUGHPUT]  = "throughput",
756
        [IPTOS_RELIABILITY] = "reliability",
757
        [IPTOS_LOWCOST]     = "low-cost",
758
};
759

760
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
5✔
761

762
bool ifname_valid_char(char a) {
491,766✔
763
        if ((unsigned char) a >= 127U)
491,766✔
764
                return false;
765

766
        if ((unsigned char) a <= 32U)
491,766✔
767
                return false;
768

769
        if (IN_SET(a,
491,762✔
770
                   ':',  /* colons are used by the legacy "alias" interface logic */
771
                   '/',  /* slashes cannot work, since we need to use network interfaces in sysfs paths, and in paths slashes are separators */
772
                   '%')) /* %d is used in the kernel's weird foo%d format string naming feature which we really really don't want to ever run into by accident */
773
                return false;
10✔
774

775
        return true;
776
}
777

778
bool ifname_valid_full(const char *p, IfnameValidFlags flags) {
140,489✔
779
        bool numeric = true;
140,489✔
780

781
        /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
782
         * but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We
783
         * also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */
784

785
        assert(!(flags & ~_IFNAME_VALID_ALL));
140,489✔
786

787
        if (isempty(p))
140,489✔
788
                return false;
789

790
        /* A valid ifindex? If so, it's valid iff IFNAME_VALID_NUMERIC is set */
791
        if (parse_ifindex(p) >= 0)
135,632✔
792
                return flags & IFNAME_VALID_NUMERIC;
57✔
793

794
        if (flags & IFNAME_VALID_ALTERNATIVE) {
135,575✔
795
                if (strlen(p) >= ALTIFNAMSIZ)
13,472✔
796
                        return false;
797
        } else {
798
                if (strlen(p) >= IFNAMSIZ)
122,103✔
799
                        return false;
800
        }
801

802
        if (dot_or_dot_dot(p))
135,568✔
803
                return false;
804

805
        /* Let's refuse "all" and "default" as interface name, to avoid collisions with the special sysctl
806
         * directories /proc/sys/net/{ipv4,ipv6}/conf/{all,default} */
807
        if (!FLAGS_SET(flags, IFNAME_VALID_SPECIAL) && STR_IN_SET(p, "all", "default"))
135,566✔
808
                return false;
×
809

810
        for (const char *t = p; *t; t++) {
620,056✔
811
                if (!ifname_valid_char(*t))
484,504✔
812
                        return false;
813

814
                numeric = numeric && ascii_isdigit(*t);
620,046✔
815
        }
816

817
        /* It's fully numeric but didn't parse as valid ifindex above? if so, it must be too large or zero or
818
         * so, let's refuse that. */
819
        if (numeric)
135,552✔
820
                return false;
2✔
821

822
        return true;
823
}
824

825
bool address_label_valid(const char *p) {
44✔
826

827
        if (isempty(p))
44✔
828
                return false;
829

830
        if (strlen(p) >= IFNAMSIZ)
44✔
831
                return false;
832

833
        while (*p) {
376✔
834
                if ((uint8_t) *p >= 127U)
332✔
835
                        return false;
836

837
                if ((uint8_t) *p <= 31U)
332✔
838
                        return false;
839
                p++;
332✔
840
        }
841

842
        return true;
843
}
844

845
int getpeercred(int fd, struct ucred *ucred) {
85,214✔
846
        socklen_t n = sizeof(struct ucred);
85,214✔
847
        struct ucred u;
85,214✔
848

849
        assert(fd >= 0);
85,214✔
850
        assert(ucred);
85,214✔
851

852
        if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n) < 0)
85,214✔
853
                return -errno;
×
854

855
        if (n != sizeof(struct ucred))
85,214✔
856
                return -EIO;
857

858
        /* Check if the data is actually useful and not suppressed due to namespacing issues */
859
        if (!pid_is_valid(u.pid))
85,214✔
860
                return -ENODATA;
861

862
        /* Note that we don't check UID/GID here, as namespace translation works differently there: instead of
863
         * receiving in "invalid" user/group we get the overflow UID/GID. */
864

865
        *ucred = u;
85,214✔
866
        return 0;
85,214✔
867
}
868

869
int getpeersec(int fd, char **ret) {
19,354✔
870
        _cleanup_free_ char *s = NULL;
38,708✔
871
        socklen_t n = 64;
19,354✔
872

873
        assert(fd >= 0);
19,354✔
874
        assert(ret);
19,354✔
875

876
        for (;;) {
19,354✔
877
                s = new0(char, n+1);
19,354✔
878
                if (!s)
19,354✔
879
                        return -ENOMEM;
880

881
                if (getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n) >= 0) {
19,354✔
882
                        s[n] = 0;
7,766✔
883
                        break;
7,766✔
884
                }
885

886
                if (errno != ERANGE)
11,588✔
887
                        return -errno;
11,588✔
888

889
                s = mfree(s);
×
890
        }
891

892
        if (isempty(s))
7,766✔
893
                return -EOPNOTSUPP;
894

895
        *ret = TAKE_PTR(s);
7,766✔
896

897
        return 0;
7,766✔
898
}
899

900
int getpeergroups(int fd, gid_t **ret) {
19,355✔
901
        socklen_t n = sizeof(gid_t) * 64U;
19,355✔
902
        _cleanup_free_ gid_t *d = NULL;
19,355✔
903

904
        assert(fd >= 0);
19,355✔
905
        assert(ret);
19,355✔
906

907
        long ngroups_max = sysconf(_SC_NGROUPS_MAX);
19,355✔
908
        if (ngroups_max > 0)
19,355✔
909
                n = MAX(n, sizeof(gid_t) * (socklen_t) ngroups_max);
19,355✔
910

911
        for (;;) {
19,355✔
912
                d = malloc(n);
19,355✔
913
                if (!d)
19,355✔
914
                        return -ENOMEM;
915

916
                if (getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, d, &n) >= 0)
19,355✔
917
                        break;
918

919
                if (errno != ERANGE)
×
920
                        return -errno;
×
921

922
                d = mfree(d);
×
923
        }
924

925
        assert_se(n % sizeof(gid_t) == 0);
19,355✔
926
        n /= sizeof(gid_t);
19,355✔
927

928
        if (n > INT_MAX)
19,355✔
929
                return -E2BIG;
930

931
        *ret = TAKE_PTR(d);
19,355✔
932

933
        return (int) n;
19,355✔
934
}
935

936
int getpeerpidfd(int fd) {
19,995✔
937
        socklen_t n = sizeof(int);
19,995✔
938
        int pidfd = -EBADF;
19,995✔
939

940
        assert(fd >= 0);
19,995✔
941

942
        if (getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD, &pidfd, &n) < 0)
19,995✔
UNCOV
943
                return -errno;
×
944

945
        if (n != sizeof(int))
19,995✔
946
                return -EIO;
947

948
        return pidfd;
19,995✔
949
}
950

951
int getpeerpidref(int fd, PidRef *ret) {
4✔
952
        int r;
4✔
953

954
        assert(fd >= 0);
4✔
955
        assert(ret);
4✔
956

957
        int pidfd = getpeerpidfd(fd);
4✔
958
        if (pidfd < 0) {
4✔
959
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(pidfd))
×
960
                        return pidfd;
×
961

962
                struct ucred ucred;
×
963
                r = getpeercred(fd, &ucred);
×
964
                if (r < 0)
×
965
                        return r;
966

967
                return pidref_set_pid(ret, ucred.pid);
×
968
        }
969

970
        return pidref_set_pidfd_consume(ret, pidfd);
4✔
971
}
972

973
ssize_t send_one_fd_iov_sa(
1,015✔
974
                int transport_fd,
975
                int fd,
976
                const struct iovec *iov, size_t iovlen,
977
                const struct sockaddr *sa, socklen_t len,
978
                int flags) {
979

980
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control = {};
1,015✔
981
        struct msghdr mh = {
1,015✔
982
                .msg_name = (struct sockaddr*) sa,
983
                .msg_namelen = len,
984
                .msg_iov = (struct iovec *)iov,
985
                .msg_iovlen = iovlen,
986
        };
987
        ssize_t k;
1,015✔
988

989
        assert(transport_fd >= 0);
1,015✔
990

991
        /*
992
         * We need either an FD or data to send.
993
         * If there's nothing, return an error.
994
         */
995
        if (fd < 0 && !iov)
1,015✔
996
                return -EINVAL;
1,015✔
997

998
        if (fd >= 0) {
1,014✔
999
                struct cmsghdr *cmsg;
1,010✔
1000

1001
                mh.msg_control = &control;
1,010✔
1002
                mh.msg_controllen = sizeof(control);
1,010✔
1003

1004
                cmsg = CMSG_FIRSTHDR(&mh);
1,010✔
1005
                cmsg->cmsg_level = SOL_SOCKET;
1,010✔
1006
                cmsg->cmsg_type = SCM_RIGHTS;
1,010✔
1007
                cmsg->cmsg_len = CMSG_LEN(sizeof(int));
1,010✔
1008
                memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
1,010✔
1009
        }
1010
        k = sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags);
1,014✔
1011
        if (k < 0)
1,014✔
1012
                return (ssize_t) -errno;
×
1013

1014
        return k;
1015
}
1016

1017
int send_one_fd_sa(
1✔
1018
                int transport_fd,
1019
                int fd,
1020
                const struct sockaddr *sa, socklen_t len,
1021
                int flags) {
1022

1023
        assert(fd >= 0);
1✔
1024

1025
        return (int) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, sa, len, flags);
1✔
1026
}
1027

1028
ssize_t receive_one_fd_iov(
6,877✔
1029
                int transport_fd,
1030
                struct iovec *iov, size_t iovlen,
1031
                int flags,
1032
                int *ret_fd) {
1033

1034
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control;
6,877✔
1035
        struct msghdr mh = {
6,877✔
1036
                .msg_control = &control,
1037
                .msg_controllen = sizeof(control),
1038
                .msg_iov = iov,
1039
                .msg_iovlen = iovlen,
1040
        };
1041
        struct cmsghdr *found;
6,877✔
1042
        ssize_t k;
6,877✔
1043

1044
        assert(transport_fd >= 0);
6,877✔
1045
        assert(ret_fd);
6,877✔
1046

1047
        /*
1048
         * Receive a single FD via @transport_fd. We don't care for
1049
         * the transport-type. We retrieve a single FD at most, so for
1050
         * packet-based transports, the caller must ensure to send
1051
         * only a single FD per packet.  This is best used in
1052
         * combination with send_one_fd().
1053
         */
1054

1055
        k = recvmsg_safe(transport_fd, &mh, MSG_CMSG_CLOEXEC | flags);
6,877✔
1056
        if (k < 0)
6,877✔
1057
                return k;
6,877✔
1058

1059
        found = cmsg_find(&mh, SOL_SOCKET, SCM_RIGHTS, CMSG_LEN(sizeof(int)));
6,707✔
1060
        if (!found) {
6,707✔
1061
                cmsg_close_all(&mh);
2,078✔
1062

1063
                /* If didn't receive an FD or any data, return an error. */
1064
                if (k == 0)
2,078✔
1065
                        return -EIO;
1066
        }
1067

1068
        if (found)
4,633✔
1069
                *ret_fd = *CMSG_TYPED_DATA(found, int);
4,629✔
1070
        else
1071
                *ret_fd = -EBADF;
4✔
1072

1073
        return k;
1074
}
1075

1076
int receive_one_fd(int transport_fd, int flags) {
923✔
1077
        int fd;
923✔
1078
        ssize_t k;
923✔
1079

1080
        k = receive_one_fd_iov(transport_fd, NULL, 0, flags, &fd);
923✔
1081
        if (k == 0)
923✔
1082
                return fd;
840✔
1083

1084
        /* k must be negative, since receive_one_fd_iov() only returns
1085
         * a positive value if data was received through the iov. */
1086
        assert(k < 0);
83✔
1087
        return (int) k;
83✔
1088
}
1089

1090
ssize_t next_datagram_size_fd(int fd) {
213,920✔
1091
        ssize_t l;
213,920✔
1092
        int k;
213,920✔
1093

1094
        /* This is a bit like FIONREAD/SIOCINQ, however a bit more powerful. The difference being: recv(MSG_PEEK) will
1095
         * actually cause the next datagram in the queue to be validated regarding checksums, which FIONREAD doesn't
1096
         * do. This difference is actually of major importance as we need to be sure that the size returned here
1097
         * actually matches what we will read with recvmsg() next, as otherwise we might end up allocating a buffer of
1098
         * the wrong size. */
1099

1100
        l = recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC);
213,920✔
1101
        if (l < 0) {
213,920✔
1102
                if (IN_SET(errno, EOPNOTSUPP, EFAULT))
535✔
1103
                        goto fallback;
×
1104

1105
                return -errno;
535✔
1106
        }
1107
        if (l == 0)
213,385✔
1108
                goto fallback;
67✔
1109

1110
        return l;
1111

1112
fallback:
67✔
1113
        k = 0;
67✔
1114

1115
        /* Some sockets (AF_PACKET) do not support null-sized recv() with MSG_TRUNC set, let's fall back to FIONREAD
1116
         * for them. Checksums don't matter for raw sockets anyway, hence this should be fine. */
1117

1118
        if (ioctl(fd, FIONREAD, &k) < 0)
67✔
1119
                return -errno;
×
1120

1121
        return (ssize_t) k;
67✔
1122
}
1123

1124
/* Put a limit on how many times will attempt to call accept4(). We loop
1125
 * only on "transient" errors, but let's make sure we don't loop forever. */
1126
#define MAX_FLUSH_ITERATIONS 1024
1127

1128
int flush_accept(int fd) {
13✔
1129

1130
        int r, b;
13✔
1131
        socklen_t l = sizeof(b);
13✔
1132

1133
        /* Similar to flush_fd() but flushes all incoming connections by accepting and immediately closing
1134
         * them. */
1135

1136
        if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &b, &l) < 0)
13✔
1137
                return -errno;
×
1138

1139
        assert(l == sizeof(b));
13✔
1140
        if (!b) /* Let's check if this socket accepts connections before calling accept(). accept4() can
13✔
1141
                 * return EOPNOTSUPP if the fd is not a listening socket, which we should treat as a fatal
1142
                 * error, or in case the incoming TCP connection triggered a network issue, which we want to
1143
                 * treat as a transient error. Thus, let's rule out the first reason for EOPNOTSUPP early, so
1144
                 * we can loop safely on transient errors below. */
1145
                return -ENOTTY;
1146

1147
        for (unsigned iteration = 0;; iteration++) {
3✔
1148
                int cfd;
8✔
1149

1150
                r = fd_wait_for_event(fd, POLLIN, 0);
8✔
1151
                if (r == -EINTR)
8✔
1152
                        continue;
×
1153
                if (r <= 0)
8✔
1154
                        return r;
1155

1156
                if (iteration >= MAX_FLUSH_ITERATIONS)
3✔
1157
                        return log_debug_errno(SYNTHETIC_ERRNO(EBUSY),
×
1158
                                               "Failed to flush connections within " STRINGIFY(MAX_FLUSH_ITERATIONS) " iterations.");
1159

1160
                cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
3✔
1161
                if (cfd < 0) {
3✔
1162
                        if (errno == EAGAIN)
×
1163
                                return 0;
1164

1165
                        if (ERRNO_IS_ACCEPT_AGAIN(errno))
×
1166
                                continue;
×
1167

1168
                        return -errno;
×
1169
                }
1170

1171
                safe_close(cfd);
3✔
1172
        }
1173
}
1174

1175
ssize_t flush_mqueue(int fd) {
×
1176
        _cleanup_free_ char *buf = NULL;
×
1177
        struct mq_attr attr;
×
1178
        ssize_t count = 0;
×
1179
        int r;
×
1180

1181
        assert(fd >= 0);
×
1182

1183
        /* Similar to flush_fd() but flushes all messages from a POSIX message queue. */
1184

1185
        for (;;) {
×
1186
                ssize_t l;
×
1187

1188
                r = fd_wait_for_event(fd, POLLIN, /* timeout= */ 0);
×
1189
                if (r == -EINTR)
×
1190
                        continue;
×
1191
                if (r < 0)
×
1192
                        return r;
×
1193
                if (r == 0)
×
1194
                        return count;
1195

1196
                if (!buf) {
×
1197
                        /* Buffer must be at least as large as mq_msgsize. */
1198
                        if (mq_getattr(fd, &attr) < 0)
×
1199
                                return -errno;
×
1200

1201
                        buf = malloc(attr.mq_msgsize);
×
1202
                        if (!buf)
×
1203
                                return -ENOMEM;
1204
                }
1205

1206
                l = mq_receive(fd, buf, attr.mq_msgsize, /* msg_prio= */ NULL);
×
1207
                if (l < 0) {
×
1208
                        if (errno == EINTR)
×
1209
                                continue;
×
1210

1211
                        if (errno == EAGAIN)
×
1212
                                return count;
1213

1214
                        return -errno;
×
1215
                }
1216

1217
                count += l;
×
1218
        }
1219
}
1220

1221
struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length) {
405,708✔
1222
        struct cmsghdr *cmsg;
405,708✔
1223

1224
        assert(mh);
405,708✔
1225

1226
        CMSG_FOREACH(cmsg, mh)
812,038✔
1227
                if (cmsg->cmsg_level == level &&
370,863✔
1228
                    cmsg->cmsg_type == type &&
370,552✔
1229
                    (length == (socklen_t) -1 || length == cmsg->cmsg_len))
369,716✔
1230
                        return cmsg;
1231

1232
        return NULL;
1233
}
1234

1235
void* cmsg_find_and_copy_data(struct msghdr *mh, int level, int type, void *buf, size_t buf_len) {
345✔
1236
        struct cmsghdr *cmsg;
345✔
1237

1238
        assert(mh);
345✔
1239
        assert(buf);
345✔
1240
        assert(buf_len > 0);
345✔
1241

1242
        /* This is similar to cmsg_find_data(), but copy the found data to buf. This should be typically used
1243
         * when reading possibly unaligned data such as timestamp, as time_t is 64-bit and size_t is 32-bit on
1244
         * RISCV32. See issue #27241. */
1245

1246
        cmsg = cmsg_find(mh, level, type, CMSG_LEN(buf_len));
345✔
1247
        if (!cmsg)
345✔
1248
                return NULL;
1249

1250
        return memcpy_safe(buf, CMSG_DATA(cmsg), buf_len);
338✔
1251
}
1252

1253
size_t sockaddr_ll_len(const struct sockaddr_ll *sa) {
357✔
1254
        /* Certain hardware address types (e.g Infiniband) do not fit into sll_addr
1255
         * (8 bytes) and run over the structure. This function returns the correct size that
1256
         * must be passed to kernel. */
1257

1258
        assert(sa->sll_family == AF_PACKET);
357✔
1259

1260
        size_t mac_len = sizeof(sa->sll_addr);
357✔
1261

1262
        if (be16toh(sa->sll_hatype) == ARPHRD_ETHER)
357✔
1263
                mac_len = MAX(mac_len, (size_t) ETH_ALEN);
357✔
1264
        if (be16toh(sa->sll_hatype) == ARPHRD_INFINIBAND)
357✔
1265
                mac_len = MAX(mac_len, (size_t) INFINIBAND_ALEN);
×
1266

1267
        return offsetof(struct sockaddr_ll, sll_addr) + mac_len;
357✔
1268
}
1269

1270
size_t sockaddr_un_len(const struct sockaddr_un *sa) {
2,620✔
1271
        /* Covers only file system and abstract AF_UNIX socket addresses, but not unnamed socket addresses. */
1272

1273
        assert(sa->sun_family == AF_UNIX);
2,620✔
1274

1275
        return offsetof(struct sockaddr_un, sun_path) +
7,860✔
1276
                (sa->sun_path[0] == 0 ?
2,620✔
1277
                        1 + strnlen(sa->sun_path+1, sizeof(sa->sun_path)-1) :
673✔
1278
                        strnlen(sa->sun_path, sizeof(sa->sun_path))+1);
1,947✔
1279
}
1280

1281
size_t sockaddr_len(const union sockaddr_union *sa) {
434✔
1282
        switch (sa->sa.sa_family) {
434✔
1283
        case AF_INET:
1284
                return sizeof(struct sockaddr_in);
1285
        case AF_INET6:
136✔
1286
                return sizeof(struct sockaddr_in6);
136✔
1287
        case AF_UNIX:
3✔
1288
                return sockaddr_un_len(&sa->un);
3✔
1289
        case AF_PACKET:
×
1290
                return sockaddr_ll_len(&sa->ll);
×
1291
        case AF_NETLINK:
×
1292
                return sizeof(struct sockaddr_nl);
×
1293
        case AF_VSOCK:
1294
                return sizeof(struct sockaddr_vm);
1295
        default:
×
1296
                assert_not_reached();
×
1297
        }
1298
}
1299

1300
int socket_ioctl_fd(void) {
4,870✔
1301
        int fd;
4,870✔
1302

1303
        /* Create a socket to invoke the various network interface ioctl()s on. Traditionally only AF_INET was good for
1304
         * that. Since kernel 4.6 AF_NETLINK works for this too. We first try to use AF_INET hence, but if that's not
1305
         * available (for example, because it is made unavailable via SECCOMP or such), we'll fall back to the more
1306
         * generic AF_NETLINK. */
1307

1308
        fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
4,870✔
1309
        if (fd < 0)
4,870✔
1310
                fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_GENERIC);
×
1311
        if (fd < 0)
×
1312
                return -errno;
×
1313

1314
        return fd;
1315
}
1316

1317
int sockaddr_un_unlink(const struct sockaddr_un *sa) {
1,723✔
1318
        const char *p, * nul;
1,723✔
1319

1320
        assert(sa);
1,723✔
1321

1322
        if (sa->sun_family != AF_UNIX)
1,723✔
1323
                return -EPROTOTYPE;
1324

1325
        if (sa->sun_path[0] == 0) /* Nothing to do for abstract sockets */
1,723✔
1326
                return 0;
1327

1328
        /* The path in .sun_path is not necessarily NUL terminated. Let's fix that. */
1329
        nul = memchr(sa->sun_path, 0, sizeof(sa->sun_path));
1,723✔
1330
        if (nul)
1,723✔
1331
                p = sa->sun_path;
1332
        else
1333
                p = memdupa_suffix0(sa->sun_path, sizeof(sa->sun_path));
×
1334

1335
        if (unlink(p) < 0)
1,723✔
1336
                return -errno;
1,336✔
1337

1338
        return 1;
1339
}
1340

1341
int sockaddr_un_set_path(struct sockaddr_un *ret, const char *path) {
412,277✔
1342
        size_t l;
412,277✔
1343

1344
        assert(ret);
412,277✔
1345
        assert(path);
412,277✔
1346

1347
        /* Initialize ret->sun_path from the specified argument. This will interpret paths starting with '@' as
1348
         * abstract namespace sockets, and those starting with '/' as regular filesystem sockets. It won't accept
1349
         * anything else (i.e. no relative paths), to avoid ambiguities. Note that this function cannot be used to
1350
         * reference paths in the abstract namespace that include NUL bytes in the name. */
1351

1352
        l = strlen(path);
412,277✔
1353
        if (l < 2)
412,277✔
1354
                return -EINVAL;
1355
        if (!IN_SET(path[0], '/', '@'))
412,276✔
1356
                return -EINVAL;
1357

1358
        /* Don't allow paths larger than the space in sockaddr_un. Note that we are a tiny bit more restrictive than
1359
         * the kernel is: we insist on NUL termination (both for abstract namespace and regular file system socket
1360
         * addresses!), which the kernel doesn't. We do this to reduce chance of incompatibility with other apps that
1361
         * do not expect non-NUL terminated file system path. */
1362
        if (l+1 > sizeof(ret->sun_path))
412,276✔
1363
                return path[0] == '@' ? -EINVAL : -ENAMETOOLONG; /* return a recognizable error if this is
2✔
1364
                                                                  * too long to fit into a sockaddr_un, but
1365
                                                                  * is a file system path, and thus might be
1366
                                                                  * connectible via O_PATH indirection. */
1367

1368
        *ret = (struct sockaddr_un) {
412,274✔
1369
                .sun_family = AF_UNIX,
1370
        };
1371

1372
        if (path[0] == '@') {
412,274✔
1373
                /* Abstract namespace socket */
1374
                memcpy(ret->sun_path + 1, path + 1, l); /* copy *with* trailing NUL byte */
221,780✔
1375
                return (int) (offsetof(struct sockaddr_un, sun_path) + l); /* 🔥 *don't* 🔥 include trailing NUL in size */
221,780✔
1376

1377
        } else {
1378
                assert(path[0] == '/');
190,494✔
1379

1380
                /* File system socket */
1381
                memcpy(ret->sun_path, path, l + 1); /* copy *with* trailing NUL byte */
190,494✔
1382
                return (int) (offsetof(struct sockaddr_un, sun_path) + l + 1); /* include trailing NUL in size */
190,494✔
1383
        }
1384
}
1385

1386
int getsockopt_int(int fd, int level, int optname, int *ret) {
14,709✔
1387
        int v;
14,709✔
1388
        socklen_t sl = sizeof(v);
14,709✔
1389

1390
        assert(fd >= 0);
14,709✔
1391
        assert(ret);
14,709✔
1392

1393
        if (getsockopt(fd, level, optname, &v, &sl) < 0)
14,709✔
1394
                return negative_errno();
54✔
1395
        if (sl != sizeof(v))
14,655✔
1396
                return -EIO;
1397

1398
        *ret = v;
14,655✔
1399
        return 0;
14,655✔
1400
}
1401

1402
int socket_bind_to_ifname(int fd, const char *ifname) {
1✔
1403
        assert(fd >= 0);
1✔
1404

1405
        /* Call with NULL to drop binding */
1406

1407
        return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen_ptr(ifname)));
2✔
1408
}
1409

1410
int socket_bind_to_ifindex(int fd, int ifindex) {
825✔
1411
        assert(fd >= 0);
825✔
1412

1413
        if (ifindex <= 0)
825✔
1414
                /* Drop binding */
1415
                return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, NULL, 0));
54✔
1416

1417
        return setsockopt_int(fd, SOL_SOCKET, SO_BINDTOIFINDEX, ifindex);
771✔
1418
}
1419

1420
int socket_autobind(int fd, char **ret_name) {
477✔
1421
        _cleanup_free_ char *name = NULL;
477✔
1422
        uint64_t random;
477✔
1423
        int r;
477✔
1424

1425
        /* Generate a random abstract socket name and bind fd to it. This is modeled after the kernel
1426
         * "autobind" feature, but uses 64-bit random number internally. */
1427

1428
        assert(fd >= 0);
477✔
1429

1430
        random = random_u64();
477✔
1431

1432
        if (asprintf(&name, "@%" PRIu64, random) < 0)
477✔
1433
                return -ENOMEM;
1434

1435
        union sockaddr_union sa;
477✔
1436
        assert_cc(DECIMAL_STR_MAX(uint64_t) < sizeof(sa.un.sun_path));
477✔
1437

1438
        r = sockaddr_un_set_path(&sa.un, name);
477✔
1439
        if (r < 0)
477✔
1440
                return r;
1441

1442
        if (bind(fd, &sa.sa, r) < 0)
477✔
1443
                return -errno;
×
1444

1445
        if (ret_name)
477✔
1446
                *ret_name = TAKE_PTR(name);
475✔
1447
        return 0;
1448
}
1449

1450
ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags) {
1,036,252✔
1451
        ssize_t n;
1,036,252✔
1452

1453
        /* A wrapper around recvmsg() that checks for MSG_CTRUNC and MSG_TRUNC, and turns them into an error,
1454
         * in a reasonably safe way, closing any received fds in the error path.
1455
         *
1456
         * Note that unlike our usual coding style this might modify *msg on failure. */
1457

1458
        assert(sockfd >= 0);
1,036,252✔
1459
        assert(msg);
1,036,252✔
1460

1461
        n = recvmsg(sockfd, msg, flags);
1,036,252✔
1462
        if (n < 0)
1,036,252✔
1463
                return -errno;
56,283✔
1464

1465
        if (FLAGS_SET(msg->msg_flags, MSG_CTRUNC) ||
979,969✔
1466
            (!FLAGS_SET(flags, MSG_PEEK) && FLAGS_SET(msg->msg_flags, MSG_TRUNC))) {
979,969✔
1467
                cmsg_close_all(msg);
×
1468
                return FLAGS_SET(msg->msg_flags, MSG_CTRUNC) ? -ECHRNG : -EXFULL;
×
1469
        }
1470

1471
        return n;
1472
}
1473

1474
int socket_get_family(int fd) {
113,157✔
1475
        int af;
113,157✔
1476
        socklen_t sl = sizeof(af);
113,157✔
1477

1478
        if (getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &af, &sl) < 0)
113,157✔
1479
                return -errno;
×
1480

1481
        if (sl != sizeof(af))
113,157✔
1482
                return -EINVAL;
1483

1484
        return af;
113,157✔
1485
}
1486

1487
int socket_set_recvpktinfo(int fd, int af, bool b) {
15,918✔
1488

1489
        if (af == AF_UNSPEC) {
15,918✔
1490
                af = socket_get_family(fd);
×
1491
                if (af < 0)
×
1492
                        return af;
1493
        }
1494

1495
        switch (af) {
15,918✔
1496

1497
        case AF_INET:
8,405✔
1498
                return setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, b);
8,405✔
1499

1500
        case AF_INET6:
7,476✔
1501
                return setsockopt_int(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, b);
7,476✔
1502

1503
        case AF_NETLINK:
37✔
1504
                return setsockopt_int(fd, SOL_NETLINK, NETLINK_PKTINFO, b);
37✔
1505

1506
        case AF_PACKET:
×
1507
                return setsockopt_int(fd, SOL_PACKET, PACKET_AUXDATA, b);
×
1508

1509
        default:
1510
                return -EAFNOSUPPORT;
1511
        }
1512
}
1513

1514
int socket_set_unicast_if(int fd, int af, int ifi) {
120✔
1515
        be32_t ifindex_be = htobe32(ifi);
120✔
1516

1517
        if (af == AF_UNSPEC) {
120✔
1518
                af = socket_get_family(fd);
×
1519
                if (af < 0)
×
1520
                        return af;
120✔
1521
        }
1522

1523
        switch (af) {
120✔
1524

1525
        case AF_INET:
86✔
1526
                return RET_NERRNO(setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)));
86✔
1527

1528
        case AF_INET6:
34✔
1529
                return RET_NERRNO(setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)));
34✔
1530

1531
        default:
1532
                return -EAFNOSUPPORT;
1533
        }
1534
}
1535

1536
int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val) {
31,605✔
1537
        if (af == AF_UNSPEC) {
31,605✔
1538
                af = socket_get_family(fd);
×
1539
                if (af < 0)
×
1540
                        return af;
1541
        }
1542

1543
        switch (af) {
31,605✔
1544

1545
        case AF_INET:
16,644✔
1546
                return setsockopt_int(fd, IPPROTO_IP, opt_ipv4, val);
16,644✔
1547

1548
        case AF_INET6:
14,961✔
1549
                return setsockopt_int(fd, IPPROTO_IPV6, opt_ipv6, val);
14,961✔
1550

1551
        default:
1552
                return -EAFNOSUPPORT;
1553
        }
1554
}
1555

1556
int socket_get_mtu(int fd, int af, size_t *ret) {
720✔
1557
        int mtu, r;
720✔
1558

1559
        if (af == AF_UNSPEC) {
720✔
1560
                af = socket_get_family(fd);
×
1561
                if (af < 0)
×
1562
                        return af;
720✔
1563
        }
1564

1565
        switch (af) {
720✔
1566

1567
        case AF_INET:
324✔
1568
                r = getsockopt_int(fd, IPPROTO_IP, IP_MTU, &mtu);
324✔
1569
                break;
1570

1571
        case AF_INET6:
396✔
1572
                r = getsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, &mtu);
396✔
1573
                break;
1574

1575
        default:
1576
                return -EAFNOSUPPORT;
1577
        }
1578

1579
        if (r < 0)
720✔
1580
                return r;
1581
        if (mtu <= 0)
666✔
1582
                return -EINVAL;
1583

1584
        *ret = (size_t) mtu;
666✔
1585
        return 0;
666✔
1586
}
1587

1588
static int connect_unix_path_simple(int fd, const char *path) {
177,650✔
1589
        union sockaddr_union sa = {
177,650✔
1590
                .un.sun_family = AF_UNIX,
1591
        };
1592
        size_t l;
177,650✔
1593

1594
        assert(fd >= 0);
177,650✔
1595
        assert(path);
177,650✔
1596

1597
        l = strlen(path);
177,650✔
1598
        assert(l > 0);
177,650✔
1599
        assert(l < sizeof(sa.un.sun_path));
177,650✔
1600

1601
        memcpy(sa.un.sun_path, path, l + 1);
177,650✔
1602
        return RET_NERRNO(connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + l + 1));
177,650✔
1603
}
1604

1605
static int connect_unix_inode(int fd, int inode_fd) {
14✔
1606
        assert(fd >= 0);
14✔
1607
        assert(inode_fd >= 0);
14✔
1608

1609
        return connect_unix_path_simple(fd, FORMAT_PROC_FD_PATH(inode_fd));
14✔
1610
}
1611

1612
int connect_unix_path(int fd, int dir_fd, const char *path) {
177,650✔
1613
        _cleanup_close_ int inode_fd = -EBADF;
177,650✔
1614

1615
        assert(fd >= 0);
177,650✔
1616
        assert(dir_fd == AT_FDCWD || dir_fd >= 0);
177,650✔
1617

1618
        /* Connects to the specified AF_UNIX socket in the file system. Works around the 108 byte size limit
1619
         * in sockaddr_un, by going via O_PATH if needed. This hence works for any kind of path. */
1620

1621
        if (!path)
177,650✔
1622
                return connect_unix_inode(fd, dir_fd); /* If no path is specified, then dir_fd refers to the socket inode to connect to. */
1✔
1623

1624
        /* Refuse zero length path early, to make sure AF_UNIX stack won't mistake this for an abstract
1625
         * namespace path, since first char is NUL */
1626
        if (isempty(path))
355,299✔
1627
                return -EINVAL;
1628

1629
        /* Shortcut for the simple case */
1630
        if (dir_fd == AT_FDCWD && strlen(path) < sizeof_field(struct sockaddr_un, sun_path))
177,649✔
1631
                return connect_unix_path_simple(fd, path);
177,636✔
1632

1633
        /* If dir_fd is specified, then we need to go the indirect O_PATH route, because connectat() does not
1634
         * exist. If the path is too long, we also need to take the indirect route, since we can't fit this
1635
         * into a sockaddr_un directly. */
1636

1637
        inode_fd = openat(dir_fd, path, O_PATH|O_CLOEXEC);
13✔
1638
        if (inode_fd < 0)
13✔
1639
                return -errno;
×
1640

1641
        return connect_unix_inode(fd, inode_fd);
13✔
1642
}
1643

1644
int socket_address_parse_unix(SocketAddress *ret_address, const char *s) {
230,432✔
1645
        struct sockaddr_un un;
230,432✔
1646
        int r;
230,432✔
1647

1648
        assert(ret_address);
230,432✔
1649
        assert(s);
230,432✔
1650

1651
        if (!IN_SET(*s, '/', '@'))
230,432✔
1652
                return -EPROTO;
230,432✔
1653

1654
        r = sockaddr_un_set_path(&un, s);
229,818✔
1655
        if (r < 0)
229,818✔
1656
                return r;
1657

1658
        *ret_address = (SocketAddress) {
229,816✔
1659
                .sockaddr.un = un,
1660
                .size = r,
1661
        };
1662

1663
        return 0;
229,816✔
1664
}
1665

1666
int socket_address_equal_unix(const char *a, const char *b) {
719✔
1667
        SocketAddress socket_a, socket_b;
719✔
1668
        int r;
719✔
1669

1670
        assert(a);
719✔
1671
        assert(b);
719✔
1672

1673
        r = socket_address_parse_unix(&socket_a, a);
719✔
1674
        if (r < 0)
719✔
1675
                return r;
719✔
1676

1677
        r = socket_address_parse_unix(&socket_b, b);
719✔
1678
        if (r < 0)
719✔
1679
                return r;
1680

1681
        return sockaddr_equal(&socket_a.sockaddr, &socket_b.sockaddr);
719✔
1682
}
1683

1684
int vsock_parse_port(const char *s, unsigned *ret) {
543✔
1685
        int r;
543✔
1686

1687
        assert(ret);
543✔
1688

1689
        if (!s)
543✔
1690
                return -EINVAL;
543✔
1691

1692
        unsigned u;
543✔
1693
        r = safe_atou(s, &u);
543✔
1694
        if (r < 0)
543✔
1695
                return r;
1696

1697
        /* Port 0 is apparently valid and not special in AF_VSOCK (unlike on IP). But VMADDR_PORT_ANY
1698
         * (UINT32_MAX) is. Hence refuse that. */
1699

1700
        if (u == VMADDR_PORT_ANY)
540✔
1701
                return -EINVAL;
1702

1703
        *ret = u;
540✔
1704
        return 0;
540✔
1705
}
1706

1707
int vsock_parse_cid(const char *s, unsigned *ret) {
502✔
1708
        assert(ret);
502✔
1709

1710
        if (!s)
502✔
1711
                return -EINVAL;
1712

1713
        /* Parsed an AF_VSOCK "CID". This is a 32bit entity, and the usual type is "unsigned". We recognize
1714
         * the three special CIDs as strings, and otherwise parse the numeric CIDs. */
1715

1716
        if (streq(s, "hypervisor"))
502✔
1717
                *ret = VMADDR_CID_HYPERVISOR;
×
1718
        else if (streq(s, "local"))
502✔
1719
                *ret = VMADDR_CID_LOCAL;
×
1720
        else if (streq(s, "host"))
502✔
1721
                *ret = VMADDR_CID_HOST;
×
1722
        else
1723
                return safe_atou(s, ret);
502✔
1724

1725
        return 0;
1726
}
1727

1728
int socket_address_parse_vsock(SocketAddress *ret_address, const char *s) {
614✔
1729
        /* AF_VSOCK socket in vsock:cid:port notation */
1730
        _cleanup_free_ char *n = NULL;
614✔
1731
        const char *e, *cid_start;
614✔
1732
        unsigned port, cid;
614✔
1733
        int type, r;
614✔
1734

1735
        assert(ret_address);
614✔
1736
        assert(s);
614✔
1737

1738
        if ((cid_start = startswith(s, "vsock:")))
614✔
1739
                type = 0;
1740
        else if ((cid_start = startswith(s, "vsock-dgram:")))
561✔
1741
                type = SOCK_DGRAM;
1742
        else if ((cid_start = startswith(s, "vsock-seqpacket:")))
561✔
1743
                type = SOCK_SEQPACKET;
1744
        else if ((cid_start = startswith(s, "vsock-stream:")))
561✔
1745
                type = SOCK_STREAM;
1746
        else
1747
                return -EPROTO;
1748

1749
        e = strchr(cid_start, ':');
545✔
1750
        if (!e)
545✔
1751
                return -EINVAL;
1752

1753
        r = vsock_parse_port(e+1, &port);
543✔
1754
        if (r < 0)
543✔
1755
                return r;
1756

1757
        n = strndup(cid_start, e - cid_start);
540✔
1758
        if (!n)
540✔
1759
                return -ENOMEM;
1760

1761
        if (isempty(n))
540✔
1762
                cid = VMADDR_CID_ANY;
38✔
1763
        else {
1764
                r = vsock_parse_cid(n, &cid);
502✔
1765
                if (r < 0)
502✔
1766
                        return r;
1767
        }
1768

1769
        *ret_address = (SocketAddress) {
537✔
1770
                .sockaddr.vm = {
1771
                        .svm_family = AF_VSOCK,
1772
                        .svm_cid = cid,
1773
                        .svm_port = port,
1774
                },
1775
                .type = type,
1776
                .size = sizeof(struct sockaddr_vm),
1777
        };
1778

1779
        return 0;
537✔
1780
}
1781

1782
int vsock_get_local_cid(unsigned *ret) {
84✔
1783
        _cleanup_close_ int vsock_fd = -EBADF;
84✔
1784

1785
        vsock_fd = open("/dev/vsock", O_RDONLY|O_CLOEXEC);
84✔
1786
        if (vsock_fd < 0)
84✔
1787
                return log_debug_errno(errno, "Failed to open %s: %m", "/dev/vsock");
16✔
1788

1789
        unsigned tmp;
68✔
1790
        if (ioctl(vsock_fd, IOCTL_VM_SOCKETS_GET_LOCAL_CID, &tmp) < 0)
68✔
1791
                return log_debug_errno(errno, "Failed to query local AF_VSOCK CID: %m");
×
1792
        log_debug("Local AF_VSOCK CID: %u", tmp);
68✔
1793

1794
        /* If ret == NULL, we're just want to check if AF_VSOCK is available, so accept
1795
         * any address. Otherwise, filter out special addresses that are cannot be used
1796
         * to identify _this_ machine from the outside. */
1797
        if (ret && IN_SET(tmp, VMADDR_CID_LOCAL, VMADDR_CID_HOST))
68✔
1798
                return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL),
×
1799
                                       "IOCTL_VM_SOCKETS_GET_LOCAL_CID returned special value (%u), ignoring.", tmp);
1800

1801
        if (ret)
65✔
1802
                *ret = tmp;
65✔
1803
        return 0;
1804
}
1805

1806
int netlink_socket_get_multicast_groups(int fd, size_t *ret_len, uint32_t **ret_groups) {
13,989✔
1807
        _cleanup_free_ uint32_t *groups = NULL;
27,978✔
1808
        socklen_t len = 0, old_len;
13,989✔
1809

1810
        assert(fd >= 0);
13,989✔
1811

1812
        if (getsockopt(fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, NULL, &len) < 0)
13,989✔
1813
                return -errno;
×
1814

1815
        if (len == 0)
13,989✔
1816
                goto finalize;
13,541✔
1817

1818
        groups = new0(uint32_t, len);
448✔
1819
        if (!groups)
448✔
1820
                return -ENOMEM;
1821

1822
        old_len = len;
448✔
1823

1824
        if (getsockopt(fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, groups, &len) < 0)
448✔
1825
                return -errno;
×
1826

1827
        if (old_len != len)
448✔
1828
                return -EIO;
1829

1830
finalize:
448✔
1831
        if (ret_len)
13,989✔
1832
                *ret_len = len;
13,989✔
1833
        if (ret_groups)
13,989✔
1834
                *ret_groups = TAKE_PTR(groups);
13,989✔
1835

1836
        return 0;
1837
}
1838

1839
int socket_get_cookie(int fd, uint64_t *ret) {
406✔
1840
        assert(fd >= 0);
406✔
1841

1842
        uint64_t cookie = 0;
406✔
1843
        socklen_t cookie_len = sizeof(cookie);
406✔
1844
        if (getsockopt(fd, SOL_SOCKET, SO_COOKIE, &cookie, &cookie_len) < 0)
406✔
1845
                return -errno;
×
1846

1847
        assert(cookie_len == sizeof(cookie));
406✔
1848
        if (ret)
406✔
1849
                *ret = cookie;
406✔
1850

1851
        return 0;
1852
}
1853

1854
void cmsg_close_all(struct msghdr *mh) {
100,060✔
1855
        assert(mh);
100,060✔
1856

1857
        struct cmsghdr *cmsg;
100,060✔
1858
        CMSG_FOREACH(cmsg, mh) {
332,956✔
1859
                if (cmsg->cmsg_level != SOL_SOCKET)
66,418✔
1860
                        continue;
×
1861

1862
                if (cmsg->cmsg_type == SCM_RIGHTS)
66,418✔
1863
                        close_many(CMSG_TYPED_DATA(cmsg, int),
×
1864
                                   (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int));
×
1865
                else if (cmsg->cmsg_type == SCM_PIDFD) {
66,418✔
1866
                        assert(cmsg->cmsg_len == CMSG_LEN(sizeof(int)));
×
1867
                        safe_close(*CMSG_TYPED_DATA(cmsg, int));
×
1868
                }
1869
        }
1870
}
100,060✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc