bpf_trace.c 12 KB

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