2ab6fb
From bcfe7befea53869e7836be912ee7efe875877169 Mon Sep 17 00:00:00 2001
2ab6fb
From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= <sebastian@centricular.com>
2ab6fb
Date: Wed, 18 May 2022 12:00:48 +0300
2ab6fb
Subject: [PATCH 1/4] avidemux: Fix integer overflow resulting in heap
2ab6fb
 corruption in DIB buffer inversion code
2ab6fb
2ab6fb
Check that width*bpp/8 doesn't overflow a guint and also that
2ab6fb
height*stride fits into the provided buffer without overflowing.
2ab6fb
2ab6fb
Thanks to Adam Doupe for analyzing and reporting the issue.
2ab6fb
2ab6fb
CVE: CVE-2022-1921
2ab6fb
2ab6fb
See https://gstreamer.freedesktop.org/security/sa-2022-0001.html
2ab6fb
2ab6fb
Fixes https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/1224
2ab6fb
2ab6fb
Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/2608>
2ab6fb
---
2ab6fb
 gst/avi/gstavidemux.c | 17 ++++++++++++++---
2ab6fb
 1 file changed, 14 insertions(+), 3 deletions(-)
2ab6fb
2ab6fb
diff --git a/gst/avi/gstavidemux.c b/gst/avi/gstavidemux.c
2ab6fb
index 25c97da03e..1c87c668d0 100644
2ab6fb
--- a/gst/avi/gstavidemux.c
2ab6fb
+++ b/gst/avi/gstavidemux.c
2ab6fb
@@ -4971,8 +4971,8 @@ swap_line (guint8 * d1, guint8 * d2, guint8 * tmp, gint bytes)
2ab6fb
 static GstBuffer *
2ab6fb
 gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
2ab6fb
 {
2ab6fb
-  gint y, w, h;
2ab6fb
-  gint bpp, stride;
2ab6fb
+  guint y, w, h;
2ab6fb
+  guint bpp, stride;
2ab6fb
   guint8 *tmp = NULL;
2ab6fb
   GstMapInfo map;
2ab6fb
   guint32 fourcc;
2ab6fb
@@ -4999,12 +4999,23 @@ gst_avi_demux_invert (GstAviStream * stream, GstBuffer * buf)
2ab6fb
   h = stream->strf.vids->height;
2ab6fb
   w = stream->strf.vids->width;
2ab6fb
   bpp = stream->strf.vids->bit_cnt ? stream->strf.vids->bit_cnt : 8;
2ab6fb
+
2ab6fb
+  if ((guint64) w * ((guint64) bpp / 8) > G_MAXUINT - 4) {
2ab6fb
+    GST_WARNING ("Width x stride overflows");
2ab6fb
+    return buf;
2ab6fb
+  }
2ab6fb
+
2ab6fb
+  if (w == 0 || h == 0) {
2ab6fb
+    GST_WARNING ("Zero width or height");
2ab6fb
+    return buf;
2ab6fb
+  }
2ab6fb
+
2ab6fb
   stride = GST_ROUND_UP_4 (w * (bpp / 8));
2ab6fb
 
2ab6fb
   buf = gst_buffer_make_writable (buf);
2ab6fb
 
2ab6fb
   gst_buffer_map (buf, &map, GST_MAP_READWRITE);
2ab6fb
-  if (map.size < (stride * h)) {
2ab6fb
+  if (map.size < ((guint64) stride * (guint64) h)) {
2ab6fb
     GST_WARNING ("Buffer is smaller than reported Width x Height x Depth");
2ab6fb
     gst_buffer_unmap (buf, &map);
2ab6fb
     return buf;
2ab6fb
-- 
2ab6fb
2.38.1
2ab6fb