]>
Commit | Line | Data |
---|---|---|
fc611f47 KS |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
3 | /* | |
4 | * Copyright (C) 2020 Google LLC. | |
5 | */ | |
6 | ||
7 | #include <linux/filter.h> | |
8 | #include <linux/bpf.h> | |
9 | #include <linux/btf.h> | |
3f6719c7 | 10 | #include <linux/binfmts.h> |
9d3fdea7 KS |
11 | #include <linux/lsm_hooks.h> |
12 | #include <linux/bpf_lsm.h> | |
9e4e01df KS |
13 | #include <linux/kallsyms.h> |
14 | #include <linux/bpf_verifier.h> | |
30897832 KS |
15 | #include <net/bpf_sk_storage.h> |
16 | #include <linux/bpf_local_storage.h> | |
6f64e477 | 17 | #include <linux/btf_ids.h> |
27672f0d | 18 | #include <linux/ima.h> |
69fd337a | 19 | #include <linux/bpf-cgroup.h> |
9d3fdea7 KS |
20 | |
21 | /* For every LSM hook that allows attachment of BPF programs, declare a nop | |
22 | * function where a BPF program can be attached. | |
23 | */ | |
24 | #define LSM_HOOK(RET, DEFAULT, NAME, ...) \ | |
25 | noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ | |
26 | { \ | |
27 | return DEFAULT; \ | |
28 | } | |
29 | ||
30 | #include <linux/lsm_hook_defs.h> | |
31 | #undef LSM_HOOK | |
fc611f47 | 32 | |
6f64e477 KS |
33 | #define LSM_HOOK(RET, DEFAULT, NAME, ...) BTF_ID(func, bpf_lsm_##NAME) |
34 | BTF_SET_START(bpf_lsm_hooks) | |
35 | #include <linux/lsm_hook_defs.h> | |
36 | #undef LSM_HOOK | |
37 | BTF_SET_END(bpf_lsm_hooks) | |
9e4e01df | 38 | |
69fd337a SF |
39 | /* List of LSM hooks that should operate on 'current' cgroup regardless |
40 | * of function signature. | |
41 | */ | |
42 | BTF_SET_START(bpf_lsm_current_hooks) | |
43 | /* operate on freshly allocated sk without any cgroup association */ | |
ef331a8d | 44 | #ifdef CONFIG_SECURITY_NETWORK |
69fd337a SF |
45 | BTF_ID(func, bpf_lsm_sk_alloc_security) |
46 | BTF_ID(func, bpf_lsm_sk_free_security) | |
ef331a8d | 47 | #endif |
69fd337a SF |
48 | BTF_SET_END(bpf_lsm_current_hooks) |
49 | ||
9113d7e4 SF |
50 | /* List of LSM hooks that trigger while the socket is properly locked. |
51 | */ | |
52 | BTF_SET_START(bpf_lsm_locked_sockopt_hooks) | |
ef331a8d | 53 | #ifdef CONFIG_SECURITY_NETWORK |
9113d7e4 SF |
54 | BTF_ID(func, bpf_lsm_socket_sock_rcv_skb) |
55 | BTF_ID(func, bpf_lsm_sock_graft) | |
56 | BTF_ID(func, bpf_lsm_inet_csk_clone) | |
57 | BTF_ID(func, bpf_lsm_inet_conn_established) | |
ef331a8d | 58 | #endif |
9113d7e4 SF |
59 | BTF_SET_END(bpf_lsm_locked_sockopt_hooks) |
60 | ||
61 | /* List of LSM hooks that trigger while the socket is _not_ locked, | |
62 | * but it's ok to call bpf_{g,s}etsockopt because the socket is still | |
63 | * in the early init phase. | |
64 | */ | |
65 | BTF_SET_START(bpf_lsm_unlocked_sockopt_hooks) | |
ef331a8d | 66 | #ifdef CONFIG_SECURITY_NETWORK |
9113d7e4 SF |
67 | BTF_ID(func, bpf_lsm_socket_post_create) |
68 | BTF_ID(func, bpf_lsm_socket_socketpair) | |
ef331a8d | 69 | #endif |
9113d7e4 SF |
70 | BTF_SET_END(bpf_lsm_unlocked_sockopt_hooks) |
71 | ||
3908fcdd | 72 | #ifdef CONFIG_CGROUP_BPF |
69fd337a SF |
73 | void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, |
74 | bpf_func_t *bpf_func) | |
75 | { | |
3908fcdd | 76 | const struct btf_param *args __maybe_unused; |
69fd337a SF |
77 | |
78 | if (btf_type_vlen(prog->aux->attach_func_proto) < 1 || | |
79 | btf_id_set_contains(&bpf_lsm_current_hooks, | |
80 | prog->aux->attach_btf_id)) { | |
81 | *bpf_func = __cgroup_bpf_run_lsm_current; | |
82 | return; | |
83 | } | |
84 | ||
3908fcdd | 85 | #ifdef CONFIG_NET |
69fd337a SF |
86 | args = btf_params(prog->aux->attach_func_proto); |
87 | ||
69fd337a SF |
88 | if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCKET]) |
89 | *bpf_func = __cgroup_bpf_run_lsm_socket; | |
90 | else if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCK]) | |
91 | *bpf_func = __cgroup_bpf_run_lsm_sock; | |
92 | else | |
93 | #endif | |
94 | *bpf_func = __cgroup_bpf_run_lsm_current; | |
95 | } | |
3908fcdd | 96 | #endif |
69fd337a | 97 | |
9e4e01df KS |
98 | int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, |
99 | const struct bpf_prog *prog) | |
100 | { | |
101 | if (!prog->gpl_compatible) { | |
102 | bpf_log(vlog, | |
103 | "LSM programs must have a GPL compatible license\n"); | |
104 | return -EINVAL; | |
105 | } | |
106 | ||
6f64e477 | 107 | if (!btf_id_set_contains(&bpf_lsm_hooks, prog->aux->attach_btf_id)) { |
9e4e01df KS |
108 | bpf_log(vlog, "attach_btf_id %u points to wrong type name %s\n", |
109 | prog->aux->attach_btf_id, prog->aux->attach_func_name); | |
110 | return -EINVAL; | |
111 | } | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
3f6719c7 KS |
116 | /* Mask for all the currently supported BPRM option flags */ |
117 | #define BPF_F_BRPM_OPTS_MASK BPF_F_BPRM_SECUREEXEC | |
118 | ||
119 | BPF_CALL_2(bpf_bprm_opts_set, struct linux_binprm *, bprm, u64, flags) | |
120 | { | |
121 | if (flags & ~BPF_F_BRPM_OPTS_MASK) | |
122 | return -EINVAL; | |
123 | ||
124 | bprm->secureexec = (flags & BPF_F_BPRM_SECUREEXEC); | |
125 | return 0; | |
126 | } | |
127 | ||
128 | BTF_ID_LIST_SINGLE(bpf_bprm_opts_set_btf_ids, struct, linux_binprm) | |
129 | ||
e2c69f3a | 130 | static const struct bpf_func_proto bpf_bprm_opts_set_proto = { |
3f6719c7 KS |
131 | .func = bpf_bprm_opts_set, |
132 | .gpl_only = false, | |
133 | .ret_type = RET_INTEGER, | |
134 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
135 | .arg1_btf_id = &bpf_bprm_opts_set_btf_ids[0], | |
136 | .arg2_type = ARG_ANYTHING, | |
137 | }; | |
138 | ||
27672f0d KS |
139 | BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, size) |
140 | { | |
141 | return ima_inode_hash(inode, dst, size); | |
142 | } | |
143 | ||
144 | static bool bpf_ima_inode_hash_allowed(const struct bpf_prog *prog) | |
145 | { | |
146 | return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id); | |
147 | } | |
148 | ||
149 | BTF_ID_LIST_SINGLE(bpf_ima_inode_hash_btf_ids, struct, inode) | |
150 | ||
e2c69f3a | 151 | static const struct bpf_func_proto bpf_ima_inode_hash_proto = { |
27672f0d KS |
152 | .func = bpf_ima_inode_hash, |
153 | .gpl_only = false, | |
154 | .ret_type = RET_INTEGER, | |
155 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
156 | .arg1_btf_id = &bpf_ima_inode_hash_btf_ids[0], | |
157 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, | |
158 | .arg3_type = ARG_CONST_SIZE, | |
159 | .allowed = bpf_ima_inode_hash_allowed, | |
160 | }; | |
161 | ||
174b1694 RS |
162 | BPF_CALL_3(bpf_ima_file_hash, struct file *, file, void *, dst, u32, size) |
163 | { | |
164 | return ima_file_hash(file, dst, size); | |
165 | } | |
166 | ||
167 | BTF_ID_LIST_SINGLE(bpf_ima_file_hash_btf_ids, struct, file) | |
168 | ||
169 | static const struct bpf_func_proto bpf_ima_file_hash_proto = { | |
170 | .func = bpf_ima_file_hash, | |
171 | .gpl_only = false, | |
172 | .ret_type = RET_INTEGER, | |
173 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
174 | .arg1_btf_id = &bpf_ima_file_hash_btf_ids[0], | |
175 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, | |
176 | .arg3_type = ARG_CONST_SIZE, | |
177 | .allowed = bpf_ima_inode_hash_allowed, | |
178 | }; | |
179 | ||
2fcc8241 KFL |
180 | BPF_CALL_1(bpf_get_attach_cookie, void *, ctx) |
181 | { | |
182 | struct bpf_trace_run_ctx *run_ctx; | |
183 | ||
184 | run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx); | |
185 | return run_ctx->bpf_cookie; | |
186 | } | |
187 | ||
188 | static const struct bpf_func_proto bpf_get_attach_cookie_proto = { | |
189 | .func = bpf_get_attach_cookie, | |
190 | .gpl_only = false, | |
191 | .ret_type = RET_INTEGER, | |
192 | .arg1_type = ARG_PTR_TO_CTX, | |
193 | }; | |
194 | ||
30897832 KS |
195 | static const struct bpf_func_proto * |
196 | bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
197 | { | |
bed89185 SF |
198 | const struct bpf_func_proto *func_proto; |
199 | ||
200 | if (prog->expected_attach_type == BPF_LSM_CGROUP) { | |
201 | func_proto = cgroup_common_func_proto(func_id, prog); | |
202 | if (func_proto) | |
203 | return func_proto; | |
204 | } | |
205 | ||
30897832 KS |
206 | switch (func_id) { |
207 | case BPF_FUNC_inode_storage_get: | |
208 | return &bpf_inode_storage_get_proto; | |
209 | case BPF_FUNC_inode_storage_delete: | |
210 | return &bpf_inode_storage_delete_proto; | |
5c9d706f | 211 | #ifdef CONFIG_NET |
30897832 | 212 | case BPF_FUNC_sk_storage_get: |
592a3498 | 213 | return &bpf_sk_storage_get_proto; |
30897832 | 214 | case BPF_FUNC_sk_storage_delete: |
592a3498 | 215 | return &bpf_sk_storage_delete_proto; |
5c9d706f | 216 | #endif /* CONFIG_NET */ |
9e7a4d98 KS |
217 | case BPF_FUNC_spin_lock: |
218 | return &bpf_spin_lock_proto; | |
219 | case BPF_FUNC_spin_unlock: | |
220 | return &bpf_spin_unlock_proto; | |
3f6719c7 KS |
221 | case BPF_FUNC_bprm_opts_set: |
222 | return &bpf_bprm_opts_set_proto; | |
27672f0d KS |
223 | case BPF_FUNC_ima_inode_hash: |
224 | return prog->aux->sleepable ? &bpf_ima_inode_hash_proto : NULL; | |
174b1694 RS |
225 | case BPF_FUNC_ima_file_hash: |
226 | return prog->aux->sleepable ? &bpf_ima_file_hash_proto : NULL; | |
2fcc8241 KFL |
227 | case BPF_FUNC_get_attach_cookie: |
228 | return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto : NULL; | |
3908fcdd | 229 | #ifdef CONFIG_NET |
9113d7e4 SF |
230 | case BPF_FUNC_setsockopt: |
231 | if (prog->expected_attach_type != BPF_LSM_CGROUP) | |
232 | return NULL; | |
233 | if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks, | |
234 | prog->aux->attach_btf_id)) | |
235 | return &bpf_sk_setsockopt_proto; | |
236 | if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks, | |
237 | prog->aux->attach_btf_id)) | |
238 | return &bpf_unlocked_sk_setsockopt_proto; | |
239 | return NULL; | |
240 | case BPF_FUNC_getsockopt: | |
241 | if (prog->expected_attach_type != BPF_LSM_CGROUP) | |
242 | return NULL; | |
243 | if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks, | |
244 | prog->aux->attach_btf_id)) | |
245 | return &bpf_sk_getsockopt_proto; | |
246 | if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks, | |
247 | prog->aux->attach_btf_id)) | |
248 | return &bpf_unlocked_sk_getsockopt_proto; | |
249 | return NULL; | |
3908fcdd | 250 | #endif |
30897832 KS |
251 | default: |
252 | return tracing_prog_func_proto(func_id, prog); | |
253 | } | |
254 | } | |
255 | ||
423f1610 | 256 | /* The set of hooks which are called without pagefaults disabled and are allowed |
712b78c6 | 257 | * to "sleep" and thus can be used for sleepable BPF programs. |
423f1610 KS |
258 | */ |
259 | BTF_SET_START(sleepable_lsm_hooks) | |
260 | BTF_ID(func, bpf_lsm_bpf) | |
261 | BTF_ID(func, bpf_lsm_bpf_map) | |
262 | BTF_ID(func, bpf_lsm_bpf_map_alloc_security) | |
263 | BTF_ID(func, bpf_lsm_bpf_map_free_security) | |
264 | BTF_ID(func, bpf_lsm_bpf_prog) | |
265 | BTF_ID(func, bpf_lsm_bprm_check_security) | |
266 | BTF_ID(func, bpf_lsm_bprm_committed_creds) | |
267 | BTF_ID(func, bpf_lsm_bprm_committing_creds) | |
268 | BTF_ID(func, bpf_lsm_bprm_creds_for_exec) | |
269 | BTF_ID(func, bpf_lsm_bprm_creds_from_file) | |
270 | BTF_ID(func, bpf_lsm_capget) | |
271 | BTF_ID(func, bpf_lsm_capset) | |
272 | BTF_ID(func, bpf_lsm_cred_prepare) | |
273 | BTF_ID(func, bpf_lsm_file_ioctl) | |
274 | BTF_ID(func, bpf_lsm_file_lock) | |
275 | BTF_ID(func, bpf_lsm_file_open) | |
276 | BTF_ID(func, bpf_lsm_file_receive) | |
78031381 MY |
277 | |
278 | #ifdef CONFIG_SECURITY_NETWORK | |
423f1610 | 279 | BTF_ID(func, bpf_lsm_inet_conn_established) |
78031381 MY |
280 | #endif /* CONFIG_SECURITY_NETWORK */ |
281 | ||
423f1610 KS |
282 | BTF_ID(func, bpf_lsm_inode_create) |
283 | BTF_ID(func, bpf_lsm_inode_free_security) | |
284 | BTF_ID(func, bpf_lsm_inode_getattr) | |
285 | BTF_ID(func, bpf_lsm_inode_getxattr) | |
286 | BTF_ID(func, bpf_lsm_inode_mknod) | |
287 | BTF_ID(func, bpf_lsm_inode_need_killpriv) | |
288 | BTF_ID(func, bpf_lsm_inode_post_setxattr) | |
289 | BTF_ID(func, bpf_lsm_inode_readlink) | |
290 | BTF_ID(func, bpf_lsm_inode_rename) | |
291 | BTF_ID(func, bpf_lsm_inode_rmdir) | |
292 | BTF_ID(func, bpf_lsm_inode_setattr) | |
293 | BTF_ID(func, bpf_lsm_inode_setxattr) | |
294 | BTF_ID(func, bpf_lsm_inode_symlink) | |
295 | BTF_ID(func, bpf_lsm_inode_unlink) | |
296 | BTF_ID(func, bpf_lsm_kernel_module_request) | |
df6b3039 | 297 | BTF_ID(func, bpf_lsm_kernel_read_file) |
423f1610 | 298 | BTF_ID(func, bpf_lsm_kernfs_init_security) |
78031381 MY |
299 | |
300 | #ifdef CONFIG_KEYS | |
423f1610 | 301 | BTF_ID(func, bpf_lsm_key_free) |
78031381 MY |
302 | #endif /* CONFIG_KEYS */ |
303 | ||
423f1610 KS |
304 | BTF_ID(func, bpf_lsm_mmap_file) |
305 | BTF_ID(func, bpf_lsm_netlink_send) | |
306 | BTF_ID(func, bpf_lsm_path_notify) | |
307 | BTF_ID(func, bpf_lsm_release_secctx) | |
308 | BTF_ID(func, bpf_lsm_sb_alloc_security) | |
309 | BTF_ID(func, bpf_lsm_sb_eat_lsm_opts) | |
310 | BTF_ID(func, bpf_lsm_sb_kern_mount) | |
311 | BTF_ID(func, bpf_lsm_sb_mount) | |
312 | BTF_ID(func, bpf_lsm_sb_remount) | |
313 | BTF_ID(func, bpf_lsm_sb_set_mnt_opts) | |
314 | BTF_ID(func, bpf_lsm_sb_show_options) | |
315 | BTF_ID(func, bpf_lsm_sb_statfs) | |
316 | BTF_ID(func, bpf_lsm_sb_umount) | |
317 | BTF_ID(func, bpf_lsm_settime) | |
78031381 MY |
318 | |
319 | #ifdef CONFIG_SECURITY_NETWORK | |
423f1610 KS |
320 | BTF_ID(func, bpf_lsm_socket_accept) |
321 | BTF_ID(func, bpf_lsm_socket_bind) | |
322 | BTF_ID(func, bpf_lsm_socket_connect) | |
323 | BTF_ID(func, bpf_lsm_socket_create) | |
324 | BTF_ID(func, bpf_lsm_socket_getpeername) | |
325 | BTF_ID(func, bpf_lsm_socket_getpeersec_dgram) | |
326 | BTF_ID(func, bpf_lsm_socket_getsockname) | |
327 | BTF_ID(func, bpf_lsm_socket_getsockopt) | |
328 | BTF_ID(func, bpf_lsm_socket_listen) | |
329 | BTF_ID(func, bpf_lsm_socket_post_create) | |
330 | BTF_ID(func, bpf_lsm_socket_recvmsg) | |
331 | BTF_ID(func, bpf_lsm_socket_sendmsg) | |
332 | BTF_ID(func, bpf_lsm_socket_shutdown) | |
333 | BTF_ID(func, bpf_lsm_socket_socketpair) | |
78031381 MY |
334 | #endif /* CONFIG_SECURITY_NETWORK */ |
335 | ||
423f1610 KS |
336 | BTF_ID(func, bpf_lsm_syslog) |
337 | BTF_ID(func, bpf_lsm_task_alloc) | |
63ee956f | 338 | BTF_ID(func, bpf_lsm_current_getsecid_subj) |
4ebd7651 | 339 | BTF_ID(func, bpf_lsm_task_getsecid_obj) |
423f1610 KS |
340 | BTF_ID(func, bpf_lsm_task_prctl) |
341 | BTF_ID(func, bpf_lsm_task_setscheduler) | |
342 | BTF_ID(func, bpf_lsm_task_to_inode) | |
401e64b3 | 343 | BTF_ID(func, bpf_lsm_userns_create) |
423f1610 KS |
344 | BTF_SET_END(sleepable_lsm_hooks) |
345 | ||
346 | bool bpf_lsm_is_sleepable_hook(u32 btf_id) | |
347 | { | |
348 | return btf_id_set_contains(&sleepable_lsm_hooks, btf_id); | |
349 | } | |
350 | ||
fc611f47 KS |
351 | const struct bpf_prog_ops lsm_prog_ops = { |
352 | }; | |
353 | ||
354 | const struct bpf_verifier_ops lsm_verifier_ops = { | |
30897832 | 355 | .get_func_proto = bpf_lsm_func_proto, |
fc611f47 KS |
356 | .is_valid_access = btf_ctx_access, |
357 | }; |