You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

817 lines
24 KiB

  1. From: Kurt Roeckx <kurt@roeckx.be>
  2. Date: Sun, 28 Jan 2018 19:26:36 +0100
  3. Subject: Check the size before reading with mad_bit_read
  4. There are various cases where it attemps to read past the end of the buffer
  5. using mad_bit_read(). Most functions didn't even know the size of the buffer
  6. they were reading from.
  7. Index: libmad-0.15.1b/bit.c
  8. ===================================================================
  9. --- libmad-0.15.1b.orig/bit.c
  10. +++ libmad-0.15.1b/bit.c
  11. @@ -138,6 +138,9 @@ unsigned long mad_bit_read(struct mad_bi
  12. {
  13. register unsigned long value;
  14. + if (len == 0)
  15. + return 0;
  16. +
  17. if (bitptr->left == CHAR_BIT)
  18. bitptr->cache = *bitptr->byte;
  19. Index: libmad-0.15.1b/frame.c
  20. ===================================================================
  21. --- libmad-0.15.1b.orig/frame.c
  22. +++ libmad-0.15.1b/frame.c
  23. @@ -120,11 +120,18 @@ static
  24. int decode_header(struct mad_header *header, struct mad_stream *stream)
  25. {
  26. unsigned int index;
  27. + struct mad_bitptr bufend_ptr;
  28. header->flags = 0;
  29. header->private_bits = 0;
  30. + mad_bit_init(&bufend_ptr, stream->bufend);
  31. +
  32. /* header() */
  33. + if (mad_bit_length(&stream->ptr, &bufend_ptr) < 32) {
  34. + stream->error = MAD_ERROR_BUFLEN;
  35. + return -1;
  36. + }
  37. /* syncword */
  38. mad_bit_skip(&stream->ptr, 11);
  39. @@ -225,8 +232,13 @@ int decode_header(struct mad_header *hea
  40. /* error_check() */
  41. /* crc_check */
  42. - if (header->flags & MAD_FLAG_PROTECTION)
  43. + if (header->flags & MAD_FLAG_PROTECTION) {
  44. + if (mad_bit_length(&stream->ptr, &bufend_ptr) < 16) {
  45. + stream->error = MAD_ERROR_BUFLEN;
  46. + return -1;
  47. + }
  48. header->crc_target = mad_bit_read(&stream->ptr, 16);
  49. + }
  50. return 0;
  51. }
  52. @@ -338,7 +350,7 @@ int mad_header_decode(struct mad_header
  53. stream->error = MAD_ERROR_BUFLEN;
  54. goto fail;
  55. }
  56. - else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
  57. + else if ((end - ptr >= 2) && !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
  58. /* mark point where frame sync word was expected */
  59. stream->this_frame = ptr;
  60. stream->next_frame = ptr + 1;
  61. @@ -361,6 +373,8 @@ int mad_header_decode(struct mad_header
  62. ptr = mad_bit_nextbyte(&stream->ptr);
  63. }
  64. + stream->error = MAD_ERROR_NONE;
  65. +
  66. /* begin processing */
  67. stream->this_frame = ptr;
  68. stream->next_frame = ptr + 1; /* possibly bogus sync word */
  69. @@ -413,7 +427,7 @@ int mad_header_decode(struct mad_header
  70. /* check that a valid frame header follows this frame */
  71. ptr = stream->next_frame;
  72. - if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
  73. + if ((end - ptr >= 2) && !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
  74. ptr = stream->next_frame = stream->this_frame + 1;
  75. goto sync;
  76. }
  77. Index: libmad-0.15.1b/layer12.c
  78. ===================================================================
  79. --- libmad-0.15.1b.orig/layer12.c
  80. +++ libmad-0.15.1b/layer12.c
  81. @@ -72,10 +72,18 @@ mad_fixed_t const linear_table[14] = {
  82. * DESCRIPTION: decode one requantized Layer I sample from a bitstream
  83. */
  84. static
  85. -mad_fixed_t I_sample(struct mad_bitptr *ptr, unsigned int nb)
  86. +mad_fixed_t I_sample(struct mad_bitptr *ptr, unsigned int nb, struct mad_stream *stream)
  87. {
  88. mad_fixed_t sample;
  89. + struct mad_bitptr frameend_ptr;
  90. + mad_bit_init(&frameend_ptr, stream->next_frame);
  91. +
  92. + if (mad_bit_length(ptr, &frameend_ptr) < nb) {
  93. + stream->error = MAD_ERROR_LOSTSYNC;
  94. + stream->sync = 0;
  95. + return 0;
  96. + }
  97. sample = mad_bit_read(ptr, nb);
  98. /* invert most significant bit, extend sign, then scale to fixed format */
  99. @@ -106,6 +114,10 @@ int mad_layer_I(struct mad_stream *strea
  100. struct mad_header *header = &frame->header;
  101. unsigned int nch, bound, ch, s, sb, nb;
  102. unsigned char allocation[2][32], scalefactor[2][32];
  103. + struct mad_bitptr bufend_ptr, frameend_ptr;
  104. +
  105. + mad_bit_init(&bufend_ptr, stream->bufend);
  106. + mad_bit_init(&frameend_ptr, stream->next_frame);
  107. nch = MAD_NCHANNELS(header);
  108. @@ -118,6 +130,11 @@ int mad_layer_I(struct mad_stream *strea
  109. /* check CRC word */
  110. if (header->flags & MAD_FLAG_PROTECTION) {
  111. + if (mad_bit_length(&stream->ptr, &bufend_ptr)
  112. + < 4 * (bound * nch + (32 - bound))) {
  113. + stream->error = MAD_ERROR_BADCRC;
  114. + return -1;
  115. + }
  116. header->crc_check =
  117. mad_bit_crc(stream->ptr, 4 * (bound * nch + (32 - bound)),
  118. header->crc_check);
  119. @@ -133,6 +150,11 @@ int mad_layer_I(struct mad_stream *strea
  120. for (sb = 0; sb < bound; ++sb) {
  121. for (ch = 0; ch < nch; ++ch) {
  122. + if (mad_bit_length(&stream->ptr, &frameend_ptr) < 4) {
  123. + stream->error = MAD_ERROR_LOSTSYNC;
  124. + stream->sync = 0;
  125. + return -1;
  126. + }
  127. nb = mad_bit_read(&stream->ptr, 4);
  128. if (nb == 15) {
  129. @@ -145,6 +167,11 @@ int mad_layer_I(struct mad_stream *strea
  130. }
  131. for (sb = bound; sb < 32; ++sb) {
  132. + if (mad_bit_length(&stream->ptr, &frameend_ptr) < 4) {
  133. + stream->error = MAD_ERROR_LOSTSYNC;
  134. + stream->sync = 0;
  135. + return -1;
  136. + }
  137. nb = mad_bit_read(&stream->ptr, 4);
  138. if (nb == 15) {
  139. @@ -161,6 +188,11 @@ int mad_layer_I(struct mad_stream *strea
  140. for (sb = 0; sb < 32; ++sb) {
  141. for (ch = 0; ch < nch; ++ch) {
  142. if (allocation[ch][sb]) {
  143. + if (mad_bit_length(&stream->ptr, &frameend_ptr) < 6) {
  144. + stream->error = MAD_ERROR_LOSTSYNC;
  145. + stream->sync = 0;
  146. + return -1;
  147. + }
  148. scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6);
  149. # if defined(OPT_STRICT)
  150. @@ -185,8 +217,10 @@ int mad_layer_I(struct mad_stream *strea
  151. for (ch = 0; ch < nch; ++ch) {
  152. nb = allocation[ch][sb];
  153. frame->sbsample[ch][s][sb] = nb ?
  154. - mad_f_mul(I_sample(&stream->ptr, nb),
  155. + mad_f_mul(I_sample(&stream->ptr, nb, stream),
  156. sf_table[scalefactor[ch][sb]]) : 0;
  157. + if (stream->error != 0)
  158. + return -1;
  159. }
  160. }
  161. @@ -194,7 +228,14 @@ int mad_layer_I(struct mad_stream *strea
  162. if ((nb = allocation[0][sb])) {
  163. mad_fixed_t sample;
  164. - sample = I_sample(&stream->ptr, nb);
  165. + if (mad_bit_length(&stream->ptr, &frameend_ptr) < nb) {
  166. + stream->error = MAD_ERROR_LOSTSYNC;
  167. + stream->sync = 0;
  168. + return -1;
  169. + }
  170. + sample = I_sample(&stream->ptr, nb, stream);
  171. + if (stream->error != 0)
  172. + return -1;
  173. for (ch = 0; ch < nch; ++ch) {
  174. frame->sbsample[ch][s][sb] =
  175. @@ -280,13 +321,21 @@ struct quantclass {
  176. static
  177. void II_samples(struct mad_bitptr *ptr,
  178. struct quantclass const *quantclass,
  179. - mad_fixed_t output[3])
  180. + mad_fixed_t output[3], struct mad_stream *stream)
  181. {
  182. unsigned int nb, s, sample[3];
  183. + struct mad_bitptr frameend_ptr;
  184. +
  185. + mad_bit_init(&frameend_ptr, stream->next_frame);
  186. if ((nb = quantclass->group)) {
  187. unsigned int c, nlevels;
  188. + if (mad_bit_length(ptr, &frameend_ptr) < quantclass->bits) {
  189. + stream->error = MAD_ERROR_LOSTSYNC;
  190. + stream->sync = 0;
  191. + return;
  192. + }
  193. /* degrouping */
  194. c = mad_bit_read(ptr, quantclass->bits);
  195. nlevels = quantclass->nlevels;
  196. @@ -299,8 +348,14 @@ void II_samples(struct mad_bitptr *ptr,
  197. else {
  198. nb = quantclass->bits;
  199. - for (s = 0; s < 3; ++s)
  200. + for (s = 0; s < 3; ++s) {
  201. + if (mad_bit_length(ptr, &frameend_ptr) < nb) {
  202. + stream->error = MAD_ERROR_LOSTSYNC;
  203. + stream->sync = 0;
  204. + return;
  205. + }
  206. sample[s] = mad_bit_read(ptr, nb);
  207. + }
  208. }
  209. for (s = 0; s < 3; ++s) {
  210. @@ -336,6 +391,9 @@ int mad_layer_II(struct mad_stream *stre
  211. unsigned char const *offsets;
  212. unsigned char allocation[2][32], scfsi[2][32], scalefactor[2][32][3];
  213. mad_fixed_t samples[3];
  214. + struct mad_bitptr frameend_ptr;
  215. +
  216. + mad_bit_init(&frameend_ptr, stream->next_frame);
  217. nch = MAD_NCHANNELS(header);
  218. @@ -402,13 +460,24 @@ int mad_layer_II(struct mad_stream *stre
  219. for (sb = 0; sb < bound; ++sb) {
  220. nbal = bitalloc_table[offsets[sb]].nbal;
  221. - for (ch = 0; ch < nch; ++ch)
  222. + for (ch = 0; ch < nch; ++ch) {
  223. + if (mad_bit_length(&stream->ptr, &frameend_ptr) < nbal) {
  224. + stream->error = MAD_ERROR_LOSTSYNC;
  225. + stream->sync = 0;
  226. + return -1;
  227. + }
  228. allocation[ch][sb] = mad_bit_read(&stream->ptr, nbal);
  229. + }
  230. }
  231. for (sb = bound; sb < sblimit; ++sb) {
  232. nbal = bitalloc_table[offsets[sb]].nbal;
  233. + if (mad_bit_length(&stream->ptr, &frameend_ptr) < nbal) {
  234. + stream->error = MAD_ERROR_LOSTSYNC;
  235. + stream->sync = 0;
  236. + return -1;
  237. + }
  238. allocation[0][sb] =
  239. allocation[1][sb] = mad_bit_read(&stream->ptr, nbal);
  240. }
  241. @@ -417,8 +486,14 @@ int mad_layer_II(struct mad_stream *stre
  242. for (sb = 0; sb < sblimit; ++sb) {
  243. for (ch = 0; ch < nch; ++ch) {
  244. - if (allocation[ch][sb])
  245. + if (allocation[ch][sb]) {
  246. + if (mad_bit_length(&stream->ptr, &frameend_ptr) < 2) {
  247. + stream->error = MAD_ERROR_LOSTSYNC;
  248. + stream->sync = 0;
  249. + return -1;
  250. + }
  251. scfsi[ch][sb] = mad_bit_read(&stream->ptr, 2);
  252. + }
  253. }
  254. }
  255. @@ -441,6 +516,11 @@ int mad_layer_II(struct mad_stream *stre
  256. for (sb = 0; sb < sblimit; ++sb) {
  257. for (ch = 0; ch < nch; ++ch) {
  258. if (allocation[ch][sb]) {
  259. + if (mad_bit_length(&stream->ptr, &frameend_ptr) < 6) {
  260. + stream->error = MAD_ERROR_LOSTSYNC;
  261. + stream->sync = 0;
  262. + return -1;
  263. + }
  264. scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6);
  265. switch (scfsi[ch][sb]) {
  266. @@ -451,11 +531,21 @@ int mad_layer_II(struct mad_stream *stre
  267. break;
  268. case 0:
  269. + if (mad_bit_length(&stream->ptr, &frameend_ptr) < 6) {
  270. + stream->error = MAD_ERROR_LOSTSYNC;
  271. + stream->sync = 0;
  272. + return -1;
  273. + }
  274. scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6);
  275. /* fall through */
  276. case 1:
  277. case 3:
  278. + if (mad_bit_length(&stream->ptr, &frameend_ptr) < 6) {
  279. + stream->error = MAD_ERROR_LOSTSYNC;
  280. + stream->sync = 0;
  281. + return -1;
  282. + }
  283. scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6);
  284. }
  285. @@ -487,7 +577,9 @@ int mad_layer_II(struct mad_stream *stre
  286. if ((index = allocation[ch][sb])) {
  287. index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
  288. - II_samples(&stream->ptr, &qc_table[index], samples);
  289. + II_samples(&stream->ptr, &qc_table[index], samples, stream);
  290. + if (stream->error != 0)
  291. + return -1;
  292. for (s = 0; s < 3; ++s) {
  293. frame->sbsample[ch][3 * gr + s][sb] =
  294. @@ -505,7 +597,9 @@ int mad_layer_II(struct mad_stream *stre
  295. if ((index = allocation[0][sb])) {
  296. index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
  297. - II_samples(&stream->ptr, &qc_table[index], samples);
  298. + II_samples(&stream->ptr, &qc_table[index], samples, stream);
  299. + if (stream->error != 0)
  300. + return -1;
  301. for (ch = 0; ch < nch; ++ch) {
  302. for (s = 0; s < 3; ++s) {
  303. Index: libmad-0.15.1b/layer3.c
  304. ===================================================================
  305. --- libmad-0.15.1b.orig/layer3.c
  306. +++ libmad-0.15.1b/layer3.c
  307. @@ -598,7 +598,8 @@ enum mad_error III_sideinfo(struct mad_b
  308. static
  309. unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
  310. struct channel *channel,
  311. - struct channel *gr1ch, int mode_extension)
  312. + struct channel *gr1ch, int mode_extension,
  313. + unsigned int bits_left, unsigned int *part2_length)
  314. {
  315. struct mad_bitptr start;
  316. unsigned int scalefac_compress, index, slen[4], part, n, i;
  317. @@ -644,8 +645,12 @@ unsigned int III_scalefactors_lsf(struct
  318. n = 0;
  319. for (part = 0; part < 4; ++part) {
  320. - for (i = 0; i < nsfb[part]; ++i)
  321. + for (i = 0; i < nsfb[part]; ++i) {
  322. + if (bits_left < slen[part])
  323. + return MAD_ERROR_BADSCFSI;
  324. channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
  325. + bits_left -= slen[part];
  326. + }
  327. }
  328. while (n < 39)
  329. @@ -690,7 +695,10 @@ unsigned int III_scalefactors_lsf(struct
  330. max = (1 << slen[part]) - 1;
  331. for (i = 0; i < nsfb[part]; ++i) {
  332. + if (bits_left < slen[part])
  333. + return MAD_ERROR_BADSCFSI;
  334. is_pos = mad_bit_read(ptr, slen[part]);
  335. + bits_left -= slen[part];
  336. channel->scalefac[n] = is_pos;
  337. gr1ch->scalefac[n++] = (is_pos == max);
  338. @@ -703,7 +711,8 @@ unsigned int III_scalefactors_lsf(struct
  339. }
  340. }
  341. - return mad_bit_length(&start, ptr);
  342. + *part2_length = mad_bit_length(&start, ptr);
  343. + return MAD_ERROR_NONE;
  344. }
  345. /*
  346. @@ -712,7 +721,8 @@ unsigned int III_scalefactors_lsf(struct
  347. */
  348. static
  349. unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
  350. - struct channel const *gr0ch, unsigned int scfsi)
  351. + struct channel const *gr0ch, unsigned int scfsi,
  352. + unsigned int bits_left, unsigned int *part2_length)
  353. {
  354. struct mad_bitptr start;
  355. unsigned int slen1, slen2, sfbi;
  356. @@ -728,12 +738,20 @@ unsigned int III_scalefactors(struct mad
  357. sfbi = 0;
  358. nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3;
  359. - while (nsfb--)
  360. + while (nsfb--) {
  361. + if (bits_left < slen1)
  362. + return MAD_ERROR_BADSCFSI;
  363. channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1);
  364. + bits_left -= slen1;
  365. + }
  366. nsfb = 6 * 3;
  367. - while (nsfb--)
  368. + while (nsfb--) {
  369. + if (bits_left < slen2)
  370. + return MAD_ERROR_BADSCFSI;
  371. channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2);
  372. + bits_left -= slen2;
  373. + }
  374. nsfb = 1 * 3;
  375. while (nsfb--)
  376. @@ -745,8 +763,12 @@ unsigned int III_scalefactors(struct mad
  377. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  378. }
  379. else {
  380. - for (sfbi = 0; sfbi < 6; ++sfbi)
  381. + for (sfbi = 0; sfbi < 6; ++sfbi) {
  382. + if (bits_left < slen1)
  383. + return MAD_ERROR_BADSCFSI;
  384. channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
  385. + bits_left -= slen1;
  386. + }
  387. }
  388. if (scfsi & 0x4) {
  389. @@ -754,8 +776,12 @@ unsigned int III_scalefactors(struct mad
  390. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  391. }
  392. else {
  393. - for (sfbi = 6; sfbi < 11; ++sfbi)
  394. + for (sfbi = 6; sfbi < 11; ++sfbi) {
  395. + if (bits_left < slen1)
  396. + return MAD_ERROR_BADSCFSI;
  397. channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
  398. + bits_left -= slen1;
  399. + }
  400. }
  401. if (scfsi & 0x2) {
  402. @@ -763,8 +789,12 @@ unsigned int III_scalefactors(struct mad
  403. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  404. }
  405. else {
  406. - for (sfbi = 11; sfbi < 16; ++sfbi)
  407. + for (sfbi = 11; sfbi < 16; ++sfbi) {
  408. + if (bits_left < slen2)
  409. + return MAD_ERROR_BADSCFSI;
  410. channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
  411. + bits_left -= slen2;
  412. + }
  413. }
  414. if (scfsi & 0x1) {
  415. @@ -772,14 +802,19 @@ unsigned int III_scalefactors(struct mad
  416. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  417. }
  418. else {
  419. - for (sfbi = 16; sfbi < 21; ++sfbi)
  420. + for (sfbi = 16; sfbi < 21; ++sfbi) {
  421. + if (bits_left < slen2)
  422. + return MAD_ERROR_BADSCFSI;
  423. channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
  424. + bits_left -= slen2;
  425. + }
  426. }
  427. channel->scalefac[21] = 0;
  428. }
  429. - return mad_bit_length(&start, ptr);
  430. + *part2_length = mad_bit_length(&start, ptr);
  431. + return MAD_ERROR_NONE;
  432. }
  433. /*
  434. @@ -933,19 +968,17 @@ static
  435. enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
  436. struct channel *channel,
  437. unsigned char const *sfbwidth,
  438. - unsigned int part2_length)
  439. + signed int part3_length)
  440. {
  441. signed int exponents[39], exp;
  442. signed int const *expptr;
  443. struct mad_bitptr peek;
  444. - signed int bits_left, cachesz;
  445. + signed int bits_left, cachesz, fakebits;
  446. register mad_fixed_t *xrptr;
  447. mad_fixed_t const *sfbound;
  448. register unsigned long bitcache;
  449. - bits_left = (signed) channel->part2_3_length - (signed) part2_length;
  450. - if (bits_left < 0)
  451. - return MAD_ERROR_BADPART3LEN;
  452. + bits_left = part3_length;
  453. III_exponents(channel, sfbwidth, exponents);
  454. @@ -956,8 +989,12 @@ enum mad_error III_huffdecode(struct mad
  455. cachesz = mad_bit_bitsleft(&peek);
  456. cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7;
  457. + if (bits_left < cachesz) {
  458. + cachesz = bits_left;
  459. + }
  460. bitcache = mad_bit_read(&peek, cachesz);
  461. bits_left -= cachesz;
  462. + fakebits = 0;
  463. xrptr = &xr[0];
  464. @@ -986,7 +1023,7 @@ enum mad_error III_huffdecode(struct mad
  465. big_values = channel->big_values;
  466. - while (big_values-- && cachesz + bits_left > 0) {
  467. + while (big_values-- && cachesz + bits_left - fakebits > 0) {
  468. union huffpair const *pair;
  469. unsigned int clumpsz, value;
  470. register mad_fixed_t requantized;
  471. @@ -1023,10 +1060,19 @@ enum mad_error III_huffdecode(struct mad
  472. unsigned int bits;
  473. bits = ((32 - 1 - 21) + (21 - cachesz)) & ~7;
  474. + if (bits_left < bits) {
  475. + bits = bits_left;
  476. + }
  477. bitcache = (bitcache << bits) | mad_bit_read(&peek, bits);
  478. cachesz += bits;
  479. bits_left -= bits;
  480. }
  481. + if (cachesz < 21) {
  482. + unsigned int bits = 21 - cachesz;
  483. + bitcache <<= bits;
  484. + cachesz += bits;
  485. + fakebits += bits;
  486. + }
  487. /* hcod (0..19) */
  488. @@ -1041,6 +1087,8 @@ enum mad_error III_huffdecode(struct mad
  489. }
  490. cachesz -= pair->value.hlen;
  491. + if (cachesz < fakebits)
  492. + return MAD_ERROR_BADHUFFDATA;
  493. if (linbits) {
  494. /* x (0..14) */
  495. @@ -1054,10 +1102,15 @@ enum mad_error III_huffdecode(struct mad
  496. case 15:
  497. if (cachesz < linbits + 2) {
  498. - bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
  499. - cachesz += 16;
  500. - bits_left -= 16;
  501. + unsigned int bits = 16;
  502. + if (bits_left < 16)
  503. + bits = bits_left;
  504. + bitcache = (bitcache << bits) | mad_bit_read(&peek, bits);
  505. + cachesz += bits;
  506. + bits_left -= bits;
  507. }
  508. + if (cachesz - fakebits < linbits)
  509. + return MAD_ERROR_BADHUFFDATA;
  510. value += MASK(bitcache, cachesz, linbits);
  511. cachesz -= linbits;
  512. @@ -1074,6 +1127,8 @@ enum mad_error III_huffdecode(struct mad
  513. }
  514. x_final:
  515. + if (cachesz - fakebits < 1)
  516. + return MAD_ERROR_BADHUFFDATA;
  517. xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
  518. -requantized : requantized;
  519. }
  520. @@ -1089,10 +1144,15 @@ enum mad_error III_huffdecode(struct mad
  521. case 15:
  522. if (cachesz < linbits + 1) {
  523. - bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
  524. - cachesz += 16;
  525. - bits_left -= 16;
  526. + unsigned int bits = 16;
  527. + if (bits_left < 16)
  528. + bits = bits_left;
  529. + bitcache = (bitcache << bits) | mad_bit_read(&peek, bits);
  530. + cachesz += bits;
  531. + bits_left -= bits;
  532. }
  533. + if (cachesz - fakebits < linbits)
  534. + return MAD_ERROR_BADHUFFDATA;
  535. value += MASK(bitcache, cachesz, linbits);
  536. cachesz -= linbits;
  537. @@ -1109,6 +1169,8 @@ enum mad_error III_huffdecode(struct mad
  538. }
  539. y_final:
  540. + if (cachesz - fakebits < 1)
  541. + return MAD_ERROR_BADHUFFDATA;
  542. xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
  543. -requantized : requantized;
  544. }
  545. @@ -1128,6 +1190,8 @@ enum mad_error III_huffdecode(struct mad
  546. requantized = reqcache[value] = III_requantize(value, exp);
  547. }
  548. + if (cachesz - fakebits < 1)
  549. + return MAD_ERROR_BADHUFFDATA;
  550. xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
  551. -requantized : requantized;
  552. }
  553. @@ -1146,6 +1210,8 @@ enum mad_error III_huffdecode(struct mad
  554. requantized = reqcache[value] = III_requantize(value, exp);
  555. }
  556. + if (cachesz - fakebits < 1)
  557. + return MAD_ERROR_BADHUFFDATA;
  558. xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
  559. -requantized : requantized;
  560. }
  561. @@ -1155,9 +1221,6 @@ enum mad_error III_huffdecode(struct mad
  562. }
  563. }
  564. - if (cachesz + bits_left < 0)
  565. - return MAD_ERROR_BADHUFFDATA; /* big_values overrun */
  566. -
  567. /* count1 */
  568. {
  569. union huffquad const *table;
  570. @@ -1167,15 +1230,24 @@ enum mad_error III_huffdecode(struct mad
  571. requantized = III_requantize(1, exp);
  572. - while (cachesz + bits_left > 0 && xrptr <= &xr[572]) {
  573. + while (cachesz + bits_left - fakebits > 0 && xrptr <= &xr[572]) {
  574. union huffquad const *quad;
  575. /* hcod (1..6) */
  576. if (cachesz < 10) {
  577. - bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
  578. - cachesz += 16;
  579. - bits_left -= 16;
  580. + unsigned int bits = 16;
  581. + if (bits_left < 16)
  582. + bits = bits_left;
  583. + bitcache = (bitcache << bits) | mad_bit_read(&peek, bits);
  584. + cachesz += bits;
  585. + bits_left -= bits;
  586. + }
  587. + if (cachesz < 10) {
  588. + unsigned int bits = 10 - cachesz;
  589. + bitcache <<= bits;
  590. + cachesz += bits;
  591. + fakebits += bits;
  592. }
  593. quad = &table[MASK(bitcache, cachesz, 4)];
  594. @@ -1188,6 +1260,11 @@ enum mad_error III_huffdecode(struct mad
  595. MASK(bitcache, cachesz, quad->ptr.bits)];
  596. }
  597. + if (cachesz - fakebits < quad->value.hlen + quad->value.v
  598. + + quad->value.w + quad->value.x + quad->value.y)
  599. + /* We don't have enough bits to read one more entry, consider them
  600. + * stuffing bits. */
  601. + break;
  602. cachesz -= quad->value.hlen;
  603. if (xrptr == sfbound) {
  604. @@ -1236,22 +1313,8 @@ enum mad_error III_huffdecode(struct mad
  605. xrptr += 2;
  606. }
  607. -
  608. - if (cachesz + bits_left < 0) {
  609. -# if 0 && defined(DEBUG)
  610. - fprintf(stderr, "huffman count1 overrun (%d bits)\n",
  611. - -(cachesz + bits_left));
  612. -# endif
  613. -
  614. - /* technically the bitstream is misformatted, but apparently
  615. - some encoders are just a bit sloppy with stuffing bits */
  616. -
  617. - xrptr -= 4;
  618. - }
  619. }
  620. - assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT);
  621. -
  622. # if 0 && defined(DEBUG)
  623. if (bits_left < 0)
  624. fprintf(stderr, "read %d bits too many\n", -bits_left);
  625. @@ -2348,10 +2411,11 @@ void III_freqinver(mad_fixed_t sample[18
  626. */
  627. static
  628. enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
  629. - struct sideinfo *si, unsigned int nch)
  630. + struct sideinfo *si, unsigned int nch, unsigned int md_len)
  631. {
  632. struct mad_header *header = &frame->header;
  633. unsigned int sfreqi, ngr, gr;
  634. + int bits_left = md_len * CHAR_BIT;
  635. {
  636. unsigned int sfreq;
  637. @@ -2383,6 +2447,7 @@ enum mad_error III_decode(struct mad_bit
  638. for (ch = 0; ch < nch; ++ch) {
  639. struct channel *channel = &granule->ch[ch];
  640. unsigned int part2_length;
  641. + unsigned int part3_length;
  642. sfbwidth[ch] = sfbwidth_table[sfreqi].l;
  643. if (channel->block_type == 2) {
  644. @@ -2391,18 +2456,30 @@ enum mad_error III_decode(struct mad_bit
  645. }
  646. if (header->flags & MAD_FLAG_LSF_EXT) {
  647. - part2_length = III_scalefactors_lsf(ptr, channel,
  648. + error = III_scalefactors_lsf(ptr, channel,
  649. ch == 0 ? 0 : &si->gr[1].ch[1],
  650. - header->mode_extension);
  651. + header->mode_extension, bits_left, &part2_length);
  652. }
  653. else {
  654. - part2_length = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
  655. - gr == 0 ? 0 : si->scfsi[ch]);
  656. + error = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
  657. + gr == 0 ? 0 : si->scfsi[ch], bits_left, &part2_length);
  658. }
  659. + if (error)
  660. + return error;
  661. +
  662. + bits_left -= part2_length;
  663. - error = III_huffdecode(ptr, xr[ch], channel, sfbwidth[ch], part2_length);
  664. + if (part2_length > channel->part2_3_length)
  665. + return MAD_ERROR_BADPART3LEN;
  666. +
  667. + part3_length = channel->part2_3_length - part2_length;
  668. + if (part3_length > bits_left)
  669. + return MAD_ERROR_BADPART3LEN;
  670. +
  671. + error = III_huffdecode(ptr, xr[ch], channel, sfbwidth[ch], part3_length);
  672. if (error)
  673. return error;
  674. + bits_left -= part3_length;
  675. }
  676. /* joint stereo processing */
  677. @@ -2519,11 +2596,13 @@ int mad_layer_III(struct mad_stream *str
  678. unsigned int nch, priv_bitlen, next_md_begin = 0;
  679. unsigned int si_len, data_bitlen, md_len;
  680. unsigned int frame_space, frame_used, frame_free;
  681. - struct mad_bitptr ptr;
  682. + struct mad_bitptr ptr, bufend_ptr;
  683. struct sideinfo si;
  684. enum mad_error error;
  685. int result = 0;
  686. + mad_bit_init(&bufend_ptr, stream->bufend);
  687. +
  688. /* allocate Layer III dynamic structures */
  689. if (stream->main_data == 0) {
  690. @@ -2587,14 +2666,15 @@ int mad_layer_III(struct mad_stream *str
  691. unsigned long header;
  692. mad_bit_init(&peek, stream->next_frame);
  693. + if (mad_bit_length(&peek, &bufend_ptr) >= 57) {
  694. + header = mad_bit_read(&peek, 32);
  695. + if ((header & 0xffe60000L) /* syncword | layer */ == 0xffe20000L) {
  696. + if (!(header & 0x00010000L)) /* protection_bit */
  697. + mad_bit_skip(&peek, 16); /* crc_check */
  698. - header = mad_bit_read(&peek, 32);
  699. - if ((header & 0xffe60000L) /* syncword | layer */ == 0xffe20000L) {
  700. - if (!(header & 0x00010000L)) /* protection_bit */
  701. - mad_bit_skip(&peek, 16); /* crc_check */
  702. -
  703. - next_md_begin =
  704. - mad_bit_read(&peek, (header & 0x00080000L) /* ID */ ? 9 : 8);
  705. + next_md_begin =
  706. + mad_bit_read(&peek, (header & 0x00080000L) /* ID */ ? 9 : 8);
  707. + }
  708. }
  709. mad_bit_finish(&peek);
  710. @@ -2653,7 +2733,7 @@ int mad_layer_III(struct mad_stream *str
  711. /* decode main_data */
  712. if (result == 0) {
  713. - error = III_decode(&ptr, frame, &si, nch);
  714. + error = III_decode(&ptr, frame, &si, nch, md_len);
  715. if (error) {
  716. stream->error = error;
  717. result = -1;