cgroup.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.prog); type++) {
  27. struct bpf_prog *prog = cgrp->bpf.prog[type];
  28. if (prog) {
  29. bpf_prog_put(prog);
  30. static_branch_dec(&cgroup_bpf_enabled_key);
  31. }
  32. }
  33. }
  34. /**
  35. * cgroup_bpf_inherit() - inherit effective programs from parent
  36. * @cgrp: the cgroup to modify
  37. * @parent: the parent to inherit from
  38. */
  39. void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent)
  40. {
  41. unsigned int type;
  42. for (type = 0; type < ARRAY_SIZE(cgrp->bpf.effective); type++) {
  43. struct bpf_prog *e;
  44. e = rcu_dereference_protected(parent->bpf.effective[type],
  45. lockdep_is_held(&cgroup_mutex));
  46. rcu_assign_pointer(cgrp->bpf.effective[type], e);
  47. }
  48. }
  49. /**
  50. * __cgroup_bpf_update() - Update the pinned program of a cgroup, and
  51. * propagate the change to descendants
  52. * @cgrp: The cgroup which descendants to traverse
  53. * @parent: The parent of @cgrp, or %NULL if @cgrp is the root
  54. * @prog: A new program to pin
  55. * @type: Type of pinning operation (ingress/egress)
  56. *
  57. * Each cgroup has a set of two pointers for bpf programs; one for eBPF
  58. * programs it owns, and which is effective for execution.
  59. *
  60. * If @prog is not %NULL, this function attaches a new program to the cgroup
  61. * and releases the one that is currently attached, if any. @prog is then made
  62. * the effective program of type @type in that cgroup.
  63. *
  64. * If @prog is %NULL, the currently attached program of type @type is released,
  65. * and the effective program of the parent cgroup (if any) is inherited to
  66. * @cgrp.
  67. *
  68. * Then, the descendants of @cgrp are walked and the effective program for
  69. * each of them is set to the effective program of @cgrp unless the
  70. * descendant has its own program attached, in which case the subbranch is
  71. * skipped. This ensures that delegated subcgroups with own programs are left
  72. * untouched.
  73. *
  74. * Must be called with cgroup_mutex held.
  75. */
  76. void __cgroup_bpf_update(struct cgroup *cgrp,
  77. struct cgroup *parent,
  78. struct bpf_prog *prog,
  79. enum bpf_attach_type type)
  80. {
  81. struct bpf_prog *old_prog, *effective;
  82. struct cgroup_subsys_state *pos;
  83. old_prog = xchg(cgrp->bpf.prog + type, prog);
  84. effective = (!prog && parent) ?
  85. rcu_dereference_protected(parent->bpf.effective[type],
  86. lockdep_is_held(&cgroup_mutex)) :
  87. prog;
  88. css_for_each_descendant_pre(pos, &cgrp->self) {
  89. struct cgroup *desc = container_of(pos, struct cgroup, self);
  90. /* skip the subtree if the descendant has its own program */
  91. if (desc->bpf.prog[type] && desc != cgrp)
  92. pos = css_rightmost_descendant(pos);
  93. else
  94. rcu_assign_pointer(desc->bpf.effective[type],
  95. effective);
  96. }
  97. if (prog)
  98. static_branch_inc(&cgroup_bpf_enabled_key);
  99. if (old_prog) {
  100. bpf_prog_put(old_prog);
  101. static_branch_dec(&cgroup_bpf_enabled_key);
  102. }
  103. }
  104. /**
  105. * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
  106. * @sk: The socken sending or receiving traffic
  107. * @skb: The skb that is being sent or received
  108. * @type: The type of program to be exectuted
  109. *
  110. * If no socket is passed, or the socket is not of type INET or INET6,
  111. * this function does nothing and returns 0.
  112. *
  113. * The program type passed in via @type must be suitable for network
  114. * filtering. No further check is performed to assert that.
  115. *
  116. * This function will return %-EPERM if any if an attached program was found
  117. * and if it returned != 1 during execution. In all other cases, 0 is returned.
  118. */
  119. int __cgroup_bpf_run_filter_skb(struct sock *sk,
  120. struct sk_buff *skb,
  121. enum bpf_attach_type type)
  122. {
  123. struct bpf_prog *prog;
  124. struct cgroup *cgrp;
  125. int ret = 0;
  126. if (!sk || !sk_fullsock(sk))
  127. return 0;
  128. if (sk->sk_family != AF_INET &&
  129. sk->sk_family != AF_INET6)
  130. return 0;
  131. cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  132. rcu_read_lock();
  133. prog = rcu_dereference(cgrp->bpf.effective[type]);
  134. if (prog) {
  135. unsigned int offset = skb->data - skb_network_header(skb);
  136. __skb_push(skb, offset);
  137. ret = bpf_prog_run_save_cb(prog, skb) == 1 ? 0 : -EPERM;
  138. __skb_pull(skb, offset);
  139. }
  140. rcu_read_unlock();
  141. return ret;
  142. }
  143. EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb);
  144. /**
  145. * __cgroup_bpf_run_filter_sk() - Run a program on a sock
  146. * @sk: sock structure to manipulate
  147. * @type: The type of program to be exectuted
  148. *
  149. * socket is passed is expected to be of type INET or INET6.
  150. *
  151. * The program type passed in via @type must be suitable for sock
  152. * filtering. No further check is performed to assert that.
  153. *
  154. * This function will return %-EPERM if any if an attached program was found
  155. * and if it returned != 1 during execution. In all other cases, 0 is returned.
  156. */
  157. int __cgroup_bpf_run_filter_sk(struct sock *sk,
  158. enum bpf_attach_type type)
  159. {
  160. struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  161. struct bpf_prog *prog;
  162. int ret = 0;
  163. rcu_read_lock();
  164. prog = rcu_dereference(cgrp->bpf.effective[type]);
  165. if (prog)
  166. ret = BPF_PROG_RUN(prog, sk) == 1 ? 0 : -EPERM;
  167. rcu_read_unlock();
  168. return ret;
  169. }
  170. EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk);