decompress_unzstd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Important notes about in-place decompression
  4. *
  5. * At least on x86, the kernel is decompressed in place: the compressed data
  6. * is placed to the end of the output buffer, and the decompressor overwrites
  7. * most of the compressed data. There must be enough safety margin to
  8. * guarantee that the write position is always behind the read position.
  9. *
  10. * The safety margin for ZSTD with a 128 KB block size is calculated below.
  11. * Note that the margin with ZSTD is bigger than with GZIP or XZ!
  12. *
  13. * The worst case for in-place decompression is that the beginning of
  14. * the file is compressed extremely well, and the rest of the file is
  15. * uncompressible. Thus, we must look for worst-case expansion when the
  16. * compressor is encoding uncompressible data.
  17. *
  18. * The structure of the .zst file in case of a compressed kernel is as follows.
  19. * Maximum sizes (as bytes) of the fields are in parenthesis.
  20. *
  21. * Frame Header: (18)
  22. * Blocks: (N)
  23. * Checksum: (4)
  24. *
  25. * The frame header and checksum overhead is at most 22 bytes.
  26. *
  27. * ZSTD stores the data in blocks. Each block has a header whose size is
  28. * a 3 bytes. After the block header, there is up to 128 KB of payload.
  29. * The maximum uncompressed size of the payload is 128 KB. The minimum
  30. * uncompressed size of the payload is never less than the payload size
  31. * (excluding the block header).
  32. *
  33. * The assumption, that the uncompressed size of the payload is never
  34. * smaller than the payload itself, is valid only when talking about
  35. * the payload as a whole. It is possible that the payload has parts where
  36. * the decompressor consumes more input than it produces output. Calculating
  37. * the worst case for this would be tricky. Instead of trying to do that,
  38. * let's simply make sure that the decompressor never overwrites any bytes
  39. * of the payload which it is currently reading.
  40. *
  41. * Now we have enough information to calculate the safety margin. We need
  42. * - 22 bytes for the .zst file format headers;
  43. * - 3 bytes per every 128 KiB of uncompressed size (one block header per
  44. * block); and
  45. * - 128 KiB (biggest possible zstd block size) to make sure that the
  46. * decompressor never overwrites anything from the block it is currently
  47. * reading.
  48. *
  49. * We get the following formula:
  50. *
  51. * safety_margin = 22 + uncompressed_size * 3 / 131072 + 131072
  52. * <= 22 + (uncompressed_size >> 15) + 131072
  53. */
  54. /*
  55. * Preboot environments #include "path/to/decompress_unzstd.c".
  56. * All of the source files we depend on must be #included.
  57. * zstd's only source dependency is xxhash, which has no source
  58. * dependencies.
  59. *
  60. * When UNZSTD_PREBOOT is defined we declare __decompress(), which is
  61. * used for kernel decompression, instead of unzstd().
  62. *
  63. * Define __DISABLE_EXPORTS in preboot environments to prevent symbols
  64. * from xxhash and zstd from being exported by the EXPORT_SYMBOL macro.
  65. */
  66. #ifdef STATIC
  67. # define UNZSTD_PREBOOT
  68. # include "xxhash.c"
  69. # include "zstd/decompress_sources.h"
  70. #else
  71. #include <linux/decompress/unzstd.h>
  72. #endif
  73. #include <linux/decompress/mm.h>
  74. #include <linux/kernel.h>
  75. #include <linux/zstd.h>
  76. /* 128MB is the maximum window size supported by zstd. */
  77. #define ZSTD_WINDOWSIZE_MAX (1 << ZSTD_WINDOWLOG_MAX)
  78. /*
  79. * Size of the input and output buffers in multi-call mode.
  80. * Pick a larger size because it isn't used during kernel decompression,
  81. * since that is single pass, and we have to allocate a large buffer for
  82. * zstd's window anyway. The larger size speeds up initramfs decompression.
  83. */
  84. #define ZSTD_IOBUF_SIZE (1 << 17)
  85. static int INIT handle_zstd_error(size_t ret, void (*error)(char *x))
  86. {
  87. const zstd_error_code err = zstd_get_error_code(ret);
  88. if (!zstd_is_error(ret))
  89. return 0;
  90. /*
  91. * zstd_get_error_name() cannot be used because error takes a char *
  92. * not a const char *
  93. */
  94. switch (err) {
  95. case ZSTD_error_memory_allocation:
  96. error("ZSTD decompressor ran out of memory");
  97. break;
  98. case ZSTD_error_prefix_unknown:
  99. error("Input is not in the ZSTD format (wrong magic bytes)");
  100. break;
  101. case ZSTD_error_dstSize_tooSmall:
  102. case ZSTD_error_corruption_detected:
  103. case ZSTD_error_checksum_wrong:
  104. error("ZSTD-compressed data is corrupt");
  105. break;
  106. default:
  107. error("ZSTD-compressed data is probably corrupt");
  108. break;
  109. }
  110. return -1;
  111. }
  112. /*
  113. * Handle the case where we have the entire input and output in one segment.
  114. * We can allocate less memory (no circular buffer for the sliding window),
  115. * and avoid some memcpy() calls.
  116. */
  117. static int INIT decompress_single(const u8 *in_buf, long in_len, u8 *out_buf,
  118. long out_len, long *in_pos,
  119. void (*error)(char *x))
  120. {
  121. const size_t wksp_size = zstd_dctx_workspace_bound();
  122. void *wksp = large_malloc(wksp_size);
  123. zstd_dctx *dctx = zstd_init_dctx(wksp, wksp_size);
  124. int err;
  125. size_t ret;
  126. if (dctx == NULL) {
  127. error("Out of memory while allocating zstd_dctx");
  128. err = -1;
  129. goto out;
  130. }
  131. /*
  132. * Find out how large the frame actually is, there may be junk at
  133. * the end of the frame that zstd_decompress_dctx() can't handle.
  134. */
  135. ret = zstd_find_frame_compressed_size(in_buf, in_len);
  136. err = handle_zstd_error(ret, error);
  137. if (err)
  138. goto out;
  139. in_len = (long)ret;
  140. ret = zstd_decompress_dctx(dctx, out_buf, out_len, in_buf, in_len);
  141. err = handle_zstd_error(ret, error);
  142. if (err)
  143. goto out;
  144. if (in_pos != NULL)
  145. *in_pos = in_len;
  146. err = 0;
  147. out:
  148. if (wksp != NULL)
  149. large_free(wksp);
  150. return err;
  151. }
  152. static int INIT __unzstd(unsigned char *in_buf, long in_len,
  153. long (*fill)(void*, unsigned long),
  154. long (*flush)(void*, unsigned long),
  155. unsigned char *out_buf, long out_len,
  156. long *in_pos,
  157. void (*error)(char *x))
  158. {
  159. zstd_in_buffer in;
  160. zstd_out_buffer out;
  161. zstd_frame_header header;
  162. void *in_allocated = NULL;
  163. void *out_allocated = NULL;
  164. void *wksp = NULL;
  165. size_t wksp_size;
  166. zstd_dstream *dstream;
  167. int err;
  168. size_t ret;
  169. /*
  170. * ZSTD decompression code won't be happy if the buffer size is so big
  171. * that its end address overflows. When the size is not provided, make
  172. * it as big as possible without having the end address overflow.
  173. */
  174. if (out_len == 0)
  175. out_len = UINTPTR_MAX - (uintptr_t)out_buf;
  176. if (fill == NULL && flush == NULL)
  177. /*
  178. * We can decompress faster and with less memory when we have a
  179. * single chunk.
  180. */
  181. return decompress_single(in_buf, in_len, out_buf, out_len,
  182. in_pos, error);
  183. /*
  184. * If in_buf is not provided, we must be using fill(), so allocate
  185. * a large enough buffer. If it is provided, it must be at least
  186. * ZSTD_IOBUF_SIZE large.
  187. */
  188. if (in_buf == NULL) {
  189. in_allocated = large_malloc(ZSTD_IOBUF_SIZE);
  190. if (in_allocated == NULL) {
  191. error("Out of memory while allocating input buffer");
  192. err = -1;
  193. goto out;
  194. }
  195. in_buf = in_allocated;
  196. in_len = 0;
  197. }
  198. /* Read the first chunk, since we need to decode the frame header. */
  199. if (fill != NULL)
  200. in_len = fill(in_buf, ZSTD_IOBUF_SIZE);
  201. if (in_len < 0) {
  202. error("ZSTD-compressed data is truncated");
  203. err = -1;
  204. goto out;
  205. }
  206. /* Set the first non-empty input buffer. */
  207. in.src = in_buf;
  208. in.pos = 0;
  209. in.size = in_len;
  210. /* Allocate the output buffer if we are using flush(). */
  211. if (flush != NULL) {
  212. out_allocated = large_malloc(ZSTD_IOBUF_SIZE);
  213. if (out_allocated == NULL) {
  214. error("Out of memory while allocating output buffer");
  215. err = -1;
  216. goto out;
  217. }
  218. out_buf = out_allocated;
  219. out_len = ZSTD_IOBUF_SIZE;
  220. }
  221. /* Set the output buffer. */
  222. out.dst = out_buf;
  223. out.pos = 0;
  224. out.size = out_len;
  225. /*
  226. * We need to know the window size to allocate the zstd_dstream.
  227. * Since we are streaming, we need to allocate a buffer for the sliding
  228. * window. The window size varies from 1 KB to ZSTD_WINDOWSIZE_MAX
  229. * (8 MB), so it is important to use the actual value so as not to
  230. * waste memory when it is smaller.
  231. */
  232. ret = zstd_get_frame_header(&header, in.src, in.size);
  233. err = handle_zstd_error(ret, error);
  234. if (err)
  235. goto out;
  236. if (ret != 0) {
  237. error("ZSTD-compressed data has an incomplete frame header");
  238. err = -1;
  239. goto out;
  240. }
  241. if (header.windowSize > ZSTD_WINDOWSIZE_MAX) {
  242. error("ZSTD-compressed data has too large a window size");
  243. err = -1;
  244. goto out;
  245. }
  246. /*
  247. * Allocate the zstd_dstream now that we know how much memory is
  248. * required.
  249. */
  250. wksp_size = zstd_dstream_workspace_bound(header.windowSize);
  251. wksp = large_malloc(wksp_size);
  252. dstream = zstd_init_dstream(header.windowSize, wksp, wksp_size);
  253. if (dstream == NULL) {
  254. error("Out of memory while allocating ZSTD_DStream");
  255. err = -1;
  256. goto out;
  257. }
  258. /*
  259. * Decompression loop:
  260. * Read more data if necessary (error if no more data can be read).
  261. * Call the decompression function, which returns 0 when finished.
  262. * Flush any data produced if using flush().
  263. */
  264. if (in_pos != NULL)
  265. *in_pos = 0;
  266. do {
  267. /*
  268. * If we need to reload data, either we have fill() and can
  269. * try to get more data, or we don't and the input is truncated.
  270. */
  271. if (in.pos == in.size) {
  272. if (in_pos != NULL)
  273. *in_pos += in.pos;
  274. in_len = fill ? fill(in_buf, ZSTD_IOBUF_SIZE) : -1;
  275. if (in_len < 0) {
  276. error("ZSTD-compressed data is truncated");
  277. err = -1;
  278. goto out;
  279. }
  280. in.pos = 0;
  281. in.size = in_len;
  282. }
  283. /* Returns zero when the frame is complete. */
  284. ret = zstd_decompress_stream(dstream, &out, &in);
  285. err = handle_zstd_error(ret, error);
  286. if (err)
  287. goto out;
  288. /* Flush all of the data produced if using flush(). */
  289. if (flush != NULL && out.pos > 0) {
  290. if (out.pos != flush(out.dst, out.pos)) {
  291. error("Failed to flush()");
  292. err = -1;
  293. goto out;
  294. }
  295. out.pos = 0;
  296. }
  297. } while (ret != 0);
  298. if (in_pos != NULL)
  299. *in_pos += in.pos;
  300. err = 0;
  301. out:
  302. if (in_allocated != NULL)
  303. large_free(in_allocated);
  304. if (out_allocated != NULL)
  305. large_free(out_allocated);
  306. if (wksp != NULL)
  307. large_free(wksp);
  308. return err;
  309. }
  310. #ifndef UNZSTD_PREBOOT
  311. STATIC int INIT unzstd(unsigned char *buf, long len,
  312. long (*fill)(void*, unsigned long),
  313. long (*flush)(void*, unsigned long),
  314. unsigned char *out_buf,
  315. long *pos,
  316. void (*error)(char *x))
  317. {
  318. return __unzstd(buf, len, fill, flush, out_buf, 0, pos, error);
  319. }
  320. #else
  321. STATIC int INIT __decompress(unsigned char *buf, long len,
  322. long (*fill)(void*, unsigned long),
  323. long (*flush)(void*, unsigned long),
  324. unsigned char *out_buf, long out_len,
  325. long *pos,
  326. void (*error)(char *x))
  327. {
  328. return __unzstd(buf, len, fill, flush, out_buf, out_len, pos, error);
  329. }
  330. #endif