bpf_trace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 flags, 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. unsigned int cpu = smp_processor_id();
  171. u64 index = flags & BPF_F_INDEX_MASK;
  172. struct bpf_event_entry *ee;
  173. struct perf_event *event;
  174. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  175. return -EINVAL;
  176. if (index == BPF_F_CURRENT_CPU)
  177. index = cpu;
  178. if (unlikely(index >= array->map.max_entries))
  179. return -E2BIG;
  180. ee = READ_ONCE(array->ptrs[index]);
  181. if (!ee)
  182. return -ENOENT;
  183. event = ee->event;
  184. if (unlikely(event->attr.type != PERF_TYPE_HARDWARE &&
  185. event->attr.type != PERF_TYPE_RAW))
  186. return -EINVAL;
  187. /* make sure event is local and doesn't have pmu::count */
  188. if (unlikely(event->oncpu != cpu || event->pmu->count))
  189. return -EINVAL;
  190. /*
  191. * we don't know if the function is run successfully by the
  192. * return value. It can be judged in other places, such as
  193. * eBPF programs.
  194. */
  195. return perf_event_read_local(event);
  196. }
  197. static const struct bpf_func_proto bpf_perf_event_read_proto = {
  198. .func = bpf_perf_event_read,
  199. .gpl_only = true,
  200. .ret_type = RET_INTEGER,
  201. .arg1_type = ARG_CONST_MAP_PTR,
  202. .arg2_type = ARG_ANYTHING,
  203. };
  204. static u64 bpf_perf_event_output(u64 r1, u64 r2, u64 flags, u64 r4, u64 size)
  205. {
  206. struct pt_regs *regs = (struct pt_regs *) (long) r1;
  207. struct bpf_map *map = (struct bpf_map *) (long) r2;
  208. struct bpf_array *array = container_of(map, struct bpf_array, map);
  209. unsigned int cpu = smp_processor_id();
  210. u64 index = flags & BPF_F_INDEX_MASK;
  211. void *data = (void *) (long) r4;
  212. struct perf_sample_data sample_data;
  213. struct bpf_event_entry *ee;
  214. struct perf_event *event;
  215. struct perf_raw_record raw = {
  216. .size = size,
  217. .data = data,
  218. };
  219. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  220. return -EINVAL;
  221. if (index == BPF_F_CURRENT_CPU)
  222. index = cpu;
  223. if (unlikely(index >= array->map.max_entries))
  224. return -E2BIG;
  225. ee = READ_ONCE(array->ptrs[index]);
  226. if (!ee)
  227. return -ENOENT;
  228. event = ee->event;
  229. if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
  230. event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
  231. return -EINVAL;
  232. if (unlikely(event->oncpu != cpu))
  233. return -EOPNOTSUPP;
  234. perf_sample_data_init(&sample_data, 0, 0);
  235. sample_data.raw = &raw;
  236. perf_event_output(event, &sample_data, regs);
  237. return 0;
  238. }
  239. static const struct bpf_func_proto bpf_perf_event_output_proto = {
  240. .func = bpf_perf_event_output,
  241. .gpl_only = true,
  242. .ret_type = RET_INTEGER,
  243. .arg1_type = ARG_PTR_TO_CTX,
  244. .arg2_type = ARG_CONST_MAP_PTR,
  245. .arg3_type = ARG_ANYTHING,
  246. .arg4_type = ARG_PTR_TO_STACK,
  247. .arg5_type = ARG_CONST_STACK_SIZE,
  248. };
  249. static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
  250. static u64 bpf_event_output(u64 r1, u64 r2, u64 flags, u64 r4, u64 size)
  251. {
  252. struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
  253. perf_fetch_caller_regs(regs);
  254. return bpf_perf_event_output((long)regs, r2, flags, r4, size);
  255. }
  256. static const struct bpf_func_proto bpf_event_output_proto = {
  257. .func = bpf_event_output,
  258. .gpl_only = true,
  259. .ret_type = RET_INTEGER,
  260. .arg1_type = ARG_PTR_TO_CTX,
  261. .arg2_type = ARG_CONST_MAP_PTR,
  262. .arg3_type = ARG_ANYTHING,
  263. .arg4_type = ARG_PTR_TO_STACK,
  264. .arg5_type = ARG_CONST_STACK_SIZE,
  265. };
  266. const struct bpf_func_proto *bpf_get_event_output_proto(void)
  267. {
  268. return &bpf_event_output_proto;
  269. }
  270. static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
  271. {
  272. switch (func_id) {
  273. case BPF_FUNC_map_lookup_elem:
  274. return &bpf_map_lookup_elem_proto;
  275. case BPF_FUNC_map_update_elem:
  276. return &bpf_map_update_elem_proto;
  277. case BPF_FUNC_map_delete_elem:
  278. return &bpf_map_delete_elem_proto;
  279. case BPF_FUNC_probe_read:
  280. return &bpf_probe_read_proto;
  281. case BPF_FUNC_ktime_get_ns:
  282. return &bpf_ktime_get_ns_proto;
  283. case BPF_FUNC_tail_call:
  284. return &bpf_tail_call_proto;
  285. case BPF_FUNC_get_current_pid_tgid:
  286. return &bpf_get_current_pid_tgid_proto;
  287. case BPF_FUNC_get_current_uid_gid:
  288. return &bpf_get_current_uid_gid_proto;
  289. case BPF_FUNC_get_current_comm:
  290. return &bpf_get_current_comm_proto;
  291. case BPF_FUNC_trace_printk:
  292. return bpf_get_trace_printk_proto();
  293. case BPF_FUNC_get_smp_processor_id:
  294. return &bpf_get_smp_processor_id_proto;
  295. case BPF_FUNC_perf_event_read:
  296. return &bpf_perf_event_read_proto;
  297. default:
  298. return NULL;
  299. }
  300. }
  301. static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
  302. {
  303. switch (func_id) {
  304. case BPF_FUNC_perf_event_output:
  305. return &bpf_perf_event_output_proto;
  306. case BPF_FUNC_get_stackid:
  307. return &bpf_get_stackid_proto;
  308. default:
  309. return tracing_func_proto(func_id);
  310. }
  311. }
  312. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  313. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  314. enum bpf_reg_type *reg_type)
  315. {
  316. if (off < 0 || off >= sizeof(struct pt_regs))
  317. return false;
  318. if (type != BPF_READ)
  319. return false;
  320. if (off % size != 0)
  321. return false;
  322. return true;
  323. }
  324. static const struct bpf_verifier_ops kprobe_prog_ops = {
  325. .get_func_proto = kprobe_prog_func_proto,
  326. .is_valid_access = kprobe_prog_is_valid_access,
  327. };
  328. static struct bpf_prog_type_list kprobe_tl = {
  329. .ops = &kprobe_prog_ops,
  330. .type = BPF_PROG_TYPE_KPROBE,
  331. };
  332. static u64 bpf_perf_event_output_tp(u64 r1, u64 r2, u64 index, u64 r4, u64 size)
  333. {
  334. /*
  335. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  336. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  337. * from there and call the same bpf_perf_event_output() helper
  338. */
  339. u64 ctx = *(long *)(uintptr_t)r1;
  340. return bpf_perf_event_output(ctx, r2, index, r4, size);
  341. }
  342. static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
  343. .func = bpf_perf_event_output_tp,
  344. .gpl_only = true,
  345. .ret_type = RET_INTEGER,
  346. .arg1_type = ARG_PTR_TO_CTX,
  347. .arg2_type = ARG_CONST_MAP_PTR,
  348. .arg3_type = ARG_ANYTHING,
  349. .arg4_type = ARG_PTR_TO_STACK,
  350. .arg5_type = ARG_CONST_STACK_SIZE,
  351. };
  352. static u64 bpf_get_stackid_tp(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  353. {
  354. u64 ctx = *(long *)(uintptr_t)r1;
  355. return bpf_get_stackid(ctx, r2, r3, r4, r5);
  356. }
  357. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  358. .func = bpf_get_stackid_tp,
  359. .gpl_only = true,
  360. .ret_type = RET_INTEGER,
  361. .arg1_type = ARG_PTR_TO_CTX,
  362. .arg2_type = ARG_CONST_MAP_PTR,
  363. .arg3_type = ARG_ANYTHING,
  364. };
  365. static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
  366. {
  367. switch (func_id) {
  368. case BPF_FUNC_perf_event_output:
  369. return &bpf_perf_event_output_proto_tp;
  370. case BPF_FUNC_get_stackid:
  371. return &bpf_get_stackid_proto_tp;
  372. default:
  373. return tracing_func_proto(func_id);
  374. }
  375. }
  376. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  377. enum bpf_reg_type *reg_type)
  378. {
  379. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  380. return false;
  381. if (type != BPF_READ)
  382. return false;
  383. if (off % size != 0)
  384. return false;
  385. return true;
  386. }
  387. static const struct bpf_verifier_ops tracepoint_prog_ops = {
  388. .get_func_proto = tp_prog_func_proto,
  389. .is_valid_access = tp_prog_is_valid_access,
  390. };
  391. static struct bpf_prog_type_list tracepoint_tl = {
  392. .ops = &tracepoint_prog_ops,
  393. .type = BPF_PROG_TYPE_TRACEPOINT,
  394. };
  395. static int __init register_kprobe_prog_ops(void)
  396. {
  397. bpf_register_prog_type(&kprobe_tl);
  398. bpf_register_prog_type(&tracepoint_tl);
  399. return 0;
  400. }
  401. late_initcall(register_kprobe_prog_ops);