compress_lzma.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /* Copyright (c) 2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, 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 "util.h"
  14. #include "torlog.h"
  15. #include "compress.h"
  16. #include "compress_lzma.h"
  17. #ifdef HAVE_LZMA
  18. #include <lzma.h>
  19. #endif
  20. /** Total number of bytes allocated for LZMA state. */
  21. static size_t total_lzma_allocation = 0;
  22. #ifdef HAVE_LZMA
  23. /** Convert a given <b>error</b> to a human readable error string. */
  24. static const char *
  25. lzma_error_str(lzma_ret error)
  26. {
  27. switch (error) {
  28. case LZMA_OK:
  29. return "Operation completed successfully";
  30. case LZMA_STREAM_END:
  31. return "End of stream";
  32. case LZMA_NO_CHECK:
  33. return "Input stream lacks integrity check";
  34. case LZMA_UNSUPPORTED_CHECK:
  35. return "Unable to calculate integrity check";
  36. case LZMA_GET_CHECK:
  37. return "Integrity check available";
  38. case LZMA_MEM_ERROR:
  39. return "Unable to allocate memory";
  40. case LZMA_MEMLIMIT_ERROR:
  41. return "Memory limit reached";
  42. case LZMA_FORMAT_ERROR:
  43. return "Unknown file format";
  44. case LZMA_OPTIONS_ERROR:
  45. return "Unsupported options";
  46. case LZMA_DATA_ERROR:
  47. return "Corrupt input data";
  48. case LZMA_BUF_ERROR:
  49. return "Unable to progress";
  50. case LZMA_PROG_ERROR:
  51. return "Programming error";
  52. default:
  53. return "Unknown LZMA error";
  54. }
  55. }
  56. #endif // HAVE_LZMA.
  57. /** Return a string representation of the version of the currently running
  58. * version of liblzma. */
  59. const char *
  60. tor_lzma_get_version_str(void)
  61. {
  62. #ifdef HAVE_LZMA
  63. return lzma_version_string();
  64. #else
  65. return "N/A";
  66. #endif
  67. }
  68. /** Return a string representation of the version of the version of liblzma
  69. * used at compilation. */
  70. const char *
  71. tor_lzma_get_header_version_str(void)
  72. {
  73. #ifdef HAVE_LZMA
  74. return LZMA_VERSION_STRING;
  75. #else
  76. return "N/A";
  77. #endif
  78. }
  79. /** Given <b>in_len</b> bytes at <b>in</b>, compress them into a newly
  80. * allocated buffer, using the LZMA method. Store the compressed string in
  81. * *<b>out</b>, and its length in *<b>out_len</b>. Return 0 on success, -1 on
  82. * failure.
  83. */
  84. int
  85. tor_lzma_compress(char **out, size_t *out_len,
  86. const char *in, size_t in_len,
  87. compress_method_t method)
  88. {
  89. #ifdef HAVE_LZMA
  90. lzma_stream stream = LZMA_STREAM_INIT;
  91. lzma_options_lzma stream_options;
  92. lzma_ret retval;
  93. lzma_action action;
  94. size_t out_size, old_size;
  95. off_t offset;
  96. tor_assert(out);
  97. tor_assert(out_len);
  98. tor_assert(in);
  99. tor_assert(in_len < UINT_MAX);
  100. tor_assert(method == LZMA_METHOD);
  101. stream.next_in = (unsigned char *)in;
  102. stream.avail_in = in_len;
  103. lzma_lzma_preset(&stream_options,
  104. tor_compress_memory_level(HIGH_COMPRESSION));
  105. retval = lzma_alone_encoder(&stream, &stream_options);
  106. if (retval != LZMA_OK) {
  107. log_warn(LD_GENERAL, "Error from LZMA encoder: %s (%u).",
  108. lzma_error_str(retval), retval);
  109. goto err;
  110. }
  111. out_size = in_len / 2;
  112. if (out_size < 1024)
  113. out_size = 1024;
  114. *out = tor_malloc(out_size);
  115. stream.next_out = (unsigned char *)*out;
  116. stream.avail_out = out_size;
  117. action = LZMA_RUN;
  118. while (1) {
  119. retval = lzma_code(&stream, action);
  120. switch (retval) {
  121. case LZMA_OK:
  122. action = LZMA_FINISH;
  123. break;
  124. case LZMA_STREAM_END:
  125. goto done;
  126. case LZMA_BUF_ERROR:
  127. offset = stream.next_out - ((unsigned char *)*out);
  128. old_size = out_size;
  129. out_size *= 2;
  130. if (out_size < old_size) {
  131. log_warn(LD_GENERAL, "Size overflow in LZMA compression.");
  132. goto err;
  133. }
  134. *out = tor_realloc(*out, out_size);
  135. stream.next_out = (unsigned char *)(*out + offset);
  136. if (out_size - offset > UINT_MAX) {
  137. log_warn(LD_BUG, "Ran over unsigned int limit of LZMA while "
  138. "compressing.");
  139. goto err;
  140. }
  141. stream.avail_out = (unsigned int)(out_size - offset);
  142. break;
  143. // We list all the possible values of `lzma_ret` here to silence the
  144. // `switch-enum` warning and to detect if a new member was added.
  145. case LZMA_NO_CHECK:
  146. case LZMA_UNSUPPORTED_CHECK:
  147. case LZMA_GET_CHECK:
  148. case LZMA_MEM_ERROR:
  149. case LZMA_MEMLIMIT_ERROR:
  150. case LZMA_FORMAT_ERROR:
  151. case LZMA_OPTIONS_ERROR:
  152. case LZMA_DATA_ERROR:
  153. case LZMA_PROG_ERROR:
  154. default:
  155. log_warn(LD_GENERAL, "LZMA compression didn't finish: %s.",
  156. lzma_error_str(retval));
  157. goto err;
  158. }
  159. }
  160. done:
  161. *out_len = stream.total_out;
  162. lzma_end(&stream);
  163. if (tor_compress_is_compression_bomb(*out_len, in_len)) {
  164. log_warn(LD_BUG, "We compressed something and got an insanely high "
  165. "compression factor; other Tor instances would think "
  166. "this is a compression bomb.");
  167. goto err;
  168. }
  169. return 0;
  170. err:
  171. lzma_end(&stream);
  172. tor_free(*out);
  173. return -1;
  174. #else // HAVE_LZMA.
  175. (void)out;
  176. (void)out_len;
  177. (void)in;
  178. (void)in_len;
  179. (void)method;
  180. return -1;
  181. #endif // HAVE_LZMA.
  182. }
  183. /** Given an LZMA compressed string of total length <b>in_len</b> bytes at
  184. * <b>in</b>, uncompress them into a newly allocated buffer. Store the
  185. * uncompressed string in *<b>out</b>, and its length in *<b>out_len</b>.
  186. * Return 0 on success, -1 on failure.
  187. *
  188. * If <b>complete_only</b> is true, we consider a truncated input as a failure;
  189. * otherwise we decompress as much as we can. Warn about truncated or corrupt
  190. * inputs at <b>protocol_warn_level</b>.
  191. */
  192. int
  193. tor_lzma_uncompress(char **out, size_t *out_len,
  194. const char *in, size_t in_len,
  195. compress_method_t method,
  196. int complete_only,
  197. int protocol_warn_level)
  198. {
  199. #ifdef HAVE_LZMA
  200. lzma_stream stream = LZMA_STREAM_INIT;
  201. lzma_ret retval;
  202. lzma_action action;
  203. size_t out_size, old_size;
  204. off_t offset;
  205. tor_assert(out);
  206. tor_assert(out_len);
  207. tor_assert(in);
  208. tor_assert(in_len < UINT_MAX);
  209. tor_assert(method == LZMA_METHOD);
  210. stream.next_in = (unsigned char *)in;
  211. stream.avail_in = in_len;
  212. // FIXME(ahf): This should be something more sensible than
  213. // UINT64_MAX: See #21665.
  214. retval = lzma_alone_decoder(&stream, UINT64_MAX);
  215. if (retval != LZMA_OK) {
  216. log_warn(LD_GENERAL, "Error from LZMA decoder: %s (%u).",
  217. lzma_error_str(retval), retval);
  218. goto err;
  219. }
  220. out_size = in_len * 2;
  221. if (out_size < 1024)
  222. out_size = 1024;
  223. if (out_size >= SIZE_T_CEILING || out_size > UINT_MAX)
  224. goto err;
  225. *out = tor_malloc(out_size);
  226. stream.next_out = (unsigned char *)*out;
  227. stream.avail_out = out_size;
  228. // FIXME(ahf): We should figure out how to use LZMA_FULL_FLUSH to
  229. // make the partial string read tests.
  230. // action = complete_only ? LZMA_FINISH : LZMA_SYNC_FLUSH. // To do this,
  231. // it seems like we have to use LZMA using their "xz" encoder instead of just
  232. // regular LZMA.
  233. (void)complete_only;
  234. action = LZMA_FINISH;
  235. while (1) {
  236. retval = lzma_code(&stream, action);
  237. switch (retval) {
  238. case LZMA_STREAM_END:
  239. if (stream.avail_in == 0)
  240. goto done;
  241. // We might have more data here. Reset our stream.
  242. lzma_end(&stream);
  243. retval = lzma_alone_decoder(&stream, UINT64_MAX);
  244. if (retval != LZMA_OK) {
  245. log_warn(LD_GENERAL, "Error from LZMA decoder: %s (%u).",
  246. lzma_error_str(retval), retval);
  247. goto err;
  248. }
  249. break;
  250. case LZMA_OK:
  251. break;
  252. case LZMA_BUF_ERROR:
  253. if (stream.avail_out > 0) {
  254. log_fn(protocol_warn_level, LD_PROTOCOL,
  255. "possible truncated or corrupt LZMA data.");
  256. goto err;
  257. }
  258. offset = stream.next_out - (unsigned char *)*out;
  259. old_size = out_size;
  260. out_size *= 2;
  261. if (out_size < old_size) {
  262. log_warn(LD_GENERAL, "Size overflow in LZMA uncompression.");
  263. goto err;
  264. }
  265. if (tor_compress_is_compression_bomb(in_len, out_size)) {
  266. log_warn(LD_GENERAL, "Input looks like a possible LZMA compression "
  267. "bomb. Not proceeding.");
  268. goto err;
  269. }
  270. if (out_size >= SIZE_T_CEILING) {
  271. log_warn(LD_BUG, "Hit SIZE_T_CEILING limit while uncompressing "
  272. "LZMA data.");
  273. goto err;
  274. }
  275. *out = tor_realloc(*out, out_size);
  276. stream.next_out = (unsigned char *)(*out + offset);
  277. if (out_size - offset > UINT_MAX) {
  278. log_warn(LD_BUG, "Ran over unsigned int limit of LZMA while "
  279. "uncompressing.");
  280. goto err;
  281. }
  282. stream.avail_out = (unsigned int)(out_size - offset);
  283. break;
  284. // We list all the possible values of `lzma_ret` here to silence the
  285. // `switch-enum` warning and to detect if a new member was added.
  286. case LZMA_NO_CHECK:
  287. case LZMA_UNSUPPORTED_CHECK:
  288. case LZMA_GET_CHECK:
  289. case LZMA_MEM_ERROR:
  290. case LZMA_MEMLIMIT_ERROR:
  291. case LZMA_FORMAT_ERROR:
  292. case LZMA_OPTIONS_ERROR:
  293. case LZMA_DATA_ERROR:
  294. case LZMA_PROG_ERROR:
  295. default:
  296. log_warn(LD_GENERAL, "LZMA decompression didn't finish: %s.",
  297. lzma_error_str(retval));
  298. goto err;
  299. }
  300. }
  301. done:
  302. *out_len = stream.next_out - (unsigned char*)*out;
  303. lzma_end(&stream);
  304. // NUL-terminate our output.
  305. if (out_size == *out_len)
  306. *out = tor_realloc(*out, out_size + 1);
  307. (*out)[*out_len] = '\0';
  308. return 0;
  309. err:
  310. lzma_end(&stream);
  311. tor_free(*out);
  312. return -1;
  313. #else // HAVE_LZMA.
  314. (void)out;
  315. (void)out_len;
  316. (void)in;
  317. (void)in_len;
  318. (void)method;
  319. (void)complete_only;
  320. (void)protocol_warn_level;
  321. return -1;
  322. #endif // HAVE_LZMA.
  323. }
  324. /** Internal LZMA state for incremental compression/decompression.
  325. * The body of this struct is not exposed. */
  326. struct tor_lzma_compress_state_t {
  327. #ifdef HAVE_LZMA
  328. lzma_stream stream; /**< The LZMA stream. */
  329. #endif
  330. int compress; /**< True if we are compressing; false if we are inflating */
  331. /** Number of bytes read so far. Used to detect compression bombs. */
  332. size_t input_so_far;
  333. /** Number of bytes written so far. Used to detect compression bombs. */
  334. size_t output_so_far;
  335. /** Approximate number of bytes allocated for this object. */
  336. size_t allocation;
  337. };
  338. /** Construct and return a tor_lzma_compress_state_t object using
  339. * <b>method</b>. If <b>compress</b>, it's for compression; otherwise it's for
  340. * decompression. */
  341. tor_lzma_compress_state_t *
  342. tor_lzma_compress_new(int compress,
  343. compress_method_t method,
  344. compression_level_t compression_level)
  345. {
  346. tor_assert(method == LZMA_METHOD);
  347. #ifdef HAVE_LZMA
  348. tor_lzma_compress_state_t *result;
  349. lzma_ret retval;
  350. lzma_options_lzma stream_options;
  351. // Note that we do not explicitly initialize the lzma_stream object here,
  352. // since the LZMA_STREAM_INIT "just" initializes all members to 0, which is
  353. // also what `tor_malloc_zero()` does.
  354. result = tor_malloc_zero(sizeof(tor_lzma_compress_state_t));
  355. result->compress = compress;
  356. // FIXME(ahf): We should either try to do the pre-calculation that is done
  357. // with the zlib backend or use a custom allocator here where we pass our
  358. // tor_lzma_compress_state_t as the opaque value.
  359. result->allocation = 0;
  360. if (compress) {
  361. lzma_lzma_preset(&stream_options,
  362. tor_compress_memory_level(compression_level));
  363. retval = lzma_alone_encoder(&result->stream, &stream_options);
  364. if (retval != LZMA_OK) {
  365. log_warn(LD_GENERAL, "Error from LZMA encoder: %s (%u).",
  366. lzma_error_str(retval), retval);
  367. goto err;
  368. }
  369. } else {
  370. // FIXME(ahf): This should be something more sensible than
  371. // UINT64_MAX: See #21665.
  372. retval = lzma_alone_decoder(&result->stream, UINT64_MAX);
  373. if (retval != LZMA_OK) {
  374. log_warn(LD_GENERAL, "Error from LZMA decoder: %s (%u).",
  375. lzma_error_str(retval), retval);
  376. goto err;
  377. }
  378. }
  379. return result;
  380. err:
  381. tor_free(result);
  382. return NULL;
  383. #else // HAVE_LZMA.
  384. (void)compress;
  385. (void)method;
  386. (void)compression_level;
  387. return NULL;
  388. #endif // HAVE_LZMA.
  389. }
  390. /** Compress/decompress some bytes using <b>state</b>. Read up to
  391. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  392. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  393. * we've reached the end of the input.
  394. *
  395. * Return TOR_COMPRESS_DONE if we've finished the entire
  396. * compression/decompression.
  397. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  398. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  399. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  400. */
  401. tor_compress_output_t
  402. tor_lzma_compress_process(tor_lzma_compress_state_t *state,
  403. char **out, size_t *out_len,
  404. const char **in, size_t *in_len,
  405. int finish)
  406. {
  407. #ifdef HAVE_LZMA
  408. lzma_ret retval;
  409. lzma_action action;
  410. tor_assert(state != NULL);
  411. tor_assert(*in_len <= UINT_MAX);
  412. tor_assert(*out_len <= UINT_MAX);
  413. state->stream.next_in = (unsigned char *)*in;
  414. state->stream.avail_in = *in_len;
  415. state->stream.next_out = (unsigned char *)*out;
  416. state->stream.avail_out = *out_len;
  417. action = finish ? LZMA_FINISH : LZMA_RUN;
  418. retval = lzma_code(&state->stream, action);
  419. state->input_so_far += state->stream.next_in - ((unsigned char *)*in);
  420. state->output_so_far += state->stream.next_out - ((unsigned char *)*out);
  421. *out = (char *)state->stream.next_out;
  422. *out_len = state->stream.avail_out;
  423. *in = (const char *)state->stream.next_in;
  424. *in_len = state->stream.avail_in;
  425. if (! state->compress &&
  426. tor_compress_is_compression_bomb(state->input_so_far,
  427. state->output_so_far)) {
  428. log_warn(LD_DIR, "Possible compression bomb; abandoning stream.");
  429. return TOR_COMPRESS_ERROR;
  430. }
  431. switch (retval) {
  432. case LZMA_OK:
  433. if (state->stream.avail_out == 0 || finish)
  434. return TOR_COMPRESS_BUFFER_FULL;
  435. return TOR_COMPRESS_OK;
  436. case LZMA_BUF_ERROR:
  437. if (state->stream.avail_in == 0 && !finish)
  438. return TOR_COMPRESS_OK;
  439. return TOR_COMPRESS_BUFFER_FULL;
  440. case LZMA_STREAM_END:
  441. return TOR_COMPRESS_DONE;
  442. // We list all the possible values of `lzma_ret` here to silence the
  443. // `switch-enum` warning and to detect if a new member was added.
  444. case LZMA_NO_CHECK:
  445. case LZMA_UNSUPPORTED_CHECK:
  446. case LZMA_GET_CHECK:
  447. case LZMA_MEM_ERROR:
  448. case LZMA_MEMLIMIT_ERROR:
  449. case LZMA_FORMAT_ERROR:
  450. case LZMA_OPTIONS_ERROR:
  451. case LZMA_DATA_ERROR:
  452. case LZMA_PROG_ERROR:
  453. default:
  454. log_warn(LD_GENERAL, "LZMA %s didn't finish: %s.",
  455. state->compress ? "compression" : "decompression",
  456. lzma_error_str(retval));
  457. return TOR_COMPRESS_ERROR;
  458. }
  459. #else // HAVE_LZMA.
  460. (void)state;
  461. (void)out;
  462. (void)out_len;
  463. (void)in;
  464. (void)in_len;
  465. (void)finish;
  466. return TOR_COMPRESS_ERROR;
  467. #endif // HAVE_LZMA.
  468. }
  469. /** Deallocate <b>state</b>. */
  470. void
  471. tor_lzma_compress_free(tor_lzma_compress_state_t *state)
  472. {
  473. if (state == NULL)
  474. return;
  475. total_lzma_allocation -= state->allocation;
  476. #ifdef HAVE_LZMA
  477. lzma_end(&state->stream);
  478. #endif
  479. tor_free(state);
  480. }
  481. /** Return the approximate number of bytes allocated for <b>state</b>. */
  482. size_t
  483. tor_lzma_compress_state_size(const tor_lzma_compress_state_t *state)
  484. {
  485. tor_assert(state != NULL);
  486. return state->allocation;
  487. }
  488. /** Return the approximate number of bytes allocated for all LZMA states. */
  489. size_t
  490. tor_lzma_get_total_allocation(void)
  491. {
  492. return total_lzma_allocation;
  493. }