Pārlūkot izejas kodu

wav: Clamp DATA chunk to size of file if possible.

Prevents a malicious file from malloc'ing multiple gigabytes.

Fixes #10052.
Ryan C. Gordon 2 nedēļas atpakaļ
vecāks
revīzija
44e4deab7c
1 mainītis faili ar 9 papildinājumiem un 0 dzēšanām
  1. 9 0
      src/audio/SDL_wave.c

+ 9 - 0
src/audio/SDL_wave.c

@@ -1775,6 +1775,7 @@ static bool WaveLoad(SDL_IOStream *src, WaveFile *file, SDL_AudioSpec *spec, Uin
     int result;
     Uint32 chunkcount = 0;
     Uint32 chunkcountlimit = 10000;
+    const Sint64 flen = SDL_GetIOSize(src);   // this might be -1 if the IOStream can't determine the total size.
     const char *hint;
     Sint64 RIFFstart, RIFFend, lastchunkpos;
     bool RIFFlengthknown = false;
@@ -1883,6 +1884,14 @@ static bool WaveLoad(SDL_IOStream *src, WaveFile *file, SDL_AudioSpec *spec, Uin
                 fmtchunk = *chunk;
             }
         } else if (chunk->fourcc == DATA) {
+            /* If the data chunk is bigger than the file, it might be corrupt
+               or the file is truncated. Try to recover by clamping the file
+               size. This also means a malicious file can't allocate 4 gigabytes
+               for the chunks without actually supplying a 4 gigabyte file. */
+            if ((flen > 0) && ((chunk->position + chunk->length) > flen)) {
+                chunk->length = flen - chunk->position;
+            }
+
             /* Only use the first data chunk. Handling the wavl list madness
              * may require a different approach.
              */