[scudo] Check quarantine batch bounds in release builds (#230247)

[Attacking Scudo's Quarantine, section 3: Write Where
Ptr](https://un1fuzz.github.io/articles/quarantine_attack.html#a3)
describes corrupting a quarantine batch's `Count` so that enqueue writes
the freed pointer outside the batch. The original [PoC and exploit code
are
here](https://github.com/un1fuzz/scudo_research/tree/main/quarantine_arbitrary_return).
`push_back()` currently guards its index with a debug-only check; an
oversized count also bypasses enqueue's full-batch equality check.

Make that bound a release-build check. Also validate both counts before
deciding whether batches can merge, enforce merge capacity in
production, and check the count before shuffling (which precedes
recycling). The capacity comparison uses subtraction after validating
both operands, avoiding corrupted-count addition wrapping around.

This closes the out-of-bounds enqueue primitive described in section 3.
It does not address section 2's Double Return attack using in-range
counts and stale entries, or authenticate batch pointers. Tests exercise
the corrupted-count mechanism; the original end-to-end exploit was not
replayed.

Validation on Ubuntu WSL x86_64 with Clang 21.1.8:

- Full debug-checks suite: 802 passed, 44 skipped; release: 802 passed,
40 skipped; zero failures.
- Five new corruption tests fail against the original release header and
pass with this patch. Coverage includes full/oversized counts, both
merge operands, sum wraparound, enqueue, shuffle, and recycling; valid
empty/full batch behavior is retained.
- All 14 quarantine tests pass for 20 shuffled ASan/UBSan runs.
- Clang-format and diff whitespace checks pass.

A local pinned-CPU `-O2` microbenchmark measured median append cost of
1.155 to 1.185 ns per entry and cache enqueue cost of 1.776 to 1.827 ns
(about +3%, with overlapping sample ranges). This is not an end-to-end
allocator benchmark and does not measure merge/shuffle cost. The checks
execute on quarantine paths; enabling quarantine and its limits is
unchanged. Android, 32-bit, AArch64/MTE, and Fuchsia were not tested.

Assisted-by: OpenAI Codex. Draft for the author's review before
requesting maintainer review.
GitOrigin-RevId: acd27dbc4ae293113fcddab8576e17ab06299d0c
2 files changed