helpers.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /* If kernel subsystem is allowing eBPF programs to call this function,
  20. * inside its own verifier_ops->get_func_proto() callback it should return
  21. * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
  22. *
  23. * Different map implementations will rely on rcu in map methods
  24. * lookup/update/delete, therefore eBPF programs must run under rcu lock
  25. * if program is allowed to access maps, so check rcu_read_lock_held in
  26. * all three functions.
  27. */
  28. static u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  29. {
  30. /* verifier checked that R1 contains a valid pointer to bpf_map
  31. * and R2 points to a program stack and map->key_size bytes were
  32. * initialized
  33. */
  34. struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
  35. void *key = (void *) (unsigned long) r2;
  36. void *value;
  37. WARN_ON_ONCE(!rcu_read_lock_held());
  38. value = map->ops->map_lookup_elem(map, key);
  39. /* lookup() returns either pointer to element value or NULL
  40. * which is the meaning of PTR_TO_MAP_VALUE_OR_NULL type
  41. */
  42. return (unsigned long) value;
  43. }
  44. const struct bpf_func_proto bpf_map_lookup_elem_proto = {
  45. .func = bpf_map_lookup_elem,
  46. .gpl_only = false,
  47. .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  48. .arg1_type = ARG_CONST_MAP_PTR,
  49. .arg2_type = ARG_PTR_TO_MAP_KEY,
  50. };
  51. static u64 bpf_map_update_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  52. {
  53. struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
  54. void *key = (void *) (unsigned long) r2;
  55. void *value = (void *) (unsigned long) r3;
  56. WARN_ON_ONCE(!rcu_read_lock_held());
  57. return map->ops->map_update_elem(map, key, value, r4);
  58. }
  59. const struct bpf_func_proto bpf_map_update_elem_proto = {
  60. .func = bpf_map_update_elem,
  61. .gpl_only = false,
  62. .ret_type = RET_INTEGER,
  63. .arg1_type = ARG_CONST_MAP_PTR,
  64. .arg2_type = ARG_PTR_TO_MAP_KEY,
  65. .arg3_type = ARG_PTR_TO_MAP_VALUE,
  66. .arg4_type = ARG_ANYTHING,
  67. };
  68. static u64 bpf_map_delete_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  69. {
  70. struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
  71. void *key = (void *) (unsigned long) r2;
  72. WARN_ON_ONCE(!rcu_read_lock_held());
  73. return map->ops->map_delete_elem(map, key);
  74. }
  75. const struct bpf_func_proto bpf_map_delete_elem_proto = {
  76. .func = bpf_map_delete_elem,
  77. .gpl_only = false,
  78. .ret_type = RET_INTEGER,
  79. .arg1_type = ARG_CONST_MAP_PTR,
  80. .arg2_type = ARG_PTR_TO_MAP_KEY,
  81. };
  82. static u64 bpf_get_prandom_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  83. {
  84. return prandom_u32();
  85. }
  86. const struct bpf_func_proto bpf_get_prandom_u32_proto = {
  87. .func = bpf_get_prandom_u32,
  88. .gpl_only = false,
  89. .ret_type = RET_INTEGER,
  90. };
  91. static u64 bpf_get_smp_processor_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  92. {
  93. return raw_smp_processor_id();
  94. }
  95. const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
  96. .func = bpf_get_smp_processor_id,
  97. .gpl_only = false,
  98. .ret_type = RET_INTEGER,
  99. };
  100. static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  101. {
  102. /* NMI safe access to clock monotonic */
  103. return ktime_get_mono_fast_ns();
  104. }
  105. const struct bpf_func_proto bpf_ktime_get_ns_proto = {
  106. .func = bpf_ktime_get_ns,
  107. .gpl_only = true,
  108. .ret_type = RET_INTEGER,
  109. };
  110. static u64 bpf_get_current_pid_tgid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  111. {
  112. struct task_struct *task = current;
  113. if (!task)
  114. return -EINVAL;
  115. return (u64) task->tgid << 32 | task->pid;
  116. }
  117. const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
  118. .func = bpf_get_current_pid_tgid,
  119. .gpl_only = false,
  120. .ret_type = RET_INTEGER,
  121. };
  122. static u64 bpf_get_current_uid_gid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  123. {
  124. struct task_struct *task = current;
  125. kuid_t uid;
  126. kgid_t gid;
  127. if (!task)
  128. return -EINVAL;
  129. current_uid_gid(&uid, &gid);
  130. return (u64) from_kgid(&init_user_ns, gid) << 32 |
  131. from_kuid(&init_user_ns, uid);
  132. }
  133. const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
  134. .func = bpf_get_current_uid_gid,
  135. .gpl_only = false,
  136. .ret_type = RET_INTEGER,
  137. };
  138. static u64 bpf_get_current_comm(u64 r1, u64 size, u64 r3, u64 r4, u64 r5)
  139. {
  140. struct task_struct *task = current;
  141. char *buf = (char *) (long) r1;
  142. if (!task)
  143. return -EINVAL;
  144. memcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm)));
  145. return 0;
  146. }
  147. const struct bpf_func_proto bpf_get_current_comm_proto = {
  148. .func = bpf_get_current_comm,
  149. .gpl_only = false,
  150. .ret_type = RET_INTEGER,
  151. .arg1_type = ARG_PTR_TO_STACK,
  152. .arg2_type = ARG_CONST_STACK_SIZE,
  153. };