Skip to content

[Swift] Fix verifier accepting truncated scalar vectors (OOB read/write, RCE)#9081

Merged
mustiikhalil merged 1 commit intogoogle:masterfrom
alimezar:fix-swift-truncated-vector-verifier
May 8, 2026
Merged

[Swift] Fix verifier accepting truncated scalar vectors (OOB read/write, RCE)#9081
mustiikhalil merged 1 commit intogoogle:masterfrom
alimezar:fix-swift-truncated-vector-verifier

Conversation

@alimezar
Copy link
Copy Markdown
Contributor

@alimezar alimezar commented May 7, 2026

Summary

getCheckedRoot accepted malformed FlatBuffers whose scalar vector element count did not match the available payload bytes. After verification succeeded, the generated accessors and mutators read and wrote past ByteBuffer.capacity, producing out-of-bounds memory disclosure, out-of-bounds memory corruption, and code execution when corrupted adjacent control data was later invoked.

Reported as Google IssueTracker issue 510740173.

Root cause

Verifiable.verifyRange passed the vector element count directly to rangeInBuffer as a byte count. For any scalar vector whose element size is greater than one byte (e.g. [long], [int], [double]), the declared length only had to fit byteBuffer.capacity bytes after the vector header instead of count * elementSize bytes, so a buffer declaring N elements but containing only N raw bytes was accepted.

Fix

Multiply the declared element count by MemoryLayout<T>.size with overflow detection and pass the resulting byte size to rangeInBuffer, so truncated scalar vectors are rejected at verification time before any unsafe generated access can occur.

@alimezar alimezar requested a review from dbaileychess as a code owner May 7, 2026 14:58
@github-actions github-actions Bot added the swift label May 7, 2026
@mustiikhalil
Copy link
Copy Markdown
Collaborator

@alimezar Thanks for your PR, I am wondering if you can wait on merging it until the following PR lands (should be today).

1- If the swift Testing changes land today, then you would need to convert the test case to use swift testing, and if possible add a link to the ticket similar to the example below:

  @Test(.bug("https://github.com/google/flatbuffers/issues/8642"))
  func testReset() throws {}
  1. If they don't land today, if possible can you still give me a link to a ticket (if the ticket is public) so I can add it to the converted test.

@alimezar
Copy link
Copy Markdown
Contributor Author

alimezar commented May 7, 2026

Hi @mustiikhalil. The IssueTracker entry is private so I've opened a public issue

Regarding the PR I dont mind waiting, once it lands I'll rebase and convert the regression test to Swift Testing.

Thank you.

@mustiikhalil
Copy link
Copy Markdown
Collaborator

@alimezar Swift testing PR is merged

…te, RCE)

getCheckedRoot accepted malformed FlatBuffers whose scalar vector
element count did not match the available payload bytes. After
verification succeeded, the generated accessors and mutators read and
wrote past ByteBuffer.capacity, producing out-of-bounds memory
disclosure, out-of-bounds memory corruption, and code execution when
the corrupted adjacent control data was later invoked.

Root cause: verifyRange passed the vector element count to
rangeInBuffer as a byte count. For any scalar vector whose element
size is greater than one byte (e.g. [long], [int], [double]), the
declared length only had to be at most byteBuffer.capacity bytes
beyond the vector header, instead of count * elementSize bytes,
so a buffer declaring N elements but containing only N raw bytes was
accepted.

Fix: multiply the declared element count by MemoryLayout<T>.size with
overflow detection and pass the resulting byte size to rangeInBuffer,
so truncated scalar vectors are rejected before any unsafe generated
access can occur. Adds a regression test that builds a
Swift_Tests_Vectors-shaped buffer declaring a length-2 ulong vector
backed by only 2 bytes of payload and asserts getCheckedRoot throws.
@alimezar alimezar force-pushed the fix-swift-truncated-vector-verifier branch from d36820c to 4cc88dc Compare May 8, 2026 07:03
@alimezar
Copy link
Copy Markdown
Contributor Author

alimezar commented May 8, 2026

Hi @mustiikhalil,

Rebased onto master and converted the regression test to Swift Testing.

Copy link
Copy Markdown
Collaborator

@mustiikhalil mustiikhalil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@alimezar Thanks for opening a PR, and help improving the swift port 🎉

@mustiikhalil mustiikhalil merged commit 1f438bd into google:master May 8, 2026
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants