Debian Codegraph @

src/bpf/restrict-fsaccess.bpf.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 /* The SPDX header above is actually correct in claiming this was
4 * LGPL-2.1-or-later, because it is. Since the kernel doesn't consider that
5 * compatible with GPL we will claim this to be GPL however, which should be
6 * fine given that LGPL-2.1-or-later downgrades to GPL if needed.
7 */
8
9 /* Trusted Execution BPF LSM program.
10 *
11 * Enforces that only binaries from signed dm-verity block devices (or the
12 * initramfs during early boot) can be executed.
13 *
14 * Architecture:
15 * - bdev_setintegrity hook: self-populates a map of trusted devices when
16 * dm-verity signals signature validity
17 * - bdev_free_security hook: removes devices from the map on teardown
18 * - bprm_check_security: blocks execve() from untrusted sources
19 * - mmap_file: blocks PROT_EXEC mmap from untrusted sources
20 * - file_mprotect: blocks W->X transitions from untrusted sources
21 */
22
23 /* If offsetof() is implemented via __builtin_offset() then it doesn't work on current compilers, since the
24 * built-ins do not understand CO-RE. Let's undefine any such macros here, to force bpf_helpers.h to define
25 * its own definitions for this. (In new versions it will do so automatically, but at least in libbpf 1.1.0
26 * it does not.) */
27 #undef offsetof
28 #undef container_of
29
30 #include "vmlinux.h"
31
32 #include <errno.h> /* IWYU pragma: keep */
33 #include <bpf/bpf_core_read.h>
34 #include <bpf/bpf_helpers.h>
35 #include <bpf/bpf_tracing.h>
36
37 #define PROT_EXEC 0x4
38 #define VM_EXEC 0x00000004
39 #define PTRACE_MODE_ATTACH 0x02
40
41 /* ---- Maps ---- */
42
43 struct {
44 __uint(type, BPF_MAP_TYPE_HASH);
45 __uint(max_entries, 0); /* placeholder */
46 __type(key, __u32); /* dev_t from bdev->bd_dev */
47 __type(value, __u8); /* 1 = signature valid */
48 } verity_devices SEC(".maps");
49
50 /* ---- Globals (set by PID1 via skeleton) ---- */
51
52 /* Device number of the initramfs superblock. PID1 sets this at load time and
53 * clears it (to 0) after switch_root. A value of 0 means "no initramfs trust
54 * — the window is closed." */
55 volatile __u32 initramfs_s_dev;
56
57 /* ---- Self-protection guard globals (set by PID1 after attach) ----
58 *
59 * While all IDs are 0 (the .bss default), the guard is inactive — no real BPF
60 * object has ID 0, so no comparisons match. PID1 populates these after
61 * attaching all programs. */
62 volatile __u32 protected_map_id_verity;
63 volatile __u32 protected_map_id_bss;
64
65 /* Must equal _RESTRICT_FILESYSTEM_ACCESS_LINK_MAX in bpf-restrict-fsaccess.h — update when adding programs */
66 #define NUM_PROTECTED_OBJS 9 /* 5 enforcement + 4 guard (bpf, bpf_map, bpf_prog, ptrace) */
67 volatile __u32 protected_prog_ids[NUM_PROTECTED_OBJS];
68 volatile __u32 protected_link_ids[NUM_PROTECTED_OBJS];
69
70 /* ---- Integrity tracking hooks ---- */
71
72 /* Preferred version: reads both value and size for defense-in-depth.
73 * Requires kernel v6.16+ or the backport of 1271a40eeafa ("bpf: Allow
74 * access to const void pointer arguments in tracing programs").
75 * On older kernels btf_ctx_access() rejects loads from const void *
76 * arguments because it fails to skip the CONST modifier when checking
77 * for void pointers. prepare_restrict_fsaccess_bpf() tries this version
78 * first and falls back to the _compat variant below if loading fails. */
79 SEC("lsm/bdev_setintegrity")
80 int BPF_PROG(restrict_fsaccess_bdev_setintegrity, struct block_device *bdev,
81 enum lsm_integrity_type type, const void *value, __u64 size)
82 {
83 if (type == LSM_INT_DMVERITY_SIG_VALID) {
84 __u32 dev = bdev->bd_dev;
85 __u8 valid = value && size > 0;
86 bpf_map_update_elem(&verity_devices, &dev, &valid, BPF_ANY);
87 }
88
89 return 0;
90 }
91
92 /* Compatibility version for kernels without 1271a40eeafa: does not
93 * read the const void *value argument (ctx[2]) to avoid the verifier
94 * rejection. Reads size (ctx[3]) directly from the raw context instead.
95 * This is safe because dm-verity guarantees value!=NULL iff size>0. */
96 #define BDEV_SETINTEGRITY_SIZE_CTX_IDX 3 /* bdev_setintegrity(bdev, type, value, size) */
97 SEC("lsm/bdev_setintegrity")
98 int BPF_PROG(restrict_fsaccess_bdev_setintegrity_compat, struct block_device *bdev,
99 enum lsm_integrity_type type)
100 {
101 if (type == LSM_INT_DMVERITY_SIG_VALID) {
102 __u32 dev = bdev->bd_dev;
103 __u8 valid = ctx[BDEV_SETINTEGRITY_SIZE_CTX_IDX] > 0;
104 bpf_map_update_elem(&verity_devices, &dev, &valid, BPF_ANY);
105 }
106
107 return 0;
108 }
109
110 SEC("lsm/bdev_free_security")
111 void BPF_PROG(restrict_fsaccess_bdev_free, struct block_device *bdev)
112 {
113 __u32 dev = bdev->bd_dev;
114 bpf_map_delete_elem(&verity_devices, &dev);
115 }
116
117 /* ---- Enforcement helpers ---- */
118
119 /* Check whether a file is from a trusted source.
120 * Returns 0 (allow) or -EPERM (deny). */
121 static __always_inline int check_trusted_file(struct file *file)
122 {
123 __u32 s_dev;
124 __u8 *sig_valid;
125
126 BPF_CORE_READ_INTO(&s_dev, file, f_inode, i_sb, s_dev);
127
128 /* Check initramfs trust (active only during early boot) */
129 if (initramfs_s_dev != 0 && s_dev == initramfs_s_dev)
130 return 0;
131
132 /* Check verity device map */
133 sig_valid = bpf_map_lookup_elem(&verity_devices, &s_dev);
134 if (sig_valid && *sig_valid)
135 return 0;
136
137 return -EPERM;
138 }
139
140 /* ---- Enforcement hooks ---- */
141
142 SEC("lsm/bprm_check_security")
143 int BPF_PROG(restrict_fsaccess_bprm_check, struct linux_binprm *bprm)
144 {
145 struct file *file;
146
147 BPF_CORE_READ_INTO(&file, bprm, file);
148 return check_trusted_file(file);
149 }
150
151 SEC("lsm/mmap_file")
152 int BPF_PROG(restrict_fsaccess_mmap_file, struct file *file, unsigned long reqprot,
153 unsigned long prot, unsigned long flags)
154 {
155 /* Only enforce on executable mappings */
156 if (!(prot & PROT_EXEC))
157 return 0;
158
159 /* Anonymous executable mapping — no file backing, deny */
160 if (!file)
161 return -EPERM;
162
163 return check_trusted_file(file);
164 }
165
166 SEC("lsm/file_mprotect")
167 int BPF_PROG(restrict_fsaccess_file_mprotect, struct vm_area_struct *vma,
168 unsigned long reqprot, unsigned long prot)
169 {
170 struct file *file;
171 unsigned long vm_flags;
172
173 /* Only enforce when adding PROT_EXEC */
174 if (!(prot & PROT_EXEC))
175 return 0;
176
177 /* If VM_EXEC is already set, the mapping is already executable — this
178 * mprotect isn't granting new executable capability, allow */
179 BPF_CORE_READ_INTO(&vm_flags, vma, vm_flags);
180 if (vm_flags & VM_EXEC)
181 return 0;
182
183 /* Anonymous executable mapping — no file backing, deny */
184 BPF_CORE_READ_INTO(&file, vma, vm_file);
185 if (!file)
186 return -EPERM;
187
188 return check_trusted_file(file);
189 }
190
191 /* ---- PID1 ptrace protection ----
192 *
193 * Blocks PTRACE_MODE_ATTACH access to PID1 from any other process. This
194 * prevents ptrace(PTRACE_ATTACH), /proc/1/mem, process_vm_readv(), and
195 * pidfd_getfd() from extracting sensitive state from PID1's address space.
196 *
197 * PTRACE_MODE_READ is allowed — monitoring tools and systemctl need
198 * /proc/1/status, /proc/1/fd/, /proc/1/ns/ *, etc.
199 *
200 * PID1 accessing itself is allowed. */
201
202 SEC("lsm/ptrace_access_check")
203 int BPF_PROG(restrict_fsaccess_ptrace_guard, struct task_struct *child,
204 unsigned int mode)
205 {
206 /* We only care about PID 1 and its threads (There are none but still.). */
207 if (child->tgid != 1)
208 return 0;
209
210 /* We only care about dangerous operations. */
211 if (!(mode & PTRACE_MODE_ATTACH))
212 return 0;
213
214 /* PID1 (any thread) accessing itself is allowed. */
215 if ((bpf_get_current_pid_tgid() >> 32) == 1)
216 return 0;
217
218 return -EPERM;
219 }
220
221 /* ---- Self-protection guard ----
222 *
223 * Three hooks protect our BPF objects from non-PID1 processes:
224 *
225 * lsm/bpf_map — fires inside bpf_map_new_fd(), the chokepoint for ALL
226 * code paths that produce a map FD (BPF_MAP_GET_FD_BY_ID,
227 * BPF_OBJ_GET, BPF_MAP_CREATE). Blocks the primary attack:
228 * obtaining an FD to verity_devices to inject fake trusted
229 * devices via BPF_MAP_UPDATE_ELEM.
230 *
231 * lsm/bpf_prog — fires inside bpf_prog_new_fd(), same chokepoint coverage
232 * for programs. Defense-in-depth.
233 *
234 * lsm/bpf — handles BPF_LINK_GET_FD_BY_ID only. There is no
235 * security_bpf_link() hook in the kernel, so link
236 * protection uses the command-level bpf() hook. This is
237 * sufficient: we don't pin links in production, so
238 * BPF_OBJ_GET is not an attack vector for links. */
239
240 SEC("lsm/bpf_map")
241 int BPF_PROG(restrict_fsaccess_bpf_map_guard, struct bpf_map *map,
242 unsigned int fmode)
243 {
244 __u32 id;
245
246 if ((bpf_get_current_pid_tgid() >> 32) == 1)
247 return 0;
248
249 id = map->id;
250 if (id != 0 && (id == protected_map_id_verity ||
251 id == protected_map_id_bss))
252 return -EPERM;
253
254 return 0;
255 }
256
257 SEC("lsm/bpf_prog")
258 int BPF_PROG(restrict_fsaccess_bpf_prog_guard, struct bpf_prog *prog)
259 {
260 __u32 id;
261
262 if ((bpf_get_current_pid_tgid() >> 32) == 1)
263 return 0;
264
265 id = BPF_CORE_READ(prog, aux, id);
266 if (id == 0)
267 return 0;
268
269 for (int i = 0; i < NUM_PROTECTED_OBJS; i++)
270 if (id == protected_prog_ids[i])
271 return -EPERM;
272
273 return 0;
274 }
275
276 SEC("lsm/bpf")
277 int BPF_PROG(restrict_fsaccess_bpf_guard, int cmd, union bpf_attr *attr,
278 unsigned int size)
279 {
280 __u32 id;
281
282 if ((bpf_get_current_pid_tgid() >> 32) == 1)
283 return 0;
284
285 if (cmd != BPF_LINK_GET_FD_BY_ID)
286 return 0;
287
288 /* link_id/map_id/prog_id share the same offset in the bpf_attr union */
289 id = attr->link_id;
290 if (id == 0)
291 return 0;
292
293 for (int i = 0; i < NUM_PROTECTED_OBJS; i++)
294 if (id == protected_link_ids[i])
295 return -EPERM;
296
297 return 0;
298 }
299
300 static const char _license[] SEC("license") = "GPL";