Page MenuHomeFreeBSD

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

Authored by jilles on Sat, Nov 15, 4:54 PM.
Tags
None
Referenced Files
Unknown Object (File)
Fri, Nov 21, 6:06 AM
Unknown Object (File)
Wed, Nov 19, 5:01 PM
Unknown Object (File)
Wed, Nov 19, 3:58 AM
Unknown Object (File)
Mon, Nov 17, 10:27 AM
Unknown Object (File)
Sun, Nov 16, 2:29 AM
Unknown Object (File)
Sun, Nov 16, 2:29 AM
Unknown Object (File)
Sun, Nov 16, 2:27 AM
Unknown Object (File)
Sat, Nov 15, 9:44 PM
Subscribers

Details

Summary

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.

Test Plan

Install patched sh with new tests, cd /usr/tests/bin/sh && kyua test and reboot (all within a virtual machine).

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Passes all of the poudriere tests. Thank you for looking at quickly. This change looks much better than my attempt.

This revision is now accepted and ready to land.Sun, Nov 16, 12:32 AM