1 // SPDX-License-Identifier: GPL-2.0
3 * Augment the raw_syscalls tracepoints with the contents of the pointer arguments.
7 * perf trace -e tools/perf/examples/bpf/augmented_raw_syscalls.c cat /etc/passwd > /dev/null
9 * This exactly matches what is marshalled into the raw_syscall:sys_enter
10 * payload expected by the 'perf trace' beautifiers.
12 * For now it just uses the existing tracepoint augmentation code in 'perf
13 * trace', in the next csets we'll hook up these with the sys_enter/sys_exit
14 * code that will combine entry/exit in a strace like way.
18 #include <linux/limits.h>
19 #include <pid_filter.h>
21 /* bpf-output associated map */
22 bpf_map(__augmented_syscalls__, PERF_EVENT_ARRAY, int, u32, __NR_CPUS__);
25 * string_args_len: one per syscall arg, 0 means not a string or don't copy it,
26 * PATH_MAX for copying everything, any other value to limit
27 * it a la 'strace -s strsize'.
31 u16 string_args_len[6];
34 bpf_map(syscalls, ARRAY, int, struct syscall, 512);
36 struct syscall_enter_args {
37 unsigned long long common_tp_fields;
39 unsigned long args[6];
42 struct syscall_exit_args {
43 unsigned long long common_tp_fields;
48 struct augmented_filename {
54 pid_filter(pids_filtered);
56 struct augmented_args_filename {
57 struct syscall_enter_args args;
58 struct augmented_filename filename;
61 bpf_map(augmented_filename_map, PERCPU_ARRAY, int, struct augmented_args_filename, 1);
64 unsigned int augmented_filename__read(struct augmented_filename *augmented_filename,
65 const void *filename_arg, unsigned int filename_len)
67 unsigned int len = sizeof(*augmented_filename);
68 int size = probe_read_str(&augmented_filename->value, filename_len, filename_arg);
70 augmented_filename->size = augmented_filename->err = 0;
72 * probe_read_str may return < 0, e.g. -EFAULT
73 * So we leave that in the augmented_filename->size that userspace will
76 len -= sizeof(augmented_filename->value) - size;
77 len &= sizeof(augmented_filename->value) - 1;
78 augmented_filename->size = size;
81 * So that username notice the error while still being able
82 * to skip this augmented arg record
84 augmented_filename->err = size;
85 len = offsetof(struct augmented_filename, value);
91 SEC("raw_syscalls:sys_enter")
92 int sys_enter(struct syscall_enter_args *args)
94 struct augmented_args_filename *augmented_args;
96 * We start len, the amount of data that will be in the perf ring
97 * buffer, if this is not filtered out by one of pid_filter__has(),
98 * syscall->enabled, etc, with the non-augmented raw syscall payload,
99 * i.e. sizeof(augmented_args->args).
101 * We'll add to this as we add augmented syscalls right after that
102 * initial, non-augmented raw_syscalls:sys_enter payload.
104 unsigned int len = sizeof(augmented_args->args);
105 struct syscall *syscall;
108 augmented_args = bpf_map_lookup_elem(&augmented_filename_map, &key);
109 if (augmented_args == NULL)
112 if (pid_filter__has(&pids_filtered, getpid()))
115 probe_read(&augmented_args->args, sizeof(augmented_args->args), args);
117 syscall = bpf_map_lookup_elem(&syscalls, &augmented_args->args.syscall_nr);
118 if (syscall == NULL || !syscall->enabled)
121 * Yonghong and Edward Cree sayz:
123 * https://www.spinics.net/lists/netdev/msg531645.html
125 * >> R0=inv(id=0) R1=inv2 R6=ctx(id=0,off=0,imm=0) R7=inv64 R10=fp0,call_-1
126 * >> 10: (bf) r1 = r6
127 * >> 11: (07) r1 += 16
128 * >> 12: (05) goto pc+2
129 * >> 15: (79) r3 = *(u64 *)(r1 +0)
130 * >> dereference of modified ctx ptr R1 off=16 disallowed
131 * > Aha, we at least got a different error message this time.
132 * > And indeed llvm has done that optimisation, rather than the more obvious
133 * > 11: r3 = *(u64 *)(r1 +16)
134 * > because it wants to have lots of reads share a single insn. You may be able
135 * > to defeat that optimisation by adding compiler barriers, idk. Maybe someone
136 * > with llvm knowledge can figure out how to stop it (ideally, llvm would know
137 * > when it's generating for bpf backend and not do that). -O0? ¯\_(ツ)_/¯
139 * The optimization mostly likes below:
152 * The compiler tries to merge common loads. There is no easy way to
153 * stop this compiler optimization without turning off a lot of other
154 * optimizations. The easiest way is to add barriers:
156 * __asm__ __volatile__("": : :"memory")
158 * after the ctx memory access to prevent their down stream merging.
161 * For now copy just the first string arg, we need to improve the protocol
162 * and have more than one.
164 * Using the unrolled loop is not working, only when we do it manually,
165 * check this out later...
168 #pragma clang loop unroll(full)
169 for (arg = 0; arg < 6; ++arg) {
170 if (syscall->string_args_len[arg] != 0) {
171 filename_len = syscall->string_args_len[arg];
172 filename_arg = (const void *)args->args[arg];
173 __asm__ __volatile__("": : :"memory");
180 ; if (syscall->string_args_len[arg] != 0) {
181 37: (69) r3 = *(u16 *)(r0 +2)
182 R0=map_value(id=0,off=0,ks=4,vs=14,imm=0) R1_w=inv0 R2_w=map_value(id=0,off=2,ks=4,vs=14,imm=0) R6=ctx(id=0,off=0,imm=0) R7=map_value(id=0,off=0,ks=4,vs=4168,imm=0) R10=fp0,call_-1 fp-8=mmmmmmmm
183 ; if (syscall->string_args_len[arg] != 0) {
184 38: (55) if r3 != 0x0 goto pc+5
185 R0=map_value(id=0,off=0,ks=4,vs=14,imm=0) R1=inv0 R2=map_value(id=0,off=2,ks=4,vs=14,imm=0) R3=inv0 R6=ctx(id=0,off=0,imm=0) R7=map_value(id=0,off=0,ks=4,vs=4168,imm=0) R10=fp0,call_-1 fp-8=mmmmmmmm
187 ; if (syscall->string_args_len[arg] != 0) {
190 42: (69) r3 = *(u16 *)(r0 +4)
191 R0=map_value(id=0,off=0,ks=4,vs=14,imm=0) R1_w=inv1 R2_w=map_value(id=0,off=4,ks=4,vs=14,imm=0) R3_w=inv0 R6=ctx(id=0,off=0,imm=0) R7=map_value(id=0,off=0,ks=4,vs=4168,imm=0) R10=fp0,call_-1 fp-8=mmmmmmmm
192 ; if (syscall->string_args_len[arg] != 0) {
193 43: (15) if r3 == 0x0 goto pc+32
194 R0=map_value(id=0,off=0,ks=4,vs=14,imm=0) R1=inv1 R2=map_value(id=0,off=4,ks=4,vs=14,imm=0) R3=inv(id=0,umax_value=65535,var_off=(0x0; 0xffff)) R6=ctx(id=0,off=0,imm=0) R7=map_value(id=0,off=0,ks=4,vs=4168,imm=0) R10=fp0,call_-1 fp-8=mmmmmmmm
195 ; filename_arg = (const void *)args->args[arg];
200 48: (79) r3 = *(u64 *)(r3 +16)
201 dereference of modified ctx ptr R3 off=8 disallowed
202 processed 46 insns (limit 1000000) max_states_per_insn 0 total_states 12 peak_states 12 mark_read 7
205 #define __loop_iter(arg) \
206 if (syscall->string_args_len[arg] != 0) { \
207 unsigned int filename_len = syscall->string_args_len[arg]; \
208 const void *filename_arg = (const void *)args->args[arg]; \
209 if (filename_len <= sizeof(augmented_args->filename.value)) \
210 len += augmented_filename__read(&augmented_args->filename, filename_arg, filename_len);
211 #define loop_iter_first() __loop_iter(0); }
212 #define loop_iter(arg) else __loop_iter(arg); }
213 #define loop_iter_last(arg) else __loop_iter(arg); __asm__ __volatile__("": : :"memory"); }
222 /* If perf_event_output fails, return non-zero so that it gets recorded unaugmented */
223 return perf_event_output(args, &__augmented_syscalls__, BPF_F_CURRENT_CPU, augmented_args, len);
226 SEC("raw_syscalls:sys_exit")
227 int sys_exit(struct syscall_exit_args *args)
229 struct syscall_exit_args exit_args;
230 struct syscall *syscall;
232 if (pid_filter__has(&pids_filtered, getpid()))
235 probe_read(&exit_args, sizeof(exit_args), args);
237 syscall = bpf_map_lookup_elem(&syscalls, &exit_args.syscall_nr);
238 if (syscall == NULL || !syscall->enabled)