Debian Codegraph @

src/bpf/sysctl-monitor.bpf.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "vmlinux.h"
4
5 #include <errno.h>
6 #include <bpf/bpf_helpers.h>
7
8 #include "sysctl-write-event.h"
9
10 struct {
11 __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
12 __type(key, u32);
13 __type(value, u32);
14 __uint(max_entries, 1);
15 } cgroup_map SEC(".maps");
16
17 struct {
18 __uint(type, BPF_MAP_TYPE_RINGBUF);
19 __uint(max_entries, 256 * 1024);
20 } written_sysctls SEC(".maps");
21
22 static bool my_streq(const char *s1, const char *s2, size_t l) {
23 for (size_t i = 0; i < l; i++) {
24 if (s1[i] != s2[i])
25 return false;
26 if (s1[i] == 0)
27 return true;
28 }
29 return true;
30 }
31
32 struct str {
33 char *s;
34 size_t l;
35 };
36
37 static long cut_last(u64 i, struct str *str) {
38 char *s;
39
40 /* Sanity check for the preverifier */
41 if (i >= str->l)
42 return 1; /* exit from the loop */
43
44 i = str->l - i - 1;
45 s = str->s + i;
46
47 if (*s == 0)
48 return 0; /* continue */
49
50 if (*s == '\n' || *s == '\r' || *s == ' ' || *s == '\t') {
51 *s = 0;
52 return 0; /* continue */
53 }
54
55 return 1; /* exit from the loop */
56 }
57
58 /* Cut off trailing whitespace and newlines */
59 static void chop(char *s, size_t l) {
60 struct str str = { s, l };
61
62 bpf_loop(l, cut_last, &str, 0);
63 }
64
65 SEC("cgroup/sysctl")
66 int sysctl_monitor(struct bpf_sysctl *ctx) {
67 int r;
68
69 /* Allow reads */
70 if (!ctx->write)
71 return 1;
72
73 /* Declare the struct without contextually initializing it.
74 * This avoid zero-filling the struct, which would be a waste of
75 * resource and code size. Since we're sending an event even on failure,
76 * truncate the strings to zero size, in case we don't populate them. */
77 struct sysctl_write_event we;
78 we.version = 1;
79 we.errorcode = 0;
80 we.path[0] = 0;
81 we.comm[0] = 0;
82 we.current[0] = 0;
83 we.newvalue[0] = 0;
84
85 /* Set the simple values first */
86 we.pid = bpf_get_current_pid_tgid() >> 32;
87 we.cgroup_id = bpf_get_current_cgroup_id();
88
89 r = bpf_current_task_under_cgroup(&cgroup_map, 0);
90 if (r < 0) {
91 we.errorcode = r;
92 goto send_event;
93 }
94 if (r == 1)
95 return 1; /* Ignore events generated by us */
96
97 /* Only monitor /proc/sys/net/ */
98 r = bpf_sysctl_get_name(ctx, we.path, sizeof(we.path), 0);
99 if (r < 0) {
100 we.errorcode = r;
101 goto send_event;
102 }
103
104 if (bpf_strncmp(we.path, 4, "net/") != 0)
105 return 1;
106
107 r = bpf_get_current_comm(we.comm, sizeof(we.comm));
108 if (r < 0 && r != -EINVAL) { /* -EINVAL: the process is already vanished */
109 we.errorcode = r;
110 goto send_event;
111 }
112
113 r = bpf_sysctl_get_current_value(ctx, we.current, sizeof(we.current));
114 if (r < 0) {
115 we.errorcode = r;
116 goto send_event;
117 }
118
119 r = bpf_sysctl_get_new_value(ctx, we.newvalue, sizeof(we.newvalue));
120 if (r < 0) {
121 we.errorcode = r;
122 goto send_event;
123 }
124
125 /* Both the kernel and userspace applications add a newline at the end,
126 * remove it from both strings */
127 chop(we.current, sizeof(we.current));
128 chop(we.newvalue, sizeof(we.newvalue));
129
130 send_event:
131 /* If new value differs or we encountered an error, send the event */
132 if (r < 0 || !my_streq(we.current, we.newvalue, sizeof(we.current)))
133 bpf_ringbuf_output(&written_sysctls, &we, sizeof(we), 0);
134
135 return 1;
136 }
137
138 char _license[] SEC("license") = "GPL";