bpf_trace.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. static DEFINE_PER_CPU(int, bpf_prog_active);
  16. /**
  17. * trace_call_bpf - invoke BPF program
  18. * @prog: BPF program
  19. * @ctx: opaque context pointer
  20. *
  21. * kprobe handlers execute BPF programs via this helper.
  22. * Can be used from static tracepoints in the future.
  23. *
  24. * Return: BPF programs always return an integer which is interpreted by
  25. * kprobe handler as:
  26. * 0 - return from kprobe (event is filtered out)
  27. * 1 - store kprobe event into ring buffer
  28. * Other values are reserved and currently alias to 1
  29. */
  30. unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx)
  31. {
  32. unsigned int ret;
  33. if (in_nmi()) /* not supported yet */
  34. return 1;
  35. preempt_disable();
  36. if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
  37. /*
  38. * since some bpf program is already running on this cpu,
  39. * don't call into another bpf program (same or different)
  40. * and don't send kprobe event into ring-buffer,
  41. * so return zero here
  42. */
  43. ret = 0;
  44. goto out;
  45. }
  46. rcu_read_lock();
  47. ret = BPF_PROG_RUN(prog, ctx);
  48. rcu_read_unlock();
  49. out:
  50. __this_cpu_dec(bpf_prog_active);
  51. preempt_enable();
  52. return ret;
  53. }
  54. EXPORT_SYMBOL_GPL(trace_call_bpf);
  55. static u64 bpf_probe_read(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  56. {
  57. void *dst = (void *) (long) r1;
  58. int size = (int) r2;
  59. void *unsafe_ptr = (void *) (long) r3;
  60. return probe_kernel_read(dst, unsafe_ptr, size);
  61. }
  62. static const struct bpf_func_proto bpf_probe_read_proto = {
  63. .func = bpf_probe_read,
  64. .gpl_only = true,
  65. .ret_type = RET_INTEGER,
  66. .arg1_type = ARG_PTR_TO_STACK,
  67. .arg2_type = ARG_CONST_STACK_SIZE,
  68. .arg3_type = ARG_ANYTHING,
  69. };
  70. static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  71. {
  72. /* NMI safe access to clock monotonic */
  73. return ktime_get_mono_fast_ns();
  74. }
  75. static const struct bpf_func_proto bpf_ktime_get_ns_proto = {
  76. .func = bpf_ktime_get_ns,
  77. .gpl_only = true,
  78. .ret_type = RET_INTEGER,
  79. };
  80. /*
  81. * limited trace_printk()
  82. * only %d %u %x %ld %lu %lx %lld %llu %llx %p conversion specifiers allowed
  83. */
  84. static u64 bpf_trace_printk(u64 r1, u64 fmt_size, u64 r3, u64 r4, u64 r5)
  85. {
  86. char *fmt = (char *) (long) r1;
  87. int mod[3] = {};
  88. int fmt_cnt = 0;
  89. int i;
  90. /*
  91. * bpf_check()->check_func_arg()->check_stack_boundary()
  92. * guarantees that fmt points to bpf program stack,
  93. * fmt_size bytes of it were initialized and fmt_size > 0
  94. */
  95. if (fmt[--fmt_size] != 0)
  96. return -EINVAL;
  97. /* check format string for allowed specifiers */
  98. for (i = 0; i < fmt_size; i++) {
  99. if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
  100. return -EINVAL;
  101. if (fmt[i] != '%')
  102. continue;
  103. if (fmt_cnt >= 3)
  104. return -EINVAL;
  105. /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
  106. i++;
  107. if (fmt[i] == 'l') {
  108. mod[fmt_cnt]++;
  109. i++;
  110. } else if (fmt[i] == 'p') {
  111. mod[fmt_cnt]++;
  112. i++;
  113. if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
  114. return -EINVAL;
  115. fmt_cnt++;
  116. continue;
  117. }
  118. if (fmt[i] == 'l') {
  119. mod[fmt_cnt]++;
  120. i++;
  121. }
  122. if (fmt[i] != 'd' && fmt[i] != 'u' && fmt[i] != 'x')
  123. return -EINVAL;
  124. fmt_cnt++;
  125. }
  126. return __trace_printk(1/* fake ip will not be printed */, fmt,
  127. mod[0] == 2 ? r3 : mod[0] == 1 ? (long) r3 : (u32) r3,
  128. mod[1] == 2 ? r4 : mod[1] == 1 ? (long) r4 : (u32) r4,
  129. mod[2] == 2 ? r5 : mod[2] == 1 ? (long) r5 : (u32) r5);
  130. }
  131. static const struct bpf_func_proto bpf_trace_printk_proto = {
  132. .func = bpf_trace_printk,
  133. .gpl_only = true,
  134. .ret_type = RET_INTEGER,
  135. .arg1_type = ARG_PTR_TO_STACK,
  136. .arg2_type = ARG_CONST_STACK_SIZE,
  137. };
  138. static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
  139. {
  140. switch (func_id) {
  141. case BPF_FUNC_map_lookup_elem:
  142. return &bpf_map_lookup_elem_proto;
  143. case BPF_FUNC_map_update_elem:
  144. return &bpf_map_update_elem_proto;
  145. case BPF_FUNC_map_delete_elem:
  146. return &bpf_map_delete_elem_proto;
  147. case BPF_FUNC_probe_read:
  148. return &bpf_probe_read_proto;
  149. case BPF_FUNC_ktime_get_ns:
  150. return &bpf_ktime_get_ns_proto;
  151. case BPF_FUNC_trace_printk:
  152. /*
  153. * this program might be calling bpf_trace_printk,
  154. * so allocate per-cpu printk buffers
  155. */
  156. trace_printk_init_buffers();
  157. return &bpf_trace_printk_proto;
  158. default:
  159. return NULL;
  160. }
  161. }
  162. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  163. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type)
  164. {
  165. /* check bounds */
  166. if (off < 0 || off >= sizeof(struct pt_regs))
  167. return false;
  168. /* only read is allowed */
  169. if (type != BPF_READ)
  170. return false;
  171. /* disallow misaligned access */
  172. if (off % size != 0)
  173. return false;
  174. return true;
  175. }
  176. static struct bpf_verifier_ops kprobe_prog_ops = {
  177. .get_func_proto = kprobe_prog_func_proto,
  178. .is_valid_access = kprobe_prog_is_valid_access,
  179. };
  180. static struct bpf_prog_type_list kprobe_tl = {
  181. .ops = &kprobe_prog_ops,
  182. .type = BPF_PROG_TYPE_KPROBE,
  183. };
  184. static int __init register_kprobe_prog_ops(void)
  185. {
  186. bpf_register_prog_type(&kprobe_tl);
  187. return 0;
  188. }
  189. late_initcall(register_kprobe_prog_ops);