bpf_trace.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. u32 *ids, prog_cnt, ids_len;
  849. int ret;
  850. if (!capable(CAP_SYS_ADMIN))
  851. return -EPERM;
  852. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  853. return -EINVAL;
  854. if (copy_from_user(&query, uquery, sizeof(query)))
  855. return -EFAULT;
  856. ids_len = query.ids_len;
  857. if (ids_len > BPF_TRACE_MAX_PROGS)
  858. return -E2BIG;
  859. ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
  860. if (!ids)
  861. return -ENOMEM;
  862. /*
  863. * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
  864. * is required when user only wants to check for uquery->prog_cnt.
  865. * There is no need to check for it since the case is handled
  866. * gracefully in bpf_prog_array_copy_info.
  867. */
  868. mutex_lock(&bpf_event_mutex);
  869. ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
  870. ids,
  871. ids_len,
  872. &prog_cnt);
  873. mutex_unlock(&bpf_event_mutex);
  874. if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
  875. copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
  876. ret = -EFAULT;
  877. kfree(ids);
  878. return ret;
  879. }
  880. extern struct bpf_raw_event_map __start__bpf_raw_tp[];
  881. extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
  882. struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name)
  883. {
  884. struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
  885. for (; btp < __stop__bpf_raw_tp; btp++) {
  886. if (!strcmp(btp->tp->name, name))
  887. return btp;
  888. }
  889. return NULL;
  890. }
  891. static __always_inline
  892. void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
  893. {
  894. rcu_read_lock();
  895. preempt_disable();
  896. (void) BPF_PROG_RUN(prog, args);
  897. preempt_enable();
  898. rcu_read_unlock();
  899. }
  900. #define UNPACK(...) __VA_ARGS__
  901. #define REPEAT_1(FN, DL, X, ...) FN(X)
  902. #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
  903. #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
  904. #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
  905. #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
  906. #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
  907. #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
  908. #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
  909. #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
  910. #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
  911. #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
  912. #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
  913. #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
  914. #define SARG(X) u64 arg##X
  915. #define COPY(X) args[X] = arg##X
  916. #define __DL_COM (,)
  917. #define __DL_SEM (;)
  918. #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  919. #define BPF_TRACE_DEFN_x(x) \
  920. void bpf_trace_run##x(struct bpf_prog *prog, \
  921. REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
  922. { \
  923. u64 args[x]; \
  924. REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
  925. __bpf_trace_run(prog, args); \
  926. } \
  927. EXPORT_SYMBOL_GPL(bpf_trace_run##x)
  928. BPF_TRACE_DEFN_x(1);
  929. BPF_TRACE_DEFN_x(2);
  930. BPF_TRACE_DEFN_x(3);
  931. BPF_TRACE_DEFN_x(4);
  932. BPF_TRACE_DEFN_x(5);
  933. BPF_TRACE_DEFN_x(6);
  934. BPF_TRACE_DEFN_x(7);
  935. BPF_TRACE_DEFN_x(8);
  936. BPF_TRACE_DEFN_x(9);
  937. BPF_TRACE_DEFN_x(10);
  938. BPF_TRACE_DEFN_x(11);
  939. BPF_TRACE_DEFN_x(12);
  940. static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  941. {
  942. struct tracepoint *tp = btp->tp;
  943. /*
  944. * check that program doesn't access arguments beyond what's
  945. * available in this tracepoint
  946. */
  947. if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
  948. return -EINVAL;
  949. return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
  950. }
  951. int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  952. {
  953. int err;
  954. mutex_lock(&bpf_event_mutex);
  955. err = __bpf_probe_register(btp, prog);
  956. mutex_unlock(&bpf_event_mutex);
  957. return err;
  958. }
  959. int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  960. {
  961. int err;
  962. mutex_lock(&bpf_event_mutex);
  963. err = tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
  964. mutex_unlock(&bpf_event_mutex);
  965. return err;
  966. }