bpf_lsm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Google LLC.
  4. */
  5. #include <linux/filter.h>
  6. #include <linux/bpf.h>
  7. #include <linux/btf.h>
  8. #include <linux/binfmts.h>
  9. #include <linux/lsm_hooks.h>
  10. #include <linux/bpf_lsm.h>
  11. #include <linux/kallsyms.h>
  12. #include <linux/bpf_verifier.h>
  13. #include <net/bpf_sk_storage.h>
  14. #include <linux/bpf_local_storage.h>
  15. #include <linux/btf_ids.h>
  16. #include <linux/ima.h>
  17. #include <linux/bpf-cgroup.h>
  18. /* For every LSM hook that allows attachment of BPF programs, declare a nop
  19. * function where a BPF program can be attached.
  20. */
  21. #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
  22. noinline RET bpf_lsm_##NAME(__VA_ARGS__) \
  23. { \
  24. return DEFAULT; \
  25. }
  26. #include <linux/lsm_hook_defs.h>
  27. #undef LSM_HOOK
  28. #define LSM_HOOK(RET, DEFAULT, NAME, ...) BTF_ID(func, bpf_lsm_##NAME)
  29. BTF_SET_START(bpf_lsm_hooks)
  30. #include <linux/lsm_hook_defs.h>
  31. #undef LSM_HOOK
  32. BTF_SET_END(bpf_lsm_hooks)
  33. /* List of LSM hooks that should operate on 'current' cgroup regardless
  34. * of function signature.
  35. */
  36. BTF_SET_START(bpf_lsm_current_hooks)
  37. /* operate on freshly allocated sk without any cgroup association */
  38. #ifdef CONFIG_SECURITY_NETWORK
  39. BTF_ID(func, bpf_lsm_sk_alloc_security)
  40. BTF_ID(func, bpf_lsm_sk_free_security)
  41. #endif
  42. BTF_SET_END(bpf_lsm_current_hooks)
  43. /* List of LSM hooks that trigger while the socket is properly locked.
  44. */
  45. BTF_SET_START(bpf_lsm_locked_sockopt_hooks)
  46. #ifdef CONFIG_SECURITY_NETWORK
  47. BTF_ID(func, bpf_lsm_sock_graft)
  48. BTF_ID(func, bpf_lsm_inet_csk_clone)
  49. BTF_ID(func, bpf_lsm_inet_conn_established)
  50. #endif
  51. BTF_SET_END(bpf_lsm_locked_sockopt_hooks)
  52. /* List of LSM hooks that trigger while the socket is _not_ locked,
  53. * but it's ok to call bpf_{g,s}etsockopt because the socket is still
  54. * in the early init phase.
  55. */
  56. BTF_SET_START(bpf_lsm_unlocked_sockopt_hooks)
  57. #ifdef CONFIG_SECURITY_NETWORK
  58. BTF_ID(func, bpf_lsm_socket_post_create)
  59. BTF_ID(func, bpf_lsm_socket_socketpair)
  60. #endif
  61. BTF_SET_END(bpf_lsm_unlocked_sockopt_hooks)
  62. #ifdef CONFIG_CGROUP_BPF
  63. void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog,
  64. bpf_func_t *bpf_func)
  65. {
  66. const struct btf_param *args __maybe_unused;
  67. if (btf_type_vlen(prog->aux->attach_func_proto) < 1 ||
  68. btf_id_set_contains(&bpf_lsm_current_hooks,
  69. prog->aux->attach_btf_id)) {
  70. *bpf_func = __cgroup_bpf_run_lsm_current;
  71. return;
  72. }
  73. #ifdef CONFIG_NET
  74. args = btf_params(prog->aux->attach_func_proto);
  75. if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCKET])
  76. *bpf_func = __cgroup_bpf_run_lsm_socket;
  77. else if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCK])
  78. *bpf_func = __cgroup_bpf_run_lsm_sock;
  79. else
  80. #endif
  81. *bpf_func = __cgroup_bpf_run_lsm_current;
  82. }
  83. #endif
  84. int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
  85. const struct bpf_prog *prog)
  86. {
  87. if (!prog->gpl_compatible) {
  88. bpf_log(vlog,
  89. "LSM programs must have a GPL compatible license\n");
  90. return -EINVAL;
  91. }
  92. if (!btf_id_set_contains(&bpf_lsm_hooks, prog->aux->attach_btf_id)) {
  93. bpf_log(vlog, "attach_btf_id %u points to wrong type name %s\n",
  94. prog->aux->attach_btf_id, prog->aux->attach_func_name);
  95. return -EINVAL;
  96. }
  97. return 0;
  98. }
  99. /* Mask for all the currently supported BPRM option flags */
  100. #define BPF_F_BRPM_OPTS_MASK BPF_F_BPRM_SECUREEXEC
  101. BPF_CALL_2(bpf_bprm_opts_set, struct linux_binprm *, bprm, u64, flags)
  102. {
  103. if (flags & ~BPF_F_BRPM_OPTS_MASK)
  104. return -EINVAL;
  105. bprm->secureexec = (flags & BPF_F_BPRM_SECUREEXEC);
  106. return 0;
  107. }
  108. BTF_ID_LIST_SINGLE(bpf_bprm_opts_set_btf_ids, struct, linux_binprm)
  109. static const struct bpf_func_proto bpf_bprm_opts_set_proto = {
  110. .func = bpf_bprm_opts_set,
  111. .gpl_only = false,
  112. .ret_type = RET_INTEGER,
  113. .arg1_type = ARG_PTR_TO_BTF_ID,
  114. .arg1_btf_id = &bpf_bprm_opts_set_btf_ids[0],
  115. .arg2_type = ARG_ANYTHING,
  116. };
  117. BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, size)
  118. {
  119. return ima_inode_hash(inode, dst, size);
  120. }
  121. static bool bpf_ima_inode_hash_allowed(const struct bpf_prog *prog)
  122. {
  123. return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
  124. }
  125. BTF_ID_LIST_SINGLE(bpf_ima_inode_hash_btf_ids, struct, inode)
  126. static const struct bpf_func_proto bpf_ima_inode_hash_proto = {
  127. .func = bpf_ima_inode_hash,
  128. .gpl_only = false,
  129. .ret_type = RET_INTEGER,
  130. .arg1_type = ARG_PTR_TO_BTF_ID,
  131. .arg1_btf_id = &bpf_ima_inode_hash_btf_ids[0],
  132. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  133. .arg3_type = ARG_CONST_SIZE,
  134. .allowed = bpf_ima_inode_hash_allowed,
  135. };
  136. BPF_CALL_3(bpf_ima_file_hash, struct file *, file, void *, dst, u32, size)
  137. {
  138. return ima_file_hash(file, dst, size);
  139. }
  140. BTF_ID_LIST_SINGLE(bpf_ima_file_hash_btf_ids, struct, file)
  141. static const struct bpf_func_proto bpf_ima_file_hash_proto = {
  142. .func = bpf_ima_file_hash,
  143. .gpl_only = false,
  144. .ret_type = RET_INTEGER,
  145. .arg1_type = ARG_PTR_TO_BTF_ID,
  146. .arg1_btf_id = &bpf_ima_file_hash_btf_ids[0],
  147. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  148. .arg3_type = ARG_CONST_SIZE,
  149. .allowed = bpf_ima_inode_hash_allowed,
  150. };
  151. BPF_CALL_1(bpf_get_attach_cookie, void *, ctx)
  152. {
  153. struct bpf_trace_run_ctx *run_ctx;
  154. run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
  155. return run_ctx->bpf_cookie;
  156. }
  157. static const struct bpf_func_proto bpf_get_attach_cookie_proto = {
  158. .func = bpf_get_attach_cookie,
  159. .gpl_only = false,
  160. .ret_type = RET_INTEGER,
  161. .arg1_type = ARG_PTR_TO_CTX,
  162. };
  163. static const struct bpf_func_proto *
  164. bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  165. {
  166. const struct bpf_func_proto *func_proto;
  167. if (prog->expected_attach_type == BPF_LSM_CGROUP) {
  168. func_proto = cgroup_common_func_proto(func_id, prog);
  169. if (func_proto)
  170. return func_proto;
  171. }
  172. switch (func_id) {
  173. case BPF_FUNC_inode_storage_get:
  174. return &bpf_inode_storage_get_proto;
  175. case BPF_FUNC_inode_storage_delete:
  176. return &bpf_inode_storage_delete_proto;
  177. #ifdef CONFIG_NET
  178. case BPF_FUNC_sk_storage_get:
  179. return &bpf_sk_storage_get_proto;
  180. case BPF_FUNC_sk_storage_delete:
  181. return &bpf_sk_storage_delete_proto;
  182. #endif /* CONFIG_NET */
  183. case BPF_FUNC_spin_lock:
  184. return &bpf_spin_lock_proto;
  185. case BPF_FUNC_spin_unlock:
  186. return &bpf_spin_unlock_proto;
  187. case BPF_FUNC_bprm_opts_set:
  188. return &bpf_bprm_opts_set_proto;
  189. case BPF_FUNC_ima_inode_hash:
  190. return prog->aux->sleepable ? &bpf_ima_inode_hash_proto : NULL;
  191. case BPF_FUNC_ima_file_hash:
  192. return prog->aux->sleepable ? &bpf_ima_file_hash_proto : NULL;
  193. case BPF_FUNC_get_attach_cookie:
  194. return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto : NULL;
  195. #ifdef CONFIG_NET
  196. case BPF_FUNC_setsockopt:
  197. if (prog->expected_attach_type != BPF_LSM_CGROUP)
  198. return NULL;
  199. if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks,
  200. prog->aux->attach_btf_id))
  201. return &bpf_sk_setsockopt_proto;
  202. if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks,
  203. prog->aux->attach_btf_id))
  204. return &bpf_unlocked_sk_setsockopt_proto;
  205. return NULL;
  206. case BPF_FUNC_getsockopt:
  207. if (prog->expected_attach_type != BPF_LSM_CGROUP)
  208. return NULL;
  209. if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks,
  210. prog->aux->attach_btf_id))
  211. return &bpf_sk_getsockopt_proto;
  212. if (btf_id_set_contains(&bpf_lsm_unlocked_sockopt_hooks,
  213. prog->aux->attach_btf_id))
  214. return &bpf_unlocked_sk_getsockopt_proto;
  215. return NULL;
  216. #endif
  217. default:
  218. return tracing_prog_func_proto(func_id, prog);
  219. }
  220. }
  221. /* The set of hooks which are called without pagefaults disabled and are allowed
  222. * to "sleep" and thus can be used for sleepable BPF programs.
  223. */
  224. BTF_SET_START(sleepable_lsm_hooks)
  225. BTF_ID(func, bpf_lsm_bpf)
  226. BTF_ID(func, bpf_lsm_bpf_map)
  227. BTF_ID(func, bpf_lsm_bpf_map_alloc_security)
  228. BTF_ID(func, bpf_lsm_bpf_map_free_security)
  229. BTF_ID(func, bpf_lsm_bpf_prog)
  230. BTF_ID(func, bpf_lsm_bprm_check_security)
  231. BTF_ID(func, bpf_lsm_bprm_committed_creds)
  232. BTF_ID(func, bpf_lsm_bprm_committing_creds)
  233. BTF_ID(func, bpf_lsm_bprm_creds_for_exec)
  234. BTF_ID(func, bpf_lsm_bprm_creds_from_file)
  235. BTF_ID(func, bpf_lsm_capget)
  236. BTF_ID(func, bpf_lsm_capset)
  237. BTF_ID(func, bpf_lsm_cred_prepare)
  238. BTF_ID(func, bpf_lsm_file_ioctl)
  239. BTF_ID(func, bpf_lsm_file_lock)
  240. BTF_ID(func, bpf_lsm_file_open)
  241. BTF_ID(func, bpf_lsm_file_receive)
  242. #ifdef CONFIG_SECURITY_NETWORK
  243. BTF_ID(func, bpf_lsm_inet_conn_established)
  244. #endif /* CONFIG_SECURITY_NETWORK */
  245. BTF_ID(func, bpf_lsm_inode_create)
  246. BTF_ID(func, bpf_lsm_inode_free_security)
  247. BTF_ID(func, bpf_lsm_inode_getattr)
  248. BTF_ID(func, bpf_lsm_inode_getxattr)
  249. BTF_ID(func, bpf_lsm_inode_mknod)
  250. BTF_ID(func, bpf_lsm_inode_need_killpriv)
  251. BTF_ID(func, bpf_lsm_inode_post_setxattr)
  252. BTF_ID(func, bpf_lsm_inode_readlink)
  253. BTF_ID(func, bpf_lsm_inode_rename)
  254. BTF_ID(func, bpf_lsm_inode_rmdir)
  255. BTF_ID(func, bpf_lsm_inode_setattr)
  256. BTF_ID(func, bpf_lsm_inode_setxattr)
  257. BTF_ID(func, bpf_lsm_inode_symlink)
  258. BTF_ID(func, bpf_lsm_inode_unlink)
  259. BTF_ID(func, bpf_lsm_kernel_module_request)
  260. BTF_ID(func, bpf_lsm_kernel_read_file)
  261. BTF_ID(func, bpf_lsm_kernfs_init_security)
  262. #ifdef CONFIG_KEYS
  263. BTF_ID(func, bpf_lsm_key_free)
  264. #endif /* CONFIG_KEYS */
  265. BTF_ID(func, bpf_lsm_mmap_file)
  266. BTF_ID(func, bpf_lsm_netlink_send)
  267. BTF_ID(func, bpf_lsm_path_notify)
  268. BTF_ID(func, bpf_lsm_release_secctx)
  269. BTF_ID(func, bpf_lsm_sb_alloc_security)
  270. BTF_ID(func, bpf_lsm_sb_eat_lsm_opts)
  271. BTF_ID(func, bpf_lsm_sb_kern_mount)
  272. BTF_ID(func, bpf_lsm_sb_mount)
  273. BTF_ID(func, bpf_lsm_sb_remount)
  274. BTF_ID(func, bpf_lsm_sb_set_mnt_opts)
  275. BTF_ID(func, bpf_lsm_sb_show_options)
  276. BTF_ID(func, bpf_lsm_sb_statfs)
  277. BTF_ID(func, bpf_lsm_sb_umount)
  278. BTF_ID(func, bpf_lsm_settime)
  279. #ifdef CONFIG_SECURITY_NETWORK
  280. BTF_ID(func, bpf_lsm_socket_accept)
  281. BTF_ID(func, bpf_lsm_socket_bind)
  282. BTF_ID(func, bpf_lsm_socket_connect)
  283. BTF_ID(func, bpf_lsm_socket_create)
  284. BTF_ID(func, bpf_lsm_socket_getpeername)
  285. BTF_ID(func, bpf_lsm_socket_getpeersec_dgram)
  286. BTF_ID(func, bpf_lsm_socket_getsockname)
  287. BTF_ID(func, bpf_lsm_socket_getsockopt)
  288. BTF_ID(func, bpf_lsm_socket_listen)
  289. BTF_ID(func, bpf_lsm_socket_post_create)
  290. BTF_ID(func, bpf_lsm_socket_recvmsg)
  291. BTF_ID(func, bpf_lsm_socket_sendmsg)
  292. BTF_ID(func, bpf_lsm_socket_shutdown)
  293. BTF_ID(func, bpf_lsm_socket_socketpair)
  294. #endif /* CONFIG_SECURITY_NETWORK */
  295. BTF_ID(func, bpf_lsm_syslog)
  296. BTF_ID(func, bpf_lsm_task_alloc)
  297. BTF_ID(func, bpf_lsm_current_getsecid_subj)
  298. BTF_ID(func, bpf_lsm_task_getsecid_obj)
  299. BTF_ID(func, bpf_lsm_task_prctl)
  300. BTF_ID(func, bpf_lsm_task_setscheduler)
  301. BTF_ID(func, bpf_lsm_task_to_inode)
  302. BTF_ID(func, bpf_lsm_userns_create)
  303. BTF_SET_END(sleepable_lsm_hooks)
  304. bool bpf_lsm_is_sleepable_hook(u32 btf_id)
  305. {
  306. return btf_id_set_contains(&sleepable_lsm_hooks, btf_id);
  307. }
  308. const struct bpf_prog_ops lsm_prog_ops = {
  309. };
  310. const struct bpf_verifier_ops lsm_verifier_ops = {
  311. .get_func_proto = bpf_lsm_func_proto,
  312. .is_valid_access = btf_ctx_access,
  313. };