#include <linux/init.h>
#include <linux/highmem.h>
#include <linux/kobject.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/vmalloc.h>
#include "internal.h"
static int module_extend_max_pages(struct load_info *info, unsigned int extent)
{ … }
static struct page *module_get_next_page(struct load_info *info)
{ … }
#if defined(CONFIG_MODULE_COMPRESS_GZIP)
#include <linux/zlib.h>
#define MODULE_COMPRESSION …
#define MODULE_DECOMPRESS_FN …
static size_t module_gzip_header_len(const u8 *buf, size_t size)
{ … }
static ssize_t module_gzip_decompress(struct load_info *info,
const void *buf, size_t size)
{ … }
#elif defined(CONFIG_MODULE_COMPRESS_XZ)
#include <linux/xz.h>
#define MODULE_COMPRESSION …
#define MODULE_DECOMPRESS_FN …
static ssize_t module_xz_decompress(struct load_info *info,
const void *buf, size_t size)
{
static const u8 signature[] = { 0xfd, '7', 'z', 'X', 'Z', 0 };
struct xz_dec *xz_dec;
struct xz_buf xz_buf;
enum xz_ret xz_ret;
size_t new_size = 0;
ssize_t retval;
if (size < sizeof(signature) ||
memcmp(buf, signature, sizeof(signature))) {
pr_err("not an xz compressed module\n");
return -EINVAL;
}
xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
if (!xz_dec)
return -ENOMEM;
xz_buf.in_size = size;
xz_buf.in = buf;
xz_buf.in_pos = 0;
do {
struct page *page = module_get_next_page(info);
if (IS_ERR(page)) {
retval = PTR_ERR(page);
goto out;
}
xz_buf.out = kmap_local_page(page);
xz_buf.out_pos = 0;
xz_buf.out_size = PAGE_SIZE;
xz_ret = xz_dec_run(xz_dec, &xz_buf);
kunmap_local(xz_buf.out);
new_size += xz_buf.out_pos;
} while (xz_buf.out_pos == PAGE_SIZE && xz_ret == XZ_OK);
if (xz_ret != XZ_STREAM_END) {
pr_err("decompression failed with status %d\n", xz_ret);
retval = -EINVAL;
goto out;
}
retval = new_size;
out:
xz_dec_end(xz_dec);
return retval;
}
#elif defined(CONFIG_MODULE_COMPRESS_ZSTD)
#include <linux/zstd.h>
#define MODULE_COMPRESSION …
#define MODULE_DECOMPRESS_FN …
static ssize_t module_zstd_decompress(struct load_info *info,
const void *buf, size_t size)
{
static const u8 signature[] = { 0x28, 0xb5, 0x2f, 0xfd };
ZSTD_outBuffer zstd_dec;
ZSTD_inBuffer zstd_buf;
zstd_frame_header header;
size_t wksp_size;
void *wksp = NULL;
ZSTD_DStream *dstream;
size_t ret;
size_t new_size = 0;
int retval;
if (size < sizeof(signature) ||
memcmp(buf, signature, sizeof(signature))) {
pr_err("not a zstd compressed module\n");
return -EINVAL;
}
zstd_buf.src = buf;
zstd_buf.pos = 0;
zstd_buf.size = size;
ret = zstd_get_frame_header(&header, zstd_buf.src, zstd_buf.size);
if (ret != 0) {
pr_err("ZSTD-compressed data has an incomplete frame header\n");
retval = -EINVAL;
goto out;
}
if (header.windowSize > (1 << ZSTD_WINDOWLOG_MAX)) {
pr_err("ZSTD-compressed data has too large a window size\n");
retval = -EINVAL;
goto out;
}
wksp_size = zstd_dstream_workspace_bound(header.windowSize);
wksp = kvmalloc(wksp_size, GFP_KERNEL);
if (!wksp) {
retval = -ENOMEM;
goto out;
}
dstream = zstd_init_dstream(header.windowSize, wksp, wksp_size);
if (!dstream) {
pr_err("Can't initialize ZSTD stream\n");
retval = -ENOMEM;
goto out;
}
do {
struct page *page = module_get_next_page(info);
if (IS_ERR(page)) {
retval = PTR_ERR(page);
goto out;
}
zstd_dec.dst = kmap_local_page(page);
zstd_dec.pos = 0;
zstd_dec.size = PAGE_SIZE;
ret = zstd_decompress_stream(dstream, &zstd_dec, &zstd_buf);
kunmap_local(zstd_dec.dst);
retval = zstd_get_error_code(ret);
if (retval)
break;
new_size += zstd_dec.pos;
} while (zstd_dec.pos == PAGE_SIZE && ret != 0);
if (retval) {
pr_err("ZSTD-decompression failed with status %d\n", retval);
retval = -EINVAL;
goto out;
}
retval = new_size;
out:
kvfree(wksp);
return retval;
}
#else
#error "Unexpected configuration for CONFIG_MODULE_DECOMPRESS"
#endif
int module_decompress(struct load_info *info, const void *buf, size_t size)
{ … }
void module_decompress_cleanup(struct load_info *info)
{ … }
#ifdef CONFIG_SYSFS
static ssize_t compression_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{ … }
static struct kobj_attribute module_compression_attr = …;
static int __init module_decompress_sysfs_init(void)
{ … }
late_initcall(module_decompress_sysfs_init);
#endif