| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/stat.h> |
| 4 |
#include <sys/utsname.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "compress.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "io-util.h" |
| 11 |
#include "reboot-util.h" |
| 12 |
#include "string-util.h" |
| 13 |
#include "tests.h" |
| 14 |
#include "tmpfile-util.h" |
| 15 |
#include "unaligned.h" |
| 16 |
|
| 17 |
static int find_kernel_image(char **ret) { |
| 18 |
struct utsname u; |
| 19 |
|
| 20 |
ASSERT_OK_ERRNO(uname(&u)); |
| 21 |
|
| 22 |
/* Kernel image names vary across architectures and distributions: |
| 23 |
* vmlinuz — compressed Linux kernel (x86, most distros) |
| 24 |
* vmlinux — uncompressed ELF kernel (ppc64, s390) |
| 25 |
* Image — uncompressed flat binary (arm64, riscv) |
| 26 |
* Image.gz — gzip-compressed Image (arm64) |
| 27 |
* zImage — compressed kernel (arm 32-bit) |
| 28 |
* vmlinuz.efi — EFI ZBOOT PE wrapper (arm64 with CONFIG_EFI_ZBOOT) */ |
| 29 |
static const char *const names[] = { |
| 30 |
"vmlinuz", |
| 31 |
"vmlinux", |
| 32 |
"Image", |
| 33 |
"Image.gz", |
| 34 |
"zImage", |
| 35 |
"vmlinuz.efi", |
| 36 |
}; |
| 37 |
|
| 38 |
/* Try /usr/lib/modules/<version>/<name> first (kernel-install convention), |
| 39 |
* then /boot/<name>-<version>, then /boot/<name> */ |
| 40 |
for (size_t i = 0; i < ELEMENTSOF(names); i++) { |
| 41 |
_cleanup_free_ char *path = NULL; |
| 42 |
|
| 43 |
path = strjoin("/usr/lib/modules/", u.release, "/", names[i]); |
| 44 |
if (!path) |
| 45 |
return -ENOMEM; |
| 46 |
|
| 47 |
if (access(path, R_OK) >= 0) { |
| 48 |
*ret = TAKE_PTR(path); |
| 49 |
return 0; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/* /boot may not be accessible without root, skip gracefully */ |
| 54 |
if (access("/boot", R_OK) >= 0) { |
| 55 |
for (size_t i = 0; i < ELEMENTSOF(names); i++) { |
| 56 |
_cleanup_free_ char *path = NULL; |
| 57 |
|
| 58 |
path = strjoin("/boot/", names[i], "-", u.release); |
| 59 |
if (!path) |
| 60 |
return -ENOMEM; |
| 61 |
|
| 62 |
if (access(path, R_OK) >= 0) { |
| 63 |
*ret = TAKE_PTR(path); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
for (size_t i = 0; i < ELEMENTSOF(names); i++) { |
| 69 |
_cleanup_free_ char *path = NULL; |
| 70 |
|
| 71 |
path = strjoin("/boot/", names[i]); |
| 72 |
if (!path) |
| 73 |
return -ENOMEM; |
| 74 |
|
| 75 |
if (access(path, R_OK) >= 0) { |
| 76 |
*ret = TAKE_PTR(path); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return -ENOENT; |
| 83 |
} |
| 84 |
|
| 85 |
TEST(passthrough_unrecognized) { |
| 86 |
/* A file with unrecognized magic should pass through as-is (return 0) */ |
| 87 |
_cleanup_close_ int fd = -EBADF; |
| 88 |
_cleanup_(unlink_tempfilep) char path[] = "/tmp/test-kexec.XXXXXX"; |
| 89 |
|
| 90 |
ASSERT_OK(fd = mkostemp_safe(path)); |
| 91 |
ASSERT_OK_EQ_ERRNO(write(fd, "HELLO WORLD\0", 12), 12); |
| 92 |
ASSERT_OK_ERRNO(lseek(fd, 0, SEEK_SET)); |
| 93 |
|
| 94 |
_cleanup_close_ int kernel_fd = -EBADF, initrd_fd = -EBADF; |
| 95 |
ASSERT_OK_ZERO(kexec_maybe_decompress_kernel(path, fd, &kernel_fd, &initrd_fd)); |
| 96 |
ASSERT_EQ(kernel_fd, -EBADF); |
| 97 |
ASSERT_EQ(initrd_fd, -EBADF); |
| 98 |
} |
| 99 |
|
| 100 |
TEST(gzip_round_trip) { |
| 101 |
_cleanup_close_ int src_fd = -EBADF, gz_fd = -EBADF; |
| 102 |
_cleanup_(unlink_tempfilep) char |
| 103 |
src_path[] = "/tmp/test-kexec-src.XXXXXX", |
| 104 |
gz_path[] = "/tmp/test-kexec-gz.XXXXXX"; |
| 105 |
int r; |
| 106 |
|
| 107 |
r = dlopen_zlib(LOG_DEBUG); |
| 108 |
if (r < 0) { |
| 109 |
log_tests_skipped("zlib not available"); |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
/* Create a source file with known content */ |
| 114 |
ASSERT_OK(src_fd = mkostemp_safe(src_path)); |
| 115 |
char buf[4096]; |
| 116 |
memset(buf, 'A', sizeof(buf)); |
| 117 |
ASSERT_OK(loop_write(src_fd, buf, sizeof(buf))); |
| 118 |
|
| 119 |
/* Compress it with gzip */ |
| 120 |
ASSERT_OK_ERRNO(lseek(src_fd, 0, SEEK_SET)); |
| 121 |
ASSERT_OK(gz_fd = mkostemp_safe(gz_path)); |
| 122 |
ASSERT_OK(compress_stream(COMPRESSION_GZIP, src_fd, gz_fd, UINT64_MAX, NULL)); |
| 123 |
|
| 124 |
/* Feed the gzip file to kexec_maybe_decompress_kernel */ |
| 125 |
ASSERT_OK_ERRNO(lseek(gz_fd, 0, SEEK_SET)); |
| 126 |
|
| 127 |
_cleanup_close_ int kernel_fd = -EBADF, initrd_fd = -EBADF; |
| 128 |
ASSERT_OK_POSITIVE(kexec_maybe_decompress_kernel(gz_path, gz_fd, &kernel_fd, &initrd_fd)); |
| 129 |
ASSERT_GE(kernel_fd, 0); |
| 130 |
ASSERT_EQ(initrd_fd, -EBADF); |
| 131 |
|
| 132 |
/* Verify the decompressed content matches the original */ |
| 133 |
char result[4096]; |
| 134 |
ASSERT_OK_EQ_ERRNO(pread(kernel_fd, result, sizeof(result), 0), (ssize_t) sizeof(result)); |
| 135 |
ASSERT_EQ(memcmp(buf, result, sizeof(buf)), 0); |
| 136 |
} |
| 137 |
|
| 138 |
TEST(zboot_synthetic) { |
| 139 |
/* Construct a minimal ZBOOT header with a gzip-compressed payload */ |
| 140 |
_cleanup_close_ int src_fd = -EBADF, gz_fd = -EBADF, zboot_fd = -EBADF; |
| 141 |
_cleanup_(unlink_tempfilep) char |
| 142 |
src_path[] = "/tmp/test-kexec-zboot-src.XXXXXX", |
| 143 |
gz_path[] = "/tmp/test-kexec-zboot-gz.XXXXXX", |
| 144 |
zboot_path[] = "/tmp/test-kexec-zboot.XXXXXX"; |
| 145 |
int r; |
| 146 |
|
| 147 |
r = dlopen_zlib(LOG_DEBUG); |
| 148 |
if (r < 0) { |
| 149 |
log_tests_skipped("zlib not available"); |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
/* Create and compress a payload */ |
| 154 |
char payload[512]; |
| 155 |
memset(payload, 'K', sizeof(payload)); |
| 156 |
|
| 157 |
ASSERT_OK(src_fd = mkostemp_safe(src_path)); |
| 158 |
ASSERT_OK(loop_write(src_fd, payload, sizeof(payload))); |
| 159 |
ASSERT_OK_ERRNO(lseek(src_fd, 0, SEEK_SET)); |
| 160 |
|
| 161 |
ASSERT_OK(gz_fd = mkostemp_safe(gz_path)); |
| 162 |
ASSERT_OK(compress_stream(COMPRESSION_GZIP, src_fd, gz_fd, UINT64_MAX, NULL)); |
| 163 |
|
| 164 |
/* Read the compressed data */ |
| 165 |
struct stat st; |
| 166 |
ASSERT_OK_ERRNO(fstat(gz_fd, &st)); |
| 167 |
size_t compressed_size = st.st_size; |
| 168 |
_cleanup_free_ void *compressed = malloc(compressed_size); |
| 169 |
ASSERT_NOT_NULL(compressed); |
| 170 |
ASSERT_OK_EQ_ERRNO(pread(gz_fd, compressed, compressed_size, 0), (ssize_t) compressed_size); |
| 171 |
|
| 172 |
/* Build the ZBOOT header: |
| 173 |
* 0x00: "MZ" |
| 174 |
* 0x04: "zimg" |
| 175 |
* 0x08: payload offset (LE32) |
| 176 |
* 0x0C: payload size (LE32) |
| 177 |
* 0x18: "gzip\0" */ |
| 178 |
uint8_t header[0x40] = {}; |
| 179 |
uint32_t payload_offset = sizeof(header); |
| 180 |
|
| 181 |
header[0] = 'M'; |
| 182 |
header[1] = 'Z'; |
| 183 |
memcpy(header + 0x04, "zimg", 4); |
| 184 |
unaligned_write_le32(header + 0x08, payload_offset); |
| 185 |
unaligned_write_le32(header + 0x0C, (uint32_t) compressed_size); |
| 186 |
memcpy(header + 0x18, "gzip", 5); |
| 187 |
|
| 188 |
ASSERT_OK(zboot_fd = mkostemp_safe(zboot_path)); |
| 189 |
ASSERT_OK(loop_write(zboot_fd, header, sizeof(header))); |
| 190 |
ASSERT_OK(loop_write(zboot_fd, compressed, compressed_size)); |
| 191 |
ASSERT_OK_ERRNO(lseek(zboot_fd, 0, SEEK_SET)); |
| 192 |
|
| 193 |
/* Test extraction */ |
| 194 |
_cleanup_close_ int kernel_fd = -EBADF, initrd_fd = -EBADF; |
| 195 |
ASSERT_OK_POSITIVE(kexec_maybe_decompress_kernel(zboot_path, zboot_fd, &kernel_fd, &initrd_fd)); |
| 196 |
ASSERT_GE(kernel_fd, 0); |
| 197 |
|
| 198 |
/* Verify decompressed content matches original payload */ |
| 199 |
char result[512]; |
| 200 |
ASSERT_OK_EQ_ERRNO(pread(kernel_fd, result, sizeof(result), 0), (ssize_t) sizeof(result)); |
| 201 |
ASSERT_EQ(memcmp(payload, result, sizeof(payload)), 0); |
| 202 |
} |
| 203 |
|
| 204 |
TEST(system_kernel) { |
| 205 |
_cleanup_free_ char *path = NULL; |
| 206 |
_cleanup_close_ int fd = -EBADF; |
| 207 |
int r; |
| 208 |
|
| 209 |
r = find_kernel_image(&path); |
| 210 |
if (r < 0) { |
| 211 |
log_tests_skipped_errno(r, "No kernel image found on this system"); |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
log_info("Found kernel image: %s", path); |
| 216 |
|
| 217 |
fd = open(path, O_RDONLY|O_CLOEXEC); |
| 218 |
if (fd < 0) { |
| 219 |
log_tests_skipped_errno(errno, "Cannot open kernel image '%s'", path); |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
_cleanup_close_ int kernel_fd = -EBADF, initrd_fd = -EBADF; |
| 224 |
ASSERT_OK(r = kexec_maybe_decompress_kernel(path, fd, &kernel_fd, &initrd_fd)); |
| 225 |
|
| 226 |
if (r == 0) { |
| 227 |
log_info("Kernel image was not compressed (passed through as-is)."); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
log_info("Kernel image was decompressed/extracted successfully."); |
| 232 |
ASSERT_GE(kernel_fd, 0); |
| 233 |
|
| 234 |
/* Verify the decompressed result is non-empty and looks plausible */ |
| 235 |
struct stat st; |
| 236 |
ASSERT_OK_ERRNO(fstat(kernel_fd, &st)); |
| 237 |
ASSERT_GT(st.st_size, 0); |
| 238 |
log_info("Decompressed kernel size: %zu bytes", (size_t) st.st_size); |
| 239 |
|
| 240 |
/* Read the first bytes and check for known kernel magic */ |
| 241 |
uint8_t magic[8]; |
| 242 |
ASSERT_OK_EQ_ERRNO(pread(kernel_fd, magic, sizeof(magic), 0), (ssize_t) sizeof(magic)); |
| 243 |
|
| 244 |
if (magic[0] == 0x7f && magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') |
| 245 |
log_info("Decompressed kernel is an ELF image."); |
| 246 |
else if (magic[0] == 'M' && magic[1] == 'Z') |
| 247 |
log_info("Decompressed kernel is a PE image."); |
| 248 |
else |
| 249 |
log_info("Decompressed kernel magic: %02x %02x %02x %02x %02x %02x %02x %02x", |
| 250 |
magic[0], magic[1], magic[2], magic[3], |
| 251 |
magic[4], magic[5], magic[6], magic[7]); |
| 252 |
|
| 253 |
/* If a UKI initrd was extracted, verify it too */ |
| 254 |
if (initrd_fd >= 0) { |
| 255 |
ASSERT_OK_ERRNO(fstat(initrd_fd, &st)); |
| 256 |
ASSERT_GT(st.st_size, 0); |
| 257 |
log_info("Extracted initrd size: %zu bytes", (size_t) st.st_size); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
DEFINE_TEST_MAIN(LOG_DEBUG); |