bpf_trace.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Copyright (c) 2011-2015 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. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/slab.h>
  10. #include <linux/bpf.h>
  11. #include <linux/filter.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/ctype.h>
  14. #include "trace.h"
  15. static DEFINE_PER_CPU(int, bpf_prog_active);
  16. /**
  17. * trace_call_bpf - invoke BPF program
  18. * @prog: BPF program
  19. * @ctx: opaque context pointer
  20. *
  21. * kprobe handlers execute BPF programs via this helper.
  22. * Can be used from static tracepoints in the future.
  23. *
  24. * Return: BPF programs always return an integer which is interpreted by
  25. * kprobe handler as:
  26. * 0 - return from kprobe (event is filtered out)
  27. * 1 - store kprobe event into ring buffer
  28. * Other values are reserved and currently alias to 1
  29. */
  30. unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx)
  31. {
  32. unsigned int ret;
  33. if (in_nmi()) /* not supported yet */
  34. return 1;
  35. preempt_disable();
  36. if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
  37. /*
  38. * since some bpf program is already running on this cpu,
  39. * don't call into another bpf program (same or different)
  40. * and don't send kprobe event into ring-buffer,
  41. * so return zero here
  42. */
  43. ret = 0;
  44. goto out;
  45. }
  46. rcu_read_lock();
  47. ret = BPF_PROG_RUN(prog, ctx);
  48. rcu_read_unlock();
  49. out:
  50. __this_cpu_dec(bpf_prog_active);
  51. preempt_enable();
  52. return ret;
  53. }
  54. EXPORT_SYMBOL_GPL(trace_call_bpf);
  55. static u64 bpf_probe_read(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  56. {
  57. void *dst = (void *) (long) r1;
  58. int size = (int) r2;
  59. void *unsafe_ptr = (void *) (long) r3;
  60. return probe_kernel_read(dst, unsafe_ptr, size);
  61. }
  62. static const struct bpf_func_proto bpf_probe_read_proto = {
  63. .func = bpf_probe_read,
  64. .gpl_only = true,
  65. .ret_type = RET_INTEGER,
  66. .arg1_type = ARG_PTR_TO_STACK,
  67. .arg2_type = ARG_CONST_STACK_SIZE,
  68. .arg3_type = ARG_ANYTHING,
  69. };
  70. /*
  71. * limited trace_printk()
  72. * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed
  73. */
  74. static u64 bpf_trace_printk(u64 r1, u64 fmt_size, u64 r3, u64 r4, u64 r5)
  75. {
  76. char *fmt = (char *) (long) r1;
  77. bool str_seen = false;
  78. int mod[3] = {};
  79. int fmt_cnt = 0;
  80. u64 unsafe_addr;
  81. char buf[64];
  82. int i;
  83. /*
  84. * bpf_check()->check_func_arg()->check_stack_boundary()
  85. * guarantees that fmt points to bpf program stack,
  86. * fmt_size bytes of it were initialized and fmt_size > 0
  87. */
  88. if (fmt[--fmt_size] != 0)
  89. return -EINVAL;
  90. /* check format string for allowed specifiers */
  91. for (i = 0; i < fmt_size; i++) {
  92. if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
  93. return -EINVAL;
  94. if (fmt[i] != '%')
  95. continue;
  96. if (fmt_cnt >= 3)
  97. return -EINVAL;
  98. /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
  99. i++;
  100. if (fmt[i] == 'l') {
  101. mod[fmt_cnt]++;
  102. i++;
  103. } else if (fmt[i] == 'p' || fmt[i] == 's') {
  104. mod[fmt_cnt]++;
  105. /* disallow any further format extensions */
  106. if (fmt[i + 1] != 0 &&
  107. !isspace(fmt[i + 1]) &&
  108. !ispunct(fmt[i + 1]))
  109. return -EINVAL;
  110. fmt_cnt++;
  111. if (fmt[i] == 's') {
  112. if (str_seen)
  113. /* allow only one '%s' per fmt string */
  114. return -EINVAL;
  115. str_seen = true;
  116. switch (fmt_cnt) {
  117. case 1:
  118. unsafe_addr = r3;
  119. r3 = (long) buf;
  120. break;
  121. case 2:
  122. unsafe_addr = r4;
  123. r4 = (long) buf;
  124. break;
  125. case 3:
  126. unsafe_addr = r5;
  127. r5 = (long) buf;
  128. break;
  129. }
  130. buf[0] = 0;
  131. strncpy_from_unsafe(buf,
  132. (void *) (long) unsafe_addr,
  133. sizeof(buf));
  134. }
  135. continue;
  136. }
  137. if (fmt[i] == 'l') {
  138. mod[fmt_cnt]++;
  139. i++;
  140. }
  141. if (fmt[i] != 'd' && fmt[i] != 'u' && fmt[i] != 'x')
  142. return -EINVAL;
  143. fmt_cnt++;
  144. }
  145. return __trace_printk(1/* fake ip will not be printed */, fmt,
  146. mod[0] == 2 ? r3 : mod[0] == 1 ? (long) r3 : (u32) r3,
  147. mod[1] == 2 ? r4 : mod[1] == 1 ? (long) r4 : (u32) r4,
  148. mod[2] == 2 ? r5 : mod[2] == 1 ? (long) r5 : (u32) r5);
  149. }
  150. static const struct bpf_func_proto bpf_trace_printk_proto = {
  151. .func = bpf_trace_printk,
  152. .gpl_only = true,
  153. .ret_type = RET_INTEGER,
  154. .arg1_type = ARG_PTR_TO_STACK,
  155. .arg2_type = ARG_CONST_STACK_SIZE,
  156. };
  157. const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
  158. {
  159. /*
  160. * this program might be calling bpf_trace_printk,
  161. * so allocate per-cpu printk buffers
  162. */
  163. trace_printk_init_buffers();
  164. return &bpf_trace_printk_proto;
  165. }
  166. static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5)
  167. {
  168. struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
  169. struct bpf_array *array = container_of(map, struct bpf_array, map);
  170. struct perf_event *event;
  171. if (unlikely(index >= array->map.max_entries))
  172. return -E2BIG;
  173. event = (struct perf_event *)array->ptrs[index];
  174. if (!event)
  175. return -ENOENT;
  176. /* make sure event is local and doesn't have pmu::count */
  177. if (event->oncpu != smp_processor_id() ||
  178. event->pmu->count)
  179. return -EINVAL;
  180. /*
  181. * we don't know if the function is run successfully by the
  182. * return value. It can be judged in other places, such as
  183. * eBPF programs.
  184. */
  185. return perf_event_read_local(event);
  186. }
  187. static const struct bpf_func_proto bpf_perf_event_read_proto = {
  188. .func = bpf_perf_event_read,
  189. .gpl_only = true,
  190. .ret_type = RET_INTEGER,
  191. .arg1_type = ARG_CONST_MAP_PTR,
  192. .arg2_type = ARG_ANYTHING,
  193. };
  194. static u64 bpf_perf_event_output(u64 r1, u64 r2, u64 index, u64 r4, u64 size)
  195. {
  196. struct pt_regs *regs = (struct pt_regs *) (long) r1;
  197. struct bpf_map *map = (struct bpf_map *) (long) r2;
  198. struct bpf_array *array = container_of(map, struct bpf_array, map);
  199. void *data = (void *) (long) r4;
  200. struct perf_sample_data sample_data;
  201. struct perf_event *event;
  202. struct perf_raw_record raw = {
  203. .size = size,
  204. .data = data,
  205. };
  206. if (unlikely(index >= array->map.max_entries))
  207. return -E2BIG;
  208. event = (struct perf_event *)array->ptrs[index];
  209. if (unlikely(!event))
  210. return -ENOENT;
  211. if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
  212. event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
  213. return -EINVAL;
  214. if (unlikely(event->oncpu != smp_processor_id()))
  215. return -EOPNOTSUPP;
  216. perf_sample_data_init(&sample_data, 0, 0);
  217. sample_data.raw = &raw;
  218. perf_event_output(event, &sample_data, regs);
  219. return 0;
  220. }
  221. static const struct bpf_func_proto bpf_perf_event_output_proto = {
  222. .func = bpf_perf_event_output,
  223. .gpl_only = true,
  224. .ret_type = RET_INTEGER,
  225. .arg1_type = ARG_PTR_TO_CTX,
  226. .arg2_type = ARG_CONST_MAP_PTR,
  227. .arg3_type = ARG_ANYTHING,
  228. .arg4_type = ARG_PTR_TO_STACK,
  229. .arg5_type = ARG_CONST_STACK_SIZE,
  230. };
  231. static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
  232. {
  233. switch (func_id) {
  234. case BPF_FUNC_map_lookup_elem:
  235. return &bpf_map_lookup_elem_proto;
  236. case BPF_FUNC_map_update_elem:
  237. return &bpf_map_update_elem_proto;
  238. case BPF_FUNC_map_delete_elem:
  239. return &bpf_map_delete_elem_proto;
  240. case BPF_FUNC_probe_read:
  241. return &bpf_probe_read_proto;
  242. case BPF_FUNC_ktime_get_ns:
  243. return &bpf_ktime_get_ns_proto;
  244. case BPF_FUNC_tail_call:
  245. return &bpf_tail_call_proto;
  246. case BPF_FUNC_get_current_pid_tgid:
  247. return &bpf_get_current_pid_tgid_proto;
  248. case BPF_FUNC_get_current_uid_gid:
  249. return &bpf_get_current_uid_gid_proto;
  250. case BPF_FUNC_get_current_comm:
  251. return &bpf_get_current_comm_proto;
  252. case BPF_FUNC_trace_printk:
  253. return bpf_get_trace_printk_proto();
  254. case BPF_FUNC_get_smp_processor_id:
  255. return &bpf_get_smp_processor_id_proto;
  256. case BPF_FUNC_perf_event_read:
  257. return &bpf_perf_event_read_proto;
  258. case BPF_FUNC_perf_event_output:
  259. return &bpf_perf_event_output_proto;
  260. default:
  261. return NULL;
  262. }
  263. }
  264. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  265. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type)
  266. {
  267. /* check bounds */
  268. if (off < 0 || off >= sizeof(struct pt_regs))
  269. return false;
  270. /* only read is allowed */
  271. if (type != BPF_READ)
  272. return false;
  273. /* disallow misaligned access */
  274. if (off % size != 0)
  275. return false;
  276. return true;
  277. }
  278. static struct bpf_verifier_ops kprobe_prog_ops = {
  279. .get_func_proto = kprobe_prog_func_proto,
  280. .is_valid_access = kprobe_prog_is_valid_access,
  281. };
  282. static struct bpf_prog_type_list kprobe_tl = {
  283. .ops = &kprobe_prog_ops,
  284. .type = BPF_PROG_TYPE_KPROBE,
  285. };
  286. static int __init register_kprobe_prog_ops(void)
  287. {
  288. bpf_register_prog_type(&kprobe_tl);
  289. return 0;
  290. }
  291. late_initcall(register_kprobe_prog_ops);