[Swift] Verify size-prefixed roots from reader position #9196
@carrerasdarren-cell Thanks for opening the PR. I was looking at the cpp approach. And it would be better if the swift implementation also follows a similar convention. Where we only skip after we have verified the prefixed value too. It's fine if we duplicate the logic for the verification here getPrefixedSizeCheckedRoot.
https://github.com/google/flatbuffers/blob/81edeb17d9118143f2c81caf27edfb0df401279e/include/flatbuffers/verifier.h#L256
https://github.com/google/flatbuffers/blob/81edeb17d9118143f2c81caf27edfb0df401279e/tests/monster_test.cpp#L609
Thanks, updated in 83b8c03. Both checked size-prefixed entry points now read and validate the UOffset prefix before calling skipPrefix(). getPrefixedSizeCheckedRoot applies the C++-style prefix <= available bytes check, while getCheckedPrefixedSizeRoot preserves its exact-size requirement. I also added an oversized-prefix regression that verifies rejection occurs without advancing ByteBuffer.reader. The production Swift modules compile with Swift 6.3.3, the modified sources/tests parse, git diff --check passes, and the deterministic malformed/valid harness still passes.
nit: & instead of normal
nit: &+
Not needed; but if you see fit, and the logic is movable. Would it make sense for this to live in the verifier instead?
LGTM! amazing work! one of the comments can be disregarded if not needed
@carrerasdarren-cell just pinging you so we can get this PR to the finish line
Addressed in b76d5d5.
Addressed in b76d5d5.
Thanks for the ping. I addressed the remaining wrapping-arithmetic nits in b76d5d5, replied to the optional placement suggestion, and resolved all three review threads. The production Swift module compiles cleanly locally with Swift 6.3.3; the refreshed CI run is now starting.
Approved these changes.
@carrerasdarren-cell thanks for your contribution
Ownership verification for Google OSS VRP report 543524003: I am the author of this pull request and the reporter of that issue. The report was submitted using artemis@perpetualsai.com; this GitHub account uses its GitHub-provided noreply commit address.
mustiikhalil
carrerasdarren-cell
Summary
ByteBuffer.readerpositionProblem
Both size-prefixed checked-root APIs call
skipPrefix()before delegating togetCheckedRoot. The returned table is constructed from the post-prefix reader position, but verification previously followed a root offset from byte zero and checked a file identifier at byte four.A malformed size-prefixed buffer could therefore place a valid decoy root where the verifier looked while returning a different malformed table from the post-prefix root. The same malformed table is rejected when checked directly, but the prefixed wrappers accepted it. Generated accessors or mutators could then operate outside the buffer bounds despite the checked API succeeding.
Fix
Capture the active reader position once and use it consistently for file-identifier verification, root verification, and table construction. The file-identifier bounds check now accounts for that position without wrapping on short buffers.
Validation
swiftcgit diff --checkpasses