compress_lzma.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* Copyright (c) 2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file compress_lzma.c
  7. * \brief Compression backend for LZMA.
  8. *
  9. * This module should never be invoked directly. Use the compress module
  10. * instead.
  11. **/
  12. #include "orconfig.h"
  13. #include "lib/compress/compress.h"
  14. #include "lib/compress/compress_lzma.h"
  15. #include "lib/log/log.h"
  16. #include "lib/log/util_bug.h"
  17. #include "lib/malloc/malloc.h"
  18. #include "lib/thread/threads.h"
  19. #ifdef HAVE_LZMA
  20. #include <lzma.h>
  21. #endif
  22. /** The maximum amount of memory we allow the LZMA decoder to use, in bytes. */
  23. #define MEMORY_LIMIT (16 * 1024 * 1024)
  24. /** Total number of bytes allocated for LZMA state. */
  25. static atomic_counter_t total_lzma_allocation;
  26. #ifdef HAVE_LZMA
  27. /** Given <b>level</b> return the memory level. */
  28. static int
  29. memory_level(compression_level_t level)
  30. {
  31. switch (level) {
  32. default:
  33. case BEST_COMPRESSION:
  34. case HIGH_COMPRESSION: return 6;
  35. case MEDIUM_COMPRESSION: return 4;
  36. case LOW_COMPRESSION: return 2;
  37. }
  38. }
  39. /** Convert a given <b>error</b> to a human readable error string. */
  40. static const char *
  41. lzma_error_str(lzma_ret error)
  42. {
  43. switch (error) {
  44. case LZMA_OK:
  45. return "Operation completed successfully";
  46. case LZMA_STREAM_END:
  47. return "End of stream";
  48. case LZMA_NO_CHECK:
  49. return "Input stream lacks integrity check";
  50. case LZMA_UNSUPPORTED_CHECK:
  51. return "Unable to calculate integrity check";
  52. case LZMA_GET_CHECK:
  53. return "Integrity check available";
  54. case LZMA_MEM_ERROR:
  55. return "Unable to allocate memory";
  56. case LZMA_MEMLIMIT_ERROR:
  57. return "Memory limit reached";
  58. case LZMA_FORMAT_ERROR:
  59. return "Unknown file format";
  60. case LZMA_OPTIONS_ERROR:
  61. return "Unsupported options";
  62. case LZMA_DATA_ERROR:
  63. return "Corrupt input data";
  64. case LZMA_BUF_ERROR:
  65. return "Unable to progress";
  66. case LZMA_PROG_ERROR:
  67. return "Programming error";
  68. default:
  69. return "Unknown LZMA error";
  70. }
  71. }
  72. #endif /* defined(HAVE_LZMA) */
  73. /** Return 1 if LZMA compression is supported; otherwise 0. */
  74. int
  75. tor_lzma_method_supported(void)
  76. {
  77. #ifdef HAVE_LZMA
  78. return 1;
  79. #else
  80. return 0;
  81. #endif
  82. }
  83. /** Return a string representation of the version of the currently running
  84. * version of liblzma. Returns NULL if LZMA is unsupported. */
  85. const char *
  86. tor_lzma_get_version_str(void)
  87. {
  88. #ifdef HAVE_LZMA
  89. return lzma_version_string();
  90. #else
  91. return NULL;
  92. #endif
  93. }
  94. /** Return a string representation of the version of liblzma used at
  95. * compilation time. Returns NULL if LZMA is unsupported. */
  96. const char *
  97. tor_lzma_get_header_version_str(void)
  98. {
  99. #ifdef HAVE_LZMA
  100. return LZMA_VERSION_STRING;
  101. #else
  102. return NULL;
  103. #endif
  104. }
  105. /** Internal LZMA state for incremental compression/decompression.
  106. * The body of this struct is not exposed. */
  107. struct tor_lzma_compress_state_t {
  108. #ifdef HAVE_LZMA
  109. lzma_stream stream; /**< The LZMA stream. */
  110. #endif
  111. int compress; /**< True if we are compressing; false if we are inflating */
  112. /** Number of bytes read so far. Used to detect compression bombs. */
  113. size_t input_so_far;
  114. /** Number of bytes written so far. Used to detect compression bombs. */
  115. size_t output_so_far;
  116. /** Approximate number of bytes allocated for this object. */
  117. size_t allocation;
  118. };
  119. #ifdef HAVE_LZMA
  120. /** Return an approximate number of bytes stored in memory to hold the LZMA
  121. * encoder/decoder state. */
  122. static size_t
  123. tor_lzma_state_size_precalc(int compress, compression_level_t level)
  124. {
  125. uint64_t memory_usage;
  126. if (compress)
  127. memory_usage = lzma_easy_encoder_memusage(memory_level(level));
  128. else
  129. memory_usage = lzma_easy_decoder_memusage(memory_level(level));
  130. if (memory_usage == UINT64_MAX) {
  131. // LCOV_EXCL_START
  132. log_warn(LD_GENERAL, "Unsupported compression level passed to LZMA %s",
  133. compress ? "encoder" : "decoder");
  134. goto err;
  135. // LCOV_EXCL_STOP
  136. }
  137. if (memory_usage + sizeof(tor_lzma_compress_state_t) > SIZE_MAX)
  138. memory_usage = SIZE_MAX;
  139. else
  140. memory_usage += sizeof(tor_lzma_compress_state_t);
  141. return (size_t)memory_usage;
  142. // LCOV_EXCL_START
  143. err:
  144. return 0;
  145. // LCOV_EXCL_STOP
  146. }
  147. #endif /* defined(HAVE_LZMA) */
  148. /** Construct and return a tor_lzma_compress_state_t object using
  149. * <b>method</b>. If <b>compress</b>, it's for compression; otherwise it's for
  150. * decompression. */
  151. tor_lzma_compress_state_t *
  152. tor_lzma_compress_new(int compress,
  153. compress_method_t method,
  154. compression_level_t level)
  155. {
  156. tor_assert(method == LZMA_METHOD);
  157. #ifdef HAVE_LZMA
  158. tor_lzma_compress_state_t *result;
  159. lzma_ret retval;
  160. lzma_options_lzma stream_options;
  161. // Note that we do not explicitly initialize the lzma_stream object here,
  162. // since the LZMA_STREAM_INIT "just" initializes all members to 0, which is
  163. // also what `tor_malloc_zero()` does.
  164. result = tor_malloc_zero(sizeof(tor_lzma_compress_state_t));
  165. result->compress = compress;
  166. result->allocation = tor_lzma_state_size_precalc(compress, level);
  167. if (compress) {
  168. lzma_lzma_preset(&stream_options, memory_level(level));
  169. retval = lzma_alone_encoder(&result->stream, &stream_options);
  170. if (retval != LZMA_OK) {
  171. // LCOV_EXCL_START
  172. log_warn(LD_GENERAL, "Error from LZMA encoder: %s (%u).",
  173. lzma_error_str(retval), retval);
  174. goto err;
  175. // LCOV_EXCL_STOP
  176. }
  177. } else {
  178. retval = lzma_alone_decoder(&result->stream, MEMORY_LIMIT);
  179. if (retval != LZMA_OK) {
  180. // LCOV_EXCL_START
  181. log_warn(LD_GENERAL, "Error from LZMA decoder: %s (%u).",
  182. lzma_error_str(retval), retval);
  183. goto err;
  184. // LCOV_EXCL_STOP
  185. }
  186. }
  187. atomic_counter_add(&total_lzma_allocation, result->allocation);
  188. return result;
  189. /* LCOV_EXCL_START */
  190. err:
  191. tor_free(result);
  192. return NULL;
  193. /* LCOV_EXCL_STOP */
  194. #else /* !(defined(HAVE_LZMA)) */
  195. (void)compress;
  196. (void)method;
  197. (void)level;
  198. return NULL;
  199. #endif /* defined(HAVE_LZMA) */
  200. }
  201. /** Compress/decompress some bytes using <b>state</b>. Read up to
  202. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  203. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  204. * we've reached the end of the input.
  205. *
  206. * Return TOR_COMPRESS_DONE if we've finished the entire
  207. * compression/decompression.
  208. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  209. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  210. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  211. */
  212. tor_compress_output_t
  213. tor_lzma_compress_process(tor_lzma_compress_state_t *state,
  214. char **out, size_t *out_len,
  215. const char **in, size_t *in_len,
  216. int finish)
  217. {
  218. #ifdef HAVE_LZMA
  219. lzma_ret retval;
  220. lzma_action action;
  221. tor_assert(state != NULL);
  222. tor_assert(*in_len <= UINT_MAX);
  223. tor_assert(*out_len <= UINT_MAX);
  224. state->stream.next_in = (unsigned char *)*in;
  225. state->stream.avail_in = *in_len;
  226. state->stream.next_out = (unsigned char *)*out;
  227. state->stream.avail_out = *out_len;
  228. action = finish ? LZMA_FINISH : LZMA_RUN;
  229. retval = lzma_code(&state->stream, action);
  230. state->input_so_far += state->stream.next_in - ((unsigned char *)*in);
  231. state->output_so_far += state->stream.next_out - ((unsigned char *)*out);
  232. *out = (char *)state->stream.next_out;
  233. *out_len = state->stream.avail_out;
  234. *in = (const char *)state->stream.next_in;
  235. *in_len = state->stream.avail_in;
  236. if (! state->compress &&
  237. tor_compress_is_compression_bomb(state->input_so_far,
  238. state->output_so_far)) {
  239. log_warn(LD_DIR, "Possible compression bomb; abandoning stream.");
  240. return TOR_COMPRESS_ERROR;
  241. }
  242. switch (retval) {
  243. case LZMA_OK:
  244. if (state->stream.avail_out == 0 || finish)
  245. return TOR_COMPRESS_BUFFER_FULL;
  246. return TOR_COMPRESS_OK;
  247. case LZMA_BUF_ERROR:
  248. if (state->stream.avail_in == 0 && !finish)
  249. return TOR_COMPRESS_OK;
  250. return TOR_COMPRESS_BUFFER_FULL;
  251. case LZMA_STREAM_END:
  252. return TOR_COMPRESS_DONE;
  253. // We list all the possible values of `lzma_ret` here to silence the
  254. // `switch-enum` warning and to detect if a new member was added.
  255. case LZMA_NO_CHECK:
  256. case LZMA_UNSUPPORTED_CHECK:
  257. case LZMA_GET_CHECK:
  258. case LZMA_MEM_ERROR:
  259. case LZMA_MEMLIMIT_ERROR:
  260. case LZMA_FORMAT_ERROR:
  261. case LZMA_OPTIONS_ERROR:
  262. case LZMA_DATA_ERROR:
  263. case LZMA_PROG_ERROR:
  264. default:
  265. log_warn(LD_GENERAL, "LZMA %s didn't finish: %s.",
  266. state->compress ? "compression" : "decompression",
  267. lzma_error_str(retval));
  268. return TOR_COMPRESS_ERROR;
  269. }
  270. #else /* !(defined(HAVE_LZMA)) */
  271. (void)state;
  272. (void)out;
  273. (void)out_len;
  274. (void)in;
  275. (void)in_len;
  276. (void)finish;
  277. return TOR_COMPRESS_ERROR;
  278. #endif /* defined(HAVE_LZMA) */
  279. }
  280. /** Deallocate <b>state</b>. */
  281. void
  282. tor_lzma_compress_free_(tor_lzma_compress_state_t *state)
  283. {
  284. if (state == NULL)
  285. return;
  286. atomic_counter_sub(&total_lzma_allocation, state->allocation);
  287. #ifdef HAVE_LZMA
  288. lzma_end(&state->stream);
  289. #endif
  290. tor_free(state);
  291. }
  292. /** Return the approximate number of bytes allocated for <b>state</b>. */
  293. size_t
  294. tor_lzma_compress_state_size(const tor_lzma_compress_state_t *state)
  295. {
  296. tor_assert(state != NULL);
  297. return state->allocation;
  298. }
  299. /** Return the approximate number of bytes allocated for all LZMA states. */
  300. size_t
  301. tor_lzma_get_total_allocation(void)
  302. {
  303. return atomic_counter_get(&total_lzma_allocation);
  304. }
  305. /** Initialize the lzma module */
  306. void
  307. tor_lzma_init(void)
  308. {
  309. atomic_counter_init(&total_lzma_allocation);
  310. }