Reject out-of-range GATHER index values at runtime to prevent OOB read#3593
Open
evilgensec wants to merge 1 commit into
Open
Reject out-of-range GATHER index values at runtime to prevent OOB read#3593evilgensec wants to merge 1 commit into
evilgensec wants to merge 1 commit into
Conversation
The reference GATHER kernel indexes the input tensor with the values in the `positions` tensor and only guards them with TFLITE_DCHECK_GE/LT, which expand to no-ops under -DNDEBUG (the release build). A negative or too-large index supplied as a runtime input therefore makes GatherEval read outside the input tensor (heap-buffer-overflow). GatherPrepare validates axis and batch_dims but never the index values. Validate the index values in GatherEval and return kTfLiteError for any index outside [0, axis_size), mirroring the runtime index check in the full TFLite GATHER kernel. Adds regression tests for an out-of-range and a negative index.
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.
BUG=GATHER reference kernel reads out of bounds on out-of-range index values (memory-safety fix)
Problem
GatherEval(tensorflow/lite/micro/kernels/gather.cc) indexes the input tensor with the values from thepositionstensor. Those values are only checked withTFLITE_DCHECK_GE/TFLITE_DCHECK_LT, which expand to a no-op under-DNDEBUG— i.e. in therelease/release_with_logsbuilds used on device.GatherPreparevalidatesaxisandbatch_dims, but never the index values themselves.When a GATHER op's
positionsare a runtime input (e.g. an embedding lookup whose index comes from the application's input), a negative or too-large index makes the kernel read outside the input tensor — a heap-buffer-overflow read whose bytes are copied into the output. The full TFLite GATHER kernel guards index values at runtime (TF_LITE_ENSURE(context, indices_has_only_positive_elements)plus the reference bounds handling) and returnskTfLiteError; the Micro kernel does not.Reproduction
release_with_logsbuild (so-DNDEBUGstrips the DCHECKs) with AddressSanitizer; a benign GATHER over a 3x4 table with one runtime index value of3(axis size is 3):A negative index (
-1) reads before the table.Fix
Validate the index values in
GatherEvaland returnkTfLiteErrorfor any index outside[0, axis_size)before dispatching to the reference kernel. Adds regression testsGatherOp_IndexGreaterThanAxisSizeFailsClosedandGatherOp_NegativeIndexFailsClosed. With the fix the out-of-range index returnskTfLiteErrorwith no out-of-bounds access;kernel_gather_testpasses 20/20.