src/common.c: Fix heap buffer overflows when writing strings in binheader 30/162530/1
authorJörn Heusipp <osmanx@problemloesungsmaschine.de>
Wed, 14 Jun 2017 10:25:40 +0000 (12:25 +0200)
committerSeungbae Shin <seungbae.shin@samsung.com>
Thu, 30 Nov 2017 06:11:00 +0000 (15:11 +0900)
commit401a34510e5d399e3725c214100e2659f029d0eb
tree63fe9b86983133de92c46ed83bfccf4649c2828e
parentdca13c572ccc62e564b79850bfe7a95c4a7ec99f
src/common.c: Fix heap buffer overflows when writing strings in binheader

Fixes the following problems:
 1. Case 's' only enlarges the buffer by 16 bytes instead of size bytes.
 2. psf_binheader_writef() enlarges the header buffer (if needed) prior to the
    big switch statement by an amount (16 bytes) which is enough for all cases
    where only a single value gets added. Cases 's', 'S', 'p' however
    additionally write an arbitrary length block of data and again enlarge the
    buffer to the required amount. However, the required space calculation does
    not take into account the size of the length field which gets output before
    the data.
 3. Buffer size requirement calculation in case 'S' does not account for the
    padding byte ("size += (size & 1) ;" happens after the calculation which
    uses "size").
 4. Case 'S' can overrun the header buffer by 1 byte when no padding is
    involved
    ("memcpy (&(psf->header.ptr [psf->header.indx]), strptr, size + 1) ;" while
    the buffer is only guaranteed to have "size" space available).
 5. "psf->header.ptr [psf->header.indx] = 0 ;" in case 'S' always writes 1 byte
    beyond the space which is guaranteed to be allocated in the header buffer.
 6. Case 's' can overrun the provided source string by 1 byte if padding is
    involved ("memcpy (&(psf->header.ptr [psf->header.indx]), strptr, size) ;"
    where "size" is "strlen (strptr) + 1" (which includes the 0 terminator,
    plus optionally another 1 which is padding and not guaranteed to be
    readable via the source string pointer).

Change-Id: I3decd7a80a46b927b3cd2c975826b888ab5175e9
Closes: https://github.com/erikd/libsndfile/issues/292
src/common.c