Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ebpf): treat sched_process_exit corner case #4557

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/ebpf/c/tracee.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,24 @@ int tracepoint__sched__sched_process_exit(struct bpf_raw_tracepoint_args *ctx)
if (!init_program_data(&p, ctx, SCHED_PROCESS_EXIT))
return 0;

// The syscall number cannot be trusted in the following cases:
//
// 1. If the task was terminated due to a signal (PF_SIGNALED is set), the syscall
// context may be inconsistent.
//
// 2. If the task was not signaled:
// - A kernel thread (PF_KTHREAD is set) is not expected to have a valid syscall context, so
// the function init_program_data has already set its syscall number as NO_SYSCALL (-1).
// - If PF_KTHREAD is not set but the syscall value is negative, it may be due to
// an invalid or clobbered context.
//
// In any of these cases, we explicitly mark the syscall number as NO_SYSCALL (-1) to avoid
// misinterpretation.
int task_flags = get_task_flags(p.event->task);
if ((task_flags & PF_SIGNALED) ||
(!(task_flags & PF_KTHREAD) && (p.event->context.syscall < 0)))
p.event->context.syscall = NO_SYSCALL;

// evaluate matched_policies before removing this pid from the maps
evaluate_scope_filters(&p);

Expand Down
3 changes: 2 additions & 1 deletion pkg/ebpf/c/vmlinux_missing.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@

#define ICMPV6_ECHO_REQUEST 128

#define PF_KTHREAD 0x00200000 /* I am a kernel thread */
#define PF_SIGNALED 0x00000400 /* Killed by a signal */
#define PF_KTHREAD 0x00200000 /* I am a kernel thread */

#define TASK_COMM_LEN 16

Expand Down
3 changes: 1 addition & 2 deletions pkg/ebpf/events_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,9 @@ func (t *Tracee) decodeEvents(ctx context.Context, sourceChan chan []byte) (<-ch
id := events.ID(eCtx.Syscall)
syscallDef := events.Core.GetDefinitionByID(id)
if syscallDef.NotValid() {
// This should never fail, as the translation used in eBPF relies on the same event definitions
commStr := string(eCtx.Comm[:bytes.IndexByte(eCtx.Comm[:], 0)])
utsNameStr := string(eCtx.UtsName[:bytes.IndexByte(eCtx.UtsName[:], 0)])
logger.Errorw(
logger.Debugw(
fmt.Sprintf("Event %s with an invalid syscall id %d", evtName, id),
"Comm", commStr,
"UtsName", utsNameStr,
Expand Down