Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.6.
  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, ...)	\
 25noinline 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)
 34BTF_SET_START(bpf_lsm_hooks)
 35#include <linux/lsm_hook_defs.h>
 36#undef LSM_HOOK
 37BTF_SET_END(bpf_lsm_hooks)
 38
 39/* List of LSM hooks that should operate on 'current' cgroup regardless
 40 * of function signature.
 41 */
 42BTF_SET_START(bpf_lsm_current_hooks)
 43/* operate on freshly allocated sk without any cgroup association */
 44#ifdef CONFIG_SECURITY_NETWORK
 45BTF_ID(func, bpf_lsm_sk_alloc_security)
 46BTF_ID(func, bpf_lsm_sk_free_security)
 47#endif
 48BTF_SET_END(bpf_lsm_current_hooks)
 49
 50/* List of LSM hooks that trigger while the socket is properly locked.
 51 */
 52BTF_SET_START(bpf_lsm_locked_sockopt_hooks)
 53#ifdef CONFIG_SECURITY_NETWORK
 54BTF_ID(func, bpf_lsm_sock_graft)
 55BTF_ID(func, bpf_lsm_inet_csk_clone)
 56BTF_ID(func, bpf_lsm_inet_conn_established)
 57#endif
 58BTF_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 */
 64BTF_SET_START(bpf_lsm_unlocked_sockopt_hooks)
 65#ifdef CONFIG_SECURITY_NETWORK
 66BTF_ID(func, bpf_lsm_socket_post_create)
 67BTF_ID(func, bpf_lsm_socket_socketpair)
 68#endif
 69BTF_SET_END(bpf_lsm_unlocked_sockopt_hooks)
 70
 71#ifdef CONFIG_CGROUP_BPF
 72void 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
 97int 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
