Skip to content

flatbuffer_utils: reject out-of-range Buffer offset/size before slicing - #7028

Open
cybrdude wants to merge 2 commits into
google-ai-edge:mainfrom
cybrdude:fix/flatbuffer-utils-overflow-hardening-20260422
Open

cybrdude wants to merge 2 commits into
google-ai-edge:mainfrom
cybrdude:fix/flatbuffer-utils-overflow-hardening-20260422

Conversation

@cybrdude

@cybrdude cybrdude commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

What

tflite/tools/flatbuffer_utils.py in read_model_from_bytearray()
slices the model bytearray using Buffer.offset / Buffer.size and
Operator.largeCustomOptionsOffset / ...Size without length
validation. Python slicing clamps out-of-range indices silently, so
an (offset, size) that wraps when added as uint64 or that
exceeds the bytearray length does not raise. It returns a clamped
or aliased slice instead.

Downstream, the LiteRT Python quantizer operates on these buffers as
if they were distinct objects, producing corrupted output tensors
when re-serialized.

How

Added overflow-safe bounds checks before each slice:

if offset > model_len or size > model_len - offset:
  raise ValueError(...)

Applied at both slice sites (Buffer and largeCustomOptions).

Relationship to TensorFlow

The same arithmetic pattern is being fixed in the C++ TFLite runtime
(interpreter_builder.cc) under a companion PR to
tensorflow/tensorflow. This change closes the Python-side tooling
that consumes the same external-offset feature.

Test

tflite/tools/flatbuffer_utils_overflow_test.py covers:

  • (offset, size) whose uint64 sum wraps below model_len = rejected
  • offset alone exceeds model_len = rejected
  • Well-formed (offset, size) within model_len = accepted

To run:

python -m pytest tflite/tools/flatbuffer_utils_overflow_test.py

@cybrdude

Copy link
Copy Markdown
Contributor Author

This PR closes #7029

@snnn snnn added the CLA:yes label Apr 24, 2026
@cybrdude

Copy link
Copy Markdown
Contributor Author

@copybara-github

@cybrdude

Copy link
Copy Markdown
Contributor Author

@snnn anything needed from me to merge?

@snnn

snnn commented May 1, 2026

Copy link
Copy Markdown
Member

Recently we moved the code to a different dir. So, I created a new branch ,apply your change to the new folder, and submitted it as #7196 .

@cybrdude

cybrdude commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

@snnn then the patch wont be credited properly?

Python slicing silently clamps out-of-range indices, allowing crafted
.tflite models to produce aliased buffers and corrupted re-serialized
output. Validate each (offset, size) pair against the model bytearray
length using overflow-safe arithmetic (size > model_len - offset)
before slicing, for both buffer.offset/size and op.largeCustomOptions
offset/size.

Adds regression tests covering uint64 wrap, offset exceeding model
length, size exceeding remaining bytes, and accepted boundary cases,
plus the corresponding BUILD target.

Re-applied at the new tree location (litert/python/tools/) following
upstream directory reorganization. Original review and approval at
google-ai-edge#7028; closed PR google-ai-edge#7196 superseded by this update per maintainer
request to preserve contributor attribution.
@cybrdude
cybrdude force-pushed the fix/flatbuffer-utils-overflow-hardening-20260422 branch from 34faca7 to 3c300ed Compare May 6, 2026 03:31
@cybrdude

cybrdude commented May 6, 2026 •

Copy link
Copy Markdown
Contributor Author

@snnn

Updated to apply the fix at the new tree location
(litert/python/tools/) per #7196, with BUILD entry for the
overflow regression tests included. -- ready for merge.

@cybrdude

cybrdude commented May 7, 2026

Copy link
Copy Markdown
Contributor Author

@snnn all 8 checks are green. Anything else needed from me before merge?

@cybrdude
cybrdude requested a review from snnn May 8, 2026 01:59
@cybrdude

Copy link
Copy Markdown
Contributor Author

@snnn would you happen to have an update on this?

@cybrdude

Copy link
Copy Markdown
Contributor Author

@snnn would you be able to review this PR & #8183

@cybrdude

Copy link
Copy Markdown
Contributor Author

@pyu10055 @niuchl @reillyeon would you be able to assist with this?

@cybrdude

cybrdude commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

@jl45G can this one also be closed?

@cybrdude

cybrdude commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

@snnn This PR has been idle since May. State recap: you approved it on Apr 24, CLA is signed, and all 8 checks are green. After the directory move I force-pushed the change to litert/python/tools/ so it applies to the current tree, and #7196 was closed in favor of this PR.

Could you merge it or queue the import? If it has since been superseded by another change, let me know and I will close it out.

@cybrdude

cybrdude commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

@sourcelite @jl45G @snnn This is ready to land and is blocked only on the workflow-approval gate.

State: approved by snnn (write access), CLA green, check-changes green, copybara reports no internal file affected, no merge conflicts. The only item outstanding is the 6 workflow runs awaiting maintainer approval, which is holding the GitHub Actions Scan check at "expected, waiting to run."

Could you approve the pending workflow runs and merge once they pass?

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