1 // SPDX-License-Identifier: GPL-2.0
3 * Augment syscalls with the contents of the pointer arguments.
7 * perf trace -e tools/perf/examples/bpf/augmented_syscalls.c cat /etc/passwd > /dev/null
9 * It'll catch some openat syscalls related to the dynamic linked and
10 * the last one should be the one for '/etc/passwd'.
12 * This matches what is marshalled into the raw_syscall:sys_enter payload
13 * expected by the 'perf trace' beautifiers, and can be used by them, that will
14 * check if perf_sample->raw_data is more than what is expected for each
15 * syscalls:sys_{enter,exit}_SYSCALL tracepoint, uing the extra data as the
16 * contents of pointer arguments.
20 #include <linux/socket.h>
22 /* bpf-output associated map */
23 bpf_map(__augmented_syscalls__
, PERF_EVENT_ARRAY
, int, u32
, __NR_CPUS__
);
25 struct syscall_exit_args
{
26 unsigned long long common_tp_fields
;
31 struct augmented_filename
{
37 #define augmented_filename_syscall(syscall) \
38 struct augmented_enter_##syscall##_args { \
39 struct syscall_enter_##syscall##_args args; \
40 struct augmented_filename filename; \
42 int syscall_enter(syscall)(struct syscall_enter_##syscall##_args *args) \
44 struct augmented_enter_##syscall##_args augmented_args = { .filename.reserved = 0, }; \
45 unsigned int len = sizeof(augmented_args); \
46 probe_read(&augmented_args.args, sizeof(augmented_args.args), args); \
47 augmented_args.filename.size = probe_read_str(&augmented_args.filename.value, \
48 sizeof(augmented_args.filename.value), \
49 args->filename_ptr); \
50 if (augmented_args.filename.size < sizeof(augmented_args.filename.value)) { \
51 len -= sizeof(augmented_args.filename.value) - augmented_args.filename.size; \
52 len &= sizeof(augmented_args.filename.value) - 1; \
54 /* If perf_event_output fails, return non-zero so that it gets recorded unaugmented */ \
55 return perf_event_output(args, &__augmented_syscalls__, BPF_F_CURRENT_CPU, \
56 &augmented_args, len); \
58 int syscall_exit(syscall)(struct syscall_exit_args *args) \
60 return 1; /* 0 as soon as we start copying data returned by the kernel, e.g. 'read' */ \
63 struct syscall_enter_openat_args
{
64 unsigned long long common_tp_fields
;
72 augmented_filename_syscall(openat
);
74 struct syscall_enter_open_args
{
75 unsigned long long common_tp_fields
;
82 augmented_filename_syscall(open
);
84 struct syscall_enter_inotify_add_watch_args
{
85 unsigned long long common_tp_fields
;
92 augmented_filename_syscall(inotify_add_watch
);
96 struct syscall_enter_newstat_args
{
97 unsigned long long common_tp_fields
;
100 struct stat
*statbuf
;
103 augmented_filename_syscall(newstat
);
105 #ifndef _K_SS_MAXSIZE
106 #define _K_SS_MAXSIZE 128
109 #define augmented_sockaddr_syscall(syscall) \
110 struct augmented_enter_##syscall##_args { \
111 struct syscall_enter_##syscall##_args args; \
112 struct sockaddr_storage addr; \
114 int syscall_enter(syscall)(struct syscall_enter_##syscall##_args *args) \
116 struct augmented_enter_##syscall##_args augmented_args; \
117 unsigned long addrlen = sizeof(augmented_args.addr); \
118 probe_read(&augmented_args.args, sizeof(augmented_args.args), args); \
119 /* FIXME_CLANG_OPTIMIZATION_THAT_ACCESSES_USER_CONTROLLED_ADDRLEN_DESPITE_THIS_CHECK */ \
120 /* if (addrlen > augmented_args.args.addrlen) */ \
121 /* addrlen = augmented_args.args.addrlen; */ \
123 probe_read(&augmented_args.addr, addrlen, args->addr_ptr); \
124 /* If perf_event_output fails, return non-zero so that it gets recorded unaugmented */ \
125 return perf_event_output(args, &__augmented_syscalls__, BPF_F_CURRENT_CPU, \
127 sizeof(augmented_args) - sizeof(augmented_args.addr) + addrlen);\
129 int syscall_exit(syscall)(struct syscall_exit_args *args) \
131 return 1; /* 0 as soon as we start copying data returned by the kernel, e.g. 'read' */ \
136 struct syscall_enter_bind_args
{
137 unsigned long long common_tp_fields
;
140 struct sockaddr
*addr_ptr
;
141 unsigned long addrlen
;
144 augmented_sockaddr_syscall(bind
);
146 struct syscall_enter_connect_args
{
147 unsigned long long common_tp_fields
;
150 struct sockaddr
*addr_ptr
;
151 unsigned long addrlen
;
154 augmented_sockaddr_syscall(connect
);
156 struct syscall_enter_sendto_args
{
157 unsigned long long common_tp_fields
;
163 struct sockaddr
*addr_ptr
;
167 augmented_sockaddr_syscall(sendto
);