compress_lzma.c 16 KB

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