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.

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