bpf_trace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. /*
  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 = READ_ONCE(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 DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
  241. static u64 bpf_event_output(u64 r1, u64 r2, u64 flags, u64 r4, u64 size)
  242. {
  243. struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
  244. perf_fetch_caller_regs(regs);
  245. return bpf_perf_event_output((long)regs, r2, flags, r4, size);
  246. }
  247. static const struct bpf_func_proto bpf_event_output_proto = {
  248. .func = bpf_event_output,
  249. .gpl_only = true,
  250. .ret_type = RET_INTEGER,
  251. .arg1_type = ARG_PTR_TO_CTX,
  252. .arg2_type = ARG_CONST_MAP_PTR,
  253. .arg3_type = ARG_ANYTHING,
  254. .arg4_type = ARG_PTR_TO_STACK,
  255. .arg5_type = ARG_CONST_STACK_SIZE,
  256. };
  257. const struct bpf_func_proto *bpf_get_event_output_proto(void)
  258. {
  259. return &bpf_event_output_proto;
  260. }
  261. static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
  262. {
  263. switch (func_id) {
  264. case BPF_FUNC_map_lookup_elem:
  265. return &bpf_map_lookup_elem_proto;
  266. case BPF_FUNC_map_update_elem:
  267. return &bpf_map_update_elem_proto;
  268. case BPF_FUNC_map_delete_elem:
  269. return &bpf_map_delete_elem_proto;
  270. case BPF_FUNC_probe_read:
  271. return &bpf_probe_read_proto;
  272. case BPF_FUNC_ktime_get_ns:
  273. return &bpf_ktime_get_ns_proto;
  274. case BPF_FUNC_tail_call:
  275. return &bpf_tail_call_proto;
  276. case BPF_FUNC_get_current_pid_tgid:
  277. return &bpf_get_current_pid_tgid_proto;
  278. case BPF_FUNC_get_current_uid_gid:
  279. return &bpf_get_current_uid_gid_proto;
  280. case BPF_FUNC_get_current_comm:
  281. return &bpf_get_current_comm_proto;
  282. case BPF_FUNC_trace_printk:
  283. return bpf_get_trace_printk_proto();
  284. case BPF_FUNC_get_smp_processor_id:
  285. return &bpf_get_smp_processor_id_proto;
  286. case BPF_FUNC_perf_event_read:
  287. return &bpf_perf_event_read_proto;
  288. default:
  289. return NULL;
  290. }
  291. }
  292. static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
  293. {
  294. switch (func_id) {
  295. case BPF_FUNC_perf_event_output:
  296. return &bpf_perf_event_output_proto;
  297. case BPF_FUNC_get_stackid:
  298. return &bpf_get_stackid_proto;
  299. default:
  300. return tracing_func_proto(func_id);
  301. }
  302. }
  303. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  304. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type)
  305. {
  306. /* check bounds */
  307. if (off < 0 || off >= sizeof(struct pt_regs))
  308. return false;
  309. /* only read is allowed */
  310. if (type != BPF_READ)
  311. return false;
  312. /* disallow misaligned access */
  313. if (off % size != 0)
  314. return false;
  315. return true;
  316. }
  317. static const struct bpf_verifier_ops kprobe_prog_ops = {
  318. .get_func_proto = kprobe_prog_func_proto,
  319. .is_valid_access = kprobe_prog_is_valid_access,
  320. };
  321. static struct bpf_prog_type_list kprobe_tl = {
  322. .ops = &kprobe_prog_ops,
  323. .type = BPF_PROG_TYPE_KPROBE,
  324. };
  325. static u64 bpf_perf_event_output_tp(u64 r1, u64 r2, u64 index, u64 r4, u64 size)
  326. {
  327. /*
  328. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  329. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  330. * from there and call the same bpf_perf_event_output() helper
  331. */
  332. u64 ctx = *(long *)(uintptr_t)r1;
  333. return bpf_perf_event_output(ctx, r2, index, r4, size);
  334. }
  335. static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
  336. .func = bpf_perf_event_output_tp,
  337. .gpl_only = true,
  338. .ret_type = RET_INTEGER,
  339. .arg1_type = ARG_PTR_TO_CTX,
  340. .arg2_type = ARG_CONST_MAP_PTR,
  341. .arg3_type = ARG_ANYTHING,
  342. .arg4_type = ARG_PTR_TO_STACK,
  343. .arg5_type = ARG_CONST_STACK_SIZE,
  344. };
  345. static u64 bpf_get_stackid_tp(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  346. {
  347. u64 ctx = *(long *)(uintptr_t)r1;
  348. return bpf_get_stackid(ctx, r2, r3, r4, r5);
  349. }
  350. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  351. .func = bpf_get_stackid_tp,
  352. .gpl_only = true,
  353. .ret_type = RET_INTEGER,
  354. .arg1_type = ARG_PTR_TO_CTX,
  355. .arg2_type = ARG_CONST_MAP_PTR,
  356. .arg3_type = ARG_ANYTHING,
  357. };
  358. static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
  359. {
  360. switch (func_id) {
  361. case BPF_FUNC_perf_event_output:
  362. return &bpf_perf_event_output_proto_tp;
  363. case BPF_FUNC_get_stackid:
  364. return &bpf_get_stackid_proto_tp;
  365. default:
  366. return tracing_func_proto(func_id);
  367. }
  368. }
  369. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type)
  370. {
  371. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  372. return false;
  373. if (type != BPF_READ)
  374. return false;
  375. if (off % size != 0)
  376. return false;
  377. return true;
  378. }
  379. static const struct bpf_verifier_ops tracepoint_prog_ops = {
  380. .get_func_proto = tp_prog_func_proto,
  381. .is_valid_access = tp_prog_is_valid_access,
  382. };
  383. static struct bpf_prog_type_list tracepoint_tl = {
  384. .ops = &tracepoint_prog_ops,
  385. .type = BPF_PROG_TYPE_TRACEPOINT,
  386. };
  387. static int __init register_kprobe_prog_ops(void)
  388. {
  389. bpf_register_prog_type(&kprobe_tl);
  390. bpf_register_prog_type(&tracepoint_tl);
  391. return 0;
  392. }
  393. late_initcall(register_kprobe_prog_ops);