Two heap-buffer-overflows in libswscale, both triggered by odd output
widths. Found by fuzzing sws_scale directly (custom harness parsing
srcW/srcH/dstW/dstH/srcFmt/dstFmt/flags from the first 16 input bytes,
frames allocated with av_image_alloc). Both reproduce on current master
(aab6cec3e8, clang ASAN build).

== Bug 1: yuv2rgb_write odd-dstW OOB write (supplements #11693) ==

Trac #11693 (2025-07-28) reports this via the yuv2rgb_X_c_template path
(yuv2rgb15_X_c, rgb555le). We hit the same root cause through a different
template path:

    #0 yuv2rgb_write       libswscale/output.c:1751:25
    #1 yuv2rgb_2_c_template libswscale/output.c:1881:9
    #2 yuv2rgb12_2_c        libswscale/output.c:1995:1
    #3 packed_vscale        libswscale/vscale.c
    #4 ff_swscale / scale_internal / sws_scale

ASAN: WRITE of size 2, 0 bytes after the 62179-byte av_image_alloc'd
destination frame.

Root cause (the analysis missing from the ticket):

    yuv2rgb_2_c_template:
        for (i = 0; i < ((dstW + 1) >> 1); i++)
            ...
            yuv2rgb_write(dest, i, Y1, Y2, ...)

    yuv2rgb_write (16bpp targets):
        dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
        dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];

With odd dstW the last iteration writes dest[dstW] — one uint16 past the
row. On every row except the last this silently corrupts the first pixel
of the next row (still inside the frame allocation, so ASAN stays quiet);
on the last row it writes past the end of the frame buffer.

Minimal trigger parameters: dstW=129 (odd), dstH=241,
srcFmt=1, dstFmt=53 (AV_PIX_FMT_RGB444BE), flags=1.

PoC (88 bytes, hex):

0042 0035 0081 00f1 0100 3500 0100 0000
0000 0008 3fff ffff ffff ffff ffff ffff
ffff ffff ffff ffff ffff ffff ffff ffff
ffff ffff ffff ffff ffff ffff ffff ffff
ffff ffff 23ff ffff ffff ffff ffff ffff
ff00 0042 0035 0000

We confirmed 11 distinct stack signatures for this bug across the
2_c_template family (rgb444be/rgb444le/rgb555/rgb565 variants). Any fix
needs to cover both the X_c_template and 2_c_template (and 1_c_template)
paths — they all share yuv2rgb_write.

== Bug 2: bayer_*_to_rgb48_copy odd-width OOB (new, no existing ticket) ==

    libswscale/bayer_template.c:229 (BAYER_RENAME(rgb48_copy)):

        for (i = 0 ; i < width; i+= 2) {
            BAYER_TO_RGB48_COPY
            src += 2 * BAYER_SIZEOF;
            dst += 6;
        }

Each iteration writes a full two-pixel block (6 uint16). With odd width
the final iteration writes the (width+1)-th pixel — 3 uint16 past the row.
Same last-row-exposes-the-heap-overflow behavior as Bug 1.

ASAN (current master):

    #0 bayer_bggr16le_to_rgb48_copy libswscale/bayer_template.c:229:9

(also reproduced via bayer_gbrg16be_to_rgb48_copy).

PoC (82 bytes, hex):

00c1 06a8 0080 0009 8f00 0900 0000 0080
0080 0000 2900 0400 2323 2323 2306 0606
0606 0606 0623 2323 236d 6d6d 6d6d 6d6d
6d6d 6d6d 6d6d 6d00 0080 0080 0081 0000
0000 0000 0000 feff 2fff ffff 87ff 009d
4a80

== Suggested fix direction ==

For both: clamp the final iteration to the actual remaining pixel count
(e.g. handle the odd tail pixel separately, or round the loop count down
and write the last pixel individually), or require callers to allocate
rows rounded up — but the current contract of av_image_alloc does not
guarantee padding past dstW, so the write-side clamp is the safer fix.

== Reproduction ==

    git clone https://git.ffmpeg.org/ffmpeg.git && cd ffmpeg
    ./configure --cc=clang --disable-everything --enable-swscale \
      --disable-doc --disable-programs \
      --extra-cflags="-fsanitize=address -g -O1 -fno-omit-frame-pointer" \
      --extra-ldflags="-fsanitize=address"
    make -j32
    # harness: LLVMFuzzerTestOneInput parses the 16-byte header above,
    # av_image_alloc both frames, sws_scale(src -> dst)
    clang -fsanitize=address,fuzzer -g -O1 -I . -I ./include harness.c \
      libswscale/libswscale.a libavutil/libavutil.a -lm -o fuzz_sws
    ./fuzz_sws poc.bin

Full PoC set (13 files) and per-signature replay results available on
request.

—
Feng Ning / Innora Security Research

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to