helpers.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. const struct bpf_func_proto bpf_get_prandom_u32_proto = {
  73. .func = bpf_user_rnd_u32,
  74. .gpl_only = false,
  75. .ret_type = RET_INTEGER,
  76. };
  77. BPF_CALL_0(bpf_get_smp_processor_id)
  78. {
  79. return smp_processor_id();
  80. }
  81. const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
  82. .func = bpf_get_smp_processor_id,
  83. .gpl_only = false,
  84. .ret_type = RET_INTEGER,
  85. };
  86. BPF_CALL_0(bpf_get_numa_node_id)
  87. {
  88. return numa_node_id();
  89. }
  90. const struct bpf_func_proto bpf_get_numa_node_id_proto = {
  91. .func = bpf_get_numa_node_id,
  92. .gpl_only = false,
  93. .ret_type = RET_INTEGER,
  94. };
  95. BPF_CALL_0(bpf_ktime_get_ns)
  96. {
  97. /* NMI safe access to clock monotonic */
  98. return ktime_get_mono_fast_ns();
  99. }
  100. const struct bpf_func_proto bpf_ktime_get_ns_proto = {
  101. .func = bpf_ktime_get_ns,
  102. .gpl_only = true,
  103. .ret_type = RET_INTEGER,
  104. };
  105. BPF_CALL_0(bpf_get_current_pid_tgid)
  106. {
  107. struct task_struct *task = current;
  108. if (unlikely(!task))
  109. return -EINVAL;
  110. return (u64) task->tgid << 32 | task->pid;
  111. }
  112. const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
  113. .func = bpf_get_current_pid_tgid,
  114. .gpl_only = false,
  115. .ret_type = RET_INTEGER,
  116. };
  117. BPF_CALL_0(bpf_get_current_uid_gid)
  118. {
  119. struct task_struct *task = current;
  120. kuid_t uid;
  121. kgid_t gid;
  122. if (unlikely(!task))
  123. return -EINVAL;
  124. current_uid_gid(&uid, &gid);
  125. return (u64) from_kgid(&init_user_ns, gid) << 32 |
  126. from_kuid(&init_user_ns, uid);
  127. }
  128. const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
  129. .func = bpf_get_current_uid_gid,
  130. .gpl_only = false,
  131. .ret_type = RET_INTEGER,
  132. };
  133. BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
  134. {
  135. struct task_struct *task = current;
  136. if (unlikely(!task))
  137. goto err_clear;
  138. strncpy(buf, task->comm, size);
  139. /* Verifier guarantees that size > 0. For task->comm exceeding
  140. * size, guarantee that buf is %NUL-terminated. Unconditionally
  141. * done here to save the size test.
  142. */
  143. buf[size - 1] = 0;
  144. return 0;
  145. err_clear:
  146. memset(buf, 0, size);
  147. return -EINVAL;
  148. }
  149. const struct bpf_func_proto bpf_get_current_comm_proto = {
  150. .func = bpf_get_current_comm,
  151. .gpl_only = false,
  152. .ret_type = RET_INTEGER,
  153. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  154. .arg2_type = ARG_CONST_SIZE,
  155. };
  156. #ifdef CONFIG_CGROUPS
  157. BPF_CALL_0(bpf_get_current_cgroup_id)
  158. {
  159. struct cgroup *cgrp = task_dfl_cgroup(current);
  160. return cgrp->kn->id.id;
  161. }
  162. const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
  163. .func = bpf_get_current_cgroup_id,
  164. .gpl_only = false,
  165. .ret_type = RET_INTEGER,
  166. };
  167. DECLARE_PER_CPU(void*, bpf_cgroup_storage);
  168. BPF_CALL_2(bpf_get_local_storage, struct bpf_map *, map, u64, flags)
  169. {
  170. /* map and flags arguments are not used now,
  171. * but provide an ability to extend the API
  172. * for other types of local storages.
  173. * verifier checks that their values are correct.
  174. */
  175. return (unsigned long) this_cpu_read(bpf_cgroup_storage);
  176. }
  177. const struct bpf_func_proto bpf_get_local_storage_proto = {
  178. .func = bpf_get_local_storage,
  179. .gpl_only = false,
  180. .ret_type = RET_PTR_TO_MAP_VALUE,
  181. .arg1_type = ARG_CONST_MAP_PTR,
  182. .arg2_type = ARG_ANYTHING,
  183. };
  184. #endif