NetBSD Problem Report #61021
From www@netbsd.org Mon Nov 16 23:25:37 2026
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256
client-signature RSA-PSS (2048 bits) client-digest SHA256)
(Client CN "mail.netbsd.org", Issuer "YR1" (not verified))
by mollari.NetBSD.org (Postfix) with ESMTPS id A3E781A923B
for <gnats-bugs@gnats.NetBSD.org>; Mon, 16 Nov 2026 23:25:37 +0000 (UTC)
Message-Id: <20261116232536.674FB1A923C@mollari.NetBSD.org>
Date: Mon, 16 Nov 2026 23:25:36 +0000 (UTC)
From: nicklpmr@gmail.com
Reply-To: nicklpmr@gmail.com
To: gnats-bugs@NetBSD.org
Subject: libc/regex: out-of-bounds read in backref() ONWBND at slice boundaries
X-Send-Pr-Version: www-1.0
X-From4GNATS: "nicklpmr@gmail.com via gnats" <gnats-admin@NetBSD.org>
>Number: 61021
>Category: lib
>Synopsis: libc/regex: out-of-bounds read in backref() ONWBND at slice boundaries
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: lib-bug-people
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Mon Nov 16 23:30:00 +0000 2026
>Originator: Nick Leppelmeier
>Release: NetBSD-current (src/lib/libc/regex/engine.c rev 1.30)
>Organization:
>Environment:
NetBSD-current src/lib/libc/regex (engine.c 1.30, regcomp.c 1.50, regexec.c 1.26); reproduced standalone on Linux 6.6.114 x86_64 (GCC 15.2) and Android Bionic (aarch64/x86_64)
>Description:
In lib/libc/regex/engine.c, the ONWBND (\B) handling inside backref() reads
out of bounds when matching at the start or end of a subject slice:
if (((sp == m->beginp) && !ISWORD(*sp)) ||
(sp == m->endp && !ISWORD(*(sp - 1))))
{ /* yes, beginning/end of subject */ }
else if (ISWORD(*(sp - 1)) == ISWORD(*sp))
{ /* yes, beginning/end of subject */ }
1. When the subject is empty (sp == m->beginp && sp == m->endp), ISWORD(*sp)
reads 1 byte past m->endp.
2. When sp == m->beginp and *sp is a word character (for example, matching
"()\1(\B.|a)" against "a"), the first condition evaluates to false and
execution falls through to ISWORD(*(sp - 1)), reading 1 byte before
m->beginp.
3. Similarly, when sp == m->endp and *(sp - 1) is a word character (for
example, matching "()\1(a\B|a)" against "a"), execution falls through to
ISWORD(*sp), reading 1 byte past m->endp.
If the subject buffer sits directly adjacent to an unmapped or PROT_NONE
guard page (such as with REG_STARTEND), regexec() crashes with SIGSEGV.
>How-To-Repeat:
/* Compile and run on NetBSD: cc -Wall -O2 repro.c -o repro && ./repro */
#include <sys/mman.h>
#include <regex.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
main(void)
{
long pgsz = sysconf(_SC_PAGESIZE);
char *buf = mmap(NULL, (size_t)pgsz * 3, PROT_NONE,
MAP_PRIVATE | MAP_ANON, -1, 0);
if (buf == MAP_FAILED)
return 1;
char *page = buf + pgsz;
if (mprotect(page, (size_t)pgsz, PROT_READ | PROT_WRITE) != 0)
return 1;
/* Place "a" right at the start of the mapped page after PROT_NONE */
page[0] = 'a';
page[1] = '\0';
regex_t re;
regmatch_t pm = { .rm_so = 0, .rm_eo = 1 };
if (regcomp(&re, "()\\1(\\B.|a)", REG_EXTENDED | REG_GNU) != 0)
return 1;
/* Triggers read of *(m->beginp - 1) -> SIGSEGV */
int rc = regexec(&re, page, 1, &pm, REG_STARTEND);
printf("rc=%d\n", rc);
regfree(&re);
return 0;
}
>Fix:
Index: lib/libc/regex/engine.c
===================================================================
--- lib/libc/regex/engine.c
+++ lib/libc/regex/engine.c
@@ -732,11 +732,20 @@ backref(
return(NULL);
break;
case ONWBND:
- if (((sp == m->beginp) && !ISWORD(*sp)) ||
- (sp == m->endp && !ISWORD(*(sp - 1))))
- { /* yes, beginning/end of subject */ }
- else if (ISWORD(*(sp - 1)) == ISWORD(*sp))
- { /* yes, beginning/end of subject */ }
+ if (sp == m->beginp && sp == m->endp)
+ { /* yes, empty subject has no word chars */ }
+ else if (sp == m->beginp) {
+ if (!ISWORD(*sp))
+ { /* yes, beginning of subject */ }
+ else
+ return(NULL);
+ } else if (sp == m->endp) {
+ if (!ISWORD(*(sp - 1)))
+ { /* yes, end of subject */ }
+ else
+ return(NULL);
+ } else if (ISWORD(*(sp - 1)) == ISWORD(*sp))
+ { /* yes, neither or both are word chars */ }
else
return(NULL);
break;
(Contact us)
$NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.