118BPF_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
127BTF_ID_LIST_SINGLE(bpf_bprm_opts_set_btf_ids, struct, linux_binprm)
128
129static 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
138BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, size)
139{
140	return ima_inode_hash(inode, dst, size);
141}
142
143static 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
148BTF_ID_LIST_SINGLE(bpf_ima_inode_hash_btf_ids, struct, inode)
149
150static const struct bpf_func_proto bpf_ima_inode_hash_proto = {
151	.func		= bpf_ima_inode_hash,
152	.gpl_only	= false,
153	.might_sleep	= true,
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
162BPF_CALL_3(bpf_ima_file_hash, struct file *, file, void *, dst, u32, size)
163{
164	return ima_file_hash(file, dst, size);
165}
166
167BTF_ID_LIST_SINGLE(bpf_ima_file_hash_btf_ids, struct, file)
168
169static const struct bpf_func_proto bpf_ima_file_hash_proto = {
170	.func		= bpf_ima_file_hash,
171	.gpl_only	= false,
172	.might_sleep	= true,
173	.ret_type	= RET_INTEGER,
174	.arg1_type	= ARG_PTR_TO_BTF_ID,
175	.arg1_btf_id	= &bpf_ima_file_hash_btf_ids[0],
176	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
177	.arg3_type	= ARG_CONST_SIZE,
178	.allowed	= bpf_ima_inode_hash_allowed,
179};
180
181BPF_CALL_1(bpf_get_attach_cookie, void *, ctx)
182{
183	struct bpf_trace_run_ctx *run_ctx;
184
185	run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
186	return run_ctx->bpf_cookie;
187}
188
189static const struct bpf_func_proto bpf_get_attach_cookie_proto = {
190	.func		= bpf_get_attach_cookie,
191	.gpl_only	= false,
192	.ret_type	= RET_INTEGER,
193	.arg1_type	= ARG_PTR_TO_CTX,
194};
195
196static const struct bpf_func_proto *
197bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
198{
199	const struct bpf_func_proto *func_proto;
200
201	if (prog->expected_attach_type == BPF_LSM_CGROUP) {
202		func_proto = cgroup_common_func_proto(func_id, prog);
203		if (func_proto)
204			return func_proto;
205	}
206
207	switch (func_id) {
208	case BPF_FUNC_inode_storage_get:
209		return &bpf_inode_storage_get_proto;
210	case BPF_FUNC_inode_storage_delete:
211		return &bpf_inode_storage_delete_proto;
212#ifdef CONFIG_NET
213	case BPF_FUNC_sk_storage_get:
214		return &bpf_sk_storage_get_proto;
215	case BPF_FUNC_sk_storage_delete:
216		return &bpf_sk_storage_delete_proto;
217#endif /* CONFIG_NET */
218	case BPF_FUNC_spin_lock:
219		return &bpf_spin_lock_proto;
220	case BPF_FUNC_spin_unlock:
221		return &bpf_spin_unlock_proto;
222	case BPF_FUNC_bprm_opts_set:
223		return &bpf_bprm_opts_set_proto;
224	case BPF_FUNC_ima_inode_hash:
225		return &bpf_ima_inode_hash_proto;
226	case BPF_FUNC_ima_file_hash:
227		return &bpf_ima_file_hash_proto;
228	case BPF_FUNC_get_attach_cookie:
229		return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto : NULL;
230#ifdef CONFIG_NET
231	case BPF_FUNC_setsockopt:
232		if (prog->expected_attach_type != BPF_LSM_CGROUP)
233			return NULL;
234		if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks,
235					prog->aux->attach_btf_id))
236			return &bpf_sk_setsockopt_proto;
237		if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks,
238					prog->aux->attach_btf_id))
239			return &bpf_unlocked_sk_setsockopt_proto;
240		return NULL;
241	case BPF_FUNC_getsockopt:
242		if (prog->expected_attach_type != BPF_LSM_CGROUP)
243			return NULL;
244		if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks,
245					prog->aux->attach_btf_id))
246			return &bpf_sk_getsockopt_proto;
247		if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks,
248					prog->aux->attach_btf_id))
249			return &bpf_unlocked_sk_getsockopt_proto;
250		return NULL;
251#endif
252	default:
253		return tracing_prog_func_proto(func_id, prog);
254	}
255}
256
257/* The set of hooks which are called without pagefaults disabled and are allowed
258 * to "sleep" and thus can be used for sleepable BPF programs.
259 */
260BTF_SET_START(sleepable_lsm_hooks)
261BTF_ID(func, bpf_lsm_bpf)
262BTF_ID(func, bpf_lsm_bpf_map)
263BTF_ID(func, bpf_lsm_bpf_map_alloc_security)
264BTF_ID(func, bpf_lsm_bpf_map_free_security)
265BTF_ID(func, bpf_lsm_bpf_prog)
266BTF_ID(func, bpf_lsm_bprm_check_security)
267BTF_ID(func, bpf_lsm_bprm_committed_creds)
268BTF_ID(func, bpf_lsm_bprm_committing_creds)
269BTF_ID(func, bpf_lsm_bprm_creds_for_exec)
270BTF_ID(func, bpf_lsm_bprm_creds_from_file)
271BTF_ID(func, bpf_lsm_capget)
272BTF_ID(func, bpf_lsm_capset)
273BTF_ID(func, bpf_lsm_cred_prepare)
274BTF_ID(func, bpf_lsm_file_ioctl)
275BTF_ID(func, bpf_lsm_file_lock)
276BTF_ID(func, bpf_lsm_file_open)
277BTF_ID(func, bpf_lsm_file_receive)
278
279#ifdef CONFIG_SECURITY_NETWORK
280BTF_ID(func, bpf_lsm_inet_conn_established)
281#endif /* CONFIG_SECURITY_NETWORK */
282
283BTF_ID(func, bpf_lsm_inode_create)
284BTF_ID(func, bpf_lsm_inode_free_security)
285BTF_ID(func, bpf_lsm_inode_getattr)
286BTF_ID(func, bpf_lsm_inode_getxattr)
287BTF_ID(func, bpf_lsm_inode_mknod)
288BTF_ID(func, bpf_lsm_inode_need_killpriv)
289BTF_ID(func, bpf_lsm_inode_post_setxattr)
290BTF_ID(func, bpf_lsm_inode_readlink)
291BTF_ID(func, bpf_lsm_inode_rename)
292BTF_ID(func, bpf_lsm_inode_rmdir)
293BTF_ID(func, bpf_lsm_inode_setattr)
294BTF_ID(func, bpf_lsm_inode_setxattr)
295BTF_ID(func, bpf_lsm_inode_symlink)
296BTF_ID(func, bpf_lsm_inode_unlink)
297BTF_ID(func, bpf_lsm_kernel_module_request)
298BTF_ID(func, bpf_lsm_kernel_read_file)
299BTF_ID(func, bpf_lsm_kernfs_init_security)
300
301#ifdef CONFIG_KEYS
302BTF_ID(func, bpf_lsm_key_free)
303#endif /* CONFIG_KEYS */
304
305BTF_ID(func, bpf_lsm_mmap_file)
306BTF_ID(func, bpf_lsm_netlink_send)
307BTF_ID(func, bpf_lsm_path_notify)
308BTF_ID(func, bpf_lsm_release_secctx)
309BTF_ID(func, bpf_lsm_sb_alloc_security)
310BTF_ID(func, bpf_lsm_sb_eat_lsm_opts)
311BTF_ID(func, bpf_lsm_sb_kern_mount)
312BTF_ID(func, bpf_lsm_sb_mount)
313BTF_ID(func, bpf_lsm_sb_remount)
314BTF_ID(func, bpf_lsm_sb_set_mnt_opts)
315BTF_ID(func, bpf_lsm_sb_show_options)
316BTF_ID(func, bpf_lsm_sb_statfs)
317BTF_ID(func, bpf_lsm_sb_umount)
318BTF_ID(func, bpf_lsm_settime)
319
320#ifdef CONFIG_SECURITY_NETWORK
321BTF_ID(func, bpf_lsm_socket_accept)
322BTF_ID(func, bpf_lsm_socket_bind)
323BTF_ID(func, bpf_lsm_socket_connect)
324BTF_ID(func, bpf_lsm_socket_create)
325BTF_ID(func, bpf_lsm_socket_getpeername)
326BTF_ID(func, bpf_lsm_socket_getpeersec_dgram)
327BTF_ID(func, bpf_lsm_socket_getsockname)
328BTF_ID(func, bpf_lsm_socket_getsockopt)
329BTF_ID(func, bpf_lsm_socket_listen)
330BTF_ID(func, bpf_lsm_socket_post_create)
331BTF_ID(func, bpf_lsm_socket_recvmsg)
332BTF_ID(func, bpf_lsm_socket_sendmsg)
333BTF_ID(func, bpf_lsm_socket_shutdown)
334BTF_ID(func, bpf_lsm_socket_socketpair)
335#endif /* CONFIG_SECURITY_NETWORK */
336
337BTF_ID(func, bpf_lsm_syslog)
338BTF_ID(func, bpf_lsm_task_alloc)
339BTF_ID(func, bpf_lsm_current_getsecid_subj)
340BTF_ID(func, bpf_lsm_task_getsecid_obj)
341BTF_ID(func, bpf_lsm_task_prctl)
342BTF_ID(func, bpf_lsm_task_setscheduler)
343BTF_ID(func, bpf_lsm_task_to_inode)
344BTF_ID(func, bpf_lsm_userns_create)
345BTF_SET_END(sleepable_lsm_hooks)
346
347BTF_SET_START(untrusted_lsm_hooks)
348BTF_ID(func, bpf_lsm_bpf_map_free_security)
349BTF_ID(func, bpf_lsm_bpf_prog_alloc_security)
350BTF_ID(func, bpf_lsm_bpf_prog_free_security)
351BTF_ID(func, bpf_lsm_file_alloc_security)
352BTF_ID(func, bpf_lsm_file_free_security)
353#ifdef CONFIG_SECURITY_NETWORK
354BTF_ID(func, bpf_lsm_sk_alloc_security)
355BTF_ID(func, bpf_lsm_sk_free_security)
356#endif /* CONFIG_SECURITY_NETWORK */
357BTF_ID(func, bpf_lsm_task_free)
358BTF_SET_END(untrusted_lsm_hooks)
359
360bool bpf_lsm_is_sleepable_hook(u32 btf_id)
361{
362	return btf_id_set_contains(&sleepable_lsm_hooks, btf_id);
363}
364
365bool bpf_lsm_is_trusted(const struct bpf_prog *prog)
366{
367	return !btf_id_set_contains(&untrusted_lsm_hooks, prog->aux->attach_btf_id);
368}
369
370const struct bpf_prog_ops lsm_prog_ops = {
371};
372
373const struct bpf_verifier_ops lsm_verifier_ops = {
374	.get_func_proto = bpf_lsm_func_proto,
375	.is_valid_access = btf_ctx_access,
376};