Skip to content

Hardening: Adopt safe buffer models, bounds-checked roots, and verifier pointer safety - #9267

Open
jagadish-chandra-vantimi wants to merge 2 commits into
google:masterfrom
jagadish-chandra-vantimi:hardening/memory-safety-parser-enhancements
Open

jagadish-chandra-vantimi wants to merge 2 commits into
google:masterfrom
jagadish-chandra-vantimi:hardening/memory-safety-parser-enhancements

Conversation

@jagadish-chandra-vantimi

Copy link
Copy Markdown

Summary of Changes

This patch introduces proactive memory-safety hardening and safe buffer abstractions across core FlatBuffers parsing and verification infrastructure:

  1. FlexBuffers Root Width Validation (flexbuffers.h):

    • Enforce valid power-of-two scalar byte widths (1, 2, 4, 8) and buffer size sufficiency during root extraction to prevent out-of-bounds reads and stack buffer inconsistencies on malformed inputs.
  2. Pointer Range & Relational Comparison Safety (verifier.h):

    • Implement uintptr_t total-ordering pointer range checking (CheckPointerInRange) across verifier entrypoints to prevent undefined behavior in pointer relational operations and avoid pointer wrap hazards.
    • Fix arithmetic in size-prefixed buffer verification (size_ - sizeof(SizeT)) to prevent integer overflow wrapping.
    • Enforce structural vtable minimums (vsize >= 2 * sizeof(voffset_t)), cleanly rejecting degenerate tables.
    • Propagate depth and table traversal budgets across nested FlatBuffers verification to prevent excessive recursion.
  3. Secure-by-Design Verification APIs (verifier.h):

    • Introduce canonical GetVerifiedRoot<T> and GetVerifiedSizePrefixedRoot<T> in flatbuffers/verifier.h so consuming applications can verify and deserialize buffer roots in a single atomic call.
  4. Table Offset Validation (table.h):

    • Prevent table metadata field misinterpretation by rejecting queries for internal metadata offsets (offsets < 4) and unaligned field indices in GetOptionalFieldOffset.
  5. Safe Buffer Model & Bounds-Checked Accessors (vector.h, array.h, stl_emulation.h, buffer_ref.h):

    • Add GetOptional(i), GetSafe(i, default), and runtime bounds-checked MutateSafe(i, val) to vectors and arrays.
    • Provide first-class span integration across detached buffers, buffer references, and root accessors.
  6. Automated Testing (tests/test.cpp):

    • Add MemorySafetyHardeningTest covering all boundary conditions, malformed header rejections, safe mutation routines, and verified root extraction.

…er pointer safety

- Adopt total-ordering pointer range validation in verifier (CheckPointerInRange) to avoid pointer relational comparison UB and wrap hazards.

- Validate FlexBuffers scalar widths to powers of two (1, 2, 4, 8) and enforce buffer length sufficiency on root parsing.

- Mitigate integer overflow in size-prefixed buffer verification.

- Enforce structural vtable size minimums (vsize >= 4) and reject metadata offsets (< 4) in GetOptionalFieldOffset.

- Propagate depth and table count budgets across nested FlatBuffers verification.

- Introduce canonical GetVerifiedRoot<T> and GetVerifiedSizePrefixedRoot<T> safe entrypoints.

- Add GetOptional, GetSafe, and MutateSafe to Vector and Array primitives.

- Add comprehensive unit tests in MemorySafetyHardeningTest.
@google-cla

google-cla Bot commented Sep 20, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added the c++ label Sep 20, 2026

This branch has not been deployed

No deployments
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.

1 participant