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_create)
264BTF_ID(func, bpf_lsm_bpf_map_free)
265BTF_ID(func, bpf_lsm_bpf_prog)
266BTF_ID(func, bpf_lsm_bpf_prog_load)
267BTF_ID(func, bpf_lsm_bpf_prog_free)
268BTF_ID(func, bpf_lsm_bpf_token_create)
269BTF_ID(func, bpf_lsm_bpf_token_free)
270BTF_ID(func, bpf_lsm_bpf_token_cmd)
271BTF_ID(func, bpf_lsm_bpf_token_capable)
272BTF_ID(func, bpf_lsm_bprm_check_security)
273BTF_ID(func, bpf_lsm_bprm_committed_creds)
274BTF_ID(func, bpf_lsm_bprm_committing_creds)
275BTF_ID(func, bpf_lsm_bprm_creds_for_exec)
276BTF_ID(func, bpf_lsm_bprm_creds_from_file)
277BTF_ID(func, bpf_lsm_capget)
278BTF_ID(func, bpf_lsm_capset)
279BTF_ID(func, bpf_lsm_cred_prepare)
280BTF_ID(func, bpf_lsm_file_ioctl)
281BTF_ID(func, bpf_lsm_file_lock)
282BTF_ID(func, bpf_lsm_file_open)
283BTF_ID(func, bpf_lsm_file_receive)
284
285BTF_ID(func, bpf_lsm_inode_create)
286BTF_ID(func, bpf_lsm_inode_free_security)
287BTF_ID(func, bpf_lsm_inode_getattr)
288BTF_ID(func, bpf_lsm_inode_getxattr)
289BTF_ID(func, bpf_lsm_inode_mknod)
290BTF_ID(func, bpf_lsm_inode_need_killpriv)
291BTF_ID(func, bpf_lsm_inode_post_setxattr)
292BTF_ID(func, bpf_lsm_inode_readlink)
293BTF_ID(func, bpf_lsm_inode_rename)
294BTF_ID(func, bpf_lsm_inode_rmdir)
295BTF_ID(func, bpf_lsm_inode_setattr)
296BTF_ID(func, bpf_lsm_inode_setxattr)
297BTF_ID(func, bpf_lsm_inode_symlink)
298BTF_ID(func, bpf_lsm_inode_unlink)
299BTF_ID(func, bpf_lsm_kernel_module_request)
300BTF_ID(func, bpf_lsm_kernel_read_file)
301BTF_ID(func, bpf_lsm_kernfs_init_security)
302
303#ifdef CONFIG_SECURITY_PATH
304BTF_ID(func, bpf_lsm_path_unlink)
305BTF_ID(func, bpf_lsm_path_mkdir)
306BTF_ID(func, bpf_lsm_path_rmdir)
307BTF_ID(func, bpf_lsm_path_truncate)
308BTF_ID(func, bpf_lsm_path_symlink)
309BTF_ID(func, bpf_lsm_path_link)
310BTF_ID(func, bpf_lsm_path_rename)
311BTF_ID(func, bpf_lsm_path_chmod)
312BTF_ID(func, bpf_lsm_path_chown)
313#endif /* CONFIG_SECURITY_PATH */
314
315#ifdef CONFIG_KEYS
316BTF_ID(func, bpf_lsm_key_free)
317#endif /* CONFIG_KEYS */
318
319BTF_ID(func, bpf_lsm_mmap_file)
320BTF_ID(func, bpf_lsm_netlink_send)
321BTF_ID(func, bpf_lsm_path_notify)
322BTF_ID(func, bpf_lsm_release_secctx)
323BTF_ID(func, bpf_lsm_sb_alloc_security)
324BTF_ID(func, bpf_lsm_sb_eat_lsm_opts)
325BTF_ID(func, bpf_lsm_sb_kern_mount)
326BTF_ID(func, bpf_lsm_sb_mount)
327BTF_ID(func, bpf_lsm_sb_remount)
328BTF_ID(func, bpf_lsm_sb_set_mnt_opts)
329BTF_ID(func, bpf_lsm_sb_show_options)
330BTF_ID(func, bpf_lsm_sb_statfs)
331BTF_ID(func, bpf_lsm_sb_umount)
332BTF_ID(func, bpf_lsm_settime)
333
334#ifdef CONFIG_SECURITY_NETWORK
335BTF_ID(func, bpf_lsm_inet_conn_established)
336
337BTF_ID(func, bpf_lsm_socket_accept)
338BTF_ID(func, bpf_lsm_socket_bind)
339BTF_ID(func, bpf_lsm_socket_connect)
340BTF_ID(func, bpf_lsm_socket_create)
341BTF_ID(func, bpf_lsm_socket_getpeername)
342BTF_ID(func, bpf_lsm_socket_getpeersec_dgram)
343BTF_ID(func, bpf_lsm_socket_getsockname)
344BTF_ID(func, bpf_lsm_socket_getsockopt)
345BTF_ID(func, bpf_lsm_socket_listen)
346BTF_ID(func, bpf_lsm_socket_post_create)
347BTF_ID(func, bpf_lsm_socket_recvmsg)
348BTF_ID(func, bpf_lsm_socket_sendmsg)
349BTF_ID(func, bpf_lsm_socket_shutdown)
350BTF_ID(func, bpf_lsm_socket_socketpair)
351#endif /* CONFIG_SECURITY_NETWORK */
352
353BTF_ID(func, bpf_lsm_syslog)
354BTF_ID(func, bpf_lsm_task_alloc)
355BTF_ID(func, bpf_lsm_current_getsecid_subj)
356BTF_ID(func, bpf_lsm_task_getsecid_obj)
357BTF_ID(func, bpf_lsm_task_prctl)
358BTF_ID(func, bpf_lsm_task_setscheduler)
359BTF_ID(func, bpf_lsm_task_to_inode)
360BTF_ID(func, bpf_lsm_userns_create)
361BTF_SET_END(sleepable_lsm_hooks)
362
363BTF_SET_START(untrusted_lsm_hooks)
364BTF_ID(func, bpf_lsm_bpf_map_free)
365BTF_ID(func, bpf_lsm_bpf_prog_free)
366BTF_ID(func, bpf_lsm_file_alloc_security)
367BTF_ID(func, bpf_lsm_file_free_security)
368#ifdef CONFIG_SECURITY_NETWORK
369BTF_ID(func, bpf_lsm_sk_alloc_security)
370BTF_ID(func, bpf_lsm_sk_free_security)
371#endif /* CONFIG_SECURITY_NETWORK */
372BTF_ID(func, bpf_lsm_task_free)
373BTF_SET_END(untrusted_lsm_hooks)
374
375bool bpf_lsm_is_sleepable_hook(u32 btf_id)
376{
377	return btf_id_set_contains(&sleepable_lsm_hooks, btf_id);
378}
379
380bool bpf_lsm_is_trusted(const struct bpf_prog *prog)
381{
382	return !btf_id_set_contains(&untrusted_lsm_hooks, prog->aux->attach_btf_id);
383}
384
385const struct bpf_prog_ops lsm_prog_ops = {
386};
387
388const struct bpf_verifier_ops lsm_verifier_ops = {
389	.get_func_proto = bpf_lsm_func_proto,
390	.is_valid_access = btf_ctx_access,
391};
392