@@ -10,6 +10,14 @@ struct bpf_map_def SEC("maps/proc-events") events = {
10
10
.max_entries = 256 , // max cpus
11
11
};
12
12
13
+ struct bpf_map_def SEC ("maps/execMap" ) execMap = {
14
+ .type = BPF_MAP_TYPE_HASH ,
15
+ .key_size = sizeof (u32 ),
16
+ .value_size = sizeof (struct data_t ),
17
+ .max_entries = 256 ,
18
+ };
19
+
20
+
13
21
static __always_inline void new_event (struct data_t * data )
14
22
{
15
23
// initializing variables with __builtin_memset() is required
@@ -23,15 +31,28 @@ static __always_inline void new_event(struct data_t* data)
23
31
bpf_probe_read (& parent , sizeof (parent ), & task -> real_parent );
24
32
data -> pid = bpf_get_current_pid_tgid () >> 32 ;
25
33
26
- // FIXME: always 0?
27
34
#if !defined(__arm__ ) && !defined(__i386__ )
28
35
// on i686 -> invalid read from stack
29
- bpf_probe_read (& data -> ppid , sizeof (data -> ppid ), & parent -> tgid );
36
+ bpf_probe_read (& data -> ppid , sizeof (u32 ), & parent -> tgid );
30
37
#endif
31
38
data -> uid = bpf_get_current_uid_gid () & 0xffffffff ;
32
39
bpf_get_current_comm (& data -> comm , sizeof (data -> comm ));
33
40
};
34
41
42
+ static __always_inline void __handle_exit_execve (struct trace_sys_exit_execve * ctx )
43
+ {
44
+ u64 pid_tgid = bpf_get_current_pid_tgid ();
45
+ struct data_t * proc = bpf_map_lookup_elem (& execMap , & pid_tgid );
46
+ if (proc == NULL ) { return ; }
47
+ if (ctx -> ret != 0 ) { goto out ; }
48
+ proc -> ret_code = ctx -> ret ;
49
+
50
+ bpf_perf_event_output (ctx , & events , BPF_F_CURRENT_CPU , proc , sizeof (* proc ));
51
+
52
+ out :
53
+ bpf_map_delete_elem (& execMap , & pid_tgid );
54
+ }
55
+
35
56
// https://0xax.gitbooks.io/linux-insides/content/SysCall/linux-syscall-4.html
36
57
// bprm_execve REGS_PARM3
37
58
// https://elixir.bootlin.com/linux/latest/source/fs/exec.c#L1796
@@ -50,16 +71,20 @@ int tracepoint__sched_sched_process_exit(struct pt_regs *ctx)
50
71
return 0 ;
51
72
};
52
73
53
- struct trace_sys_enter_execve {
54
- short common_type ;
55
- char common_flags ;
56
- char common_preempt_count ;
57
- int common_pid ;
58
- int __syscall_nr ;
59
- char * filename ;
60
- const char * const * argv ;
61
- const char * const * envp ;
74
+ SEC ("tracepoint/syscalls/sys_exit_execve" )
75
+ int tracepoint__syscalls_sys_exit_execve (struct trace_sys_exit_execve * ctx )
76
+ {
77
+ __handle_exit_execve (ctx );
78
+ return 0 ;
62
79
};
80
+
81
+ SEC ("tracepoint/syscalls/sys_exit_execveat" )
82
+ int tracepoint__syscalls_sys_exit_execveat (struct trace_sys_exit_execve * ctx )
83
+ {
84
+ __handle_exit_execve (ctx );
85
+ return 0 ;
86
+ };
87
+
63
88
SEC ("tracepoint/syscalls/sys_enter_execve" )
64
89
int tracepoint__syscalls_sys_enter_execve (struct trace_sys_enter_execve * ctx )
65
90
{
@@ -93,24 +118,19 @@ int tracepoint__syscalls_sys_enter_execve(struct trace_sys_enter_execve* ctx)
93
118
}
94
119
#endif
95
120
96
- // With some commands, this helper fails with error -28 (ENOSPC). Misleading error? cmd failed maybe?
97
- // BUG: after coming back from suspend state, this helper fails with error -95 (EOPNOTSUPP)
98
- // Possible workaround: count -95 errors, and from userspace reinitialize the streamer if errors >= n-errors
99
- bpf_perf_event_output (ctx , & events , BPF_F_CURRENT_CPU , data , sizeof (* data ));
121
+ // in case of failure adding the item to the map, send it directly
122
+ u64 pid_tgid = bpf_get_current_pid_tgid ();
123
+ if (bpf_map_update_elem (& execMap , & pid_tgid , data , BPF_ANY ) != 0 ) {
124
+
125
+ // With some commands, this helper fails with error -28 (ENOSPC). Misleading error? cmd failed maybe?
126
+ // BUG: after coming back from suspend state, this helper fails with error -95 (EOPNOTSUPP)
127
+ // Possible workaround: count -95 errors, and from userspace reinitialize the streamer if errors >= n-errors
128
+ bpf_perf_event_output (ctx , & events , BPF_F_CURRENT_CPU , data , sizeof (* data ));
129
+ }
130
+
100
131
return 0 ;
101
132
};
102
133
103
- struct trace_sys_enter_execveat {
104
- short common_type ;
105
- char common_flags ;
106
- char common_preempt_count ;
107
- int common_pid ;
108
- int __syscall_nr ;
109
- char * filename ;
110
- const char * const * argv ;
111
- const char * const * envp ;
112
- int flags ;
113
- };
114
134
SEC ("tracepoint/syscalls/sys_enter_execveat" )
115
135
int tracepoint__syscalls_sys_enter_execveat (struct trace_sys_enter_execveat * ctx )
116
136
{
@@ -140,10 +160,16 @@ int tracepoint__syscalls_sys_enter_execveat(struct trace_sys_enter_execveat* ctx
140
160
data -> args_count ++ ;
141
161
}
142
162
143
- // With some commands, this helper fails with error -28 (ENOSPC). Misleading error? cmd failed maybe?
144
- // BUG: after coming back from suspend state, this helper fails with error -95 (EOPNOTSUPP)
145
- // Possible workaround: count -95 errors, and from userspace reinitialize the streamer if errors >= n-errors
146
- bpf_perf_event_output (ctx , & events , BPF_F_CURRENT_CPU , data , sizeof (* data ));
163
+ // in case of failure adding the item to the map, send it directly
164
+ u64 pid_tgid = bpf_get_current_pid_tgid ();
165
+ if (bpf_map_update_elem (& execMap , & pid_tgid , data , BPF_ANY ) != 0 ) {
166
+
167
+ // With some commands, this helper fails with error -28 (ENOSPC). Misleading error? cmd failed maybe?
168
+ // BUG: after coming back from suspend state, this helper fails with error -95 (EOPNOTSUPP)
169
+ // Possible workaround: count -95 errors, and from userspace reinitialize the streamer if errors >= n-errors
170
+ bpf_perf_event_output (ctx , & events , BPF_F_CURRENT_CPU , data , sizeof (* data ));
171
+ }
172
+
147
173
return 0 ;
148
174
};
149
175
0 commit comments