//nefariousplan

CVE-2026-8796: Sereal::Decoder's COPY Bounds Check Was a Comment From 2012

vendor

patterns

cve

proof of concept

Sereal::Decoder 5.005 is a security release. The Changes entry says "prevent buffer overrun reads from COPY tags." The diff on Perl/Decoder/srl_decoder.c adds forty lines and deletes six. Two of the deleted lines are a comment that has been in the file since September 8, 2012, when Yves Orton, then at Booking.com, wrote it into the COPY-handling branch of srl_read_hash and again into the COPY-handling branch of srl_read_object:

/* note we do NOT validate these items, as we have alread read them
 * and if they were a problem we would not be here to process them! */

CVE-2026-8796 is the bug those two lines were protecting. Paul Johnson reported it on May 13, 2026. The bytes the comment said had already been validated had never been validated. They had never been bytes the decoder had read as anything at all. The typo "alread" is preserved verbatim in both copies and shipped through every Sereal::Decoder release on CPAN from October 2012 to April 2026: thirteen years and seven months of releases in which the bounds check on the COPY tag was a sentence in English explaining why the bounds check was not necessary.

The comment was the bounds check.

Sereal::Decoder 5.005 is a security release. The Changes entry says "prevent buffer overrun reads from COPY tags." The diff on Perl/Decoder/srl_decoder.c adds forty lines and deletes six. Two of the deleted lines are a comment that has been in the file since September 8, 2012, when Yves Orton, then at Booking.com, wrote it into the COPY-handling branch of srl_read_hash and again into the COPY-handling branch of srl_read_object:

/* note we do NOT validate these items, as we have alread read them
 * and if they were a problem we would not be here to process them! */

CVE-2026-8796 is the bug those two lines were protecting. Paul Johnson reported it on May 13, 2026. The bytes the comment said had already been validated had never been validated. They had never been bytes the decoder had read as anything at all. The typo "alread" is preserved verbatim in both copies and shipped through every Sereal::Decoder release on CPAN from October 2012 to April 2026: thirteen years and seven months of releases in which the bounds check on the COPY tag was a sentence in English explaining why the bounds check was not necessary.

The COPY tag's contract was a back-reference the decoder never indexed

Sereal is the binary serialization format Booking.com open-sourced in 2012 as a faster alternative to Storable. The Perl decoder reads a header, then a body, byte by byte. The first byte of each value is a tag. The tag's high bits select an operation (BINARY, ARRAY, HASH, OBJECT, COPY, REFN, varint, and so on) and its low bits, where the operation permits it, carry inline data. SHORT_BINARY is the tag whose byte range is 0x60 through 0x7F: the high three bits are 011 and the low five bits encode an inline string length from 0 to 31 bytes. A SHORT_BINARY tag at byte position N declares "the next tag & 0x1F bytes are a string."

The COPY tag exists so a producer that emits the same string twice does not have to write it twice. A repeated hash key, a repeated class name, a repeated inline binary, the producer emits the string in full the first time, records the byte offset of the string's tag, and on the second occurrence emits a COPY tag whose varint payload is that recorded offset. The decoder, on seeing COPY, computes from = body_pos + offset, reads the byte at from, and processes that byte as a tag.

The format specification describes COPY's offset as pointing to a "previously emitted tag." That phrasing is the Convention Is The Allowlist shape: the set of valid COPY targets is "tag positions," a constrained subset of "any in-buffer offset," and a conforming decoder would have to maintain that set and reject offsets outside it. Sereal::Decoder does not maintain that set in versions 1 through 5.004. It does not maintain it because the comment at the COPY handler explains why it does not need to: "we have alread read them," "if they were a problem we would not be here to process them." Read as a security argument, the comment says: any byte we are about to interpret as a tag is a byte we previously interpreted as a tag, and the previous interpretation would have caught it if it were malformed.

That argument is true if the COPY offset is a tag offset. The COPY offset is a varint. The decoder validates it by checking, prior to 5.005, that the offset is less than the buffer length. The set of bytes the decoder accepts as a SHORT_BINARY tag is whatever bytes in the buffer happen to fall in the range 0x60 through 0x7F, regardless of whether they were ever a tag. Every byte the producer wrote into the body, including bytes that were emitted as the content of a previous string, is a candidate.

What the regression test plants

The 5.005 release ships Perl/Decoder/t/903_regr_oob_copy_shortbinary.t. It is the minimum byte sequence that demonstrates the bug. Stripped of the six-byte Sereal header and the outer ARRAY wrapper, the body for the OBJECT variant is:

0x26 0x1E                            ; BINARY tag, declared length = 30
0x42 0x42 0x42 ... 0x42 0x7F         ; 29 'B' bytes, then 0x7F
0x2C 0x2F 0x22                       ; OBJECT, COPY, varint 34
0x28 0x25                            ; REFN, UNDEF

The BINARY tag at the head of the body declares a thirty-byte string. The decoder reads its length varint, advances past it, and copies thirty bytes into a Perl scalar. The last of those thirty bytes is 0x7F. The producer chose 0x7F because 0x7F is the SHORT_BINARY tag with the maximum inline length of 31, and the producer is going to ask the decoder to re-read this byte as if it were a tag. At the time the BINARY's content is read, the decoder consumes 0x7F as just another content byte and moves on.

The next tag is 0x2C, OBJECT. The OBJECT handler reads the next tag to get the class name. The next tag is 0x2F, COPY. The COPY handler reads a varint payload, which is 0x22, the value 34. srl_read_object enters its COPY branch. It computes from = body_pos + 34, which points at the planted 0x7F byte that the producer arranged to be exactly the last byte of the BINARY's content. It reads that byte. The byte matches IS_SRL_HDR_SHORT_BINARY. The handler extracts the low five bits, gets 31, and reads 31 bytes starting at from + 1.

