cgroup.c 19 KB

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