1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* Copyright © 2024 Intel Corporation
*/
#define DLSYM_LOCALLY_ENABLED ENABLE_ZSTD_DLOPEN
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <zstd.h>
#include <shared/elf-note.h>
#include <shared/util.h>
#include "libkmod.h"
#include "libkmod-internal.h"
#include "libkmod-internal-file.h"
#define DL_SYMBOL_TABLE(M) \
M(ZSTD_decompress) \
M(ZSTD_getErrorName) \
M(ZSTD_getFrameContentSize) \
M(ZSTD_isError)
DL_SYMBOL_TABLE(DECLARE_SYM)
static int dlopen_zstd(void)
{
#if !DLSYM_LOCALLY_ENABLED
return 0;
#else
static void *dl = NULL;
ELF_NOTE_DLOPEN("zstd", "Support for uncompressing zstd-compressed modules",
ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED, "libzstd.so.1");
return dlsym_many(&dl, "libzstd.so.1", DL_SYMBOL_TABLE(DLSYM_ARG) NULL);
#endif
}
int kmod_file_load_zstd(struct kmod_file *file)
{
void *src_buf = MAP_FAILED, *dst_buf = NULL;
size_t src_size, dst_size;
unsigned long long frame_size;
struct stat st;
int ret;
ret = dlopen_zstd();
if (ret < 0) {
ERR(file->ctx, "zstd: can't load and resolve symbols (%s)",
strerror(-ret));
return -EINVAL;
}
if (fstat(file->fd, &st) < 0) {
ret = -errno;
ERR(file->ctx, "zstd: %m\n");
goto out;
}
if ((uintmax_t)st.st_size > SIZE_MAX) {
ret = -ENOMEM;
goto out;
}
src_size = st.st_size;
src_buf = mmap(NULL, src_size, PROT_READ, MAP_PRIVATE, file->fd, 0);
if (src_buf == MAP_FAILED) {
ret = -errno;
goto out;
}
frame_size = sym_ZSTD_getFrameContentSize(src_buf, src_size);
if (frame_size == 0 || frame_size == ZSTD_CONTENTSIZE_UNKNOWN ||
frame_size == ZSTD_CONTENTSIZE_ERROR) {
ret = -EINVAL;
ERR(file->ctx, "zstd: Failed to determine decompression size\n");
goto out;
}
if (frame_size > SIZE_MAX) {
ret = -ENOMEM;
goto out;
}
dst_size = frame_size;
dst_buf = malloc(dst_size);
if (dst_buf == NULL) {
ret = -errno;
goto out;
}
dst_size = sym_ZSTD_decompress(dst_buf, dst_size, src_buf, src_size);
if (sym_ZSTD_isError(dst_size)) {
ERR(file->ctx, "zstd: %s\n", sym_ZSTD_getErrorName(dst_size));
ret = -EINVAL;
goto out;
}
file->memory = dst_buf;
file->size = dst_size;
ret = 0;
dst_buf = NULL;
out:
free(dst_buf);
if (src_buf != MAP_FAILED)
munmap(src_buf, src_size);
return ret;
}
|