bpf_trace.c 8.4 KB

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