|
|
Message-ID: <f59b825f-fe36-47a1-a4a9-cb9fdec0900c@pmgroup.ch>
Date: Sat, 28 Nov 2026 21:07:28 -0500
From: Georg Meyer <dev@...roup.ch>
To: musl@...ts.openwall.com
Subject: regcomp: NULL dereference in marksub when tre_ast_new_union fails,
under a failed allocation
In src/regex/regcomp.c, around the end of group/alternation parsing:
nunion = tre_ast_new_union(ctx->mem, nunion, nbranch);
nbranch = 0;
...
err = marksub(ctx, nunion, tre_stack_pop_int(stack));
tre_ast_new_union's result is never checked before it is passed into
marksub(). marksub() does check its own *internal* allocations (the
tre_ast_new_literal/tre_ast_new_catenation it calls when
node->submatch_id >= 0), but it unconditionally dereferences its `node`
argument at the top ("if (node->submatch_id >= 0)"), so if the caller
handed it a NULL node, it crashes there before any of its own checks run.
tre_ast_new_union allocates via ctx->mem (musl's own slab allocator, not
plain malloc, but it is backed by malloc/calloc/realloc), and returns
NULL on allocation failure like the rest of the parser does. With malloc
wrapped to fail on a chosen call, this path is reachable.
Reproduced with a harness that fails the Nth malloc/calloc/realloc call,
one call per fresh process (so a crash can't be an artifact of a
long-lived test process's heap state. I had exactly that problem with a
different engine and want to flag that this result was checked that way
on purpose). Minimized with a delta-debugger:
pattern (ERE): ]?4}(a*é(b?.+1,56})+
string: (empty)
fail at: the 9th malloc/calloc/realloc call during regcomp
Original, unminimized trigger (also ERE), found by random generation and
not hand-written:
pattern:
([a-c]?\.(.{3,4}(a*é(b?)(.+[^a]?b+1)+)([a-c]b)+x?)(((\6b){2,11} *)){1,56})+
string: three lines, roughly "c1xa\nbacba\ncbaa"
fail at: the 11th allocation call
I did not minimize the pattern down to something that looks like a
hand-written regex; `]?4}(a*é(b?.+1,56})+` is what a delta-debugger
produced and it is syntactically odd (glibc's regcomp rejects it, return
code 8 / REG_EBRACE, rather than crashing on it). A pattern that another
libc's parser rejects outright should probably not be able to crash this
one's parser.
I checked musl's history before writing this: commit 4260dfe1 ("regcomp:
propagate allocation failures", 2015) added allocation-failure checks
through much of this parser, but it does not touch this nunion/marksub
call, and that commit's checkout-tip (musl git 7813e9119eb5fe681b6750b8a033a
0490bca0941, which I confirmed is the current origin/master as of
2026-11-28) still has the gap. I found no later fix and no existing
report of this specific crash in a web search.
Suggested fix (attached): check nunion for NULL right after the
tre_ast_new_union call above and return REG_ESPACE, the same way every
other allocation in this function already is handled. I rebuilt musl
with this applied and ran a 20000-seed deterministic sweep against it:
the previously-crashing seeds now return REG_ESPACE cleanly instead of
segfaulting.
View attachment "musl-marksub-null.patch" of type "text/x-patch" (851 bytes)
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.