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

systemd / systemd / 14554080340

19 Apr 2025 11:46AM UTC coverage: 72.101% (-0.03%) from 72.13%
14554080340

push

github

web-flow
Add two new paragraphs to coding style about header files (#37188)

296880 of 411754 relevant lines covered (72.1%)

687547.52 hits per line

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

56.72
/src/shared/bpf-program.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <sys/stat.h>
5
#include <sys/types.h>
6
#include <unistd.h>
7

8
#include "alloc-util.h"
9
#include "bpf-program.h"
10
#include "errno-util.h"
11
#include "escape.h"
12
#include "fd-util.h"
13
#include "memory-util.h"
14
#include "missing_syscall.h"
15
#include "path-util.h"
16
#include "serialize.h"
17
#include "string-table.h"
18

19
static const char *const bpf_cgroup_attach_type_table[__MAX_BPF_ATTACH_TYPE] = {
20
        [BPF_CGROUP_INET_INGRESS] =     "ingress",
21
        [BPF_CGROUP_INET_EGRESS] =      "egress",
22
        [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create",
23
        [BPF_CGROUP_SOCK_OPS] =         "sock_ops",
24
        [BPF_CGROUP_DEVICE] =           "device",
25
        [BPF_CGROUP_INET4_BIND] =       "bind4",
26
        [BPF_CGROUP_INET6_BIND] =       "bind6",
27
        [BPF_CGROUP_INET4_CONNECT] =    "connect4",
28
        [BPF_CGROUP_INET6_CONNECT] =    "connect6",
29
        [BPF_CGROUP_INET4_POST_BIND] =  "post_bind4",
30
        [BPF_CGROUP_INET6_POST_BIND] =  "post_bind6",
31
        [BPF_CGROUP_UDP4_SENDMSG] =     "sendmsg4",
32
        [BPF_CGROUP_UDP6_SENDMSG] =     "sendmsg6",
33
        [BPF_CGROUP_SYSCTL] =           "sysctl",
34
        [BPF_CGROUP_UDP4_RECVMSG] =     "recvmsg4",
35
        [BPF_CGROUP_UDP6_RECVMSG] =     "recvmsg6",
36
        [BPF_CGROUP_GETSOCKOPT] =       "getsockopt",
37
        [BPF_CGROUP_SETSOCKOPT] =       "setsockopt",
38
};
39

40
DEFINE_STRING_TABLE_LOOKUP(bpf_cgroup_attach_type, int);
738✔
41

42
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(bpf_program_hash_ops, void, trivial_hash_func, trivial_compare_func, bpf_program_free);
×
43

44
BPFProgram *bpf_program_free(BPFProgram *p) {
113,392✔
45
        if (!p)
113,392✔
46
                return NULL;
47
        /* Unfortunately, the kernel currently doesn't implicitly detach BPF programs from their cgroups when the last
48
         * fd to the BPF program is closed. This has nasty side-effects since this means that abnormally terminated
49
         * programs that attached one of their BPF programs to a cgroup will leave this program pinned for good with
50
         * zero chance of recovery, until the cgroup is removed. This is particularly problematic if the cgroup in
51
         * question is the root cgroup (or any other cgroup belonging to a service that cannot be restarted during
52
         * operation, such as dbus), as the memory for the BPF program can only be reclaimed through a reboot. To
53
         * counter this, we track closely to which cgroup a program was attached to and will detach it on our own
54
         * whenever we close the BPF fd. */
55
        (void) bpf_program_cgroup_detach(p);
1,011✔
56

57
        safe_close(p->kernel_fd);
1,011✔
58
        free(p->prog_name);
1,011✔
59
        free(p->instructions);
1,011✔
60
        free(p->attached_path);
1,011✔
61

62
        return mfree(p);
1,011✔
63
}
64

65
 /* struct bpf_prog_info info must be initialized since its value is both input and output
66
  * for BPF_OBJ_GET_INFO_BY_FD syscall. */
67
static int bpf_program_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, uint32_t info_len) {
×
68
        union bpf_attr attr;
×
69

70
        /* Explicitly memset to zero since some compilers may produce non-zero-initialized padding when
71
         * structured initialization is used.
72
         * Refer to https://github.com/systemd/systemd/issues/18164
73
         */
74
        zero(attr);
×
75
        attr.info.bpf_fd = prog_fd;
×
76
        attr.info.info_len = info_len;
×
77
        attr.info.info = PTR_TO_UINT64(info);
×
78

79
        return RET_NERRNO(bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr)));
