https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=290330

--- Comment #4 from [email protected] ---
A commit in branch main references this bug:

URL:
https://cgit.FreeBSD.org/src/commit/?id=75a6c38e4d5c651b7398bf2bea5baa41a0939e92

commit 75a6c38e4d5c651b7398bf2bea5baa41a0939e92
Author:     Jilles Tjoelker <[email protected]>
AuthorDate: 2025-11-15 16:43:03 +0000
Commit:     Jilles Tjoelker <[email protected]>
CommitDate: 2025-11-17 18:32:38 +0000

    sh: Fix a double free in a rare scenario with pipes

    The command
      sh -c 'sleep 3 | sleep 2 & sleep 3 & kill %1; wait %1'
    crashes (with appropriate sanitization such as putting
    MALLOC_CONF=abort:true,junk:true in the environment or compiling with
    -fsanitize=address).

    What happens here is that waitcmdloop() calls dowait() with a NULL job
    pointer, instructing dowait() to freejob() if it's a non-interactive
    shell and $! was not and cannot be referenced for it. However,
    waitcmdloop() then uses fields possibly freed by freejob() and calls
    freejob() again.

    This only occurs if the job being waited for is identified via % syntax
    ($! has never been referenced for it), it is a pipeline with two or more
    elements and another background job has been started before the wait
    command. That seems special enough for a bug to remain. Test scripts
    written by Jilles would almost always use $! and not % syntax.

    We can instead make waitcmdloop() pass its job pointer to dowait(),
    fixing up things for that (waitcmdloop() will have to call deljob() if
    it does not call freejob()).

    The crash from
    https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=290330#c2 appears to
    be the same bug.

    PR:             290330
    Reported by:    bdrewery
    Reviewed by:    bdrewery
    Differential Revision:  https://reviews.freebsd.org/D53773

 bin/sh/jobs.c                        | 3 ++-
 bin/sh/tests/builtins/Makefile       | 1 +
 bin/sh/tests/builtins/wait11.0 (new) | 6 ++++++
 3 files changed, 9 insertions(+), 1 deletion(-)

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to