Skip to contentSkip to conversation

init: distinguish compressed ramdisks from image size

PATCH In review Zhiling Zou 2 participants 5 messages Oct 14 – Oct 16 VFS

lore
Share & tools
lore
#0 lore raw

cover 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
#1 lore raw

PATCH 1/1 init: distinguish compressed ramdisks from image size

identify_ramdisk_image() uses a zero block count to mark compressed images. A malformed legacy filesystem header can also produce a zero block count, causing rd_load_image() to call crd_load() with a NULL decompressor and panic.

Track whether the image is compressed separately from its block count. This keeps zero-sized or overflowed legacy image sizes on the normal validation path while preserving the handling of compressed images.

Fixes 30d65dbfe3ad ("bzip2/lzma: config and initramfs support for bzip2/lzma decompression")
Assisted-by LLM
---
 init/do_mounts_rd.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
init/do_mounts_rd.c +12 -10
index 48bfab2fc62f..f113469448e9 100644
@@ -31,9 +31,9 @@ static int __init crd_load(decompress_fn deco);
 
 /*
  * This routine tries to find a RAM disk image to load, and returns the
- * number of blocks to read for a non-compressed image, 0 if the image
- * is a compressed image, and -1 if an image with the right magic
- * numbers could not be found.
+ * number of blocks to read for a non-compressed image, and -1 if an
+ * image with the right magic numbers could not be found. The compressed
+ * flag is set if the image is compressed.
  *
  * We currently check for the following magic numbers:
  *	minix
@@ -50,7 +50,7 @@ static int __init crd_load(decompress_fn deco);
  */
 static int __init
 identify_ramdisk_image(struct file *file, loff_t pos,
-		decompress_fn *decompressor)
+		decompress_fn *decompressor, bool *compressed)
 {
 	const int size = 512;
 	struct minix_super_block *minixsb;
@@ -63,6 +63,7 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 	unsigned long n;
 	int start_block = rd_image_start;
 
+	*compressed = false;
 	buf = kmalloc(size, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
@@ -87,7 +88,7 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 			printk(KERN_EMERG
 			       "RAMDISK: %s decompressor not configured!\n",
 			       compress_name);
-		nblocks = 0;
+		*compressed = true;
 		goto done;
 	}
 
@@ -185,6 +186,7 @@ int __init rd_load_image(void)
 	char *buf = NULL;
 	unsigned short rotate = 0;
 	decompress_fn decompressor = NULL;
+	bool compressed;
 	char rotator[4] = { '|' , '/' , '-' , '\\' };
 
 	out_file = filp_open("/dev/ram", O_RDWR, 0);
@@ -196,15 +198,15 @@ int __init rd_load_image(void)
 		goto noclose_input;
 
 	in_pos = rd_image_start * BLOCK_SIZE;
-	nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor);
-	if (nblocks < 0)
-		goto done;
-
-	if (nblocks == 0) {
+	nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor,
+					 &compressed);
+	if (compressed) {
 		if (crd_load(decompressor) == 0)
 			goto successful_load;
 		goto done;
 	}
+	if (nblocks < 0)
+		goto done;
 
 	/*
 	 * NOTE NOTE: nblocks is not actually blocks but
Signature · 1 line
2.55.0
#2 lore raw
Quoted 80 lines · Zhiling Zou

identify_ramdisk_image() uses a zero block count to mark compressed images. A malformed legacy filesystem header can also produce a zero block count, causing rd_load_image() to call crd_load() with a NULL decompressor and panic.

Track whether the image is compressed separately from its block count. This keeps zero-sized or overflowed legacy image sizes on the normal validation path while preserving the handling of compressed images.

Fixes 30d65dbfe3ad ("bzip2/lzma: config and initramfs support for bzip2/lzma decompression")
Assisted-by LLM

---
init/do_mounts_rd.c | 22 ++++++++++++----------

1 file changed, 12 insertions(+), 10 deletions(-)
init/do_mounts_rd.c +3 -3
index 48bfab2fc62f..f113469448e9 100644
@@ -31,9 +31,9 @@ static int __init crd_load(decompress_fn deco);
 
 /*
 * This routine tries to find a RAM disk image to load, and returns the
- * number of blocks to read for a non-compressed image, 0 if the image
- * is a compressed image, and -1 if an image with the right magic
- * numbers could not be found.
+ * number of blocks to read for a non-compressed image, and -1 if an
+ * image with the right magic numbers could not be found. The compressed
+ * flag is set if the image is compressed.
 *
 * We currently check for the following magic numbers:
 *	minix
@@ -50,7 +50,7 @@ static int __init crd_load(decompress_fn deco);
 */

