Blame SOURCES/wavpack-0001-issue-27-do-not-overwrite-stack-on-corrupt-RF64-file.patch

613f70
From: David Bryant <david@wavpack.com>
613f70
Date: Sun, 4 Feb 2018 11:28:15 -0800
613f70
Subject: [PATCH] issue #27, do not overwrite stack on corrupt RF64 file
613f70
613f70
613f70
diff --git a/cli/riff.c b/cli/riff.c
613f70
index 8b1af45..de98c1e 100644
613f70
--- a/cli/riff.c
613f70
+++ b/cli/riff.c
613f70
@@ -42,6 +42,7 @@ typedef struct {
613f70
 
613f70
 #pragma pack(pop)
613f70
 
613f70
+#define CS64ChunkFormat "4D"
613f70
 #define DS64ChunkFormat "DDDL"
613f70
 
613f70
 #define WAVPACK_NO_ERROR    0
613f70
@@ -101,13 +102,13 @@ int ParseRiffHeaderConfig (FILE *infile, char *infilename, char *fourcc, Wavpack
613f70
 
613f70
         if (!strncmp (chunk_header.ckID, "ds64", 4)) {
613f70
             if (chunk_header.ckSize < sizeof (DS64Chunk) ||
613f70
-                !DoReadFile (infile, &ds64_chunk, chunk_header.ckSize, &bcount) ||
613f70
-                bcount != chunk_header.ckSize) {
613f70
+                !DoReadFile (infile, &ds64_chunk, sizeof (DS64Chunk), &bcount) ||
613f70
+                bcount != sizeof (DS64Chunk)) {
613f70
                     error_line ("%s is not a valid .WAV file!", infilename);
613f70
                     return WAVPACK_SOFT_ERROR;
613f70
             }
613f70
             else if (!(config->qmode & QMODE_NO_STORE_WRAPPER) &&
613f70
-                !WavpackAddWrapper (wpc, &ds64_chunk, chunk_header.ckSize)) {
613f70
+                !WavpackAddWrapper (wpc, &ds64_chunk, sizeof (DS64Chunk))) {
613f70
                     error_line ("%s", WavpackGetErrorMessage (wpc));
613f70
                     return WAVPACK_SOFT_ERROR;
613f70
             }
613f70
@@ -315,10 +316,11 @@ int ParseRiffHeaderConfig (FILE *infile, char *infilename, char *fourcc, Wavpack
613f70
 
613f70
 int WriteRiffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples, int qmode)
613f70
 {
613f70
-    int do_rf64 = 0, write_junk = 1;
613f70
+    int do_rf64 = 0, write_junk = 1, table_length = 0;
613f70
     ChunkHeader ds64hdr, datahdr, fmthdr;
613f70
     RiffChunkHeader riffhdr;
613f70
     DS64Chunk ds64_chunk;
613f70
+    CS64Chunk cs64_chunk;
613f70
     JunkChunk junkchunk;
613f70
     WaveHeader wavhdr;
613f70
     uint32_t bcount;
613f70
@@ -380,6 +382,7 @@ int WriteRiffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,
613f70
     strncpy (riffhdr.formType, "WAVE", sizeof (riffhdr.formType));
613f70
     total_riff_bytes = sizeof (riffhdr) + wavhdrsize + sizeof (datahdr) + ((total_data_bytes + 1) & ~(int64_t)1);
613f70
     if (do_rf64) total_riff_bytes += sizeof (ds64hdr) + sizeof (ds64_chunk);
613f70
+    total_riff_bytes += table_length * sizeof (CS64Chunk);
613f70
     if (write_junk) total_riff_bytes += sizeof (junkchunk);
613f70
     strncpy (fmthdr.ckID, "fmt ", sizeof (fmthdr.ckID));
613f70
     strncpy (datahdr.ckID, "data", sizeof (datahdr.ckID));
613f70
@@ -394,11 +397,12 @@ int WriteRiffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,
613f70
 
613f70
     if (do_rf64) {
613f70
         strncpy (ds64hdr.ckID, "ds64", sizeof (ds64hdr.ckID));
613f70
-        ds64hdr.ckSize = sizeof (ds64_chunk);
613f70
+        ds64hdr.ckSize = sizeof (ds64_chunk) + (table_length * sizeof (CS64Chunk));
613f70
         CLEAR (ds64_chunk);
613f70
         ds64_chunk.riffSize64 = total_riff_bytes;
613f70
         ds64_chunk.dataSize64 = total_data_bytes;
613f70
         ds64_chunk.sampleCount64 = total_samples;
613f70
+        ds64_chunk.tableLength = table_length;
613f70
         riffhdr.ckSize = (uint32_t) -1;
613f70
         datahdr.ckSize = (uint32_t) -1;
613f70
         WavpackNativeToLittleEndian (&ds64hdr, ChunkHeaderFormat);
613f70
@@ -409,6 +413,14 @@ int WriteRiffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,
613f70
         datahdr.ckSize = (uint32_t) total_data_bytes;
613f70
     }
613f70
 
613f70
+    // this "table" is just a dummy placeholder for testing (normally not written)
613f70
+
613f70
+    if (table_length) {
613f70
+        strncpy (cs64_chunk.ckID, "dmmy", sizeof (cs64_chunk.ckID));
613f70
+        cs64_chunk.chunkSize64 = 12345678;
613f70
+        WavpackNativeToLittleEndian (&cs64_chunk, CS64ChunkFormat);
613f70
+    }
613f70
+
613f70
     // write the RIFF chunks up to just before the data starts
613f70
 
613f70
     WavpackNativeToLittleEndian (&riffhdr, ChunkHeaderFormat);
613f70
@@ -418,8 +430,21 @@ int WriteRiffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,
613f70
 
613f70
     if (!DoWriteFile (outfile, &riffhdr, sizeof (riffhdr), &bcount) || bcount != sizeof (riffhdr) ||
613f70
         (do_rf64 && (!DoWriteFile (outfile, &ds64hdr, sizeof (ds64hdr), &bcount) || bcount != sizeof (ds64hdr))) ||
613f70
-        (do_rf64 && (!DoWriteFile (outfile, &ds64_chunk, sizeof (ds64_chunk), &bcount) || bcount != sizeof (ds64_chunk))) ||
613f70
-        (write_junk && (!DoWriteFile (outfile, &junkchunk, sizeof (junkchunk), &bcount) || bcount != sizeof (junkchunk))) ||
613f70
+        (do_rf64 && (!DoWriteFile (outfile, &ds64_chunk, sizeof (ds64_chunk), &bcount) || bcount != sizeof (ds64_chunk)))) {
613f70
+            error_line ("can't write .WAV data, disk probably full!");
613f70
+            return FALSE;
613f70
+    }
613f70
+
613f70
+    // again, this is normally not written except for testing
613f70
+
613f70
+    while (table_length--)
613f70
+        if (!DoWriteFile (outfile, &cs64_chunk, sizeof (cs64_chunk), &bcount) || bcount != sizeof (cs64_chunk)) {
613f70
+            error_line ("can't write .WAV data, disk probably full!");
613f70
+            return FALSE;
613f70
+        }
613f70
+
613f70
+
613f70
+    if ((write_junk && (!DoWriteFile (outfile, &junkchunk, sizeof (junkchunk), &bcount) || bcount != sizeof (junkchunk))) ||
613f70
         !DoWriteFile (outfile, &fmthdr, sizeof (fmthdr), &bcount) || bcount != sizeof (fmthdr) ||
613f70
         !DoWriteFile (outfile, &wavhdr, wavhdrsize, &bcount) || bcount != wavhdrsize ||
613f70
         !DoWriteFile (outfile, &datahdr, sizeof (datahdr), &bcount) || bcount != sizeof (datahdr)) {