helpers.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. */
  12. #include <linux/bpf.h>
  13. #include <linux/rcupdate.h>
  14. #include <linux/random.h>
  15. #include <linux/smp.h>
  16. #include <linux/topology.h>
  17. #include <linux/ktime.h>
  18. #include <linux/sched.h>
  19. #include <linux/uidgid.h>
  20. #include <linux/filter.h>
  21. /* If kernel subsystem is allowing eBPF programs to call this function,
  22. * inside its own verifier_ops->get_func_proto() callback it should return
  23. * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
  24. *
  25. * Different map implementations will rely on rcu in map methods
  26. * lookup/update/delete, therefore eBPF programs must run under rcu lock
  27. * if program is allowed to access maps, so check rcu_read_lock_held in
  28. * all three functions.
  29. */
  30. BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
  31. {
  32. WARN_ON_ONCE(!rcu_read_lock_held());
  33. return (unsigned long) map->ops->map_lookup_elem(map, key);
  34. }
  35. const struct bpf_func_proto bpf_map_lookup_elem_proto = {
  36. .func = bpf_map_lookup_elem,
  37. .gpl_only = false,
  38. .pkt_access = true,
  39. .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  40. .arg1_type = ARG_CONST_MAP_PTR,
  41. .arg2_type = ARG_PTR_TO_MAP_KEY,
  42. };
  43. BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
  44. void *, value, u64, flags)
  45. {
  46. WARN_ON_ONCE(!rcu_read_lock_held());
  47. return map->ops->map_update_elem(map, key, value, flags);
  48. }
  49. const struct bpf_func_proto bpf_map_update_elem_proto = {
  50. .func = bpf_map_update_elem,
  51. .gpl_only = false,
  52. .pkt_access = true,
  53. .ret_type = RET_INTEGER,
  54. .arg1_type = ARG_CONST_MAP_PTR,
  55. .arg2_type = ARG_PTR_TO_MAP_KEY,
  56. .arg3_type = ARG_PTR_TO_MAP_VALUE,
  57. .arg4_type = ARG_ANYTHING,
  58. };
  59. BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
  60. {
  61. WARN_ON_ONCE(!rcu_read_lock_held());
  62. return map->ops->map_delete_elem(map, key);
  63. }
  64. const struct bpf_func_proto bpf_map_delete_elem_proto = {
  65. .func = bpf_map_delete_elem,
  66. .gpl_only = false,
  67. .pkt_access = true,
  68. .ret_type = RET_INTEGER,
  69. .arg1_type = ARG_CONST_MAP_PTR,
  70. .arg2_type = ARG_PTR_TO_MAP_KEY,
  71. };
  72. BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags)
  73. {
  74. return map->ops->map_push_elem(map, value, flags);
  75. }
  76. const struct bpf_func_proto bpf_map_push_elem_proto = {
  77. .func = bpf_map_push_elem,
  78. .gpl_only = false,
  79. .pkt_access = true,
  80. .ret_type = RET_INTEGER,
  81. .arg1_type = ARG_CONST_MAP_PTR,
  82. .arg2_type = ARG_PTR_TO_MAP_VALUE,
  83. .arg3_type = ARG_ANYTHING,
  84. };
  85. BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value)
  86. {
  87. return map->ops->map_pop_elem(map, value);
  88. }
  89. const struct bpf_func_proto bpf_map_pop_elem_proto = {
  90. .func = bpf_map_pop_elem,
  91. .gpl_only = false,
  92. .pkt_access = true,
  93. .ret_type = RET_INTEGER,
  94. .arg1_type = ARG_CONST_MAP_PTR,
  95. .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
  96. };
  97. BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value)
  98. {
  99. return map->ops->map_peek_elem(map, value);
  100. }
  101. const struct bpf_func_proto bpf_map_peek_elem_proto = {
  102. .func = bpf_map_pop_elem,
  103. .gpl_only = false,
  104. .pkt_access = true,
  105. .ret_type = RET_INTEGER,
  106. .arg1_type = ARG_CONST_MAP_PTR,
  107. .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
  108. };
  109. const struct bpf_func_proto bpf_get_prandom_u32_proto = {
  110. .func = bpf_user_rnd_u32,
  111. .gpl_only = false,
  112. .ret_type = RET_INTEGER,
  113. };
  114. BPF_CALL_0(bpf_get_smp_processor_id)
  115. {
  116. return smp_processor_id();
  117. }
  118. const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
  119. .func = bpf_get_smp_processor_id,
  120. .gpl_only = false,
  121. .ret_type = RET_INTEGER,
  122. };
  123. BPF_CALL_0(bpf_get_numa_node_id)
  124. {
  125. return numa_node_id();
  126. }
  127. const struct bpf_func_proto bpf_get_numa_node_id_proto = {
  128. .func = bpf_get_numa_node_id,
  129. .gpl_only = false,
  130. .ret_type = RET_INTEGER,
  131. };
  132. BPF_CALL_0(bpf_ktime_get_ns)
  133. {
  134. /* NMI safe access to clock monotonic */
  135. return ktime_get_mono_fast_ns();
  136. }
  137. const struct bpf_func_proto bpf_ktime_get_ns_proto = {
  138. .func = bpf_ktime_get_ns,
  139. .gpl_only = true,
  140. .ret_type = RET_INTEGER,
  141. };
  142. BPF_CALL_0(bpf_get_current_pid_tgid)
  143. {
  144. struct task_struct *task = current;
  145. if (unlikely(!task))
  146. return -EINVAL;
  147. return (u64) task->tgid << 32 | task->pid;
  148. }
  149. const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
  150. .func = bpf_get_current_pid_tgid,
  151. .gpl_only = false,
  152. .ret_type = RET_INTEGER,
  153. };
  154. BPF_CALL_0(bpf_get_current_uid_gid)
  155. {
  156. struct task_struct *task = current;
  157. kuid_t uid;
  158. kgid_t gid;
  159. if (unlikely(!task))
  160. return -EINVAL;
  161. current_uid_gid(&uid, &gid);
  162. return (u64) from_kgid(&init_user_ns, gid) << 32 |
  163. from_kuid(&init_user_ns, uid);
  164. }
  165. const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
  166. .func = bpf_get_current_uid_gid,
  167. .gpl_only = false,
  168. .ret_type = RET_INTEGER,
  169. };
  170. BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
  171. {
  172. struct task_struct *task = current;
  173. if (unlikely(!task))
  174. goto err_clear;
  175. strncpy(buf, task->comm, size);
  176. /* Verifier guarantees that size > 0. For task->comm exceeding
  177. * size, guarantee that buf is %NUL-terminated. Unconditionally
  178. * done here to save the size test.
  179. */
  180. buf[size - 1] = 0;
  181. return 0;
  182. err_clear:
  183. memset(buf, 0, size);
  184. return -EINVAL;
  185. }
  186. const struct bpf_func_proto bpf_get_current_comm_proto = {
  187. .func = bpf_get_current_comm,
  188. .gpl_only = false,
  189. .ret_type = RET_INTEGER,
  190. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  191. .arg2_type = ARG_CONST_SIZE,
  192. };
  193. #ifdef CONFIG_CGROUPS
  194. BPF_CALL_0(bpf_get_current_cgroup_id)
  195. {
  196. struct cgroup *cgrp = task_dfl_cgroup(current);
  197. return cgrp->kn->id.id;
  198. }
  199. const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
  200. .func = bpf_get_current_cgroup_id,
  201. .gpl_only = false,
  202. .ret_type = RET_INTEGER,
  203. };
  204. #ifdef CONFIG_CGROUP_BPF
  205. DECLARE_PER_CPU(struct bpf_cgroup_storage*,
  206. bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
  207. BPF_CALL_2(bpf_get_local_storage, struct bpf_map *, map, u64, flags)
  208. {
  209. /* flags argument is not used now,
  210. * but provides an ability to extend the API.
  211. * verifier checks that its value is correct.
  212. */
  213. enum bpf_cgroup_storage_type stype = cgroup_storage_type(map);
  214. struct bpf_cgroup_storage *storage;
  215. void *ptr;
  216. storage = this_cpu_read(bpf_cgroup_storage[stype]);
  217. if (stype == BPF_CGROUP_STORAGE_SHARED)
  218. ptr = &READ_ONCE(storage->buf)->data[0];
  219. else
  220. ptr = this_cpu_ptr(storage->percpu_buf);
  221. return (unsigned long)ptr;
  222. }
  223. const struct bpf_func_proto bpf_get_local_storage_proto = {
  224. .func = bpf_get_local_storage,
  225. .gpl_only = false,
  226. .ret_type = RET_PTR_TO_MAP_VALUE,
  227. .arg1_type = ARG_CONST_MAP_PTR,
  228. .arg2_type = ARG_ANYTHING,
  229. };
  230. #endif
  231. #endif