arch: fix broken BuildID for arm64 and riscv
[linux.git] / kernel / bpf / bpf_lsm.c
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>
10 #include <linux/binfmts.h>
11 #include <linux/lsm_hooks.h>
12 #include <linux/bpf_lsm.h>
13 #include <linux/kallsyms.h>
14 #include <linux/bpf_verifier.h>
15 #include <net/bpf_sk_storage.h>
16 #include <linux/bpf_local_storage.h>
17 #include <linux/btf_ids.h>
18 #include <linux/ima.h>
19 #include <linux/bpf-cgroup.h>
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
32
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)
38
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 */
44 #ifdef CONFIG_SECURITY_NETWORK
45 BTF_ID(func, bpf_lsm_sk_alloc_security)
46 BTF_ID(func, bpf_lsm_sk_free_security)
47 #endif
48 BTF_SET_END(bpf_lsm_current_hooks)
49
50 /* List of LSM hooks that trigger while the socket is properly locked.
51  */
52 BTF_SET_START(bpf_lsm_locked_sockopt_hooks)
53 #ifdef CONFIG_SECURITY_NETWORK
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)
58 #endif
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)
66 #ifdef CONFIG_SECURITY_NETWORK
67 BTF_ID(func, bpf_lsm_socket_post_create)
68 BTF_ID(func, bpf_lsm_socket_socketpair)
69 #endif
70 BTF_SET_END(bpf_lsm_unlocked_sockopt_hooks)
71
72 #ifdef CONFIG_CGROUP_BPF
73 void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog,
74                              bpf_func_t *bpf_func)
75 {
76         const struct btf_param *args __maybe_unused;
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
85 #ifdef CONFIG_NET
86         args = btf_params(prog->aux->attach_func_proto);
87
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 }
96 #endif
97
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
107         if (!btf_id_set_contains(&bpf_lsm_hooks, prog->aux->attach_btf_id)) {
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
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
130 static const struct bpf_func_proto bpf_bprm_opts_set_proto = {
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
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
151 static const struct bpf_func_proto bpf_ima_inode_hash_proto = {
152         .func           = bpf_ima_inode_hash,
153         .gpl_only       = false,
154         .might_sleep    = true,
155         .ret_type       = RET_INTEGER,
156         .arg1_type      = ARG_PTR_TO_BTF_ID,
157         .arg1_btf_id    = &bpf_ima_inode_hash_btf_ids[0],
158         .arg2_type      = ARG_PTR_TO_UNINIT_MEM,
159         .arg3_type      = ARG_CONST_SIZE,
160         .allowed        = bpf_ima_inode_hash_allowed,
161 };
162
163 BPF_CALL_3(bpf_ima_file_hash, struct file *, file, void *, dst, u32, size)
164 {
165         return ima_file_hash(file, dst, size);
166 }
167
168 BTF_ID_LIST_SINGLE(bpf_ima_file_hash_btf_ids, struct, file)
169
170 static const struct bpf_func_proto bpf_ima_file_hash_proto = {
171         .func           = bpf_ima_file_hash,
172         .gpl_only       = false,
173         .might_sleep    = true,
174         .ret_type       = RET_INTEGER,
175         .arg1_type      = ARG_PTR_TO_BTF_ID,
176         .arg1_btf_id    = &bpf_ima_file_hash_btf_ids[0],
177         .arg2_type      = ARG_PTR_TO_UNINIT_MEM,
178         .arg3_type      = ARG_CONST_SIZE,
179         .allowed        = bpf_ima_inode_hash_allowed,
180 };
181
182 BPF_CALL_1(bpf_get_attach_cookie, void *, ctx)
183 {
184         struct bpf_trace_run_ctx *run_ctx;
185
186         run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
187         return run_ctx->bpf_cookie;
188 }
189
190 static const struct bpf_func_proto bpf_get_attach_cookie_proto = {
191         .func           = bpf_get_attach_cookie,
192         .gpl_only       = false,
193         .ret_type       = RET_INTEGER,
194         .arg1_type      = ARG_PTR_TO_CTX,
195 };
196
197 static const struct bpf_func_proto *
198 bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
199 {
200         const struct bpf_func_proto *func_proto;
201
202         if (prog->expected_attach_type == BPF_LSM_CGROUP) {
203                 func_proto = cgroup_common_func_proto(func_id, prog);
204                 if (func_proto)
205                         return func_proto;
206         }
207
208         switch (func_id) {
209         case BPF_FUNC_inode_storage_get:
210                 return &bpf_inode_storage_get_proto;
211         case BPF_FUNC_inode_storage_delete:
212                 return &bpf_inode_storage_delete_proto;
213 #ifdef CONFIG_NET
214         case BPF_FUNC_sk_storage_get:
215                 return &bpf_sk_storage_get_proto;
216         case BPF_FUNC_sk_storage_delete:
217                 return &bpf_sk_storage_delete_proto;
218 #endif /* CONFIG_NET */
219         case BPF_FUNC_spin_lock:
220                 return &bpf_spin_lock_proto;
221         case BPF_FUNC_spin_unlock:
222                 return &bpf_spin_unlock_proto;
223         case BPF_FUNC_bprm_opts_set:
224                 return &bpf_bprm_opts_set_proto;
225         case BPF_FUNC_ima_inode_hash:
226                 return &bpf_ima_inode_hash_proto;
227         case BPF_FUNC_ima_file_hash:
228                 return &bpf_ima_file_hash_proto;
229         case BPF_FUNC_get_attach_cookie:
230                 return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto : NULL;
231 #ifdef CONFIG_NET
232         case BPF_FUNC_setsockopt:
233                 if (prog->expected_attach_type != BPF_LSM_CGROUP)
234                         return NULL;
235                 if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks,
236                                         prog->aux->attach_btf_id))
237                         return &bpf_sk_setsockopt_proto;
238                 if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks,
239                                         prog->aux->attach_btf_id))
240                         return &bpf_unlocked_sk_setsockopt_proto;
241                 return NULL;
242         case BPF_FUNC_getsockopt:
243                 if (prog->expected_attach_type != BPF_LSM_CGROUP)
244                         return NULL;
245                 if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks,
246                                         prog->aux->attach_btf_id))
247                         return &bpf_sk_getsockopt_proto;
248                 if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks,
249                                         prog->aux->attach_btf_id))
250                         return &bpf_unlocked_sk_getsockopt_proto;
251                 return NULL;
252 #endif
253         default:
254                 return tracing_prog_func_proto(func_id, prog);
255         }
256 }
257
258 /* The set of hooks which are called without pagefaults disabled and are allowed
259  * to "sleep" and thus can be used for sleepable BPF programs.
260  */
261 BTF_SET_START(sleepable_lsm_hooks)
262 BTF_ID(func, bpf_lsm_bpf)
263 BTF_ID(func, bpf_lsm_bpf_map)
264 BTF_ID(func, bpf_lsm_bpf_map_alloc_security)
265 BTF_ID(func, bpf_lsm_bpf_map_free_security)
266 BTF_ID(func, bpf_lsm_bpf_prog)
267 BTF_ID(func, bpf_lsm_bprm_check_security)
268 BTF_ID(func, bpf_lsm_bprm_committed_creds)
269 BTF_ID(func, bpf_lsm_bprm_committing_creds)
270 BTF_ID(func, bpf_lsm_bprm_creds_for_exec)
271 BTF_ID(func, bpf_lsm_bprm_creds_from_file)
272 BTF_ID(func, bpf_lsm_capget)
273 BTF_ID(func, bpf_lsm_capset)
274 BTF_ID(func, bpf_lsm_cred_prepare)
275 BTF_ID(func, bpf_lsm_file_ioctl)
276 BTF_ID(func, bpf_lsm_file_lock)
277 BTF_ID(func, bpf_lsm_file_open)
278 BTF_ID(func, bpf_lsm_file_receive)
279
280 #ifdef CONFIG_SECURITY_NETWORK
281 BTF_ID(func, bpf_lsm_inet_conn_established)
282 #endif /* CONFIG_SECURITY_NETWORK */
283
284 BTF_ID(func, bpf_lsm_inode_create)
285 BTF_ID(func, bpf_lsm_inode_free_security)
286 BTF_ID(func, bpf_lsm_inode_getattr)
287 BTF_ID(func, bpf_lsm_inode_getxattr)
288 BTF_ID(func, bpf_lsm_inode_mknod)
289 BTF_ID(func, bpf_lsm_inode_need_killpriv)
290 BTF_ID(func, bpf_lsm_inode_post_setxattr)
291 BTF_ID(func, bpf_lsm_inode_readlink)
292 BTF_ID(func, bpf_lsm_inode_rename)
293 BTF_ID(func, bpf_lsm_inode_rmdir)
294 BTF_ID(func, bpf_lsm_inode_setattr)
295 BTF_ID(func, bpf_lsm_inode_setxattr)
296 BTF_ID(func, bpf_lsm_inode_symlink)
297 BTF_ID(func, bpf_lsm_inode_unlink)
298 BTF_ID(func, bpf_lsm_kernel_module_request)
299 BTF_ID(func, bpf_lsm_kernel_read_file)
300 BTF_ID(func, bpf_lsm_kernfs_init_security)
301
302 #ifdef CONFIG_KEYS
303 BTF_ID(func, bpf_lsm_key_free)
304 #endif /* CONFIG_KEYS */
305
306 BTF_ID(func, bpf_lsm_mmap_file)
307 BTF_ID(func, bpf_lsm_netlink_send)
308 BTF_ID(func, bpf_lsm_path_notify)
309 BTF_ID(func, bpf_lsm_release_secctx)
310 BTF_ID(func, bpf_lsm_sb_alloc_security)
311 BTF_ID(func, bpf_lsm_sb_eat_lsm_opts)
312 BTF_ID(func, bpf_lsm_sb_kern_mount)
313 BTF_ID(func, bpf_lsm_sb_mount)
314 BTF_ID(func, bpf_lsm_sb_remount)
315 BTF_ID(func, bpf_lsm_sb_set_mnt_opts)
316 BTF_ID(func, bpf_lsm_sb_show_options)
317 BTF_ID(func, bpf_lsm_sb_statfs)
318 BTF_ID(func, bpf_lsm_sb_umount)
319 BTF_ID(func, bpf_lsm_settime)
320
321 #ifdef CONFIG_SECURITY_NETWORK
322 BTF_ID(func, bpf_lsm_socket_accept)
323 BTF_ID(func, bpf_lsm_socket_bind)
324 BTF_ID(func, bpf_lsm_socket_connect)
325 BTF_ID(func, bpf_lsm_socket_create)
326 BTF_ID(func, bpf_lsm_socket_getpeername)
327 BTF_ID(func, bpf_lsm_socket_getpeersec_dgram)
328 BTF_ID(func, bpf_lsm_socket_getsockname)
329 BTF_ID(func, bpf_lsm_socket_getsockopt)
330 BTF_ID(func, bpf_lsm_socket_listen)
331 BTF_ID(func, bpf_lsm_socket_post_create)
332 BTF_ID(func, bpf_lsm_socket_recvmsg)
333 BTF_ID(func, bpf_lsm_socket_sendmsg)
334 BTF_ID(func, bpf_lsm_socket_shutdown)
335 BTF_ID(func, bpf_lsm_socket_socketpair)
336 #endif /* CONFIG_SECURITY_NETWORK */
337
338 BTF_ID(func, bpf_lsm_syslog)
339 BTF_ID(func, bpf_lsm_task_alloc)
340 BTF_ID(func, bpf_lsm_current_getsecid_subj)
341 BTF_ID(func, bpf_lsm_task_getsecid_obj)
342 BTF_ID(func, bpf_lsm_task_prctl)
343 BTF_ID(func, bpf_lsm_task_setscheduler)
344 BTF_ID(func, bpf_lsm_task_to_inode)
345 BTF_ID(func, bpf_lsm_userns_create)
346 BTF_SET_END(sleepable_lsm_hooks)
347
348 BTF_SET_START(untrusted_lsm_hooks)
349 BTF_ID(func, bpf_lsm_bpf_map_free_security)
350 BTF_ID(func, bpf_lsm_bpf_prog_alloc_security)
351 BTF_ID(func, bpf_lsm_bpf_prog_free_security)
352 BTF_ID(func, bpf_lsm_file_alloc_security)
353 BTF_ID(func, bpf_lsm_file_free_security)
354 BTF_ID(func, bpf_lsm_sk_alloc_security)
355 BTF_ID(func, bpf_lsm_sk_free_security)
356 BTF_ID(func, bpf_lsm_task_free)
357 BTF_SET_END(untrusted_lsm_hooks)
358
359 bool bpf_lsm_is_sleepable_hook(u32 btf_id)
360 {
361         return btf_id_set_contains(&sleepable_lsm_hooks, btf_id);
362 }
363
364 bool bpf_lsm_is_trusted(const struct bpf_prog *prog)
365 {
366         return !btf_id_set_contains(&untrusted_lsm_hooks, prog->aux->attach_btf_id);
367 }
368
369 const struct bpf_prog_ops lsm_prog_ops = {
370 };
371
372 const struct bpf_verifier_ops lsm_verifier_ops = {
373         .get_func_proto = bpf_lsm_func_proto,
374         .is_valid_access = btf_ctx_access,
375 };
This page took 0.046217 seconds and 4 git commands to generate.