-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"The patch for CVE-2026-8376 is six lines of arithmetic-overflow guard added to `Perl_study_chunk` in `regcomp_study.c`. Five of those lines are a renamed copy of code already in the same function, two hundred lines earlier, guarding a different `mincount` multiplication against the same `SSize_t_MAX` ceiling and ending in the same `FAIL(\"Regexp out of space\")` string literal. John Lightsey committed the first check on 20 November 2019, in a commit titled \"Prevent integer overflow from nested regex quantifiers.\" Tony Cook committed the second check on 12 May 2026, after Timothy Legge reported that `Perl_study_chunk` was heap-overflowing on every 32-bit build of Perl 5.43.10 and earlier. Six years and six months separate the two checks in the same function.\n\n## The check was already in the file\n\n`Perl_study_chunk` is Perl's regex optimizer. The function walks a compiled regex tree and computes properties the matcher will use to skip ahead: minimum match length, fixed-substring anchors, character-class invariants. When it sees a quantifier with a known minimum repetition count, it has to do at least two pieces of arithmetic. The first is a length accumulator. The second is a buffer sizer.\n\nThe length accumulator is at line 2587, and since November 2019 it has been guarded:\n\n```c\nif ( ( minnext > 0 && mincount >= SSize_t_MAX / minnext )\n || min >= SSize_t_MAX - minnext * mincount )\n{\n FAIL(\"Regexp out of space\");\n}\n\nmin += minnext * mincount;\n```\n\nThis is the canonical signed-multiply overflow guard, written exactly the way a careful C programmer writes it. The first clause guards against `mincount * minnext` overflowing `SSize_t`. The second clause guards against the cumulative sum overflowing once added. If either check fires, the regex is rejected at compile time with the user-facing message `Regexp out of space`. The check exists. The check works. The check is in `Perl_study_chunk` because the person who put it there understood that `mincount` is whatever the user wrote inside `{...}` in their regex, that `SSize_t` is 32 bits wide on a 32-bit build, and that the obvious arithmetic between the two will overflow if the user picks bad numbers.\n\nTwo hundred lines below Lightsey's guard, the same function does the second piece of arithmetic. Until 12 May 2026, the second site was unguarded.\n\n## The second arithmetic site is older than the guard\n\nWhen `study_chunk` finishes accumulating a fixed substring and discovers it sits inside a quantifier with `mincount > 1`, it materializes the repeated string into `data->last_found` so the matcher can use it as a forward anchor. The pre-patch code reads:\n\n```c\nif (deltanext == 0 && pos_before == b) {\n /* What was added is a constant string */\n if (mincount > 1) {\n\n SvGROW(last_str, (mincount * l) + 1);\n repeatcpy(SvPVX(last_str) + l,\n SvPVX_const(last_str), l,\n mincount - 1);\n SvCUR_set(last_str, SvCUR(last_str) * mincount);\n```\n\n`l` is the byte length of the substring. `mincount` is the user's quantifier. `mincount * l` is the size of the buffer the function is about to allocate via `SvGROW`. `repeatcpy` then writes `l * (mincount - 1)` more bytes into that buffer starting at offset `l`.\n\nIf `mincount * l` overflows `SSize_t`, `SvGROW` is called with the truncated low bits of the product. `repeatcpy` is called with the full untruncated counts. The write lands wherever the allocator put the now-undersized buffer.\n\n`git blame` puts the `SvGROW(last_str, (mincount * l) + 1)` line in Chip Salzenberg's \"Jumbo regexp patch applied\" commit from 15 November 1997. It is twenty-eight years and six months old. Lightsey's overflow guard, two hundred lines up in the same function, is six years and six months old. The bug pre-dates the protective pattern by more than two decades, and the protective pattern has been visible in the same function for over half a decade without being applied to the second site.\n\nThe fix is commit `5e7f119`:\n\n```c\nif (l > 0 &&\n (mincount >= SSize_t_MAX / (SSize_t)l\n || old > SSize_t_MAX - mincount * (SSize_t)l)) {\n FAIL(\"Regexp out of space\");\n}\n```\n\nLay it against Lightsey's check. Same function, same `SSize_t_MAX` ceiling, same two-clause structure (guard the multiplication, guard the accumulator), same `FAIL(\"Regexp out of space\")` literal. Lightsey's variables are `minnext` and `min`. Cook's are `l` and `old`. Cook's site needs an explicit `(SSize_t)` cast because `l` is `STRLEN` rather than signed. The algebra is identical. Cook's check is Lightsey's check, applied to the second arithmetic site in the same function, six and a half years after the first.\n\n## The PoC is one regex\n\nThe seclists.org disclosure included no PoC. The test commit on the security branch (`e842efd`, 12 May 2026) is the PoC, and it is also the canonical exercise of the second arithmetic site:\n\n```perl\n/\\x{10000}{1073741824}/\n```\n\n`\\x{10000}` is the smallest Unicode codepoint that requires four bytes in UTF-8. `{1073741824}` is `2**30`. The regex is one four-byte character, repeated `2**30` times.\n\n`Perl_study_chunk` reaches the second arithmetic site with `l = 4` and `mincount = 1073741824`. The intended buffer size is `4 * 1073741824 = 2**32` bytes. `SSize_t_MAX` on a 32-bit build is `2**31 - 1`. `mincount * l` overflows to `0`. `SvGROW(last_str, 0 + 1)` allocates a one-byte payload. `repeatcpy` then writes roughly `4 * (1073741824 - 1)` bytes past the end of it.\n\nThe same commit ships a second test in case the first is dismissed as a corner of a corner:\n\n```perl\n/(?:\\x{10001}\\x{10000}){536870912}/\n```\n\n`536870912` is `2**29`. The grouped substring is two four-byte UTF-8 characters. `l = 8`, `mincount = 2**29`. The product is again `2**32`. The arithmetic that overflowed once overflows the same way with different operands.\n\nA 32-bit Perl process that compiles either of these patterns from attacker-controlled input dies in a heap write. The CVE is rated 9.8 because the compile step runs inside whatever interpreter the operator handed an untrusted regex to: a web app accepting a search pattern, a log parser loading grok rules from disk, a CI pipeline taking a regex flag, a CGI script that exposes a `--filter` query parameter. The compile is the attack surface.\n\n## What the test commit admitted\n\nThe test commit `e842efd` shipped the two PoC patterns wrapped in:\n\n```perl\nSKIP:\n{ # sec #147\n $Config{ptrsize} == 4\n or skip \"these only fail on x32 and use too much memory on x64\", 2;\n local $::TODO = \"This crashes\";\n # original case\n fresh_perl_like('/\\x{10000}{1073741824}/',\n qr/Regexp out of space/, {}, \"ssize_t overflow\");\n```\n\n`$::TODO = \"This crashes\"` is Test::More's marker for a test the maintainer expects to fail. The line is committed admission that on the day the test landed, Tony Cook knew the test would crash the Perl interpreter, knew the crash was a heap write past the end of a `SvGROW` allocation, and shipped the test alongside the fix so the fix's correctness would be the test's pass.\n\nThe fix commit, four minutes later in the same author's session, removes the TODO marker:\n\n```diff\n- local $::TODO = \"This crashes\";\n```\n\nThat is the smallest, most honest summary of what changed: the maintainer agreed that the test now passes, because the second arithmetic site is now guarded the way the first one was.\n\n## 32-bit is the precondition, and 32-bit is still shipped\n\nTwo facts about the precondition.\n\nFirst: `SSize_t` is the signed companion to `size_t`. On a build where `sizeof(void*) == 4`, `SSize_t_MAX == 2**31 - 1`. On a build where `sizeof(void*) == 8`, `SSize_t_MAX == 2**63 - 1`. The PoC chooses numbers that overflow the first and don't come close to the second. A 64-bit perl will ask the allocator for four gigabytes, fail the allocation if memory pressure refuses, and survive. A 32-bit perl miscomputes the size and writes through the resulting tiny buffer.\n\nSecond: 32-bit Perl is still shipped. Strawberry Perl distributes a 32-bit Windows binary alongside the 64-bit one. ActiveState distributes a 32-bit installer. Most embedded Linux ships a 32-bit userspace. Every router, smart speaker, NAS appliance, and industrial controller running an MIPS or ARMv7 busybox-style distribution is running a 32-bit Perl if it runs Perl at all. The CVE applies to those builds. The CVE does not apply to the laptop in front of most of the people reading the disclosure.\n\nThe vendor advisory's mitigation, \"avoid compiling untrusted regular expressions on 32-bit Perl builds until patched,\" is correct and underspecifies the population. The population is every device whose firmware bundled a 32-bit Perl years ago and has not been rebuilt since.\n\n## The function still doesn't have the invariant\n\nThe patch closes the second arithmetic site. It does not close the bug class in the function.\n\nTwenty-two lines after the new guard, the same code path computes:\n\n```c\ndata->last_end += l * (mincount - 1);\n```\n\nAnother `mincount * l`. The patch leaves it untouched, on the assumption that the new `FAIL` fires before execution reaches it. The assumption is correct in the current control flow. It is also structural rather than enforced. `regcomp_study.c` does not have an invariant that says \"every multiplication of attacker-controlled `mincount` against an attacker-controlled byte length must be checked against `SSize_t_MAX` first.\" It has two guarded sites now, where it had one before, and at least one further site that is guarded only by virtue of running after the second guard.\n\nThis is design-debt-driver in the small. Perl's regex compiler accepts user-supplied repetition counts and multiplies them against per-element lengths to size buffers. Every such multiplication is an overflow surface. Closing the surface site by site as researchers find them is the only mode the code is in. Eighteen days from report to fix for this one. The next one resurfaces when the next researcher fuzzes the next arithmetic.\n\nThe surrounding commits to `blead` from the same author in the same month read:\n\n```\ne4f6817 Storable.xs: Avoid signed int overflow when unpacking a list of hook data items\n40754ed pp_pack.c: Avoid some other potential overflows when calculating sizes\n5f7eb6b pp_pack.c: Avoid ssize_t overflow when calculating the size of a structure\n5e7f119 perl/perl-security#147: test against the actual character lengths\n```\n\nFour arithmetic-overflow fixes in May 2026, across three subsystems, all signed-integer multiplication of user-supplied counts against per-element sizes. Different files, different functions, same class. The class is wherever a parser accepts a length-times-size arithmetic at the surface and the runtime has to materialize the product into a buffer. Perl exposes that at `{n}` quantifiers, `[N]` pack counts, and frame counts in Storable hooks. Three subsystems for the same arithmetic, fixed once per arithmetic site.\n\nPoC: [perl/perl5 t/re/pat_psycho.t](https://github.com/Perl/perl5/blob/blead/t/re/pat_psycho.t)","closing_line":"The arithmetic was guarded. The function was not.","hook_md":"The patch for CVE-2026-8376 is six lines of arithmetic-overflow guard added to `Perl_study_chunk` in `regcomp_study.c`. Five of those lines are a renamed copy of code already in the same function, two hundred lines earlier, guarding a different `mincount` multiplication against the same `SSize_t_MAX` ceiling and ending in the same `FAIL(\"Regexp out of space\")` string literal. John Lightsey committed the first check on 20 November 2019, in a commit titled \"Prevent integer overflow from nested regex quantifiers.\" Tony Cook committed the second check on 12 May 2026, after Timothy Legge reported that `Perl_study_chunk` was heap-overflowing on every 32-bit build of Perl 5.43.10 and earlier. Six years and six months separate the two checks in the same function.\nThe arithmetic was guarded. The function was not.","post_id":501,"slug":"perl-cve-2026-8376-the-check-was-already-in-the-file","title":"CVE-2026-8376: The Overflow Check Was Already In Perl_study_chunk","type":"initial","unreadable_sentence":"The arithmetic was guarded. The function was not."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCan90nAAKCRDeZjl4jgkQ JjvIAP9EdeHoOpshgnoJwo5f93H0Mc9KN7CfGACheqQdWikItgD8C5xH99P+BWXY 41qdHRk4PA2Cbsleijl5mlKvsTTh3QU= =vDYx -----END PGP SIGNATURE-----