init: distinguish compressed ramdisks from image size
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest1 patch 3 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/1 | init: distinguish compressed ramdisks from image sizeinit/ | +12 | 3 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH 0/1] init: distinguish compressed ramdisks from image size
Hi Linux kernel maintainers,
We found and validated an issue in init/do_mounts_rd.c. The bug is reachable by a root user who boots a crafted external initrd with kexec_file_load().
We've tested it, and it should not affect any other functionality.
We will provide detailed information about the bug in this email, along with a PoC to trigger it.
---- details below ----
Bug details:
identify_ramdisk_image() uses nblocks == 0 to mark a compressed image. The same value can result from a malformed legacy filesystem header, including a SquashFS superblock with bytes_used == 0. In that case no compression format was detected and decompressor remains NULL.
When the external initrd is preserved as /initrd.image, rd_load_image() passes that NULL decompressor to crd_load(), which panics with "Could not decompress initial ramdisk image".
Track compression detection separately from the legacy image block count. A zero-sized or overflowed legacy image now follows the normal size validation path, while compressed images retain their existing handling, including the missing-decompressor diagnostic.
Reproducer:
chmod +x poc.sh
./poc.sh /root/bzImage
The reproducer was run as root in a 2 vCPU, 2 GB x86 QEMU environment.
------BEGIN poc.sh------
#!/bin/sh
set -eu
if [ "$(id -u)" -ne 0 ]; then
echo "run as root" >&2
exit 1
fi
KERNEL_IMAGE="${1:-/root/bzImage}"
INITRD="/tmp/bad-initrd.bin"
CMDLINE="root=/dev/sda rw console=ttyS0 quiet loglevel=3 systemd.show_status=false panic=-1 panic_on_warn=0 oops=panic"
if [ ! -r "$KERNEL_IMAGE" ]; then
echo "kernel image not readable: $KERNEL_IMAGE" >&2
exit 1
fi
python3 - <<'PY'
from pathlib import Path
p = Path("/tmp/bad-initrd.bin")
data = bytearray(4096)
data[0:4] = b"hsqs"
p.write_bytes(data)
PY
kexec -u || true
kexec -s -l "$KERNEL_IMAGE" --initrd="$INITRD" --command-line="$CMDLINE"
kexec -s -e------END poc.sh--------
----BEGIN crash log----
[ 4.114775][ T1] Invalid ramdisk decompression routine. Select appropriate config option. [ 4.116143][ T1] Kernel panic - not syncing: Could not decompress initial ramdisk image. [ 4.117281][ T1] CPU: 3 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.12.95 #4 53deed68f5e8c87eac9412ec2ca16e6c37d9fe62 [ 4.118753][ T1] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 4.120369][ T1] Call Trace: [ 4.120845][ T1] <TASK> [ 4.121266][ T1] panic+0x544/0x620 [ 4.121840][ T1] ? __pfx_panic+0x10/0x10 [ 4.122436][ T1] ? srso_alias_return_thunk+0x5/0xfbef5 [ 4.123231][ T1] ? srso_alias_return_thunk+0x5/0xfbef5 [ 4.123961][ T1] ? kfree+0x143/0x4a0 [ 4.124541][ T1] rd_load_image+0x970/0xae0 [ 4.125192][ T1] ? __pfx_rd_load_image+0x10/0x10 [ 4.125867][ T1] ? init_mknod+0x16c/0x300 [ 4.126499][ T1] ? kmem_cache_alloc_noprof+0x208/0x3e0 [ 4.127230][ T1] ? __pfx_init_mknod+0x10/0x10 [ 4.127881][ T1] ? srso_alias_return_thunk+0x5/0xfbef5 [ 4.128618][ T1] ? __asan_memcpy+0x3c/0x60 [ 4.129223][ T1] ? srso_alias_return_thunk+0x5/0xfbef5 [ 4.129975][ T1] initrd_load+0x48/0x350 [ 4.130559][ T1] prepare_namespace+0x8b/0x2d0 [ 4.131202][ T1] ? __pfx_prepare_namespace+0x10/0x10 [ 4.131935][ T1] kernel_init_freeable+0x6d0/0x960 [ 4.132634][ T1] ? __pfx_kernel_init+0x10/0x10 [ 4.133296][ T1] kernel_init+0x1f/0x1e0 [ 4.133877][ T1] ? _raw_spin_unlock_irq+0x28/0x40 [ 4.134574][ T1] ret_from_fork+0x31/0x70 [ 4.135152][ T1] ? __pfx_kernel_init+0x10/0x10 [ 4.135808][ T1] ret_from_fork_asm+0x1a/0x30 [ 4.136505][ T1] </TASK> [ 4.137158][ T1] Kernel Offset: 0x9000000 from 0xffffffff81000000 (relocation range: 0xffffffff80000000-0xffffffffbfffffff)
-----END crash log-----
Best regards,
Zhiling Zou
Zhiling Zou (1):
init: distinguish compressed ramdisks from image size
init/do_mounts_rd.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
Signature · 1 line
2.55.0