After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 724464 - mpegts: does not check data sizes when parsing descriptors
mpegts: does not check data sizes when parsing descriptors
Status: RESOLVED FIXED
Product: GStreamer
Classification: Platform
Component: gst-plugins-bad
git master
Other Linux
: Normal critical
: 1.3.1
Assigned To: GStreamer Maintainers
GStreamer Maintainers
Depends on:
Blocks:
 
 
Reported: 2014-02-16 09:20 UTC by Sebastian Dröge (slomo)
Modified: 2014-02-25 09:22 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
mpegts: Fix descriptor checks (11.84 KB, patch)
2014-02-24 14:54 UTC, Edward Hervey
reviewed Details | Review
mpegts: Fix descriptor checks (12.32 KB, patch)
2014-02-25 07:46 UTC, Edward Hervey
committed Details | Review

Description Sebastian Dröge (slomo) 2014-02-16 09:20:17 UTC
See summary, I noticed this while reviewing bug #724069. As these descriptors come from an untrusted source I would say that checking at least the size before parsing would make a lot of sense to prevent crashes and other interesting effects.
Comment 1 Edward Hervey 2014-02-20 11:10:16 UTC
There's even a FIXME in the code about it :)

/*
 * TODO
 *
 * * Add common validation code for data presence and minimum/maximum expected
 *   size.
Comment 2 Edward Hervey 2014-02-20 12:15:03 UTC
commit 142233d9171c6bcc93717bba48a11841c01da20c
Author: Edward Hervey <edward@collabora.com>
Date:   Thu Feb 20 19:08:33 2014 +0100

    mpegts: Add size guards for descriptors where neeeded
    
    Fixes https://bugzilla.gnome.org/show_bug.cgi?id=724464
Comment 3 Sebastian Dröge (slomo) 2014-02-20 19:29:04 UTC
I don't think g_return_val_if_fail() are good for this. It's for catching programming mistakes, not for catching invalid input streams.
Comment 4 Edward Hervey 2014-02-21 07:13:35 UTC
Whoops, good point. I'll change all those g_return_val_if_fail in descriptor parsing to regular return FALSE.
Comment 5 Edward Hervey 2014-02-21 07:16:43 UTC
Actually ... I'm wondering if all the following checks in descriptor parsing shouldn't be moved to regular returns:

* descriptor presence (desc != NULL)
* non-empty descriptor (desc->data != NULL)
* correct descriptor type (desc->-tag == <expected descriptor type>)
* descriptor length (desc->length ...)

Maybe the first one can be kept as a g_return_val_if_fail (it's an argument), but the others are all dependent on the data stream.
Comment 6 Edward Hervey 2014-02-21 07:22:29 UTC
I'm thinking about something like this, which would reduce code, yet still checks for API arguments.

(gstmpegts-private.h)
#define __common_desc_checks(desc, tagtype, minlen)			\
  ((desc)->data != NULL && (desc)->tag == (tagtype) && (desc)->length >= (minlen))
#define __common_desc_checks_exact(desc, tagtype, len)			\
  ((desc)->data != NULL && (desc)->tag == (tagtype) && (desc)->length == (len))

(gstmpegtsdescriptor.c)
gboolean
gst_mpegts_descriptor_parse_iso_639_language (const GstMpegTsDescriptor *
    descriptor, GstMpegTsISO639LanguageDescriptor * res)
...
  g_return_val_if_fail (descriptor != NULL && res != NULL, FALSE);
  if (!__common_desc_checks(descriptor, 0x0A, 0))
    return FALSE;
Comment 7 Edward Hervey 2014-02-24 14:54:47 UTC
Created attachment 270136 [details] [review]
mpegts: Fix descriptor checks

Only use g_return_val_if_fail on provided direct arguments. The others get
checked all the time.
Comment 8 Sebastian Dröge (slomo) 2014-02-24 15:30:16 UTC
Review of attachment 270136 [details] [review]:

::: gst-libs/gst/mpegts/gstmpegts-private.h
@@ +52,3 @@
+  ((desc)->data != NULL && (desc)->tag == (tagtype) && (desc)->length >= (minlen))
+#define __common_desc_checks_exact(desc, tagtype, len)			\
+  ((desc)->data != NULL && (desc)->tag == (tagtype) && (desc)->length == (len))

I think these should be logged with GST_WARNING() or something
Comment 9 Edward Hervey 2014-02-25 07:08:44 UTC
Good point.

And I'll also add a 'return value' argument to the macro to avoid having to constantly repeat the if(!..) return retval; in the calling code. I'll rename the macro acccordingly.
Comment 10 Edward Hervey 2014-02-25 07:46:12 UTC
Created attachment 270223 [details] [review]
mpegts: Fix descriptor checks

Only use g_return_val_if_fail on provided direct arguments. The others get
checked all the time.
Comment 11 Sebastian Dröge (slomo) 2014-02-25 09:07:28 UTC
Comment on attachment 270223 [details] [review]
mpegts: Fix descriptor checks

Looks good :)
Comment 12 Edward Hervey 2014-02-25 09:22:01 UTC
commit 63145f4576ef9e910f8a4cbf9317c255a2729bca
Author: Edward Hervey <bilboed@bilboed.com>
Date:   Mon Feb 24 15:52:53 2014 +0100

    mpegts: Fix descriptor checks
    
    Only use g_return_val_if_fail on provided direct arguments. The others get
    checked all the time.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=724464