0010-src-common-c-Fix-heap-buffer-overflows-when-writing-strings-in.patch 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. From cf7a8182c2642c50f1cf90dddea9ce96a8bad2e8 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?J=C3=B6rn=20Heusipp?= <osmanx@problemloesungsmaschine.de>
  3. Date: Wed, 14 Jun 2017 12:25:40 +0200
  4. Subject: [PATCH] src/common.c: Fix heap buffer overflows when writing strings
  5. in binheader
  6. Fixes the following problems:
  7. 1. Case 's' only enlarges the buffer by 16 bytes instead of size bytes.
  8. 2. psf_binheader_writef() enlarges the header buffer (if needed) prior to the
  9. big switch statement by an amount (16 bytes) which is enough for all cases
  10. where only a single value gets added. Cases 's', 'S', 'p' however
  11. additionally write an arbitrary length block of data and again enlarge the
  12. buffer to the required amount. However, the required space calculation does
  13. not take into account the size of the length field which gets output before
  14. the data.
  15. 3. Buffer size requirement calculation in case 'S' does not account for the
  16. padding byte ("size += (size & 1) ;" happens after the calculation which
  17. uses "size").
  18. 4. Case 'S' can overrun the header buffer by 1 byte when no padding is
  19. involved
  20. ("memcpy (&(psf->header.ptr [psf->header.indx]), strptr, size + 1) ;" while
  21. the buffer is only guaranteed to have "size" space available).
  22. 5. "psf->header.ptr [psf->header.indx] = 0 ;" in case 'S' always writes 1 byte
  23. beyond the space which is guaranteed to be allocated in the header buffer.
  24. 6. Case 's' can overrun the provided source string by 1 byte if padding is
  25. involved ("memcpy (&(psf->header.ptr [psf->header.indx]), strptr, size) ;"
  26. where "size" is "strlen (strptr) + 1" (which includes the 0 terminator,
  27. plus optionally another 1 which is padding and not guaranteed to be
  28. readable via the source string pointer).
  29. Closes: https://github.com/erikd/libsndfile/issues/292
  30. Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  31. [Retrieved from:
  32. https://github.com/erikd/libsndfile/commit/cf7a8182c2642c50f1cf90dddea9ce96a8bad2e8]
  33. ---
  34. src/common.c | 15 +++++++--------
  35. 1 file changed, 7 insertions(+), 8 deletions(-)
  36. diff --git a/src/common.c b/src/common.c
  37. index 1a6204ca..6b2a2ee9 100644
  38. --- a/src/common.c
  39. +++ b/src/common.c
  40. @@ -681,16 +681,16 @@ psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...)
  41. /* Write a C string (guaranteed to have a zero terminator). */
  42. strptr = va_arg (argptr, char *) ;
  43. size = strlen (strptr) + 1 ;
  44. - size += (size & 1) ;
  45. - if (psf->header.indx + (sf_count_t) size >= psf->header.len && psf_bump_header_allocation (psf, 16))
  46. + if (psf->header.indx + 4 + (sf_count_t) size + (sf_count_t) (size & 1) > psf->header.len && psf_bump_header_allocation (psf, 4 + size + (size & 1)))
  47. return count ;
  48. if (psf->rwf_endian == SF_ENDIAN_BIG)
  49. - header_put_be_int (psf, size) ;
  50. + header_put_be_int (psf, size + (size & 1)) ;
  51. else
  52. - header_put_le_int (psf, size) ;
  53. + header_put_le_int (psf, size + (size & 1)) ;
  54. memcpy (&(psf->header.ptr [psf->header.indx]), strptr, size) ;
  55. + size += (size & 1) ;
  56. psf->header.indx += size ;
  57. psf->header.ptr [psf->header.indx - 1] = 0 ;
  58. count += 4 + size ;
  59. @@ -703,16 +703,15 @@ psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...)
  60. */
  61. strptr = va_arg (argptr, char *) ;
  62. size = strlen (strptr) ;
  63. - if (psf->header.indx + (sf_count_t) size > psf->header.len && psf_bump_header_allocation (psf, size))
  64. + if (psf->header.indx + 4 + (sf_count_t) size + (sf_count_t) (size & 1) > psf->header.len && psf_bump_header_allocation (psf, 4 + size + (size & 1)))
  65. return count ;
  66. if (psf->rwf_endian == SF_ENDIAN_BIG)
  67. header_put_be_int (psf, size) ;
  68. else
  69. header_put_le_int (psf, size) ;
  70. - memcpy (&(psf->header.ptr [psf->header.indx]), strptr, size + 1) ;
  71. + memcpy (&(psf->header.ptr [psf->header.indx]), strptr, size + (size & 1)) ;
  72. size += (size & 1) ;
  73. psf->header.indx += size ;
  74. - psf->header.ptr [psf->header.indx] = 0 ;
  75. count += 4 + size ;
  76. break ;
  77. @@ -724,7 +723,7 @@ psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...)
  78. size = (size & 1) ? size : size + 1 ;
  79. size = (size > 254) ? 254 : size ;
  80. - if (psf->header.indx + (sf_count_t) size > psf->header.len && psf_bump_header_allocation (psf, size))
  81. + if (psf->header.indx + 1 + (sf_count_t) size > psf->header.len && psf_bump_header_allocation (psf, 1 + size))
  82. return count ;
  83. header_put_byte (psf, size) ;