| 1 | |
| 2 | |
| 3 | * Copyright 2021 Google LLC.
|
| 4 |
|
| 5 |
|
| 6 | #include <linux/init.h>
|
| 7 | #include <linux/highmem.h>
|
| 8 | #include <linux/kobject.h>
|
| 9 | #include <linux/mm.h>
|
| 10 | #include <linux/module.h>
|
| 11 | #include <linux/slab.h>
|
| 12 | #include <linux/sysfs.h>
|
| 13 | #include <linux/vmalloc.h>
|
| 14 |
|
| 15 | #include "internal.h"
|
| 16 |
|
| 17 | static int module_extend_max_pages(struct load_info *info, unsigned int extent)
|
| 18 | {
|
| 19 | struct page **new_pages;
|
| 20 | unsigned int new_max = info->max_pages + extent;
|
| 21 |
|
| 22 | new_pages = kvrealloc(info->pages,
|
| 23 | size_mul(new_max, sizeof(*info->pages)),
|
| 24 | GFP_KERNEL);
|
| 25 | if (!new_pages)
|
| 26 | return -ENOMEM;
|
| 27 |
|
| 28 | info->pages = new_pages;
|
| 29 | info->max_pages = new_max;
|
| 30 |
|
| 31 | return 0;
|
| 32 | }
|
| 33 |
|
| 34 | static struct page *module_get_next_page(struct load_info *info)
|
| 35 | {
|
| 36 | struct page *page;
|
| 37 | int error;
|
| 38 |
|
| 39 | if (info->max_pages == info->used_pages) {
|
| 40 | error = module_extend_max_pages(info, info->used_pages);
|
| 41 | if (error)
|
| 42 | return ERR_PTR(error);
|
| 43 | }
|
| 44 |
|
| 45 | page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
|
| 46 | if (!page)
|
| 47 | return ERR_PTR(-ENOMEM);
|
| 48 |
|
| 49 | info->pages[info->used_pages++] = page;
|
| 50 | return page;
|
| 51 | }
|
| 52 |
|
| 53 | #if defined(CONFIG_MODULE_COMPRESS_GZIP)
|
| 54 | #include <linux/zlib.h>
|
| 55 | #define MODULE_COMPRESSION gzip
|
| 56 | #define MODULE_DECOMPRESS_FN module_gzip_decompress
|
| 57 |
|
| 58 | |
| 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.
|
| 62 |
|
| 63 | static size_t module_gzip_header_len(const u8 *buf, size_t size)
|
| 64 | {
|
| 65 | const u8 signature[] = { 0x1f, 0x8b, 0x08 };
|
| 66 | size_t len = 10;
|
| 67 |
|
| 68 | if (size < len || memcmp(buf, signature, sizeof(signature)))
|
| 69 | return 0;
|
| 70 |
|
| 71 | if (buf[3] & 0x08) {
|
| 72 | do {
|
| 73 | |
| 74 | * If we can't find the end of the file name we must
|
| 75 | * be dealing with a corrupted file.
|
| 76 |
|
| 77 | if (len == size)
|
| 78 | return 0;
|
| 79 | } while (buf[len++] != '\0');
|
| 80 | }
|
| 81 |
|
| 82 | return len;
|
| 83 | }
|
| 84 |
|
| 85 | static ssize_t module_gzip_decompress(struct load_info *info,
|
| 86 | const void *buf, size_t size)
|
| 87 | {
|
| 88 | struct z_stream_s s = { 0 };
|
| 89 | size_t new_size = 0;
|
| 90 | size_t gzip_hdr_len;
|
| 91 | ssize_t retval;
|
| 92 | int rc;
|
| 93 |
|
| 94 | gzip_hdr_len = module_gzip_header_len(buf, size);
|
| 95 | if (!gzip_hdr_len) {
|
| 96 | pr_err("not a gzip compressed module\n");
|
| 97 | return -EINVAL;
|
| 98 | }
|
| 99 |
|
| 100 | s.next_in = buf + gzip_hdr_len;
|
| 101 | s.avail_in = size - gzip_hdr_len;
|
| 102 |
|
| 103 | s.workspace = kvmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
|
| 104 | if (!s.workspace)
|
| 105 | return -ENOMEM;
|
| 106 |
|
| 107 | rc = zlib_inflateInit2(&s, -MAX_WBITS);
|
| 108 | if (rc != Z_OK) {
|
| 109 | pr_err("failed to initialize decompressor: %d\n", rc);
|
| 110 | retval = -EINVAL;
|
| 111 | goto out;
|
| 112 | }
|
| 113 |
|
| 114 | do {
|
| 115 | struct page *page = module_get_next_page(info);
|
| 116 |
|
| 117 | if (IS_ERR(page)) {
|
| 118 | retval = PTR_ERR(page);
|
| 119 | goto out_inflate_end;
|
| 120 | }
|
| 121 |
|
| 122 | s.next_out = kmap_local_page(page);
|
| 123 | s.avail_out = PAGE_SIZE;
|
| 124 | rc = zlib_inflate(&s, 0);
|
| 125 | kunmap_local(s.next_out);
|
| 126 |
|
| 127 | new_size += PAGE_SIZE - s.avail_out;
|
| 128 | } while (rc == Z_OK);
|
| 129 |
|
| 130 | if (rc != Z_STREAM_END) {
|
| 131 | pr_err("decompression failed with status %d\n", rc);
|
| 132 | retval = -EINVAL;
|
| 133 | goto out_inflate_end;
|
| 134 | }
|
| 135 |
|
| 136 | retval = new_size;
|
| 137 |
|
| 138 | out_inflate_end:
|
| 139 | zlib_inflateEnd(&s);
|
| 140 | out:
|
| 141 | kvfree(s.workspace);
|
| 142 | return retval;
|
| 143 | }
|
| 144 | #elif defined(CONFIG_MODULE_COMPRESS_XZ)
|
| 145 | #include <linux/xz.h>
|
| 146 | #define MODULE_COMPRESSION xz
|
| 147 | #define MODULE_DECOMPRESS_FN module_xz_decompress
|
| 148 |
|
| 149 | static ssize_t module_xz_decompress(struct load_info *info,
|
| 150 | const void *buf, size_t size)
|
| 151 | {
|
| 152 | static const u8 signature[] = { 0xfd, '7', 'z', 'X', 'Z', 0 };
|
| 153 | struct xz_dec *xz_dec;
|
| 154 | struct xz_buf xz_buf;
|
| 155 | enum xz_ret xz_ret;
|
| 156 | size_t new_size = 0;
|
| 157 | ssize_t retval;
|
| 158 |
|
| 159 | if (size < sizeof(signature) ||
|
| 160 | memcmp(buf, signature, sizeof(signature))) {
|
| 161 | pr_err("not an xz compressed module\n");
|
| 162 | return -EINVAL;
|
| 163 | }
|
| 164 |
|
| 165 | xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
|
| 166 | if (!xz_dec)
|
| 167 | return -ENOMEM;
|
| 168 |
|
| 169 | xz_buf.in_size = size;
|
| 170 | xz_buf.in = buf;
|
| 171 | xz_buf.in_pos = 0;
|
| 172 |
|
| 173 | do {
|
| 174 | struct page *page = module_get_next_page(info);
|
| 175 |
|
| 176 | if (IS_ERR(page)) {
|
| 177 | retval = PTR_ERR(page);
|
| 178 | goto out;
|
| 179 | }
|
| 180 |
|
| 181 | xz_buf.out = kmap_local_page(page);
|
| 182 | xz_buf.out_pos = 0;
|
| 183 | xz_buf.out_size = PAGE_SIZE;
|
| 184 | xz_ret = xz_dec_run(xz_dec, &xz_buf);
|
| 185 | kunmap_local(xz_buf.out);
|
| 186 |
|
| 187 | new_size += xz_buf.out_pos;
|
| 188 | } while (xz_buf.out_pos == PAGE_SIZE && xz_ret == XZ_OK);
|
| 189 |
|
| 190 | if (xz_ret != XZ_STREAM_END) {
|
| 191 | pr_err("decompression failed with status %d\n", xz_ret);
|
| 192 | retval = -EINVAL;
|
| 193 | goto out;
|
| 194 | }
|
| 195 |
|
| 196 | retval = new_size;
|
| 197 |
|
| 198 | out:
|
| 199 | xz_dec_end(xz_dec);
|
| 200 | return retval;
|
| 201 | }
|
| 202 | #elif defined(CONFIG_MODULE_COMPRESS_ZSTD)
|
| 203 | #include <linux/zstd.h>
|
| 204 | #define MODULE_COMPRESSION zstd
|
| 205 | #define MODULE_DECOMPRESS_FN module_zstd_decompress
|
| 206 |
|
| 207 | static ssize_t module_zstd_decompress(struct load_info *info,
|
| 208 | const void *buf, size_t size)
|
| 209 | {
|
| 210 | static const u8 signature[] = { 0x28, 0xb5, 0x2f, 0xfd };
|
| 211 | ZSTD_outBuffer zstd_dec;
|
| 212 | ZSTD_inBuffer zstd_buf;
|
| 213 | zstd_frame_header header;
|
| 214 | size_t wksp_size;
|
| 215 | void *wksp = NULL;
|
| 216 | ZSTD_DStream *dstream;
|
| 217 | size_t ret;
|
| 218 | size_t new_size = 0;
|
| 219 | int retval;
|
| 220 |
|
| 221 | if (size < sizeof(signature) ||
|
| 222 | memcmp(buf, signature, sizeof(signature))) {
|
| 223 | pr_err("not a zstd compressed module\n");
|
| 224 | return -EINVAL;
|
| 225 | }
|
| 226 |
|
| 227 | zstd_buf.src = buf;
|
| 228 | zstd_buf.pos = 0;
|
| 229 | zstd_buf.size = size;
|
| 230 |
|
| 231 | ret = zstd_get_frame_header(&header, zstd_buf.src, zstd_buf.size);
|
| 232 | if (ret != 0) {
|
| 233 | pr_err("ZSTD-compressed data has an incomplete frame header\n");
|
| 234 | retval = -EINVAL;
|
| 235 | goto out;
|
| 236 | }
|
| 237 | if (header.windowSize > (1 << ZSTD_WINDOWLOG_MAX)) {
|
| 238 | pr_err("ZSTD-compressed data has too large a window size\n");
|
| 239 | retval = -EINVAL;
|
| 240 | goto out;
|
| 241 | }
|
| 242 |
|
| 243 | wksp_size = zstd_dstream_workspace_bound(header.windowSize);
|
| 244 | wksp = kvmalloc(wksp_size, GFP_KERNEL);
|
| 245 | if (!wksp) {
|
| 246 | retval = -ENOMEM;
|
| 247 | goto out;
|
| 248 | }
|
| 249 |
|
| 250 | dstream = zstd_init_dstream(header.windowSize, wksp, wksp_size);
|
| 251 | if (!dstream) {
|
| 252 | pr_err("Can't initialize ZSTD stream\n");
|
| 253 | retval = -ENOMEM;
|
| 254 | goto out;
|
| 255 | }
|
| 256 |
|
| 257 | do {
|
| 258 | struct page *page = module_get_next_page(info);
|
| 259 |
|
| 260 | if (IS_ERR(page)) {
|
| 261 | retval = PTR_ERR(page);
|
| 262 | goto out;
|
| 263 | }
|
| 264 |
|
| 265 | zstd_dec.dst = kmap_local_page(page);
|
| 266 | zstd_dec.pos = 0;
|
| 267 | zstd_dec.size = PAGE_SIZE;
|
| 268 |
|
| 269 | ret = zstd_decompress_stream(dstream, &zstd_dec, &zstd_buf);
|
| 270 | kunmap_local(zstd_dec.dst);
|
| 271 | retval = zstd_get_error_code(ret);
|
| 272 | if (retval)
|
| 273 | break;
|
| 274 |
|
| 275 | new_size += zstd_dec.pos;
|
| 276 | } while (zstd_dec.pos == PAGE_SIZE && ret != 0);
|
| 277 |
|
| 278 | if (retval) {
|
| 279 | pr_err("ZSTD-decompression failed with status %d\n", retval);
|
| 280 | retval = -EINVAL;
|
| 281 | goto out;
|
| 282 | }
|
| 283 |
|
| 284 | retval = new_size;
|
| 285 |
|
| 286 | out:
|
| 287 | kvfree(wksp);
|
| 288 | return retval;
|
| 289 | }
|
| 290 | #else
|
| 291 | #error "Unexpected configuration for CONFIG_MODULE_DECOMPRESS"
|
| 292 | #endif
|
| 293 |
|
| 294 | int module_decompress(struct load_info *info, const void *buf, size_t size)
|
| 295 | {
|
| 296 | unsigned int n_pages;
|
| 297 | ssize_t data_size;
|
| 298 | int error;
|
| 299 |
|
| 300 | #if defined(CONFIG_MODULE_STATS)
|
| 301 | info->compressed_len = size;
|
| 302 | #endif
|
| 303 |
|
| 304 | |
| 305 | * Start with number of pages twice as big as needed for
|
| 306 | * compressed data.
|
| 307 |
|
| 308 | n_pages = DIV_ROUND_UP(size, PAGE_SIZE) * 2;
|
| 309 | error = module_extend_max_pages(info, n_pages);
|
| 310 | if (error)
|
| 311 | return error;
|
| 312 |
|
| 313 | data_size = MODULE_DECOMPRESS_FN(info, buf, size);
|
| 314 | if (data_size < 0) {
|
| 315 | error = data_size;
|
| 316 | goto err;
|
| 317 | }
|
| 318 |
|
| 319 | info->hdr = vmap(info->pages, info->used_pages, VM_MAP, PAGE_KERNEL);
|
| 320 | if (!info->hdr) {
|
| 321 | error = -ENOMEM;
|
| 322 | goto err;
|
| 323 | }
|
| 324 |
|
| 325 | info->len = data_size;
|
| 326 | return 0;
|
| 327 |
|
| 328 | err:
|
| 329 | module_decompress_cleanup(info);
|
| 330 | return error;
|
| 331 | }
|
| 332 |
|
| 333 | void module_decompress_cleanup(struct load_info *info)
|
| 334 | {
|
| 335 | int i;
|
| 336 |
|
| 337 | if (info->hdr)
|
| 338 | vunmap(info->hdr);
|
| 339 |
|
| 340 | for (i = 0; i < info->used_pages; i++)
|
| 341 | __free_page(info->pages[i]);
|
| 342 |
|
| 343 | kvfree(info->pages);
|
| 344 |
|
| 345 | info->pages = NULL;
|
| 346 | info->max_pages = info->used_pages = 0;
|
| 347 | }
|
| 348 |
|
| 349 | #ifdef CONFIG_SYSFS
|
| 350 | static ssize_t compression_show(struct kobject *kobj,
|
| 351 | struct kobj_attribute *attr, char *buf)
|
| 352 | {
|
| 353 | return sysfs_emit(buf, __stringify(MODULE_COMPRESSION) "\n");
|
| 354 | }
|
| 355 |
|
| 356 | static struct kobj_attribute module_compression_attr = __ATTR_RO(compression);
|
| 357 |
|
| 358 | static int __init module_decompress_sysfs_init(void)
|
| 359 | {
|
| 360 | int error;
|
| 361 |
|
| 362 | error = sysfs_create_file(&module_kset->kobj,
|
| 363 | &module_compression_attr.attr);
|
| 364 | if (error)
|
| 365 | pr_warn("Failed to create 'compression' attribute");
|
| 366 |
|
| 367 | return 0;
|
| 368 | }
|
| 369 | late_initcall(module_decompress_sysfs_init);
|
| 370 | #endif
|