The 31-byte read runs through the OBJECT tag, the COPY tag, the varint payload, the REFN and UNDEF tags, and twenty-six bytes past the end of the input buffer. Those twenty-six bytes are whatever happens to be in the perl heap adjacent to the input scalar. The handler returns them as the OBJECT's class name. The OBJECT path takes a class name and uses it to construct a blessed reference whose package is the named class, which means the heap bytes are interned into the symbol table as a package name and a Perl program can read them back as a string.

The HASH variant in the same test does the same operation through srl_read_hash for a hash key. The hash key path leaks the heap bytes as a hash key, where the same keys %$hash call by the caller materializes them as a Perl string.

What the fix asserts is what the comment said had happened

The 5.005 patch removes the comment and adds four lines at the end of the COPY branch in both srl_read_hash and srl_read_object. In srl_read_hash:

} else if (tag == SRL_HDR_COPY) {
    const U8 *copytag_start = dec->buf.pos - 1;
    UV ofs = srl_read_varint_uv_offset(aTHX_ dec->pbuf, " while reading COPY tag");
    from = dec->buf.body_pos + ofs;
    tag = *from++;
    if (IS_SRL_HDR_SHORT_BINARY(tag)) {
        key_len = (KEYLENTYPE)SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag);
    }
    ...
    if (expect_false(from + key_len > copytag_start)) {
        SRL_RDR_ERRORf2(dec->pbuf,
            COPY_OFFSET_ERROR_FMT_PREFIX " while reading key",
            (int)ofs, (int)key_len);
    }
}

copytag_start is captured at function entry as dec->buf.pos - 1, which is the byte position of the COPY tag itself. The new check asserts that the read of key_len bytes from from ends before the COPY tag. It is the structural property the COPY mechanism implies: a back-reference reads bytes that were emitted before the back-reference. Any read whose endpoint passes copytag_start is reading bytes that were not in the buffer when the COPY was supposed to be looking at them, or, on the heap-overflow path, bytes that were never in the buffer at all.

The general-purpose srl_read_copy (called when COPY appears in value contexts outside a hash key or class name) gets the same treatment. The old check was:

if (expect_false((IV)item > dec->buf.end - dec->buf.start)) {
    SRL_RDR_ERRORf1(dec->pbuf, "COPY(%d) points out of packet", (int)item);
}

The new check is:

if (expect_false(target_start > copytag_start)) {
    SRL_RDR_ERRORf1(dec->pbuf, "COPY target %d must preceed its own offset",
        (int)ofs);
}

The old check tested that the offset fit in the buffer. The new check tests that the target precedes the COPY tag. The old check is what the format spec's wording could be read to permit. The new check is what the format spec's wording was always describing.

The outer frame, the inner length

The mechanism is the Frame Bounds Headers Only shape. The outer frame is the input buffer Sereal received from the caller. The inner length is the SHORT_BINARY tag's low-five-bit length field, which drives a read against the same buffer. The outer frame is checked: prior to 5.005, the COPY offset had to fall inside it. The inner length is not checked against the same frame, which is how the read runs past the buffer's end.

The variant nuance is that the inner length field is not a field the decoder parsed as a length under the format's grammar. The decoder reached the inner length field by re-decoding a byte at a caller-controlled offset as if it were a tag. The byte was not a tag when the decoder consumed it the first time. It was the last byte of a BINARY's content, a value the decoder copied into a Perl scalar and moved on from. The OP-TEE PKCS#11 exhibit at CVE-2026-33317 shows an inner length writing past the outer frame. The Netty QPACK exhibit at CVE-2026-42582 shows an inner length sizing an allocation the outer frame could never have delivered. The Sereal exhibit adds the case where the inner length field is not in the grammar at all: it is bytes the producer planted in another value's payload, then summoned through COPY into the role of a length.

September 8, 2012

The COPY-handling code with the assumption comment was introduced in commit d0f6821d, "get BLESSV working and fixup some COPY related issues," authored by Yves Orton at his [email protected] address on September 8, 2012. The previous day, commit 983cec5d had reorganized the repository from a single-Perl-tree layout into the Perl, Java, Go, and JavaScript trees Sereal has today. The decoder, in its current C form, is one day older than the comment that explained why it skipped a bounds check.

Sereal was Booking.com's internal serialization format. The blog posts and conference talks that accompanied the open-source release benchmarked it against Storable, JSON::XS, and Data::MessagePack on the workload Booking.com cared about, which was caching Perl data structures inside Booking.com's infrastructure. Inside Booking.com's infrastructure, the producer and the consumer of every Sereal blob were under the same operator's control, and a COPY offset pointing at non-tag bytes was a producer bug, not an attacker primitive. The threat model the comment is written against is the threat model of a closed cache.

The package the comment shipped to CPAN as was the same package with the same threat model. The consumers who picked up Sereal::Decoder were operators who read the Booking.com benchmark, configured Sereal as the value serializer for Redis caches, message queues, log pipelines, and inter-service RPC, and ran the decoder against whatever bytes those subsystems delivered. The decoder did not know the producer had changed. The comment did not know the producer had changed. The format spec did not, until the v5.005 commit, even document that COPY's target byte must precede the COPY tag. The five-line addition to sereal_spec.pod in the same commit is the spec catching up to the runtime, which is catching up to a threat model that has been live for thirteen years.

Sereal's SECURITY.md is also new in 5.005. The project did not have a security policy before this CVE.

The 5.005 patch's check, from + key_len > copytag_start, is six tokens of arithmetic that the September 8, 2012 comment said was unnecessary because it had already happened. The check had never happened. The comment was the bounds check.

The comment shipped in September 2012. The bounds check shipped in May 2026.