compress_lzma.c 16 KB

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