helpers.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/ktime.h>
  17. #include <linux/sched.h>
  18. #include <linux/uidgid.h>
  19. #include <linux/filter.h>
  20. /* If kernel subsystem is allowing eBPF programs to call this function,
  21. * inside its own verifier_ops->get_func_proto() callback it should return
  22. * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
  23. *
  24. * Different map implementations will rely on rcu in map methods
  25. * lookup/update/delete, therefore eBPF programs must run under rcu lock
  26. * if program is allowed to access maps, so check rcu_read_lock_held in
  27. * all three functions.
  28. */
  29. BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
  30. {
  31. WARN_ON_ONCE(!rcu_read_lock_held());
  32. return (unsigned long) map->ops->map_lookup_elem(map, key);
  33. }
  34. const struct bpf_func_proto bpf_map_lookup_elem_proto = {
  35. .func = bpf_map_lookup_elem,
  36. .gpl_only = false,
  37. .pkt_access = true,
  38. .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  39. .arg1_type = ARG_CONST_MAP_PTR,
  40. .arg2_type = ARG_PTR_TO_MAP_KEY,
  41. };
  42. BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
  43. void *, value, u64, flags)
  44. {
  45. WARN_ON_ONCE(!rcu_read_lock_held());
  46. return map->ops->map_update_elem(map, key, value, flags);
  47. }
  48. const struct bpf_func_proto bpf_map_update_elem_proto = {
  49. .func = bpf_map_update_elem,
  50. .gpl_only = false,
  51. .pkt_access = true,
  52. .ret_type = RET_INTEGER,
  53. .arg1_type = ARG_CONST_MAP_PTR,
  54. .arg2_type = ARG_PTR_TO_MAP_KEY,
  55. .arg3_type = ARG_PTR_TO_MAP_VALUE,
  56. .arg4_type = ARG_ANYTHING,
  57. };
  58. BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
  59. {
  60. WARN_ON_ONCE(!rcu_read_lock_held());
  61. return map->ops->map_delete_elem(map, key);
  62. }
  63. const struct bpf_func_proto bpf_map_delete_elem_proto = {
  64. .func = bpf_map_delete_elem,
  65. .gpl_only = false,
  66. .pkt_access = true,
  67. .ret_type = RET_INTEGER,
  68. .arg1_type = ARG_CONST_MAP_PTR,
  69. .arg2_type = ARG_PTR_TO_MAP_KEY,
  70. };
  71. const struct bpf_func_proto bpf_get_prandom_u32_proto = {
  72. .func = bpf_user_rnd_u32,
  73. .gpl_only = false,
  74. .ret_type = RET_INTEGER,
  75. };
  76. BPF_CALL_0(bpf_get_smp_processor_id)
  77. {
  78. return smp_processor_id();
  79. }
  80. const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
  81. .func = bpf_get_smp_processor_id,
  82. .gpl_only = false,
  83. .ret_type = RET_INTEGER,
  84. };
  85. BPF_CALL_0(bpf_ktime_get_ns)
  86. {
  87. /* NMI safe access to clock monotonic */
  88. return ktime_get_mono_fast_ns();
  89. }
  90. const struct bpf_func_proto bpf_ktime_get_ns_proto = {
  91. .func = bpf_ktime_get_ns,
  92. .gpl_only = true,
  93. .ret_type = RET_INTEGER,
  94. };
  95. BPF_CALL_0(bpf_get_current_pid_tgid)
  96. {
  97. struct task_struct *task = current;
  98. if (unlikely(!task))
  99. return -EINVAL;
  100. return (u64) task->tgid << 32 | task->pid;
  101. }
  102. const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
  103. .func = bpf_get_current_pid_tgid,
  104. .gpl_only = false,
  105. .ret_type = RET_INTEGER,
  106. };
  107. BPF_CALL_0(bpf_get_current_uid_gid)
  108. {
  109. struct task_struct *task = current;
  110. kuid_t uid;
  111. kgid_t gid;
  112. if (unlikely(!task))
  113. return -EINVAL;
  114. current_uid_gid(&uid, &gid);
  115. return (u64) from_kgid(&init_user_ns, gid) << 32 |
  116. from_kuid(&init_user_ns, uid);
  117. }
  118. const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
  119. .func = bpf_get_current_uid_gid,
  120. .gpl_only = false,
  121. .ret_type = RET_INTEGER,
  122. };
  123. BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
  124. {
  125. struct task_struct *task = current;
  126. if (unlikely(!task))
  127. goto err_clear;
  128. strncpy(buf, task->comm, size);
  129. /* Verifier guarantees that size > 0. For task->comm exceeding
  130. * size, guarantee that buf is %NUL-terminated. Unconditionally
  131. * done here to save the size test.
  132. */
  133. buf[size - 1] = 0;
  134. return 0;
  135. err_clear:
  136. memset(buf, 0, size);
  137. return -EINVAL;
  138. }
  139. const struct bpf_func_proto bpf_get_current_comm_proto = {
  140. .func = bpf_get_current_comm,
  141. .gpl_only = false,
  142. .ret_type = RET_INTEGER,
  143. .arg1_type = ARG_PTR_TO_RAW_STACK,
  144. .arg2_type = ARG_CONST_STACK_SIZE,
  145. };