libmad-frame_length.diff 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ; You can calculate where the next frame will start depending on things
  2. ; like the bitrate. See mad_header_decode(). It seems that when decoding
  3. ; the frame you can go past that boundary. This attempts to catch those cases,
  4. ; but might not catch all of them.
  5. ; For more info see http://bugs.debian.org/508133
  6. Index: libmad-0.15.1b/layer12.c
  7. ===================================================================
  8. --- libmad-0.15.1b.orig/layer12.c 2008-12-23 21:38:07.000000000 +0100
  9. +++ libmad-0.15.1b/layer12.c 2008-12-23 21:38:12.000000000 +0100
  10. @@ -134,6 +134,12 @@
  11. for (sb = 0; sb < bound; ++sb) {
  12. for (ch = 0; ch < nch; ++ch) {
  13. nb = mad_bit_read(&stream->ptr, 4);
  14. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  15. + {
  16. + stream->error = MAD_ERROR_LOSTSYNC;
  17. + stream->sync = 0;
  18. + return -1;
  19. + }
  20. if (nb == 15) {
  21. stream->error = MAD_ERROR_BADBITALLOC;
  22. @@ -146,6 +152,12 @@
  23. for (sb = bound; sb < 32; ++sb) {
  24. nb = mad_bit_read(&stream->ptr, 4);
  25. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  26. + {
  27. + stream->error = MAD_ERROR_LOSTSYNC;
  28. + stream->sync = 0;
  29. + return -1;
  30. + }
  31. if (nb == 15) {
  32. stream->error = MAD_ERROR_BADBITALLOC;
  33. @@ -162,6 +174,12 @@
  34. for (ch = 0; ch < nch; ++ch) {
  35. if (allocation[ch][sb]) {
  36. scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6);
  37. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  38. + {
  39. + stream->error = MAD_ERROR_LOSTSYNC;
  40. + stream->sync = 0;
  41. + return -1;
  42. + }
  43. # if defined(OPT_STRICT)
  44. /*
  45. @@ -187,6 +205,12 @@
  46. frame->sbsample[ch][s][sb] = nb ?
  47. mad_f_mul(I_sample(&stream->ptr, nb),
  48. sf_table[scalefactor[ch][sb]]) : 0;
  49. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  50. + {
  51. + stream->error = MAD_ERROR_LOSTSYNC;
  52. + stream->sync = 0;
  53. + return -1;
  54. + }
  55. }
  56. }
  57. @@ -195,6 +219,12 @@
  58. mad_fixed_t sample;
  59. sample = I_sample(&stream->ptr, nb);
  60. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  61. + {
  62. + stream->error = MAD_ERROR_LOSTSYNC;
  63. + stream->sync = 0;
  64. + return -1;
  65. + }
  66. for (ch = 0; ch < nch; ++ch) {
  67. frame->sbsample[ch][s][sb] =
  68. @@ -403,7 +433,15 @@
  69. nbal = bitalloc_table[offsets[sb]].nbal;
  70. for (ch = 0; ch < nch; ++ch)
  71. + {
  72. allocation[ch][sb] = mad_bit_read(&stream->ptr, nbal);
  73. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  74. + {
  75. + stream->error = MAD_ERROR_LOSTSYNC;
  76. + stream->sync = 0;
  77. + return -1;
  78. + }
  79. + }
  80. }
  81. for (sb = bound; sb < sblimit; ++sb) {
  82. @@ -411,6 +449,13 @@
  83. allocation[0][sb] =
  84. allocation[1][sb] = mad_bit_read(&stream->ptr, nbal);
  85. +
  86. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  87. + {
  88. + stream->error = MAD_ERROR_LOSTSYNC;
  89. + stream->sync = 0;
  90. + return -1;
  91. + }
  92. }
  93. /* decode scalefactor selection info */
  94. @@ -419,6 +464,12 @@
  95. for (ch = 0; ch < nch; ++ch) {
  96. if (allocation[ch][sb])
  97. scfsi[ch][sb] = mad_bit_read(&stream->ptr, 2);
  98. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  99. + {
  100. + stream->error = MAD_ERROR_LOSTSYNC;
  101. + stream->sync = 0;
  102. + return -1;
  103. + }
  104. }
  105. }
  106. @@ -442,6 +493,12 @@
  107. for (ch = 0; ch < nch; ++ch) {
  108. if (allocation[ch][sb]) {
  109. scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6);
  110. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  111. + {
  112. + stream->error = MAD_ERROR_LOSTSYNC;
  113. + stream->sync = 0;
  114. + return -1;
  115. + }
  116. switch (scfsi[ch][sb]) {
  117. case 2:
  118. @@ -452,11 +509,23 @@
  119. case 0:
  120. scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6);
  121. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  122. + {
  123. + stream->error = MAD_ERROR_LOSTSYNC;
  124. + stream->sync = 0;
  125. + return -1;
  126. + }
  127. /* fall through */
  128. case 1:
  129. case 3:
  130. scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6);
  131. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  132. + {
  133. + stream->error = MAD_ERROR_LOSTSYNC;
  134. + stream->sync = 0;
  135. + return -1;
  136. + }
  137. }
  138. if (scfsi[ch][sb] & 1)
  139. @@ -488,6 +557,12 @@
  140. index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
  141. II_samples(&stream->ptr, &qc_table[index], samples);
  142. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  143. + {
  144. + stream->error = MAD_ERROR_LOSTSYNC;
  145. + stream->sync = 0;
  146. + return -1;
  147. + }
  148. for (s = 0; s < 3; ++s) {
  149. frame->sbsample[ch][3 * gr + s][sb] =
  150. @@ -506,6 +581,12 @@
  151. index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
  152. II_samples(&stream->ptr, &qc_table[index], samples);
  153. + if (mad_bit_nextbyte(&stream->ptr) > stream->next_frame)
  154. + {
  155. + stream->error = MAD_ERROR_LOSTSYNC;
  156. + stream->sync = 0;
  157. + return -1;
  158. + }
  159. for (ch = 0; ch < nch; ++ch) {
  160. for (s = 0; s < 3; ++s) {
  161. Index: libmad-0.15.1b/layer3.c
  162. ===================================================================
  163. --- libmad-0.15.1b.orig/layer3.c 2008-12-23 21:38:07.000000000 +0100
  164. +++ libmad-0.15.1b/layer3.c 2008-12-23 21:38:12.000000000 +0100
  165. @@ -2608,6 +2608,12 @@
  166. next_md_begin = 0;
  167. md_len = si.main_data_begin + frame_space - next_md_begin;
  168. + if (md_len + MAD_BUFFER_GUARD > MAD_BUFFER_MDLEN)
  169. + {
  170. + stream->error = MAD_ERROR_LOSTSYNC;
  171. + stream->sync = 0;
  172. + return -1;
  173. + }
  174. frame_used = 0;