~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/bpf/bpf_lsm.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php