helpers.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  38. .arg1_type = ARG_CONST_MAP_PTR,
  39. .arg2_type = ARG_PTR_TO_MAP_KEY,
  40. };
  41. BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
  42. void *, value, u64, flags)
  43. {
  44. WARN_ON_ONCE(!rcu_read_lock_held());
  45. return map->ops->map_update_elem(map, key, value, flags);
  46. }
  47. const struct bpf_func_proto bpf_map_update_elem_proto = {
  48. .func = bpf_map_update_elem,
  49. .gpl_only = false,
  50. .ret_type = RET_INTEGER,
  51. .arg1_type = ARG_CONST_MAP_PTR,
  52. .arg2_type = ARG_PTR_TO_MAP_KEY,
  53. .arg3_type = ARG_PTR_TO_MAP_VALUE,
  54. .arg4_type = ARG_ANYTHING,
  55. };
  56. BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
  57. {
  58. WARN_ON_ONCE(!rcu_read_lock_held());
  59. return map->ops->map_delete_elem(map, key);
  60. }
  61. const struct bpf_func_proto bpf_map_delete_elem_proto = {
  62. .func = bpf_map_delete_elem,
  63. .gpl_only = false,
  64. .ret_type = RET_INTEGER,
  65. .arg1_type = ARG_CONST_MAP_PTR,
  66. .arg2_type = ARG_PTR_TO_MAP_KEY,
  67. };
  68. const struct bpf_func_proto bpf_get_prandom_u32_proto = {
  69. .func = bpf_user_rnd_u32,
  70. .gpl_only = false,
  71. .ret_type = RET_INTEGER,
  72. };
  73. BPF_CALL_0(bpf_get_smp_processor_id)
  74. {
  75. return smp_processor_id();
  76. }
  77. const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
  78. .func = bpf_get_smp_processor_id,
  79. .gpl_only = false,
  80. .ret_type = RET_INTEGER,
  81. };
  82. BPF_CALL_0(bpf_ktime_get_ns)
  83. {
  84. /* NMI safe access to clock monotonic */
  85. return ktime_get_mono_fast_ns();
  86. }
  87. const struct bpf_func_proto bpf_ktime_get_ns_proto = {
  88. .func = bpf_ktime_get_ns,
  89. .gpl_only = true,
  90. .ret_type = RET_INTEGER,
  91. };
  92. BPF_CALL_0(bpf_get_current_pid_tgid)
  93. {
  94. struct task_struct *task = current;
  95. if (unlikely(!task))
  96. return -EINVAL;
  97. return (u64) task->tgid << 32 | task->pid;
  98. }
  99. const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
  100. .func = bpf_get_current_pid_tgid,
  101. .gpl_only = false,
  102. .ret_type = RET_INTEGER,
  103. };
  104. BPF_CALL_0(bpf_get_current_uid_gid)
  105. {
  106. struct task_struct *task = current;
  107. kuid_t uid;
  108. kgid_t gid;
  109. if (unlikely(!task))
  110. return -EINVAL;
  111. current_uid_gid(&uid, &gid);
  112. return (u64) from_kgid(&init_user_ns, gid) << 32 |
  113. from_kuid(&init_user_ns, uid);
  114. }
  115. const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
  116. .func = bpf_get_current_uid_gid,
  117. .gpl_only = false,
  118. .ret_type = RET_INTEGER,
  119. };
  120. BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
  121. {
  122. struct task_struct *task = current;
  123. if (unlikely(!task))
  124. goto err_clear;
  125. strncpy(buf, task->comm, size);
  126. /* Verifier guarantees that size > 0. For task->comm exceeding
  127. * size, guarantee that buf is %NUL-terminated. Unconditionally
  128. * done here to save the size test.
  129. */
  130. buf[size - 1] = 0;
  131. return 0;
  132. err_clear:
  133. memset(buf, 0, size);
  134. return -EINVAL;
  135. }
  136. const struct bpf_func_proto bpf_get_current_comm_proto = {
  137. .func = bpf_get_current_comm,
  138. .gpl_only = false,
  139. .ret_type = RET_INTEGER,
  140. .arg1_type = ARG_PTR_TO_RAW_STACK,
  141. .arg2_type = ARG_CONST_STACK_SIZE,
  142. };