×
80
}
81

82
int bpf_program_new(uint32_t prog_type, const char *prog_name, BPFProgram **ret) {
698✔
83
        _cleanup_(bpf_program_freep) BPFProgram *p = NULL;
1,396✔
84
        _cleanup_free_ char *name = NULL;
698✔
85

86
        if (prog_name) {
698✔
87
                if (strlen(prog_name) >= BPF_OBJ_NAME_LEN)
460✔
88
                        return -ENAMETOOLONG;
89

90
                name = strdup(prog_name);
460✔
91
                if (!name)
460✔
92
                        return -ENOMEM;
93
        }
94

95
        p = new(BPFProgram, 1);
698✔
96
        if (!p)
698✔
97
                return -ENOMEM;
98

99
        *p = (BPFProgram) {
698✔
100
                .prog_type = prog_type,
101
                .kernel_fd = -EBADF,
102
                .prog_name = TAKE_PTR(name),
698✔
103
        };
104

105
        *ret = TAKE_PTR(p);
698✔
106

107
        return 0;
698✔
108
}
109

110
int bpf_program_new_from_bpffs_path(const char *path, BPFProgram **ret) {
×
111
        _cleanup_(bpf_program_freep) BPFProgram *p = NULL;
×
112
        struct bpf_prog_info info = {};
×
113
        int r;
×
114

115
        assert(path);
×
116
        assert(ret);
×
117

118
        p = new(BPFProgram, 1);
×
119
        if (!p)
×
120
                return -ENOMEM;
121

122
        *p = (BPFProgram) {
×
123
                .prog_type = BPF_PROG_TYPE_UNSPEC,
124
                .kernel_fd = -EBADF,
125
        };
126

127
        r = bpf_program_load_from_bpf_fs(p, path);
×
128
        if (r < 0)
×
129
                return r;
130

131
        r = bpf_program_get_info_by_fd(p->kernel_fd, &info, sizeof(info));
×
132
        if (r < 0)
×
133
                return r;
134

135
        p->prog_type = info.type;
×
136
        *ret = TAKE_PTR(p);
×
137

138
        return 0;
×
139
}
140

141
int bpf_program_add_instructions(BPFProgram *p, const struct bpf_insn *instructions, size_t count) {
3,319✔
142

143
        assert(p);
3,319✔
144

145
        if (p->kernel_fd >= 0) /* don't allow modification after we uploaded things to the kernel */
3,319✔
146
                return -EBUSY;
147

148
        if (!GREEDY_REALLOC(p->instructions, p->n_instructions + count))
3,319✔
149
                return -ENOMEM;
150

151
        memcpy(p->instructions + p->n_instructions, instructions, sizeof(struct bpf_insn) * count);
3,319✔
152
        p->n_instructions += count;
3,319✔
153

154
        return 0;
3,319✔
155
}
156

157
int bpf_program_load_kernel(BPFProgram *p, char *log_buf, size_t log_size) {
698✔
158
        union bpf_attr attr;
698✔
159

160
        assert(p);
698✔
161

162
        if (p->kernel_fd >= 0) { /* make this idempotent */
698✔
163
                memzero(log_buf, log_size);
×
164
                return 0;
×
165
        }
166

167
        // FIXME: Clang doesn't 0-pad with structured initialization, causing
168
        // the kernel to reject the bpf_attr as invalid. See:
169
        // https://github.com/torvalds/linux/blob/v5.9/kernel/bpf/syscall.c#L65
170
        // Ideally it should behave like GCC, so that we can remove these workarounds.
171
        zero(attr);
698✔
172
        attr.prog_type = p->prog_type;
698✔
173
        attr.insns = PTR_TO_UINT64(p->instructions);
698✔
174
        attr.insn_cnt = p->n_instructions;
698✔
175
        attr.license = PTR_TO_UINT64("GPL");
698✔
176
        attr.log_buf = PTR_TO_UINT64(log_buf);
698✔
177
        attr.log_level = !!log_buf;
698✔
178
        attr.log_size = log_size;
698✔
179
        if (p->prog_name)
698✔
180
                strncpy(attr.prog_name, p->prog_name, BPF_OBJ_NAME_LEN - 1);
460✔
181

182
        p->kernel_fd = bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
698✔
183
        if (p->kernel_fd < 0)
698✔
184
                return -errno;
258✔
185

186
        return 0;
187
}
188

