Rammadaeus's picture
Upload README.md with huggingface_hub
0714e84 verified

TFLite MEAN Operator Stack Buffer Overflow via ResolveAxis()

Vulnerability

The TFLite MEAN operator's ResolveAxis() function at reduce.cc:425 copies axis tensor elements from the model file into a fixed-size MeanParams.axis[4] array (4 elements, 8 bytes) on the stack without checking bounds. A crafted model with more than 4 axis elements causes a stack buffer overflow with attacker-controlled write size.

CWE-121: Stack-based Buffer Overflow

Impact

  • ASAN build: Crashes at the first out-of-bounds write (detected by ASAN shadow memory)
  • Production build (Python tensorflow 2.20.0): Crashes with SIGSEGV or SIGFPE depending on overflow depth
  • The overflow size is fully controlled by the attacker (axis tensor element count in the model file)
  • With 200 axis elements: 392 bytes of int16 values overwrite adjacent stack variables, saved frame pointer, and return address area

Reproduction

pip install tensorflow-cpu  # or tensorflow
python3 poc_generator.py 200
python3 -c "
import tensorflow as tf
interp = tf.lite.Interpreter(model_path='/tmp/mean_rce_axis200.tflite')
interp.allocate_tensors()
interp.invoke()  # crashes with SIGSEGV or SIGFPE
"

Or run ./reproduce.sh.

Files

  • poc_generator.py — Generates crafted TFLite model with MEAN op and large axis tensor
  • poc_axis200.tflite — Pre-generated PoC model (200 axis elements, 1360 bytes)
  • reproduce.sh — Self-contained reproduction script
  • asan_output.txt — Full ASAN crash trace

Root Cause

In tensorflow/lite/kernels/reduce.cc:420-427:

void ResolveAxis(const int* axis_data, int axis_count,
                 tflite::MeanParams* op_params) {
  int i = 0;
  for (; i < axis_count; ++i) {
    op_params->axis[i] = static_cast<int16>(axis_data[i]);  // No bounds check!
  }
  // ...
}

MeanParams.axis is declared in types.h as int16_t axis[4] (only 4 elements). axis_count comes from NumElements(op_context.axis) which reads the axis tensor shape from the model file — fully attacker-controlled with no validation against the array size.

Trigger Path

Model.Invoke()
  → Subgraph::InvokeImpl()
    → reduce::EvalMean<kGenericOptimized>()  [reduce.cc:551]
      → case kTfLiteUInt8:  [reduce.cc:611]
        → ResolveAxis(axis_data, num_axis, &op_params)  [reduce.cc:613]
          → op_params.axis[i] = ... for i=0..199  ← OVERFLOW at i=4

Requires UINT8 input tensor to reach the vulnerable code path.