bpf_trace.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
  2. * Copyright (c) 2016 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include <linux/slab.h>
  11. #include <linux/bpf.h>
  12. #include <linux/bpf_perf_event.h>
  13. #include <linux/filter.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/ctype.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/error-injection.h>
  18. #include "trace_probe.h"
  19. #include "trace.h"
  20. u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  21. /**
  22. * trace_call_bpf - invoke BPF program
  23. * @call: tracepoint event
  24. * @ctx: opaque context pointer
  25. *
  26. * kprobe handlers execute BPF programs via this helper.
  27. * Can be used from static tracepoints in the future.
  28. *
  29. * Return: BPF programs always return an integer which is interpreted by
  30. * kprobe handler as:
  31. * 0 - return from kprobe (event is filtered out)
  32. * 1 - store kprobe event into ring buffer
  33. * Other values are reserved and currently alias to 1
  34. */
  35. unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
  36. {
  37. unsigned int ret;
  38. if (in_nmi()) /* not supported yet */
  39. return 1;
  40. preempt_disable();
  41. if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
  42. /*
  43. * since some bpf program is already running on this cpu,
  44. * don't call into another bpf program (same or different)
  45. * and don't send kprobe event into ring-buffer,
  46. * so return zero here
  47. */
  48. ret = 0;
  49. goto out;
  50. }
  51. /*
  52. * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
  53. * to all call sites, we did a bpf_prog_array_valid() there to check
  54. * whether call->prog_array is empty or not, which is
  55. * a heurisitc to speed up execution.
  56. *
  57. * If bpf_prog_array_valid() fetched prog_array was
  58. * non-NULL, we go into trace_call_bpf() and do the actual
  59. * proper rcu_dereference() under RCU lock.
  60. * If it turns out that prog_array is NULL then, we bail out.
  61. * For the opposite, if the bpf_prog_array_valid() fetched pointer
  62. * was NULL, you'll skip the prog_array with the risk of missing
  63. * out of events when it was updated in between this and the
  64. * rcu_dereference() which is accepted risk.
  65. */
  66. ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
  67. out:
  68. __this_cpu_dec(bpf_prog_active);
  69. preempt_enable();
  70. return ret;
  71. }
  72. EXPORT_SYMBOL_GPL(trace_call_bpf);
  73. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  74. BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
  75. {
  76. regs_set_return_value(regs, rc);
  77. override_function_with_return(regs);
  78. return 0;
  79. }
  80. static const struct bpf_func_proto bpf_override_return_proto = {
  81. .func = bpf_override_return,
  82. .gpl_only = true,
  83. .ret_type = RET_INTEGER,
  84. .arg1_type = ARG_PTR_TO_CTX,
  85. .arg2_type = ARG_ANYTHING,
  86. };
  87. #endif
  88. BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
  89. {
  90. int ret;
  91. ret = probe_kernel_read(dst, unsafe_ptr, size);
  92. if (unlikely(ret < 0))
  93. memset(dst, 0, size);
  94. return ret;
  95. }
  96. static const struct bpf_func_proto bpf_probe_read_proto = {
  97. .func = bpf_probe_read,
  98. .gpl_only = true,
  99. .ret_type = RET_INTEGER,
  100. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  101. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  102. .arg3_type = ARG_ANYTHING,
  103. };
  104. BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
  105. u32, size)
  106. {
  107. /*
  108. * Ensure we're in user context which is safe for the helper to
  109. * run. This helper has no business in a kthread.
  110. *
  111. * access_ok() should prevent writing to non-user memory, but in
  112. * some situations (nommu, temporary switch, etc) access_ok() does
  113. * not provide enough validation, hence the check on KERNEL_DS.
  114. */
  115. if (unlikely(in_interrupt() ||
  116. current->flags & (PF_KTHREAD | PF_EXITING)))
  117. return -EPERM;
  118. if (unlikely(uaccess_kernel()))
  119. return -EPERM;
  120. if (!access_ok(VERIFY_WRITE, unsafe_ptr, size))
  121. return -EPERM;
  122. return probe_kernel_write(unsafe_ptr, src, size);
  123. }
  124. static const struct bpf_func_proto bpf_probe_write_user_proto = {
  125. .func = bpf_probe_write_user,
  126. .gpl_only = true,
  127. .ret_type = RET_INTEGER,
  128. .arg1_type = ARG_ANYTHING,
  129. .arg2_type = ARG_PTR_TO_MEM,
  130. .arg3_type = ARG_CONST_SIZE,
  131. };
  132. static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
  133. {
  134. pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
  135. current->comm, task_pid_nr(current));
  136. return &bpf_probe_write_user_proto;
  137. }
  138. /*
  139. * Only limited trace_printk() conversion specifiers allowed:
  140. * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
  141. */
  142. BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
  143. u64, arg2, u64, arg3)
  144. {
  145. bool str_seen = false;
  146. int mod[3] = {};
  147. int fmt_cnt = 0;
  148. u64 unsafe_addr;
  149. char buf[64];
  150. int i;
  151. /*
  152. * bpf_check()->check_func_arg()->check_stack_boundary()
  153. * guarantees that fmt points to bpf program stack,
  154. * fmt_size bytes of it were initialized and fmt_size > 0
  155. */
  156. if (fmt[--fmt_size] != 0)
  157. return -EINVAL;
  158. /* check format string for allowed specifiers */
  159. for (i = 0; i < fmt_size; i++) {
  160. if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
  161. return -EINVAL;
  162. if (fmt[i] != '%')
  163. continue;
  164. if (fmt_cnt >= 3)
  165. return -EINVAL;
  166. /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
  167. i++;
  168. if (fmt[i] == 'l') {
  169. mod[fmt_cnt]++;
  170. i++;
  171. } else if (fmt[i] == 'p' || fmt[i] == 's') {
  172. mod[fmt_cnt]++;
  173. i++;
  174. if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
  175. return -EINVAL;
  176. fmt_cnt++;
  177. if (fmt[i - 1] == 's') {
  178. if (str_seen)
  179. /* allow only one '%s' per fmt string */
  180. return -EINVAL;
  181. str_seen = true;
  182. switch (fmt_cnt) {
  183. case 1:
  184. unsafe_addr = arg1;
  185. arg1 = (long) buf;
  186. break;
  187. case 2:
  188. unsafe_addr = arg2;
  189. arg2 = (long) buf;
  190. break;
  191. case 3:
  192. unsafe_addr = arg3;
  193. arg3 = (long) buf;
  194. break;
  195. }
  196. buf[0] = 0;
  197. strncpy_from_unsafe(buf,
  198. (void *) (long) unsafe_addr,
  199. sizeof(buf));
  200. }
  201. continue;
  202. }
  203. if (fmt[i] == 'l') {
  204. mod[fmt_cnt]++;
  205. i++;
  206. }
  207. if (fmt[i] != 'i' && fmt[i] != 'd' &&
  208. fmt[i] != 'u' && fmt[i] != 'x')
  209. return -EINVAL;
  210. fmt_cnt++;
  211. }
  212. /* Horrid workaround for getting va_list handling working with different
  213. * argument type combinations generically for 32 and 64 bit archs.
  214. */
  215. #define __BPF_TP_EMIT() __BPF_ARG3_TP()
  216. #define __BPF_TP(...) \
  217. __trace_printk(0 /* Fake ip */, \
  218. fmt, ##__VA_ARGS__)
  219. #define __BPF_ARG1_TP(...) \
  220. ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
  221. ? __BPF_TP(arg1, ##__VA_ARGS__) \
  222. : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
  223. ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
  224. : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
  225. #define __BPF_ARG2_TP(...) \
  226. ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
  227. ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
  228. : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
  229. ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
  230. : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
  231. #define __BPF_ARG3_TP(...) \
  232. ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
  233. ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
  234. : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
  235. ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
  236. : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
  237. return __BPF_TP_EMIT();
  238. }
  239. static const struct bpf_func_proto bpf_trace_printk_proto = {
  240. .func = bpf_trace_printk,
  241. .gpl_only = true,
  242. .ret_type = RET_INTEGER,
  243. .arg1_type = ARG_PTR_TO_MEM,
  244. .arg2_type = ARG_CONST_SIZE,
  245. };
  246. const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
  247. {
  248. /*
  249. * this program might be calling bpf_trace_printk,
  250. * so allocate per-cpu printk buffers
  251. */
  252. trace_printk_init_buffers();
  253. return &bpf_trace_printk_proto;
  254. }
  255. static __always_inline int
  256. get_map_perf_counter(struct bpf_map *map, u64 flags,
  257. u64 *value, u64 *enabled, u64 *running)
  258. {
  259. struct bpf_array *array = container_of(map, struct bpf_array, map);
  260. unsigned int cpu = smp_processor_id();
  261. u64 index = flags & BPF_F_INDEX_MASK;
  262. struct bpf_event_entry *ee;
  263. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  264. return -EINVAL;
  265. if (index == BPF_F_CURRENT_CPU)
  266. index = cpu;
  267. if (unlikely(index >= array->map.max_entries))
  268. return -E2BIG;
  269. ee = READ_ONCE(array->ptrs[index]);
  270. if (!ee)
  271. return -ENOENT;
  272. return perf_event_read_local(ee->event, value, enabled, running);
  273. }
  274. BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
  275. {
  276. u64 value = 0;
  277. int err;
  278. err = get_map_perf_counter(map, flags, &value, NULL, NULL);
  279. /*
  280. * this api is ugly since we miss [-22..-2] range of valid
  281. * counter values, but that's uapi
  282. */
  283. if (err)
  284. return err;
  285. return value;
  286. }
  287. static const struct bpf_func_proto bpf_perf_event_read_proto = {
  288. .func = bpf_perf_event_read,
  289. .gpl_only = true,
  290. .ret_type = RET_INTEGER,
  291. .arg1_type = ARG_CONST_MAP_PTR,
  292. .arg2_type = ARG_ANYTHING,
  293. };
  294. BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
  295. struct bpf_perf_event_value *, buf, u32, size)
  296. {
  297. int err = -EINVAL;
  298. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  299. goto clear;
  300. err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
  301. &buf->running);
  302. if (unlikely(err))
  303. goto clear;
  304. return 0;
  305. clear:
  306. memset(buf, 0, size);
  307. return err;
  308. }
  309. static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
  310. .func = bpf_perf_event_read_value,
  311. .gpl_only = true,
  312. .ret_type = RET_INTEGER,
  313. .arg1_type = ARG_CONST_MAP_PTR,
  314. .arg2_type = ARG_ANYTHING,
  315. .arg3_type = ARG_PTR_TO_UNINIT_MEM,
  316. .arg4_type = ARG_CONST_SIZE,
  317. };
  318. static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd);
  319. static __always_inline u64
  320. __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
  321. u64 flags, struct perf_sample_data *sd)
  322. {
  323. struct bpf_array *array = container_of(map, struct bpf_array, map);
  324. unsigned int cpu = smp_processor_id();
  325. u64 index = flags & BPF_F_INDEX_MASK;
  326. struct bpf_event_entry *ee;
  327. struct perf_event *event;
  328. if (index == BPF_F_CURRENT_CPU)
  329. index = cpu;
  330. if (unlikely(index >= array->map.max_entries))
  331. return -E2BIG;
  332. ee = READ_ONCE(array->ptrs[index]);
  333. if (!ee)
  334. return -ENOENT;
  335. event = ee->event;
  336. if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
  337. event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
  338. return -EINVAL;
  339. if (unlikely(event->oncpu != cpu))
  340. return -EOPNOTSUPP;
  341. perf_event_output(event, sd, regs);
  342. return 0;
  343. }
  344. BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
  345. u64, flags, void *, data, u64, size)
  346. {
  347. struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd);
  348. struct perf_raw_record raw = {
  349. .frag = {
  350. .size = size,
  351. .data = data,
  352. },
  353. };
  354. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  355. return -EINVAL;
  356. perf_sample_data_init(sd, 0, 0);
  357. sd->raw = &raw;
  358. return __bpf_perf_event_output(regs, map, flags, sd);
  359. }
  360. static const struct bpf_func_proto bpf_perf_event_output_proto = {
  361. .func = bpf_perf_event_output,
  362. .gpl_only = true,
  363. .ret_type = RET_INTEGER,
  364. .arg1_type = ARG_PTR_TO_CTX,
  365. .arg2_type = ARG_CONST_MAP_PTR,
  366. .arg3_type = ARG_ANYTHING,
  367. .arg4_type = ARG_PTR_TO_MEM,
  368. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  369. };
  370. static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
  371. static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd);
  372. u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  373. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
  374. {
  375. struct perf_sample_data *sd = this_cpu_ptr(&bpf_misc_sd);
  376. struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
  377. struct perf_raw_frag frag = {
  378. .copy = ctx_copy,
  379. .size = ctx_size,
  380. .data = ctx,
  381. };
  382. struct perf_raw_record raw = {
  383. .frag = {
  384. {
  385. .next = ctx_size ? &frag : NULL,
  386. },
  387. .size = meta_size,
  388. .data = meta,
  389. },
  390. };
  391. perf_fetch_caller_regs(regs);
  392. perf_sample_data_init(sd, 0, 0);
  393. sd->raw = &raw;
  394. return __bpf_perf_event_output(regs, map, flags, sd);
  395. }
  396. BPF_CALL_0(bpf_get_current_task)
  397. {
  398. return (long) current;
  399. }
  400. static const struct bpf_func_proto bpf_get_current_task_proto = {
  401. .func = bpf_get_current_task,
  402. .gpl_only = true,
  403. .ret_type = RET_INTEGER,
  404. };
  405. BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
  406. {
  407. struct bpf_array *array = container_of(map, struct bpf_array, map);
  408. struct cgroup *cgrp;
  409. if (unlikely(in_interrupt()))
  410. return -EINVAL;
  411. if (unlikely(idx >= array->map.max_entries))
  412. return -E2BIG;
  413. cgrp = READ_ONCE(array->ptrs[idx]);
  414. if (unlikely(!cgrp))
  415. return -EAGAIN;
  416. return task_under_cgroup_hierarchy(current, cgrp);
  417. }
  418. static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
  419. .func = bpf_current_task_under_cgroup,
  420. .gpl_only = false,
  421. .ret_type = RET_INTEGER,
  422. .arg1_type = ARG_CONST_MAP_PTR,
  423. .arg2_type = ARG_ANYTHING,
  424. };
  425. BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
  426. const void *, unsafe_ptr)
  427. {
  428. int ret;
  429. /*
  430. * The strncpy_from_unsafe() call will likely not fill the entire
  431. * buffer, but that's okay in this circumstance as we're probing
  432. * arbitrary memory anyway similar to bpf_probe_read() and might
  433. * as well probe the stack. Thus, memory is explicitly cleared
  434. * only in error case, so that improper users ignoring return
  435. * code altogether don't copy garbage; otherwise length of string
  436. * is returned that can be used for bpf_perf_event_output() et al.
  437. */
  438. ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
  439. if (unlikely(ret < 0))
  440. memset(dst, 0, size);
  441. return ret;
  442. }
  443. static const struct bpf_func_proto bpf_probe_read_str_proto = {
  444. .func = bpf_probe_read_str,
  445. .gpl_only = true,
  446. .ret_type = RET_INTEGER,
  447. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  448. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  449. .arg3_type = ARG_ANYTHING,
  450. };
  451. static const struct bpf_func_proto *
  452. tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  453. {
  454. switch (func_id) {
  455. case BPF_FUNC_map_lookup_elem:
  456. return &bpf_map_lookup_elem_proto;
  457. case BPF_FUNC_map_update_elem:
  458. return &bpf_map_update_elem_proto;
  459. case BPF_FUNC_map_delete_elem:
  460. return &bpf_map_delete_elem_proto;
  461. case BPF_FUNC_probe_read:
  462. return &bpf_probe_read_proto;
  463. case BPF_FUNC_ktime_get_ns:
  464. return &bpf_ktime_get_ns_proto;
  465. case BPF_FUNC_tail_call:
  466. return &bpf_tail_call_proto;
  467. case BPF_FUNC_get_current_pid_tgid:
  468. return &bpf_get_current_pid_tgid_proto;
  469. case BPF_FUNC_get_current_task:
  470. return &bpf_get_current_task_proto;
  471. case BPF_FUNC_get_current_uid_gid:
  472. return &bpf_get_current_uid_gid_proto;
  473. case BPF_FUNC_get_current_comm:
  474. return &bpf_get_current_comm_proto;
  475. case BPF_FUNC_trace_printk:
  476. return bpf_get_trace_printk_proto();
  477. case BPF_FUNC_get_smp_processor_id:
  478. return &bpf_get_smp_processor_id_proto;
  479. case BPF_FUNC_get_numa_node_id:
  480. return &bpf_get_numa_node_id_proto;
  481. case BPF_FUNC_perf_event_read:
  482. return &bpf_perf_event_read_proto;
  483. case BPF_FUNC_probe_write_user:
  484. return bpf_get_probe_write_proto();
  485. case BPF_FUNC_current_task_under_cgroup:
  486. return &bpf_current_task_under_cgroup_proto;
  487. case BPF_FUNC_get_prandom_u32:
  488. return &bpf_get_prandom_u32_proto;
  489. case BPF_FUNC_probe_read_str:
  490. return &bpf_probe_read_str_proto;
  491. default:
  492. return NULL;
  493. }
  494. }
  495. static const struct bpf_func_proto *
  496. kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  497. {
  498. switch (func_id) {
  499. case BPF_FUNC_perf_event_output:
  500. return &bpf_perf_event_output_proto;
  501. case BPF_FUNC_get_stackid:
  502. return &bpf_get_stackid_proto;
  503. case BPF_FUNC_perf_event_read_value:
  504. return &bpf_perf_event_read_value_proto;
  505. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  506. case BPF_FUNC_override_return:
  507. return &bpf_override_return_proto;
  508. #endif
  509. default:
  510. return tracing_func_proto(func_id, prog);
  511. }
  512. }
  513. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  514. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  515. const struct bpf_prog *prog,
  516. struct bpf_insn_access_aux *info)
  517. {
  518. if (off < 0 || off >= sizeof(struct pt_regs))
  519. return false;
  520. if (type != BPF_READ)
  521. return false;
  522. if (off % size != 0)
  523. return false;
  524. /*
  525. * Assertion for 32 bit to make sure last 8 byte access
  526. * (BPF_DW) to the last 4 byte member is disallowed.
  527. */
  528. if (off + size > sizeof(struct pt_regs))
  529. return false;
  530. return true;
  531. }
  532. const struct bpf_verifier_ops kprobe_verifier_ops = {
  533. .get_func_proto = kprobe_prog_func_proto,
  534. .is_valid_access = kprobe_prog_is_valid_access,
  535. };
  536. const struct bpf_prog_ops kprobe_prog_ops = {
  537. };
  538. BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
  539. u64, flags, void *, data, u64, size)
  540. {
  541. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  542. /*
  543. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  544. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  545. * from there and call the same bpf_perf_event_output() helper inline.
  546. */
  547. return ____bpf_perf_event_output(regs, map, flags, data, size);
  548. }
  549. static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
  550. .func = bpf_perf_event_output_tp,
  551. .gpl_only = true,
  552. .ret_type = RET_INTEGER,
  553. .arg1_type = ARG_PTR_TO_CTX,
  554. .arg2_type = ARG_CONST_MAP_PTR,
  555. .arg3_type = ARG_ANYTHING,
  556. .arg4_type = ARG_PTR_TO_MEM,
  557. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  558. };
  559. BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
  560. u64, flags)
  561. {
  562. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  563. /*
  564. * Same comment as in bpf_perf_event_output_tp(), only that this time
  565. * the other helper's function body cannot be inlined due to being
  566. * external, thus we need to call raw helper function.
  567. */
  568. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  569. flags, 0, 0);
  570. }
  571. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  572. .func = bpf_get_stackid_tp,
  573. .gpl_only = true,
  574. .ret_type = RET_INTEGER,
  575. .arg1_type = ARG_PTR_TO_CTX,
  576. .arg2_type = ARG_CONST_MAP_PTR,
  577. .arg3_type = ARG_ANYTHING,
  578. };
  579. static const struct bpf_func_proto *
  580. tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  581. {
  582. switch (func_id) {
  583. case BPF_FUNC_perf_event_output:
  584. return &bpf_perf_event_output_proto_tp;
  585. case BPF_FUNC_get_stackid:
  586. return &bpf_get_stackid_proto_tp;
  587. default:
  588. return tracing_func_proto(func_id, prog);
  589. }
  590. }
  591. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  592. const struct bpf_prog *prog,
  593. struct bpf_insn_access_aux *info)
  594. {
  595. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  596. return false;
  597. if (type != BPF_READ)
  598. return false;
  599. if (off % size != 0)
  600. return false;
  601. BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
  602. return true;
  603. }
  604. const struct bpf_verifier_ops tracepoint_verifier_ops = {
  605. .get_func_proto = tp_prog_func_proto,
  606. .is_valid_access = tp_prog_is_valid_access,
  607. };
  608. const struct bpf_prog_ops tracepoint_prog_ops = {
  609. };
  610. BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
  611. struct bpf_perf_event_value *, buf, u32, size)
  612. {
  613. int err = -EINVAL;
  614. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  615. goto clear;
  616. err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
  617. &buf->running);
  618. if (unlikely(err))
  619. goto clear;
  620. return 0;
  621. clear:
  622. memset(buf, 0, size);
  623. return err;
  624. }
  625. static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
  626. .func = bpf_perf_prog_read_value,
  627. .gpl_only = true,
  628. .ret_type = RET_INTEGER,
  629. .arg1_type = ARG_PTR_TO_CTX,
  630. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  631. .arg3_type = ARG_CONST_SIZE,
  632. };
  633. static const struct bpf_func_proto *
  634. pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  635. {
  636. switch (func_id) {
  637. case BPF_FUNC_perf_event_output:
  638. return &bpf_perf_event_output_proto_tp;
  639. case BPF_FUNC_get_stackid:
  640. return &bpf_get_stackid_proto_tp;
  641. case BPF_FUNC_perf_prog_read_value:
  642. return &bpf_perf_prog_read_value_proto;
  643. default:
  644. return tracing_func_proto(func_id, prog);
  645. }
  646. }
  647. /*
  648. * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
  649. * to avoid potential recursive reuse issue when/if tracepoints are added
  650. * inside bpf_*_event_output and/or bpf_get_stack_id
  651. */
  652. static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs);
  653. BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
  654. struct bpf_map *, map, u64, flags, void *, data, u64, size)
  655. {
  656. struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
  657. perf_fetch_caller_regs(regs);
  658. return ____bpf_perf_event_output(regs, map, flags, data, size);
  659. }
  660. static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
  661. .func = bpf_perf_event_output_raw_tp,
  662. .gpl_only = true,
  663. .ret_type = RET_INTEGER,
  664. .arg1_type = ARG_PTR_TO_CTX,
  665. .arg2_type = ARG_CONST_MAP_PTR,
  666. .arg3_type = ARG_ANYTHING,
  667. .arg4_type = ARG_PTR_TO_MEM,
  668. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  669. };
  670. BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
  671. struct bpf_map *, map, u64, flags)
  672. {
  673. struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
  674. perf_fetch_caller_regs(regs);
  675. /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
  676. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  677. flags, 0, 0);
  678. }
  679. static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
  680. .func = bpf_get_stackid_raw_tp,
  681. .gpl_only = true,
  682. .ret_type = RET_INTEGER,
  683. .arg1_type = ARG_PTR_TO_CTX,
  684. .arg2_type = ARG_CONST_MAP_PTR,
  685. .arg3_type = ARG_ANYTHING,
  686. };
  687. static const struct bpf_func_proto *
  688. raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  689. {
  690. switch (func_id) {
  691. case BPF_FUNC_perf_event_output:
  692. return &bpf_perf_event_output_proto_raw_tp;
  693. case BPF_FUNC_get_stackid:
  694. return &bpf_get_stackid_proto_raw_tp;
  695. default:
  696. return tracing_func_proto(func_id, prog);
  697. }
  698. }
  699. static bool raw_tp_prog_is_valid_access(int off, int size,
  700. enum bpf_access_type type,
  701. const struct bpf_prog *prog,
  702. struct bpf_insn_access_aux *info)
  703. {
  704. /* largest tracepoint in the kernel has 12 args */
  705. if (off < 0 || off >= sizeof(__u64) * 12)
  706. return false;
  707. if (type != BPF_READ)
  708. return false;
  709. if (off % size != 0)
  710. return false;
  711. return true;
  712. }
  713. const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
  714. .get_func_proto = raw_tp_prog_func_proto,
  715. .is_valid_access = raw_tp_prog_is_valid_access,
  716. };
  717. const struct bpf_prog_ops raw_tracepoint_prog_ops = {
  718. };
  719. static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  720. const struct bpf_prog *prog,
  721. struct bpf_insn_access_aux *info)
  722. {
  723. const int size_u64 = sizeof(u64);
  724. if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
  725. return false;
  726. if (type != BPF_READ)
  727. return false;
  728. if (off % size != 0)
  729. return false;
  730. switch (off) {
  731. case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
  732. bpf_ctx_record_field_size(info, size_u64);
  733. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  734. return false;
  735. break;
  736. case bpf_ctx_range(struct bpf_perf_event_data, addr):
  737. bpf_ctx_record_field_size(info, size_u64);
  738. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  739. return false;
  740. break;
  741. default:
  742. if (size != sizeof(long))
  743. return false;
  744. }
  745. return true;
  746. }
  747. static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
  748. const struct bpf_insn *si,
  749. struct bpf_insn *insn_buf,
  750. struct bpf_prog *prog, u32 *target_size)
  751. {
  752. struct bpf_insn *insn = insn_buf;
  753. switch (si->off) {
  754. case offsetof(struct bpf_perf_event_data, sample_period):
  755. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  756. data), si->dst_reg, si->src_reg,
  757. offsetof(struct bpf_perf_event_data_kern, data));
  758. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  759. bpf_target_off(struct perf_sample_data, period, 8,
  760. target_size));
  761. break;
  762. case offsetof(struct bpf_perf_event_data, addr):
  763. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  764. data), si->dst_reg, si->src_reg,
  765. offsetof(struct bpf_perf_event_data_kern, data));
  766. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  767. bpf_target_off(struct perf_sample_data, addr, 8,
  768. target_size));
  769. break;
  770. default:
  771. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  772. regs), si->dst_reg, si->src_reg,
  773. offsetof(struct bpf_perf_event_data_kern, regs));
  774. *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
  775. si->off);
  776. break;
  777. }
  778. return insn - insn_buf;
  779. }
  780. const struct bpf_verifier_ops perf_event_verifier_ops = {
  781. .get_func_proto = pe_prog_func_proto,
  782. .is_valid_access = pe_prog_is_valid_access,
  783. .convert_ctx_access = pe_prog_convert_ctx_access,
  784. };
  785. const struct bpf_prog_ops perf_event_prog_ops = {
  786. };
  787. static DEFINE_MUTEX(bpf_event_mutex);
  788. #define BPF_TRACE_MAX_PROGS 64
  789. int perf_event_attach_bpf_prog(struct perf_event *event,
  790. struct bpf_prog *prog)
  791. {
  792. struct bpf_prog_array __rcu *old_array;
  793. struct bpf_prog_array *new_array;
  794. int ret = -EEXIST;
  795. /*
  796. * Kprobe override only works if they are on the function entry,
  797. * and only if they are on the opt-in list.
  798. */
  799. if (prog->kprobe_override &&
  800. (!trace_kprobe_on_func_entry(event->tp_event) ||
  801. !trace_kprobe_error_injectable(event->tp_event)))
  802. return -EINVAL;
  803. mutex_lock(&bpf_event_mutex);
  804. if (event->prog)
  805. goto unlock;
  806. old_array = event->tp_event->prog_array;
  807. if (old_array &&
  808. bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
  809. ret = -E2BIG;
  810. goto unlock;
  811. }
  812. ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
  813. if (ret < 0)
  814. goto unlock;
  815. /* set the new array to event->tp_event and set event->prog */
  816. event->prog = prog;
  817. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  818. bpf_prog_array_free(old_array);
  819. unlock:
  820. mutex_unlock(&bpf_event_mutex);
  821. return ret;
  822. }
  823. void perf_event_detach_bpf_prog(struct perf_event *event)
  824. {
  825. struct bpf_prog_array __rcu *old_array;
  826. struct bpf_prog_array *new_array;
  827. int ret;
  828. mutex_lock(&bpf_event_mutex);
  829. if (!event->prog)
  830. goto unlock;
  831. old_array = event->tp_event->prog_array;
  832. ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
  833. if (ret < 0) {
  834. bpf_prog_array_delete_safe(old_array, event->prog);
  835. } else {
  836. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  837. bpf_prog_array_free(old_array);
  838. }
  839. bpf_prog_put(event->prog);
  840. event->prog = NULL;
  841. unlock:
  842. mutex_unlock(&bpf_event_mutex);
  843. }
  844. int perf_event_query_prog_array(struct perf_event *event, void __user *info)
  845. {
  846. struct perf_event_query_bpf __user *uquery = info;
  847. struct perf_event_query_bpf query = {};
  848. int ret;
  849. if (!capable(CAP_SYS_ADMIN))
  850. return -EPERM;
  851. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  852. return -EINVAL;
  853. if (copy_from_user(&query, uquery, sizeof(query)))
  854. return -EFAULT;
  855. if (query.ids_len > BPF_TRACE_MAX_PROGS)
  856. return -E2BIG;
  857. mutex_lock(&bpf_event_mutex);
  858. ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
  859. uquery->ids,
  860. query.ids_len,
  861. &uquery->prog_cnt);
  862. mutex_unlock(&bpf_event_mutex);
  863. return ret;
  864. }
  865. extern struct bpf_raw_event_map __start__bpf_raw_tp[];
  866. extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
  867. struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name)
  868. {
  869. struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
  870. for (; btp < __stop__bpf_raw_tp; btp++) {
  871. if (!strcmp(btp->tp->name, name))
  872. return btp;
  873. }
  874. return NULL;
  875. }
  876. static __always_inline
  877. void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
  878. {
  879. rcu_read_lock();
  880. preempt_disable();
  881. (void) BPF_PROG_RUN(prog, args);
  882. preempt_enable();
  883. rcu_read_unlock();
  884. }
  885. #define UNPACK(...) __VA_ARGS__
  886. #define REPEAT_1(FN, DL, X, ...) FN(X)
  887. #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
  888. #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
  889. #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
  890. #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
  891. #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
  892. #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
  893. #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
  894. #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
  895. #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
  896. #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
  897. #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
  898. #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
  899. #define SARG(X) u64 arg##X
  900. #define COPY(X) args[X] = arg##X
  901. #define __DL_COM (,)
  902. #define __DL_SEM (;)
  903. #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  904. #define BPF_TRACE_DEFN_x(x) \
  905. void bpf_trace_run##x(struct bpf_prog *prog, \
  906. REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
  907. { \
  908. u64 args[x]; \
  909. REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
  910. __bpf_trace_run(prog, args); \
  911. } \
  912. EXPORT_SYMBOL_GPL(bpf_trace_run##x)
  913. BPF_TRACE_DEFN_x(1);
  914. BPF_TRACE_DEFN_x(2);
  915. BPF_TRACE_DEFN_x(3);
  916. BPF_TRACE_DEFN_x(4);
  917. BPF_TRACE_DEFN_x(5);
  918. BPF_TRACE_DEFN_x(6);
  919. BPF_TRACE_DEFN_x(7);
  920. BPF_TRACE_DEFN_x(8);
  921. BPF_TRACE_DEFN_x(9);
  922. BPF_TRACE_DEFN_x(10);
  923. BPF_TRACE_DEFN_x(11);
  924. BPF_TRACE_DEFN_x(12);
  925. static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  926. {
  927. struct tracepoint *tp = btp->tp;
  928. /*
  929. * check that program doesn't access arguments beyond what's
  930. * available in this tracepoint
  931. */
  932. if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
  933. return -EINVAL;
  934. return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
  935. }
  936. int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  937. {
  938. int err;
  939. mutex_lock(&bpf_event_mutex);
  940. err = __bpf_probe_register(btp, prog);
  941. mutex_unlock(&bpf_event_mutex);
  942. return err;
  943. }
  944. int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  945. {
  946. int err;
  947. mutex_lock(&bpf_event_mutex);
  948. err = tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
  949. mutex_unlock(&bpf_event_mutex);
  950. return err;
  951. }