index_hash.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file index_hash.c
  4. /// \brief Validates Index by using a hash function
  5. //
  6. // Author: Lasse Collin
  7. //
  8. // This file has been put into the public domain.
  9. // You can do whatever you want with this file.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "common.h"
  13. #include "index.h"
  14. #include "check.h"
  15. typedef struct {
  16. /// Sum of the Block sizes (including Block Padding)
  17. lzma_vli blocks_size;
  18. /// Sum of the Uncompressed Size fields
  19. lzma_vli uncompressed_size;
  20. /// Number of Records
  21. lzma_vli count;
  22. /// Size of the List of Index Records as bytes
  23. lzma_vli index_list_size;
  24. /// Check calculated from Unpadded Sizes and Uncompressed Sizes.
  25. lzma_check_state check;
  26. } lzma_index_hash_info;
  27. struct lzma_index_hash_s {
  28. enum {
  29. SEQ_BLOCK,
  30. SEQ_COUNT,
  31. SEQ_UNPADDED,
  32. SEQ_UNCOMPRESSED,
  33. SEQ_PADDING_INIT,
  34. SEQ_PADDING,
  35. SEQ_CRC32,
  36. } sequence;
  37. /// Information collected while decoding the actual Blocks.
  38. lzma_index_hash_info blocks;
  39. /// Information collected from the Index field.
  40. lzma_index_hash_info records;
  41. /// Number of Records not fully decoded
  42. lzma_vli remaining;
  43. /// Unpadded Size currently being read from an Index Record.
  44. lzma_vli unpadded_size;
  45. /// Uncompressed Size currently being read from an Index Record.
  46. lzma_vli uncompressed_size;
  47. /// Position in variable-length integers when decoding them from
  48. /// the List of Records.
  49. size_t pos;
  50. /// CRC32 of the Index
  51. uint32_t crc32;
  52. };
  53. extern LZMA_API(lzma_index_hash *)
  54. lzma_index_hash_init(lzma_index_hash *index_hash,
  55. const lzma_allocator *allocator)
  56. {
  57. if (index_hash == NULL) {
  58. index_hash = lzma_alloc(sizeof(lzma_index_hash), allocator);
  59. if (index_hash == NULL)
  60. return NULL;
  61. }
  62. index_hash->sequence = SEQ_BLOCK;
  63. index_hash->blocks.blocks_size = 0;
  64. index_hash->blocks.uncompressed_size = 0;
  65. index_hash->blocks.count = 0;
  66. index_hash->blocks.index_list_size = 0;
  67. index_hash->records.blocks_size = 0;
  68. index_hash->records.uncompressed_size = 0;
  69. index_hash->records.count = 0;
  70. index_hash->records.index_list_size = 0;
  71. index_hash->unpadded_size = 0;
  72. index_hash->uncompressed_size = 0;
  73. index_hash->pos = 0;
  74. index_hash->crc32 = 0;
  75. // These cannot fail because LZMA_CHECK_BEST is known to be supported.
  76. (void)lzma_check_init(&index_hash->blocks.check, LZMA_CHECK_BEST);
  77. (void)lzma_check_init(&index_hash->records.check, LZMA_CHECK_BEST);
  78. return index_hash;
  79. }
  80. extern LZMA_API(void)
  81. lzma_index_hash_end(lzma_index_hash *index_hash,
  82. const lzma_allocator *allocator)
  83. {
  84. lzma_free(index_hash, allocator);
  85. return;
  86. }
  87. extern LZMA_API(lzma_vli)
  88. lzma_index_hash_size(const lzma_index_hash *index_hash)
  89. {
  90. // Get the size of the Index from ->blocks instead of ->records for
  91. // cases where application wants to know the Index Size before
  92. // decoding the Index.
  93. return index_size(index_hash->blocks.count,
  94. index_hash->blocks.index_list_size);
  95. }
  96. /// Updates the sizes and the hash without any validation.
  97. static void
  98. hash_append(lzma_index_hash_info *info, lzma_vli unpadded_size,
  99. lzma_vli uncompressed_size)
  100. {
  101. info->blocks_size += vli_ceil4(unpadded_size);
  102. info->uncompressed_size += uncompressed_size;
  103. info->index_list_size += lzma_vli_size(unpadded_size)
  104. + lzma_vli_size(uncompressed_size);
  105. ++info->count;
  106. const lzma_vli sizes[2] = { unpadded_size, uncompressed_size };
  107. lzma_check_update(&info->check, LZMA_CHECK_BEST,
  108. (const uint8_t *)(sizes), sizeof(sizes));
  109. return;
  110. }
  111. extern LZMA_API(lzma_ret)
  112. lzma_index_hash_append(lzma_index_hash *index_hash, lzma_vli unpadded_size,
  113. lzma_vli uncompressed_size)
  114. {
  115. // Validate the arguments.
  116. if (index_hash->sequence != SEQ_BLOCK
  117. || unpadded_size < UNPADDED_SIZE_MIN
  118. || unpadded_size > UNPADDED_SIZE_MAX
  119. || uncompressed_size > LZMA_VLI_MAX)
  120. return LZMA_PROG_ERROR;
  121. // Update the hash.
  122. hash_append(&index_hash->blocks, unpadded_size, uncompressed_size);
  123. // Validate the properties of *info are still in allowed limits.
  124. if (index_hash->blocks.blocks_size > LZMA_VLI_MAX
  125. || index_hash->blocks.uncompressed_size > LZMA_VLI_MAX
  126. || index_size(index_hash->blocks.count,
  127. index_hash->blocks.index_list_size)
  128. > LZMA_BACKWARD_SIZE_MAX
  129. || index_stream_size(index_hash->blocks.blocks_size,
  130. index_hash->blocks.count,
  131. index_hash->blocks.index_list_size)
  132. > LZMA_VLI_MAX)
  133. return LZMA_DATA_ERROR;
  134. return LZMA_OK;
  135. }
  136. extern LZMA_API(lzma_ret)
  137. lzma_index_hash_decode(lzma_index_hash *index_hash, const uint8_t *in,
  138. size_t *in_pos, size_t in_size)
  139. {
  140. // Catch zero input buffer here, because in contrast to Index encoder
  141. // and decoder functions, applications call this function directly
  142. // instead of via lzma_code(), which does the buffer checking.
  143. if (*in_pos >= in_size)
  144. return LZMA_BUF_ERROR;
  145. // NOTE: This function has many similarities to index_encode() and
  146. // index_decode() functions found from index_encoder.c and
  147. // index_decoder.c. See the comments especially in index_encoder.c.
  148. const size_t in_start = *in_pos;
  149. lzma_ret ret = LZMA_OK;
  150. while (*in_pos < in_size)
  151. switch (index_hash->sequence) {
  152. case SEQ_BLOCK:
  153. // Check the Index Indicator is present.
  154. if (in[(*in_pos)++] != 0x00)
  155. return LZMA_DATA_ERROR;
  156. index_hash->sequence = SEQ_COUNT;
  157. break;
  158. case SEQ_COUNT: {
  159. ret = lzma_vli_decode(&index_hash->remaining,
  160. &index_hash->pos, in, in_pos, in_size);
  161. if (ret != LZMA_STREAM_END)
  162. goto out;
  163. // The count must match the count of the Blocks decoded.
  164. if (index_hash->remaining != index_hash->blocks.count)
  165. return LZMA_DATA_ERROR;
  166. ret = LZMA_OK;
  167. index_hash->pos = 0;
  168. // Handle the special case when there are no Blocks.
  169. index_hash->sequence = index_hash->remaining == 0
  170. ? SEQ_PADDING_INIT : SEQ_UNPADDED;
  171. break;
  172. }
  173. case SEQ_UNPADDED:
  174. case SEQ_UNCOMPRESSED: {
  175. lzma_vli *size = index_hash->sequence == SEQ_UNPADDED
  176. ? &index_hash->unpadded_size
  177. : &index_hash->uncompressed_size;
  178. ret = lzma_vli_decode(size, &index_hash->pos,
  179. in, in_pos, in_size);
  180. if (ret != LZMA_STREAM_END)
  181. goto out;
  182. ret = LZMA_OK;
  183. index_hash->pos = 0;
  184. if (index_hash->sequence == SEQ_UNPADDED) {
  185. if (index_hash->unpadded_size < UNPADDED_SIZE_MIN
  186. || index_hash->unpadded_size
  187. > UNPADDED_SIZE_MAX)
  188. return LZMA_DATA_ERROR;
  189. index_hash->sequence = SEQ_UNCOMPRESSED;
  190. } else {
  191. // Update the hash.
  192. hash_append(&index_hash->records,
  193. index_hash->unpadded_size,
  194. index_hash->uncompressed_size);
  195. // Verify that we don't go over the known sizes. Note
  196. // that this validation is simpler than the one used
  197. // in lzma_index_hash_append(), because here we know
  198. // that values in index_hash->blocks are already
  199. // validated and we are fine as long as we don't
  200. // exceed them in index_hash->records.
  201. if (index_hash->blocks.blocks_size
  202. < index_hash->records.blocks_size
  203. || index_hash->blocks.uncompressed_size
  204. < index_hash->records.uncompressed_size
  205. || index_hash->blocks.index_list_size
  206. < index_hash->records.index_list_size)
  207. return LZMA_DATA_ERROR;
  208. // Check if this was the last Record.
  209. index_hash->sequence = --index_hash->remaining == 0
  210. ? SEQ_PADDING_INIT : SEQ_UNPADDED;
  211. }
  212. break;
  213. }
  214. case SEQ_PADDING_INIT:
  215. index_hash->pos = (LZMA_VLI_C(4) - index_size_unpadded(
  216. index_hash->records.count,
  217. index_hash->records.index_list_size)) & 3;
  218. index_hash->sequence = SEQ_PADDING;
  219. // Fall through
  220. case SEQ_PADDING:
  221. if (index_hash->pos > 0) {
  222. --index_hash->pos;
  223. if (in[(*in_pos)++] != 0x00)
  224. return LZMA_DATA_ERROR;
  225. break;
  226. }
  227. // Compare the sizes.
  228. if (index_hash->blocks.blocks_size
  229. != index_hash->records.blocks_size
  230. || index_hash->blocks.uncompressed_size
  231. != index_hash->records.uncompressed_size
  232. || index_hash->blocks.index_list_size
  233. != index_hash->records.index_list_size)
  234. return LZMA_DATA_ERROR;
  235. // Finish the hashes and compare them.
  236. lzma_check_finish(&index_hash->blocks.check, LZMA_CHECK_BEST);
  237. lzma_check_finish(&index_hash->records.check, LZMA_CHECK_BEST);
  238. if (memcmp(index_hash->blocks.check.buffer.u8,
  239. index_hash->records.check.buffer.u8,
  240. lzma_check_size(LZMA_CHECK_BEST)) != 0)
  241. return LZMA_DATA_ERROR;
  242. // Finish the CRC32 calculation.
  243. index_hash->crc32 = lzma_crc32(in + in_start,
  244. *in_pos - in_start, index_hash->crc32);
  245. index_hash->sequence = SEQ_CRC32;
  246. // Fall through
  247. case SEQ_CRC32:
  248. do {
  249. if (*in_pos == in_size)
  250. return LZMA_OK;
  251. if (((index_hash->crc32 >> (index_hash->pos * 8))
  252. & 0xFF) != in[(*in_pos)++]) {
  253. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  254. return LZMA_DATA_ERROR;
  255. #endif
  256. }
  257. } while (++index_hash->pos < 4);
  258. return LZMA_STREAM_END;
  259. default:
  260. assert(0);
  261. return LZMA_PROG_ERROR;
  262. }
  263. out:
  264. // Update the CRC32,
  265. index_hash->crc32 = lzma_crc32(in + in_start,
  266. *in_pos - in_start, index_hash->crc32);
  267. return ret;
  268. }