Integer overflow in tensor size calculations leads to heap buffer overflow#3524
Open
chupaohong wants to merge 1 commit intotensorflow:mainfrom
Open
Integer overflow in tensor size calculations leads to heap buffer overflow#3524chupaohong wants to merge 1 commit intotensorflow:mainfrom
chupaohong wants to merge 1 commit intotensorflow:mainfrom
Conversation
|
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. |
Author
|
Hello, For detailed information about the exploit code (poc), please review the ticket via link: https://issuetracker.google.com/issues/501064564 Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Vulnerability
BytesRequiredForTensor,TfLiteEvalTensorByteLength,ElementCount, andRuntimeShape::FlatSizeuseintto accumulate element counts. A malformed model with large tensor dimensions (e.g. shape[1024, 1024, 4096]) overflowsintand wraps to zero. Kernel ops then write past the undersized buffer, causing out of bounds write and memory corruption.Same bug class as #3516, but in the allocation helpers rather than a single kernel, so all operators are affected.
Attack scenario
Any application that loads a
.tflitemodel from an external source (OTA model update, SD card, model marketplace, user upload) is vulnerable. The attacker only needs to modify the tensor shape field in the FlatBuffer; no special configuration or flags are required on the target.tflite-micro runs primarily on microcontrollers where ASLR and heap guards are usually absent, so going from heap corruption to code execution is not difficult. The attacker controls both the allocation size (via tensor dimensions) and the data that gets written (via model weights).
On Android or Linux hosts the same overflow causes heap corruption and likely a crash. Code execution depends on heap layout.
Root cause
int element_countinBytesRequiredForTensor(memory_helpers.cc:109) andTfLiteEvalTensorByteLength(:130)int resultinElementCount(micro_utils.cc:30)int buffer_sizeinRuntimeShape::FlatSize(runtime_shape.h:97)All four overflow on large positive dimensions and the result feeds into arena allocation sizes.
Fix
Change
inttosize_tfor the accumulator in each location and cast individual dims tosize_tbefore multiplying.Verification
Before:
BytesRequiredForTensorreturns0for shape[1024, 1024, 4096]uint8.After: returns
4294967296.BUG=None