cgroup.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * Functions to manage eBPF programs attached to cgroups
  3. *
  4. * Copyright (c) 2016 Daniel Mack
  5. *
  6. * This file is subject to the terms and conditions of version 2 of the GNU
  7. * General Public License. See the file COPYING in the main directory of the
  8. * Linux distribution for more details.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/atomic.h>
  12. #include <linux/cgroup.h>
  13. #include <linux/slab.h>
  14. #include <linux/bpf.h>
  15. #include <linux/bpf-cgroup.h>
  16. #include <net/sock.h>
  17. DEFINE_STATIC_KEY_FALSE(cgroup_bpf_enabled_key);
  18. EXPORT_SYMBOL(cgroup_bpf_enabled_key);
  19. /**
  20. * cgroup_bpf_put() - put references of all bpf programs
  21. * @cgrp: the cgroup to modify
  22. */
  23. void cgroup_bpf_put(struct cgroup *cgrp)
  24. {
  25. unsigned int type;
  26. for (type = 0; type < ARRAY_SIZE(cgrp->bpf.progs); type++) {
  27. struct list_head *progs = &cgrp->bpf.progs[type];
  28. struct bpf_prog_list *pl, *tmp;
  29. list_for_each_entry_safe(pl, tmp, progs, node) {
  30. list_del(&pl->node);
  31. bpf_prog_put(pl->prog);
  32. kfree(pl);
  33. static_branch_dec(&cgroup_bpf_enabled_key);
  34. }
  35. bpf_prog_array_free(cgrp->bpf.effective[type]);
  36. }
  37. }
  38. /* count number of elements in the list.
  39. * it's slow but the list cannot be long
  40. */
  41. static u32 prog_list_length(struct list_head *head)
  42. {
  43. struct bpf_prog_list *pl;
  44. u32 cnt = 0;
  45. list_for_each_entry(pl, head, node) {
  46. if (!pl->prog)
  47. continue;
  48. cnt++;
  49. }
  50. return cnt;
  51. }
  52. /* if parent has non-overridable prog attached,
  53. * disallow attaching new programs to the descendent cgroup.
  54. * if parent has overridable or multi-prog, allow attaching
  55. */
  56. static bool hierarchy_allows_attach(struct cgroup *cgrp,
  57. enum bpf_attach_type type,
  58. u32 new_flags)
  59. {
  60. struct cgroup *p;
  61. p = cgroup_parent(cgrp);
  62. if (!p)
  63. return true;
  64. do {
  65. u32 flags = p->bpf.flags[type];
  66. u32 cnt;
  67. if (flags & BPF_F_ALLOW_MULTI)
  68. return true;
  69. cnt = prog_list_length(&p->bpf.progs[type]);
  70. WARN_ON_ONCE(cnt > 1);
  71. if (cnt == 1)
  72. return !!(flags & BPF_F_ALLOW_OVERRIDE);
  73. p = cgroup_parent(p);
  74. } while (p);
  75. return true;
  76. }
  77. /* compute a chain of effective programs for a given cgroup:
  78. * start from the list of programs in this cgroup and add
  79. * all parent programs.
  80. * Note that parent's F_ALLOW_OVERRIDE-type program is yielding
  81. * to programs in this cgroup
  82. */
  83. static int compute_effective_progs(struct cgroup *cgrp,
  84. enum bpf_attach_type type,
  85. struct bpf_prog_array __rcu **array)
  86. {
  87. struct bpf_prog_array __rcu *progs;
  88. struct bpf_prog_list *pl;
  89. struct cgroup *p = cgrp;
  90. int cnt = 0;
  91. /* count number of effective programs by walking parents */
  92. do {
  93. if (cnt == 0 || (p->bpf.flags[type] & BPF_F_ALLOW_MULTI))
  94. cnt += prog_list_length(&p->bpf.progs[type]);
  95. p = cgroup_parent(p);
  96. } while (p);
  97. progs = bpf_prog_array_alloc(cnt, GFP_KERNEL);
  98. if (!progs)
  99. return -ENOMEM;
  100. /* populate the array with effective progs */
  101. cnt = 0;
  102. p = cgrp;
  103. do {
  104. if (cnt == 0 || (p->bpf.flags[type] & BPF_F_ALLOW_MULTI))
  105. list_for_each_entry(pl,
  106. &p->bpf.progs[type], node) {
  107. if (!pl->prog)
  108. continue;
  109. rcu_dereference_protected(progs, 1)->
  110. progs[cnt++] = pl->prog;
  111. }
  112. p = cgroup_parent(p);
  113. } while (p);
  114. *array = progs;
  115. return 0;
  116. }
  117. static void activate_effective_progs(struct cgroup *cgrp,
  118. enum bpf_attach_type type,
  119. struct bpf_prog_array __rcu *array)
  120. {
  121. struct bpf_prog_array __rcu *old_array;
  122. old_array = xchg(&cgrp->bpf.effective[type], array);
  123. /* free prog array after grace period, since __cgroup_bpf_run_*()
  124. * might be still walking the array
  125. */
  126. bpf_prog_array_free(old_array);
  127. }
  128. /**
  129. * cgroup_bpf_inherit() - inherit effective programs from parent
  130. * @cgrp: the cgroup to modify
  131. */
  132. int cgroup_bpf_inherit(struct cgroup *cgrp)
  133. {
  134. /* has to use marco instead of const int, since compiler thinks
  135. * that array below is variable length
  136. */
  137. #define NR ARRAY_SIZE(cgrp->bpf.effective)
  138. struct bpf_prog_array __rcu *arrays[NR] = {};
  139. int i;
  140. for (i = 0; i < NR; i++)
  141. INIT_LIST_HEAD(&cgrp->bpf.progs[i]);
  142. for (i = 0; i < NR; i++)
  143. if (compute_effective_progs(cgrp, i, &arrays[i]))
  144. goto cleanup;
  145. for (i = 0; i < NR; i++)
  146. activate_effective_progs(cgrp, i, arrays[i]);
  147. return 0;
  148. cleanup:
  149. for (i = 0; i < NR; i++)
  150. bpf_prog_array_free(arrays[i]);
  151. return -ENOMEM;
  152. }
  153. #define BPF_CGROUP_MAX_PROGS 64
  154. /**
  155. * __cgroup_bpf_attach() - Attach the program to a cgroup, and
  156. * propagate the change to descendants
  157. * @cgrp: The cgroup which descendants to traverse
  158. * @prog: A program to attach
  159. * @type: Type of attach operation
  160. *
  161. * Must be called with cgroup_mutex held.
  162. */
  163. int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
  164. enum bpf_attach_type type, u32 flags)
  165. {
  166. struct list_head *progs = &cgrp->bpf.progs[type];
  167. struct bpf_prog *old_prog = NULL;
  168. struct cgroup_subsys_state *css;
  169. struct bpf_prog_list *pl;
  170. bool pl_was_allocated;
  171. int err;
  172. if ((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI))
  173. /* invalid combination */
  174. return -EINVAL;
  175. if (!hierarchy_allows_attach(cgrp, type, flags))
  176. return -EPERM;
  177. if (!list_empty(progs) && cgrp->bpf.flags[type] != flags)
  178. /* Disallow attaching non-overridable on top
  179. * of existing overridable in this cgroup.
  180. * Disallow attaching multi-prog if overridable or none
  181. */
  182. return -EPERM;
  183. if (prog_list_length(progs) >= BPF_CGROUP_MAX_PROGS)
  184. return -E2BIG;
  185. if (flags & BPF_F_ALLOW_MULTI) {
  186. list_for_each_entry(pl, progs, node)
  187. if (pl->prog == prog)
  188. /* disallow attaching the same prog twice */
  189. return -EINVAL;
  190. pl = kmalloc(sizeof(*pl), GFP_KERNEL);
  191. if (!pl)
  192. return -ENOMEM;
  193. pl_was_allocated = true;
  194. pl->prog = prog;
  195. list_add_tail(&pl->node, progs);
  196. } else {
  197. if (list_empty(progs)) {
  198. pl = kmalloc(sizeof(*pl), GFP_KERNEL);
  199. if (!pl)
  200. return -ENOMEM;
  201. pl_was_allocated = true;
  202. list_add_tail(&pl->node, progs);
  203. } else {
  204. pl = list_first_entry(progs, typeof(*pl), node);
  205. old_prog = pl->prog;
  206. pl_was_allocated = false;
  207. }
  208. pl->prog = prog;
  209. }
  210. cgrp->bpf.flags[type] = flags;
  211. /* allocate and recompute effective prog arrays */
  212. css_for_each_descendant_pre(css, &cgrp->self) {
  213. struct cgroup *desc = container_of(css, struct cgroup, self);
  214. err = compute_effective_progs(desc, type, &desc->bpf.inactive);
  215. if (err)
  216. goto cleanup;
  217. }
  218. /* all allocations were successful. Activate all prog arrays */
  219. css_for_each_descendant_pre(css, &cgrp->self) {
  220. struct cgroup *desc = container_of(css, struct cgroup, self);
  221. activate_effective_progs(desc, type, desc->bpf.inactive);
  222. desc->bpf.inactive = NULL;
  223. }
  224. static_branch_inc(&cgroup_bpf_enabled_key);
  225. if (old_prog) {
  226. bpf_prog_put(old_prog);
  227. static_branch_dec(&cgroup_bpf_enabled_key);
  228. }
  229. return 0;
  230. cleanup:
  231. /* oom while computing effective. Free all computed effective arrays
  232. * since they were not activated
  233. */
  234. css_for_each_descendant_pre(css, &cgrp->self) {
  235. struct cgroup *desc = container_of(css, struct cgroup, self);
  236. bpf_prog_array_free(desc->bpf.inactive);
  237. desc->bpf.inactive = NULL;
  238. }
  239. /* and cleanup the prog list */
  240. pl->prog = old_prog;
  241. if (pl_was_allocated) {
  242. list_del(&pl->node);
  243. kfree(pl);
  244. }
  245. return err;
  246. }
  247. /**
  248. * __cgroup_bpf_detach() - Detach the program from a cgroup, and
  249. * propagate the change to descendants
  250. * @cgrp: The cgroup which descendants to traverse
  251. * @prog: A program to detach or NULL
  252. * @type: Type of detach operation
  253. *
  254. * Must be called with cgroup_mutex held.
  255. */
  256. int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
  257. enum bpf_attach_type type, u32 unused_flags)
  258. {
  259. struct list_head *progs = &cgrp->bpf.progs[type];
  260. u32 flags = cgrp->bpf.flags[type];
  261. struct bpf_prog *old_prog = NULL;
  262. struct cgroup_subsys_state *css;
  263. struct bpf_prog_list *pl;
  264. int err;
  265. if (flags & BPF_F_ALLOW_MULTI) {
  266. if (!prog)
  267. /* to detach MULTI prog the user has to specify valid FD
  268. * of the program to be detached
  269. */
  270. return -EINVAL;
  271. } else {
  272. if (list_empty(progs))
  273. /* report error when trying to detach and nothing is attached */
  274. return -ENOENT;
  275. }
  276. if (flags & BPF_F_ALLOW_MULTI) {
  277. /* find the prog and detach it */
  278. list_for_each_entry(pl, progs, node) {
  279. if (pl->prog != prog)
  280. continue;
  281. old_prog = prog;
  282. /* mark it deleted, so it's ignored while
  283. * recomputing effective
  284. */
  285. pl->prog = NULL;
  286. break;
  287. }
  288. if (!old_prog)
  289. return -ENOENT;
  290. } else {
  291. /* to maintain backward compatibility NONE and OVERRIDE cgroups
  292. * allow detaching with invalid FD (prog==NULL)
  293. */
  294. pl = list_first_entry(progs, typeof(*pl), node);
  295. old_prog = pl->prog;
  296. pl->prog = NULL;
  297. }
  298. /* allocate and recompute effective prog arrays */
  299. css_for_each_descendant_pre(css, &cgrp->self) {
  300. struct cgroup *desc = container_of(css, struct cgroup, self);
  301. err = compute_effective_progs(desc, type, &desc->bpf.inactive);
  302. if (err)
  303. goto cleanup;
  304. }
  305. /* all allocations were successful. Activate all prog arrays */
  306. css_for_each_descendant_pre(css, &cgrp->self) {
  307. struct cgroup *desc = container_of(css, struct cgroup, self);
  308. activate_effective_progs(desc, type, desc->bpf.inactive);
  309. desc->bpf.inactive = NULL;
  310. }
  311. /* now can actually delete it from this cgroup list */
  312. list_del(&pl->node);
  313. kfree(pl);
  314. if (list_empty(progs))
  315. /* last program was detached, reset flags to zero */
  316. cgrp->bpf.flags[type] = 0;
  317. bpf_prog_put(old_prog);
  318. static_branch_dec(&cgroup_bpf_enabled_key);
  319. return 0;
  320. cleanup:
  321. /* oom while computing effective. Free all computed effective arrays
  322. * since they were not activated
  323. */
  324. css_for_each_descendant_pre(css, &cgrp->self) {
  325. struct cgroup *desc = container_of(css, struct cgroup, self);
  326. bpf_prog_array_free(desc->bpf.inactive);
  327. desc->bpf.inactive = NULL;
  328. }
  329. /* and restore back old_prog */
  330. pl->prog = old_prog;
  331. return err;
  332. }
  333. /* Must be called with cgroup_mutex held to avoid races. */
  334. int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
  335. union bpf_attr __user *uattr)
  336. {
  337. __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
  338. enum bpf_attach_type type = attr->query.attach_type;
  339. struct list_head *progs = &cgrp->bpf.progs[type];
  340. u32 flags = cgrp->bpf.flags[type];
  341. int cnt, ret = 0, i;
  342. if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE)
  343. cnt = bpf_prog_array_length(cgrp->bpf.effective[type]);
  344. else
  345. cnt = prog_list_length(progs);
  346. if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
  347. return -EFAULT;
  348. if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt)))
  349. return -EFAULT;
  350. if (attr->query.prog_cnt == 0 || !prog_ids || !cnt)
  351. /* return early if user requested only program count + flags */
  352. return 0;
  353. if (attr->query.prog_cnt < cnt) {
  354. cnt = attr->query.prog_cnt;
  355. ret = -ENOSPC;
  356. }
  357. if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
  358. return bpf_prog_array_copy_to_user(cgrp->bpf.effective[type],
  359. prog_ids, cnt);
  360. } else {
  361. struct bpf_prog_list *pl;
  362. u32 id;
  363. i = 0;
  364. list_for_each_entry(pl, progs, node) {
  365. id = pl->prog->aux->id;
  366. if (copy_to_user(prog_ids + i, &id, sizeof(id)))
  367. return -EFAULT;
  368. if (++i == cnt)
  369. break;
  370. }
  371. }
  372. return ret;
  373. }
  374. int cgroup_bpf_prog_attach(const union bpf_attr *attr,
  375. enum bpf_prog_type ptype, struct bpf_prog *prog)
  376. {
  377. struct cgroup *cgrp;
  378. int ret;
  379. cgrp = cgroup_get_from_fd(attr->target_fd);
  380. if (IS_ERR(cgrp))
  381. return PTR_ERR(cgrp);
  382. ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
  383. attr->attach_flags);
  384. cgroup_put(cgrp);
  385. return ret;
  386. }
  387. int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
  388. {
  389. struct bpf_prog *prog;
  390. struct cgroup *cgrp;
  391. int ret;
  392. cgrp = cgroup_get_from_fd(attr->target_fd);
  393. if (IS_ERR(cgrp))
  394. return PTR_ERR(cgrp);
  395. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  396. if (IS_ERR(prog))
  397. prog = NULL;
  398. ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
  399. if (prog)
  400. bpf_prog_put(prog);
  401. cgroup_put(cgrp);
  402. return ret;
  403. }
  404. int cgroup_bpf_prog_query(const union bpf_attr *attr,
  405. union bpf_attr __user *uattr)
  406. {
  407. struct cgroup *cgrp;
  408. int ret;
  409. cgrp = cgroup_get_from_fd(attr->query.target_fd);
  410. if (IS_ERR(cgrp))
  411. return PTR_ERR(cgrp);
  412. ret = cgroup_bpf_query(cgrp, attr, uattr);
  413. cgroup_put(cgrp);
  414. return ret;
  415. }
  416. /**
  417. * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
  418. * @sk: The socket sending or receiving traffic
  419. * @skb: The skb that is being sent or received
  420. * @type: The type of program to be exectuted
  421. *
  422. * If no socket is passed, or the socket is not of type INET or INET6,
  423. * this function does nothing and returns 0.
  424. *
  425. * The program type passed in via @type must be suitable for network
  426. * filtering. No further check is performed to assert that.
  427. *
  428. * This function will return %-EPERM if any if an attached program was found
  429. * and if it returned != 1 during execution. In all other cases, 0 is returned.
  430. */
  431. int __cgroup_bpf_run_filter_skb(struct sock *sk,
  432. struct sk_buff *skb,
  433. enum bpf_attach_type type)
  434. {
  435. unsigned int offset = skb->data - skb_network_header(skb);
  436. struct sock *save_sk;
  437. struct cgroup *cgrp;
  438. int ret;
  439. if (!sk || !sk_fullsock(sk))
  440. return 0;
  441. if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
  442. return 0;
  443. cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  444. save_sk = skb->sk;
  445. skb->sk = sk;
  446. __skb_push(skb, offset);
  447. ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], skb,
  448. bpf_prog_run_save_cb);
  449. __skb_pull(skb, offset);
  450. skb->sk = save_sk;
  451. return ret == 1 ? 0 : -EPERM;
  452. }
  453. EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb);
  454. /**
  455. * __cgroup_bpf_run_filter_sk() - Run a program on a sock
  456. * @sk: sock structure to manipulate
  457. * @type: The type of program to be exectuted
  458. *
  459. * socket is passed is expected to be of type INET or INET6.
  460. *
  461. * The program type passed in via @type must be suitable for sock
  462. * filtering. No further check is performed to assert that.
  463. *
  464. * This function will return %-EPERM if any if an attached program was found
  465. * and if it returned != 1 during execution. In all other cases, 0 is returned.
  466. */
  467. int __cgroup_bpf_run_filter_sk(struct sock *sk,
  468. enum bpf_attach_type type)
  469. {
  470. struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  471. int ret;
  472. ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], sk, BPF_PROG_RUN);
  473. return ret == 1 ? 0 : -EPERM;
  474. }
  475. EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk);
  476. /**
  477. * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
  478. * provided by user sockaddr
  479. * @sk: sock struct that will use sockaddr
  480. * @uaddr: sockaddr struct provided by user
  481. * @type: The type of program to be exectuted
  482. * @t_ctx: Pointer to attach type specific context
  483. *
  484. * socket is expected to be of type INET or INET6.
  485. *
  486. * This function will return %-EPERM if an attached program is found and
  487. * returned value != 1 during execution. In all other cases, 0 is returned.
  488. */
  489. int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
  490. struct sockaddr *uaddr,
  491. enum bpf_attach_type type,
  492. void *t_ctx)
  493. {
  494. struct bpf_sock_addr_kern ctx = {
  495. .sk = sk,
  496. .uaddr = uaddr,
  497. .t_ctx = t_ctx,
  498. };
  499. struct sockaddr_storage unspec;
  500. struct cgroup *cgrp;
  501. int ret;
  502. /* Check socket family since not all sockets represent network
  503. * endpoint (e.g. AF_UNIX).
  504. */
  505. if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
  506. return 0;
  507. if (!ctx.uaddr) {
  508. memset(&unspec, 0, sizeof(unspec));
  509. ctx.uaddr = (struct sockaddr *)&unspec;
  510. }
  511. cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  512. ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx, BPF_PROG_RUN);
  513. return ret == 1 ? 0 : -EPERM;
  514. }
  515. EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr);
  516. /**
  517. * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
  518. * @sk: socket to get cgroup from
  519. * @sock_ops: bpf_sock_ops_kern struct to pass to program. Contains
  520. * sk with connection information (IP addresses, etc.) May not contain
  521. * cgroup info if it is a req sock.
  522. * @type: The type of program to be exectuted
  523. *
  524. * socket passed is expected to be of type INET or INET6.
  525. *
  526. * The program type passed in via @type must be suitable for sock_ops
  527. * filtering. No further check is performed to assert that.
  528. *
  529. * This function will return %-EPERM if any if an attached program was found
  530. * and if it returned != 1 during execution. In all other cases, 0 is returned.
  531. */
  532. int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
  533. struct bpf_sock_ops_kern *sock_ops,
  534. enum bpf_attach_type type)
  535. {
  536. struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  537. int ret;
  538. ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], sock_ops,
  539. BPF_PROG_RUN);
  540. return ret == 1 ? 0 : -EPERM;
  541. }
  542. EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_ops);
  543. int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
  544. short access, enum bpf_attach_type type)
  545. {
  546. struct cgroup *cgrp;
  547. struct bpf_cgroup_dev_ctx ctx = {
  548. .access_type = (access << 16) | dev_type,
  549. .major = major,
  550. .minor = minor,
  551. };
  552. int allow = 1;
  553. rcu_read_lock();
  554. cgrp = task_dfl_cgroup(current);
  555. allow = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx,
  556. BPF_PROG_RUN);
  557. rcu_read_unlock();
  558. return !allow;
  559. }
  560. EXPORT_SYMBOL(__cgroup_bpf_check_dev_permission);
  561. static const struct bpf_func_proto *
  562. cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  563. {
  564. switch (func_id) {
  565. case BPF_FUNC_map_lookup_elem:
  566. return &bpf_map_lookup_elem_proto;
  567. case BPF_FUNC_map_update_elem:
  568. return &bpf_map_update_elem_proto;
  569. case BPF_FUNC_map_delete_elem:
  570. return &bpf_map_delete_elem_proto;
  571. case BPF_FUNC_get_current_uid_gid:
  572. return &bpf_get_current_uid_gid_proto;
  573. case BPF_FUNC_trace_printk:
  574. if (capable(CAP_SYS_ADMIN))
  575. return bpf_get_trace_printk_proto();
  576. default:
  577. return NULL;
  578. }
  579. }
  580. static bool cgroup_dev_is_valid_access(int off, int size,
  581. enum bpf_access_type type,
  582. const struct bpf_prog *prog,
  583. struct bpf_insn_access_aux *info)
  584. {
  585. const int size_default = sizeof(__u32);
  586. if (type == BPF_WRITE)
  587. return false;
  588. if (off < 0 || off + size > sizeof(struct bpf_cgroup_dev_ctx))
  589. return false;
  590. /* The verifier guarantees that size > 0. */
  591. if (off % size != 0)
  592. return false;
  593. switch (off) {
  594. case bpf_ctx_range(struct bpf_cgroup_dev_ctx, access_type):
  595. bpf_ctx_record_field_size(info, size_default);
  596. if (!bpf_ctx_narrow_access_ok(off, size, size_default))
  597. return false;
  598. break;
  599. default:
  600. if (size != size_default)
  601. return false;
  602. }
  603. return true;
  604. }
  605. const struct bpf_prog_ops cg_dev_prog_ops = {
  606. };
  607. const struct bpf_verifier_ops cg_dev_verifier_ops = {
  608. .get_func_proto = cgroup_dev_func_proto,
  609. .is_valid_access = cgroup_dev_is_valid_access,
  610. };