Skip to contentSkip to conversation

hpfs: return an error instead of hitting BUG()

PATCH New shaos 1 participant 1 message Nov 28 Hpfs filesystem

lore
Share & tools
lore

shaos

author
#0 lore raw

PATCH 1/1 hpfs: return an error instead of hitting BUG()

Hello,

When hpfs_get_block() is asked to create a block that does not match mmu_private, it executes BUG() even though the function has an existing -EIO return path immediately below. The mismatch is reachable when a write follows a failed lookup of a sector in a malformed HPFS image. The write therefore oopses the kernel instead of returning an I/O error.

Remove the BUG() and use the existing error path. The trigger mounts a crafted HPFS image read-write and writes one byte to a file. With the patch, pwrite() returns -EIO and the kernel remains alive; writing to the valid control image succeeds.

HEAD commit:    8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
git tree:       fe970b05333fb8e41da21e9e289f652abcc7cba7
source version: v7.3-23088-g8d7fe9885c62f (Linux 7.4.0-rc4)
compiler:       gcc 11.4.0, GNU ld 2.38

console output:
https://gist.githubusercontent.com/shaos26/384b45e91d728eb7cda8291e3f7f996c/raw/8d91f0fa714fecba04be70a22b69bb5f42fc4213/console-output.txt
kernel config:
https://gist.githubusercontent.com/shaos26/384b45e91d728eb7cda8291e3f7f996c/raw/8d91f0fa714fecba04be70a22b69bb5f42fc4213/kernel-config.txt
reproducer:
https://gist.githubusercontent.com/shaos26/384b45e91d728eb7cda8291e3f7f996c/raw/8d91f0fa714fecba04be70a22b69bb5f42fc4213/poc.c
image generator:
https://gist.githubusercontent.com/shaos26/384b45e91d728eb7cda8291e3f7f996c/raw/8d91f0fa714fecba04be70a22b69bb5f42fc4213/hpfs_image_gen.c
initramfs init:
https://gist.githubusercontent.com/shaos26/384b45e91d728eb7cda8291e3f7f996c/raw/8d91f0fa714fecba04be70a22b69bb5f42fc4213/init

Download the Gist files into one working directory:

    gist_id=384b45e91d728eb7cda8291e3f7f996c
    gist_revision=8d91f0fa714fecba04be70a22b69bb5f42fc4213
    gist_base=https://gist.githubusercontent.com/shaos26
    gist=$gist_base/$gist_id/raw/$gist_revision
    for file in console-output.txt kernel-config.txt poc.c \
        hpfs_image_gen.c init; do
        curl -fL "$gist/$file" -o "$file"
    done

Clone the kernel and build it from the specified revision and config:

git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux
git checkout 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
mkdir build
cp ../kernel-config.txt build/.config
make O=build olddefconfig
make O=build -j"$(nproc)" bzImage
cd ..

Build the image and initramfs:

gcc -O2 -Wall -Wextra -Werror -o hpfs_image_gen hpfs_image_gen.c gcc -O2 -static -Wall -Wextra -Werror -o poc poc.c ./hpfs_image_gen /tmp/hpfs-positive.img 513 ./hpfs_image_gen /tmp/hpfs-control.img 513 mapped mkdir -p root/bin root/usr/bin root/mnt
cp /usr/bin/busybox root/bin/busybox
root/bin/busybox --install -s root/bin
cp poc root/usr/bin/poc
cp init root/init
(cd root && find . -print0 | cpio --null -o -H newc) | gzip -9 \
> initramfs.cpio.gz

Run the positive and mapped-extent control images:

    timeout 60 qemu-system-x86_64 -m 2048 -smp 1 \
        -kernel linux/build/arch/x86/boot/bzImage \
        -initrd initramfs.cpio.gz \
        -append 'console=ttyS0,115200 panic=1 oops=panic loglevel=4 rdinit=/init' \
        -drive file=/tmp/hpfs-positive.img,format=raw,if=virtio \
        -display none -serial file:/tmp/hpfs-positive.log -monitor none -no-reboot
    timeout 60 qemu-system-x86_64 -m 2048 -smp 1 \
        -kernel linux/build/arch/x86/boot/bzImage \
        -initrd initramfs.cpio.gz \
        -append 'console=ttyS0,115200 panic=1 oops=panic loglevel=4 rdinit=/init' \
        -drive file=/tmp/hpfs-control.img,format=raw,if=virtio \
        -display none -serial file:/tmp/hpfs-control.log -monitor none -no-reboot

With the patch, the malformed image's write returns -EIO and the kernel survives. The mapped-extent control returns one byte written. The unfixed kernel hits BUG() in hpfs_get_block() for the positive image.

Mounting the crafted image requires CAP_SYS_ADMIN. This reproduces a kernel BUG on malformed filesystem data; it does not demonstrate privilege escalation or memory disclosure.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

Thanks,

shaos

---

fs/hpfs/file.c +0 -1
index ef7eb601be09b..13f375a8b2c1a 100644
@@ -99,7 +99,6 @@ static int hpfs_get_block(struct inode *inode, sector_t iblock, struct buffer_he
 	}
 	if (!create) goto ret_0;
 	if (iblock<<9 != hpfs_i(inode)->mmu_private) {
-		BUG();
 		r = -EIO;
 		goto ret_r;
 	}