189
int bpf_program_load_from_bpf_fs(BPFProgram *p, const char *path) {
×
190
        union bpf_attr attr;
×
191

192
        assert(p);
×
193

194
        if (p->kernel_fd >= 0) /* don't overwrite an assembled or loaded program */
×
195
                return -EBUSY;
×
196

197
        zero(attr);
×
198
        attr.pathname = PTR_TO_UINT64(path);
×
199

200
        p->kernel_fd = bpf(BPF_OBJ_GET, &attr, sizeof(attr));
×
201
        if (p->kernel_fd < 0)
×
202
                return -errno;
×
203

204
        return 0;
205
}
206

207
int bpf_program_cgroup_attach(BPFProgram *p, int type, const char *path, uint32_t flags) {
336✔
208
        _cleanup_free_ char *copy = NULL;
336✔
209
        _cleanup_close_ int fd = -EBADF;
336✔
210
        union bpf_attr attr;
336✔
211
        int r;
336✔
212

213
        assert(p);
336✔
214
        assert(type >= 0);
336✔
215
        assert(path);
336✔
216

217
        if (!IN_SET(flags, 0, BPF_F_ALLOW_OVERRIDE, BPF_F_ALLOW_MULTI))
336✔
218
                return -EINVAL;
219

220
        /* We need to track which cgroup the program is attached to, and we can only track one attachment, hence let's
221
        * refuse this early. */
222
        if (p->attached_path) {
336✔
223
                if (!path_equal(p->attached_path, path))
×
224
                        return -EBUSY;
225
                if (p->attached_type != type)
×
226
                        return -EBUSY;
227
                if (p->attached_flags != flags)
×
228
                        return -EBUSY;
229

230
                /* Here's a shortcut: if we previously attached this program already, then we don't have to do so
231
                 * again. Well, with one exception: if we are in BPF_F_ALLOW_OVERRIDE mode then someone else might have
232
                 * replaced our program since the last time, hence let's reattach it again, just to be safe. In flags
233
                 * == 0 mode this is not an issue since nobody else can replace our program in that case, and in flags
234
                 * == BPF_F_ALLOW_MULTI mode any other's program would be installed in addition to ours hence ours
235
                 * would remain in effect. */
236
                if (flags != BPF_F_ALLOW_OVERRIDE)
×
237
                        return 0;
238
        }
239

240
        /* Ensure we have a kernel object for this. */
241
        r = bpf_program_load_kernel(p, NULL, 0);
336✔
242
        if (r < 0)
336✔
243
                return r;
244

245
        copy = strdup(path);
336✔
246
        if (!copy)
336✔
247
                return -ENOMEM;
248

249
        fd = open(path, O_DIRECTORY|O_RDONLY|O_CLOEXEC);
336✔
250
        if (fd < 0)
336✔
251
                return -errno;
×
252

253
        zero(attr);
336✔
254
        attr.attach_type = type;
336✔
255
        attr.target_fd = fd;
336✔
256
        attr.attach_bpf_fd = p->kernel_fd;
336✔
257
        attr.attach_flags = flags;
336✔
258

259
        if (bpf(BPF_PROG_ATTACH, &attr, sizeof(attr)) < 0)
336✔
260
                return -errno;
×
261

262
        free_and_replace(p->attached_path, copy);
336✔
263
        p->attached_type = type;
336✔
264
        p->attached_flags = flags;
336✔
265

266
        return 0;
336✔
267
}
268

269
int bpf_program_cgroup_detach(BPFProgram *p) {
1,011✔
270
        _cleanup_close_ int fd = -EBADF;
1,011✔
271

272
        assert(p);
1,011✔
273

274
        if (!p->attached_path)
1,011✔
275
                return -EUNATCH;
276

277
        fd = open(p->attached_path, O_DIRECTORY|O_RDONLY|O_CLOEXEC);
224✔
278
        if (fd < 0) {
224✔
279
                if (errno != ENOENT)
105✔
280
                        return -errno;
×
281

282
                /* If the cgroup does not exist anymore, then we don't have to explicitly detach, it got detached
283
                 * implicitly by the removal, hence don't complain */
284

285
        } else {
286
                union bpf_attr attr;
119✔
287

288
                zero(attr);
119✔
289
                attr.attach_type = p->attached_type;
119✔
290
                attr.target_fd = fd;
119✔
291
                attr.attach_bpf_fd = p->kernel_fd;
119✔
292

293
                if (bpf(BPF_PROG_DETACH, &attr, sizeof(attr)) < 0)
119✔
294
                        return -errno;
×
295
        }
296

297
        p->attached_path = mfree(p->attached_path);
224✔
298

299
        return 0;
224✔
300
}
301

