Skip to content

fix(sensor-linux-ebpf): a fork records the parent's tgid as ppid, and a new thread no entry (Closes #372) - #375

Open
SoFrexs wants to merge 1 commit into
mainfrom
fix/372-fork-ppid-tgid
Open

SoFrexs wants to merge 1 commit into
mainfrom
fix/372-fork-ppid-tgid

Conversation

@SoFrexs

@SoFrexs SoFrexs commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Closes #372.

sched_process_fork fires for every clone, threads included. raw_sched_process_fork read task_struct.pid on both tasks, which is a thread id. That caused two problems:

  • A fork by a secondary thread stored that thread's id as ppid. Any multi-threaded server or runtime that spawns a child hits this. The id names no process, while an entry primed from /proc/<pid>/stat holds the parent's tgid. The same parent therefore showed up under two ids, and a lineage walk or a parent-image lookup from such a child missed it.
  • Every new thread got its own PROC_LINEAGE entry under its thread id. No reader looks an entry up by thread id: the probes key on bpf_get_current_pid_tgid() >> 32, and priming on /proc pids. These entries only took slots in a 65 536-entry map. Worse, when another thread of the process later forked, its id led the exec probe to a thread entry, whose own ppid (the creating thread) was then reported as the grandparent.

Change

  • sensor-linux-wire: new lineage.rs. forked_process(parent: TaskIds, child: TaskIds) -> Option<ForkedProcess> decides what to store, from plain ids:

    • nothing for a new thread (pid != tgid);
    • otherwise the child's tgid as the key and the parent's tgid as ppid.

    The probe stays a sequence of reads, and the rule is tested on the host.

  • raw.rs. The fork probe reads pid and tgid of both tasks through a small task_ids helper (TASK_TGID_OFFSET was already resolved, for the signal probe). It then stores what forked_process returns. The exit and exec probes are unchanged: by the time sched_process_exec fires, de_thread has already given the exec'ing thread the tgid as its pid.

  • docs/sensors/linux/process-context.md. The lineage section already said "the parent's tgid", which was not true until now. It now also says that threads get no entry, and that the stored comm is the forking thread's. A thread renamed with prctl(PR_SET_NAME), such as tokio-runtime-w, may not share its process's comm.

Tests

wire/src/lineage.rs:

  • a_fork_by_the_main_thread_records_the_parent_process;
  • a_fork_by_a_secondary_thread_records_the_parent_process_not_the_thread: the regression (parent pid 105, tgid 100 gives ppid 100);
  • a_new_thread_records_nothing.

Run on Windows:

  • cargo test -p sensor-linux-wire: passes, with the 3 new tests.
  • clippy -D warnings is clean on sensor-linux-wire, both with and without --features user, and for --target x86_64-unknown-linux-gnu.
  • tools/clippy-ebpf.sh (pinned nightly, bpfel, x86_64 and aarch64) is clean.
  • cargo +nightly fmt --all --check, tools/check-docs.py and tools/check-deps.py are clean.

Not run here:

  • The verifier load on the CI kernels. linux-ebpf in CI covers it: the probe gains two bpf_probe_read_kernel calls and an early return.
  • A live check that forks from a non-leader thread (no Linux VM on this host). A quick one on fedora-lab or debian12: python3 -c 'import threading,subprocess; t=threading.Thread(target=lambda: subprocess.run(["sleep","1"])); t.start(); t.join()'. The exec event of sleep should carry the python pid as ppid. Before this change, it carried the thread's id.

Out of scope

  • When the thread-group leader exits alone (pthread_exit in main) while the process keeps running, raw_sched_process_exit removes the process's entry early. This is unchanged. A fix would remove the entry only on the last thread's exit (group_dead), which needs another field read.
  • The parent comm is taken from the forking thread, not from the group leader (that needs task_struct.group_leader).

🤖 Generated with Claude Code

… a new thread no entry

sched_process_fork fires for every clone. The probe read task_struct.pid on
both tasks, so a fork by a secondary thread stored that thread's id as ppid,
and every new thread got a PROC_LINEAGE entry under its thread id. The map is
keyed and read by tgid everywhere else (probes, /proc priming).

The probe now reads pid and tgid of both tasks, and sensor-linux-wire's
forked_process decides what to store: nothing for a thread (pid != tgid), the
child's tgid with the parent's tgid otherwise. Host unit tests pin it.

Closes #372

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: sofrexs <[email protected]>

@Jihair54 Jihair54 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked live: the fix does what it says. A fork from a secondary thread now records the process, not the thread. The run also showed a lineage gap that predates this PR. I'd track it in its own issue rather than hold this one; see the follow-up below.

Live check. Debian 12, kernel 6.1.0-17. I ran a small C program in four modes under agent run --standalone, once with main's binary (2ce1d45b) and once with this PR's head (7e19a2fe), and read each exec event of a marker /bin/echo:

Mode main this PR
main thread forks, child execs ppid = tgid, generations set same
secondary thread forks, child execs ppid = the thread's id (10950, tgid 10949) ppid = tgid (11004), generations set
secondary thread calls execve itself ppid=0, no generations, no parent_comm same
main thread pthread_exits, worker forks and execs ppid = the worker's tid, parent_process_generation set (from the worker's own thread entry) ppid = tgid, parent_process_generation absent

Row 2 is #372, fixed. The probe also loaded and ran on this kernel, so TASK_TGID_OFFSET resolves correctly on 6.1. A wrong offset would have dropped every fork entry, and rows 1 and 2 still carry generations.

Follow-up (not caused by this PR, but the same root): raw_sched_process_exit removes the process entry when the leader thread exits, not when the process does. It removes PROC_LINEAGE[task.pid], and for the leader that pid is the tgid.

  • Row 3. In de_thread, the old leader exits before the exec'ing thread takes its id. That exit removes the entry, so record_sched_process_exec misses on the tgid and reports ppid=0. An exec from any non-leader thread therefore arrives with no lineage, in one syscall. Rules read ppid=0 as "unknown" (crates/rules/src/impact_state.rs:257), so for a parent-child rule this is an evasion.
  • Row 4. This is the out-of-scope case your description names. One visible change: on main, the child kept a parent_process_generation only because the worker had its own thread entry. With thread entries gone, that stamp is now absent. The ppid is right now, so I count the trade as positive, but it is a change.

Both go away if the exit probe removes the entry only when the whole thread group is dead (task->signal->live == 0, or the tracepoint's group_dead where the kernel has it). I'd open that as an issue. If you agree, the "raw_sched_process_exit removes it" line in process-context.md could then point to it.

Nit. The exit probe still runs a remove for every non-leader thread exit. It always misses, so it is harmless. It would read more clearly mirrored on the fork side: read task_ids and return early when pid != tgid. That fits best with the follow-up above.

Tested

  • Windows host, at 7e19a2fe: cargo test -p sensor-linux-wire passed (the 3 new lineage tests included); cargo clippy -p sensor-linux-wire --all-targets -- -D warnings and cargo +nightly fmt -p sensor-linux-wire -- --check are clean.
  • debian12 (6.1.0-17): built agent with the eBPF object at the PR head, then ran the four-mode live check above against main's binary and the PR's.

🤖 Generated with Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sensor-linux-ebpf: the fork probe records the forking thread's id as ppid, not the parent's tgid

2 participants