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