bpf_trace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 ret, size = (int) r2;
  58. void *unsafe_ptr = (void *) (long) r3;
  59. ret = probe_kernel_read(dst, unsafe_ptr, size);
  60. if (unlikely(ret < 0))
  61. memset(dst, 0, size);
  62. return ret;
  63. }
  64. static const struct bpf_func_proto bpf_probe_read_proto = {
  65. .func = bpf_probe_read,
  66. .gpl_only = true,
  67. .ret_type = RET_INTEGER,
  68. .arg1_type = ARG_PTR_TO_RAW_STACK,
  69. .arg2_type = ARG_CONST_STACK_SIZE,
  70. .arg3_type = ARG_ANYTHING,
  71. };
  72. /*
  73. * limited trace_printk()
  74. * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed
  75. */
  76. static u64 bpf_trace_printk(u64 r1, u64 fmt_size, u64 r3, u64 r4, u64 r5)
  77. {
  78. char *fmt = (char *) (long) r1;
  79. bool str_seen = false;
  80. int mod[3] = {};
  81. int fmt_cnt = 0;
  82. u64 unsafe_addr;
  83. char buf[64];
  84. int i;
  85. /*
  86. * bpf_check()->check_func_arg()->check_stack_boundary()
  87. * guarantees that fmt points to bpf program stack,
  88. * fmt_size bytes of it were initialized and fmt_size > 0
  89. */
  90. if (fmt[--fmt_size] != 0)
  91. return -EINVAL;
  92. /* check format string for allowed specifiers */
  93. for (i = 0; i < fmt_size; i++) {
  94. if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
  95. return -EINVAL;
  96. if (fmt[i] != '%')
  97. continue;
  98. if (fmt_cnt >= 3)
  99. return -EINVAL;
  100. /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
  101. i++;
  102. if (fmt[i] == 'l') {
  103. mod[fmt_cnt]++;
  104. i++;
  105. } else if (fmt[i] == 'p' || fmt[i] == 's') {
  106. mod[fmt_cnt]++;
  107. i++;
  108. if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
  109. return -EINVAL;
  110. fmt_cnt++;
  111. if (fmt[i - 1] == '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. struct file *file;
  172. if (unlikely(index >= array->map.max_entries))
  173. return -E2BIG;
  174. file = (struct file *)array->ptrs[index];
  175. if (unlikely(!file))
  176. return -ENOENT;
  177. event = file->private_data;
  178. /* make sure event is local and doesn't have pmu::count */
  179. if (event->oncpu != smp_processor_id() ||
  180. event->pmu->count)
  181. return -EINVAL;
  182. /*
  183. * we don't know if the function is run successfully by the
  184. * return value. It can be judged in other places, such as
  185. * eBPF programs.
  186. */
  187. return perf_event_read_local(event);
  188. }
  189. static const struct bpf_func_proto bpf_perf_event_read_proto = {
  190. .func = bpf_perf_event_read,
  191. .gpl_only = true,
  192. .ret_type = RET_INTEGER,
  193. .arg1_type = ARG_CONST_MAP_PTR,
  194. .arg2_type = ARG_ANYTHING,
  195. };
  196. static u64 bpf_perf_event_output(u64 r1, u64 r2, u64 flags, u64 r4, u64 size)
  197. {
  198. struct pt_regs *regs = (struct pt_regs *) (long) r1;
  199. struct bpf_map *map = (struct bpf_map *) (long) r2;
  200. struct bpf_array *array = container_of(map, struct bpf_array, map);
  201. u64 index = flags & BPF_F_INDEX_MASK;
  202. void *data = (void *) (long) r4;
  203. struct perf_sample_data sample_data;
  204. struct perf_event *event;
  205. struct file *file;
  206. struct perf_raw_record raw = {
  207. .size = size,
  208. .data = data,
  209. };
  210. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  211. return -EINVAL;
  212. if (index == BPF_F_CURRENT_CPU)
  213. index = raw_smp_processor_id();
  214. if (unlikely(index >= array->map.max_entries))
  215. return -E2BIG;
  216. file = (struct file *)array->ptrs[index];
  217. if (unlikely(!file))
  218. return -ENOENT;
  219. event = file->private_data;
  220. if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
  221. event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
  222. return -EINVAL;
  223. if (unlikely(event->oncpu != smp_processor_id()))
  224. return -EOPNOTSUPP;
  225. perf_sample_data_init(&sample_data, 0, 0);
  226. sample_data.raw = &raw;
  227. perf_event_output(event, &sample_data, regs);
  228. return 0;
  229. }
  230. static const struct bpf_func_proto bpf_perf_event_output_proto = {
  231. .func = bpf_perf_event_output,
  232. .gpl_only = true,
  233. .ret_type = RET_INTEGER,
  234. .arg1_type = ARG_PTR_TO_CTX,
  235. .arg2_type = ARG_CONST_MAP_PTR,
  236. .arg3_type = ARG_ANYTHING,
  237. .arg4_type = ARG_PTR_TO_STACK,
  238. .arg5_type = ARG_CONST_STACK_SIZE,
  239. };
  240. static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
  241. {
  242. switch (func_id) {
  243. case BPF_FUNC_map_lookup_elem:
  244. return &bpf_map_lookup_elem_proto;
  245. case BPF_FUNC_map_update_elem:
  246. return &bpf_map_update_elem_proto;
  247. case BPF_FUNC_map_delete_elem:
  248. return &bpf_map_delete_elem_proto;
  249. case BPF_FUNC_probe_read:
  250. return &bpf_probe_read_proto;
  251. case BPF_FUNC_ktime_get_ns:
  252. return &bpf_ktime_get_ns_proto;
  253. case BPF_FUNC_tail_call:
  254. return &bpf_tail_call_proto;
  255. case BPF_FUNC_get_current_pid_tgid:
  256. return &bpf_get_current_pid_tgid_proto;
  257. case BPF_FUNC_get_current_uid_gid:
  258. return &bpf_get_current_uid_gid_proto;
  259. case BPF_FUNC_get_current_comm:
  260. return &bpf_get_current_comm_proto;
  261. case BPF_FUNC_trace_printk:
  262. return bpf_get_trace_printk_proto();
  263. case BPF_FUNC_get_smp_processor_id:
  264. return &bpf_get_smp_processor_id_proto;
  265. case BPF_FUNC_perf_event_read:
  266. return &bpf_perf_event_read_proto;
  267. default:
  268. return NULL;
  269. }
  270. }
  271. static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
  272. {
  273. switch (func_id) {
  274. case BPF_FUNC_perf_event_output:
  275. return &bpf_perf_event_output_proto;
  276. case BPF_FUNC_get_stackid:
  277. return &bpf_get_stackid_proto;
  278. default:
  279. return tracing_func_proto(func_id);
  280. }
  281. }
  282. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  283. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type)
  284. {
  285. /* check bounds */
  286. if (off < 0 || off >= sizeof(struct pt_regs))
  287. return false;
  288. /* only read is allowed */
  289. if (type != BPF_READ)
  290. return false;
  291. /* disallow misaligned access */
  292. if (off % size != 0)
  293. return false;
  294. return true;
  295. }
  296. static const struct bpf_verifier_ops kprobe_prog_ops = {
  297. .get_func_proto = kprobe_prog_func_proto,
  298. .is_valid_access = kprobe_prog_is_valid_access,
  299. };
  300. static struct bpf_prog_type_list kprobe_tl = {
  301. .ops = &kprobe_prog_ops,
  302. .type = BPF_PROG_TYPE_KPROBE,
  303. };
  304. static u64 bpf_perf_event_output_tp(u64 r1, u64 r2, u64 index, u64 r4, u64 size)
  305. {
  306. /*
  307. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  308. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  309. * from there and call the same bpf_perf_event_output() helper
  310. */
  311. u64 ctx = *(long *)(uintptr_t)r1;
  312. return bpf_perf_event_output(ctx, r2, index, r4, size);
  313. }
  314. static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
  315. .func = bpf_perf_event_output_tp,
  316. .gpl_only = true,
  317. .ret_type = RET_INTEGER,
  318. .arg1_type = ARG_PTR_TO_CTX,
  319. .arg2_type = ARG_CONST_MAP_PTR,
  320. .arg3_type = ARG_ANYTHING,
  321. .arg4_type = ARG_PTR_TO_STACK,
  322. .arg5_type = ARG_CONST_STACK_SIZE,
  323. };
  324. static u64 bpf_get_stackid_tp(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  325. {
  326. u64 ctx = *(long *)(uintptr_t)r1;
  327. return bpf_get_stackid(ctx, r2, r3, r4, r5);
  328. }
  329. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  330. .func = bpf_get_stackid_tp,
  331. .gpl_only = true,
  332. .ret_type = RET_INTEGER,
  333. .arg1_type = ARG_PTR_TO_CTX,
  334. .arg2_type = ARG_CONST_MAP_PTR,
  335. .arg3_type = ARG_ANYTHING,
  336. };
  337. static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
  338. {
  339. switch (func_id) {
  340. case BPF_FUNC_perf_event_output:
  341. return &bpf_perf_event_output_proto_tp;
  342. case BPF_FUNC_get_stackid:
  343. return &bpf_get_stackid_proto_tp;
  344. default:
  345. return tracing_func_proto(func_id);
  346. }
  347. }
  348. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type)
  349. {
  350. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  351. return false;
  352. if (type != BPF_READ)
  353. return false;
  354. if (off % size != 0)
  355. return false;
  356. return true;
  357. }
  358. static const struct bpf_verifier_ops tracepoint_prog_ops = {
  359. .get_func_proto = tp_prog_func_proto,
  360. .is_valid_access = tp_prog_is_valid_access,
  361. };
  362. static struct bpf_prog_type_list tracepoint_tl = {
  363. .ops = &tracepoint_prog_ops,
  364. .type = BPF_PROG_TYPE_TRACEPOINT,
  365. };
  366. static int __init register_kprobe_prog_ops(void)
  367. {
  368. bpf_register_prog_type(&kprobe_tl);
  369. bpf_register_prog_type(&tracepoint_tl);
  370. return 0;
  371. }
  372. late_initcall(register_kprobe_prog_ops);