diff options
author | Alexei Starovoitov <ast@plumgrid.com> | 2015-08-28 18:56:23 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2015-08-28 19:27:27 -0400 |
commit | 8d3b7dce8622919da5c5822ef7338d6604c9fe6e (patch) | |
tree | 8934c8b0f6aac4a01488c59c45b8dc55c64cc721 /kernel/trace/bpf_trace.c | |
parent | 1a6877b9c0c2ad901d4335d909432d3bb6d3a330 (diff) |
bpf: add support for %s specifier to bpf_trace_printk()
%s specifier makes bpf program and kernel debugging easier.
To make sure that trace_printk won't crash the unsafe string
is copied into stack and unsafe pointer is substituted.
The following C program:
#include <linux/fs.h>
int foo(struct pt_regs *ctx, struct filename *filename)
{
void *name = 0;
bpf_probe_read(&name, sizeof(name), &filename->name);
bpf_trace_printk("executed %s\n", name);
return 0;
}
when attached to kprobe do_execve()
will produce output in /sys/kernel/debug/tracing/trace_pipe :
make-13492 [002] d..1 3250.997277: : executed /bin/sh
sh-13493 [004] d..1 3250.998716: : executed /usr/bin/gcc
gcc-13494 [002] d..1 3250.999822: : executed /usr/lib/gcc/x86_64-linux-gnu/4.7/cc1
gcc-13495 [002] d..1 3251.006731: : executed /usr/bin/as
gcc-13496 [002] d..1 3251.011831: : executed /usr/lib/gcc/x86_64-linux-gnu/4.7/collect2
collect2-13497 [000] d..1 3251.012941: : executed /usr/bin/ld
Suggested-by: Brendan Gregg <brendan.d.gregg@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'kernel/trace/bpf_trace.c')
-rw-r--r-- | kernel/trace/bpf_trace.c | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index ef9936df1b04..0fe96c7c8803 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c | |||
@@ -81,13 +81,16 @@ static const struct bpf_func_proto bpf_probe_read_proto = { | |||
81 | 81 | ||
82 | /* | 82 | /* |
83 | * limited trace_printk() | 83 | * limited trace_printk() |
84 | * only %d %u %x %ld %lu %lx %lld %llu %llx %p conversion specifiers allowed | 84 | * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed |
85 | */ | 85 | */ |
86 | static u64 bpf_trace_printk(u64 r1, u64 fmt_size, u64 r3, u64 r4, u64 r5) | 86 | static u64 bpf_trace_printk(u64 r1, u64 fmt_size, u64 r3, u64 r4, u64 r5) |
87 | { | 87 | { |
88 | char *fmt = (char *) (long) r1; | 88 | char *fmt = (char *) (long) r1; |
89 | bool str_seen = false; | ||
89 | int mod[3] = {}; | 90 | int mod[3] = {}; |
90 | int fmt_cnt = 0; | 91 | int fmt_cnt = 0; |
92 | u64 unsafe_addr; | ||
93 | char buf[64]; | ||
91 | int i; | 94 | int i; |
92 | 95 | ||
93 | /* | 96 | /* |
@@ -114,12 +117,37 @@ static u64 bpf_trace_printk(u64 r1, u64 fmt_size, u64 r3, u64 r4, u64 r5) | |||
114 | if (fmt[i] == 'l') { | 117 | if (fmt[i] == 'l') { |
115 | mod[fmt_cnt]++; | 118 | mod[fmt_cnt]++; |
116 | i++; | 119 | i++; |
117 | } else if (fmt[i] == 'p') { | 120 | } else if (fmt[i] == 'p' || fmt[i] == 's') { |
118 | mod[fmt_cnt]++; | 121 | mod[fmt_cnt]++; |
119 | i++; | 122 | i++; |
120 | if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0) | 123 | if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0) |
121 | return -EINVAL; | 124 | return -EINVAL; |
122 | fmt_cnt++; | 125 | fmt_cnt++; |
126 | if (fmt[i - 1] == 's') { | ||
127 | if (str_seen) | ||
128 | /* allow only one '%s' per fmt string */ | ||
129 | return -EINVAL; | ||
130 | str_seen = true; | ||
131 | |||
132 | switch (fmt_cnt) { | ||
133 | case 1: | ||
134 | unsafe_addr = r3; | ||
135 | r3 = (long) buf; | ||
136 | break; | ||
137 | case 2: | ||
138 | unsafe_addr = r4; | ||
139 | r4 = (long) buf; | ||
140 | break; | ||
141 | case 3: | ||
142 | unsafe_addr = r5; | ||
143 | r5 = (long) buf; | ||
144 | break; | ||
145 | } | ||
146 | buf[0] = 0; | ||
147 | strncpy_from_unsafe(buf, | ||
148 | (void *) (long) unsafe_addr, | ||
149 | sizeof(buf)); | ||
150 | } | ||
123 | continue; | 151 | continue; |
124 | } | 152 | } |
125 | 153 | ||