PR #25975 opened by fuzzyrichie
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25975
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25975.patch

ljpeg_encode_yuv_mb() unconditionally reads full hsample x vsample blocks for a 
ceil(W/h) x ceil(H/v) grid, so any odd dimension with subsampled YUV reads 
samples from beyond the plane and DC-codes them into the JPEG 
(uninitialized-heap leak into the output; heap OOB read for exact-size 
buffers). Same class as the huffyuv median row overread in #25945 and 9e2f8ad5; 
the mjpeg decoder skips these samples (mjpegdec.c:1308) but the encoder has no 
bound. Reject the affected dimensions at init(); BGR input (1x1 sampling) is 
unaffected. ASAN repro and positive control (64x48 yuvj420p encodes) available 
on request; make fate passes.


>From 215d238fa63078946aa22746f69e610c2b02c140 Mon Sep 17 00:00:00 2001
From: Richard Desmarais <[email protected]>
Date: Thu, 12 Nov 2026 00:29:28 -0500
Subject: [PATCH] avcodec/ljpegenc: reject dimensions the macroblock grid reads
 past

ljpeg_encode_yuv_mb() unconditionally reads a full hsample x vsample
block for every macroblock of a ceil(W/h) x ceil(H/v) grid. For odd
dimensions the last macroblock row/column reads samples at
row == height / column == width, i.e. beyond the plane: the same class as
the huffyuv median predictor row overread (PR#25945) and the recently
fixed left-prediction row read (9e2f8ad5). Unlike the mjpeg decoder,
which skips these samples ("Nothing to do" in ljpeg_decode_yuv_scan,
mjpegdec.c:1308), the encoder has no bound on this path.

The out-of-plane samples are DC-coded into the output JPEG, leaking the
(uninitialized, with default padded buffers) heap contents into the
encoded stream; callers providing exact-size planes get a heap OOB read
(ASAN: ljpegenc.c:247, 0 bytes after the luma plane).

ff_mjpeg_init_hvsample() gives hsample == vsample == 2 for subsampled
YUV and vsample == 2 for YUV444P as well, so any odd height (and odd
width for subsampled formats) is affected. BGR input is handled by
ljpeg_encode_bgr(), is bound-safe for odd dimensions and keeps
hsample == vsample == 1, so it is unaffected by the new check.

Reject the affected dimensions at init() time; positive control
(64x48 yuvj420p) still encodes.

Signed-off-by: Richard Desmarais <[email protected]>
---
 libavcodec/ljpegenc.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/libavcodec/ljpegenc.c b/libavcodec/ljpegenc.c
index ab37ab0d5cc..e72a8fd3401 100644
--- a/libavcodec/ljpegenc.c
+++ b/libavcodec/ljpegenc.c
@@ -281,6 +281,15 @@ static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
 
     ff_mjpeg_init_hvsample(avctx, s->hsample, s->vsample);
 
+    /* ljpeg_encode_yuv_mb reads a full hsample x vsample block for every
+     * macroblock of a ceil(W/h) x ceil(H/v) grid, so odd dimensions would
+     * read (and encode) samples from beyond the plane. */
+    if (avctx->width % s->hsample[0] || avctx->height % s->vsample[0]) {
+        av_log(avctx, AV_LOG_ERROR, "Frame dimensions must be a multiple of "
+               "%dx%d for ljpeg.\n", s->hsample[0], s->vsample[0]);
+        return AVERROR(EINVAL);
+    }
+
     ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
                                  s->huff_code_dc_luminance,
                                  ff_mjpeg_bits_dc_luminance,
-- 
2.54.0

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

Reply via email to