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