decompress.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2021 Google LLC.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/highmem.h>
  7. #include <linux/kobject.h>
  8. #include <linux/mm.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/vmalloc.h>
  13. #include "internal.h"
  14. static int module_extend_max_pages(struct load_info *info, unsigned int extent)
  15. {
  16. struct page **new_pages;
  17. new_pages = kvmalloc_array(info->max_pages + extent,
  18. sizeof(info->pages), GFP_KERNEL);
  19. if (!new_pages)
  20. return -ENOMEM;
  21. memcpy(new_pages, info->pages, info->max_pages * sizeof(info->pages));
  22. kvfree(info->pages);
  23. info->pages = new_pages;
  24. info->max_pages += extent;
  25. return 0;
  26. }
  27. static struct page *module_get_next_page(struct load_info *info)
  28. {
  29. struct page *page;
  30. int error;
  31. if (info->max_pages == info->used_pages) {
  32. error = module_extend_max_pages(info, info->used_pages);
  33. if (error)
  34. return ERR_PTR(error);
  35. }
  36. page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  37. if (!page)
  38. return ERR_PTR(-ENOMEM);
  39. info->pages[info->used_pages++] = page;
  40. return page;
  41. }
  42. #ifdef CONFIG_MODULE_COMPRESS_GZIP
  43. #include <linux/zlib.h>
  44. #define MODULE_COMPRESSION gzip
  45. #define MODULE_DECOMPRESS_FN module_gzip_decompress
  46. /*
  47. * Calculate length of the header which consists of signature, header
  48. * flags, time stamp and operating system ID (10 bytes total), plus
  49. * an optional filename.
  50. */
  51. static size_t module_gzip_header_len(const u8 *buf, size_t size)
  52. {
  53. const u8 signature[] = { 0x1f, 0x8b, 0x08 };
  54. size_t len = 10;
  55. if (size < len || memcmp(buf, signature, sizeof(signature)))
  56. return 0;
  57. if (buf[3] & 0x08) {
  58. do {
  59. /*
  60. * If we can't find the end of the file name we must
  61. * be dealing with a corrupted file.
  62. */
  63. if (len == size)
  64. return 0;
  65. } while (buf[len++] != '\0');
  66. }
  67. return len;
  68. }
  69. static ssize_t module_gzip_decompress(struct load_info *info,
  70. const void *buf, size_t size)
  71. {
  72. struct z_stream_s s = { 0 };
  73. size_t new_size = 0;
  74. size_t gzip_hdr_len;
  75. ssize_t retval;
  76. int rc;
  77. gzip_hdr_len = module_gzip_header_len(buf, size);
  78. if (!gzip_hdr_len) {
  79. pr_err("not a gzip compressed module\n");
  80. return -EINVAL;
  81. }
  82. s.next_in = buf + gzip_hdr_len;
  83. s.avail_in = size - gzip_hdr_len;
  84. s.workspace = vmalloc(zlib_inflate_workspacesize());
  85. if (!s.workspace)
  86. return -ENOMEM;
  87. rc = zlib_inflateInit2(&s, -MAX_WBITS);
  88. if (rc != Z_OK) {
  89. pr_err("failed to initialize decompressor: %d\n", rc);
  90. retval = -EINVAL;
  91. goto out;
  92. }
  93. do {
  94. struct page *page = module_get_next_page(info);
  95. if (IS_ERR(page)) {
  96. retval = PTR_ERR(page);
  97. goto out_inflate_end;
  98. }
  99. s.next_out = kmap_local_page(page);
  100. s.avail_out = PAGE_SIZE;
  101. rc = zlib_inflate(&s, 0);
  102. kunmap_local(s.next_out);
  103. new_size += PAGE_SIZE - s.avail_out;
  104. } while (rc == Z_OK);
  105. if (rc != Z_STREAM_END) {
  106. pr_err("decompression failed with status %d\n", rc);
  107. retval = -EINVAL;
  108. goto out_inflate_end;
  109. }
  110. retval = new_size;
  111. out_inflate_end:
  112. zlib_inflateEnd(&s);
  113. out:
  114. vfree(s.workspace);
  115. return retval;
  116. }
  117. #elif CONFIG_MODULE_COMPRESS_XZ
  118. #include <linux/xz.h>
  119. #define MODULE_COMPRESSION xz
  120. #define MODULE_DECOMPRESS_FN module_xz_decompress
  121. static ssize_t module_xz_decompress(struct load_info *info,
  122. const void *buf, size_t size)
  123. {
  124. static const u8 signature[] = { 0xfd, '7', 'z', 'X', 'Z', 0 };
  125. struct xz_dec *xz_dec;
  126. struct xz_buf xz_buf;
  127. enum xz_ret xz_ret;
  128. size_t new_size = 0;
  129. ssize_t retval;
  130. if (size < sizeof(signature) ||
  131. memcmp(buf, signature, sizeof(signature))) {
  132. pr_err("not an xz compressed module\n");
  133. return -EINVAL;
  134. }
  135. xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
  136. if (!xz_dec)
  137. return -ENOMEM;
  138. xz_buf.in_size = size;
  139. xz_buf.in = buf;
  140. xz_buf.in_pos = 0;
  141. do {
  142. struct page *page = module_get_next_page(info);
  143. if (IS_ERR(page)) {
  144. retval = PTR_ERR(page);
  145. goto out;
  146. }
  147. xz_buf.out = kmap_local_page(page);
  148. xz_buf.out_pos = 0;
  149. xz_buf.out_size = PAGE_SIZE;
  150. xz_ret = xz_dec_run(xz_dec, &xz_buf);
  151. kunmap_local(xz_buf.out);
  152. new_size += xz_buf.out_pos;
  153. } while (xz_buf.out_pos == PAGE_SIZE && xz_ret == XZ_OK);
  154. if (xz_ret != XZ_STREAM_END) {
  155. pr_err("decompression failed with status %d\n", xz_ret);
  156. retval = -EINVAL;
  157. goto out;
  158. }
  159. retval = new_size;
  160. out:
  161. xz_dec_end(xz_dec);
  162. return retval;
  163. }
  164. #else
  165. #error "Unexpected configuration for CONFIG_MODULE_DECOMPRESS"
  166. #endif
  167. int module_decompress(struct load_info *info, const void *buf, size_t size)
  168. {
  169. unsigned int n_pages;
  170. ssize_t data_size;
  171. int error;
  172. /*
  173. * Start with number of pages twice as big as needed for
  174. * compressed data.
  175. */
  176. n_pages = DIV_ROUND_UP(size, PAGE_SIZE) * 2;
  177. error = module_extend_max_pages(info, n_pages);
  178. data_size = MODULE_DECOMPRESS_FN(info, buf, size);
  179. if (data_size < 0) {
  180. error = data_size;
  181. goto err;
  182. }
  183. info->hdr = vmap(info->pages, info->used_pages, VM_MAP, PAGE_KERNEL);
  184. if (!info->hdr) {
  185. error = -ENOMEM;
  186. goto err;
  187. }
  188. info->len = data_size;
  189. return 0;
  190. err:
  191. module_decompress_cleanup(info);
  192. return error;
  193. }
  194. void module_decompress_cleanup(struct load_info *info)
  195. {
  196. int i;
  197. if (info->hdr)
  198. vunmap(info->hdr);
  199. for (i = 0; i < info->used_pages; i++)
  200. __free_page(info->pages[i]);
  201. kvfree(info->pages);
  202. info->pages = NULL;
  203. info->max_pages = info->used_pages = 0;
  204. }
  205. #ifdef CONFIG_SYSFS
  206. static ssize_t compression_show(struct kobject *kobj,
  207. struct kobj_attribute *attr, char *buf)
  208. {
  209. return sysfs_emit(buf, __stringify(MODULE_COMPRESSION) "\n");
  210. }
  211. static struct kobj_attribute module_compression_attr = __ATTR_RO(compression);
  212. static int __init module_decompress_sysfs_init(void)
  213. {
  214. int error;
  215. error = sysfs_create_file(&module_kset->kobj,
  216. &module_compression_attr.attr);
  217. if (error)
  218. pr_warn("Failed to create 'compression' attribute");
  219. return 0;
  220. }
  221. late_initcall(module_decompress_sysfs_init);
  222. #endif