[BUG] Heap-buffer-overflow (OOB read) in offline transducer greedy search decoder when decoder.onnx vocab_size metadata mismatches joiner output width

Author: 3em0Created Sep 24, 2026Updated Sep 29, 2026
Labelsbug

Important Instructions ⚠️

Before submitting this issue, please read carefully and confirm the following:

  • I am using the latest version of sherpa-onnx. If not, I will upgrade and check if the issue persists.
  • I understand that issues not reproducible on the latest version may be closed without response.
  • I have verified that the error is reproducible using only sherpa-onnx, not code from my own project.
  • I will provide detailed steps to reproduce the issue.
  • I will provide complete logs and error messages.
  • I understand that issues not following these instructions may be closed or receive no response (如果直接删掉这个issue template 里的步骤,我们很有可能不会回复您的issue).

Describe the issue

The offline transducer greedy search decoder trusts the vocab_size entry in decoder.onnx custom metadata without validating it against the actual output width of joiner.onnx. The metadata is parsed with atoi() and only checked for being non-negative (sherpa-onnx/csrc/macros.h, SHERPA_ONNX_READ_META_DATA); no upper bound and no cross-file consistency check exist.

When a model directory declares vocab_size = 4096 in decoder.onnx metadata while joiner.onnx actually outputs 4 logits per frame, the greedy decoder uses 4096 as the per-frame length over the joiner output buffer:

  • LogSoftmax(p_logit, vocab_size) reads and writes 4096 floats per frame (sherpa-onnx/csrc/math.h:81 first dereferences std::max_element past the end of the buffer; lines 89-91 then write input[i] -= offset)
  • std::max_element(p_logit, p_logit + vocab_size) and p_logit += vocab_size (offline-transducer-greedy-search-decoder.cc:61, 65) keep walking out of bounds for every frame

Running sherpa-onnx-offline with such a model pack crashes under AddressSanitizer with a heap-buffer-overflow READ inside LogSoftmax; in a non-ASan build the subsequent out-of-bounds write (math.h:90) executes and corrupts the heap.

A negative control - the identical model pack with only the decoder metadata corrected to vocab_size = 4 - recognizes normally, confirming the metadata / joiner-width mismatch alone is what triggers the overflow.

I found this while auditing how sherpa-onnx validates model metadata and would like to coordinate disclosure. If a private reporting channel is available, please let me know and I will move the full details there.

Steps to reproduce

  1. Build sherpa-onnx-offline with AddressSanitizer (-fsanitize=address in CMAKE_CXX_FLAGS / CMAKE_C_FLAGS).

  2. Create a model directory evil/ with the official ONNX toolchain: evil/ ├── encoder.onnx # metadata_props: model_type = "zipformer2" ├── decoder.onnx # metadata_props: vocab_size = 4096, context_size = 2 ├── joiner.onnx # output tensor: last dimension = 4 └── tokens.txt The only anomaly is decoder.onnx metadata claiming 4096 vocabulary entries while the joiner emits 4 logits per frame. Note the encoder must carry model_type "zipformer2" (or "conformer"/"zipformer"), otherwise the recognizer rejects the pack before reaching the vulnerable code.

  3. Run: env ASAN_OPTIONS=detect_leaks=0 ./sherpa-onnx-offline
    --debug=true --provider=cpu:provider_noarena.config
    --encoder=evil/encoder.onnx
    --decoder=evil/decoder.onnx
    --joiner=evil/joiner.onnx
    --tokens=evil/tokens.txt
    test.wav where provider_noarena.config contains "EnableCpuMemArena=0". This documented option disables the ORT CPU memory arena so every tensor gets its own allocation and ASan can see the over-read; with the arena at its default the same out-of-bounds access happens silently (the recognition output is then computed over adjacent heap memory). The crash itself is caused by the metadata mismatch, not by this setting - see step 5.

  4. Observe the AddressSanitizer abort: heap-buffer-overflow READ of size 4 in LogSoftmax (sherpa-onnx/csrc/math.h:81, std::max_element), called from OfflineTransducerGreedySearchDecoder::Decode (sherpa-onnx/csrc/offline-transducer-greedy-search-decoder.cc:58); the faulting address is 0 bytes after the 64-byte joiner-output tensor allocated by libonnxruntime.

  5. Negative control: change decoder.onnx metadata to vocab_size = 4 and rerun with the identical command line. Recognition completes normally (exit 0), which isolates the vocab_size / joiner output-width mismatch as the trigger.

Expected behavior

The model loader should reject (with a clear error) any offline transducer model pack whose decoder metadata vocab_size is inconsistent with the joiner output tensor width - or should derive the per-frame logit length from the actual joiner output shape instead of trusting the metadata.

Environment

  • sherpa-onnx version: v1.13.8 (vulnerable code also present on master 040afe360a38e25daaa325ce8889abf93ea02609, which is what was built for this report)
  • OS: WSL2 Ubuntu 24.04 x86_64 (host: Windows)
  • Python version: 3.12.3 (only used to generate the ONNX models, onnx 1.23.0)
  • onnxruntime version: 1.28.2 (prebuilt CPU package linked by the sherpa-onnx build)
  • GPU (if applicable): not required (CPU build)
  • NPU (if applicable): none
  • Build: sherpa-onnx-offline compiled with AddressSanitizer (g++ 13.3, -fsanitize=address -fno-omit-frame-pointer -g -O1)

Logs

Full transcript of the crashing run is attached (images/sherpaonnx-vocab-size-logsoftmax-oob-03-asan-crash.png is a rendering of it). Core of the AddressSanitizer report:

==9123==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x50a000001500 at pc ... READ of size 4 at 0x50a000001500 thread T0 #0 std::__max_element<float*, ...> /usr/include/c++/13/bits/stl_algo.h:5715 #2 std::max_element<float*>(float*, float*) /usr/include/c++/13/bits/stl_algo.h:5739 #3 void sherpa_onnx::LogSoftmax(float*, int) sherpa-onnx/csrc/math.h:81 #4 sherpa_onnx::OfflineTransducerGreedySearchDecoder::Decode(...) sherpa-onnx/csrc/offline-transducer-greedy-search-decoder.cc:58 #5 sherpa_onnx::OfflineRecognizerTransducerImpl::DecodeStreams(...) 0x50a000001500 is located 0 bytes after 64-byte region [0x50a0000014c0,0x50a000001500) allocated by thread T0 here: #1 operator new(unsigned long, std::align_val_t) (libonnxruntime.so) SUMMARY: AddressSanitizer: heap-buffer-overflow [exit 1]

(The OOB write at sherpa-onnx/csrc/math.h:90 is not reached because ASan halts on the first error; it does execute in non-instrumented builds.

With the ORT CPU memory arena at its default (enabled) the same run completes without an ASan report while the recognition log-probs visibly differ from the negative control and track adjacent heap contents - the over-read happens either way; the arena only decides whether ASan sees it. The negative control (decoder metadata vocab_size = 4) exits 0 under the identical command line.)