helpers.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. const struct bpf_func_proto bpf_get_prandom_u32_proto = {
  83. .func = bpf_user_rnd_u32,
  84. .gpl_only = false,
  85. .ret_type = RET_INTEGER,
  86. };
  87. static u64 bpf_get_smp_processor_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  88. {
  89. return raw_smp_processor_id();
  90. }
  91. const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
  92. .func = bpf_get_smp_processor_id,
  93. .gpl_only = false,
  94. .ret_type = RET_INTEGER,
  95. };
  96. static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  97. {
  98. /* NMI safe access to clock monotonic */
  99. return ktime_get_mono_fast_ns();
  100. }
  101. const struct bpf_func_proto bpf_ktime_get_ns_proto = {
  102. .func = bpf_ktime_get_ns,
  103. .gpl_only = true,
  104. .ret_type = RET_INTEGER,
  105. };
  106. static u64 bpf_get_current_pid_tgid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  107. {
  108. struct task_struct *task = current;
  109. if (!task)
  110. return -EINVAL;
  111. return (u64) task->tgid << 32 | task->pid;
  112. }
  113. const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
  114. .func = bpf_get_current_pid_tgid,
  115. .gpl_only = false,
  116. .ret_type = RET_INTEGER,
  117. };
  118. static u64 bpf_get_current_uid_gid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  119. {
  120. struct task_struct *task = current;
  121. kuid_t uid;
  122. kgid_t gid;
  123. if (!task)
  124. return -EINVAL;
  125. current_uid_gid(&uid, &gid);
  126. return (u64) from_kgid(&init_user_ns, gid) << 32 |
  127. from_kuid(&init_user_ns, uid);
  128. }
  129. const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
  130. .func = bpf_get_current_uid_gid,
  131. .gpl_only = false,
  132. .ret_type = RET_INTEGER,
  133. };
  134. static u64 bpf_get_current_comm(u64 r1, u64 size, u64 r3, u64 r4, u64 r5)
  135. {
  136. struct task_struct *task = current;
  137. char *buf = (char *) (long) r1;
  138. if (!task)
  139. return -EINVAL;
  140. strlcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm)));
  141. return 0;
  142. }
  143. const struct bpf_func_proto bpf_get_current_comm_proto = {
  144. .func = bpf_get_current_comm,
  145. .gpl_only = false,
  146. .ret_type = RET_INTEGER,
  147. .arg1_type = ARG_PTR_TO_STACK,
  148. .arg2_type = ARG_CONST_STACK_SIZE,
  149. };