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 const struct bpf_func_proto *func_proto; 193 194 if (prog->expected_attach_type == BPF_LSM_CGROUP) { 195 func_proto = cgroup_common_func_proto(func_id, prog); 196 if (func_proto) 197 return func_proto; 198 } 199 200 switch (func_id) { 201 case BPF_FUNC_inode_storage_get: 202 return &bpf_inode_storage_get_proto; 203 case BPF_FUNC_inode_storage_delete: 204 return &bpf_inode_storage_delete_proto; 205 #ifdef CONFIG_NET 206 case BPF_FUNC_sk_storage_get: 207 return &bpf_sk_storage_get_proto; 208 case BPF_FUNC_sk_storage_delete: 209 return &bpf_sk_storage_delete_proto; 210 #endif /* CONFIG_NET */ 211 case BPF_FUNC_spin_lock: 212 return &bpf_spin_lock_proto; 213 case BPF_FUNC_spin_unlock: 214 return &bpf_spin_unlock_proto; 215 case BPF_FUNC_bprm_opts_set: 216 return &bpf_bprm_opts_set_proto; 217 case BPF_FUNC_ima_inode_hash: 218 return prog->aux->sleepable ? &bpf_ima_inode_hash_proto : NULL; 219 case BPF_FUNC_ima_file_hash: 220 return prog->aux->sleepable ? &bpf_ima_file_hash_proto : NULL; 221 case BPF_FUNC_get_attach_cookie: 222 return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto : NULL; 223 #ifdef CONFIG_NET 224 case BPF_FUNC_setsockopt: 225 if (prog->expected_attach_type != BPF_LSM_CGROUP) 226 return NULL; 227 if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks, 228 prog->aux->attach_btf_id)) 229 return &bpf_sk_setsockopt_proto; 230 if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks, 231 prog->aux->attach_btf_id)) 232 return &bpf_unlocked_sk_setsockopt_proto; 233 return NULL; 234 case BPF_FUNC_getsockopt: 235 if (prog->expected_attach_type != BPF_LSM_CGROUP) 236 return NULL; 237 if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks, 238 prog->aux->attach_btf_id)) 239 return &bpf_sk_getsockopt_proto; 240 if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks, 241 prog->aux->attach_btf_id)) 242 return &bpf_unlocked_sk_getsockopt_proto; 243 return NULL; 244 #endif 245 default: 246 return tracing_prog_func_proto(func_id, prog); 247 } 248 } 249 250 /* The set of hooks which are called without pagefaults disabled and are allowed 251 * to "sleep" and thus can be used for sleepable BPF programs. 252 */ 253 BTF_SET_START(sleepable_lsm_hooks) 254 BTF_ID(func, bpf_lsm_bpf) 255 BTF_ID(func, bpf_lsm_bpf_map) 256 BTF_ID(func, bpf_lsm_bpf_map_alloc_security) 257 BTF_ID(func, bpf_lsm_bpf_map_free_security) 258 BTF_ID(func, bpf_lsm_bpf_prog) 259 BTF_ID(func, bpf_lsm_bprm_check_security) 260 BTF_ID(func, bpf_lsm_bprm_committed_creds) 261 BTF_ID(func, bpf_lsm_bprm_committing_creds) 262 BTF_ID(func, bpf_lsm_bprm_creds_for_exec) 263 BTF_ID(func, bpf_lsm_bprm_creds_from_file) 264 BTF_ID(func, bpf_lsm_capget) 265 BTF_ID(func, bpf_lsm_capset) 266 BTF_ID(func, bpf_lsm_cred_prepare) 267 BTF_ID(func, bpf_lsm_file_ioctl) 268 BTF_ID(func, bpf_lsm_file_lock) 269 BTF_ID(func, bpf_lsm_file_open) 270 BTF_ID(func, bpf_lsm_file_receive) 271 272 #ifdef CONFIG_SECURITY_NETWORK 273 BTF_ID(func, bpf_lsm_inet_conn_established) 274 #endif /* CONFIG_SECURITY_NETWORK */ 275 276 BTF_ID(func, bpf_lsm_inode_create) 277 BTF_ID(func, bpf_lsm_inode_free_security) 278 BTF_ID(func, bpf_lsm_inode_getattr) 279 BTF_ID(func, bpf_lsm_inode_getxattr) 280 BTF_ID(func, bpf_lsm_inode_mknod) 281 BTF_ID(func, bpf_lsm_inode_need_killpriv) 282 BTF_ID(func, bpf_lsm_inode_post_setxattr) 283 BTF_ID(func, bpf_lsm_inode_readlink) 284 BTF_ID(func, bpf_lsm_inode_rename) 285 BTF_ID(func, bpf_lsm_inode_rmdir) 286 BTF_ID(func, bpf_lsm_inode_setattr) 287 BTF_ID(func, bpf_lsm_inode_setxattr) 288 BTF_ID(func, bpf_lsm_inode_symlink) 289 BTF_ID(func, bpf_lsm_inode_unlink) 290 BTF_ID(func, bpf_lsm_kernel_module_request) 291 BTF_ID(func, bpf_lsm_kernel_read_file) 292 BTF_ID(func, bpf_lsm_kernfs_init_security) 293 294 #ifdef CONFIG_KEYS 295 BTF_ID(func, bpf_lsm_key_free) 296 #endif /* CONFIG_KEYS */ 297 298 BTF_ID(func, bpf_lsm_mmap_file) 299 BTF_ID(func, bpf_lsm_netlink_send) 300 BTF_ID(func, bpf_lsm_path_notify) 301 BTF_ID(func, bpf_lsm_release_secctx) 302 BTF_ID(func, bpf_lsm_sb_alloc_security) 303 BTF_ID(func, bpf_lsm_sb_eat_lsm_opts) 304 BTF_ID(func, bpf_lsm_sb_kern_mount) 305 BTF_ID(func, bpf_lsm_sb_mount) 306 BTF_ID(func, bpf_lsm_sb_remount) 307 BTF_ID(func, bpf_lsm_sb_set_mnt_opts) 308 BTF_ID(func, bpf_lsm_sb_show_options) 309 BTF_ID(func, bpf_lsm_sb_statfs) 310 BTF_ID(func, bpf_lsm_sb_umount) 311 BTF_ID(func, bpf_lsm_settime) 312 313 #ifdef CONFIG_SECURITY_NETWORK 314 BTF_ID(func, bpf_lsm_socket_accept) 315 BTF_ID(func, bpf_lsm_socket_bind) 316 BTF_ID(func, bpf_lsm_socket_connect) 317 BTF_ID(func, bpf_lsm_socket_create) 318 BTF_ID(func, bpf_lsm_socket_getpeername) 319 BTF_ID(func, bpf_lsm_socket_getpeersec_dgram) 320 BTF_ID(func, bpf_lsm_socket_getsockname) 321 BTF_ID(func, bpf_lsm_socket_getsockopt) 322 BTF_ID(func, bpf_lsm_socket_listen) 323 BTF_ID(func, bpf_lsm_socket_post_create) 324 BTF_ID(func, bpf_lsm_socket_recvmsg) 325 BTF_ID(func, bpf_lsm_socket_sendmsg) 326 BTF_ID(func, bpf_lsm_socket_shutdown) 327 BTF_ID(func, bpf_lsm_socket_socketpair) 328 #endif /* CONFIG_SECURITY_NETWORK */ 329 330 BTF_ID(func, bpf_lsm_syslog) 331 BTF_ID(func, bpf_lsm_task_alloc) 332 BTF_ID(func, bpf_lsm_current_getsecid_subj) 333 BTF_ID(func, bpf_lsm_task_getsecid_obj) 334 BTF_ID(func, bpf_lsm_task_prctl) 335 BTF_ID(func, bpf_lsm_task_setscheduler) 336 BTF_ID(func, bpf_lsm_task_to_inode) 337 BTF_SET_END(sleepable_lsm_hooks) 338 339 bool bpf_lsm_is_sleepable_hook(u32 btf_id) 340 { 341 return btf_id_set_contains(&sleepable_lsm_hooks, btf_id); 342 } 343 344 const struct bpf_prog_ops lsm_prog_ops = { 345 }; 346 347 const struct bpf_verifier_ops lsm_verifier_ops = { 348 .get_func_proto = bpf_lsm_func_proto, 349 .is_valid_access = btf_ctx_access, 350 }; 351