jfs: reject self-linked directory tree pages
Subsystems (2)
PATCH 1/1 jfs: reject self-linked directory tree pages
A malformed JFS directory leaf can point its next or previous sibling link back to its own block. check_dtpage() validates the page's slot metadata but accepts either self-link.
For an empty leftmost leaf, jfs_readdir() emits no entries from the page and follows header.next without making progress. A single getdents64() then loops in kernel context. A crafted image reproduces this on a read-only JFS mount.
Reject a non-root directory page whose sibling link names its own block. Save the requested block number before BT_GETPAGE() so the validator can compare the page links with the block that was read. If the leftmost page is rejected, leave the directory position at EOF so a later getdents64() does not retry the corrupt page after returning the already emitted dot entries.
The reproducer uses a 16 MiB JFS image with one valid empty leaf at block 112. The trigger has header.next=112; the control changes only that field to zero.
HEAD commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc git tree: fe970b05333fb8e41da21e9e289f652abcc7cba7 source version: Linux 7.4.0-rc4 compiler: gcc 11.4.0, GNU ld 2.38
Console output:
https://gist.githubusercontent.com/shaos26/111949c00d3d0b42fe7a11e90f0f47fb/raw/dde677d84dbf51c6bc82fc81535aefae86370afd/console-output.txt
Kernel config:
https://gist.githubusercontent.com/shaos26/111949c00d3d0b42fe7a11e90f0f47fb/raw/dde677d84dbf51c6bc82fc81535aefae86370afd/kernel-config.txt
PoC:
https://gist.githubusercontent.com/shaos26/111949c00d3d0b42fe7a11e90f0f47fb/raw/dde677d84dbf51c6bc82fc81535aefae86370afd/poc.c
JFS image builder:
https://gist.githubusercontent.com/shaos26/111949c00d3d0b42fe7a11e90f0f47fb/raw/dde677d84dbf51c6bc82fc81535aefae86370afd/mkjfs_cycle.py
Build the test kernel from the specified commit and config. Required tools are gcc, make, cpio, gzip, Python 3 and QEMU. Run these commands to download the files, build the kernel and create both images and the initramfs:
work=$(mktemp -d /tmp/jfs-readdir.XXXXXX)
cd "$work"
gist_id=111949c00d3d0b42fe7a11e90f0f47fb
gist_revision=dde677d84dbf51c6bc82fc81535aefae86370afd
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 \
mkjfs_cycle.py; do
curl -fL "$gist/$file" -o "$file"
done
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 ..
python3 mkjfs_cycle.py trigger trigger.img
python3 mkjfs_cycle.py control control.img
mkdir -p rootfs/dev rootfs/proc rootfs/mnt
gcc -O2 -static -Wall -Wextra -Werror -o rootfs/init poc.c
chmod 0755 rootfs/init
(cd rootfs && find . -print0 |
cpio --null -o -H newc --owner=0:0 |
gzip -9) > initramfs.cpio.gzRun the vulnerable kernel with the trigger image and then the one-field control:
kernel_cmdline='console=ttyS0 earlyprintk=serial nokaslr'
kernel_cmdline="$kernel_cmdline panic=1 oops=panic"
qemu-system-x86_64 -machine pc -cpu qemu64 -m 1024 -smp 2 \
-kernel linux/build/arch/x86/boot/bzImage \
-initrd initramfs.cpio.gz \
-drive file=trigger.img,format=raw,if=virtio,readonly=on \
-append "$kernel_cmdline" \
-nographic -no-reboot
qemu-system-x86_64 -machine pc -cpu qemu64 -m 1024 -smp 2 \
-kernel linux/build/arch/x86/boot/bzImage \
-initrd initramfs.cpio.gz \
-drive file=control.img,format=raw,if=virtio,readonly=on \
-append "$kernel_cmdline" \
-nographic -no-rebootThe trigger kernel stays in state R, with CPU ticks advancing from 100 to 142. Its stack includes check_dtpage() and jfs_readdir(); it remains in state R at tick 206 after SIGKILL. With the patch below, the trigger logs the rejected self-link and getdents64() returns the dot entries followed by EOF. The control also completes normally. The console file contains all three runs. To build and check the patched kernel, save the diff below as fix.patch in the Linux source directory, then run:
git apply --check fix.patch
git apply fix.patch
make O=build -j"$(nproc)" bzImageRun the same two QEMU commands with the rebuilt kernel to check both images again.
This is a local denial of service when a crafted JFS image is mounted. The test used a read-only mount. It demonstrates no memory corruption, information disclosure or privilege escalation.
The patch was built against the same source tree and config. The positive and control images were both tested with it. The PoC builds with -Wall -Wextra -Werror. The patch passes scripts/checkpatch.pl --no-tree --strict and git apply --check.
--- fs/jfs/jfs_dtree.c | 21 +++++++++++++++++---- fs/jfs/jfs_dtree.h | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-)
fs/jfs/jfs_dtree.c +17 -4
@@ -113,9 +113,10 @@ struct dtsplit {/* get page buffer for specified block address */ #define DT_GETPAGE (IP ,BN ,MP ,SIZE ,P ,RC ) \ do { \ - BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot); \ + s64 dt_bn = (BN); \ + BT_GETPAGE(IP, dt_bn, MP, dtpage_t, SIZE, P, RC, i_dtroot); \ if (!(RC)) { \ - if ((BN) && !check_dtpage(P)) { \ + if (dt_bn && !check_dtpage(P, dt_bn)) { \ BT_PUTPAGE(MP); \ jfs_error((IP)->i_sb, \ "DT_GETPAGE: dtree page corrupt\n"); \
@@ -2826,8 +2827,12 @@ int jfs_readdir(struct file *file, struct dir_context *ctx)return 0 ; } -if ((rc =dtReadFirst (ip, &btstack))) + rc =dtReadFirst (ip, &btstack); +if (rc) { +/* Avoid retrying a corrupt first page on the next call. */ + ctx->pos =DIREND ;return rc; + }DT_GETSEARCH (ip, btstack.top, bn, mp, p, index); }
@@ -4381,12 +4386,20 @@ bool check_dtroot(dtroot_t *p)return true; } -bool check_dtpage (dtpage_t *p) +bool check_dtpage (dtpage_t *p,s64 bn) { DECLARE_BITMAP(bitmap, DTPAGEMAXSLOT) = {0};const int stblsize = ((PSIZE >>L2DTSLOTSIZE ) +31 ) >>L2DTSLOTSIZE ;int i; +/* A directory page cannot be its own sibling. */ +if (unlikely (le64_to_cpu (p->header.next) == bn || +le64_to_cpu (p->header.prev) == bn)) { +jfs_err ("Self-referential sibling link in dtpage %lld\n" , + (long long )bn); +return false; + } +/* Validate maxslot (maximum number of slots in the page) * dtpage_t slot array is defined to hold up to DTPAGEMAXSLOT (128) slots */
fs/jfs/jfs_dtree.h +1 -1
@@ -256,5 +256,5 @@ extern int jfs_readdir(struct file *file, struct dir_context *ctx);extern bool check_dtroot (dtroot_t *p); -extern bool check_dtpage (dtpage_t *p); +bool check_dtpage (dtpage_t *p,s64 bn);#endif /* !_H_JFS_DTREE */
Signature · 1 line
2.43.0