TFLite: Fix string input buffer sizing in SetStringData#4141
Open
Alearner12 wants to merge 1 commit into
Open
Conversation
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.
Fix TFLite string input buffer sizing in TensorFlow Serving
Summary
TfLiteInterpreterWrapper::SetStringData()sizes the TFLite string tensor buffer header frombatch_size, but writes one offset entry for every flattened string element in the TensorFlow input tensor. For non-rank-1 string inputs, a request can make the flattened string count larger thanbatch_size, causing the offset table writes to exceed the allocated buffer.This change sizes the string buffer from the actual flattened string count, keeps offsets as
size_tuntil the final checked conversion to TFLite'sint32_toffset format, and adds overflow/allocation checks. A regression test covers a shape[1, 2]string tensor, where the first dimension is1but the flattened string count is2.Reachability
This is reachable through the supported TensorFlow Serving Predict path when TFLite serving is enabled:
tensorflow_model_server --prefer_tflite_model=truesetsSessionBundleConfig.prefer_tflite_model.SavedModelBundleFactoryloadsmodel.tfliteintoTfLiteSession.PredictionServiceImpl::Predict()and RESTHttpRestApiHandler::ProcessPredictRequest()route request tensors throughTensorflowPredictor::Predict().TfLiteSession::SetInputAndInvokeMiniBatch()handles string inputs by resizing the TFLite input to{batch_size}and then callingSetStringData()with the full TensorFlow string tensor.For a string tensor with shape
[1, 2],batch_sizeis1whiletensor.flat<tstring>().size()is2. No guard rejects that shape beforeSetStringData()writes the string offset table.Impact
The confirmed primitive is a heap buffer overflow in the TFLite string input marshalling path. A minimal ASan reproduction of the original arithmetic with
batch_size = 1and two flattened string elements reports:The direct overwrite is controlled by the number of flattened strings and their offsets. Practical impact depends on deployment using TFLite model serving and on allocator/layout conditions, so the conservative impact statement is remote process memory corruption in TFLite-enabled TensorFlow Serving.
Fix
std::vector<size_t>.total_size,num_strings, and the final byte sizes againststd::numeric_limits.int32_toffset type.[1, 2]) to ensure the buffer sizes itself against the flattened string count correctly.Verification
Performed:
SetStringData().Not performed: