Repository navigation
Conversation
… 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
left a comment
There was a problem hiding this comment.
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, sorecord_sched_process_execmisses on the tgid and reportsppid=0. An exec from any non-leader thread therefore arrives with no lineage, in one syscall. Rules readppid=0as "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_generationonly because the worker had its own thread entry. With thread entries gone, that stamp is now absent. Theppidis 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-wirepassed (the 3 newlineagetests included);cargo clippy -p sensor-linux-wire --all-targets -- -D warningsandcargo +nightly fmt -p sensor-linux-wire -- --checkare clean. - debian12 (6.1.0-17): built
agentwith 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
Closes #372.
sched_process_forkfires for everyclone, threads included.raw_sched_process_forkreadtask_struct.pidon both tasks, which is a thread id. That caused two problems: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>/statholds 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.PROC_LINEAGEentry under its thread id. No reader looks an entry up by thread id: the probes key onbpf_get_current_pid_tgid() >> 32, and priming on/procpids. 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 ownppid(the creating thread) was then reported as the grandparent.Change
sensor-linux-wire: newlineage.rs.forked_process(parent: TaskIds, child: TaskIds) -> Option<ForkedProcess>decides what to store, from plain ids:pid != tgid);ppid.The probe stays a sequence of reads, and the rule is tested on the host.
raw.rs. The fork probe readspidandtgidof both tasks through a smalltask_idshelper (TASK_TGID_OFFSETwas already resolved, for the signal probe). It then stores whatforked_processreturns. The exit and exec probes are unchanged: by the timesched_process_execfires,de_threadhas already given the exec'ing thread the tgid as itspid.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 withprctl(PR_SET_NAME), such astokio-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 (parentpid105,tgid100 givesppid100);a_new_thread_records_nothing.Run on Windows:
cargo test -p sensor-linux-wire: passes, with the 3 new tests.-D warningsis clean onsensor-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.pyandtools/check-deps.pyare clean.Not run here:
linux-ebpfin CI covers it: the probe gains twobpf_probe_read_kernelcalls and an early return.python3 -c 'import threading,subprocess; t=threading.Thread(target=lambda: subprocess.run(["sleep","1"])); t.start(); t.join()'. The exec event ofsleepshould carry the python pid asppid. Before this change, it carried the thread's id.Out of scope
pthread_exitinmain) while the process keeps running,raw_sched_process_exitremoves 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.task_struct.group_leader).🤖 Generated with Claude Code