File CVE-2024-47541.patch of Package gstreamer-plugins-base.36920

From 15bb318416e1bf6b6b557006a37d1da86c3a76a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= <sebastian@centricular.com>
Date: Mon, 30 Sep 2024 21:40:44 +0300
Subject: [PATCH 1/2] ssaparse: Search for closing brace after opening brace

Otherwise removing anything between the braces leads to out of bound writes if
there is a closing brace before the first opening brace.

Thanks to Antonio Morales for finding and reporting the issue.

Fixes GHSL-2024-228
Fixes https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/3870

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/8036>
---
diff -urp gst-plugins-base-1.8.3.orig/gst/subparse/gstssaparse.c gst-plugins-base-1.8.3/gst/subparse/gstssaparse.c
--- gst-plugins-base-1.8.3.orig/gst/subparse/gstssaparse.c	2016-03-24 06:36:33.000000000 -0500
+++ gst-plugins-base-1.8.3/gst/subparse/gstssaparse.c	2025-01-06 15:45:28.095779129 -0600
@@ -140,6 +140,35 @@ gst_ssa_parse_sink_event (GstPad * pad,
   return res;
 }
 
+#ifndef HAVE_MEMMEM
+// memmem() is a GNU extension so if it's not available we'll need
+// our own implementation here. Thanks C.
+static void *
+my_memmem (const void *haystack, size_t haystacklen, const void *needle,
+    size_t needlelen)
+{
+  const guint8 *cur, *end;
+
+  if (needlelen > haystacklen)
+    return NULL;
+  if (needlelen == 0)
+    return (void *) haystack;
+
+
+  cur = haystack;
+  end = cur + haystacklen - needlelen;
+
+  for (; cur <= end; cur++) {
+    if (memcmp (cur, needle, needlelen) == 0)
+      return (void *) cur;
+  }
+
+  return NULL;
+}
+#else
+#define my_memmem memmem
+#endif
+
 static gboolean
 gst_ssa_parse_setcaps (GstPad * sinkpad, GstCaps * caps)
 {
@@ -148,6 +177,7 @@ gst_ssa_parse_setcaps (GstPad * sinkpad,
   const GValue *val;
   GstStructure *s;
   const guchar bom_utf8[] = { 0xEF, 0xBB, 0xBF };
+  const guint8 header[] = "[Script Info]";
   const gchar *end;
   GstBuffer *priv;
   GstMapInfo map;
@@ -185,7 +215,7 @@ gst_ssa_parse_setcaps (GstPad * sinkpad,
     left -= 3;
   }
 
-  if (!strstr (ptr, "[Script Info]"))
+  if (!my_memmem (ptr, left, header, sizeof (header) - 1))
     goto invalid_init;
 
   if (!g_utf8_validate (ptr, left, &end)) {
@@ -221,6 +251,10 @@ invalid_init:
   }
 }
 
+#ifdef my_memmem
+#undef my_memmem
+#endif
+
 static gboolean
 gst_ssa_parse_remove_override_codes (GstSsaParse * parse, gchar * txt)
 {
@@ -228,7 +262,7 @@ gst_ssa_parse_remove_override_codes (Gst
   gboolean removed_any = FALSE;
 
   while ((t = strchr (txt, '{'))) {
-    end = strchr (txt, '}');
+    end = strchr (t, '}');
     if (end == NULL) {
       GST_WARNING_OBJECT (parse, "Missing { for style override code");
       return removed_any;
openSUSE Build Service is sponsored by