302
int bpf_map_new(
×
303
                const char *name,
304
                enum bpf_map_type type,
305
                size_t key_size,
306
                size_t value_size,
307
                size_t max_entries,
308
                uint32_t flags) {
309

310
        union bpf_attr attr;
×
311
        const char *n = name;
×
312

313
        zero(attr);
×
314
        attr.map_type = type;
×
315
        attr.key_size = key_size;
×
316
        attr.value_size = value_size;
×
317
        attr.max_entries = max_entries;
×
318
        attr.map_flags = flags;
×
319

320
        /* The map name is primarily informational for debugging purposes, and typically too short
321
         * to carry the full unit name, hence we employ a trivial lossy escaping to make it fit
322
         * (truncation + only alphanumerical, "." and "_" are allowed as per
323
         * https://docs.kernel.org/bpf/maps.html#usage-notes) */
324
        for (size_t i = 0; i < sizeof(attr.map_name) - 1 && *n; i++, n++)
×
325
                attr.map_name[i] = strchr(ALPHANUMERICAL ".", *n) ? *n : '_';
×
326

327
        return RET_NERRNO(bpf(BPF_MAP_CREATE, &attr, sizeof(attr)));
×
328
}
329

330
int bpf_map_update_element(int fd, const void *key, void *value) {
×
331
        union bpf_attr attr;
×
332

333
        zero(attr);
×
334
        attr.map_fd = fd;
×
335
        attr.key = PTR_TO_UINT64(key);
×
336
        attr.value = PTR_TO_UINT64(value);
×
337

338
        return RET_NERRNO(bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)));
×
339
}
340

341
int bpf_map_lookup_element(int fd, const void *key, void *value) {
×
342
        union bpf_attr attr;
×
343

344
        zero(attr);
×
345
        attr.map_fd = fd;
×
346
        attr.key = PTR_TO_UINT64(key);
×
347
        attr.value = PTR_TO_UINT64(value);
×
348

349
        return RET_NERRNO(bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)));
×
350
}
351

352
int bpf_program_pin(int prog_fd, const char *bpffs_path) {
×
353
        union bpf_attr attr;
×
354

355
        zero(attr);
×
356
        attr.pathname = PTR_TO_UINT64((void *) bpffs_path);
×
357
        attr.bpf_fd = prog_fd;
×
358

359
        return RET_NERRNO(bpf(BPF_OBJ_PIN, &attr, sizeof(attr)));
×
360
}
361

362
int bpf_program_get_id_by_fd(int prog_fd, uint32_t *ret_id) {
×
363
        struct bpf_prog_info info = {};
×
364
        int r;
×
365

366
        assert(ret_id);
×
367

368
        r = bpf_program_get_info_by_fd(prog_fd, &info, sizeof(info));
×
369
        if (r < 0)
×
370
                return r;
×
371

372
        *ret_id = info.id;
×
373

374
        return 0;
×
375
};
376

