perf augmented_raw_syscalls: Copy strings from all syscalls with 1st or 2nd string arg
[linux-2.6-microblaze.git] / tools / perf / examples / bpf / augmented_raw_syscalls.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Augment the raw_syscalls tracepoints with the contents of the pointer arguments.
4  *
5  * Test it with:
6  *
7  * perf trace -e tools/perf/examples/bpf/augmented_raw_syscalls.c cat /etc/passwd > /dev/null
8  *
9  * This exactly matches what is marshalled into the raw_syscall:sys_enter
10  * payload expected by the 'perf trace' beautifiers.
11  *
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.
15  */
16
17 #include <unistd.h>
18 #include <pid_filter.h>
19
20 /* bpf-output associated map */
21 bpf_map(__augmented_syscalls__, PERF_EVENT_ARRAY, int, u32, __NR_CPUS__);
22
23 struct syscall {
24         bool    enabled;
25 };
26
27 bpf_map(syscalls, ARRAY, int, struct syscall, 512);
28
29 struct syscall_enter_args {
30         unsigned long long common_tp_fields;
31         long               syscall_nr;
32         unsigned long      args[6];
33 };
34
35 struct syscall_exit_args {
36         unsigned long long common_tp_fields;
37         long               syscall_nr;
38         long               ret;
39 };
40
41 struct augmented_filename {
42         unsigned int    size;
43         int             reserved;
44         char            value[256];
45 };
46
47 /* syscalls where the first arg is a string */
48 #define SYS_OPEN                 2
49 #define SYS_STAT                 4
50 #define SYS_LSTAT                6
51 #define SYS_ACCESS              21
52 #define SYS_EXECVE              59
53 #define SYS_TRUNCATE            76
54 #define SYS_CHDIR               80
55 #define SYS_RENAME              82
56 #define SYS_MKDIR               83
57 #define SYS_RMDIR               84
58 #define SYS_CREAT               85
59 #define SYS_LINK                86
60 #define SYS_UNLINK              87
61 #define SYS_SYMLINK             88
62 #define SYS_READLINK            89
63 #define SYS_CHMOD               90
64 #define SYS_CHOWN               92
65 #define SYS_LCHOWN              94
66 #define SYS_MKNOD              133
67 #define SYS_STATFS             137
68 #define SYS_PIVOT_ROOT         155
69 #define SYS_CHROOT             161
70 #define SYS_ACCT               163
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
88
89 /* syscalls where the first arg is a string */
90
91 #define SYS_PWRITE64            18
92 #define SYS_EXECVE              59
93 #define SYS_RENAME              82
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
119
120 pid_filter(pids_filtered);
121
122 SEC("raw_syscalls:sys_enter")
123 int sys_enter(struct syscall_enter_args *args)
124 {
125         struct {
126                 struct syscall_enter_args args;
127                 struct augmented_filename filename;
128         } augmented_args;
129         struct syscall *syscall;
130         unsigned int len = sizeof(augmented_args);
131         const void *filename_arg = NULL;
132
133         if (pid_filter__has(&pids_filtered, getpid()))
134                 return 0;
135
136         probe_read(&augmented_args.args, sizeof(augmented_args.args), args);
137
138         syscall = bpf_map_lookup_elem(&syscalls, &augmented_args.args.syscall_nr);
139         if (syscall == NULL || !syscall->enabled)
140                 return 0;
141         /*
142          * Yonghong and Edward Cree sayz:
143          *
144          * https://www.spinics.net/lists/netdev/msg531645.html
145          *
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?  ¯\_(ツ)_/¯
159          *
160          * The optimization mostly likes below:
161          *
162          *      br1:
163          *      ...
164          *      r1 += 16
165          *      goto merge
166          *      br2:
167          *      ...
168          *      r1 += 20
169          *      goto merge
170          *      merge:
171          *      *(u64 *)(r1 + 0)
172          *
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:
176          *
177          *       __asm__ __volatile__("": : :"memory")
178          *
179          *       after the ctx memory access to prevent their down stream merging.
180          */
181         /*
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.
187          *
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.
193          */
194         switch (augmented_args.args.syscall_nr) {
195         case SYS_ACCT:
196         case SYS_ADD_KEY:
197         case SYS_CHDIR:
198         case SYS_CHMOD:
199         case SYS_CHOWN:
200         case SYS_CHROOT:
201         case SYS_CREAT:
202         case SYS_DELETE_MODULE:
203         case SYS_EXECVE:
204         case SYS_GETXATTR:
205         case SYS_LCHOWN:
206         case SYS_LGETXATTR:
207         case SYS_LINK:
208         case SYS_LISTXATTR:
209         case SYS_LLISTXATTR:
210         case SYS_LREMOVEXATTR:
211         case SYS_LSETXATTR:
212         case SYS_LSTAT:
213         case SYS_MEMFD_CREATE:
214         case SYS_MKDIR:
215         case SYS_MKNOD:
216         case SYS_MQ_OPEN:
217         case SYS_MQ_UNLINK:
218         case SYS_PIVOT_ROOT:
219         case SYS_READLINK:
220         case SYS_REMOVEXATTR:
221         case SYS_RENAME:
222         case SYS_REQUEST_KEY:
223         case SYS_RMDIR:
224         case SYS_SETXATTR:
225         case SYS_STAT:
226         case SYS_STATFS:
227         case SYS_SWAPOFF:
228         case SYS_SWAPON:
229         case SYS_SYMLINK:
230         case SYS_SYMLINKAT:
231         case SYS_TRUNCATE:
232         case SYS_UNLINK:
233         case SYS_ACCESS:
234         case SYS_OPEN:   filename_arg = (const void *)args->args[0];
235                         __asm__ __volatile__("": : :"memory");
236                          break;
237         case SYS_EXECVEAT:
238         case SYS_FACCESSAT:
239         case SYS_FCHMODAT:
240         case SYS_FCHOWNAT:
241         case SYS_FGETXATTR:
242         case SYS_FINIT_MODULE:
243         case SYS_FREMOVEXATTR:
244         case SYS_FSETXATTR:
245         case SYS_FUTIMESAT:
246         case SYS_INOTIFY_ADD_WATCH:
247         case SYS_LINKAT:
248         case SYS_MKDIRAT:
249         case SYS_MKNODAT:
250         case SYS_MQ_TIMEDSEND:
251         case SYS_NAME_TO_HANDLE_AT:
252         case SYS_NEWFSTATAT:
253         case SYS_PWRITE64:
254         case SYS_QUOTACTL:
255         case SYS_READLINKAT:
256         case SYS_RENAMEAT:
257         case SYS_RENAMEAT2:
258         case SYS_STATX:
259         case SYS_UNLINKAT:
260         case SYS_UTIMENSAT:
261         case SYS_OPENAT: filename_arg = (const void *)args->args[1];
262                          break;
263         }
264
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),
269                                                               filename_arg);
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;
273                 }
274         } else {
275                 len = sizeof(augmented_args.args);
276         }
277
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);
280 }
281
282 SEC("raw_syscalls:sys_exit")
283 int sys_exit(struct syscall_exit_args *args)
284 {
285         struct syscall_exit_args exit_args;
286         struct syscall *syscall;
287
288         if (pid_filter__has(&pids_filtered, getpid()))
289                 return 0;
290
291         probe_read(&exit_args, sizeof(exit_args), args);
292
293         syscall = bpf_map_lookup_elem(&syscalls, &exit_args.syscall_nr);
294         if (syscall == NULL || !syscall->enabled)
295                 return 0;
296
297         return 1;
298 }
299
300 license(GPL);