bpf_lsm.c 13 KB

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