377
int bpf_program_serialize_attachment(
14,865✔
378
                FILE *f,
379
                FDSet *fds,
380
                const char *key,
381
                BPFProgram *p) {
382

383
        _cleanup_free_ char *escaped = NULL;
14,865✔
384
        int copy, r;
14,865✔
385

386
        if (!p || !p->attached_path)
14,865✔
387
                return 0;
388

389
        assert(p->kernel_fd >= 0);
425✔
390

391
        escaped = cescape(p->attached_path);
425✔
392
        if (!escaped)
425✔
393
                return -ENOMEM;
394

395
        copy = fdset_put_dup(fds, p->kernel_fd);
425✔
396
        if (copy < 0)
425✔
397
                return log_error_errno(copy, "Failed to add BPF kernel fd to serialize: %m");
×
398

399
        r = serialize_item_format(
425✔
400
                        f,
401
                        key,
402
                        "%i %s %s",
403
                        copy,
404
                        bpf_cgroup_attach_type_to_string(p->attached_type),
405
                        escaped);
406
        if (r < 0)
425✔
407
                return r;
408

409
        /* After serialization, let's forget the fact that this program is attached. The attachment — if you
410
         * so will — is now 'owned' by the serialization, and not us anymore. Why does that matter? Because
411
         * of BPF's less-than-ideal lifecycle handling: to detach a program from a cgroup we have to
412
         * explicitly do so, it's not done implicitly on close(). Now, since we are serializing here we don't
413
         * want the program to be detached while freeing things, so that the attachment can be retained after
414
         * deserializing again. bpf_program_free() implicitly detaches things, if attached_path is non-NULL,
415
         * hence we set it to NULL here. */
416

417
        p->attached_path = mfree(p->attached_path);
425✔
418
        return 0;
425✔
419
}
420

421
int bpf_program_serialize_attachment_set(FILE *f, FDSet *fds, const char *key, Set *set) {
9,910✔
422
        BPFProgram *p;
9,910✔
423
        int r;
9,910✔
424

425
        SET_FOREACH(p, set) {
9,910✔
426
                r = bpf_program_serialize_attachment(f, fds, key, p);
×
427
                if (r < 0)
×
428
                        return r;
×
429
        }
430

431
        return 0;
9,910✔
432
}
433

434
int bpf_program_deserialize_attachment(const char *v, FDSet *fds, BPFProgram **bpfp) {
313✔
435
        _cleanup_free_ char *sfd = NULL, *sat = NULL, *unescaped = NULL;
313✔
436
        _cleanup_(bpf_program_freep) BPFProgram *p = NULL;
×
437
        _cleanup_close_ int fd = -EBADF;
313✔
438
        ssize_t l;
313✔
439
        int ifd, at, r;
313✔
440

441
        assert(v);
313✔
442
        assert(bpfp);
313✔
443

444
        /* Extract first word: the fd number */
445
        r = extract_first_word(&v, &sfd, NULL, 0);
313✔
446
        if (r < 0)
313✔
447
                return r;
448
        if (r == 0)
313✔
449
                return -EINVAL;
450

451
        ifd = parse_fd(sfd);
313✔
452
        if (ifd < 0)
313✔
453
                return r;
454

455
        /* Extract second word: the attach type */
456
        r = extract_first_word(&v, &sat, NULL, 0);
313✔
457
        if (r < 0)
313✔
458
                return r;
459
        if (r == 0)
313✔
460
                return -EINVAL;
461

462
        at = bpf_cgroup_attach_type_from_string(sat);
313✔
463
        if (at < 0)
313✔
464
                return at;
465

466
        /* The rest is the path */
467
        if (isempty(v))
626✔
468
                return -EINVAL;
469

470
        l = cunescape(v, 0, &unescaped);
313✔
471
        if (l < 0)
313✔
472
                return l;
×
473

474
        fd = fdset_remove(fds, ifd);
313✔
475
        if (fd < 0)
313✔
476
                return fd;
477

478
        p = new(BPFProgram, 1);
313✔
479
        if (!p)
313✔
480
                return -ENOMEM;
481

482
        *p = (BPFProgram) {
313✔
483
                .kernel_fd = TAKE_FD(fd),
313✔
484
                .prog_type = BPF_PROG_TYPE_UNSPEC,
485
                .attached_path = TAKE_PTR(unescaped),
313✔
486
                .attached_type = at,
487
        };
488

489
        if (*bpfp)
313✔
490
                bpf_program_free(*bpfp);
×
491

492
        *bpfp = TAKE_PTR(p);
313✔
493
        return 0;
313✔
494
}
495

496
int bpf_program_deserialize_attachment_set(const char *v, FDSet *fds, Set **bpfsetp) {
×
497
        BPFProgram *p = NULL;
×
498
        int r;
×
499

500
        assert(v);
×
501
        assert(bpfsetp);
×
502

503
        r = bpf_program_deserialize_attachment(v, fds, &p);
×
504
        if (r < 0)
×
505
                return r;
×
506

507
        r = set_ensure_consume(bpfsetp, &bpf_program_hash_ops, p);
×
508
        if (r < 0)
×
509
                return r;
×
510

511
        return 0;
512
}
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