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