Debian Codegraph @

utils/cap-audit/cap_audit.bpf.c

raw
1 // SPDX-License-Identifier: GPL-2.1-or-later
2 /*
3 * cap_audit.bpf.c - Capture capability checks for a target application
4 * Copyright (c) 2026 Red Hat Inc.
5 * All Rights Reserved.
6 *
7 * This software may be freely redistributed and/or modified under the
8 * terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * Authors:
18 * Steve Grubb <[email protected]>
19 * Portions of this code were made with codex 5.2
20 */
21
22 #include "vmlinux.h"
23
24 #ifndef CAP_OPT_NOAUDIT
25 #define CAP_OPT_NOAUDIT 2
26 #endif
27
28 #include <bpf/bpf_core_read.h>
29 #include <bpf/bpf_helpers.h>
30 #include <bpf/bpf_tracing.h>
31
32 /*
33 * BPF overview:
34 * The BPF side attaches to capability helpers (cap_capable, ns_capable,
35 * capable) and syscall tracepoints to capture capability checks only for a
36 * target process tree.
37 *
38 * The design challenge is noise filtering: distinguish capability checks
39 * caused by the target application from kernel-internal checks running under
40 * the same PID. The first filtering layer lives entirely in this BPF program
41 * and is encoded in target_pids map values.
42 *
43 * target_pids values are phases, not just booleans:
44 * 0 = not traced
45 * 1 = pre-exec (PID registered, but exec transition not complete)
46 * 2 = post-exec (target image is running; capability events are recordable)
47 *
48 * Phase 1 suppresses all capability events for the initial child so checks
49 * between fork() and execve() are dropped. That removes pre-exec noise such
50 * as DAC_READ_SEARCH from PATH traversal while resolving the target binary.
51 *
52 * The transition point is sched_process_exec. That tracepoint fires in
53 * begin_new_exec() after the point-of-no-return where the old image is gone
54 * and the new executable is committed. This is a kernel-level signal, so it
55 * works regardless of libc/toolchain choice (glibc, musl, static, scripts).
56 *
57 * Fork inherits the parent's phase value. Children of an already running
58 * target (phase 2) begin recording immediately, while children spawned before
59 * the initial exec remain in phase 1 until their own exec transition.
60 *
61 * raw_syscalls/sys_enter and sys_exit use should_trace_pid() (phase > 0)
62 * so syscall context is available the instant the exec gate opens.
63 * Capability hooks use should_record_pid() (phase >= 2), so only post-exec
64 * capability checks are emitted.
65 *
66 * For traced tasks, the program builds cap_event records with task identity,
67 * syscall context, namespace inode, the CAP_OPT_* flags passed to
68 * cap_capable(), and stack id; tracks per-capability statistics; and
69 * streams finalized events to userspace through a ring buffer. The cap_opts
70 * field carries the CAP_OPT_* flags so userspace can correlate known
71 * advisory call sites without treating CAP_OPT_NOAUDIT as a standalone
72 * filter. CAP_OPT_NOAUDIT means "do not audit" and is only a confirming
73 * signal alongside syscall and capability matching. Fork/exit tracepoints
74 * keep the PID filter in sync so children are traced and exits are pruned.
75 */
76
77 #ifndef PERF_MAX_STACK_DEPTH
78 #define PERF_MAX_STACK_DEPTH 127
79 #endif
80
81 #if !defined(__TARGET_ARCH_x86) && !defined(__TARGET_ARCH_arm64) && \
82 !defined(__TARGET_ARCH_arm) && !defined(__TARGET_ARCH_powerpc) && \
83 !defined(__TARGET_ARCH_s390) && !defined(__TARGET_ARCH_riscv) && \
84 !defined(__TARGET_ARCH_mips) && !defined(__TARGET_ARCH_loongarch)
85 #if defined(__x86_64__) || defined(__i386__)
86 #define __TARGET_ARCH_x86
87 #elif defined(__aarch64__)
88 #define __TARGET_ARCH_arm64
89 #elif defined(__arm__)
90 #define __TARGET_ARCH_arm
91 #elif defined(__powerpc__)
92 #define __TARGET_ARCH_powerpc
93 #elif defined(__s390x__) || defined(__s390__)
94 #define __TARGET_ARCH_s390
95 #elif defined(__riscv)
96 #define __TARGET_ARCH_riscv
97 #elif defined(__mips__)
98 #define __TARGET_ARCH_mips
99 #elif defined(__loongarch64)
100 #define __TARGET_ARCH_loongarch
101 #else
102 #define __TARGET_ARCH_x86
103 #endif
104 #endif
105
106 char LICENSE[] SEC("license") = "GPL";
107
108 struct cap_event {
109 __u64 timestamp_ns;
110 __u32 pid;
111 __u32 tid;
112 __u32 uid;
113 __u32 gid;
114 int capability;
115 int result;
116 int syscall_nr;
117 char comm[TASK_COMM_LEN];
118 __u64 stack_id;
119 __u32 targ_ns_inum;
120 __u32 cap_opts;
121 };
122
123 struct cap_stats {
124 __u64 checks;
125 __u64 granted;
126 __u64 denied;
127 };
128
129 // This sets the limit for how many child processes can be traced.
130 // Because of this limit, the tracer may not be suitable for shell
131 // scripts or long running process that fork child handlers that
132 // terminate soon after launching. When this fills up, no more
133 // children will be traced. This is the breaking point for long
134 // running apps. The other limits aren't as likely to be broken.
135 // This is approx 16 bytes per entry. (Default uses 128K of memory)
136 struct {
137 __uint(type, BPF_MAP_TYPE_HASH);
138 __type(key, __u32);
139 __type(value, __u8);
140 __uint(max_entries, 8192);
141 } target_pids SEC(".maps");
142
143 // This declares the size of the ring buf that holds events for
144 // userspace to access.
145 struct {
146 __uint(type, BPF_MAP_TYPE_RINGBUF);
147 __uint(max_entries, 256 * 1024);
148 } cap_events SEC(".maps");
149
150 // This declares how many unique stack traces to hold. This is used
151 // to determine which syscall a capability was requested from. If this
152 // fills up, no more stack traces will be collected. This is about
153 // 1K per entry. (Default uses 20 MB of memory)
154 struct {
155 __uint(type, BPF_MAP_TYPE_STACK_TRACE);
156 __uint(key_size, sizeof(__u32));
157 __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(__u64));
158 __uint(max_entries, 20000);
159 } stack_traces SEC(".maps");
160
161 // This declares how many capabilities can be watched. As of the 6.18
162 // kernel, it only uses 40. So, 64 is future proof as none have been added
163 // in a while.
164 struct {
165 __uint(type, BPF_MAP_TYPE_ARRAY);
166 __type(key, __u32);
167 __type(value, struct cap_stats);
168 __uint(max_entries, 64);
169 } capability_stats SEC(".maps");
170
171 // The cap_events_inflight map uses pid_tgid as the key. There is a race
172 // scenario when deep syscall chains that check multiple capabilities
173 // or nested function calls where each checks a capability. In these cases
174 // because it uses the same pid_tgid, it can overwrite a previous event.
175 // example:
176 //
177 // In kernel, during mount():
178 // sys_mount() {
179 // First check
180 // if (!capable(CAP_SYS_ADMIN)) // ← kprobe #1 fires
181 // return -EPERM;
182 //
183 // Path resolution might trigger
184 // if (!capable(CAP_DAC_OVERRIDE)) // ← kprobe #2 fires BEFORE kretprobe #1!
185 // return -EACCES;
186 // ... more work ...
187 //
188 // return 0; // ← kretprobe #1 and #2 fire
189 //}
190 //
191 // Statistics are SAFE: The capability_stats map is updated immediately in
192 // the kprobe using atomic operations, so counts are always accurate. The
193 // probability is higher for complex syscalls like mount, setuid, or network
194 // operations.
195 //
196 // Possible solutions
197 // Option 1: Per-CPU Map - change to BPF_MAP_TYPE_PERCPU_HASH. Drawback is
198 // it uses a map for each CPU so if 64 cores, map is 64KB of memory.
199 //
200 // Option 2: Include Stack Pointer in Key -
201 // key[0] = bpf_get_current_pid_tgid();
202 // key[1] = PT_REGS_SP(ctx); // Stack pointer makes it unique
203 // bpf_map_update_elem(&cap_events_inflight, &key, ev, BPF_ANY);
204 //
205 // Option 3: Accept the Race (Current Approach)
206 // Rationale:
207 // * The capability_stats map is always correct (atomic updates)
208 // * Individual event details might be wrong, but aggregate data is right
209 // * For the tool's purpose (determining required capabilities), statistics
210 // are what matter
211 // * Individual events are mainly for debugging/verbose mode
212
213 struct {
214 __uint(type, BPF_MAP_TYPE_HASH);
215 __type(key, __u64);
216 __type(value, struct cap_event);
217 __uint(max_entries, 1024);
218 } cap_events_inflight SEC(".maps");
219
220 // In theory, if sys_exit is not called, a syscall can leak. This can
221 // happen due to SIGKILL or a core dump. This might matter if this is
222 // tracing a long running with many threads some of which get SIGKILL.
223 // Might be more likely if its a child process rather than a thread.
224 // Because each run of the tracer is a new instance, the only concern
225 // is long tracing sessions. If this really was a concern, we could
226 // change to BPF_MAP_TYPE_PERCPU_HASH so that a leak on CPU0 doesn't
227 // affect tracing on CPU1. This is just mentioned here because it is
228 // an esoteric problem and not likely to show up. But this documents
229 // it and a possible solution. The drawback is that it uses more memory.
230 struct {
231 __uint(type, BPF_MAP_TYPE_HASH);
232 __type(key, __u64);
233 __type(value, int);
234 __uint(max_entries, 4096);
235 } current_syscalls SEC(".maps");
236
237 static __always_inline int get_pid_phase(__u32 pid)
238 {
239 __u8 *val = bpf_map_lookup_elem(&target_pids, &pid);
240
241 return val ? *val : 0;
242 }
243
244 /*
245 * should_trace_pid - check if the current PID is in the target set.
246 * @pid: process ID of the current task.
247 *
248 * Looks up the PID in target_pids and returns 1 when tracing is enabled for
249 * it, otherwise 0.
250 */
251 static __always_inline int should_trace_pid(__u32 pid)
252 {
253 return get_pid_phase(pid) > 0;
254 }
255
256 /*
257 * should_record_pid - check if the current PID is post-exec.
258 * @pid: process ID of the current task.
259 *
260 * Returns 1 for traced PIDs that have completed exec.
261 */
262 static __always_inline int should_record_pid(__u32 pid)
263 {
264 return get_pid_phase(pid) >= 2;
265 }
266
267 /*
268 * record_stats - increment capability check count.
269 * @cap: capability number.
270 *
271 * Increments the "checks" counter for the capability in capability_stats.
272 * Out-of-range capability numbers are ignored. Returns nothing.
273 */
274 static __always_inline void record_stats(int cap)
275 {
276 __u32 key;
277 struct cap_stats *stats;
278
279 if (cap < 0 || cap >= 64)
280 return;
281
282 key = (__u32)cap;
283 stats = bpf_map_lookup_elem(&capability_stats, &key);
284 if (stats)
285 __sync_fetch_and_add(&stats->checks, 1);
286 else {
287 struct cap_stats new_stats = { 0 };
288
289 new_stats.checks = 1;
290 if (!bpf_map_update_elem(&capability_stats, &key,
291 &new_stats, BPF_NOEXIST))
292 return;
293
294 stats = bpf_map_lookup_elem(&capability_stats, &key);
295 if (stats)
296 __sync_fetch_and_add(&stats->checks, 1);
297 }
298 }
299
300 /*
301 * update_result_stats - record whether a capability check succeeded.
302 * @cap: capability number from the in-flight event.
303 * @ret: return value from the capability helper (0 = granted).
304 *
305 * Updates the granted/denied counters for the capability when a matching
306 * entry already exists. Out-of-range capability numbers are ignored.
307 */
308 static __always_inline void update_result_stats(int cap, int ret)
309 {
310 __u32 key;
311 struct cap_stats *stats;
312
313 if (cap < 0 || cap >= 64)
314 return;
315
316 key = (__u32)cap;
317 stats = bpf_map_lookup_elem(&capability_stats, &key);
318 if (!stats)
319 return;
320
321 if (!ret)
322 __sync_fetch_and_add(&stats->granted, 1);
323 else
324 __sync_fetch_and_add(&stats->denied, 1);
325 }
326
327 /*
328 * read_syscall - fetch the syscall number for the current task.
329 * @ctx: pt_regs provided by the kprobe.
330 *
331 * Uses a per-thread map populated by sys_enter tracepoints when available,
332 * and falls back to architecture-specific pt_regs fields. Returns the syscall
333 * number or -1 when it cannot be determined.
334 */
335 static __always_inline int read_syscall(struct pt_regs *ctx)
336 {
337 __u64 pid_tgid;
338 int *nr;
339
340 pid_tgid = bpf_get_current_pid_tgid();
341 nr = bpf_map_lookup_elem(&current_syscalls, &pid_tgid);
342 if (nr)
343 return *nr;
344
345 #ifdef __TARGET_ARCH_x86
346 return BPF_CORE_READ(ctx, orig_ax);
347 #elif defined(__TARGET_ARCH_arm64)
348 return BPF_CORE_READ(ctx, syscallno);
349 #elif defined(__TARGET_ARCH_powerpc)
350 return BPF_CORE_READ(ctx, gpr[0]);
351 #elif defined(__TARGET_ARCH_s390)
352 return BPF_CORE_READ(ctx, gprs[2]);
353 #else
354 return -1;
355 #endif
356 }
357
358 /*
359 * fill_event_common - populate the static fields of a cap_event.
360 * @ev: event structure to fill.
361 * @ctx: pt_regs from the capability hook.
362 * @cap: capability number being checked.
363 *
364 * Captures PID/TID, timestamp, UID/GID, command name, syscall number, and
365 * user stack id for the current task.
366 */
367 static __always_inline void fill_event_common(struct cap_event *ev,
368 struct pt_regs *ctx, int cap)
369 {
370 __u64 pid_tgid;
371 __u64 uid_gid;
372
373 pid_tgid = bpf_get_current_pid_tgid();
374 ev->pid = pid_tgid >> 32;
375 ev->tid = (__u32)pid_tgid;
376 ev->capability = cap;
377 ev->timestamp_ns = bpf_ktime_get_ns();
378
379 uid_gid = bpf_get_current_uid_gid();
380 ev->uid = uid_gid >> 32;
381 ev->gid = (__u32)uid_gid;
382 bpf_get_current_comm(&ev->comm, sizeof(ev->comm));
383
384 ev->syscall_nr = read_syscall(ctx);
385 ev->stack_id = bpf_get_stackid(ctx, &stack_traces,
386 BPF_F_USER_STACK);
387 }
388
389 /*
390 * stash_event - store a partially filled event until the kretprobe fires.
391 * @ev: event to stash.
392 *
393 * Keeps the event keyed by pid_tgid in cap_events_inflight so the return
394 * probe can finalize result status before emitting to userspace.
395 */
396 static __always_inline void stash_event(struct cap_event *ev)
397 {
398 __u64 pid_tgid;
399
400 pid_tgid = bpf_get_current_pid_tgid();
401 bpf_map_update_elem(&cap_events_inflight, &pid_tgid, ev,
402 BPF_ANY);
403 }
404
405 /*
406 * submit_event - finalize and emit a stashed event to the ring buffer.
407 * @ret: return code from the capability helper (0 = granted).
408 *
409 * Looks up the in-flight event, copies it to the ring buffer, sets the result
410 * flag (1 = granted, 0 = denied), and removes the temporary entry. Returns 0
411 * whether or not an event was emitted.
412 */
413 static __always_inline int submit_event(int ret)
414 {
415 __u64 pid_tgid;
416 struct cap_event *stored;
417 struct cap_event *out;
418
419 pid_tgid = bpf_get_current_pid_tgid();
420 stored = bpf_map_lookup_elem(&cap_events_inflight, &pid_tgid);
421 if (!stored)
422 return 0;
423
424 out = bpf_ringbuf_reserve(&cap_events, sizeof(*out), 0);
425 if (!out)
426 goto cleanup;
427
428 __builtin_memcpy(out, stored, sizeof(*out));
429 out->result = ret ? 0 : 1;
430
431 bpf_ringbuf_submit(out, 0);
432
433 cleanup:
434 bpf_map_delete_elem(&cap_events_inflight, &pid_tgid);
435 return 0;
436 }
437
438 /*
439 * handle_capable - common logic for capability helper entry probes.
440 * @ctx: pt_regs for the probed function.
441 * @cap: capability number under evaluation.
442 * @targ_ns: optional target namespace pointer (may be NULL).
443 *
444 * Filters by PID first; for traced tasks it records a stats increment,
445 * populates a cap_event with contextual information, and stashes it so the
446 * return probe can attach the result. Returns 0 to indicate the kprobe should
447 * allow normal execution to continue.
448 */
449 static __always_inline int handle_capable(struct pt_regs *ctx, int cap,
450 struct user_namespace *targ_ns,
451 unsigned int cap_opts)
452 {
453 struct cap_event ev = { 0 };
454 __u32 pid;
455
456 pid = bpf_get_current_pid_tgid() >> 32;
457 if (!should_record_pid(pid))
458 return 0;
459
460 /* Track how many times this capability was inspected. */
461 record_stats(cap);
462 /* Collect task identity, syscall, and stack trace information. */
463 fill_event_common(&ev, ctx, cap);
464 ev.cap_opts = (__u32)cap_opts;
465
466 if (targ_ns) {
467 struct ns_common *ns;
468
469 ns = (struct ns_common *)targ_ns;
470 ev.targ_ns_inum = BPF_CORE_READ(ns, inum);
471 }
472
473 /* Save event so the kretprobe can add the success/failure result. */
474 stash_event(&ev);
475 return 0;
476 }
477
478 /*
479 * trace_cap_capable - entry probe for cap_capable().
480 *
481 * Delegates to handle_capable(). Arguments mirror the kernel helper and the
482 * capability number, namespace pointer, and opts value are recorded for the
483 * in-flight event. Returns 0.
484 */
485 SEC("kprobe/cap_capable")
486 int BPF_KPROBE(trace_cap_capable, const struct cred *cred,
487 struct user_namespace *targ_ns, int cap, unsigned int opts)
488 {
489 return handle_capable(ctx, cap, targ_ns, opts);
490 }
491
492 /*
493 * trace_cap_capable_ret - return probe for cap_capable().
494 * @ret: kernel return value (0 = granted).
495 *
496 * Updates result statistics for the capability tied to this pid_tgid and
497 * emits the finalized event to userspace. Always returns 0.
498 */
499 SEC("kretprobe/cap_capable")
500 int BPF_KRETPROBE(trace_cap_capable_ret, int ret)
501 {
502 __u64 pid_tgid;
503 struct cap_event *stored;
504
505 pid_tgid = bpf_get_current_pid_tgid();
506 stored = bpf_map_lookup_elem(&cap_events_inflight, &pid_tgid);
507 if (stored)
508 update_result_stats(stored->capability, ret);
509
510 return submit_event(ret);
511 }
512
513 /*
514 * trace_ns_capable - entry probe for ns_capable().
515 *
516 * Uses handle_capable() to capture namespace-aware capability checks. Returns
517 * 0.
518 */
519 SEC("kprobe/ns_capable")
520 int BPF_KPROBE(trace_ns_capable, struct user_namespace *ns, int cap)
521 {
522 return handle_capable(ctx, cap, ns, 0);
523 }
524
525 /*
526 * trace_ns_capable_ret - return probe for ns_capable().
527 * @ret: kernel return value.
528 *
529 * Emits the stored event with the grant/deny result. Returns 0.
530 */
531 SEC("kretprobe/ns_capable")
532 int BPF_KRETPROBE(trace_ns_capable_ret, int ret)
533 {
534 return submit_event(ret);
535 }
536
537 /*
538 * trace_ns_capable_noaudit - entry probe for ns_capable_noaudit().
539 *
540 * Captures capability checks that bypass kernel audit logging but should be
541 * observed by the auditor. Returns 0.
542 */
543 SEC("kprobe/ns_capable_noaudit")
544 int BPF_KPROBE(trace_ns_capable_noaudit, struct user_namespace *ns, int cap)
545 {
546 return handle_capable(ctx, cap, ns, 0);
547 }
548
549 /*
550 * trace_ns_capable_noaudit_ret - return probe for ns_capable_noaudit().
551 * @ret: kernel return value.
552 *
553 * Finalizes and emits the stored event. Returns 0.
554 */
555 SEC("kretprobe/ns_capable_noaudit")
556 int BPF_KRETPROBE(trace_ns_capable_noaudit_ret, int ret)
557 {
558 return submit_event(ret);
559 }
560
561 /*
562 * trace_capable - entry probe for capable().
563 *
564 * Handles capability checks that do not involve namespaces. Returns 0.
565 */
566 SEC("kprobe/capable")
567 int BPF_KPROBE(trace_capable, int cap)
568 {
569 return handle_capable(ctx, cap, NULL, 0);
570 }
571
572 /*
573 * trace_capable_ret - return probe for capable().
574 * @ret: kernel return value.
575 *
576 * Emits the stored capability event. Returns 0.
577 */
578 SEC("kretprobe/capable")
579 int BPF_KRETPROBE(trace_capable_ret, int ret)
580 {
581 return submit_event(ret);
582 }
583
584 /*
585 * trace_sys_enter - remember syscall numbers on entry.
586 * @ctx: raw_syscalls/sys_enter tracepoint context.
587 *
588 * Stores the syscall number in a per-thread map for later lookup by the
589 * capability probes. No-op for non-traced PIDs. Returns 0.
590 */
591 SEC("tracepoint/raw_syscalls/sys_enter")
592 int trace_sys_enter(struct trace_event_raw_sys_enter *ctx)
593 {
594 __u64 pid_tgid;
595 __u32 pid;
596 __u32 id;
597
598 pid_tgid = bpf_get_current_pid_tgid();
599 pid = pid_tgid >> 32;
600 if (!should_trace_pid(pid))
601 return 0;
602
603 id = ctx->id;
604 bpf_map_update_elem(&current_syscalls, &pid_tgid, &id, BPF_ANY);
605 return 0;
606 }
607
608 /*
609 * trace_sys_exit - clear syscall tracking on exit.
610 * @ctx: raw_syscalls/sys_exit tracepoint context.
611 *
612 * Removes the stored syscall number for the thread when tracing. Returns 0.
613 */
614 SEC("tracepoint/raw_syscalls/sys_exit")
615 int trace_sys_exit(struct trace_event_raw_sys_exit *ctx)
616 {
617 __u64 pid_tgid;
618 __u32 pid;
619
620 pid_tgid = bpf_get_current_pid_tgid();
621 pid = pid_tgid >> 32;
622 if (!should_trace_pid(pid))
623 return 0;
624
625 bpf_map_delete_elem(&current_syscalls, &pid_tgid);
626 return 0;
627 }
628
629 /*
630 * trace_sched_process_fork - follow new child processes.
631 * @ctx: sched_process_fork tracepoint data.
632 *
633 * When a traced parent forks, automatically add the child PID to the filter
634 * map so subsequent capability checks are captured. Returns 0.
635 */
636 SEC("tracepoint/sched/sched_process_fork")
637 int trace_sched_process_fork(struct trace_event_raw_sched_process_fork *ctx)
638 {
639 __u32 parent_pid;
640 __u32 child_pid;
641 __u8 *parent_val;
642
643 parent_pid = ctx->parent_pid;
644 child_pid = ctx->child_pid;
645
646 parent_val = bpf_map_lookup_elem(&target_pids, &parent_pid);
647 if (parent_val) {
648 __u8 child_val = *parent_val;
649
650 bpf_map_update_elem(&target_pids, &child_pid, &child_val,
651 BPF_ANY);
652 }
653
654 return 0;
655 }
656
657 /*
658 * trace_sched_process_exec - mark a traced PID as post-exec.
659 * @ctx: sched_process_exec tracepoint data.
660 *
661 * Transitions the PID from pre-exec (phase 1) to post-exec (phase 2) once
662 * the new binary image is loaded.
663 */
664 SEC("tracepoint/sched/sched_process_exec")
665 int trace_sched_process_exec(struct trace_event_raw_sched_process_exec *ctx)
666 {
667 __u32 pid;
668 __u8 *val;
669 __u8 new_val = 2;
670
671 pid = bpf_get_current_pid_tgid() >> 32;
672 val = bpf_map_lookup_elem(&target_pids, &pid);
673 if (val)
674 bpf_map_update_elem(&target_pids, &pid, &new_val, BPF_ANY);
675
676 return 0;
677 }
678
679 /*
680 * trace_sched_process_exit - prune exited processes from the target set.
681 * @ctx: sched_process_exit tracepoint data.
682 *
683 * Removes the exiting PID from the target_pids map to prevent stale entries.
684 * Returns 0.
685 */
686 SEC("tracepoint/sched/sched_process_exit")
687 int trace_sched_process_exit(struct trace_event_raw_sched_process_template *ctx)
688 {
689 __u32 pid;
690
691 pid = ctx->pid;
692 bpf_map_delete_elem(&target_pids, &pid);
693
694 return 0;
695 }