PR #25932 opened by Kilian (lusoris)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25932
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25932.patch

This fixes an out-of-bounds read in the libx264 wrapper: with interlaced or 
fake-interlaced encoding, an odd macroblock row count, `mb_info=1` and video 
hint side data, x264 reads one row of `mb_info` past the map that 
`setup_mb_info()` allocates.

## Cause

`setup_mb_info()` allocates `mb_width * mb_height` entries, with `mb_height` 
the frame's own macroblock row count. x264 rounds its row count up to an even 
number when interlaced or fake-interlaced encoding is on (`x264_sps_init()` in 
`encoder/set.c` sets `i_mb_height` that way when `b_frame_mbs_only` is off) and 
reads `mb_info` over that grid, for example in `x264_frame_deblock_row()` 
(`common/deblock.c`). With an odd count it reads one row past the allocation.

Pull request 25909 fixes the same sizing for the ROI offset map in 
`setup_roi()`; its review pointed out this one.

## Change

`setup_mb_info()` now allocates the map for an even row count, as the review of 
25909 suggested. The fill keeps its extent, so it leaves the added row at 0, 
which x264 reads as not constant, the conservative choice.

## Testing

The reproducer is a small API program that encodes four 4:2:0 frames with 
libx264, `mb_info=1` and an `AV_VIDEO_HINT_TYPE_CHANGED` hint with no 
rectangles on each frame, and prints the MD5 of all packets. FFmpeg master 
08d6ed2100 and x264 0480cb05 are both built with AddressSanitizer.

- master: with `-x264-params interlaced=1` or `fake-interlaced=1` at 64x48 and 
48x80, and with `interlaced=1` and one 32x32 hint rectangle at 64x48, 
AddressSanitizer reports a heap-buffer-overflow read of 1 byte in 
`x264_frame_deblock_row()` (`common/deblock.c:580`), just past the map 
allocated in `setup_mb_info()`. 64x64, 96x32 and progressive encoding run clean.
- With this change, all 14 runs (three modes at four sizes, plus the rectangle 
case progressive and interlaced) exit 0 without an AddressSanitizer report.
- The MD5 is identical between master and this change for the nine runs whose 
grid does not round: progressive at all four sizes, both interlaced modes at 
64x64 and 96x32, and the progressive rectangle case.

This touches the same file as pull requests 25875 and 25909, in a different 
function; whichever lands later will be rebased.



>From 4d7de8b464458cd24f2029a6b05f15cc45c3a9d9 Mon Sep 17 00:00:00 2001
From: lusoris <[email protected]>
Date: Wed, 11 Nov 2026 03:42:22 +0100
Subject: [PATCH] avcodec/libx264: size the mb_info map for interlaced encoding

x264 rounds its macroblock row count up to an even number for
interlaced and fake-interlaced encoding and reads mb_info over that
grid. setup_mb_info() allocated the map for the frame's own row count,
so with an odd count x264 read one row past the allocation: at 64x48
with -x264-params interlaced=1 or fake-interlaced=1 and video hint
side data, AddressSanitizer reports a heap-buffer-overflow in
x264_frame_deblock_row().

Allocate the map for an even row count. The extra row stays 0, so x264
treats those macroblocks as not constant, and the output for sizes and
modes that do not round is unchanged.

Found in review of pull request 25909 (ROI offset map).

Signed-off-by: lusoris <[email protected]>
---
 libavcodec/libx264.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index a432c0b50e5..fa90e00232e 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -362,6 +362,10 @@ static int setup_mb_info(AVCodecContext *ctx, 
x264_picture_t *pic,
 {
     int mb_width = (frame->width + MB_SIZE - 1) / MB_SIZE;
     int mb_height = (frame->height + MB_SIZE - 1) / MB_SIZE;
+    /* x264 rounds the macroblock row count up to an even number for
+     * interlaced and fake-interlaced encoding and reads mb_info
+     * over that grid, so size the map for it. */
+    int mb_height_alloc = FFALIGN(mb_height, 2);
 
     const AVVideoRect *mbinfo_rects;
     int nb_rects;
@@ -370,7 +374,7 @@ static int setup_mb_info(AVCodecContext *ctx, 
x264_picture_t *pic,
     mbinfo_rects = (const AVVideoRect *)av_video_hint_rects(info);
     nb_rects = info->nb_rects;
 
-    mbinfo = av_calloc(mb_width * mb_height, sizeof(*mbinfo));
+    mbinfo = av_calloc(mb_width * mb_height_alloc, sizeof(*mbinfo));
     if (!mbinfo)
         return AVERROR(ENOMEM);
 
-- 
2.54.0

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

Reply via email to