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 struct bpf_map
SEC("maps") __augmented_syscalls__
= {
23 .type
= BPF_MAP_TYPE_PERF_EVENT_ARRAY
,
24 .key_size
= sizeof(int),
25 .value_size
= sizeof(u32
),
26 .max_entries
= __NR_CPUS__
,
29 struct syscall_exit_args
{
30 unsigned long long common_tp_fields
;
35 struct augmented_filename
{
41 #define augmented_filename_syscall(syscall) \
42 struct augmented_enter_##syscall##_args { \
43 struct syscall_enter_##syscall##_args args; \
44 struct augmented_filename filename; \
46 int syscall_enter(syscall)(struct syscall_enter_##syscall##_args *args) \
48 struct augmented_enter_##syscall##_args augmented_args = { .filename.reserved = 0, }; \
49 unsigned int len = sizeof(augmented_args); \
50 probe_read(&augmented_args.args, sizeof(augmented_args.args), args); \
51 augmented_args.filename.size = probe_read_str(&augmented_args.filename.value, \
52 sizeof(augmented_args.filename.value), \
53 args->filename_ptr); \
54 if (augmented_args.filename.size < sizeof(augmented_args.filename.value)) { \
55 len -= sizeof(augmented_args.filename.value) - augmented_args.filename.size; \
56 len &= sizeof(augmented_args.filename.value) - 1; \
58 perf_event_output(args, &__augmented_syscalls__, BPF_F_CURRENT_CPU, \
59 &augmented_args, len); \
62 int syscall_exit(syscall)(struct syscall_exit_args *args) \
64 return 1; /* 0 as soon as we start copying data returned by the kernel, e.g. 'read' */ \
67 struct syscall_enter_openat_args
{
68 unsigned long long common_tp_fields
;
76 augmented_filename_syscall(openat
);
78 struct syscall_enter_open_args
{
79 unsigned long long common_tp_fields
;
86 augmented_filename_syscall(open
);
88 struct syscall_enter_inotify_add_watch_args
{
89 unsigned long long common_tp_fields
;
96 augmented_filename_syscall(inotify_add_watch
);
100 struct syscall_enter_newstat_args
{
101 unsigned long long common_tp_fields
;
104 struct stat
*statbuf
;
107 augmented_filename_syscall(newstat
);
109 #ifndef _K_SS_MAXSIZE
110 #define _K_SS_MAXSIZE 128
113 #define augmented_sockaddr_syscall(syscall) \
114 struct augmented_enter_##syscall##_args { \
115 struct syscall_enter_##syscall##_args args; \
116 struct sockaddr_storage addr; \
118 int syscall_enter(syscall)(struct syscall_enter_##syscall##_args *args) \
120 struct augmented_enter_##syscall##_args augmented_args; \
121 unsigned long addrlen = sizeof(augmented_args.addr); \
122 probe_read(&augmented_args.args, sizeof(augmented_args.args), args); \
123 /* FIXME_CLANG_OPTIMIZATION_THAT_ACCESSES_USER_CONTROLLED_ADDRLEN_DESPITE_THIS_CHECK */ \
124 /* if (addrlen > augmented_args.args.addrlen) */ \
125 /* addrlen = augmented_args.args.addrlen; */ \
127 probe_read(&augmented_args.addr, addrlen, args->addr_ptr); \
128 perf_event_output(args, &__augmented_syscalls__, BPF_F_CURRENT_CPU, \
130 sizeof(augmented_args) - sizeof(augmented_args.addr) + addrlen); \
133 int syscall_exit(syscall)(struct syscall_exit_args *args) \
135 return 1; /* 0 as soon as we start copying data returned by the kernel, e.g. 'read' */ \
140 struct syscall_enter_bind_args
{
141 unsigned long long common_tp_fields
;
144 struct sockaddr
*addr_ptr
;
145 unsigned long addrlen
;
148 augmented_sockaddr_syscall(bind
);
150 struct syscall_enter_connect_args
{
151 unsigned long long common_tp_fields
;
154 struct sockaddr
*addr_ptr
;
155 unsigned long addrlen
;
158 augmented_sockaddr_syscall(connect
);
160 struct syscall_enter_sendto_args
{
161 unsigned long long common_tp_fields
;
167 struct sockaddr
*addr_ptr
;
171 augmented_sockaddr_syscall(sendto
);