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 <net/bpf_sk_storage.h> 15 #include <linux/bpf_local_storage.h> 16 #include <linux/btf_ids.h> 17 #include <linux/ima.h> 18 #include <linux/bpf-cgroup.h> 19 20 /* For every LSM hook that allows attachment of BPF programs, declare a nop 21 * function where a BPF program can be attached. Notably, we qualify each with 22 * weak linkage such that strong overrides can be implemented if need be. 23 */ 24 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \ 25 __weak 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 BTF_SET_START(bpf_lsm_disabled_hooks) 40 BTF_ID(func, bpf_lsm_vm_enough_memory) 41 BTF_ID(func, bpf_lsm_inode_need_killpriv) 42 BTF_ID(func, bpf_lsm_inode_getsecurity) 43 BTF_ID(func, bpf_lsm_inode_listsecurity) 44 BTF_ID(func, bpf_lsm_inode_copy_up_xattr) 45 BTF_ID(func, bpf_lsm_getselfattr) 46 BTF_ID(func, bpf_lsm_getprocattr) 47 BTF_ID(func, bpf_lsm_setprocattr) 48 #ifdef CONFIG_KEYS 49 BTF_ID(func, bpf_lsm_key_getsecurity) 50 #endif 51 #ifdef CONFIG_AUDIT 52 BTF_ID(func, bpf_lsm_audit_rule_match) 53 #endif 54 #ifdef CONFIG_SECURITY_NETWORK_XFRM 55 BTF_ID(func, bpf_lsm_xfrm_decode_session) 56 #endif 57 BTF_ID(func, bpf_lsm_ismaclabel) 58 BTF_ID(func, bpf_lsm_file_alloc_security) 59 BTF_SET_END(bpf_lsm_disabled_hooks) 60 61 /* List of LSM hooks that should operate on 'current' cgroup regardless 62 * of function signature. 63 */ 64 BTF_SET_START(bpf_lsm_current_hooks) 65 /* operate on freshly allocated sk without any cgroup association */ 66 #ifdef CONFIG_SECURITY_NETWORK 67 BTF_ID(func, bpf_lsm_sk_alloc_security) 68 BTF_ID(func, bpf_lsm_sk_free_security) 69 #endif 70 BTF_SET_END(bpf_lsm_current_hooks) 71 72 /* List of LSM hooks that trigger while the socket is properly locked. 73 */ 74 BTF_SET_START(bpf_lsm_locked_sockopt_hooks) 75 #ifdef CONFIG_SECURITY_NETWORK 76 BTF_ID(func, bpf_lsm_sock_graft) 77 BTF_ID(func, bpf_lsm_inet_csk_clone) 78 BTF_ID(func, bpf_lsm_inet_conn_established) 79 #endif 80 BTF_SET_END(bpf_lsm_locked_sockopt_hooks) 81 82 /* List of LSM hooks that trigger while the socket is _not_ locked, 83 * but it's ok to call bpf_{g,s}etsockopt because the socket is still 84 * in the early init phase. 85 */ 86 BTF_SET_START(bpf_lsm_unlocked_sockopt_hooks) 87 #ifdef CONFIG_SECURITY_NETWORK 88 BTF_ID(func, bpf_lsm_socket_post_create) 89 BTF_ID(func, bpf_lsm_socket_socketpair) 90 #endif 91 BTF_SET_END(bpf_lsm_unlocked_sockopt_hooks) 92 93 #ifdef CONFIG_CGROUP_BPF 94 void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, 95 bpf_func_t *bpf_func) 96 { 97 const struct btf_param *args __maybe_unused; 98 99 if (btf_type_vlen(prog->aux->attach_func_proto) < 1 || 100 btf_id_set_contains(&bpf_lsm_current_hooks, 101 prog->aux->attach_btf_id)) { 102 *bpf_func = __cgroup_bpf_run_lsm_current; 103 return; 104 } 105 106 #ifdef CONFIG_NET 107 args = btf_params(prog->aux->attach_func_proto); 108 109 if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCKET]) 110 *bpf_func = __cgroup_bpf_run_lsm_socket; 111 else if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCK]) 112 *bpf_func = __cgroup_bpf_run_lsm_sock; 113 else 114 #endif 115 *bpf_func = __cgroup_bpf_run_lsm_current; 116 } 117 #endif 118 119 BTF_ID_LIST_SINGLE(bpf_lsm_inode_init_security_btf_id, func, 120 bpf_lsm_inode_init_security) 121 122 int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, 123 struct bpf_prog *prog) 124 { 125 u32 btf_id = prog->aux->attach_btf_id; 126 const char *func_name = prog->aux->attach_func_name; 127 128 if (!prog->gpl_compatible) { 129 bpf_log(vlog, 130 "LSM programs must have a GPL compatible license\n"); 131 return -EINVAL; 132 } 133 134 if (btf_id_set_contains(&bpf_lsm_disabled_hooks, btf_id)) { 135 bpf_log(vlog, "attach_btf_id %u points to disabled hook %s\n", 136 btf_id, func_name); 137 return -EINVAL; 138 } 139 140 if (!btf_id_set_contains(&bpf_lsm_hooks, btf_id)) { 141 bpf_log(vlog, "attach_btf_id %u points to wrong type name %s\n", 142 btf_id, func_name); 143 return -EINVAL; 144 } 145 146 if (btf_id == bpf_lsm_inode_init_security_btf_id[0]) { 147 /* 148 * inode, dir, qstr, xattrs, xattr_count 149 * 150 * The trusted pointer this hands the program is only as 151 * trustworthy as the context it is loaded from, which 152 * lsm_verifier_ops keeps read-only. 153 */ 154 static const struct bpf_ctx_arg_aux xattr_count_arg_info = { 155 .offset = 4 * sizeof(u64), 156 .reg_type = PTR_TO_MEM | MEM_RDONLY | PTR_TRUSTED, 157 .mem_size = sizeof(int), 158 }; 159 160 return bpf_prog_ctx_arg_info_init(prog, &xattr_count_arg_info, 1); 161 } 162 163 return 0; 164 } 165 166 /* Mask for all the currently supported BPRM option flags */ 167 #define BPF_F_BRPM_OPTS_MASK BPF_F_BPRM_SECUREEXEC 168 169 BPF_CALL_2(bpf_bprm_opts_set, struct linux_binprm *, bprm, u64, flags) 170 { 171 if (flags & ~BPF_F_BRPM_OPTS_MASK) 172 return -EINVAL; 173 174 bprm->secureexec = (flags & BPF_F_BPRM_SECUREEXEC); 175 return 0; 176 } 177 178 BTF_ID_LIST_SINGLE(bpf_bprm_opts_set_btf_ids, struct, linux_binprm) 179 180 static const struct bpf_func_proto bpf_bprm_opts_set_proto = { 181 .func = bpf_bprm_opts_set, 182 .gpl_only = false, 183 .ret_type = RET_INTEGER, 184 .arg1_type = ARG_PTR_TO_BTF_ID, 185 .arg1_btf_id = &bpf_bprm_opts_set_btf_ids[0], 186 .arg2_type = ARG_ANYTHING, 187 }; 188 189 BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, size) 190 { 191 return ima_inode_hash(inode, dst, size); 192 } 193 194 static bool bpf_ima_inode_hash_allowed(const struct bpf_prog *prog) 195 { 196 return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id); 197 } 198 199 BTF_ID_LIST_SINGLE(bpf_ima_inode_hash_btf_ids, struct, inode) 200 201 static const struct bpf_func_proto bpf_ima_inode_hash_proto = { 202 .func = bpf_ima_inode_hash, 203 .gpl_only = false, 204 .might_sleep = true, 205 .ret_type = RET_INTEGER, 206 .arg1_type = ARG_PTR_TO_BTF_ID, 207 .arg1_btf_id = &bpf_ima_inode_hash_btf_ids[0], 208 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 209 .arg3_type = ARG_MEM_SIZE, 210 .allowed = bpf_ima_inode_hash_allowed, 211 }; 212 213 BPF_CALL_3(bpf_ima_file_hash, struct file *, file, void *, dst, u32, size) 214 { 215 return ima_file_hash(file, dst, size); 216 } 217 218 BTF_ID_LIST_SINGLE(bpf_ima_file_hash_btf_ids, struct, file) 219 220 static const struct bpf_func_proto bpf_ima_file_hash_proto = { 221 .func = bpf_ima_file_hash, 222 .gpl_only = false, 223 .might_sleep = true, 224 .ret_type = RET_INTEGER, 225 .arg1_type = ARG_PTR_TO_BTF_ID, 226 .arg1_btf_id = &bpf_ima_file_hash_btf_ids[0], 227 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 228 .arg3_type = ARG_MEM_SIZE, 229 .allowed = bpf_ima_inode_hash_allowed, 230 }; 231 232 BPF_CALL_1(bpf_get_attach_cookie, void *, ctx) 233 { 234 struct bpf_trace_run_ctx *run_ctx; 235 236 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx); 237 return run_ctx->bpf_cookie; 238 } 239 240 static const struct bpf_func_proto bpf_get_attach_cookie_proto = { 241 .func = bpf_get_attach_cookie, 242 .gpl_only = false, 243 .ret_type = RET_INTEGER, 244 .arg1_type = ARG_PTR_TO_CTX, 245 }; 246 247 static const struct bpf_func_proto * 248 bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 249 { 250 const struct bpf_func_proto *func_proto; 251 252 if (prog->expected_attach_type == BPF_LSM_CGROUP) { 253 func_proto = cgroup_common_func_proto(func_id, prog); 254 if (func_proto) 255 return func_proto; 256 } 257 258 switch (func_id) { 259 case BPF_FUNC_inode_storage_get: 260 return &bpf_inode_storage_get_proto; 261 case BPF_FUNC_inode_storage_delete: 262 return &bpf_inode_storage_delete_proto; 263 #ifdef CONFIG_NET 264 case BPF_FUNC_sk_storage_get: 265 return &bpf_sk_storage_get_proto; 266 case BPF_FUNC_sk_storage_delete: 267 return &bpf_sk_storage_delete_proto; 268 #endif /* CONFIG_NET */ 269 case BPF_FUNC_spin_lock: 270 return &bpf_spin_lock_proto; 271 case BPF_FUNC_spin_unlock: 272 return &bpf_spin_unlock_proto; 273 case BPF_FUNC_bprm_opts_set: 274 return &bpf_bprm_opts_set_proto; 275 case BPF_FUNC_ima_inode_hash: 276 return &bpf_ima_inode_hash_proto; 277 case BPF_FUNC_ima_file_hash: 278 return &bpf_ima_file_hash_proto; 279 case BPF_FUNC_get_attach_cookie: 280 return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto : NULL; 281 #ifdef CONFIG_NET 282 case BPF_FUNC_setsockopt: 283 if (prog->expected_attach_type != BPF_LSM_CGROUP) 284 return NULL; 285 if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks, 286 prog->aux->attach_btf_id)) 287 return &bpf_sk_setsockopt_proto; 288 if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks, 289 prog->aux->attach_btf_id)) 290 return &bpf_unlocked_sk_setsockopt_proto; 291 return NULL; 292 case BPF_FUNC_getsockopt: 293 if (prog->expected_attach_type != BPF_LSM_CGROUP) 294 return NULL; 295 if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks, 296 prog->aux->attach_btf_id)) 297 return &bpf_sk_getsockopt_proto; 298 if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks, 299 prog->aux->attach_btf_id)) 300 return &bpf_unlocked_sk_getsockopt_proto; 301 return NULL; 302 #endif 303 default: 304 return tracing_prog_func_proto(func_id, prog); 305 } 306 } 307 308 /* The set of hooks which are called without pagefaults disabled and are allowed 309 * to "sleep" and thus can be used for sleepable BPF programs. 310 */ 311 BTF_SET_START(sleepable_lsm_hooks) 312 BTF_ID(func, bpf_lsm_bpf) 313 BTF_ID(func, bpf_lsm_bpf_map) 314 BTF_ID(func, bpf_lsm_bpf_map_create) 315 BTF_ID(func, bpf_lsm_bpf_map_free) 316 BTF_ID(func, bpf_lsm_bpf_prog) 317 BTF_ID(func, bpf_lsm_bpf_prog_load) 318 BTF_ID(func, bpf_lsm_bpf_token_create) 319 BTF_ID(func, bpf_lsm_bpf_token_free) 320 BTF_ID(func, bpf_lsm_bpf_token_cmd) 321 BTF_ID(func, bpf_lsm_bpf_token_capable) 322 BTF_ID(func, bpf_lsm_bprm_check_security) 323 BTF_ID(func, bpf_lsm_bprm_committed_creds) 324 BTF_ID(func, bpf_lsm_bprm_committing_creds) 325 BTF_ID(func, bpf_lsm_bprm_creds_for_exec) 326 BTF_ID(func, bpf_lsm_bprm_creds_from_file) 327 BTF_ID(func, bpf_lsm_capget) 328 BTF_ID(func, bpf_lsm_capset) 329 BTF_ID(func, bpf_lsm_cred_prepare) 330 BTF_ID(func, bpf_lsm_file_ioctl) 331 BTF_ID(func, bpf_lsm_file_lock) 332 BTF_ID(func, bpf_lsm_file_open) 333 BTF_ID(func, bpf_lsm_file_post_open) 334 BTF_ID(func, bpf_lsm_file_receive) 335 336 BTF_ID(func, bpf_lsm_inode_create) 337 BTF_ID(func, bpf_lsm_inode_free_security) 338 BTF_ID(func, bpf_lsm_inode_getattr) 339 BTF_ID(func, bpf_lsm_inode_getxattr) 340 BTF_ID(func, bpf_lsm_inode_mknod) 341 BTF_ID(func, bpf_lsm_inode_need_killpriv) 342 BTF_ID(func, bpf_lsm_inode_post_setxattr) 343 BTF_ID(func, bpf_lsm_inode_post_removexattr) 344 BTF_ID(func, bpf_lsm_inode_readlink) 345 BTF_ID(func, bpf_lsm_inode_removexattr) 346 BTF_ID(func, bpf_lsm_inode_rename) 347 BTF_ID(func, bpf_lsm_inode_rmdir) 348 BTF_ID(func, bpf_lsm_inode_setattr) 349 BTF_ID(func, bpf_lsm_inode_setxattr) 350 BTF_ID(func, bpf_lsm_inode_symlink) 351 BTF_ID(func, bpf_lsm_inode_unlink) 352 BTF_ID(func, bpf_lsm_kernel_module_request) 353 BTF_ID(func, bpf_lsm_kernel_read_file) 354 BTF_ID(func, bpf_lsm_kernfs_init_security) 355 356 #ifdef CONFIG_SECURITY_PATH 357 BTF_ID(func, bpf_lsm_path_unlink) 358 BTF_ID(func, bpf_lsm_path_mkdir) 359 BTF_ID(func, bpf_lsm_path_rmdir) 360 BTF_ID(func, bpf_lsm_path_truncate) 361 BTF_ID(func, bpf_lsm_path_symlink) 362 BTF_ID(func, bpf_lsm_path_link) 363 BTF_ID(func, bpf_lsm_path_rename) 364 BTF_ID(func, bpf_lsm_path_chmod) 365 BTF_ID(func, bpf_lsm_path_chown) 366 #endif /* CONFIG_SECURITY_PATH */ 367 368 BTF_ID(func, bpf_lsm_mmap_file) 369 BTF_ID(func, bpf_lsm_netlink_send) 370 BTF_ID(func, bpf_lsm_path_notify) 371 BTF_ID(func, bpf_lsm_release_secctx) 372 BTF_ID(func, bpf_lsm_sb_alloc_security) 373 BTF_ID(func, bpf_lsm_sb_eat_lsm_opts) 374 BTF_ID(func, bpf_lsm_sb_kern_mount) 375 BTF_ID(func, bpf_lsm_sb_mount) 376 BTF_ID(func, bpf_lsm_sb_remount) 377 BTF_ID(func, bpf_lsm_sb_set_mnt_opts) 378 BTF_ID(func, bpf_lsm_sb_show_options) 379 BTF_ID(func, bpf_lsm_sb_statfs) 380 BTF_ID(func, bpf_lsm_sb_umount) 381 BTF_ID(func, bpf_lsm_settime) 382 383 #ifdef CONFIG_SECURITY_NETWORK 384 BTF_ID(func, bpf_lsm_socket_accept) 385 BTF_ID(func, bpf_lsm_socket_bind) 386 BTF_ID(func, bpf_lsm_socket_connect) 387 BTF_ID(func, bpf_lsm_socket_create) 388 BTF_ID(func, bpf_lsm_socket_getpeername) 389 BTF_ID(func, bpf_lsm_socket_getpeersec_dgram) 390 BTF_ID(func, bpf_lsm_socket_getsockname) 391 BTF_ID(func, bpf_lsm_socket_getsockopt) 392 BTF_ID(func, bpf_lsm_socket_listen) 393 BTF_ID(func, bpf_lsm_socket_post_create) 394 BTF_ID(func, bpf_lsm_socket_recvmsg) 395 BTF_ID(func, bpf_lsm_socket_sendmsg) 396 BTF_ID(func, bpf_lsm_socket_shutdown) 397 BTF_ID(func, bpf_lsm_socket_socketpair) 398 #endif /* CONFIG_SECURITY_NETWORK */ 399 400 BTF_ID(func, bpf_lsm_syslog) 401 BTF_ID(func, bpf_lsm_task_alloc) 402 BTF_ID(func, bpf_lsm_task_prctl) 403 BTF_ID(func, bpf_lsm_task_setscheduler) 404 BTF_ID(func, bpf_lsm_userns_create) 405 BTF_ID(func, bpf_lsm_bdev_alloc_security) 406 BTF_ID(func, bpf_lsm_bdev_setintegrity) 407 BTF_SET_END(sleepable_lsm_hooks) 408 409 BTF_SET_START(untrusted_lsm_hooks) 410 BTF_ID(func, bpf_lsm_bpf_map_free) 411 BTF_ID(func, bpf_lsm_bpf_prog_free) 412 BTF_ID(func, bpf_lsm_file_alloc_security) 413 BTF_ID(func, bpf_lsm_file_free_security) 414 #ifdef CONFIG_SECURITY_NETWORK 415 BTF_ID(func, bpf_lsm_sk_alloc_security) 416 BTF_ID(func, bpf_lsm_sk_free_security) 417 #endif /* CONFIG_SECURITY_NETWORK */ 418 BTF_ID(func, bpf_lsm_task_free) 419 BTF_ID(func, bpf_lsm_bdev_alloc_security) 420 BTF_ID(func, bpf_lsm_bdev_free_security) 421 BTF_SET_END(untrusted_lsm_hooks) 422 423 bool bpf_lsm_is_sleepable_hook(u32 btf_id) 424 { 425 return btf_id_set_contains(&sleepable_lsm_hooks, btf_id); 426 } 427 428 bool bpf_lsm_is_trusted(const struct bpf_prog *prog) 429 { 430 return !btf_id_set_contains(&untrusted_lsm_hooks, prog->aux->attach_btf_id); 431 } 432 433 const struct bpf_prog_ops lsm_prog_ops = { 434 }; 435 436 const struct bpf_verifier_ops lsm_verifier_ops = { 437 .get_func_proto = bpf_lsm_func_proto, 438 .is_valid_access = bpf_tracing_btf_ctx_access, 439 }; 440 441 /* hooks return 0 or 1 */ 442 BTF_SET_START(bool_lsm_hooks) 443 #ifdef CONFIG_SECURITY_NETWORK_XFRM 444 BTF_ID(func, bpf_lsm_xfrm_state_pol_flow_match) 445 #endif 446 #ifdef CONFIG_AUDIT 447 BTF_ID(func, bpf_lsm_audit_rule_known) 448 #endif 449 BTF_ID(func, bpf_lsm_inode_xattr_skipcap) 450 BTF_SET_END(bool_lsm_hooks) 451 452 /* hooks returning void */ 453 #define LSM_HOOK_void(DEFAULT, NAME, ...) BTF_ID(func, bpf_lsm_##NAME) 454 #define LSM_HOOK_int(DEFAULT, NAME, ...) /* nothing */ 455 #define LSM_HOOK(RET, DEFAULT, NAME, ...) LSM_HOOK_##RET(DEFAULT, NAME, __VA_ARGS__) 456 BTF_SET_START(void_lsm_hooks) 457 #include <linux/lsm_hook_defs.h> 458 #undef LSM_HOOK 459 #undef LSM_HOOK_void 460 #undef LSM_HOOK_int 461 BTF_SET_END(void_lsm_hooks) 462 463 bool bpf_lsm_hook_returns_errno(u32 btf_id) 464 { 465 if (btf_id_set_contains(&bool_lsm_hooks, btf_id)) 466 return false; 467 if (btf_id_set_contains(&void_lsm_hooks, btf_id)) 468 return false; 469 return true; 470 } 471 472 int bpf_lsm_get_retval_range(const struct bpf_prog *prog, 473 struct bpf_retval_range *retval_range) 474 { 475 /* no return value range for void hooks */ 476 if (!prog->aux->attach_func_proto->type) 477 return -EINVAL; 478 479 if (btf_id_set_contains(&bool_lsm_hooks, prog->aux->attach_btf_id)) { 480 retval_range->minval = 0; 481 retval_range->maxval = 1; 482 } else { 483 /* All other available LSM hooks, except task_prctl, return 0 484 * on success and negative error code on failure. 485 * To keep things simple, we only allow bpf progs to return 0 486 * or negative errno for task_prctl too. 487 */ 488 retval_range->minval = -MAX_ERRNO; 489 retval_range->maxval = 0; 490 } 491 return 0; 492 } 493