Skip to content

Commit

Permalink
issue #28, fix buffer overflows and bad allocs on corrupt CAF files
Browse files Browse the repository at this point in the history
  • Loading branch information
dbry committed Feb 12, 2018
1 parent 36a24c7 commit 8e3fe45
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions cli/caff.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ typedef struct

#define CAFChannelDescriptionFormat "LLLLL"

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 };
static const char TMH_std [] = { 1,2,3,11,8,9,5,6,10,12,13,14,15,7,4,16 };
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 };
static const char TMH_std [] = { 1,2,3,11,8,9,5,6,10,12,13,14,15,7,4,16,0 };

static struct {
uint32_t mChannelLayoutTag; // Core Audio layout, 100 - 146 in high word, num channels in low word
Expand Down Expand Up @@ -274,10 +274,19 @@ int ParseCaffHeaderConfig (FILE *infile, char *infilename, char *fourcc, Wavpack
}
}
else if (!strncmp (caf_chunk_header.mChunkType, "chan", 4)) {
CAFChannelLayout *caf_channel_layout = malloc ((size_t) caf_chunk_header.mChunkSize);
CAFChannelLayout *caf_channel_layout;

if (caf_chunk_header.mChunkSize < sizeof (CAFChannelLayout) ||
!DoReadFile (infile, caf_channel_layout, (uint32_t) caf_chunk_header.mChunkSize, &bcount) ||
if (caf_chunk_header.mChunkSize < sizeof (CAFChannelLayout) || caf_chunk_header.mChunkSize > 1024) {
error_line ("this .CAF file has an invalid 'chan' chunk!");
return WAVPACK_SOFT_ERROR;
}

if (debug_logging_mode)
error_line ("'chan' chunk is %d bytes", (int) caf_chunk_header.mChunkSize);

caf_channel_layout = malloc ((size_t) caf_chunk_header.mChunkSize);

if (!DoReadFile (infile, caf_channel_layout, (uint32_t) caf_chunk_header.mChunkSize, &bcount) ||
bcount != caf_chunk_header.mChunkSize) {
error_line ("%s is not a valid .CAF file!", infilename);
free (caf_channel_layout);
Expand Down Expand Up @@ -495,8 +504,15 @@ int ParseCaffHeaderConfig (FILE *infile, char *infilename, char *fourcc, Wavpack
}
else { // just copy unknown chunks to output file

int bytes_to_copy = (uint32_t) caf_chunk_header.mChunkSize;
char *buff = malloc (bytes_to_copy);
uint32_t bytes_to_copy = (uint32_t) caf_chunk_header.mChunkSize;
char *buff;

if (caf_chunk_header.mChunkSize < 0 || caf_chunk_header.mChunkSize > 1048576) {
error_line ("%s is not a valid .CAF file!", infilename);
return WAVPACK_SOFT_ERROR;
}

buff = malloc (bytes_to_copy);

if (debug_logging_mode)
error_line ("extra unknown chunk \"%c%c%c%c\" of %d bytes",
Expand Down

1 comment on commit 8e3fe45

@dbry
Copy link
Owner Author

@dbry dbry commented on 8e3fe45 Feb 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually fixes issue #26 (mis-typed commit)

Please sign in to comment.