66#include "d_path.h"
77#include "maps.h"
88#include "types.h"
9+ #include "raw_event.h"
910
1011#include <bpf/bpf_helpers.h>
1112#include <bpf/bpf_core_read.h>
@@ -77,52 +78,90 @@ __always_inline static const char* get_memory_cgroup(struct helper_t* helper) {
7778 return helper -> buf ;
7879}
7980
80- __always_inline static void process_fill_lineage (process_t * p , struct helper_t * helper , bool use_bpf_d_path ) {
81+ __always_inline static long process_fill_lineage (struct raw_event_t * event , struct helper_t * helper , bool use_bpf_d_path ) {
8182 struct task_struct * task = (struct task_struct * )bpf_get_current_task_btf ();
82- p -> lineage_len = 0 ;
83+ uint16_t lineage_len_pos = event -> len ;
84+ event -> len += 2 ;
8385
84- for (int i = 0 ; i < LINEAGE_MAX ; i ++ ) {
86+ uint16_t i = 0 ;
87+ for (; i < LINEAGE_MAX ; i ++ ) {
8588 struct task_struct * parent = task -> real_parent ;
8689
8790 if (task == parent || parent -> pid == 0 ) {
88- return ;
91+ break ;
8992 }
9093 task = parent ;
9194
92- p -> lineage [i ].uid = task -> cred -> uid .val ;
93-
94- d_path (& task -> mm -> exe_file -> f_path , p -> lineage [i ].exe_path , PATH_MAX , use_bpf_d_path );
95- p -> lineage_len ++ ;
95+ raw_event_copy_uint (event , task -> cred -> uid .val );
96+ long err = raw_event_d_path (event , & task -> mm -> exe_file -> f_path , use_bpf_d_path );
97+ if (err != 0 ) {
98+ bpf_printk ("Failed to read lineage exe_path" );
99+ return err ;
100+ }
96101 }
102+
103+ // go back and set the amount of lineage processes in the buffer
104+ uint16_t back = event -> len ;
105+ event -> len = lineage_len_pos ;
106+
107+ raw_event_copy_uint (event , i );
108+
109+ event -> len = back ;
110+ return 0 ;
97111}
98112
99113__always_inline static unsigned long get_mount_ns () {
100114 struct task_struct * task = (struct task_struct * )bpf_get_current_task_btf ();
101115 return task -> nsproxy -> mnt_ns -> ns .inum ;
102116}
103117
104- __always_inline static int64_t process_fill (process_t * p , bool use_bpf_d_path ) {
118+ /**
119+ * Fill in the information about the current process to the event
120+ * buffer.
121+ *
122+ * This method serializes all required process information for the event
123+ * as a binary blob into the provided event buffer. The serialized data
124+ * will look something like this:
125+ * |--|--|--|--|-------|--------------|-------------|------------|-|----|---|
126+ * | | | | | | | | | | ^ grandparent lineage
127+ * | | | | | | | | | ^ parent lineage
128+ * | | | | | | | | ^ in_root_mount_ns
129+ * | | | | | | | ^ cgroup
130+ * | | | | | | ^ executable path
131+ * | | | | | ^ arguments
132+ * | | | | ^ comm
133+ * | | | ^ pid
134+ * | | ^ loginuid
135+ * | ^ gid
136+ * ^ uid
137+ */
138+ __always_inline static int64_t process_fill (struct raw_event_t * event , bool use_bpf_d_path ) {
105139 struct task_struct * task = (struct task_struct * )bpf_get_current_task_btf ();
106140 uint32_t key = 0 ;
107141 uint64_t uid_gid = bpf_get_current_uid_gid ();
108- p -> uid = uid_gid & 0xFFFFFFFF ;
109- p -> gid = ( uid_gid >> 32 ) & 0xFFFFFFFF ;
110- p -> login_uid = task -> loginuid .val ;
111- p -> pid = (bpf_get_current_pid_tgid () >> 32 ) & 0xFFFFFFFF ;
112- u_int64_t err = bpf_get_current_comm ( p -> comm , TASK_COMM_LEN );
142+ raw_event_copy_u32 ( event , uid_gid & 0xFFFFFFFF ) ;
143+ raw_event_copy_u32 ( event , (( uid_gid >> 32 ) & 0xFFFFFFFF )) ;
144+ raw_event_copy_uint ( event , task -> loginuid .val ) ;
145+ raw_event_copy_u32 ( event , (bpf_get_current_pid_tgid () >> 32 ) & 0xFFFFFFFF ) ;
146+ uint64_t err = raw_event_copy_comm ( event );
113147 if (err != 0 ) {
114148 bpf_printk ("Failed to fill task comm" );
115149 return err ;
116150 }
117151
118152 unsigned long arg_start = task -> mm -> arg_start ;
119153 unsigned long arg_end = task -> mm -> arg_end ;
120- p -> args_len = (arg_end - arg_start ) & 0xFFF ;
121- p -> args [4095 ] = '\0' ; // Ensure string termination at end of buffer
122- err = bpf_probe_read_user (p -> args , p -> args_len , (const char * )arg_start );
123- if (err != 0 ) {
124- bpf_printk ("Failed to fill task args" );
125- return err ;
154+ uint16_t args_len = (arg_end - arg_start ) & 0xFFF ;
155+ err = raw_event_copy_buffer (event , (const void * )arg_start , args_len );
156+ if (err < 0 ) {
157+ bpf_printk ("Failed to read process args" );
158+ return -1 ;
159+ }
160+
161+ err = raw_event_d_path (event , & task -> mm -> exe_file -> f_path , use_bpf_d_path );
162+ if (err < 0 ) {
163+ bpf_printk ("Failed to read exe_path" );
164+ return -1 ;
126165 }
127166
128167 struct helper_t * helper = bpf_map_lookup_elem (& helper_map , & key );
@@ -131,16 +170,26 @@ __always_inline static int64_t process_fill(process_t* p, bool use_bpf_d_path) {
131170 return -1 ;
132171 }
133172
134- p -> exe_path_len = d_path (& task -> mm -> exe_file -> f_path , p -> exe_path , PATH_MAX , use_bpf_d_path );
135-
136173 const char * cg = get_memory_cgroup (helper );
137174 if (cg != NULL ) {
138- bpf_probe_read_str (p -> memory_cgroup , PATH_MAX , cg );
175+ // Reserve space for the cgroup length
176+ event -> len += 2 ;
177+ uint16_t cg_len = (uint16_t )bpf_probe_read_str (& event -> buf [event -> len ], PATH_MAX , cg );
178+
179+ // Move back and fix the length
180+ event -> len -= 2 ;
181+ raw_event_copy_u16 (event , cg_len - 1 );
182+
183+ // Forward past the cgroup
184+ event -> len += ((cg_len - 1 ) & (PATH_MAX - 1 ));
139185 }
140186
141- p -> in_root_mount_ns = get_mount_ns () == host_mount_ns ;
187+ raw_event_copy_u8 ( event , get_mount_ns () == host_mount_ns ) ;
142188
143- process_fill_lineage (p , helper , use_bpf_d_path );
189+ err = process_fill_lineage (event , helper , use_bpf_d_path );
190+ if (err < 0 ) {
191+ return -1 ;
192+ }
144193
145194 return 0 ;
146195}
0 commit comments