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