xref: /linux/kernel/bpf/bpf_lsm.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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 <net/bpf_sk_storage.h>
15 #include <linux/bpf_local_storage.h>
16 #include <linux/btf_ids.h>
17 #include <linux/ima.h>
18 #include <linux/bpf-cgroup.h>
19 
20 /* For every LSM hook that allows attachment of BPF programs, declare a nop
21  * function where a BPF program can be attached. Notably, we qualify each with
22  * weak linkage such that strong overrides can be implemented if need be.
23  */
24 #define LSM_HOOK(RET, DEFAULT, NAME, ...)	\
25 __weak 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 BTF_SET_START(bpf_lsm_disabled_hooks)
40 BTF_ID(func, bpf_lsm_vm_enough_memory)
41 BTF_ID(func, bpf_lsm_inode_need_killpriv)
42 BTF_ID(func, bpf_lsm_inode_getsecurity)
43 BTF_ID(func, bpf_lsm_inode_listsecurity)
44 BTF_ID(func, bpf_lsm_inode_copy_up_xattr)
45 BTF_ID(func, bpf_lsm_getselfattr)
46 BTF_ID(func, bpf_lsm_getprocattr)
47 BTF_ID(func, bpf_lsm_setprocattr)
48 #ifdef CONFIG_KEYS
49 BTF_ID(func, bpf_lsm_key_getsecurity)
50 #endif
51 #ifdef CONFIG_AUDIT
52 BTF_ID(func, bpf_lsm_audit_rule_match)
53 #endif
54 BTF_ID(func, bpf_lsm_ismaclabel)
55 BTF_ID(func, bpf_lsm_file_alloc_security)
56 BTF_SET_END(bpf_lsm_disabled_hooks)
57 
58 /* List of LSM hooks that should operate on 'current' cgroup regardless
59  * of function signature.
60  */
61 BTF_SET_START(bpf_lsm_current_hooks)
62 /* operate on freshly allocated sk without any cgroup association */
63 #ifdef CONFIG_SECURITY_NETWORK
64 BTF_ID(func, bpf_lsm_sk_alloc_security)
65 BTF_ID(func, bpf_lsm_sk_free_security)
66 #endif
67 BTF_SET_END(bpf_lsm_current_hooks)
68 
69 /* List of LSM hooks that trigger while the socket is properly locked.
70  */
71 BTF_SET_START(bpf_lsm_locked_sockopt_hooks)
72 #ifdef CONFIG_SECURITY_NETWORK
73 BTF_ID(func, bpf_lsm_sock_graft)
74 BTF_ID(func, bpf_lsm_inet_csk_clone)
75 BTF_ID(func, bpf_lsm_inet_conn_established)
76 #endif
77 BTF_SET_END(bpf_lsm_locked_sockopt_hooks)
78 
79 /* List of LSM hooks that trigger while the socket is _not_ locked,
80  * but it's ok to call bpf_{g,s}etsockopt because the socket is still
81  * in the early init phase.
82  */
83 BTF_SET_START(bpf_lsm_unlocked_sockopt_hooks)
84 #ifdef CONFIG_SECURITY_NETWORK
85 BTF_ID(func, bpf_lsm_socket_post_create)
86 BTF_ID(func, bpf_lsm_socket_socketpair)
87 #endif
88 BTF_SET_END(bpf_lsm_unlocked_sockopt_hooks)
89 
90 #ifdef CONFIG_CGROUP_BPF
91 void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog,
92 			     bpf_func_t *bpf_func)
93 {
94 	const struct btf_param *args __maybe_unused;
95 
96 	if (btf_type_vlen(prog->aux->attach_func_proto) < 1 ||
97 	    btf_id_set_contains(&bpf_lsm_current_hooks,
98 				prog->aux->attach_btf_id)) {
99 		*bpf_func = __cgroup_bpf_run_lsm_current;
100 		return;
101 	}
102 
103 #ifdef CONFIG_NET
104 	args = btf_params(prog->aux->attach_func_proto);
105 
106 	if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCKET])
107 		*bpf_func = __cgroup_bpf_run_lsm_socket;
108 	else if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCK])
109 		*bpf_func = __cgroup_bpf_run_lsm_sock;
110 	else
111 #endif
112 		*bpf_func = __cgroup_bpf_run_lsm_current;
113 }
114 #endif
115 
116 int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
117 			const struct bpf_prog *prog)
118 {
119 	u32 btf_id = prog->aux->attach_btf_id;
120 	const char *func_name = prog->aux->attach_func_name;
121 
122 	if (!prog->gpl_compatible) {
123 		bpf_log(vlog,
124 			"LSM programs must have a GPL compatible license\n");
125 		return -EINVAL;
126 	}
127 
128 	if (btf_id_set_contains(&bpf_lsm_disabled_hooks, btf_id)) {
129 		bpf_log(vlog, "attach_btf_id %u points to disabled hook %s\n",
130 			btf_id, func_name);
131 		return -EINVAL;
132 	}
133 
134 	if (!btf_id_set_contains(&bpf_lsm_hooks, btf_id)) {
135 		bpf_log(vlog, "attach_btf_id %u points to wrong type name %s\n",
136 			btf_id, func_name);
137 		return -EINVAL;
138 	}
139 
140 	return 0;
141 }
142 
143 /* Mask for all the currently supported BPRM option flags */
144 #define BPF_F_BRPM_OPTS_MASK	BPF_F_BPRM_SECUREEXEC
145 
146 BPF_CALL_2(bpf_bprm_opts_set, struct linux_binprm *, bprm, u64, flags)
147 {
148 	if (flags & ~BPF_F_BRPM_OPTS_MASK)
149 		return -EINVAL;
150 
151 	bprm->secureexec = (flags & BPF_F_BPRM_SECUREEXEC);
152 	return 0;
153 }
154 
155 BTF_ID_LIST_SINGLE(bpf_bprm_opts_set_btf_ids, struct, linux_binprm)
156 
157 static const struct bpf_func_proto bpf_bprm_opts_set_proto = {
158 	.func		= bpf_bprm_opts_set,
159 	.gpl_only	= false,
160 	.ret_type	= RET_INTEGER,
161 	.arg1_type	= ARG_PTR_TO_BTF_ID,
162 	.arg1_btf_id	= &bpf_bprm_opts_set_btf_ids[0],
163 	.arg2_type	= ARG_ANYTHING,
164 };
165 
166 BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, size)
167 {
168 	return ima_inode_hash(inode, dst, size);
169 }
170 
171 static bool bpf_ima_inode_hash_allowed(const struct bpf_prog *prog)
172 {
173 	return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
174 }
175 
176 BTF_ID_LIST_SINGLE(bpf_ima_inode_hash_btf_ids, struct, inode)
177 
178 static const struct bpf_func_proto bpf_ima_inode_hash_proto = {
179 	.func		= bpf_ima_inode_hash,
180 	.gpl_only	= false,
181 	.might_sleep	= true,
182 	.ret_type	= RET_INTEGER,
183 	.arg1_type	= ARG_PTR_TO_BTF_ID,
184 	.arg1_btf_id	= &bpf_ima_inode_hash_btf_ids[0],
185 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
186 	.arg3_type	= ARG_CONST_SIZE,
187 	.allowed	= bpf_ima_inode_hash_allowed,
188 };
189 
190 BPF_CALL_3(bpf_ima_file_hash, struct file *, file, void *, dst, u32, size)
191 {
192 	return ima_file_hash(file, dst, size);
193 }
194 
195 BTF_ID_LIST_SINGLE(bpf_ima_file_hash_btf_ids, struct, file)
196 
197 static const struct bpf_func_proto bpf_ima_file_hash_proto = {
198 	.func		= bpf_ima_file_hash,
199 	.gpl_only	= false,
200 	.might_sleep	= true,
201 	.ret_type	= RET_INTEGER,
202 	.arg1_type	= ARG_PTR_TO_BTF_ID,
203 	.arg1_btf_id	= &bpf_ima_file_hash_btf_ids[0],
204 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
205 	.arg3_type	= ARG_CONST_SIZE,
206 	.allowed	= bpf_ima_inode_hash_allowed,
207 };
208 
209 BPF_CALL_1(bpf_get_attach_cookie, void *, ctx)
210 {
211 	struct bpf_trace_run_ctx *run_ctx;
212 
213 	run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
214 	return run_ctx->bpf_cookie;
215 }
216 
217 static const struct bpf_func_proto bpf_get_attach_cookie_proto = {
218 	.func		= bpf_get_attach_cookie,
219 	.gpl_only	= false,
220 	.ret_type	= RET_INTEGER,
221 	.arg1_type	= ARG_PTR_TO_CTX,
222 };
223 
224 static const struct bpf_func_proto *
225 bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
226 {
227 	const struct bpf_func_proto *func_proto;
228 
229 	if (prog->expected_attach_type == BPF_LSM_CGROUP) {
230 		func_proto = cgroup_common_func_proto(func_id, prog);
231 		if (func_proto)
232 			return func_proto;
233 	}
234 
235 	switch (func_id) {
236 	case BPF_FUNC_inode_storage_get:
237 		return &bpf_inode_storage_get_proto;
238 	case BPF_FUNC_inode_storage_delete:
239 		return &bpf_inode_storage_delete_proto;
240 #ifdef CONFIG_NET
241 	case BPF_FUNC_sk_storage_get:
242 		return &bpf_sk_storage_get_proto;
243 	case BPF_FUNC_sk_storage_delete:
244 		return &bpf_sk_storage_delete_proto;
245 #endif /* CONFIG_NET */
246 	case BPF_FUNC_spin_lock:
247 		return &bpf_spin_lock_proto;
248 	case BPF_FUNC_spin_unlock:
249 		return &bpf_spin_unlock_proto;
250 	case BPF_FUNC_bprm_opts_set:
251 		return &bpf_bprm_opts_set_proto;
252 	case BPF_FUNC_ima_inode_hash:
253 		return &bpf_ima_inode_hash_proto;
254 	case BPF_FUNC_ima_file_hash:
255 		return &bpf_ima_file_hash_proto;
256 	case BPF_FUNC_get_attach_cookie:
257 		return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto : NULL;
258 #ifdef CONFIG_NET
259 	case BPF_FUNC_setsockopt:
260 		if (prog->expected_attach_type != BPF_LSM_CGROUP)
261 			return NULL;
262 		if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks,
263 					prog->aux->attach_btf_id))
264 			return &bpf_sk_setsockopt_proto;
265 		if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks,
266 					prog->aux->attach_btf_id))
267 			return &bpf_unlocked_sk_setsockopt_proto;
268 		return NULL;
269 	case BPF_FUNC_getsockopt:
270 		if (prog->expected_attach_type != BPF_LSM_CGROUP)
271 			return NULL;
272 		if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks,
273 					prog->aux->attach_btf_id))
274 			return &bpf_sk_getsockopt_proto;
275 		if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks,
276 					prog->aux->attach_btf_id))
277 			return &bpf_unlocked_sk_getsockopt_proto;
278 		return NULL;
279 #endif
280 	default:
281 		return tracing_prog_func_proto(func_id, prog);
282 	}
283 }
284 
285 /* The set of hooks which are called without pagefaults disabled and are allowed
286  * to "sleep" and thus can be used for sleepable BPF programs.
287  */
288 BTF_SET_START(sleepable_lsm_hooks)
289 BTF_ID(func, bpf_lsm_bpf)
290 BTF_ID(func, bpf_lsm_bpf_map)
291 BTF_ID(func, bpf_lsm_bpf_map_create)
292 BTF_ID(func, bpf_lsm_bpf_map_free)
293 BTF_ID(func, bpf_lsm_bpf_prog)
294 BTF_ID(func, bpf_lsm_bpf_prog_load)
295 BTF_ID(func, bpf_lsm_bpf_prog_free)
296 BTF_ID(func, bpf_lsm_bpf_token_create)
297 BTF_ID(func, bpf_lsm_bpf_token_free)
298 BTF_ID(func, bpf_lsm_bpf_token_cmd)
299 BTF_ID(func, bpf_lsm_bpf_token_capable)
300 BTF_ID(func, bpf_lsm_bprm_check_security)
301 BTF_ID(func, bpf_lsm_bprm_committed_creds)
302 BTF_ID(func, bpf_lsm_bprm_committing_creds)
303 BTF_ID(func, bpf_lsm_bprm_creds_for_exec)
304 BTF_ID(func, bpf_lsm_bprm_creds_from_file)
305 BTF_ID(func, bpf_lsm_capget)
306 BTF_ID(func, bpf_lsm_capset)
307 BTF_ID(func, bpf_lsm_cred_prepare)
308 BTF_ID(func, bpf_lsm_file_ioctl)
309 BTF_ID(func, bpf_lsm_file_lock)
310 BTF_ID(func, bpf_lsm_file_open)
311 BTF_ID(func, bpf_lsm_file_post_open)
312 BTF_ID(func, bpf_lsm_file_receive)
313 
314 BTF_ID(func, bpf_lsm_inode_create)
315 BTF_ID(func, bpf_lsm_inode_free_security)
316 BTF_ID(func, bpf_lsm_inode_getattr)
317 BTF_ID(func, bpf_lsm_inode_getxattr)
318 BTF_ID(func, bpf_lsm_inode_mknod)
319 BTF_ID(func, bpf_lsm_inode_need_killpriv)
320 BTF_ID(func, bpf_lsm_inode_post_setxattr)
321 BTF_ID(func, bpf_lsm_inode_post_removexattr)
322 BTF_ID(func, bpf_lsm_inode_readlink)
323 BTF_ID(func, bpf_lsm_inode_removexattr)
324 BTF_ID(func, bpf_lsm_inode_rename)
325 BTF_ID(func, bpf_lsm_inode_rmdir)
326 BTF_ID(func, bpf_lsm_inode_setattr)
327 BTF_ID(func, bpf_lsm_inode_setxattr)
328 BTF_ID(func, bpf_lsm_inode_symlink)
329 BTF_ID(func, bpf_lsm_inode_unlink)
330 BTF_ID(func, bpf_lsm_kernel_module_request)
331 BTF_ID(func, bpf_lsm_kernel_read_file)
332 BTF_ID(func, bpf_lsm_kernfs_init_security)
333 
334 #ifdef CONFIG_SECURITY_PATH
335 BTF_ID(func, bpf_lsm_path_unlink)
336 BTF_ID(func, bpf_lsm_path_mkdir)
337 BTF_ID(func, bpf_lsm_path_rmdir)
338 BTF_ID(func, bpf_lsm_path_truncate)
339 BTF_ID(func, bpf_lsm_path_symlink)
340 BTF_ID(func, bpf_lsm_path_link)
341 BTF_ID(func, bpf_lsm_path_rename)
342 BTF_ID(func, bpf_lsm_path_chmod)
343 BTF_ID(func, bpf_lsm_path_chown)
344 #endif /* CONFIG_SECURITY_PATH */
345 
346 BTF_ID(func, bpf_lsm_mmap_file)
347 BTF_ID(func, bpf_lsm_netlink_send)
348 BTF_ID(func, bpf_lsm_path_notify)
349 BTF_ID(func, bpf_lsm_release_secctx)
350 BTF_ID(func, bpf_lsm_sb_alloc_security)
351 BTF_ID(func, bpf_lsm_sb_eat_lsm_opts)
352 BTF_ID(func, bpf_lsm_sb_kern_mount)
353 BTF_ID(func, bpf_lsm_sb_mount)
354 BTF_ID(func, bpf_lsm_sb_remount)
355 BTF_ID(func, bpf_lsm_sb_set_mnt_opts)
356 BTF_ID(func, bpf_lsm_sb_show_options)
357 BTF_ID(func, bpf_lsm_sb_statfs)
358 BTF_ID(func, bpf_lsm_sb_umount)
359 BTF_ID(func, bpf_lsm_settime)
360 
361 #ifdef CONFIG_SECURITY_NETWORK
362 BTF_ID(func, bpf_lsm_inet_conn_established)
363 
364 BTF_ID(func, bpf_lsm_socket_accept)
365 BTF_ID(func, bpf_lsm_socket_bind)
366 BTF_ID(func, bpf_lsm_socket_connect)
367 BTF_ID(func, bpf_lsm_socket_create)
368 BTF_ID(func, bpf_lsm_socket_getpeername)
369 BTF_ID(func, bpf_lsm_socket_getpeersec_dgram)
370 BTF_ID(func, bpf_lsm_socket_getsockname)
371 BTF_ID(func, bpf_lsm_socket_getsockopt)
372 BTF_ID(func, bpf_lsm_socket_listen)
373 BTF_ID(func, bpf_lsm_socket_post_create)
374 BTF_ID(func, bpf_lsm_socket_recvmsg)
375 BTF_ID(func, bpf_lsm_socket_sendmsg)
376 BTF_ID(func, bpf_lsm_socket_shutdown)
377 BTF_ID(func, bpf_lsm_socket_socketpair)
378 #endif /* CONFIG_SECURITY_NETWORK */
379 
380 BTF_ID(func, bpf_lsm_syslog)
381 BTF_ID(func, bpf_lsm_task_alloc)
382 BTF_ID(func, bpf_lsm_task_prctl)
383 BTF_ID(func, bpf_lsm_task_setscheduler)
384 BTF_ID(func, bpf_lsm_task_to_inode)
385 BTF_ID(func, bpf_lsm_userns_create)
386 BTF_SET_END(sleepable_lsm_hooks)
387 
388 BTF_SET_START(untrusted_lsm_hooks)
389 BTF_ID(func, bpf_lsm_bpf_map_free)
390 BTF_ID(func, bpf_lsm_bpf_prog_free)
391 BTF_ID(func, bpf_lsm_file_alloc_security)
392 BTF_ID(func, bpf_lsm_file_free_security)
393 #ifdef CONFIG_SECURITY_NETWORK
394 BTF_ID(func, bpf_lsm_sk_alloc_security)
395 BTF_ID(func, bpf_lsm_sk_free_security)
396 #endif /* CONFIG_SECURITY_NETWORK */
397 BTF_ID(func, bpf_lsm_task_free)
398 BTF_SET_END(untrusted_lsm_hooks)
399 
400 bool bpf_lsm_is_sleepable_hook(u32 btf_id)
401 {
402 	return btf_id_set_contains(&sleepable_lsm_hooks, btf_id);
403 }
404 
405 bool bpf_lsm_is_trusted(const struct bpf_prog *prog)
406 {
407 	return !btf_id_set_contains(&untrusted_lsm_hooks, prog->aux->attach_btf_id);
408 }
409 
410 const struct bpf_prog_ops lsm_prog_ops = {
411 };
412 
413 const struct bpf_verifier_ops lsm_verifier_ops = {
414 	.get_func_proto = bpf_lsm_func_proto,
415 	.is_valid_access = btf_ctx_access,
416 };
417 
418 /* hooks return 0 or 1 */
419 BTF_SET_START(bool_lsm_hooks)
420 #ifdef CONFIG_SECURITY_NETWORK_XFRM
421 BTF_ID(func, bpf_lsm_xfrm_state_pol_flow_match)
422 #endif
423 #ifdef CONFIG_AUDIT
424 BTF_ID(func, bpf_lsm_audit_rule_known)
425 #endif
426 BTF_ID(func, bpf_lsm_inode_xattr_skipcap)
427 BTF_SET_END(bool_lsm_hooks)
428 
429 int bpf_lsm_get_retval_range(const struct bpf_prog *prog,
430 			     struct bpf_retval_range *retval_range)
431 {
432 	/* no return value range for void hooks */
433 	if (!prog->aux->attach_func_proto->type)
434 		return -EINVAL;
435 
436 	if (btf_id_set_contains(&bool_lsm_hooks, prog->aux->attach_btf_id)) {
437 		retval_range->minval = 0;
438 		retval_range->maxval = 1;
439 	} else {
440 		/* All other available LSM hooks, except task_prctl, return 0
441 		 * on success and negative error code on failure.
442 		 * To keep things simple, we only allow bpf progs to return 0
443 		 * or negative errno for task_prctl too.
444 		 */
445 		retval_range->minval = -MAX_ERRNO;
446 		retval_range->maxval = 0;
447 	}
448 	return 0;
449 }
450