xz_dec_stream.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /* xz_dec_stream.c - .xz Stream decoder */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * This file is based on code from XZ embedded project
  21. * http://tukaani.org/xz/embedded.html
  22. */
  23. #include "xz_config.h"
  24. #include "xz_private.h"
  25. #include "xz_stream.h"
  26. #include <grub/crypto.h>
  27. /* Hash used to validate the Index field */
  28. struct xz_dec_hash {
  29. vli_type unpadded;
  30. vli_type uncompressed;
  31. #ifndef GRUB_EMBED_DECOMPRESSOR
  32. uint64_t *hash_context;
  33. #endif
  34. };
  35. /* Enough for up to 512 bits. */
  36. #define MAX_HASH_SIZE 64
  37. struct xz_dec {
  38. /* Position in dec_main() */
  39. enum {
  40. SEQ_STREAM_HEADER,
  41. SEQ_BLOCK_START,
  42. SEQ_BLOCK_HEADER,
  43. SEQ_BLOCK_UNCOMPRESS,
  44. SEQ_BLOCK_PADDING,
  45. SEQ_BLOCK_CHECK,
  46. SEQ_INDEX,
  47. SEQ_INDEX_PADDING,
  48. SEQ_INDEX_CRC32,
  49. SEQ_STREAM_FOOTER
  50. } sequence;
  51. /* Position in variable-length integers and Check fields */
  52. uint32_t pos;
  53. /* Variable-length integer decoded by dec_vli() */
  54. vli_type vli;
  55. /* Saved in_pos and out_pos */
  56. size_t in_start;
  57. size_t out_start;
  58. /* CRC32 value in Block or Index */
  59. #ifndef GRUB_EMBED_DECOMPRESSOR
  60. uint8_t hash_value[MAX_HASH_SIZE]; /* need for crc32_validate*/
  61. #endif
  62. int have_hash_value;
  63. #ifndef GRUB_EMBED_DECOMPRESSOR
  64. uint64_t *hash_context;
  65. uint64_t *crc32_context;
  66. #endif
  67. /* Hash function calculated from uncompressed data */
  68. #ifndef GRUB_EMBED_DECOMPRESSOR
  69. const gcry_md_spec_t *hash;
  70. const gcry_md_spec_t *crc32;
  71. grub_uint8_t hash_id;
  72. #endif
  73. grub_size_t hash_size;
  74. /* True if we are operating in single-call mode. */
  75. bool single_call;
  76. /*
  77. * True if the next call to xz_dec_run() is allowed to return
  78. * XZ_BUF_ERROR.
  79. */
  80. bool allow_buf_error;
  81. /* Information stored in Block Header */
  82. struct {
  83. /*
  84. * Value stored in the Compressed Size field, or
  85. * VLI_UNKNOWN if Compressed Size is not present.
  86. */
  87. vli_type compressed;
  88. /*
  89. * Value stored in the Uncompressed Size field, or
  90. * VLI_UNKNOWN if Uncompressed Size is not present.
  91. */
  92. vli_type uncompressed;
  93. /* Size of the Block Header field */
  94. uint32_t size;
  95. } block_header;
  96. /* Information collected when decoding Blocks */
  97. struct {
  98. /* Observed compressed size of the current Block */
  99. vli_type compressed;
  100. /* Observed uncompressed size of the current Block */
  101. vli_type uncompressed;
  102. /* Number of Blocks decoded so far */
  103. vli_type count;
  104. /*
  105. * Hash calculated from the Block sizes. This is used to
  106. * validate the Index field.
  107. */
  108. struct xz_dec_hash hash;
  109. } block;
  110. /* Variables needed when verifying the Index field */
  111. struct {
  112. /* Position in dec_index() */
  113. enum {
  114. SEQ_INDEX_COUNT,
  115. SEQ_INDEX_UNPADDED,
  116. SEQ_INDEX_UNCOMPRESSED
  117. } sequence;
  118. /* Size of the Index in bytes */
  119. vli_type size;
  120. /* Number of Records (matches block.count in valid files) */
  121. vli_type count;
  122. /*
  123. * Hash calculated from the Records (matches block.hash in
  124. * valid files).
  125. */
  126. struct xz_dec_hash hash;
  127. } index;
  128. /*
  129. * Temporary buffer needed to hold Stream Header, Block Header,
  130. * and Stream Footer. The Block Header is the biggest (1 KiB)
  131. * so we reserve space according to that. buf[] has to be aligned
  132. * to a multiple of four bytes; the size_t variables before it
  133. * should guarantee this.
  134. */
  135. struct {
  136. size_t pos;
  137. size_t size;
  138. uint8_t buf[1024];
  139. } temp;
  140. struct xz_dec_lzma2 *lzma2;
  141. #ifdef XZ_DEC_BCJ
  142. struct xz_dec_bcj *bcj;
  143. bool bcj_active;
  144. #endif
  145. };
  146. /*
  147. * Fill s->temp by copying data starting from b->in[b->in_pos]. Caller
  148. * must have set s->temp.pos to indicate how much data we are supposed
  149. * to copy into s->temp.buf. Return true once s->temp.pos has reached
  150. * s->temp.size.
  151. */
  152. static bool fill_temp(struct xz_dec *s, struct xz_buf *b)
  153. {
  154. size_t copy_size = min_t(size_t,
  155. b->in_size - b->in_pos, s->temp.size - s->temp.pos);
  156. memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size);
  157. b->in_pos += copy_size;
  158. s->temp.pos += copy_size;
  159. if (s->temp.pos == s->temp.size) {
  160. s->temp.pos = 0;
  161. return true;
  162. }
  163. return false;
  164. }
  165. /* Decode a variable-length integer (little-endian base-128 encoding) */
  166. static enum xz_ret dec_vli(struct xz_dec *s,
  167. const uint8_t *in, size_t *in_pos, size_t in_size)
  168. {
  169. uint8_t b;
  170. if (s->pos == 0)
  171. s->vli = 0;
  172. while (*in_pos < in_size) {
  173. b = in[*in_pos];
  174. ++*in_pos;
  175. s->vli |= (vli_type)(b & 0x7F) << s->pos;
  176. if ((b & 0x80) == 0) {
  177. /* Don't allow non-minimal encodings. */
  178. if (b == 0 && s->pos != 0)
  179. return XZ_DATA_ERROR;
  180. s->pos = 0;
  181. return XZ_STREAM_END;
  182. }
  183. s->pos += 7;
  184. if (s->pos == 7 * VLI_BYTES_MAX)
  185. return XZ_DATA_ERROR;
  186. }
  187. return XZ_OK;
  188. }
  189. /*
  190. * Decode the Compressed Data field from a Block. Update and validate
  191. * the observed compressed and uncompressed sizes of the Block so that
  192. * they don't exceed the values possibly stored in the Block Header
  193. * (validation assumes that no integer overflow occurs, since vli_type
  194. * is normally uint64_t). Update the CRC32 if presence of the CRC32
  195. * field was indicated in Stream Header.
  196. *
  197. * Once the decoding is finished, validate that the observed sizes match
  198. * the sizes possibly stored in the Block Header. Update the hash and
  199. * Block count, which are later used to validate the Index field.
  200. */
  201. static enum xz_ret dec_block(struct xz_dec *s, struct xz_buf *b)
  202. {
  203. enum xz_ret ret;
  204. s->in_start = b->in_pos;
  205. s->out_start = b->out_pos;
  206. #ifdef XZ_DEC_BCJ
  207. if (s->bcj_active)
  208. ret = xz_dec_bcj_run(s->bcj, s->lzma2, b);
  209. else
  210. #endif
  211. ret = xz_dec_lzma2_run(s->lzma2, b);
  212. s->block.compressed += b->in_pos - s->in_start;
  213. s->block.uncompressed += b->out_pos - s->out_start;
  214. /*
  215. * There is no need to separately check for VLI_UNKNOWN, since
  216. * the observed sizes are always smaller than VLI_UNKNOWN.
  217. */
  218. if (s->block.compressed > s->block_header.compressed
  219. || s->block.uncompressed
  220. > s->block_header.uncompressed)
  221. return XZ_DATA_ERROR;
  222. #ifndef GRUB_EMBED_DECOMPRESSOR
  223. if (s->hash)
  224. s->hash->write(s->hash_context,b->out + s->out_start,
  225. b->out_pos - s->out_start);
  226. if (s->crc32)
  227. s->crc32->write(s->crc32_context,b->out + s->out_start,
  228. b->out_pos - s->out_start);
  229. #endif
  230. if (ret == XZ_STREAM_END) {
  231. if (s->block_header.compressed != VLI_UNKNOWN
  232. && s->block_header.compressed
  233. != s->block.compressed)
  234. return XZ_DATA_ERROR;
  235. if (s->block_header.uncompressed != VLI_UNKNOWN
  236. && s->block_header.uncompressed
  237. != s->block.uncompressed)
  238. return XZ_DATA_ERROR;
  239. s->block.hash.unpadded += s->block_header.size
  240. + s->block.compressed;
  241. s->block.hash.unpadded += s->hash_size;
  242. s->block.hash.uncompressed += s->block.uncompressed;
  243. #ifndef GRUB_EMBED_DECOMPRESSOR
  244. if (s->hash)
  245. s->hash->write(s->block.hash.hash_context,
  246. (const uint8_t *)&s->block.hash,
  247. 2 * sizeof(vli_type));
  248. #endif
  249. ++s->block.count;
  250. }
  251. return ret;
  252. }
  253. /* Update the Index size and the CRC32 value. */
  254. static void index_update(struct xz_dec *s, const struct xz_buf *b)
  255. {
  256. size_t in_used = b->in_pos - s->in_start;
  257. s->index.size += in_used;
  258. #ifndef GRUB_EMBED_DECOMPRESSOR
  259. if (s->hash)
  260. s->hash->write(s->hash_context,b->in + s->in_start, in_used);
  261. if (s->crc32)
  262. s->crc32->write(s->crc32_context,b->in + s->in_start, in_used);
  263. #endif
  264. }
  265. /*
  266. * Decode the Number of Records, Unpadded Size, and Uncompressed Size
  267. * fields from the Index field. That is, Index Padding and CRC32 are not
  268. * decoded by this function.
  269. *
  270. * This can return XZ_OK (more input needed), XZ_STREAM_END (everything
  271. * successfully decoded), or XZ_DATA_ERROR (input is corrupt).
  272. */
  273. static enum xz_ret dec_index(struct xz_dec *s, struct xz_buf *b)
  274. {
  275. enum xz_ret ret;
  276. do {
  277. ret = dec_vli(s, b->in, &b->in_pos, b->in_size);
  278. if (ret != XZ_STREAM_END) {
  279. index_update(s, b);
  280. return ret;
  281. }
  282. switch (s->index.sequence) {
  283. case SEQ_INDEX_COUNT:
  284. s->index.count = s->vli;
  285. /*
  286. * Validate that the Number of Records field
  287. * indicates the same number of Records as
  288. * there were Blocks in the Stream.
  289. */
  290. if (s->index.count != s->block.count)
  291. return XZ_DATA_ERROR;
  292. s->index.sequence = SEQ_INDEX_UNPADDED;
  293. break;
  294. case SEQ_INDEX_UNPADDED:
  295. s->index.hash.unpadded += s->vli;
  296. s->index.sequence = SEQ_INDEX_UNCOMPRESSED;
  297. break;
  298. case SEQ_INDEX_UNCOMPRESSED:
  299. s->index.hash.uncompressed += s->vli;
  300. #ifndef GRUB_EMBED_DECOMPRESSOR
  301. if (s->hash)
  302. s->hash->write(s->index.hash.hash_context,
  303. (const uint8_t *)&s->index.hash, 2 * sizeof(vli_type));
  304. #endif
  305. --s->index.count;
  306. s->index.sequence = SEQ_INDEX_UNPADDED;
  307. break;
  308. }
  309. } while (s->index.count > 0);
  310. return XZ_STREAM_END;
  311. }
  312. /*
  313. * Validate that the next four input bytes match the value of s->crc32.
  314. * s->pos must be zero when starting to validate the first byte.
  315. */
  316. static enum xz_ret hash_validate(struct xz_dec *s, struct xz_buf *b,
  317. int crc32)
  318. {
  319. #ifndef GRUB_EMBED_DECOMPRESSOR
  320. const gcry_md_spec_t *hash = crc32 ? s->crc32 : s->hash;
  321. void *hash_context = crc32 ? s->crc32_context
  322. : s->hash_context;
  323. if(!s->have_hash_value && hash
  324. && sizeof (s->hash_value) >= hash->mdlen)
  325. {
  326. hash->final(hash_context);
  327. grub_memcpy (s->hash_value, hash->read(hash_context),
  328. hash->mdlen);
  329. s->have_hash_value = 1;
  330. if (s->hash_id == 1 || crc32)
  331. {
  332. grub_uint8_t t;
  333. t = s->hash_value[0];
  334. s->hash_value[0] = s->hash_value[3];
  335. s->hash_value[3] = t;
  336. t = s->hash_value[1];
  337. s->hash_value[1] = s->hash_value[2];
  338. s->hash_value[2] = t;
  339. }
  340. }
  341. #endif
  342. if (b->in_pos == b->in_size)
  343. return XZ_OK;
  344. if (!crc32 && s->hash_size == 0)
  345. s->pos += 8;
  346. while (s->pos < (crc32 ? 32 : s->hash_size * 8)) {
  347. if (b->in_pos == b->in_size)
  348. return XZ_OK;
  349. #ifndef GRUB_EMBED_DECOMPRESSOR
  350. if (hash && s->hash_value[s->pos / 8] != b->in[b->in_pos])
  351. return XZ_DATA_ERROR;
  352. #endif
  353. b->in_pos++;
  354. s->pos += 8;
  355. }
  356. #ifndef GRUB_EMBED_DECOMPRESSOR
  357. if (s->hash)
  358. s->hash->init(s->hash_context);
  359. if (s->crc32)
  360. s->crc32->init(s->crc32_context);
  361. #endif
  362. s->have_hash_value = 0;
  363. s->pos = 0;
  364. return XZ_STREAM_END;
  365. }
  366. static const struct
  367. {
  368. const char *name;
  369. grub_size_t size;
  370. } hashes[] = {
  371. [0x01] = { "CRC32", 4},
  372. [0x04] = { "CRC64", 8},
  373. [0x0A] = { "SHA256", 32},
  374. };
  375. /* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */
  376. static enum xz_ret dec_stream_header(struct xz_dec *s)
  377. {
  378. if (! memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE))
  379. return XZ_FORMAT_ERROR;
  380. #ifndef GRUB_EMBED_DECOMPRESSOR
  381. s->crc32 = grub_crypto_lookup_md_by_name ("CRC32");
  382. if (s->crc32)
  383. {
  384. uint8_t readhash[4];
  385. uint8_t computed_hash[4];
  386. if(4 != s->crc32->mdlen)
  387. return XZ_DATA_ERROR;
  388. grub_crypto_hash (s->crc32, computed_hash,
  389. s->temp.buf + HEADER_MAGIC_SIZE, 2);
  390. readhash[0] = s->temp.buf[HEADER_MAGIC_SIZE + 5];
  391. readhash[1] = s->temp.buf[HEADER_MAGIC_SIZE + 4];
  392. readhash[2] = s->temp.buf[HEADER_MAGIC_SIZE + 3];
  393. readhash[3] = s->temp.buf[HEADER_MAGIC_SIZE + 2];
  394. if (grub_memcmp (readhash, computed_hash,
  395. s->crc32->mdlen) != 0)
  396. return XZ_DATA_ERROR;
  397. }
  398. #endif
  399. #ifndef GRUB_EMBED_DECOMPRESSOR
  400. /*
  401. * Decode the Stream Flags field.
  402. */
  403. if (s->temp.buf[HEADER_MAGIC_SIZE] != 0
  404. || s->temp.buf[HEADER_MAGIC_SIZE + 1] >= ARRAY_SIZE (hashes)
  405. || (hashes[s->temp.buf[HEADER_MAGIC_SIZE + 1]].name == 0
  406. && s->temp.buf[HEADER_MAGIC_SIZE + 1] != 0))
  407. return XZ_OPTIONS_ERROR;
  408. s->hash_id = s->temp.buf[HEADER_MAGIC_SIZE + 1];
  409. if (s->crc32)
  410. {
  411. s->crc32_context = kmalloc(s->crc32->contextsize, GFP_KERNEL);
  412. if (s->crc32_context == NULL)
  413. return XZ_MEMLIMIT_ERROR;
  414. s->crc32->init(s->crc32_context);
  415. }
  416. #endif
  417. if (s->temp.buf[HEADER_MAGIC_SIZE + 1])
  418. {
  419. s->hash_size = hashes[s->temp.buf[HEADER_MAGIC_SIZE + 1]].size;
  420. #ifndef GRUB_EMBED_DECOMPRESSOR
  421. s->hash = grub_crypto_lookup_md_by_name (hashes[s->temp.buf[HEADER_MAGIC_SIZE + 1]].name);
  422. if (s->hash)
  423. {
  424. if (s->hash->mdlen != s->hash_size)
  425. return XZ_OPTIONS_ERROR;
  426. s->hash_context = kmalloc(s->hash->contextsize, GFP_KERNEL);
  427. if (s->hash_context == NULL)
  428. {
  429. kfree(s->crc32_context);
  430. return XZ_MEMLIMIT_ERROR;
  431. }
  432. s->index.hash.hash_context = kmalloc(s->hash->contextsize,
  433. GFP_KERNEL);
  434. if (s->index.hash.hash_context == NULL)
  435. {
  436. kfree(s->hash_context);
  437. kfree(s->crc32_context);
  438. return XZ_MEMLIMIT_ERROR;
  439. }
  440. s->block.hash.hash_context = kmalloc(s->hash->contextsize, GFP_KERNEL);
  441. if (s->block.hash.hash_context == NULL)
  442. {
  443. kfree(s->index.hash.hash_context);
  444. kfree(s->hash_context);
  445. kfree(s->crc32_context);
  446. return XZ_MEMLIMIT_ERROR;
  447. }
  448. s->hash->init(s->hash_context);
  449. s->hash->init(s->index.hash.hash_context);
  450. s->hash->init(s->block.hash.hash_context);
  451. }
  452. #endif
  453. }
  454. else
  455. {
  456. #ifndef GRUB_EMBED_DECOMPRESSOR
  457. s->hash = 0;
  458. #endif
  459. s->hash_size = 0;
  460. }
  461. s->have_hash_value = 0;
  462. return XZ_OK;
  463. }
  464. /* Decode the Stream Footer field (the last 12 bytes of the .xz Stream) */
  465. static enum xz_ret dec_stream_footer(struct xz_dec *s)
  466. {
  467. if (! memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE))
  468. return XZ_DATA_ERROR;
  469. #ifndef GRUB_EMBED_DECOMPRESSOR
  470. if (s->crc32)
  471. {
  472. uint8_t readhash[4];
  473. uint8_t computed_hash[4];
  474. if (4 != s->crc32->mdlen)
  475. return XZ_DATA_ERROR;
  476. grub_crypto_hash (s->crc32, computed_hash,
  477. s->temp.buf + 4, 6);
  478. readhash[0] = s->temp.buf[3];
  479. readhash[1] = s->temp.buf[2];
  480. readhash[2] = s->temp.buf[1];
  481. readhash[3] = s->temp.buf[0];
  482. if(grub_memcmp (readhash, computed_hash,
  483. s->crc32->mdlen) != 0)
  484. return XZ_DATA_ERROR;
  485. }
  486. #endif
  487. /*
  488. * Validate Backward Size. Note that we never added the size of the
  489. * Index CRC32 field to s->index.size, thus we use s->index.size / 4
  490. * instead of s->index.size / 4 - 1.
  491. */
  492. if ((s->index.size >> 2) != get_le32(s->temp.buf + 4))
  493. return XZ_DATA_ERROR;
  494. #ifndef GRUB_EMBED_DECOMPRESSOR
  495. if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->hash_id)
  496. return XZ_DATA_ERROR;
  497. #endif
  498. /*
  499. * Use XZ_STREAM_END instead of XZ_OK to be more convenient
  500. * for the caller.
  501. */
  502. return XZ_STREAM_END;
  503. }
  504. /* Decode the Block Header and initialize the filter chain. */
  505. static enum xz_ret dec_block_header(struct xz_dec *s)
  506. {
  507. enum xz_ret ret;
  508. /*
  509. * Validate the CRC32. We know that the temp buffer is at least
  510. * eight bytes so this is safe.
  511. */
  512. s->temp.size -= 4;
  513. #ifndef GRUB_EMBED_DECOMPRESSOR
  514. if (s->crc32)
  515. {
  516. uint8_t readhash[4], computed_hash[4];
  517. if(4 != s->crc32->mdlen)
  518. return XZ_DATA_ERROR;
  519. grub_crypto_hash (s->crc32, computed_hash,
  520. s->temp.buf, s->temp.size);
  521. readhash[3] = s->temp.buf[s->temp.size];
  522. readhash[2] = s->temp.buf[s->temp.size + 1];
  523. readhash[1] = s->temp.buf[s->temp.size + 2];
  524. readhash[0] = s->temp.buf[s->temp.size + 3];
  525. if(grub_memcmp (readhash, computed_hash,
  526. s->crc32->mdlen) != 0)
  527. return XZ_DATA_ERROR;
  528. }
  529. #endif
  530. s->temp.pos = 2;
  531. /*
  532. * Catch unsupported Block Flags. We support only one or two filters
  533. * in the chain, so we catch that with the same test.
  534. */
  535. #ifdef XZ_DEC_BCJ
  536. if (s->temp.buf[1] & 0x3E)
  537. #else
  538. if (s->temp.buf[1] & 0x3F)
  539. #endif
  540. return XZ_OPTIONS_ERROR;
  541. /* Compressed Size */
  542. if (s->temp.buf[1] & 0x40) {
  543. if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
  544. != XZ_STREAM_END)
  545. return XZ_DATA_ERROR;
  546. s->block_header.compressed = s->vli;
  547. } else {
  548. s->block_header.compressed = VLI_UNKNOWN;
  549. }
  550. /* Uncompressed Size */
  551. if (s->temp.buf[1] & 0x80) {
  552. if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
  553. != XZ_STREAM_END)
  554. return XZ_DATA_ERROR;
  555. s->block_header.uncompressed = s->vli;
  556. } else {
  557. s->block_header.uncompressed = VLI_UNKNOWN;
  558. }
  559. #ifdef XZ_DEC_BCJ
  560. /* If there are two filters, the first one must be a BCJ filter. */
  561. s->bcj_active = s->temp.buf[1] & 0x01;
  562. if (s->bcj_active) {
  563. if (s->temp.size - s->temp.pos < 2)
  564. return XZ_OPTIONS_ERROR;
  565. ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]);
  566. if (ret != XZ_OK)
  567. return ret;
  568. /*
  569. * We don't support custom start offset,
  570. * so Size of Properties must be zero.
  571. */
  572. if (s->temp.buf[s->temp.pos++] != 0x00)
  573. return XZ_OPTIONS_ERROR;
  574. }
  575. #endif
  576. /* Valid Filter Flags always take at least two bytes. */
  577. if (s->temp.size - s->temp.pos < 2)
  578. return XZ_DATA_ERROR;
  579. /* Filter ID = LZMA2 */
  580. if (s->temp.buf[s->temp.pos++] != 0x21)
  581. return XZ_OPTIONS_ERROR;
  582. /* Size of Properties = 1-byte Filter Properties */
  583. if (s->temp.buf[s->temp.pos++] != 0x01)
  584. return XZ_OPTIONS_ERROR;
  585. /* Filter Properties contains LZMA2 dictionary size. */
  586. if (s->temp.size - s->temp.pos < 1)
  587. return XZ_DATA_ERROR;
  588. ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]);
  589. if (ret != XZ_OK)
  590. return ret;
  591. /* The rest must be Header Padding. */
  592. while (s->temp.pos < s->temp.size)
  593. if (s->temp.buf[s->temp.pos++] != 0x00)
  594. return XZ_OPTIONS_ERROR;
  595. s->temp.pos = 0;
  596. s->block.compressed = 0;
  597. s->block.uncompressed = 0;
  598. return XZ_OK;
  599. }
  600. static enum xz_ret dec_main(struct xz_dec *s, struct xz_buf *b)
  601. {
  602. enum xz_ret ret;
  603. /*
  604. * Store the start position for the case when we are in the middle
  605. * of the Index field.
  606. */
  607. s->in_start = b->in_pos;
  608. while (true) {
  609. switch (s->sequence) {
  610. case SEQ_STREAM_HEADER:
  611. /*
  612. * Stream Header is copied to s->temp, and then
  613. * decoded from there. This way if the caller
  614. * gives us only little input at a time, we can
  615. * still keep the Stream Header decoding code
  616. * simple. Similar approach is used in many places
  617. * in this file.
  618. */
  619. if (!fill_temp(s, b))
  620. return XZ_OK;
  621. ret = dec_stream_header(s);
  622. if (ret != XZ_OK)
  623. return ret;
  624. s->sequence = SEQ_BLOCK_START;
  625. /* FALLTHROUGH */
  626. case SEQ_BLOCK_START:
  627. /* We need one byte of input to continue. */
  628. if (b->in_pos == b->in_size)
  629. return XZ_OK;
  630. /* See if this is the beginning of the Index field. */
  631. if (b->in[b->in_pos] == 0) {
  632. s->in_start = b->in_pos++;
  633. s->sequence = SEQ_INDEX;
  634. break;
  635. }
  636. /*
  637. * Calculate the size of the Block Header and
  638. * prepare to decode it.
  639. */
  640. s->block_header.size
  641. = ((uint32_t)b->in[b->in_pos] + 1) * 4;
  642. s->temp.size = s->block_header.size;
  643. s->temp.pos = 0;
  644. s->sequence = SEQ_BLOCK_HEADER;
  645. /* FALLTHROUGH */
  646. case SEQ_BLOCK_HEADER:
  647. if (!fill_temp(s, b))
  648. return XZ_OK;
  649. ret = dec_block_header(s);
  650. if (ret != XZ_OK)
  651. return ret;
  652. s->sequence = SEQ_BLOCK_UNCOMPRESS;
  653. /* FALLTHROUGH */
  654. case SEQ_BLOCK_UNCOMPRESS:
  655. ret = dec_block(s, b);
  656. if (ret != XZ_STREAM_END)
  657. return ret;
  658. s->sequence = SEQ_BLOCK_PADDING;
  659. case SEQ_BLOCK_PADDING:
  660. /*
  661. * Size of Compressed Data + Block Padding
  662. * must be a multiple of four. We don't need
  663. * s->block.compressed for anything else
  664. * anymore, so we use it here to test the size
  665. * of the Block Padding field.
  666. */
  667. while (s->block.compressed & 3) {
  668. if (b->in_pos == b->in_size)
  669. return XZ_OK;
  670. if (b->in[b->in_pos++] != 0)
  671. return XZ_DATA_ERROR;
  672. ++s->block.compressed;
  673. }
  674. s->sequence = SEQ_BLOCK_CHECK;
  675. /* FALLTHROUGH */
  676. case SEQ_BLOCK_CHECK:
  677. ret = hash_validate(s, b, 0);
  678. if (ret != XZ_STREAM_END)
  679. return ret;
  680. s->sequence = SEQ_BLOCK_START;
  681. break;
  682. case SEQ_INDEX:
  683. ret = dec_index(s, b);
  684. if (ret != XZ_STREAM_END)
  685. return ret;
  686. s->sequence = SEQ_INDEX_PADDING;
  687. case SEQ_INDEX_PADDING:
  688. while ((s->index.size + (b->in_pos - s->in_start))
  689. & 3) {
  690. if (b->in_pos == b->in_size) {
  691. index_update(s, b);
  692. return XZ_OK;
  693. }
  694. if (b->in[b->in_pos++] != 0)
  695. return XZ_DATA_ERROR;
  696. }
  697. /* Finish the CRC32 value and Index size. */
  698. index_update(s, b);
  699. #ifndef GRUB_EMBED_DECOMPRESSOR
  700. if (s->hash)
  701. {
  702. /* Compare the hashes to validate the Index field. */
  703. s->hash->final(s->block.hash.hash_context);
  704. s->hash->final(s->index.hash.hash_context);
  705. if (s->block.hash.unpadded != s->index.hash.unpadded
  706. || s->block.hash.uncompressed != s->index.hash.uncompressed
  707. || grub_memcmp (s->hash->read(s->block.hash.hash_context),
  708. s->hash->read(s->index.hash.hash_context),
  709. s->hash->mdlen) != 0)
  710. return XZ_DATA_ERROR;
  711. }
  712. #endif
  713. s->sequence = SEQ_INDEX_CRC32;
  714. /* FALLTHROUGH */
  715. case SEQ_INDEX_CRC32:
  716. ret = hash_validate(s, b, 1);
  717. if (ret != XZ_STREAM_END)
  718. return ret;
  719. s->temp.size = STREAM_HEADER_SIZE;
  720. s->sequence = SEQ_STREAM_FOOTER;
  721. /* FALLTHROUGH */
  722. case SEQ_STREAM_FOOTER:
  723. if (!fill_temp(s, b))
  724. return XZ_OK;
  725. return dec_stream_footer(s);
  726. }
  727. }
  728. /* Never reached */
  729. }
  730. /*
  731. * xz_dec_run() is a wrapper for dec_main() to handle some special cases in
  732. * multi-call and single-call decoding.
  733. *
  734. * In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we
  735. * are not going to make any progress anymore. This is to prevent the caller
  736. * from calling us infinitely when the input file is truncated or otherwise
  737. * corrupt. Since zlib-style API allows that the caller fills the input buffer
  738. * only when the decoder doesn't produce any new output, we have to be careful
  739. * to avoid returning XZ_BUF_ERROR too easily: XZ_BUF_ERROR is returned only
  740. * after the second consecutive call to xz_dec_run() that makes no progress.
  741. *
  742. * In single-call mode, if we couldn't decode everything and no error
  743. * occurred, either the input is truncated or the output buffer is too small.
  744. * Since we know that the last input byte never produces any output, we know
  745. * that if all the input was consumed and decoding wasn't finished, the file
  746. * must be corrupt. Otherwise the output buffer has to be too small or the
  747. * file is corrupt in a way that decoding it produces too big output.
  748. *
  749. * If single-call decoding fails, we reset b->in_pos and b->out_pos back to
  750. * their original values. This is because with some filter chains there won't
  751. * be any valid uncompressed data in the output buffer unless the decoding
  752. * actually succeeds (that's the price to pay of using the output buffer as
  753. * the workspace).
  754. */
  755. enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b)
  756. {
  757. size_t in_start;
  758. size_t out_start;
  759. enum xz_ret ret;
  760. if (s->single_call)
  761. xz_dec_reset(s);
  762. in_start = b->in_pos;
  763. out_start = b->out_pos;
  764. ret = dec_main(s, b);
  765. if (s->single_call) {
  766. if (ret == XZ_OK)
  767. ret = b->in_pos == b->in_size
  768. ? XZ_DATA_ERROR : XZ_BUF_ERROR;
  769. if (ret != XZ_STREAM_END) {
  770. b->in_pos = in_start;
  771. b->out_pos = out_start;
  772. }
  773. } else if (ret == XZ_OK && in_start == b->in_pos
  774. && out_start == b->out_pos) {
  775. if (s->allow_buf_error)
  776. ret = XZ_BUF_ERROR;
  777. s->allow_buf_error = true;
  778. } else {
  779. s->allow_buf_error = false;
  780. }
  781. return ret;
  782. }
  783. #ifdef GRUB_EMBED_DECOMPRESSOR
  784. struct xz_dec decoder;
  785. #endif
  786. struct xz_dec * xz_dec_init(uint32_t dict_max)
  787. {
  788. struct xz_dec *s;
  789. #ifdef GRUB_EMBED_DECOMPRESSOR
  790. s = &decoder;
  791. #else
  792. s = kmalloc(sizeof(*s), GFP_KERNEL);
  793. if (s == NULL)
  794. return NULL;
  795. #endif
  796. memset (s, 0, sizeof (*s));
  797. s->single_call = dict_max == 0;
  798. #ifdef XZ_DEC_BCJ
  799. s->bcj = xz_dec_bcj_create(s->single_call);
  800. if (s->bcj == NULL)
  801. goto error_bcj;
  802. #endif
  803. s->lzma2 = xz_dec_lzma2_create(dict_max);
  804. if (s->lzma2 == NULL)
  805. goto error_lzma2;
  806. xz_dec_reset(s);
  807. return s;
  808. error_lzma2:
  809. #ifdef XZ_DEC_BCJ
  810. xz_dec_bcj_end(s->bcj);
  811. error_bcj:
  812. #endif
  813. #ifndef GRUB_EMBED_DECOMPRESSOR
  814. kfree(s);
  815. #endif
  816. return NULL;
  817. }
  818. void xz_dec_reset(struct xz_dec *s)
  819. {
  820. s->sequence = SEQ_STREAM_HEADER;
  821. s->allow_buf_error = false;
  822. s->pos = 0;
  823. {
  824. #ifndef GRUB_EMBED_DECOMPRESSOR
  825. uint64_t *t;
  826. t = s->block.hash.hash_context;
  827. #endif
  828. memzero(&s->block, sizeof(s->block));
  829. #ifndef GRUB_EMBED_DECOMPRESSOR
  830. s->block.hash.hash_context = t;
  831. t = s->index.hash.hash_context;
  832. #endif
  833. memzero(&s->index, sizeof(s->index));
  834. #ifndef GRUB_EMBED_DECOMPRESSOR
  835. s->index.hash.hash_context = t;
  836. #endif
  837. }
  838. s->temp.pos = 0;
  839. s->temp.size = STREAM_HEADER_SIZE;
  840. #ifndef GRUB_EMBED_DECOMPRESSOR
  841. if (s->hash)
  842. {
  843. s->hash->init(s->hash_context);
  844. s->hash->init(s->index.hash.hash_context);
  845. s->hash->init(s->block.hash.hash_context);
  846. }
  847. #endif
  848. s->have_hash_value = 0;
  849. }
  850. void xz_dec_end(struct xz_dec *s)
  851. {
  852. if (s != NULL) {
  853. xz_dec_lzma2_end(s->lzma2);
  854. #ifndef GRUB_EMBED_DECOMPRESSOR
  855. kfree(s->index.hash.hash_context);
  856. kfree(s->block.hash.hash_context);
  857. kfree(s->hash_context);
  858. kfree(s->crc32_context);
  859. #endif
  860. #ifdef XZ_DEC_BCJ
  861. xz_dec_bcj_end(s->bcj);
  862. #endif
  863. #ifndef GRUB_EMBED_DECOMPRESSOR
  864. kfree(s);
  865. #endif
  866. }
  867. }