bpf_trace.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /*
  71. * limited trace_printk()
  72. * only %d %u %x %ld %lu %lx %lld %llu %llx %p conversion specifiers allowed
  73. */
  74. static u64 bpf_trace_printk(u64 r1, u64 fmt_size, u64 r3, u64 r4, u64 r5)
  75. {
  76. char *fmt = (char *) (long) r1;
  77. int mod[3] = {};
  78. int fmt_cnt = 0;
  79. int i;
  80. /*
  81. * bpf_check()->check_func_arg()->check_stack_boundary()
  82. * guarantees that fmt points to bpf program stack,
  83. * fmt_size bytes of it were initialized and fmt_size > 0
  84. */
  85. if (fmt[--fmt_size] != 0)
  86. return -EINVAL;
  87. /* check format string for allowed specifiers */
  88. for (i = 0; i < fmt_size; i++) {
  89. if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
  90. return -EINVAL;
  91. if (fmt[i] != '%')
  92. continue;
  93. if (fmt_cnt >= 3)
  94. return -EINVAL;
  95. /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
  96. i++;
  97. if (fmt[i] == 'l') {
  98. mod[fmt_cnt]++;
  99. i++;
  100. } else if (fmt[i] == 'p') {
  101. mod[fmt_cnt]++;
  102. i++;
  103. if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
  104. return -EINVAL;
  105. fmt_cnt++;
  106. continue;
  107. }
  108. if (fmt[i] == 'l') {
  109. mod[fmt_cnt]++;
  110. i++;
  111. }
  112. if (fmt[i] != 'd' && fmt[i] != 'u' && fmt[i] != 'x')
  113. return -EINVAL;
  114. fmt_cnt++;
  115. }
  116. return __trace_printk(1/* fake ip will not be printed */, fmt,
  117. mod[0] == 2 ? r3 : mod[0] == 1 ? (long) r3 : (u32) r3,
  118. mod[1] == 2 ? r4 : mod[1] == 1 ? (long) r4 : (u32) r4,
  119. mod[2] == 2 ? r5 : mod[2] == 1 ? (long) r5 : (u32) r5);
  120. }
  121. static const struct bpf_func_proto bpf_trace_printk_proto = {
  122. .func = bpf_trace_printk,
  123. .gpl_only = true,
  124. .ret_type = RET_INTEGER,
  125. .arg1_type = ARG_PTR_TO_STACK,
  126. .arg2_type = ARG_CONST_STACK_SIZE,
  127. };
  128. const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
  129. {
  130. /*
  131. * this program might be calling bpf_trace_printk,
  132. * so allocate per-cpu printk buffers
  133. */
  134. trace_printk_init_buffers();
  135. return &bpf_trace_printk_proto;
  136. }
  137. static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
  138. {
  139. switch (func_id) {
  140. case BPF_FUNC_map_lookup_elem:
  141. return &bpf_map_lookup_elem_proto;
  142. case BPF_FUNC_map_update_elem:
  143. return &bpf_map_update_elem_proto;
  144. case BPF_FUNC_map_delete_elem:
  145. return &bpf_map_delete_elem_proto;
  146. case BPF_FUNC_probe_read:
  147. return &bpf_probe_read_proto;
  148. case BPF_FUNC_ktime_get_ns:
  149. return &bpf_ktime_get_ns_proto;
  150. case BPF_FUNC_tail_call:
  151. return &bpf_tail_call_proto;
  152. case BPF_FUNC_get_current_pid_tgid:
  153. return &bpf_get_current_pid_tgid_proto;
  154. case BPF_FUNC_get_current_uid_gid:
  155. return &bpf_get_current_uid_gid_proto;
  156. case BPF_FUNC_get_current_comm:
  157. return &bpf_get_current_comm_proto;
  158. case BPF_FUNC_trace_printk:
  159. return bpf_get_trace_printk_proto();
  160. case BPF_FUNC_get_smp_processor_id:
  161. return &bpf_get_smp_processor_id_proto;
  162. default:
  163. return NULL;
  164. }
  165. }
  166. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  167. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type)
  168. {
  169. /* check bounds */
  170. if (off < 0 || off >= sizeof(struct pt_regs))
  171. return false;
  172. /* only read is allowed */
  173. if (type != BPF_READ)
  174. return false;
  175. /* disallow misaligned access */
  176. if (off % size != 0)
  177. return false;
  178. return true;
  179. }
  180. static struct bpf_verifier_ops kprobe_prog_ops = {
  181. .get_func_proto = kprobe_prog_func_proto,
  182. .is_valid_access = kprobe_prog_is_valid_access,
  183. };
  184. static struct bpf_prog_type_list kprobe_tl = {
  185. .ops = &kprobe_prog_ops,
  186. .type = BPF_PROG_TYPE_KPROBE,
  187. };
  188. static int __init register_kprobe_prog_ops(void)
  189. {
  190. bpf_register_prog_type(&kprobe_tl);
  191. return 0;
  192. }
  193. late_initcall(register_kprobe_prog_ops);