static int __init identify_ramdisk_image(struct file *file, loff_t pos,

init/do_mounts_rd.c +1 -1
-		decompress_fn *decompressor)
+		decompress_fn *decompressor, bool *compressed)
{
	const int size = 512;
	struct minix_super_block *minixsb;
init/do_mounts_rd.c +5 -6
@@ -63,6 +63,7 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 	unsigned long n;
 	int start_block = rd_image_start;
 
+	*compressed = false;
 	buf = kmalloc(size, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
@@ -87,7 +88,7 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 			printk(KERN_EMERG
 			       "RAMDISK: %s decompressor not configured!\n",
 			       compress_name);
-		nblocks = 0;
+		*compressed = true;
 		goto done;
 	}
 
@@ -185,6 +186,7 @@ int __init rd_load_image(void)
 	char *buf = NULL;
 	unsigned short rotate = 0;
 	decompress_fn decompressor = NULL;
+	bool compressed;
 	char rotator[4] = { '|' , '/' , '-' , '\\' };
 
 	out_file = filp_open("/dev/ram", O_RDWR, 0);
@@ -196,15 +198,15 @@ int __init rd_load_image(void)
 		goto noclose_input;
 
 	in_pos = rd_image_start * BLOCK_SIZE;
-	nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor);
-	if (nblocks < 0)
-		goto done;
-
-	if (nblocks == 0) {
+	nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor,
+					 &compressed);

init/do_mounts_rd.c int __init rd_load_image(void) · line 212

Quoted 10 lines
init/do_mounts_rd.c +3 -0
+	if (compressed) {
 		if (crd_load(decompressor) == 0)
 			goto successful_load;
 		goto done;
 	}
+	if (nblocks < 0)
+		goto done;
 
 	/*
 	 * NOTE NOTE: nblocks is not actually blocks but

Why the hell do we still support legacy filesystem boot ramdisks?

author
#3 lore raw
Quoted 90 lines · H. Peter Anvin
Quoted 88 lines · Zhiling Zou

identify_ramdisk_image() uses a zero block count to mark compressed images. A malformed legacy filesystem header can also produce a zero block count, causing rd_load_image() to call crd_load() with a NULL decompressor and panic.

Track whether the image is compressed separately from its block count. This keeps zero-sized or overflowed legacy image sizes on the normal validation path while preserving the handling of compressed images.

Fixes 30d65dbfe3ad ("bzip2/lzma: config and initramfs support for bzip2/lzma decompression")
Assisted-by LLM

---
init/do_mounts_rd.c | 22 ++++++++++++----------

1 file changed, 12 insertions(+), 10 deletions(-)
init/do_mounts_rd.c +3 -3
index 48bfab2fc62f..f113469448e9 100644
@@ -31,9 +31,9 @@ static int __init crd_load(decompress_fn deco);
 
 /*
 * This routine tries to find a RAM disk image to load, and returns the
- * number of blocks to read for a non-compressed image, 0 if the image
- * is a compressed image, and -1 if an image with the right magic
- * numbers could not be found.
+ * number of blocks to read for a non-compressed image, and -1 if an
+ * image with the right magic numbers could not be found. The compressed
+ * flag is set if the image is compressed.
 *
 * We currently check for the following magic numbers:
 *    minix
@@ -50,7 +50,7 @@ static int __init crd_load(decompress_fn deco);
 */

