rcuscale: Move shutdown from wait_event() to wait_event_idle()
[platform/kernel/linux-starfive.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_sock_graft)
55 BTF_ID(func, bpf_lsm_inet_csk_clone)
56 BTF_ID(func, bpf_lsm_inet_conn_established)
57 #endif
58 BTF_SET_END(bpf_lsm_locked_sockopt_hooks)
59
60 /* List of LSM hooks that trigger while the socket is _not_ locked,
61  * but it's ok to call bpf_{g,s}etsockopt because the socket is still
62  * in the early init phase.
63  */
64 BTF_SET_START(bpf_lsm_unlocked_sockopt_hooks)
65 #ifdef CONFIG_SECURITY_NETWORK
66 BTF_ID(func, bpf_lsm_socket_post_create)
67 BTF_ID(func, bpf_lsm_socket_socketpair)
68 #endif
69 BTF_SET_END(bpf_lsm_unlocked_sockopt_hooks)
70
71 #ifdef CONFIG_CGROUP_BPF
72 void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog,
73                              bpf_func_t *bpf_func)
74 {
75         const struct btf_param *args __maybe_unused;
76
77         if (btf_type_vlen(prog->aux->attach_func_proto) < 1 ||
78             btf_id_set_contains(&bpf_lsm_current_hooks,
79                                 prog->aux->attach_btf_id)) {
80                 *bpf_func = __cgroup_bpf_run_lsm_current;
81                 return;
82         }
83
84 #ifdef CONFIG_NET
85         args = btf_params(prog->aux->attach_func_proto);
86
87         if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCKET])
88                 *bpf_func = __cgroup_bpf_run_lsm_socket;
89         else if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCK])
90                 *bpf_func = __cgroup_bpf_run_lsm_sock;
91         else
92 #endif
93                 *bpf_func = __cgroup_bpf_run_lsm_current;
94 }
95 #endif
96
97 int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
98                         const struct bpf_prog *prog)
99 {
100         if (!prog->gpl_compatible) {
101                 bpf_log(vlog,
102                         "LSM programs must have a GPL compatible license\n");
103                 return -EINVAL;
104         }
105
106         if (!btf_id_set_contains(&bpf_lsm_hooks, prog->aux->attach_btf_id)) {
107                 bpf_log(vlog, "attach_btf_id %u points to wrong type name %s\n",
108                         prog->aux->attach_btf_id, prog->aux->attach_func_name);
109                 return -EINVAL;
110         }
111
112         return 0;
113 }
114
115 /* Mask for all the currently supported BPRM option flags */
116 #define BPF_F_BRPM_OPTS_MASK    BPF_F_BPRM_SECUREEXEC
117
118 BPF_CALL_2(bpf_bprm_opts_set, struct linux_binprm *, bprm, u64, flags)
119 {
120         if (flags & ~BPF_F_BRPM_OPTS_MASK)
121                 return -EINVAL;
122
123         bprm->secureexec = (flags & BPF_F_BPRM_SECUREEXEC);
124         return 0;
125 }
126
127 BTF_ID_LIST_SINGLE(bpf_bprm_opts_set_btf_ids, struct, linux_binprm)
128
129 static const struct bpf_func_proto bpf_bprm_opts_set_proto = {
130         .func           = bpf_bprm_opts_set,
131         .gpl_only       = false,
132         .ret_type       = RET_INTEGER,
133         .arg1_type      = ARG_PTR_TO_BTF_ID,
134         .arg1_btf_id    = &bpf_bprm_opts_set_btf_ids[0],
135         .arg2_type      = ARG_ANYTHING,
136 };
137
138 BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, size)
139 {
140         return ima_inode_hash(inode, dst, size);
141 }
142
143 static bool bpf_ima_inode_hash_allowed(const struct bpf_prog *prog)
144 {
145         return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
146 }
147
148 BTF_ID_LIST_SINGLE(bpf_ima_inode_hash_btf_ids, struct, inode)
149
150 static const struct bpf_func_proto bpf_ima_inode_hash_proto = {
151         .func           = bpf_ima_inode_hash,
152         .gpl_only       = false,
153         .ret_type       = RET_INTEGER,
154         .arg1_type      = ARG_PTR_TO_BTF_ID,
155         .arg1_btf_id    = &bpf_ima_inode_hash_btf_ids[0],
156         .arg2_type      = ARG_PTR_TO_UNINIT_MEM,
157         .arg3_type      = ARG_CONST_SIZE,
158         .allowed        = bpf_ima_inode_hash_allowed,
159 };
160
161 BPF_CALL_3(bpf_ima_file_hash, struct file *, file, void *, dst, u32, size)
162 {
163         return ima_file_hash(file, dst, size);
164 }
165
166 BTF_ID_LIST_SINGLE(bpf_ima_file_hash_btf_ids, struct, file)
167
168 static const struct bpf_func_proto bpf_ima_file_hash_proto = {
169         .func           = bpf_ima_file_hash,
170         .gpl_only       = false,
171         .ret_type       = RET_INTEGER,
172         .arg1_type      = ARG_PTR_TO_BTF_ID,
173         .arg1_btf_id    = &bpf_ima_file_hash_btf_ids[0],
174         .arg2_type      = ARG_PTR_TO_UNINIT_MEM,
175         .arg3_type      = ARG_CONST_SIZE,
176         .allowed        = bpf_ima_inode_hash_allowed,
177 };
178
179 BPF_CALL_1(bpf_get_attach_cookie, void *, ctx)
180 {
181         struct bpf_trace_run_ctx *run_ctx;
182
183         run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
184         return run_ctx->bpf_cookie;
185 }
186
187 static const struct bpf_func_proto bpf_get_attach_cookie_proto = {
188         .func           = bpf_get_attach_cookie,
189         .gpl_only       = false,
190         .ret_type       = RET_INTEGER,
191         .arg1_type      = ARG_PTR_TO_CTX,
192 };
193
194 static const struct bpf_func_proto *
195 bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
196 {
197         const struct bpf_func_proto *func_proto;
198
199         if (prog->expected_attach_type == BPF_LSM_CGROUP) {
200                 func_proto = cgroup_common_func_proto(func_id, prog);
201                 if (func_proto)
202                         return func_proto;
203         }
204
205         switch (func_id) {
206         case BPF_FUNC_inode_storage_get:
207                 return &bpf_inode_storage_get_proto;
208         case BPF_FUNC_inode_storage_delete:
209                 return &bpf_inode_storage_delete_proto;
210 #ifdef CONFIG_NET
211         case BPF_FUNC_sk_storage_get:
212                 return &bpf_sk_storage_get_proto;
213         case BPF_FUNC_sk_storage_delete:
214                 return &bpf_sk_storage_delete_proto;
215 #endif /* CONFIG_NET */
216         case BPF_FUNC_spin_lock:
217                 return &bpf_spin_lock_proto;
218         case BPF_FUNC_spin_unlock:
219                 return &bpf_spin_unlock_proto;
220         case BPF_FUNC_bprm_opts_set:
221                 return &bpf_bprm_opts_set_proto;
222         case BPF_FUNC_ima_inode_hash:
223                 return prog->aux->sleepable ? &bpf_ima_inode_hash_proto : NULL;
224         case BPF_FUNC_ima_file_hash:
225                 return prog->aux->sleepable ? &bpf_ima_file_hash_proto : NULL;
226         case BPF_FUNC_get_attach_cookie:
227                 return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto : NULL;
228 #ifdef CONFIG_NET
229         case BPF_FUNC_setsockopt:
230                 if (prog->expected_attach_type != BPF_LSM_CGROUP)
231                         return NULL;
232                 if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks,
233                                         prog->aux->attach_btf_id))
234                         return &bpf_sk_setsockopt_proto;
235                 if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks,
236                                         prog->aux->attach_btf_id))
237                         return &bpf_unlocked_sk_setsockopt_proto;
238                 return NULL;
239         case BPF_FUNC_getsockopt:
240                 if (prog->expected_attach_type != BPF_LSM_CGROUP)
241                         return NULL;
242                 if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks,
243                                         prog->aux->attach_btf_id))
244                         return &bpf_sk_getsockopt_proto;
245                 if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks,
246                                         prog->aux->attach_btf_id))
247                         return &bpf_unlocked_sk_getsockopt_proto;
248                 return NULL;
249 #endif
250         default:
251                 return tracing_prog_func_proto(func_id, prog);
252         }
253 }
254
255 /* The set of hooks which are called without pagefaults disabled and are allowed
256  * to "sleep" and thus can be used for sleepable BPF programs.
257  */
258 BTF_SET_START(sleepable_lsm_hooks)
259 BTF_ID(func, bpf_lsm_bpf)
260 BTF_ID(func, bpf_lsm_bpf_map)
261 BTF_ID(func, bpf_lsm_bpf_map_alloc_security)
262 BTF_ID(func, bpf_lsm_bpf_map_free_security)
263 BTF_ID(func, bpf_lsm_bpf_prog)
264 BTF_ID(func, bpf_lsm_bprm_check_security)
265 BTF_ID(func, bpf_lsm_bprm_committed_creds)
266 BTF_ID(func, bpf_lsm_bprm_committing_creds)
267 BTF_ID(func, bpf_lsm_bprm_creds_for_exec)
268 BTF_ID(func, bpf_lsm_bprm_creds_from_file)
269 BTF_ID(func, bpf_lsm_capget)
270 BTF_ID(func, bpf_lsm_capset)
271 BTF_ID(func, bpf_lsm_cred_prepare)
272 BTF_ID(func, bpf_lsm_file_ioctl)
273 BTF_ID(func, bpf_lsm_file_lock)
274 BTF_ID(func, bpf_lsm_file_open)
275 BTF_ID(func, bpf_lsm_file_receive)
276
277 #ifdef CONFIG_SECURITY_NETWORK
278 BTF_ID(func, bpf_lsm_inet_conn_established)
279 #endif /* CONFIG_SECURITY_NETWORK */
280
281 BTF_ID(func, bpf_lsm_inode_create)
282 BTF_ID(func, bpf_lsm_inode_free_security)
283 BTF_ID(func, bpf_lsm_inode_getattr)
284 BTF_ID(func, bpf_lsm_inode_getxattr)
285 BTF_ID(func, bpf_lsm_inode_mknod)
286 BTF_ID(func, bpf_lsm_inode_need_killpriv)
287 BTF_ID(func, bpf_lsm_inode_post_setxattr)
288 BTF_ID(func, bpf_lsm_inode_readlink)
289 BTF_ID(func, bpf_lsm_inode_rename)
290 BTF_ID(func, bpf_lsm_inode_rmdir)
291 BTF_ID(func, bpf_lsm_inode_setattr)
292 BTF_ID(func, bpf_lsm_inode_setxattr)
293 BTF_ID(func, bpf_lsm_inode_symlink)
294 BTF_ID(func, bpf_lsm_inode_unlink)
295 BTF_ID(func, bpf_lsm_kernel_module_request)
296 BTF_ID(func, bpf_lsm_kernel_read_file)
297 BTF_ID(func, bpf_lsm_kernfs_init_security)
298
299 #ifdef CONFIG_KEYS
300 BTF_ID(func, bpf_lsm_key_free)
301 #endif /* CONFIG_KEYS */
302
303 BTF_ID(func, bpf_lsm_mmap_file)
304 BTF_ID(func, bpf_lsm_netlink_send)
305 BTF_ID(func, bpf_lsm_path_notify)
306 BTF_ID(func, bpf_lsm_release_secctx)
307 BTF_ID(func, bpf_lsm_sb_alloc_security)
308 BTF_ID(func, bpf_lsm_sb_eat_lsm_opts)
309 BTF_ID(func, bpf_lsm_sb_kern_mount)
310 BTF_ID(func, bpf_lsm_sb_mount)
311 BTF_ID(func, bpf_lsm_sb_remount)
312 BTF_ID(func, bpf_lsm_sb_set_mnt_opts)
313 BTF_ID(func, bpf_lsm_sb_show_options)
314 BTF_ID(func, bpf_lsm_sb_statfs)
315 BTF_ID(func, bpf_lsm_sb_umount)
316 BTF_ID(func, bpf_lsm_settime)
317
318 #ifdef CONFIG_SECURITY_NETWORK
319 BTF_ID(func, bpf_lsm_socket_accept)
320 BTF_ID(func, bpf_lsm_socket_bind)
321 BTF_ID(func, bpf_lsm_socket_connect)
322 BTF_ID(func, bpf_lsm_socket_create)
323 BTF_ID(func, bpf_lsm_socket_getpeername)
324 BTF_ID(func, bpf_lsm_socket_getpeersec_dgram)
325 BTF_ID(func, bpf_lsm_socket_getsockname)
326 BTF_ID(func, bpf_lsm_socket_getsockopt)
327 BTF_ID(func, bpf_lsm_socket_listen)
328 BTF_ID(func, bpf_lsm_socket_post_create)
329 BTF_ID(func, bpf_lsm_socket_recvmsg)
330 BTF_ID(func, bpf_lsm_socket_sendmsg)
331 BTF_ID(func, bpf_lsm_socket_shutdown)
332 BTF_ID(func, bpf_lsm_socket_socketpair)
333 #endif /* CONFIG_SECURITY_NETWORK */
334
335 BTF_ID(func, bpf_lsm_syslog)
336 BTF_ID(func, bpf_lsm_task_alloc)
337 BTF_ID(func, bpf_lsm_current_getsecid_subj)
338 BTF_ID(func, bpf_lsm_task_getsecid_obj)
339 BTF_ID(func, bpf_lsm_task_prctl)
340 BTF_ID(func, bpf_lsm_task_setscheduler)
341 BTF_ID(func, bpf_lsm_task_to_inode)
342 BTF_ID(func, bpf_lsm_userns_create)
343 BTF_SET_END(sleepable_lsm_hooks)
344
345 bool bpf_lsm_is_sleepable_hook(u32 btf_id)
346 {
347         return btf_id_set_contains(&sleepable_lsm_hooks, btf_id);
348 }
349
350 const struct bpf_prog_ops lsm_prog_ops = {
351 };
352
353 const struct bpf_verifier_ops lsm_verifier_ops = {
354         .get_func_proto = bpf_lsm_func_proto,
355         .is_valid_access = btf_ctx_access,
356 };