index_hash.c 8.9 KB

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