PR #26053 opened by Marti2203
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26053
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26053.patch
For formats with more than 8 bits, the histograms and the contrast change table
have `1 << depth` entries, but `compute_histogram16()` and
`apply_contrast_change16()` index them with the raw 16-bit sample. A sample
with bits set above the depth reads and writes past the 512-entry tables:
ffmpeg -filter_complex
"nullsrc=s=64x32:r=5:d=2,format=gray9,geq=lum='if(lt(X,8),X*7+Y,65535-Y)',tmidequalizer"
-f null -
gives an ASan heap-buffer-overflow in `compute_histogram16()`.
The patch clips the sample to the last entry before using it as an index, in
both places, with an `unsigned` bound as suggested in the review of #25902;
`apply_contrast_change()` now receives the table size. In-depth input is not
affected: the same graph with in-range samples gives identical framecrc output
before and after for gray9–gray16, and the out-of-depth input now runs without
an ASan report.
This is the `tmidequalizer` case pointed out by Forgejo_Fairy in the review of
#25902.
From 511499f93165e230008bbcec66d7367f37f09ff6 Mon Sep 17 00:00:00 2001
From: Martin Mirchev <[email protected]>
Date: Sat, 14 Nov 2026 14:11:55 +0800
Subject: [PATCH] avfilter/vf_tmidequalizer: clip out-of-depth samples to the
histogram
For formats with more than 8 bits, the histograms and the contrast
change table have 1 << depth entries, but compute_histogram16() and
apply_contrast_change16() index them with the raw 16-bit sample. A
sample with bits set above the depth, e.g. from a gray9le rawvideo
input with arbitrary words, reads and writes past the 512-entry tables.
Clip the sample to the last entry before using it as an index, as
vf_midequalizer does.
Signed-off-by: Martin Mirchev <[email protected]>
---
libavfilter/vf_tmidequalizer.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/libavfilter/vf_tmidequalizer.c b/libavfilter/vf_tmidequalizer.c
index 102d7c244f0..b85535bb264 100644
--- a/libavfilter/vf_tmidequalizer.c
+++ b/libavfilter/vf_tmidequalizer.c
@@ -52,7 +52,8 @@ typedef struct TMidEqualizerContext {
int w, int h, float *histogram, size_t hsize);
void (*apply_contrast_change)(const uint8_t *src, ptrdiff_t src_linesize,
uint8_t *dst, ptrdiff_t dst_linesize,
- int w, int h, float *change, float *orig);
+ int w, int h, float *change, float *orig,
+ size_t hsize);
} TMidEqualizerContext;
#define OFFSET(x) offsetof(TMidEqualizerContext, x)
@@ -135,7 +136,8 @@ static void compute_contrast_function(const float *const
histograms[256],
static void apply_contrast_change8(const uint8_t *src, ptrdiff_t src_linesize,
uint8_t *dst, ptrdiff_t dst_linesize,
- int w, int h, float *change, float *orig)
+ int w, int h, float *change, float *orig,
+ size_t hsize)
{
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++)
@@ -148,14 +150,15 @@ static void apply_contrast_change8(const uint8_t *src,
ptrdiff_t src_linesize,
static void apply_contrast_change16(const uint8_t *ssrc, ptrdiff_t
src_linesize,
uint8_t *ddst, ptrdiff_t dst_linesize,
- int w, int h, float *change, float *orig)
+ int w, int h, float *change, float *orig,
+ size_t hsize)
{
const uint16_t *src = (const uint16_t *)ssrc;
uint16_t *dst = (uint16_t *)ddst;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++)
- dst[x] = lrintf(change[src[x]]);
+ dst[x] = lrintf(change[FFMIN(src[x], (unsigned)hsize - 1)]);
dst += dst_linesize / 2;
src += src_linesize / 2;
@@ -271,7 +274,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
s->apply_contrast_change(in->data[p], in->linesize[p],
out->data[p], out->linesize[p],
s->plane_width[p], s->plane_height[p],
- s->change[p], s->histogram[p][idx]);
+ s->change[p], s->histogram[p][idx],
+ s->histogram_size);
}
}
@@ -309,7 +313,7 @@ static void compute_histogram16(const uint8_t *ssrc,
ptrdiff_t linesize,
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++)
- histogram[src[x]] += 1;
+ histogram[FFMIN(src[x], (unsigned)hsize - 1)] += 1;
src += linesize / 2;
}
--
2.54.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]