[PATCH] posix: Fix check_matching return value on allocation failure (bug 34949)

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Thu Nov 12 13:54:20 GMT 2026



On 10/11/26 11:24, Yuvraj Opawat wrote:
> From 040433e0aff08a32cda6c2611de0fbbe159287cb Mon Sep 17 00:00:00 2001
> From: Yuvraj Opawat <opawatyuvraj@gmail.com <mailto:opawatyuvraj@gmail.com>>
> Date: Tue, 10 Nov 2026 19:04:28 +0530
> Subject: [PATCH] posix: Fix check_matching return value on allocation failure
>  (bug 34949)
> 
> check_matching returns the match position as an Idx; its documented
> protocol is -1 for no match and -2 for an internal error.  However,
> if the pattern contains a backreference (dfa->nbackref > 0) and an
> allocation failure occurs in check_subexp_matching_top or
> transit_state_bkref in the initialization block of check_matching,
> the raw reg_errcode_t value (REG_ESPACE = 12) is returned instead of
> -2.
> 
> re_search_internal only guards against -2, so 12 passes the check and
> is treated as a real match position: mctx.state_log[12] is read from
> a slot that was never written (state_log is allocated with re_malloc
> and filled only up to the highest index actually visited), and the
> resulting NULL or uninitialized state pointer is dereferenced in
> check_halt_state_context, causing a segmentation fault under memory
> allocation failure.  This is reproducible, for example, with the
> pattern "\(a\)*\1" and an interposed malloc made to fail at the
> allocation of re_sub_match_top_t in match_ctx_add_subtop.
> 
> Replace the two "return err;" statements with "return -2;", matching
> every other error path in check_matching and restoring the documented
> return protocol.
> 
> Add bug-regex40 which forks a child for each of the first 64
> allocations performed by regexec on the backreference pattern
> "\(a\)*\1" with an empty subject string, makes exactly that
> allocation fail via an in-process malloc/calloc/realloc interposer
> (no LD_PRELOAD), and verifies that every child exits normally instead
> of crashing and that the injected failures are actually exercised
> (regexec surfaces allocation failures as REG_NOMATCH through its
> return value).
> 
> Checked on x86_64-linux-gnu.
> 
> Signed-off-by: Yuvraj Opawat <opawatyuvraj@gmail.com <mailto:opawatyuvraj@gmail.com>>

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  posix/Makefile      |   1 +
>  posix/bug-regex40.c | 147 ++++++++++++++++++++++++++++++++++++++++++++
>  posix/regexec.c     |   4 +-
>  3 files changed, 150 insertions(+), 2 deletions(-)
>  create mode 100644 posix/bug-regex40.c
> 
> diff --git a/posix/Makefile b/posix/Makefile
> index ae26443ac9..6e934ba067 100644
> --- a/posix/Makefile
> +++ b/posix/Makefile
> @@ -243,6 +243,7 @@ tests := \
>    bug-regex37 \
>    bug-regex38 \
>    bug-regex39 \
> +  bug-regex40 \
>    regexbug1 \
>    runptests \
>    runtests \
> diff --git a/posix/bug-regex40.c b/posix/bug-regex40.c
> new file mode 100644
> index 0000000000..9252b73050
> --- /dev/null
> +++ b/posix/bug-regex40.c
> @@ -0,0 +1,147 @@
> +/* Test that allocation failures during regexec are reported cleanly
> +   with a backreference pattern (BZ #34949).
> +
> +   With dfa->nbackref > 0, check_matching returned the raw
> +   reg_errcode_t value (REG_ESPACE = 12) instead of -2 if
> +   check_subexp_matching_top or transit_state_bkref failed in its
> +   initialization block, so re_search_internal treated 12 as a match
> +   position, read the never-written mctx.state_log[12] slot and
> +   dereferenced it in check_halt_state_context, crashing the process.
> +
> +   Fork a child for each of the first 64 allocations performed by
> +   regexec and make exactly that allocation fail; every child must
> +   exit normally instead of crashing, and the sweep must actually
> +   exercise the injected failures.  The subject string is empty so that
> +   the sub-expression is never closed; this confines the exercised
> +   error paths to the initialization block of check_matching, which is
> +   what the fix changes.
> +   Copyright The GNU Toolchain Authors.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/ <https://www.gnu.org/licenses/>>.  */
> +
> +#include <regex.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <sys/wait.h>
> +#include <unistd.h>
> +
> +#include <support/check.h>
> +
> +/* Interpose malloc/calloc/realloc so that the Nth allocation performed
> +   by regexec can be made to fail without needing LD_PRELOAD: calls to
> +   malloc from within libc.so resolve to these definitions in this
> +   program.  */
> +extern void *__libc_malloc (size_t);
> +extern void *__libc_calloc (size_t, size_t);
> +extern void *__libc_realloc (void *, size_t);
> +
> +/* The Nth allocation to fail, or 0 to disable failure injection.  */
> +static int fail_at;
> +static int alloc_count;
> +
> +void *
> +malloc (size_t size)
> +{
> +  if (fail_at > 0 && ++alloc_count == fail_at)
> +    return NULL;
> +  return __libc_malloc (size);
> +}
> +
> +void *
> +calloc (size_t nmemb, size_t size)
> +{
> +  if (fail_at > 0 && ++alloc_count == fail_at)
> +    return NULL;
> +  return __libc_calloc (nmemb, size);
> +}
> +
> +void *
> +realloc (void *ptr, size_t size)
> +{
> +  if (fail_at > 0 && ++alloc_count == fail_at)
> +    return NULL;
> +  return __libc_realloc (ptr, size);
> +}
> +
> +/* Upper bound of the sweep; it must exceed the total number of
> +   allocations performed by regexec for the test pattern.  */
> +#define FAIL_AT_MAX 64
> +
> +static int
> +child_run (int fail_nth, regex_t *preg, const char *string)
> +{
> +  fail_at = fail_nth;
> +  alloc_count = 0;
> +  regexec (preg, string, 0, NULL, 0);
> +  fail_at = 0;
> +
> +  /* regexec reports every error, including allocation failures, as
> +     REG_NOMATCH through its return value, so the observable contract
> +     here is simply that regexec returns cleanly instead of crashing
> +     the process.  Return 2 if the injected failure was actually
> +     exercised, i.e. regexec performed at least fail_nth allocations;
> +     this keeps the sweep from passing vacuously.  */
> +  return alloc_count >= fail_nth ? 2 : 0;
> +}
> +
> +static int
> +do_test (void)
> +{
> +  static const char pattern[] = "\\(a\\)*\\1";
> +  regex_t preg;
> +  bool saw_failure = false;
> +  int result = 0;
> +
> +  TEST_VERIFY_EXIT (regcomp (&preg, pattern, 0) == 0);
> +
> +  for (int n = 1; n <= FAIL_AT_MAX; n++)
> +    {
> +      pid_t pid = fork ();
> +      TEST_VERIFY_EXIT (pid >= 0);
> +      if (pid == 0)
> +       _exit (child_run (n, &preg, ""));
> +
> +      int status;
> +      TEST_VERIFY_EXIT (waitpid (pid, &status, 0) == pid);
> +      if (!WIFEXITED (status))
> +       {
> +         printf ("FAIL: child failing allocation #%d died from "
> +                 "signal %d\n", n, WTERMSIG (status));
> +         result = 1;
> +         continue;
> +       }
> +      if (WEXITSTATUS (status) == 2)
> +       saw_failure = true;
> +      else if (WEXITSTATUS (status) != 0)
> +       {
> +         printf ("FAIL: unexpected exit status %d from child failing "
> +                 "allocation #%d\n", WEXITSTATUS (status), n);
> +         result = 1;
> +       }
> +    }
> +
> +  if (!saw_failure)
> +    {
> +      puts ("FAIL: no injected allocation failure was exercised");
> +      result = 1;
> +    }
> +
> +  regfree (&preg);
> +  return result;
> +}
> +
> +#include <support/test-driver.c>
> diff --git a/posix/regexec.c b/posix/regexec.c
> index 9ac5565295..89a75b7443 100644
> --- a/posix/regexec.c
> +++ b/posix/regexec.c
> @@ -1078,13 +1078,13 @@ check_matching (re_match_context_t *mctx, bool fl_longest_match,
>           at_init_state = false;
>           err = check_subexp_matching_top (mctx, &cur_state->nodes, 0);
>           if (__glibc_unlikely (err != REG_NOERROR))
> -           return err;
> +           return -2;
> 
>           if (cur_state->has_backref)
>             {
>               err = transit_state_bkref (mctx, &cur_state->nodes);
>               if (__glibc_unlikely (err != REG_NOERROR))
> -               return err;
> +               return -2;
>             }
>         }
>      }
> --
> 2.43.0
> 
> 
> Mailsuite 	Sender notified with Mailtrack  ·  Opt out <https://u.list-prefs.com/en/privacy/opt-out/unsubscribe/ced639908b4905ec7588092694756fc637eccc5c/4247c84b79ba4cff0da8a94deefc285cb7136d8c324b9fa739f827edddb385e4077db0d9b0f0d2180b70b6c5157c080d9000e33a2d6894069430499f16077bb3>
> 	11/10/26, 07:53:42 PM
> 



More information about the Libc-alpha mailing list