token.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include <linux/bpf.h>
  2. #include <linux/vmalloc.h>
  3. #include <linux/fdtable.h>
  4. #include <linux/file.h>
  5. #include <linux/fs.h>
  6. #include <linux/kernel.h>
  7. #include <linux/idr.h>
  8. #include <linux/namei.h>
  9. #include <linux/user_namespace.h>
  10. #include <linux/security.h>
  11. static bool bpf_ns_capable(struct user_namespace *ns, int cap)
  12. {
  13. return ns_capable(ns, cap) || (cap != CAP_SYS_ADMIN && ns_capable(ns, CAP_SYS_ADMIN));
  14. }
  15. bool bpf_token_capable(const struct bpf_token *token, int cap)
  16. {
  17. struct user_namespace *userns;
  18. /* BPF token allows ns_capable() level of capabilities */
  19. userns = token ? token->userns : &init_user_ns;
  20. if (!bpf_ns_capable(userns, cap))
  21. return false;
  22. if (token && security_bpf_token_capable(token, cap) < 0)
  23. return false;
  24. return true;
  25. }
  26. void bpf_token_inc(struct bpf_token *token)
  27. {
  28. atomic64_inc(&token->refcnt);
  29. }
  30. static void bpf_token_free(struct bpf_token *token)
  31. {
  32. security_bpf_token_free(token);
  33. put_user_ns(token->userns);
  34. kfree(token);
  35. }
  36. static void bpf_token_put_deferred(struct work_struct *work)
  37. {
  38. struct bpf_token *token = container_of(work, struct bpf_token, work);
  39. bpf_token_free(token);
  40. }
  41. void bpf_token_put(struct bpf_token *token)
  42. {
  43. if (!token)
  44. return;
  45. if (!atomic64_dec_and_test(&token->refcnt))
  46. return;
  47. INIT_WORK(&token->work, bpf_token_put_deferred);
  48. schedule_work(&token->work);
  49. }
  50. static int bpf_token_release(struct inode *inode, struct file *filp)
  51. {
  52. struct bpf_token *token = filp->private_data;
  53. bpf_token_put(token);
  54. return 0;
  55. }
  56. static void bpf_token_show_fdinfo(struct seq_file *m, struct file *filp)
  57. {
  58. struct bpf_token *token = filp->private_data;
  59. u64 mask;
  60. BUILD_BUG_ON(__MAX_BPF_CMD >= 64);
  61. mask = BIT_ULL(__MAX_BPF_CMD) - 1;
  62. if ((token->allowed_cmds & mask) == mask)
  63. seq_printf(m, "allowed_cmds:\tany\n");
  64. else
  65. seq_printf(m, "allowed_cmds:\t0x%llx\n", token->allowed_cmds);
  66. BUILD_BUG_ON(__MAX_BPF_MAP_TYPE >= 64);
  67. mask = BIT_ULL(__MAX_BPF_MAP_TYPE) - 1;
  68. if ((token->allowed_maps & mask) == mask)
  69. seq_printf(m, "allowed_maps:\tany\n");
  70. else
  71. seq_printf(m, "allowed_maps:\t0x%llx\n", token->allowed_maps);
  72. BUILD_BUG_ON(__MAX_BPF_PROG_TYPE >= 64);
  73. mask = BIT_ULL(__MAX_BPF_PROG_TYPE) - 1;
  74. if ((token->allowed_progs & mask) == mask)
  75. seq_printf(m, "allowed_progs:\tany\n");
  76. else
  77. seq_printf(m, "allowed_progs:\t0x%llx\n", token->allowed_progs);
  78. BUILD_BUG_ON(__MAX_BPF_ATTACH_TYPE >= 64);
  79. mask = BIT_ULL(__MAX_BPF_ATTACH_TYPE) - 1;
  80. if ((token->allowed_attachs & mask) == mask)
  81. seq_printf(m, "allowed_attachs:\tany\n");
  82. else
  83. seq_printf(m, "allowed_attachs:\t0x%llx\n", token->allowed_attachs);
  84. }
  85. #define BPF_TOKEN_INODE_NAME "bpf-token"
  86. static const struct inode_operations bpf_token_iops = { };
  87. static const struct file_operations bpf_token_fops = {
  88. .release = bpf_token_release,
  89. .show_fdinfo = bpf_token_show_fdinfo,
  90. };
  91. int bpf_token_create(union bpf_attr *attr)
  92. {
  93. struct bpf_mount_opts *mnt_opts;
  94. struct bpf_token *token = NULL;
  95. struct user_namespace *userns;
  96. struct inode *inode;
  97. struct file *file;
  98. CLASS(fd, f)(attr->token_create.bpffs_fd);
  99. struct path path;
  100. struct super_block *sb;
  101. umode_t mode;
  102. int err, fd;
  103. if (fd_empty(f))
  104. return -EBADF;
  105. path = fd_file(f)->f_path;
  106. sb = path.dentry->d_sb;
  107. if (path.dentry != sb->s_root)
  108. return -EINVAL;
  109. if (sb->s_op != &bpf_super_ops)
  110. return -EINVAL;
  111. err = path_permission(&path, MAY_ACCESS);
  112. if (err)
  113. return err;
  114. userns = sb->s_user_ns;
  115. /*
  116. * Enforce that creators of BPF tokens are in the same user
  117. * namespace as the BPF FS instance. This makes reasoning about
  118. * permissions a lot easier and we can always relax this later.
  119. */
  120. if (current_user_ns() != userns)
  121. return -EPERM;
  122. if (!ns_capable(userns, CAP_BPF))
  123. return -EPERM;
  124. /* Creating BPF token in init_user_ns doesn't make much sense. */
  125. if (current_user_ns() == &init_user_ns)
  126. return -EOPNOTSUPP;
  127. mnt_opts = sb->s_fs_info;
  128. if (mnt_opts->delegate_cmds == 0 &&
  129. mnt_opts->delegate_maps == 0 &&
  130. mnt_opts->delegate_progs == 0 &&
  131. mnt_opts->delegate_attachs == 0)
  132. return -ENOENT; /* no BPF token delegation is set up */
  133. mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
  134. inode = bpf_get_inode(sb, NULL, mode);
  135. if (IS_ERR(inode))
  136. return PTR_ERR(inode);
  137. inode->i_op = &bpf_token_iops;
  138. inode->i_fop = &bpf_token_fops;
  139. clear_nlink(inode); /* make sure it is unlinked */
  140. file = alloc_file_pseudo(inode, path.mnt, BPF_TOKEN_INODE_NAME, O_RDWR, &bpf_token_fops);
  141. if (IS_ERR(file)) {
  142. iput(inode);
  143. return PTR_ERR(file);
  144. }
  145. token = kzalloc(sizeof(*token), GFP_USER);
  146. if (!token) {
  147. err = -ENOMEM;
  148. goto out_file;
  149. }
  150. atomic64_set(&token->refcnt, 1);
  151. /* remember bpffs owning userns for future ns_capable() checks */
  152. token->userns = get_user_ns(userns);
  153. token->allowed_cmds = mnt_opts->delegate_cmds;
  154. token->allowed_maps = mnt_opts->delegate_maps;
  155. token->allowed_progs = mnt_opts->delegate_progs;
  156. token->allowed_attachs = mnt_opts->delegate_attachs;
  157. err = security_bpf_token_create(token, attr, &path);
  158. if (err)
  159. goto out_token;
  160. fd = get_unused_fd_flags(O_CLOEXEC);
  161. if (fd < 0) {
  162. err = fd;
  163. goto out_token;
  164. }
  165. file->private_data = token;
  166. fd_install(fd, file);
  167. return fd;
  168. out_token:
  169. bpf_token_free(token);
  170. out_file:
  171. fput(file);
  172. return err;
  173. }
  174. struct bpf_token *bpf_token_get_from_fd(u32 ufd)
  175. {
  176. CLASS(fd, f)(ufd);
  177. struct bpf_token *token;
  178. if (fd_empty(f))
  179. return ERR_PTR(-EBADF);
  180. if (fd_file(f)->f_op != &bpf_token_fops)
  181. return ERR_PTR(-EINVAL);
  182. token = fd_file(f)->private_data;
  183. bpf_token_inc(token);
  184. return token;
  185. }
  186. bool bpf_token_allow_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
  187. {
  188. if (!token)
  189. return false;
  190. if (!(token->allowed_cmds & BIT_ULL(cmd)))
  191. return false;
  192. return security_bpf_token_cmd(token, cmd) == 0;
  193. }
  194. bool bpf_token_allow_map_type(const struct bpf_token *token, enum bpf_map_type type)
  195. {
  196. if (!token || type >= __MAX_BPF_MAP_TYPE)
  197. return false;
  198. return token->allowed_maps & BIT_ULL(type);
  199. }
  200. bool bpf_token_allow_prog_type(const struct bpf_token *token,
  201. enum bpf_prog_type prog_type,
  202. enum bpf_attach_type attach_type)
  203. {
  204. if (!token || prog_type >= __MAX_BPF_PROG_TYPE || attach_type >= __MAX_BPF_ATTACH_TYPE)
  205. return false;
  206. return (token->allowed_progs & BIT_ULL(prog_type)) &&
  207. (token->allowed_attachs & BIT_ULL(attach_type));
  208. }