KP Singh | fc611f4 | 2020-03-29 01:43:49 +0100 | [diff] [blame] | 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> |
KP Singh | 3f6719c | 2020-11-17 23:29:28 +0000 | [diff] [blame] | 10 | #include <linux/binfmts.h> |
KP Singh | 9d3fdea | 2020-03-29 01:43:51 +0100 | [diff] [blame] | 11 | #include <linux/lsm_hooks.h> |
| 12 | #include <linux/bpf_lsm.h> |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 13 | #include <linux/kallsyms.h> |
| 14 | #include <linux/bpf_verifier.h> |
KP Singh | 3089783 | 2020-08-25 20:29:18 +0200 | [diff] [blame] | 15 | #include <net/bpf_sk_storage.h> |
| 16 | #include <linux/bpf_local_storage.h> |
KP Singh | 6f64e47 | 2020-11-05 23:06:51 +0000 | [diff] [blame] | 17 | #include <linux/btf_ids.h> |
KP Singh | 27672f0 | 2020-11-24 15:12:09 +0000 | [diff] [blame] | 18 | #include <linux/ima.h> |
KP Singh | 9d3fdea | 2020-03-29 01:43:51 +0100 | [diff] [blame] | 19 | |
| 20 | /* For every LSM hook that allows attachment of BPF programs, declare a nop |
| 21 | * function where a BPF program can be attached. |
| 22 | */ |
| 23 | #define LSM_HOOK(RET, DEFAULT, NAME, ...) \ |
| 24 | noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ |
| 25 | { \ |
| 26 | return DEFAULT; \ |
| 27 | } |
| 28 | |
| 29 | #include <linux/lsm_hook_defs.h> |
| 30 | #undef LSM_HOOK |
KP Singh | fc611f4 | 2020-03-29 01:43:49 +0100 | [diff] [blame] | 31 | |
KP Singh | 6f64e47 | 2020-11-05 23:06:51 +0000 | [diff] [blame] | 32 | #define LSM_HOOK(RET, DEFAULT, NAME, ...) BTF_ID(func, bpf_lsm_##NAME) |
| 33 | BTF_SET_START(bpf_lsm_hooks) |
| 34 | #include <linux/lsm_hook_defs.h> |
| 35 | #undef LSM_HOOK |
| 36 | BTF_SET_END(bpf_lsm_hooks) |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 37 | |
| 38 | int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, |
| 39 | const struct bpf_prog *prog) |
| 40 | { |
| 41 | if (!prog->gpl_compatible) { |
| 42 | bpf_log(vlog, |
| 43 | "LSM programs must have a GPL compatible license\n"); |
| 44 | return -EINVAL; |
| 45 | } |
| 46 | |
KP Singh | 6f64e47 | 2020-11-05 23:06:51 +0000 | [diff] [blame] | 47 | if (!btf_id_set_contains(&bpf_lsm_hooks, prog->aux->attach_btf_id)) { |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 48 | bpf_log(vlog, "attach_btf_id %u points to wrong type name %s\n", |
| 49 | prog->aux->attach_btf_id, prog->aux->attach_func_name); |
| 50 | return -EINVAL; |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
KP Singh | 3f6719c | 2020-11-17 23:29:28 +0000 | [diff] [blame] | 56 | /* Mask for all the currently supported BPRM option flags */ |
| 57 | #define BPF_F_BRPM_OPTS_MASK BPF_F_BPRM_SECUREEXEC |
| 58 | |
| 59 | BPF_CALL_2(bpf_bprm_opts_set, struct linux_binprm *, bprm, u64, flags) |
| 60 | { |
| 61 | if (flags & ~BPF_F_BRPM_OPTS_MASK) |
| 62 | return -EINVAL; |
| 63 | |
| 64 | bprm->secureexec = (flags & BPF_F_BPRM_SECUREEXEC); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | BTF_ID_LIST_SINGLE(bpf_bprm_opts_set_btf_ids, struct, linux_binprm) |
| 69 | |
| 70 | const static struct bpf_func_proto bpf_bprm_opts_set_proto = { |
| 71 | .func = bpf_bprm_opts_set, |
| 72 | .gpl_only = false, |
| 73 | .ret_type = RET_INTEGER, |
| 74 | .arg1_type = ARG_PTR_TO_BTF_ID, |
| 75 | .arg1_btf_id = &bpf_bprm_opts_set_btf_ids[0], |
| 76 | .arg2_type = ARG_ANYTHING, |
| 77 | }; |
| 78 | |
KP Singh | 27672f0 | 2020-11-24 15:12:09 +0000 | [diff] [blame] | 79 | BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, size) |
| 80 | { |
| 81 | return ima_inode_hash(inode, dst, size); |
| 82 | } |
| 83 | |
| 84 | static bool bpf_ima_inode_hash_allowed(const struct bpf_prog *prog) |
| 85 | { |
| 86 | return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id); |
| 87 | } |
| 88 | |
| 89 | BTF_ID_LIST_SINGLE(bpf_ima_inode_hash_btf_ids, struct, inode) |
| 90 | |
| 91 | const static struct bpf_func_proto bpf_ima_inode_hash_proto = { |
| 92 | .func = bpf_ima_inode_hash, |
| 93 | .gpl_only = false, |
| 94 | .ret_type = RET_INTEGER, |
| 95 | .arg1_type = ARG_PTR_TO_BTF_ID, |
| 96 | .arg1_btf_id = &bpf_ima_inode_hash_btf_ids[0], |
| 97 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, |
| 98 | .arg3_type = ARG_CONST_SIZE, |
| 99 | .allowed = bpf_ima_inode_hash_allowed, |
| 100 | }; |
| 101 | |
KP Singh | 3089783 | 2020-08-25 20:29:18 +0200 | [diff] [blame] | 102 | static const struct bpf_func_proto * |
| 103 | bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 104 | { |
| 105 | switch (func_id) { |
| 106 | case BPF_FUNC_inode_storage_get: |
| 107 | return &bpf_inode_storage_get_proto; |
| 108 | case BPF_FUNC_inode_storage_delete: |
| 109 | return &bpf_inode_storage_delete_proto; |
| 110 | case BPF_FUNC_sk_storage_get: |
Martin KaFai Lau | 592a349 | 2020-09-24 17:04:02 -0700 | [diff] [blame] | 111 | return &bpf_sk_storage_get_proto; |
KP Singh | 3089783 | 2020-08-25 20:29:18 +0200 | [diff] [blame] | 112 | case BPF_FUNC_sk_storage_delete: |
Martin KaFai Lau | 592a349 | 2020-09-24 17:04:02 -0700 | [diff] [blame] | 113 | return &bpf_sk_storage_delete_proto; |
KP Singh | 9e7a4d9 | 2020-11-06 10:37:39 +0000 | [diff] [blame] | 114 | case BPF_FUNC_spin_lock: |
| 115 | return &bpf_spin_lock_proto; |
| 116 | case BPF_FUNC_spin_unlock: |
| 117 | return &bpf_spin_unlock_proto; |
KP Singh | 4cf1bc1 | 2020-11-06 10:37:40 +0000 | [diff] [blame] | 118 | case BPF_FUNC_task_storage_get: |
| 119 | return &bpf_task_storage_get_proto; |
| 120 | case BPF_FUNC_task_storage_delete: |
| 121 | return &bpf_task_storage_delete_proto; |
KP Singh | 3f6719c | 2020-11-17 23:29:28 +0000 | [diff] [blame] | 122 | case BPF_FUNC_bprm_opts_set: |
| 123 | return &bpf_bprm_opts_set_proto; |
KP Singh | 27672f0 | 2020-11-24 15:12:09 +0000 | [diff] [blame] | 124 | case BPF_FUNC_ima_inode_hash: |
| 125 | return prog->aux->sleepable ? &bpf_ima_inode_hash_proto : NULL; |
KP Singh | 3089783 | 2020-08-25 20:29:18 +0200 | [diff] [blame] | 126 | default: |
| 127 | return tracing_prog_func_proto(func_id, prog); |
| 128 | } |
| 129 | } |
| 130 | |
KP Singh | 423f161 | 2020-11-13 00:59:29 +0000 | [diff] [blame] | 131 | /* The set of hooks which are called without pagefaults disabled and are allowed |
| 132 | * to "sleep" and thus can be used for sleeable BPF programs. |
| 133 | */ |
| 134 | BTF_SET_START(sleepable_lsm_hooks) |
| 135 | BTF_ID(func, bpf_lsm_bpf) |
| 136 | BTF_ID(func, bpf_lsm_bpf_map) |
| 137 | BTF_ID(func, bpf_lsm_bpf_map_alloc_security) |
| 138 | BTF_ID(func, bpf_lsm_bpf_map_free_security) |
| 139 | BTF_ID(func, bpf_lsm_bpf_prog) |
| 140 | BTF_ID(func, bpf_lsm_bprm_check_security) |
| 141 | BTF_ID(func, bpf_lsm_bprm_committed_creds) |
| 142 | BTF_ID(func, bpf_lsm_bprm_committing_creds) |
| 143 | BTF_ID(func, bpf_lsm_bprm_creds_for_exec) |
| 144 | BTF_ID(func, bpf_lsm_bprm_creds_from_file) |
| 145 | BTF_ID(func, bpf_lsm_capget) |
| 146 | BTF_ID(func, bpf_lsm_capset) |
| 147 | BTF_ID(func, bpf_lsm_cred_prepare) |
| 148 | BTF_ID(func, bpf_lsm_file_ioctl) |
| 149 | BTF_ID(func, bpf_lsm_file_lock) |
| 150 | BTF_ID(func, bpf_lsm_file_open) |
| 151 | BTF_ID(func, bpf_lsm_file_receive) |
Mikko Ylinen | 7803138 | 2021-01-25 08:39:36 +0200 | [diff] [blame] | 152 | |
| 153 | #ifdef CONFIG_SECURITY_NETWORK |
KP Singh | 423f161 | 2020-11-13 00:59:29 +0000 | [diff] [blame] | 154 | BTF_ID(func, bpf_lsm_inet_conn_established) |
Mikko Ylinen | 7803138 | 2021-01-25 08:39:36 +0200 | [diff] [blame] | 155 | #endif /* CONFIG_SECURITY_NETWORK */ |
| 156 | |
KP Singh | 423f161 | 2020-11-13 00:59:29 +0000 | [diff] [blame] | 157 | BTF_ID(func, bpf_lsm_inode_create) |
| 158 | BTF_ID(func, bpf_lsm_inode_free_security) |
| 159 | BTF_ID(func, bpf_lsm_inode_getattr) |
| 160 | BTF_ID(func, bpf_lsm_inode_getxattr) |
| 161 | BTF_ID(func, bpf_lsm_inode_mknod) |
| 162 | BTF_ID(func, bpf_lsm_inode_need_killpriv) |
| 163 | BTF_ID(func, bpf_lsm_inode_post_setxattr) |
| 164 | BTF_ID(func, bpf_lsm_inode_readlink) |
| 165 | BTF_ID(func, bpf_lsm_inode_rename) |
| 166 | BTF_ID(func, bpf_lsm_inode_rmdir) |
| 167 | BTF_ID(func, bpf_lsm_inode_setattr) |
| 168 | BTF_ID(func, bpf_lsm_inode_setxattr) |
| 169 | BTF_ID(func, bpf_lsm_inode_symlink) |
| 170 | BTF_ID(func, bpf_lsm_inode_unlink) |
| 171 | BTF_ID(func, bpf_lsm_kernel_module_request) |
| 172 | BTF_ID(func, bpf_lsm_kernfs_init_security) |
Mikko Ylinen | 7803138 | 2021-01-25 08:39:36 +0200 | [diff] [blame] | 173 | |
| 174 | #ifdef CONFIG_KEYS |
KP Singh | 423f161 | 2020-11-13 00:59:29 +0000 | [diff] [blame] | 175 | BTF_ID(func, bpf_lsm_key_free) |
Mikko Ylinen | 7803138 | 2021-01-25 08:39:36 +0200 | [diff] [blame] | 176 | #endif /* CONFIG_KEYS */ |
| 177 | |
KP Singh | 423f161 | 2020-11-13 00:59:29 +0000 | [diff] [blame] | 178 | BTF_ID(func, bpf_lsm_mmap_file) |
| 179 | BTF_ID(func, bpf_lsm_netlink_send) |
| 180 | BTF_ID(func, bpf_lsm_path_notify) |
| 181 | BTF_ID(func, bpf_lsm_release_secctx) |
| 182 | BTF_ID(func, bpf_lsm_sb_alloc_security) |
| 183 | BTF_ID(func, bpf_lsm_sb_eat_lsm_opts) |
| 184 | BTF_ID(func, bpf_lsm_sb_kern_mount) |
| 185 | BTF_ID(func, bpf_lsm_sb_mount) |
| 186 | BTF_ID(func, bpf_lsm_sb_remount) |
| 187 | BTF_ID(func, bpf_lsm_sb_set_mnt_opts) |
| 188 | BTF_ID(func, bpf_lsm_sb_show_options) |
| 189 | BTF_ID(func, bpf_lsm_sb_statfs) |
| 190 | BTF_ID(func, bpf_lsm_sb_umount) |
| 191 | BTF_ID(func, bpf_lsm_settime) |
Mikko Ylinen | 7803138 | 2021-01-25 08:39:36 +0200 | [diff] [blame] | 192 | |
| 193 | #ifdef CONFIG_SECURITY_NETWORK |
KP Singh | 423f161 | 2020-11-13 00:59:29 +0000 | [diff] [blame] | 194 | BTF_ID(func, bpf_lsm_socket_accept) |
| 195 | BTF_ID(func, bpf_lsm_socket_bind) |
| 196 | BTF_ID(func, bpf_lsm_socket_connect) |
| 197 | BTF_ID(func, bpf_lsm_socket_create) |
| 198 | BTF_ID(func, bpf_lsm_socket_getpeername) |
| 199 | BTF_ID(func, bpf_lsm_socket_getpeersec_dgram) |
| 200 | BTF_ID(func, bpf_lsm_socket_getsockname) |
| 201 | BTF_ID(func, bpf_lsm_socket_getsockopt) |
| 202 | BTF_ID(func, bpf_lsm_socket_listen) |
| 203 | BTF_ID(func, bpf_lsm_socket_post_create) |
| 204 | BTF_ID(func, bpf_lsm_socket_recvmsg) |
| 205 | BTF_ID(func, bpf_lsm_socket_sendmsg) |
| 206 | BTF_ID(func, bpf_lsm_socket_shutdown) |
| 207 | BTF_ID(func, bpf_lsm_socket_socketpair) |
Mikko Ylinen | 7803138 | 2021-01-25 08:39:36 +0200 | [diff] [blame] | 208 | #endif /* CONFIG_SECURITY_NETWORK */ |
| 209 | |
KP Singh | 423f161 | 2020-11-13 00:59:29 +0000 | [diff] [blame] | 210 | BTF_ID(func, bpf_lsm_syslog) |
| 211 | BTF_ID(func, bpf_lsm_task_alloc) |
Paul Moore | 4ebd765 | 2021-02-19 14:26:21 -0500 | [diff] [blame^] | 212 | BTF_ID(func, bpf_lsm_task_getsecid_subj) |
| 213 | BTF_ID(func, bpf_lsm_task_getsecid_obj) |
KP Singh | 423f161 | 2020-11-13 00:59:29 +0000 | [diff] [blame] | 214 | BTF_ID(func, bpf_lsm_task_prctl) |
| 215 | BTF_ID(func, bpf_lsm_task_setscheduler) |
| 216 | BTF_ID(func, bpf_lsm_task_to_inode) |
| 217 | BTF_SET_END(sleepable_lsm_hooks) |
| 218 | |
| 219 | bool bpf_lsm_is_sleepable_hook(u32 btf_id) |
| 220 | { |
| 221 | return btf_id_set_contains(&sleepable_lsm_hooks, btf_id); |
| 222 | } |
| 223 | |
KP Singh | fc611f4 | 2020-03-29 01:43:49 +0100 | [diff] [blame] | 224 | const struct bpf_prog_ops lsm_prog_ops = { |
| 225 | }; |
| 226 | |
| 227 | const struct bpf_verifier_ops lsm_verifier_ops = { |
KP Singh | 3089783 | 2020-08-25 20:29:18 +0200 | [diff] [blame] | 228 | .get_func_proto = bpf_lsm_func_proto, |
KP Singh | fc611f4 | 2020-03-29 01:43:49 +0100 | [diff] [blame] | 229 | .is_valid_access = btf_ctx_access, |
| 230 | }; |