static int __init identify_ramdisk_image(struct file *file, loff_t pos,

init/do_mounts_rd.c +1 -1
-              decompress_fn *decompressor)
+              decompress_fn *decompressor, bool *compressed)
{
      const int size = 512;
      struct minix_super_block *minixsb;
init/do_mounts_rd.c +8 -6
@@ -63,6 +63,7 @@ identify_ramdisk_image(struct file *file, loff_t pos,
      unsigned long n;
      int start_block = rd_image_start;
 
+      *compressed = false;
      buf = kmalloc(size, GFP_KERNEL);
      if (!buf)
              return -ENOMEM;
@@ -87,7 +88,7 @@ identify_ramdisk_image(struct file *file, loff_t pos,
                      printk(KERN_EMERG
                             "RAMDISK: %s decompressor not configured!\n",
                             compress_name);
-              nblocks = 0;
+              *compressed = true;
              goto done;
      }
 
@@ -185,6 +186,7 @@ int __init rd_load_image(void)
      char *buf = NULL;
      unsigned short rotate = 0;
      decompress_fn decompressor = NULL;
+      bool compressed;
      char rotator[4] = { '|' , '/' , '-' , '\\' };
 
      out_file = filp_open("/dev/ram", O_RDWR, 0);
@@ -196,15 +198,15 @@ int __init rd_load_image(void)
              goto noclose_input;
 
      in_pos = rd_image_start * BLOCK_SIZE;
-      nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor);
-      if (nblocks < 0)
-              goto done;
-
-      if (nblocks == 0) {
+      nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor,
+                                       &compressed);
+      if (compressed) {
              if (crd_load(decompressor) == 0)
                      goto successful_load;
              goto done;
      }
+      if (nblocks < 0)
+              goto done;
 
Quoted 4 lines

init/do_mounts_rd.c int __init rd_load_image(void) · line 212

Quoted 2 lines
init/do_mounts_rd.c +0 -0
      /*
       * NOTE NOTE: nblocks is not actually blocks but

Why the hell do we still support legacy filesystem boot ramdisks?

I’m sorry for the noise. I overlooked that legacy filesystem ramdisk support is deprecated and should have taken that into account before sending this patch. I’ll withdraw it.

#4 lore raw
Quoted 91 lines · zhilin zou
Quoted 90 lines · H. Peter Anvin
Quoted 88 lines · Zhiling Zou

identify_ramdisk_image() uses a zero block count to mark compressed images. A malformed legacy filesystem header can also produce a zero block count, causing rd_load_image() to call crd_load() with a NULL decompressor and panic.

Track whether the image is compressed separately from its block count. This keeps zero-sized or overflowed legacy image sizes on the normal validation path while preserving the handling of compressed images.

Fixes 30d65dbfe3ad ("bzip2/lzma: config and initramfs support for bzip2/lzma decompression")
Assisted-by LLM

---
init/do_mounts_rd.c | 22 ++++++++++++----------

1 file changed, 12 insertions(+), 10 deletions(-)
init/do_mounts_rd.c +3 -3
index 48bfab2fc62f..f113469448e9 100644
@@ -31,9 +31,9 @@ static int __init crd_load(decompress_fn deco);
 
 /*
 * This routine tries to find a RAM disk image to load, and returns the
- * number of blocks to read for a non-compressed image, 0 if the image
- * is a compressed image, and -1 if an image with the right magic
- * numbers could not be found.
+ * number of blocks to read for a non-compressed image, and -1 if an
+ * image with the right magic numbers could not be found. The compressed
+ * flag is set if the image is compressed.
 *
 * We currently check for the following magic numbers:
 *    minix
@@ -50,7 +50,7 @@ static int __init crd_load(decompress_fn deco);
 */

static int __init identify_ramdisk_image(struct file *file, loff_t pos,

init/do_mounts_rd.c +1 -1
-              decompress_fn *decompressor)
+              decompress_fn *decompressor, bool *compressed)
{
      const int size = 512;
      struct minix_super_block *minixsb;
init/do_mounts_rd.c +8 -6
@@ -63,6 +63,7 @@ identify_ramdisk_image(struct file *file, loff_t pos,
      unsigned long n;
      int start_block = rd_image_start;
 
+      *compressed = false;
      buf = kmalloc(size, GFP_KERNEL);
      if (!buf)
              return -ENOMEM;
@@ -87,7 +88,7 @@ identify_ramdisk_image(struct file *file, loff_t pos,
                      printk(KERN_EMERG
                             "RAMDISK: %s decompressor not configured!\n",
                             compress_name);
-              nblocks = 0;
+              *compressed = true;
              goto done;
      }
 
@@ -185,6 +186,7 @@ int __init rd_load_image(void)
      char *buf = NULL;
      unsigned short rotate = 0;
      decompress_fn decompressor = NULL;
+      bool compressed;
      char rotator[4] = { '|' , '/' , '-' , '\\' };
 
      out_file = filp_open("/dev/ram", O_RDWR, 0);
@@ -196,15 +198,15 @@ int __init rd_load_image(void)
              goto noclose_input;
 
      in_pos = rd_image_start * BLOCK_SIZE;
-      nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor);
-      if (nblocks < 0)
-              goto done;
-
-      if (nblocks == 0) {
+      nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor,
+                                       &compressed);
+      if (compressed) {
              if (crd_load(decompressor) == 0)
                      goto successful_load;
              goto done;
      }
+      if (nblocks < 0)
+              goto done;
 
Quoted 9 lines
Quoted 4 lines

init/do_mounts_rd.c int __init rd_load_image(void) · line 212

Quoted 2 lines
init/do_mounts_rd.c +0 -0
      /*
       * NOTE NOTE: nblocks is not actually blocks but

Why the hell do we still support legacy filesystem boot ramdisks?

I’m sorry for the noise. I overlooked that legacy filesystem ramdisk support is deprecated and should have taken that into account before sending this patch. I’ll withdraw it.

This would be a good time to mark in BROKEN.