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 <pid_filter.h>
20 /* bpf-output associated map */
21 bpf_map(__augmented_syscalls__, PERF_EVENT_ARRAY, int, u32, __NR_CPUS__);
27 bpf_map(syscalls, ARRAY, int, struct syscall, 512);
29 struct syscall_enter_args {
30 unsigned long long common_tp_fields;
32 unsigned long args[6];
35 struct syscall_exit_args {
36 unsigned long long common_tp_fields;
41 struct augmented_filename {
47 /* syscalls where the first arg is a string */
53 #define SYS_TRUNCATE 76
61 #define SYS_SYMLINK 88
62 #define SYS_READLINK 89
67 #define SYS_STATFS 137
68 #define SYS_PIVOT_ROOT 155
69 #define SYS_CHROOT 161
71 #define SYS_SWAPON 167
72 #define SYS_SWAPOFF 168
73 #define SYS_DELETE_MODULE 176
74 #define SYS_SETXATTR 188
75 #define SYS_LSETXATTR 189
76 #define SYS_GETXATTR 191
77 #define SYS_LGETXATTR 192
78 #define SYS_LISTXATTR 194
79 #define SYS_LLISTXATTR 195
80 #define SYS_REMOVEXATTR 197
81 #define SYS_LREMOVEXATTR 198
82 #define SYS_MQ_OPEN 240
83 #define SYS_MQ_UNLINK 241
84 #define SYS_ADD_KEY 248
85 #define SYS_REQUEST_KEY 249
86 #define SYS_SYMLINKAT 266
87 #define SYS_MEMFD_CREATE 319
89 /* syscalls where the first arg is a string */
91 #define SYS_PWRITE64 18
94 #define SYS_QUOTACTL 179
95 #define SYS_FSETXATTR 190
96 #define SYS_FGETXATTR 193
97 #define SYS_FREMOVEXATTR 199
98 #define SYS_MQ_TIMEDSEND 242
99 #define SYS_REQUEST_KEY 249
100 #define SYS_INOTIFY_ADD_WATCH 254
101 #define SYS_OPENAT 257
102 #define SYS_MKDIRAT 258
103 #define SYS_MKNODAT 259
104 #define SYS_FCHOWNAT 260
105 #define SYS_FUTIMESAT 261
106 #define SYS_NEWFSTATAT 262
107 #define SYS_UNLINKAT 263
108 #define SYS_RENAMEAT 264
109 #define SYS_LINKAT 265
110 #define SYS_READLINKAT 267
111 #define SYS_FCHMODAT 268
112 #define SYS_FACCESSAT 269
113 #define SYS_UTIMENSAT 280
114 #define SYS_NAME_TO_HANDLE_AT 303
115 #define SYS_FINIT_MODULE 313
116 #define SYS_RENAMEAT2 316
117 #define SYS_EXECVEAT 322
118 #define SYS_STATX 332
120 pid_filter(pids_filtered);
122 SEC("raw_syscalls:sys_enter")
123 int sys_enter(struct syscall_enter_args *args)
126 struct syscall_enter_args args;
127 struct augmented_filename filename;
129 struct syscall *syscall;
130 unsigned int len = sizeof(augmented_args);
131 const void *filename_arg = NULL;
133 if (pid_filter__has(&pids_filtered, getpid()))
136 probe_read(&augmented_args.args, sizeof(augmented_args.args), args);
138 syscall = bpf_map_lookup_elem(&syscalls, &augmented_args.args.syscall_nr);
139 if (syscall == NULL || !syscall->enabled)
142 * Yonghong and Edward Cree sayz:
144 * https://www.spinics.net/lists/netdev/msg531645.html
146 * >> R0=inv(id=0) R1=inv2 R6=ctx(id=0,off=0,imm=0) R7=inv64 R10=fp0,call_-1
147 * >> 10: (bf) r1 = r6
148 * >> 11: (07) r1 += 16
149 * >> 12: (05) goto pc+2
150 * >> 15: (79) r3 = *(u64 *)(r1 +0)
151 * >> dereference of modified ctx ptr R1 off=16 disallowed
152 * > Aha, we at least got a different error message this time.
153 * > And indeed llvm has done that optimisation, rather than the more obvious
154 * > 11: r3 = *(u64 *)(r1 +16)
155 * > because it wants to have lots of reads share a single insn. You may be able
156 * > to defeat that optimisation by adding compiler barriers, idk. Maybe someone
157 * > with llvm knowledge can figure out how to stop it (ideally, llvm would know
158 * > when it's generating for bpf backend and not do that). -O0? ¯\_(ツ)_/¯
160 * The optimization mostly likes below:
173 * The compiler tries to merge common loads. There is no easy way to
174 * stop this compiler optimization without turning off a lot of other
175 * optimizations. The easiest way is to add barriers:
177 * __asm__ __volatile__("": : :"memory")
179 * after the ctx memory access to prevent their down stream merging.
182 * This table of what args are strings will be provided by userspace,
183 * in the syscalls map, i.e. we will already have to do the lookup to
184 * see if this specific syscall is filtered, so we can as well get more
185 * info about what syscall args are strings or pointers, and how many
186 * bytes to copy, per arg, etc.
188 * For now hard code it, till we have all the basic mechanisms in place
189 * to automate everything and make the kernel part be completely driven
190 * by information obtained in userspace for each kernel version and
191 * processor architecture, making the kernel part the same no matter what
192 * kernel version or processor architecture it runs on.
194 switch (augmented_args.args.syscall_nr) {
202 case SYS_DELETE_MODULE:
210 case SYS_LREMOVEXATTR:
213 case SYS_MEMFD_CREATE:
220 case SYS_REMOVEXATTR:
222 case SYS_REQUEST_KEY:
234 case SYS_OPEN: filename_arg = (const void *)args->args[0];
235 __asm__ __volatile__("": : :"memory");
242 case SYS_FINIT_MODULE:
243 case SYS_FREMOVEXATTR:
246 case SYS_INOTIFY_ADD_WATCH:
250 case SYS_MQ_TIMEDSEND:
251 case SYS_NAME_TO_HANDLE_AT:
261 case SYS_OPENAT: filename_arg = (const void *)args->args[1];
265 if (filename_arg != NULL) {
266 augmented_args.filename.reserved = 0;
267 augmented_args.filename.size = probe_read_str(&augmented_args.filename.value,
268 sizeof(augmented_args.filename.value),
270 if (augmented_args.filename.size < sizeof(augmented_args.filename.value)) {
271 len -= sizeof(augmented_args.filename.value) - augmented_args.filename.size;
272 len &= sizeof(augmented_args.filename.value) - 1;
275 len = sizeof(augmented_args.args);
278 /* If perf_event_output fails, return non-zero so that it gets recorded unaugmented */
279 return perf_event_output(args, &__augmented_syscalls__, BPF_F_CURRENT_CPU, &augmented_args, len);
282 SEC("raw_syscalls:sys_exit")
283 int sys_exit(struct syscall_exit_args *args)
285 struct syscall_exit_args exit_args;
286 struct syscall *syscall;
288 if (pid_filter__has(&pids_filtered, getpid()))
291 probe_read(&exit_args, sizeof(exit_args), args);
293 syscall = bpf_map_lookup_elem(&syscalls, &exit_args.syscall_nr);
294 if (syscall == NULL || !syscall->enabled)