@@ -151,6 +151,15 @@ struct
151
151
__uint (max_entries , MAX_PENDING_DEAD_PARENTS );
152
152
} orphans_map SEC (".maps" );
153
153
154
+ /*
155
+ Buffer for reading container info of a process. The Container ID is located at `buf[id_offset]`
156
+ */
157
+ struct container_id_buffer
158
+ {
159
+ char buf [CONTAINER_ID_MAX_BUF ];
160
+ int id_offset ;
161
+ };
162
+
154
163
/*
155
164
Identifies the container engine and reads the cgroup id of a process
156
165
from its `task_struct` into an given array of character.
@@ -182,8 +191,7 @@ The array size MUST be greater than 72.
182
191
of the process after a successful parse of a `container`
183
192
cgroup name for the given process
184
193
*/
185
- static __always_inline int get_container_info (struct task_struct * cur_tsk ,
186
- char * buf , int * offset )
194
+ static __always_inline int get_container_info (struct task_struct * cur_tsk , struct container_id_buffer * c_id_buf )
187
195
{
188
196
int cgrp_id ;
189
197
char buf_parent [CONTAINER_ID_MAX_BUF ];
@@ -198,9 +206,9 @@ static __always_inline int get_container_info(struct task_struct *cur_tsk,
198
206
199
207
struct kernfs_node * kn = BPF_CORE_READ (cur_tsk , cgroups , subsys [cgrp_id ], cgroup , kn );
200
208
const char * name = BPF_CORE_READ (kn , name );
201
- if (bpf_probe_read_kernel_str (buf , CONTAINER_ID_MAX_BUF , name ) < 0 )
209
+ if (bpf_probe_read_kernel_str (c_id_buf -> buf , CONTAINER_ID_MAX_BUF , name ) < 0 )
202
210
{
203
- LOG_ERROR ("failed to get kernfs node name: %s\n" , buf );
211
+ LOG_ERROR ("failed to get kernfs node name: %s\n" , c_id_buf -> buf );
204
212
return FAILED_READ_CGROUP_NAME ;
205
213
}
206
214
@@ -227,9 +235,9 @@ static __always_inline int get_container_info(struct task_struct *cur_tsk,
227
235
// `docker` and the child node contains the container ID.
228
236
229
237
// Case 1.
230
- if (STRNCMP (buf , 7 , "docker-" ) == 0 )
238
+ if (STRNCMP (c_id_buf -> buf , 7 , "docker-" ) == 0 )
231
239
{
232
- * offset = 7 ;
240
+ c_id_buf -> id_offset = 7 ;
233
241
return DOCKER_CONTAINER_ENGINE ;
234
242
}
235
243
@@ -240,36 +248,36 @@ static __always_inline int get_container_info(struct task_struct *cur_tsk,
240
248
if (STRNCMP (buf_parent , 6 , "docker" ) == 0 && buf_parent [6 ] == '\0' )
241
249
{
242
250
// The last node is unprefixed, it contains just container ID.
243
- * offset = 0 ;
251
+ c_id_buf -> id_offset = 0 ;
244
252
return DOCKER_CONTAINER_ENGINE ;
245
253
}
246
254
247
255
// Podman case
248
256
//
249
257
// the check for NULL character is needed to avoid collisions with
250
258
// `containerd-` prefixed cgroup name
251
- if (STRNCMP (buf , 9 , "container" ) == 0 && buf [9 ] == '\0' )
259
+ if (STRNCMP (c_id_buf -> buf , 9 , "container" ) == 0 && c_id_buf -> buf [9 ] == '\0' )
252
260
{
253
261
// Read `parent_name` to the main buffer `buf`.
254
- if (parent_name == NULL || bpf_probe_read_kernel_str (buf , CONTAINER_ID_MAX_BUF , parent_name ) < 0 )
262
+ if (parent_name == NULL || bpf_probe_read_kernel_str (c_id_buf -> buf , CONTAINER_ID_MAX_BUF , parent_name ) < 0 )
255
263
{
256
- LOG_ERROR ("failed to get parent kernfs node name: %s\n" , buf );
264
+ LOG_ERROR ("failed to get parent kernfs node name: %s\n" , c_id_buf -> buf );
257
265
return FAILED_READ_PARENT_CGROUP_NAME ;
258
266
}
259
267
260
- if (STRNCMP (buf , 7 , "libpod-" ) == 0 )
268
+ if (STRNCMP (c_id_buf -> buf , 7 , "libpod-" ) == 0 )
261
269
{
262
- * offset = 7 ;
270
+ c_id_buf -> id_offset = 7 ;
263
271
return PODMAN_CONTAINER_ENGINE ;
264
272
}
265
273
266
274
// Error podman step 2
267
- LOG_ERROR ("failed parsing libpod id: %s\n" , buf );
275
+ LOG_ERROR ("failed parsing libpod id: %s\n" , c_id_buf -> buf );
268
276
return FAILED_PARSE_LIBPOD_CGROUP_NAME ;
269
277
}
270
278
271
279
// No container or unknown container engine
272
- LOG_DEBUG ("no container or unknown container engine. id: %s\n" , buf );
280
+ LOG_DEBUG ("no container or unknown container engine. id: %s\n" , c_id_buf -> buf );
273
281
return UNKNOWN_CONTAINER_ENGINE ;
274
282
}
275
283
@@ -282,7 +290,7 @@ int BPF_PROG(sched_process_fork, struct task_struct *parent,
282
290
pid_t parent_tgid = BPF_CORE_READ (parent , tgid );
283
291
pid_t child_tgid = BPF_CORE_READ (child , tgid );
284
292
285
- char buf [ CONTAINER_ID_MAX_BUF ] ;
293
+ struct container_id_buffer c_id_buf ;
286
294
287
295
// if parent process group matches the child one, we're forking a thread
288
296
// and we ignore the event.
@@ -311,8 +319,7 @@ int BPF_PROG(sched_process_fork, struct task_struct *parent,
311
319
event -> fork .namespaces .time = BPF_CORE_READ (child , nsproxy , time_ns , ns .inum );
312
320
event -> fork .namespaces .cgroup = BPF_CORE_READ (child , nsproxy , cgroup_ns , ns .inum );
313
321
314
- int id_offset ;
315
- int container_engine = get_container_info (child , buf , & id_offset );
322
+ int container_engine = get_container_info (child , & c_id_buf );
316
323
if (container_engine < 0 )
317
324
{
318
325
// TODO: print error ??
@@ -327,9 +334,9 @@ int BPF_PROG(sched_process_fork, struct task_struct *parent,
327
334
event -> fork .option_index .container_id .container_engine = container_engine ;
328
335
buffer_index_init (& event -> buffer , & event -> fork .option_index .container_id .cgroup_id );
329
336
buffer_append_str (& event -> buffer , & event -> fork .option_index .container_id .cgroup_id ,
330
- buf + id_offset , CONTAINER_ID_MAX_BUF );
337
+ c_id_buf . buf + c_id_buf . id_offset , CONTAINER_ID_MAX_BUF );
331
338
332
- LOG_DEBUG ("fork - detected container with id: %s" , buf + id_offset );
339
+ LOG_DEBUG ("fork - detected container with id: %s" , c_id_buf . buf + c_id_buf . id_offset );
333
340
}
334
341
335
342
output_process_event (ctx , event );
@@ -342,7 +349,7 @@ int BPF_PROG(sched_process_exec, struct task_struct *p, pid_t old_pid,
342
349
{
343
350
pid_t tgid = bpf_get_current_pid_tgid () >> 32 ;
344
351
345
- char buf [ CONTAINER_ID_MAX_BUF ] ;
352
+ struct container_id_buffer c_id_buf ;
346
353
347
354
struct process_event * event = init_process_event (EVENT_EXEC , tgid );
348
355
if (!event )
@@ -360,8 +367,7 @@ int BPF_PROG(sched_process_exec, struct task_struct *p, pid_t old_pid,
360
367
event -> exec .namespaces .time = BPF_CORE_READ (p , nsproxy , time_ns , ns .inum );
361
368
event -> exec .namespaces .cgroup = BPF_CORE_READ (p , nsproxy , cgroup_ns , ns .inum );
362
369
363
- int id_offset ;
364
- int container_engine = get_container_info (p , buf , & id_offset );
370
+ int container_engine = get_container_info (p , & c_id_buf );
365
371
if (container_engine < 0 )
366
372
{
367
373
event -> exec .option_index .discriminant = OPTION_NONE ;
@@ -374,9 +380,9 @@ int BPF_PROG(sched_process_exec, struct task_struct *p, pid_t old_pid,
374
380
event -> exec .option_index .container_id .container_engine = container_engine ;
375
381
buffer_index_init (& event -> buffer , & event -> exec .option_index .container_id .cgroup_id );
376
382
buffer_append_str (& event -> buffer , & event -> exec .option_index .container_id .cgroup_id ,
377
- buf + id_offset , CONTAINER_ID_MAX_BUF );
383
+ c_id_buf . buf + c_id_buf . id_offset , CONTAINER_ID_MAX_BUF );
378
384
379
- LOG_DEBUG ("exec - detected container with id: %s" , buf + id_offset );
385
+ LOG_DEBUG ("exec - detected container with id: %s" , c_id_buf . buf + c_id_buf . id_offset );
380
386
}
381
387
382
388
// This is needed because the first MAX_IMAGE_LEN bytes of buffer will
0 commit comments