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


>From 2f53b5dbd6906761dce9f5c1c8005b2e4f6affd5 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 11 Nov 2026 22:41:02 +0100
Subject: [PATCH 1/3] avformat/rtpenc: check the sample size and channel count
 in rtp_send_samples()

A G.726 stream with 0 bits per sample or a PCM stream with 0 channels
divided by zero. A channel count of 1 << 28 overflowed the sample size.

Fixes: division by zero
Fixes: signed integer overflow
Fixes: dRzvfoUcV9EZ

Found during the review of https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25222

Division by zero Replicated through UnModified FFmpeg with ASAN and UBSAN
Signed integer overflow Replicated through UnModified FFmpeg with UBSAN
---
 libavformat/rtpenc.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c
index 823e5e2cf23..f462f7826f5 100644
--- a/libavformat/rtpenc.c
+++ b/libavformat/rtpenc.c
@@ -426,10 +426,19 @@ void ff_rtp_send_data(AVFormatContext *s1, const uint8_t 
*buf1, int len, int m)
 /* send an integer number of samples and compute time stamp and fill
    the rtp send buffer before sending. */
 static int rtp_send_samples(AVFormatContext *s1,
-                            const uint8_t *buf1, int size, int 
sample_size_bits)
+                            const uint8_t *buf1, int size, int bits_per_sample)
 {
     RTPMuxContext *s = s1->priv_data;
+    int nb_channels = s1->streams[0]->codecpar->ch_layout.nb_channels;
     int len, max_packet_size, n;
+
+    if (bits_per_sample <= 0 || nb_channels <= 0 ||
+        nb_channels > INT_MAX / bits_per_sample) {
+        av_log(s1, AV_LOG_ERROR, "Invalid sample size %d or channel count 
%d\n",
+               bits_per_sample, nb_channels);
+        return AVERROR(EINVAL);
+    }
+    int sample_size_bits = bits_per_sample * nb_channels;
     /* Calculate the number of bytes to get samples aligned on a byte border */
     int aligned_samples_size = sample_size_bits/av_gcd(sample_size_bits, 8);
 
@@ -617,24 +626,23 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket 
*pkt)
     case AV_CODEC_ID_PCM_ALAW:
     case AV_CODEC_ID_PCM_U8:
     case AV_CODEC_ID_PCM_S8:
-        return rtp_send_samples(s1, pkt->data, size, 8 * 
st->codecpar->ch_layout.nb_channels);
+        return rtp_send_samples(s1, pkt->data, size, 8);
     case AV_CODEC_ID_PCM_U16BE:
     case AV_CODEC_ID_PCM_U16LE:
     case AV_CODEC_ID_PCM_S16BE:
     case AV_CODEC_ID_PCM_S16LE:
-        return rtp_send_samples(s1, pkt->data, size, 16 * 
st->codecpar->ch_layout.nb_channels);
+        return rtp_send_samples(s1, pkt->data, size, 16);
     case AV_CODEC_ID_PCM_S24BE:
-        return rtp_send_samples(s1, pkt->data, size, 24 * 
st->codecpar->ch_layout.nb_channels);
+        return rtp_send_samples(s1, pkt->data, size, 24);
     case AV_CODEC_ID_ADPCM_G722:
         /* The actual sample size is half a byte per sample, but since the
          * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
          * the correct parameter for send_samples_bits is 8 bits per stream
          * clock. */
-        return rtp_send_samples(s1, pkt->data, size, 8 * 
st->codecpar->ch_layout.nb_channels);
+        return rtp_send_samples(s1, pkt->data, size, 8);
     case AV_CODEC_ID_ADPCM_G726:
     case AV_CODEC_ID_ADPCM_G726LE:
-        return rtp_send_samples(s1, pkt->data, size,
-                                st->codecpar->bits_per_coded_sample * 
st->codecpar->ch_layout.nb_channels);
+        return rtp_send_samples(s1, pkt->data, size, 
st->codecpar->bits_per_coded_sample);
     case AV_CODEC_ID_MP2:
     case AV_CODEC_ID_MP3:
         rtp_send_mpegaudio(s1, pkt->data, size);
-- 
2.54.0


>From 7571c774b9ad261705de0531ad404ef67eb26f41 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 11 Nov 2026 22:45:24 +0100
Subject: [PATCH 2/3] avformat/rtmpdh: do not pass NULL to mpz_clear()

Fixes: NULL pointer dereference
Fixes: nFFdUE2kqlXX

Found during the review of https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25059

NULL pointer dereference Replicated through Modified FFmpeg with ASAN
---
 libavformat/rtmpdh.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/libavformat/rtmpdh.c b/libavformat/rtmpdh.c
index fb7aaf07c0e..1ed1a52c49d 100644
--- a/libavformat/rtmpdh.c
+++ b/libavformat/rtmpdh.c
@@ -66,10 +66,11 @@
         if (bn)                         \
             mpz_init2(bn, 1);           \
     } while (0)
-#define bn_free(bn)     \
-    do {                \
-        mpz_clear(bn);  \
-        av_free(bn);    \
+#define bn_free(bn)         \
+    do {                    \
+        if (bn)             \
+            mpz_clear(bn);  \
+        av_free(bn);        \
     } while (0)
 #define bn_set_word(bn, w)          mpz_set_ui(bn, w)
 #define bn_cmp(a, b)                mpz_cmp(a, b)
-- 
2.54.0


>From 94d53aca8ecc2cc3b9d5b37559e09a777e357d17 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 11 Nov 2026 22:45:24 +0100
Subject: [PATCH 3/3] avformat/rtmpdh: leave freeing the private key to
 ff_dh_free()

Fixes: double free
Fixes: QqzZphPWboPU

Found during the review of https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25059

Double free Replicated through Modified FFmpeg with ASAN
---
 libavformat/rtmpdh.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/libavformat/rtmpdh.c b/libavformat/rtmpdh.c
index 1ed1a52c49d..f5ec9e15e36 100644
--- a/libavformat/rtmpdh.c
+++ b/libavformat/rtmpdh.c
@@ -238,10 +238,8 @@ static FFBigNum dh_generate_key(FF_DH *dh)
     bn_random(dh->priv_key, 8 * num_bytes);
 
     bn_new(dh->pub_key);
-    if (!dh->pub_key) {
-        bn_free(dh->priv_key);
+    if (!dh->pub_key)
         return NULL;
-    }
 
     if (bn_modexp(dh->pub_key, dh->g, dh->priv_key, dh->p) < 0)
         return NULL;
-- 
2.54.0

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

Reply via email to