NetBSD-Bugs archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
lib/61021: libc/regex: out-of-bounds read in backref() ONWBND at slice boundaries
>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;
Home |
Main Index |
Thread Index |
Old Index