bpf_trace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. static u64 bpf_probe_write_user(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  73. {
  74. void *unsafe_ptr = (void *) (long) r1;
  75. void *src = (void *) (long) r2;
  76. int size = (int) r3;
  77. /*
  78. * Ensure we're in user context which is safe for the helper to
  79. * run. This helper has no business in a kthread.
  80. *
  81. * access_ok() should prevent writing to non-user memory, but in
  82. * some situations (nommu, temporary switch, etc) access_ok() does
  83. * not provide enough validation, hence the check on KERNEL_DS.
  84. */
  85. if (unlikely(in_interrupt() ||
  86. current->flags & (PF_KTHREAD | PF_EXITING)))
  87. return -EPERM;
  88. if (unlikely(segment_eq(get_fs(), KERNEL_DS)))
  89. return -EPERM;
  90. if (!access_ok(VERIFY_WRITE, unsafe_ptr, size))
  91. return -EPERM;
  92. return probe_kernel_write(unsafe_ptr, src, size);
  93. }
  94. static const struct bpf_func_proto bpf_probe_write_user_proto = {
  95. .func = bpf_probe_write_user,
  96. .gpl_only = true,
  97. .ret_type = RET_INTEGER,
  98. .arg1_type = ARG_ANYTHING,
  99. .arg2_type = ARG_PTR_TO_STACK,
  100. .arg3_type = ARG_CONST_STACK_SIZE,
  101. };
  102. static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
  103. {
  104. pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
  105. current->comm, task_pid_nr(current));
  106. return &bpf_probe_write_user_proto;
  107. }
  108. /*
  109. * limited trace_printk()
  110. * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed
  111. */
  112. static u64 bpf_trace_printk(u64 r1, u64 fmt_size, u64 r3, u64 r4, u64 r5)
  113. {
  114. char *fmt = (char *) (long) r1;
  115. bool str_seen = false;
  116. int mod[3] = {};
  117. int fmt_cnt = 0;
  118. u64 unsafe_addr;
  119. char buf[64];
  120. int i;
  121. /*
  122. * bpf_check()->check_func_arg()->check_stack_boundary()
  123. * guarantees that fmt points to bpf program stack,
  124. * fmt_size bytes of it were initialized and fmt_size > 0
  125. */
  126. if (fmt[--fmt_size] != 0)
  127. return -EINVAL;
  128. /* check format string for allowed specifiers */
  129. for (i = 0; i < fmt_size; i++) {
  130. if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
  131. return -EINVAL;
  132. if (fmt[i] != '%')
  133. continue;
  134. if (fmt_cnt >= 3)
  135. return -EINVAL;
  136. /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
  137. i++;
  138. if (fmt[i] == 'l') {
  139. mod[fmt_cnt]++;
  140. i++;
  141. } else if (fmt[i] == 'p' || fmt[i] == 's') {
  142. mod[fmt_cnt]++;
  143. i++;
  144. if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
  145. return -EINVAL;
  146. fmt_cnt++;
  147. if (fmt[i - 1] == 's') {
  148. if (str_seen)
  149. /* allow only one '%s' per fmt string */
  150. return -EINVAL;
  151. str_seen = true;
  152. switch (fmt_cnt) {
  153. case 1:
  154. unsafe_addr = r3;
  155. r3 = (long) buf;
  156. break;
  157. case 2:
  158. unsafe_addr = r4;
  159. r4 = (long) buf;
  160. break;
  161. case 3:
  162. unsafe_addr = r5;
  163. r5 = (long) buf;
  164. break;
  165. }
  166. buf[0] = 0;
  167. strncpy_from_unsafe(buf,
  168. (void *) (long) unsafe_addr,
  169. sizeof(buf));
  170. }
  171. continue;
  172. }
  173. if (fmt[i] == 'l') {
  174. mod[fmt_cnt]++;
  175. i++;
  176. }
  177. if (fmt[i] != 'd' && fmt[i] != 'u' && fmt[i] != 'x')
  178. return -EINVAL;
  179. fmt_cnt++;
  180. }
  181. return __trace_printk(1/* fake ip will not be printed */, fmt,
  182. mod[0] == 2 ? r3 : mod[0] == 1 ? (long) r3 : (u32) r3,
  183. mod[1] == 2 ? r4 : mod[1] == 1 ? (long) r4 : (u32) r4,
  184. mod[2] == 2 ? r5 : mod[2] == 1 ? (long) r5 : (u32) r5);
  185. }
  186. static const struct bpf_func_proto bpf_trace_printk_proto = {
  187. .func = bpf_trace_printk,
  188. .gpl_only = true,
  189. .ret_type = RET_INTEGER,
  190. .arg1_type = ARG_PTR_TO_STACK,
  191. .arg2_type = ARG_CONST_STACK_SIZE,
  192. };
  193. const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
  194. {
  195. /*
  196. * this program might be calling bpf_trace_printk,
  197. * so allocate per-cpu printk buffers
  198. */
  199. trace_printk_init_buffers();
  200. return &bpf_trace_printk_proto;
  201. }
  202. static u64 bpf_perf_event_read(u64 r1, u64 flags, u64 r3, u64 r4, u64 r5)
  203. {
  204. struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
  205. struct bpf_array *array = container_of(map, struct bpf_array, map);
  206. unsigned int cpu = smp_processor_id();
  207. u64 index = flags & BPF_F_INDEX_MASK;
  208. struct bpf_event_entry *ee;
  209. struct perf_event *event;
  210. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  211. return -EINVAL;
  212. if (index == BPF_F_CURRENT_CPU)
  213. index = cpu;
  214. if (unlikely(index >= array->map.max_entries))
  215. return -E2BIG;
  216. ee = READ_ONCE(array->ptrs[index]);
  217. if (!ee)
  218. return -ENOENT;
  219. event = ee->event;
  220. if (unlikely(event->attr.type != PERF_TYPE_HARDWARE &&
  221. event->attr.type != PERF_TYPE_RAW))
  222. return -EINVAL;
  223. /* make sure event is local and doesn't have pmu::count */
  224. if (unlikely(event->oncpu != cpu || event->pmu->count))
  225. return -EINVAL;
  226. /*
  227. * we don't know if the function is run successfully by the
  228. * return value. It can be judged in other places, such as
  229. * eBPF programs.
  230. */
  231. return perf_event_read_local(event);
  232. }
  233. static const struct bpf_func_proto bpf_perf_event_read_proto = {
  234. .func = bpf_perf_event_read,
  235. .gpl_only = true,
  236. .ret_type = RET_INTEGER,
  237. .arg1_type = ARG_CONST_MAP_PTR,
  238. .arg2_type = ARG_ANYTHING,
  239. };
  240. static __always_inline u64
  241. __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
  242. u64 flags, struct perf_raw_record *raw)
  243. {
  244. struct bpf_array *array = container_of(map, struct bpf_array, map);
  245. unsigned int cpu = smp_processor_id();
  246. u64 index = flags & BPF_F_INDEX_MASK;
  247. struct perf_sample_data sample_data;
  248. struct bpf_event_entry *ee;
  249. struct perf_event *event;
  250. if (index == BPF_F_CURRENT_CPU)
  251. index = cpu;
  252. if (unlikely(index >= array->map.max_entries))
  253. return -E2BIG;
  254. ee = READ_ONCE(array->ptrs[index]);
  255. if (!ee)
  256. return -ENOENT;
  257. event = ee->event;
  258. if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
  259. event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
  260. return -EINVAL;
  261. if (unlikely(event->oncpu != cpu))
  262. return -EOPNOTSUPP;
  263. perf_sample_data_init(&sample_data, 0, 0);
  264. sample_data.raw = raw;
  265. perf_event_output(event, &sample_data, regs);
  266. return 0;
  267. }
  268. static u64 bpf_perf_event_output(u64 r1, u64 r2, u64 flags, u64 r4, u64 size)
  269. {
  270. struct pt_regs *regs = (struct pt_regs *)(long) r1;
  271. struct bpf_map *map = (struct bpf_map *)(long) r2;
  272. void *data = (void *)(long) r4;
  273. struct perf_raw_record raw = {
  274. .frag = {
  275. .size = size,
  276. .data = data,
  277. },
  278. };
  279. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  280. return -EINVAL;
  281. return __bpf_perf_event_output(regs, map, flags, &raw);
  282. }
  283. static const struct bpf_func_proto bpf_perf_event_output_proto = {
  284. .func = bpf_perf_event_output,
  285. .gpl_only = true,
  286. .ret_type = RET_INTEGER,
  287. .arg1_type = ARG_PTR_TO_CTX,
  288. .arg2_type = ARG_CONST_MAP_PTR,
  289. .arg3_type = ARG_ANYTHING,
  290. .arg4_type = ARG_PTR_TO_STACK,
  291. .arg5_type = ARG_CONST_STACK_SIZE,
  292. };
  293. static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
  294. u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  295. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
  296. {
  297. struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
  298. struct perf_raw_frag frag = {
  299. .copy = ctx_copy,
  300. .size = ctx_size,
  301. .data = ctx,
  302. };
  303. struct perf_raw_record raw = {
  304. .frag = {
  305. {
  306. .next = ctx_size ? &frag : NULL,
  307. },
  308. .size = meta_size,
  309. .data = meta,
  310. },
  311. };
  312. perf_fetch_caller_regs(regs);
  313. return __bpf_perf_event_output(regs, map, flags, &raw);
  314. }
  315. static u64 bpf_get_current_task(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  316. {
  317. return (long) current;
  318. }
  319. static const struct bpf_func_proto bpf_get_current_task_proto = {
  320. .func = bpf_get_current_task,
  321. .gpl_only = true,
  322. .ret_type = RET_INTEGER,
  323. };
  324. static u64 bpf_current_task_under_cgroup(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  325. {
  326. struct bpf_map *map = (struct bpf_map *)(long)r1;
  327. struct bpf_array *array = container_of(map, struct bpf_array, map);
  328. struct cgroup *cgrp;
  329. u32 idx = (u32)r2;
  330. if (unlikely(in_interrupt()))
  331. return -EINVAL;
  332. if (unlikely(idx >= array->map.max_entries))
  333. return -E2BIG;
  334. cgrp = READ_ONCE(array->ptrs[idx]);
  335. if (unlikely(!cgrp))
  336. return -EAGAIN;
  337. return task_under_cgroup_hierarchy(current, cgrp);
  338. }
  339. static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
  340. .func = bpf_current_task_under_cgroup,
  341. .gpl_only = false,
  342. .ret_type = RET_INTEGER,
  343. .arg1_type = ARG_CONST_MAP_PTR,
  344. .arg2_type = ARG_ANYTHING,
  345. };
  346. static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
  347. {
  348. switch (func_id) {
  349. case BPF_FUNC_map_lookup_elem:
  350. return &bpf_map_lookup_elem_proto;
  351. case BPF_FUNC_map_update_elem:
  352. return &bpf_map_update_elem_proto;
  353. case BPF_FUNC_map_delete_elem:
  354. return &bpf_map_delete_elem_proto;
  355. case BPF_FUNC_probe_read:
  356. return &bpf_probe_read_proto;
  357. case BPF_FUNC_ktime_get_ns:
  358. return &bpf_ktime_get_ns_proto;
  359. case BPF_FUNC_tail_call:
  360. return &bpf_tail_call_proto;
  361. case BPF_FUNC_get_current_pid_tgid:
  362. return &bpf_get_current_pid_tgid_proto;
  363. case BPF_FUNC_get_current_task:
  364. return &bpf_get_current_task_proto;
  365. case BPF_FUNC_get_current_uid_gid:
  366. return &bpf_get_current_uid_gid_proto;
  367. case BPF_FUNC_get_current_comm:
  368. return &bpf_get_current_comm_proto;
  369. case BPF_FUNC_trace_printk:
  370. return bpf_get_trace_printk_proto();
  371. case BPF_FUNC_get_smp_processor_id:
  372. return &bpf_get_smp_processor_id_proto;
  373. case BPF_FUNC_perf_event_read:
  374. return &bpf_perf_event_read_proto;
  375. case BPF_FUNC_probe_write_user:
  376. return bpf_get_probe_write_proto();
  377. case BPF_FUNC_current_task_under_cgroup:
  378. return &bpf_current_task_under_cgroup_proto;
  379. case BPF_FUNC_get_prandom_u32:
  380. return &bpf_get_prandom_u32_proto;
  381. default:
  382. return NULL;
  383. }
  384. }
  385. static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
  386. {
  387. switch (func_id) {
  388. case BPF_FUNC_perf_event_output:
  389. return &bpf_perf_event_output_proto;
  390. case BPF_FUNC_get_stackid:
  391. return &bpf_get_stackid_proto;
  392. default:
  393. return tracing_func_proto(func_id);
  394. }
  395. }
  396. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  397. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  398. enum bpf_reg_type *reg_type)
  399. {
  400. if (off < 0 || off >= sizeof(struct pt_regs))
  401. return false;
  402. if (type != BPF_READ)
  403. return false;
  404. if (off % size != 0)
  405. return false;
  406. return true;
  407. }
  408. static const struct bpf_verifier_ops kprobe_prog_ops = {
  409. .get_func_proto = kprobe_prog_func_proto,
  410. .is_valid_access = kprobe_prog_is_valid_access,
  411. };
  412. static struct bpf_prog_type_list kprobe_tl = {
  413. .ops = &kprobe_prog_ops,
  414. .type = BPF_PROG_TYPE_KPROBE,
  415. };
  416. static u64 bpf_perf_event_output_tp(u64 r1, u64 r2, u64 index, u64 r4, u64 size)
  417. {
  418. /*
  419. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  420. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  421. * from there and call the same bpf_perf_event_output() helper
  422. */
  423. u64 ctx = *(long *)(uintptr_t)r1;
  424. return bpf_perf_event_output(ctx, r2, index, r4, size);
  425. }
  426. static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
  427. .func = bpf_perf_event_output_tp,
  428. .gpl_only = true,
  429. .ret_type = RET_INTEGER,
  430. .arg1_type = ARG_PTR_TO_CTX,
  431. .arg2_type = ARG_CONST_MAP_PTR,
  432. .arg3_type = ARG_ANYTHING,
  433. .arg4_type = ARG_PTR_TO_STACK,
  434. .arg5_type = ARG_CONST_STACK_SIZE,
  435. };
  436. static u64 bpf_get_stackid_tp(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  437. {
  438. u64 ctx = *(long *)(uintptr_t)r1;
  439. return bpf_get_stackid(ctx, r2, r3, r4, r5);
  440. }
  441. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  442. .func = bpf_get_stackid_tp,
  443. .gpl_only = true,
  444. .ret_type = RET_INTEGER,
  445. .arg1_type = ARG_PTR_TO_CTX,
  446. .arg2_type = ARG_CONST_MAP_PTR,
  447. .arg3_type = ARG_ANYTHING,
  448. };
  449. static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
  450. {
  451. switch (func_id) {
  452. case BPF_FUNC_perf_event_output:
  453. return &bpf_perf_event_output_proto_tp;
  454. case BPF_FUNC_get_stackid:
  455. return &bpf_get_stackid_proto_tp;
  456. default:
  457. return tracing_func_proto(func_id);
  458. }
  459. }
  460. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  461. enum bpf_reg_type *reg_type)
  462. {
  463. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  464. return false;
  465. if (type != BPF_READ)
  466. return false;
  467. if (off % size != 0)
  468. return false;
  469. return true;
  470. }
  471. static const struct bpf_verifier_ops tracepoint_prog_ops = {
  472. .get_func_proto = tp_prog_func_proto,
  473. .is_valid_access = tp_prog_is_valid_access,
  474. };
  475. static struct bpf_prog_type_list tracepoint_tl = {
  476. .ops = &tracepoint_prog_ops,
  477. .type = BPF_PROG_TYPE_TRACEPOINT,
  478. };
  479. static int __init register_kprobe_prog_ops(void)
  480. {
  481. bpf_register_prog_type(&kprobe_tl);
  482. bpf_register_prog_type(&tracepoint_tl);
  483. return 0;
  484. }
  485. late_initcall(register_kprobe_prog_ops);