Blame SOURCES/wavpack-0003-issue-28-fix-buffer-overflows-and-bad-allocs-on-corr.patch

613f70
From: David Bryant <david@wavpack.com>
613f70
Date: Sun, 11 Feb 2018 16:37:47 -0800
613f70
Subject: [PATCH] issue #28, fix buffer overflows and bad allocs on corrupt CAF
613f70
 files
613f70
613f70
613f70
diff --git a/cli/caff.c b/cli/caff.c
613f70
index ae57c4b..6248a71 100644
613f70
--- a/cli/caff.c
613f70
+++ b/cli/caff.c
613f70
@@ -89,8 +89,8 @@ typedef struct
613f70
 
613f70
 #define CAFChannelDescriptionFormat "LLLLL"
613f70
 
613f70
-static const char TMH_full [] = { 1,2,3,13,9,10,5,6,12,14,15,16,17,9,4,18,7,8,19,20,21 };
613f70
-static const char TMH_std [] = { 1,2,3,11,8,9,5,6,10,12,13,14,15,7,4,16 };
613f70
+static const char TMH_full [] = { 1,2,3,13,9,10,5,6,12,14,15,16,17,9,4,18,7,8,19,20,21,0 };
613f70
+static const char TMH_std [] = { 1,2,3,11,8,9,5,6,10,12,13,14,15,7,4,16,0 };
613f70
 
613f70
 static struct {
613f70
     uint32_t mChannelLayoutTag;     // Core Audio layout, 100 - 146 in high word, num channels in low word
613f70
@@ -274,10 +274,19 @@ int ParseCaffHeaderConfig (FILE *infile, char *infilename, char *fourcc, Wavpack
613f70
             }
613f70
         }
613f70
         else if (!strncmp (caf_chunk_header.mChunkType, "chan", 4)) {
613f70
-            CAFChannelLayout *caf_channel_layout = malloc ((size_t) caf_chunk_header.mChunkSize);
613f70
+            CAFChannelLayout *caf_channel_layout;
613f70
 
613f70
-            if (caf_chunk_header.mChunkSize < sizeof (CAFChannelLayout) ||
613f70
-                !DoReadFile (infile, caf_channel_layout, (uint32_t) caf_chunk_header.mChunkSize, &bcount) ||
613f70
+            if (caf_chunk_header.mChunkSize < sizeof (CAFChannelLayout) || caf_chunk_header.mChunkSize > 1024) {
613f70
+                error_line ("this .CAF file has an invalid 'chan' chunk!");
613f70
+                return WAVPACK_SOFT_ERROR;
613f70
+            }
613f70
+
613f70
+            if (debug_logging_mode)
613f70
+                error_line ("'chan' chunk is %d bytes", (int) caf_chunk_header.mChunkSize);
613f70
+
613f70
+            caf_channel_layout = malloc ((size_t) caf_chunk_header.mChunkSize);
613f70
+
613f70
+            if (!DoReadFile (infile, caf_channel_layout, (uint32_t) caf_chunk_header.mChunkSize, &bcount) ||
613f70
                 bcount != caf_chunk_header.mChunkSize) {
613f70
                     error_line ("%s is not a valid .CAF file!", infilename);
613f70
                     free (caf_channel_layout);
613f70
@@ -495,8 +504,15 @@ int ParseCaffHeaderConfig (FILE *infile, char *infilename, char *fourcc, Wavpack
613f70
         }
613f70
         else {          // just copy unknown chunks to output file
613f70
 
613f70
-            int bytes_to_copy = (uint32_t) caf_chunk_header.mChunkSize;
613f70
-            char *buff = malloc (bytes_to_copy);
613f70
+            uint32_t bytes_to_copy = (uint32_t) caf_chunk_header.mChunkSize;
613f70
+            char *buff;
613f70
+
613f70
+            if (caf_chunk_header.mChunkSize < 0 || caf_chunk_header.mChunkSize > 1048576) {
613f70
+                error_line ("%s is not a valid .CAF file!", infilename);
613f70
+                return WAVPACK_SOFT_ERROR;
613f70
+            }
613f70
+
613f70
+            buff = malloc (bytes_to_copy);
613f70
 
613f70
             if (debug_logging_mode)
613f70
                 error_line ("extra unknown chunk \"%c%c%c%c\" of %d bytes",