xref: /OK3568_Linux_fs/kernel/tools/perf/examples/bpf/augmented_raw_syscalls.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Augment the raw_syscalls tracepoints with the contents of the pointer arguments.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Test it with:
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * perf trace -e tools/perf/examples/bpf/augmented_raw_syscalls.c cat /etc/passwd > /dev/null
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This exactly matches what is marshalled into the raw_syscall:sys_enter
10*4882a593Smuzhiyun  * payload expected by the 'perf trace' beautifiers.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * For now it just uses the existing tracepoint augmentation code in 'perf
13*4882a593Smuzhiyun  * trace', in the next csets we'll hook up these with the sys_enter/sys_exit
14*4882a593Smuzhiyun  * code that will combine entry/exit in a strace like way.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <unistd.h>
18*4882a593Smuzhiyun #include <linux/limits.h>
19*4882a593Smuzhiyun #include <linux/socket.h>
20*4882a593Smuzhiyun #include <pid_filter.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /* bpf-output associated map */
23*4882a593Smuzhiyun bpf_map(__augmented_syscalls__, PERF_EVENT_ARRAY, int, u32, __NR_CPUS__);
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun  * string_args_len: one per syscall arg, 0 means not a string or don't copy it,
27*4882a593Smuzhiyun  * 		    PATH_MAX for copying everything, any other value to limit
28*4882a593Smuzhiyun  * 		    it a la 'strace -s strsize'.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun struct syscall {
31*4882a593Smuzhiyun 	bool	enabled;
32*4882a593Smuzhiyun 	u16	string_args_len[6];
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun bpf_map(syscalls, ARRAY, int, struct syscall, 512);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun  * What to augment at entry?
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * Pointer arg payloads (filenames, etc) passed from userspace to the kernel
41*4882a593Smuzhiyun  */
42*4882a593Smuzhiyun bpf_map(syscalls_sys_enter, PROG_ARRAY, u32, u32, 512);
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun  * What to augment at exit?
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * Pointer arg payloads returned from the kernel (struct stat, etc) to userspace.
48*4882a593Smuzhiyun  */
49*4882a593Smuzhiyun bpf_map(syscalls_sys_exit, PROG_ARRAY, u32, u32, 512);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun struct syscall_enter_args {
52*4882a593Smuzhiyun 	unsigned long long common_tp_fields;
53*4882a593Smuzhiyun 	long		   syscall_nr;
54*4882a593Smuzhiyun 	unsigned long	   args[6];
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun struct syscall_exit_args {
58*4882a593Smuzhiyun 	unsigned long long common_tp_fields;
59*4882a593Smuzhiyun 	long		   syscall_nr;
60*4882a593Smuzhiyun 	long		   ret;
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun struct augmented_arg {
64*4882a593Smuzhiyun 	unsigned int	size;
65*4882a593Smuzhiyun 	int		err;
66*4882a593Smuzhiyun 	char		value[PATH_MAX];
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun pid_filter(pids_filtered);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun struct augmented_args_payload {
72*4882a593Smuzhiyun        struct syscall_enter_args args;
73*4882a593Smuzhiyun        union {
74*4882a593Smuzhiyun 		struct {
75*4882a593Smuzhiyun 			struct augmented_arg arg, arg2;
76*4882a593Smuzhiyun 		};
77*4882a593Smuzhiyun 		struct sockaddr_storage saddr;
78*4882a593Smuzhiyun 	};
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun // We need more tmp space than the BPF stack can give us
82*4882a593Smuzhiyun bpf_map(augmented_args_tmp, PERCPU_ARRAY, int, struct augmented_args_payload, 1);
83*4882a593Smuzhiyun 
augmented_args_payload(void)84*4882a593Smuzhiyun static inline struct augmented_args_payload *augmented_args_payload(void)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	int key = 0;
87*4882a593Smuzhiyun 	return bpf_map_lookup_elem(&augmented_args_tmp, &key);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
augmented__output(void * ctx,struct augmented_args_payload * args,int len)90*4882a593Smuzhiyun static inline int augmented__output(void *ctx, struct augmented_args_payload *args, int len)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	/* If perf_event_output fails, return non-zero so that it gets recorded unaugmented */
93*4882a593Smuzhiyun 	return perf_event_output(ctx, &__augmented_syscalls__, BPF_F_CURRENT_CPU, args, len);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun static inline
augmented_arg__read_str(struct augmented_arg * augmented_arg,const void * arg,unsigned int arg_len)97*4882a593Smuzhiyun unsigned int augmented_arg__read_str(struct augmented_arg *augmented_arg, const void *arg, unsigned int arg_len)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	unsigned int augmented_len = sizeof(*augmented_arg);
100*4882a593Smuzhiyun 	int string_len = probe_read_str(&augmented_arg->value, arg_len, arg);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	augmented_arg->size = augmented_arg->err = 0;
103*4882a593Smuzhiyun 	/*
104*4882a593Smuzhiyun 	 * probe_read_str may return < 0, e.g. -EFAULT
105*4882a593Smuzhiyun 	 * So we leave that in the augmented_arg->size that userspace will
106*4882a593Smuzhiyun 	 */
107*4882a593Smuzhiyun 	if (string_len > 0) {
108*4882a593Smuzhiyun 		augmented_len -= sizeof(augmented_arg->value) - string_len;
109*4882a593Smuzhiyun 		augmented_len &= sizeof(augmented_arg->value) - 1;
110*4882a593Smuzhiyun 		augmented_arg->size = string_len;
111*4882a593Smuzhiyun 	} else {
112*4882a593Smuzhiyun 		/*
113*4882a593Smuzhiyun 		 * So that username notice the error while still being able
114*4882a593Smuzhiyun 		 * to skip this augmented arg record
115*4882a593Smuzhiyun 		 */
116*4882a593Smuzhiyun 		augmented_arg->err = string_len;
117*4882a593Smuzhiyun 		augmented_len = offsetof(struct augmented_arg, value);
118*4882a593Smuzhiyun 	}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	return augmented_len;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun SEC("!raw_syscalls:unaugmented")
syscall_unaugmented(struct syscall_enter_args * args)124*4882a593Smuzhiyun int syscall_unaugmented(struct syscall_enter_args *args)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	return 1;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /*
130*4882a593Smuzhiyun  * These will be tail_called from SEC("raw_syscalls:sys_enter"), so will find in
131*4882a593Smuzhiyun  * augmented_args_tmp what was read by that raw_syscalls:sys_enter and go
132*4882a593Smuzhiyun  * on from there, reading the first syscall arg as a string, i.e. open's
133*4882a593Smuzhiyun  * filename.
134*4882a593Smuzhiyun  */
135*4882a593Smuzhiyun SEC("!syscalls:sys_enter_connect")
sys_enter_connect(struct syscall_enter_args * args)136*4882a593Smuzhiyun int sys_enter_connect(struct syscall_enter_args *args)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	struct augmented_args_payload *augmented_args = augmented_args_payload();
139*4882a593Smuzhiyun 	const void *sockaddr_arg = (const void *)args->args[1];
140*4882a593Smuzhiyun 	unsigned int socklen = args->args[2];
141*4882a593Smuzhiyun 	unsigned int len = sizeof(augmented_args->args);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun         if (augmented_args == NULL)
144*4882a593Smuzhiyun                 return 1; /* Failure: don't filter */
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	if (socklen > sizeof(augmented_args->saddr))
147*4882a593Smuzhiyun 		socklen = sizeof(augmented_args->saddr);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	probe_read(&augmented_args->saddr, socklen, sockaddr_arg);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	return augmented__output(args, augmented_args, len + socklen);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun SEC("!syscalls:sys_enter_sendto")
sys_enter_sendto(struct syscall_enter_args * args)155*4882a593Smuzhiyun int sys_enter_sendto(struct syscall_enter_args *args)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	struct augmented_args_payload *augmented_args = augmented_args_payload();
158*4882a593Smuzhiyun 	const void *sockaddr_arg = (const void *)args->args[4];
159*4882a593Smuzhiyun 	unsigned int socklen = args->args[5];
160*4882a593Smuzhiyun 	unsigned int len = sizeof(augmented_args->args);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun         if (augmented_args == NULL)
163*4882a593Smuzhiyun                 return 1; /* Failure: don't filter */
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (socklen > sizeof(augmented_args->saddr))
166*4882a593Smuzhiyun 		socklen = sizeof(augmented_args->saddr);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	probe_read(&augmented_args->saddr, socklen, sockaddr_arg);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	return augmented__output(args, augmented_args, len + socklen);
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun SEC("!syscalls:sys_enter_open")
sys_enter_open(struct syscall_enter_args * args)174*4882a593Smuzhiyun int sys_enter_open(struct syscall_enter_args *args)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct augmented_args_payload *augmented_args = augmented_args_payload();
177*4882a593Smuzhiyun 	const void *filename_arg = (const void *)args->args[0];
178*4882a593Smuzhiyun 	unsigned int len = sizeof(augmented_args->args);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun         if (augmented_args == NULL)
181*4882a593Smuzhiyun                 return 1; /* Failure: don't filter */
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	len += augmented_arg__read_str(&augmented_args->arg, filename_arg, sizeof(augmented_args->arg.value));
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	return augmented__output(args, augmented_args, len);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun SEC("!syscalls:sys_enter_openat")
sys_enter_openat(struct syscall_enter_args * args)189*4882a593Smuzhiyun int sys_enter_openat(struct syscall_enter_args *args)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	struct augmented_args_payload *augmented_args = augmented_args_payload();
192*4882a593Smuzhiyun 	const void *filename_arg = (const void *)args->args[1];
193*4882a593Smuzhiyun 	unsigned int len = sizeof(augmented_args->args);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun         if (augmented_args == NULL)
196*4882a593Smuzhiyun                 return 1; /* Failure: don't filter */
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	len += augmented_arg__read_str(&augmented_args->arg, filename_arg, sizeof(augmented_args->arg.value));
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	return augmented__output(args, augmented_args, len);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun SEC("!syscalls:sys_enter_rename")
sys_enter_rename(struct syscall_enter_args * args)204*4882a593Smuzhiyun int sys_enter_rename(struct syscall_enter_args *args)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	struct augmented_args_payload *augmented_args = augmented_args_payload();
207*4882a593Smuzhiyun 	const void *oldpath_arg = (const void *)args->args[0],
208*4882a593Smuzhiyun 		   *newpath_arg = (const void *)args->args[1];
209*4882a593Smuzhiyun 	unsigned int len = sizeof(augmented_args->args), oldpath_len;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun         if (augmented_args == NULL)
212*4882a593Smuzhiyun                 return 1; /* Failure: don't filter */
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	oldpath_len = augmented_arg__read_str(&augmented_args->arg, oldpath_arg, sizeof(augmented_args->arg.value));
215*4882a593Smuzhiyun 	len += oldpath_len + augmented_arg__read_str((void *)(&augmented_args->arg) + oldpath_len, newpath_arg, sizeof(augmented_args->arg.value));
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	return augmented__output(args, augmented_args, len);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun SEC("!syscalls:sys_enter_renameat")
sys_enter_renameat(struct syscall_enter_args * args)221*4882a593Smuzhiyun int sys_enter_renameat(struct syscall_enter_args *args)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	struct augmented_args_payload *augmented_args = augmented_args_payload();
224*4882a593Smuzhiyun 	const void *oldpath_arg = (const void *)args->args[1],
225*4882a593Smuzhiyun 		   *newpath_arg = (const void *)args->args[3];
226*4882a593Smuzhiyun 	unsigned int len = sizeof(augmented_args->args), oldpath_len;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun         if (augmented_args == NULL)
229*4882a593Smuzhiyun                 return 1; /* Failure: don't filter */
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	oldpath_len = augmented_arg__read_str(&augmented_args->arg, oldpath_arg, sizeof(augmented_args->arg.value));
232*4882a593Smuzhiyun 	len += oldpath_len + augmented_arg__read_str((void *)(&augmented_args->arg) + oldpath_len, newpath_arg, sizeof(augmented_args->arg.value));
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	return augmented__output(args, augmented_args, len);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun SEC("raw_syscalls:sys_enter")
sys_enter(struct syscall_enter_args * args)238*4882a593Smuzhiyun int sys_enter(struct syscall_enter_args *args)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	struct augmented_args_payload *augmented_args;
241*4882a593Smuzhiyun 	/*
242*4882a593Smuzhiyun 	 * We start len, the amount of data that will be in the perf ring
243*4882a593Smuzhiyun 	 * buffer, if this is not filtered out by one of pid_filter__has(),
244*4882a593Smuzhiyun 	 * syscall->enabled, etc, with the non-augmented raw syscall payload,
245*4882a593Smuzhiyun 	 * i.e. sizeof(augmented_args->args).
246*4882a593Smuzhiyun 	 *
247*4882a593Smuzhiyun 	 * We'll add to this as we add augmented syscalls right after that
248*4882a593Smuzhiyun 	 * initial, non-augmented raw_syscalls:sys_enter payload.
249*4882a593Smuzhiyun 	 */
250*4882a593Smuzhiyun 	unsigned int len = sizeof(augmented_args->args);
251*4882a593Smuzhiyun 	struct syscall *syscall;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	if (pid_filter__has(&pids_filtered, getpid()))
254*4882a593Smuzhiyun 		return 0;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	augmented_args = augmented_args_payload();
257*4882a593Smuzhiyun 	if (augmented_args == NULL)
258*4882a593Smuzhiyun 		return 1;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	probe_read(&augmented_args->args, sizeof(augmented_args->args), args);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/*
263*4882a593Smuzhiyun 	 * Jump to syscall specific augmenter, even if the default one,
264*4882a593Smuzhiyun 	 * "!raw_syscalls:unaugmented" that will just return 1 to return the
265*4882a593Smuzhiyun 	 * unagmented tracepoint payload.
266*4882a593Smuzhiyun 	 */
267*4882a593Smuzhiyun 	bpf_tail_call(args, &syscalls_sys_enter, augmented_args->args.syscall_nr);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	// If not found on the PROG_ARRAY syscalls map, then we're filtering it:
270*4882a593Smuzhiyun 	return 0;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun SEC("raw_syscalls:sys_exit")
sys_exit(struct syscall_exit_args * args)274*4882a593Smuzhiyun int sys_exit(struct syscall_exit_args *args)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun 	struct syscall_exit_args exit_args;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	if (pid_filter__has(&pids_filtered, getpid()))
279*4882a593Smuzhiyun 		return 0;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	probe_read(&exit_args, sizeof(exit_args), args);
282*4882a593Smuzhiyun 	/*
283*4882a593Smuzhiyun 	 * Jump to syscall specific return augmenter, even if the default one,
284*4882a593Smuzhiyun 	 * "!raw_syscalls:unaugmented" that will just return 1 to return the
285*4882a593Smuzhiyun 	 * unagmented tracepoint payload.
286*4882a593Smuzhiyun 	 */
287*4882a593Smuzhiyun 	bpf_tail_call(args, &syscalls_sys_exit, exit_args.syscall_nr);
288*4882a593Smuzhiyun 	/*
289*4882a593Smuzhiyun 	 * If not found on the PROG_ARRAY syscalls map, then we're filtering it:
290*4882a593Smuzhiyun 	 */
291*4882a593Smuzhiyun 	return 0;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun license(GPL);
295