-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"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`:\n\n```c\n/* note we do NOT validate these items, as we have alread read them\n * and if they were a problem we would not be here to process them! */\n```\n\nCVE-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.\n\n## The COPY tag's contract was a back-reference the decoder never indexed\n\nSereal 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.\"\n\nThe 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.\n\nThe format specification describes COPY's offset as pointing to a \"previously emitted tag.\" That phrasing is the [Convention Is The Allowlist](/patterns/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.\n\nThat 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.\n\n## What the regression test plants\n\nThe 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:\n\n```\n0x26 0x1E ; BINARY tag, declared length = 30\n0x42 0x42 0x42 ... 0x42 0x7F ; 29 'B' bytes, then 0x7F\n0x2C 0x2F 0x22 ; OBJECT, COPY, varint 34\n0x28 0x25 ; REFN, UNDEF\n```\n\nThe 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.\n\nThe 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`.\n\nThe 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.\n\nThe 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.\n\n## What the fix asserts is what the comment said had happened\n\nThe 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`:\n\n```c\n} else if (tag == SRL_HDR_COPY) {\n const U8 *copytag_start = dec->buf.pos - 1;\n UV ofs = srl_read_varint_uv_offset(aTHX_ dec->pbuf, \" while reading COPY tag\");\n from = dec->buf.body_pos + ofs;\n tag = *from++;\n if (IS_SRL_HDR_SHORT_BINARY(tag)) {\n key_len = (KEYLENTYPE)SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag);\n }\n ...\n if (expect_false(from + key_len > copytag_start)) {\n SRL_RDR_ERRORf2(dec->pbuf,\n COPY_OFFSET_ERROR_FMT_PREFIX \" while reading key\",\n (int)ofs, (int)key_len);\n }\n}\n```\n\n`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.\n\nThe 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:\n\n```c\nif (expect_false((IV)item > dec->buf.end - dec->buf.start)) {\n SRL_RDR_ERRORf1(dec->pbuf, \"COPY(%d) points out of packet\", (int)item);\n}\n```\n\nThe new check is:\n\n```c\nif (expect_false(target_start > copytag_start)) {\n SRL_RDR_ERRORf1(dec->pbuf, \"COPY target %d must preceed its own offset\",\n (int)ofs);\n}\n```\n\nThe 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.\n\n## The outer frame, the inner length\n\nThe mechanism is the [Frame Bounds Headers Only](/patterns/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.\n\nThe 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](/posts/optee-pkcs11-cve-2026-33317-two-length-fields-one-bound) shows an inner length writing past the outer frame. The Netty QPACK exhibit at [CVE-2026-42582](/posts/netty-qpack-cve-2026-42582-patch-closes-three-advisory-names-one) 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.\n\n## September 8, 2012\n\nThe 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 `yves.orton@booking.com` 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.\n\nSereal 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.\n\nThe 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.\n\nSereal's SECURITY.md is also new in 5.005. The project did not have a security policy before this CVE.\n\nThe 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.\n","closing_line":"The comment shipped in September 2012. The bounds check shipped in May 2026.","hook_md":"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`:\n\n```c\n/* note we do NOT validate these items, as we have alread read them\n * and if they were a problem we would not be here to process them! */\n```\n\nCVE-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.\n\nThe comment was the bounds check.","post_id":587,"slug":"sereal-decoder-cve-2026-8796-copy-bounds-check-was-a-2012-comment","title":"CVE-2026-8796: Sereal::Decoder's COPY Bounds Check Was a Comment From 2012","type":"initial","unreadable_sentence":"For thirteen years and seven months, the bounds check on Sereal's COPY tag was a sentence in English explaining why the bounds check was not necessary."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCar1sZQAKCRDeZjl4jgkQ JuvgAQC+WzRxXzv4ZbkQK+MJ3rPLIhp3wwreYPewdITsaWjn/gEAkyvssWDg/Jzj lM4eMdcvBI2qzxG6oCYHxn9mZL3jGg4= =t7uj -----END PGP SIGNATURE-----