cgroup.c 20 KB

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