Skip to content

A corrupt WAL page-count field lets recovery allocate an unbounded WALPage[] and die on an Error the recovery guard cannot catch #6932

Closed

Description

@ruispereira

Summary

WAL recovery reads a transaction's segment count straight from the file and allocates a WALPage[] of that size with no sanity check. The neighbouring per-page guard was added by #4958 for exactly this hazard and says so; the outer count has the same exposure and no guard. The resulting OutOfMemoryError is an Error, so the catch (Exception) recovery guard cannot catch it.

Where (HEAD c1785ced6)

engine/src/main/java/com/arcadedb/engine/WALFile.java:167 and :177, in getTransaction:

final int pages = readInt(pos);
...
tx.pages = new WALPage[pages];

The guard that exists, at :212, added by #4958: "a corrupt per-page header can still declare a huge deltaSize ... and trigger a transient ~2GB allocation, or an OutOfMemoryError that escapes the catch(Exception) recovery guard below". At pages = Integer.MAX_VALUE the array alone is ~17GB of references, and catch (final Exception e) { return null; } (:243) cannot catch the Error.

The count is derivable and therefore checkable: segmentSize is already bounded against the file length at :173, and every segment costs at least PAGE_HEADER_SIZE + 1 bytes, so any legal pages satisfies pages <= segmentSize / (PAGE_HEADER_SIZE + 1). segmentSize itself is never checked for sign, so a negative value makes the :173 bound pass trivially.

Repro

TransactionManager.checkIntegrity walks a WAL with arbitrary bytes twice over: getFirstTransaction/getTransaction(nextPos) (:341, :385) and findNextValidTransactionPosition (:344, :386). The latter derives a candidate txStart from every 8-byte window in the file that happens to equal MAGIC_NUMBER - a value that can occur inside a page payload - and calls getTransaction(txStart) on it. So a single corrupt or torn WAL feeds getTransaction fully arbitrary header bytes.

checkIntegrity has a try { } finally { } with no catch (:304, :457), so the Error unwinds out of LocalDatabase.open before either the WAL drop or the .corrupt rename runs: the database cannot be opened, and the next attempt hits the same bytes. That is the same permanent open-failure loop #5063 fixed for a different cause.

Issue4508TornWALRecoveryTest and Issue4958PreservedWALTest cover the torn and preserved paths but neither injects a bogus page count.

Suggested fix

After reading segmentSize at :170, reject segmentSize < 0; after reading pages at :167, reject pages < 0 || (long) pages * (PAGE_HEADER_SIZE + 1) > segmentSize by returning null - the same benign "not a transaction" answer the surrounding checks already give - before the array is allocated.

Scope / confidence

High. Verified getTransaction is reached with unvalidated positions rather than only at record boundaries, and that the scan's only guards on the derived start are segmentSize >= 0 and txStart >= fromPos, neither of which constrains pages. Searched upstream for prior art: only #4958, which fixed the per-page deltaSize allocation and left this one.

Activity

  1. added
    storage
    on Aug 31, 2026
  2. added this to the 26.9.1 milestone on Aug 31, 2026
  3. self-assigned this
    on Aug 31, 2026
  4. added a commit that references this issue on Sep 1, 2026
  5. robfrank commented on Sep 1, 2026

    @robfrank
    Collaborator

    Fixed in #6962 (merged).

    When recovery scanned a damaged write-ahead log, it read the number of pages in a transaction straight off disk and immediately allocated an array that size, without checking the value made any sense. A corrupt or torn WAL could therefore claim a transaction of billions of pages, and the resulting OutOfMemoryError escaped the recovery guard entirely, so the database failed to open and every retry died the same way. WALFile.getTransaction now rejects a negative segment size and any page count that could not possibly fit in the segment the record itself declares, treating such a record as "not a transaction" exactly the way it already treats a torn one. A second, quieter hole was fixed alongside it: a hand-built record declaring a negative segment size used to parse cleanly.

    The bound is exact rather than a guess, so valid transactions at the tightest legal density still recover normally. Seven regression tests plant the corrupt bytes directly; without the fix, two of them do not merely fail but kill the whole test JVM.


    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  6. ruispereira commented on Sep 1, 2026

    @ruispereira
    ContributorAuthor

    Re-validated against main at 8cfac2923: confirmed fixed, including every part this issue listed. No follow-up needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugstorage

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions