[PATCH] omfs: require exact directory-name matches

From: shaos

Date: Thu Nov 26 2026 - 07:29:37 EST


Hello,

omfs_scan_list() compares an on-disk name with strncmp() using
only the requested name length. If a bucket contains "alphad" before
"alpha", looking up "alpha" accepts the longer name as a match.
Operations on the short name can then act on the wrong inode: the
reproducer reads the contents of "alphad" through "/alpha", and
unlink("/alpha") removes "alphad" while the real "alpha" remains.

Require the stored name length to equal namelen before comparing the
bytes. This keeps the bucket-chain lookup exact and prevents the
wrong directory entry from being removed.

The reproducer uses a crafted writable OMFS image. Mounting it
requires CAP_SYS_ADMIN; unlink additionally requires write permission
on the mounted directory. This is a filesystem correctness and data
integrity issue. No privilege escalation is demonstrated.

HEAD commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
git tree: fe970b05333fb8e41da21e9e289f652abcc7cba7
source version: Linux 7.4.0-rc4-g8d7fe9885c62
compiler: gcc 11.4.0, GNU ld 2.38

The Gist contains the console output, kernel configuration, PoC,
OMFS image builder, and init script:
https://gist.githubusercontent.com/shaos26/652a268fb0905bb1ada587cf8ab135db/raw/77a78f40e36b1853c4913832be9773a5d2147194/console-output.txt
https://gist.githubusercontent.com/shaos26/652a268fb0905bb1ada587cf8ab135db/raw/77a78f40e36b1853c4913832be9773a5d2147194/kernel-config.txt
https://gist.githubusercontent.com/shaos26/652a268fb0905bb1ada587cf8ab135db/raw/77a78f40e36b1853c4913832be9773a5d2147194/poc.c
https://gist.githubusercontent.com/shaos26/652a268fb0905bb1ada587cf8ab135db/raw/77a78f40e36b1853c4913832be9773a5d2147194/mkimage.py
https://gist.githubusercontent.com/shaos26/652a268fb0905bb1ada587cf8ab135db/raw/77a78f40e36b1853c4913832be9773a5d2147194/init

Build the kernel from the HEAD commit above with kernel-config.txt.
The PoC requires Python 3, static gcc, BusyBox, cpio, gzip, and QEMU:

work=$(mktemp -d)
python3 mkimage.py "$work/base.img"
gcc -O2 -static -Wall -Wextra -o "$work/omfs_probe" poc.c
mkdir -p "$work/rootfs/bin"
cp /usr/bin/busybox "$work/rootfs/bin/busybox"
for applet in sh mount umount poweroff dmesg ls; do
ln -s busybox "$work/rootfs/bin/$applet"
done
cp init "$work/rootfs/init"
cp "$work/omfs_probe" "$work/rootfs/omfs_probe"
(cd "$work/rootfs" && find . -print0 |
cpio --null -o --format=newc | gzip -9) > "$work/initrd.gz"
qemu-system-x86_64 -m 1G -smp 2 \
-kernel linux/build/arch/x86/boot/bzImage \
-initrd "$work/initrd.gz" \
-drive file="$work/base.img",if=virtio,format=raw,cache=none \
-append 'console=ttyS0 rdinit=/init panic=-1 oops=panic loglevel=7' \
-display none -serial file:omfs.log -monitor none -no-reboot

The test enumerates both names, opens both paths, compares their inode
numbers, unlinks "alpha", and checks the directory again after a
remount. The unpatched run shows that "alpha" reads and resolves to
the "alphad" inode and deletes "alphad". With this patch, "alpha"
resolves to its own inode, its unlink removes the exact entry, and
"alphad" remains readable after remount.

Fixes: a3ab7155ea21 ("omfs: add directory routines")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: shaos <shawnq1@xxxxxxx>
---
fs/omfs/dir.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/omfs/dir.c b/fs/omfs/dir.c
index 18f4b4543cc81..a00e62baf1cc7 100644
--- a/fs/omfs/dir.c
+++ b/fs/omfs/dir.c
@@ -53,7 +53,8 @@ static struct buffer_head *omfs_scan_list(struct inode *dir, u64 block,
goto err;
}

- if (strncmp(oi->i_name, name, namelen) == 0)
+ if (strnlen(oi->i_name, OMFS_NAMELEN) == namelen &&
+ memcmp(oi->i_name, name, namelen) == 0)
return bh;

*prev_block = block;
--
2.43.0