1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2021 Google LLC.
6 #include <linux/init.h>
7 #include <linux/highmem.h>
8 #include <linux/kobject.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 #include <linux/vmalloc.h>
15 #include "module-internal.h"
17 static int module_extend_max_pages(struct load_info *info, unsigned int extent)
19 struct page **new_pages;
21 new_pages = kvmalloc_array(info->max_pages + extent,
22 sizeof(info->pages), GFP_KERNEL);
26 memcpy(new_pages, info->pages, info->max_pages * sizeof(info->pages));
28 info->pages = new_pages;
29 info->max_pages += extent;
34 static struct page *module_get_next_page(struct load_info *info)
39 if (info->max_pages == info->used_pages) {
40 error = module_extend_max_pages(info, info->used_pages);
42 return ERR_PTR(error);
45 page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
47 return ERR_PTR(-ENOMEM);
49 info->pages[info->used_pages++] = page;
53 #ifdef CONFIG_MODULE_COMPRESS_GZIP
54 #include <linux/zlib.h>
55 #define MODULE_COMPRESSION gzip
56 #define MODULE_DECOMPRESS_FN module_gzip_decompress
59 * Calculate length of the header which consists of signature, header
60 * flags, time stamp and operating system ID (10 bytes total), plus
61 * an optional filename.
63 static size_t module_gzip_header_len(const u8 *buf, size_t size)
65 const u8 signature[] = { 0x1f, 0x8b, 0x08 };
68 if (size < len || memcmp(buf, signature, sizeof(signature)))
74 * If we can't find the end of the file name we must
75 * be dealing with a corrupted file.
79 } while (buf[len++] != '\0');
85 static ssize_t module_gzip_decompress(struct load_info *info,
86 const void *buf, size_t size)
88 struct z_stream_s s = { 0 };
94 gzip_hdr_len = module_gzip_header_len(buf, size);
96 pr_err("not a gzip compressed module\n");
100 s.next_in = buf + gzip_hdr_len;
101 s.avail_in = size - gzip_hdr_len;
103 s.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
107 rc = zlib_inflateInit2(&s, -MAX_WBITS);
109 pr_err("failed to initialize decompressor: %d\n", rc);
115 struct page *page = module_get_next_page(info);
118 goto out_inflate_end;
121 s.next_out = kmap(page);
122 s.avail_out = PAGE_SIZE;
123 rc = zlib_inflate(&s, 0);
126 new_size += PAGE_SIZE - s.avail_out;
127 } while (rc == Z_OK);
129 if (rc != Z_STREAM_END) {
130 pr_err("decompression failed with status %d\n", rc);
132 goto out_inflate_end;
143 #elif CONFIG_MODULE_COMPRESS_XZ
144 #include <linux/xz.h>
145 #define MODULE_COMPRESSION xz
146 #define MODULE_DECOMPRESS_FN module_xz_decompress
148 static ssize_t module_xz_decompress(struct load_info *info,
149 const void *buf, size_t size)
151 static const u8 signature[] = { 0xfd, '7', 'z', 'X', 'Z', 0 };
152 struct xz_dec *xz_dec;
153 struct xz_buf xz_buf;
158 if (size < sizeof(signature) ||
159 memcmp(buf, signature, sizeof(signature))) {
160 pr_err("not an xz compressed module\n");
164 xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
168 xz_buf.in_size = size;
173 struct page *page = module_get_next_page(info);
179 xz_buf.out = kmap(page);
181 xz_buf.out_size = PAGE_SIZE;
182 xz_ret = xz_dec_run(xz_dec, &xz_buf);
185 new_size += xz_buf.out_pos;
186 } while (xz_buf.out_pos == PAGE_SIZE && xz_ret == XZ_OK);
188 if (xz_ret != XZ_STREAM_END) {
189 pr_err("decompression failed with status %d\n", xz_ret);
201 #error "Unexpected configuration for CONFIG_MODULE_DECOMPRESS"
204 int module_decompress(struct load_info *info, const void *buf, size_t size)
206 unsigned int n_pages;
211 * Start with number of pages twice as big as needed for
214 n_pages = DIV_ROUND_UP(size, PAGE_SIZE) * 2;
215 error = module_extend_max_pages(info, n_pages);
217 data_size = MODULE_DECOMPRESS_FN(info, buf, size);
223 info->hdr = vmap(info->pages, info->used_pages, VM_MAP, PAGE_KERNEL);
229 info->len = data_size;
233 module_decompress_cleanup(info);
237 void module_decompress_cleanup(struct load_info *info)
244 for (i = 0; i < info->used_pages; i++)
245 __free_page(info->pages[i]);
250 info->max_pages = info->used_pages = 0;
254 static ssize_t compression_show(struct kobject *kobj,
255 struct kobj_attribute *attr, char *buf)
257 return sysfs_emit(buf, "%s\n", __stringify(MODULE_COMPRESSION));
259 static struct kobj_attribute module_compression_attr = __ATTR_RO(compression);
261 static int __init module_decompress_sysfs_init(void)
265 error = sysfs_create_file(&module_kset->kobj,
266 &module_compression_attr.attr);
268 pr_warn("Failed to create 'compression' attribute");
272 late_initcall(module_decompress_sysfs_init);