helpers.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 = false,
  103. .ret_type = RET_INTEGER,
  104. };
  105. BPF_CALL_0(bpf_ktime_get_boot_ns)
  106. {
  107. /* NMI safe access to clock boottime */
  108. return ktime_get_boot_fast_ns();
  109. }
  110. const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = {
  111. .func = bpf_ktime_get_boot_ns,
  112. .gpl_only = false,
  113. .ret_type = RET_INTEGER,
  114. };
  115. BPF_CALL_0(bpf_get_current_pid_tgid)
  116. {
  117. struct task_struct *task = current;
  118. if (unlikely(!task))
  119. return -EINVAL;
  120. return (u64) task->tgid << 32 | task->pid;
  121. }
  122. const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
  123. .func = bpf_get_current_pid_tgid,
  124. .gpl_only = false,
  125. .ret_type = RET_INTEGER,
  126. };
  127. BPF_CALL_0(bpf_get_current_uid_gid)
  128. {
  129. struct task_struct *task = current;
  130. kuid_t uid;
  131. kgid_t gid;
  132. if (unlikely(!task))
  133. return -EINVAL;
  134. current_uid_gid(&uid, &gid);
  135. return (u64) from_kgid(&init_user_ns, gid) << 32 |
  136. from_kuid(&init_user_ns, uid);
  137. }
  138. const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
  139. .func = bpf_get_current_uid_gid,
  140. .gpl_only = false,
  141. .ret_type = RET_INTEGER,
  142. };
  143. BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
  144. {
  145. struct task_struct *task = current;
  146. if (unlikely(!task))
  147. goto err_clear;
  148. strncpy(buf, task->comm, size);
  149. /* Verifier guarantees that size > 0. For task->comm exceeding
  150. * size, guarantee that buf is %NUL-terminated. Unconditionally
  151. * done here to save the size test.
  152. */
  153. buf[size - 1] = 0;
  154. return 0;
  155. err_clear:
  156. memset(buf, 0, size);
  157. return -EINVAL;
  158. }
  159. const struct bpf_func_proto bpf_get_current_comm_proto = {
  160. .func = bpf_get_current_comm,
  161. .gpl_only = false,
  162. .ret_type = RET_INTEGER,
  163. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  164. .arg2_type = ARG_CONST_SIZE,
  165. };