Hello, befs_btree_read() walks leaf nodes through their on-disk right links while key_sum + all_key_count <= key_no. The only progress term is key_sum += all_key_count. A leaf with all_key_count == 0 contributes nothing; if its right link is not BEFS_BT_INVAL, the loop re-reads a node forever. In the submitted image the second leaf has zero keys and its right link points back at itself, so getdents64() spins in kernel mode and never returns to userspace. The trigger is a crafted BEFS image mounted read-only. The PoC mounts /dev/vda read-only, forks, opens the directory and calls getdents64(). A close control image differs only in the second leaf's right link, which is the BEFS_BT_INVAL sentinel instead of a self link. Reject an empty non-terminal leaf in befs_btree_read(). The BEFS_BT_INVAL sentinel still returns BEFS_BT_END, and a root empty leaf still returns BEFS_BT_EMPTY. The same unvalidated-link class also exists in the interior-node descents in befs_btree_find() and befs_btree_seekleaf(), which follow overflow and value-array links. Bound those descents by BEFS_BT_MAX_TRAVERSE_NODES (4096) and return BEFS_ERR on exhaustion; a well-formed BeFS directory B+tree is far shallower than this. HEAD commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc git tree: fe970b05333fb8e41da21e9e289f652abcc7cba7 source version: Linux 7.4.0-rc4 compiler: gcc 11.4.0, GNU ld 2.38 QEMU: 6.2.0, x86-64 TCG Console output: https://gist.githubusercontent.com/ForestHYS/8e0b6094a0df9fad7250ebd7e003f594/raw/d6509c8f7b0303c1057d3e258eb45bd346d395d0/console-output.txt Kernel config: https://gist.githubusercontent.com/ForestHYS/8e0b6094a0df9fad7250ebd7e003f594/raw/d6509c8f7b0303c1057d3e258eb45bd346d395d0/kernel-config.txt PoC source: https://gist.githubusercontent.com/ForestHYS/8e0b6094a0df9fad7250ebd7e003f594/raw/d6509c8f7b0303c1057d3e258eb45bd346d395d0/poc.c Image builder (generates both images): https://gist.githubusercontent.com/ForestHYS/8e0b6094a0df9fad7250ebd7e003f594/raw/d6509c8f7b0303c1057d3e258eb45bd346d395d0/make_image.py QEMU runner: https://gist.githubusercontent.com/ForestHYS/8e0b6094a0df9fad7250ebd7e003f594/raw/d6509c8f7b0303c1057d3e258eb45bd346d395d0/run_qemu.sh Fixed-kernel console: https://gist.githubusercontent.com/ForestHYS/8e0b6094a0df9fad7250ebd7e003f594/raw/d6509c8f7b0303c1057d3e258eb45bd346d395d0/fixed-console-output.txt Build the test kernel from the commit above with the attached config: mkdir -p build cp kernel-config.txt build/.config make O=build olddefconfig make O=build -j"$(nproc)" bzImage Compile the probe as /init and create an initramfs: gcc -static -O2 -Wall -Wextra -o init poc.c mkdir -p rootfs/dev rootfs/proc rootfs/mnt cp init rootfs/init chmod 0755 rootfs/init (cd rootfs && find . -print0 | cpio --null -o -H newc | \ gzip -9) > initramfs-befs.cpio.gz Generate the cycle and control images: python3 make_image.py befs-cycle.img python3 make_image.py befs-control.img --control Run the cycle image and the control image in fresh QEMU boots: KERNEL=build/arch/x86/boot/bzImage \ sh run_qemu.sh befs-cycle.img cycle.log KERNEL=build/arch/x86/boot/bzImage \ sh run_qemu.sh befs-control.img control.log make_image.py deterministically generates both images; the rebuilt files match the tested images byte-for-byte. On the unmodified kernel the cycle image left the child in state R: after SIGKILL the child was still running and SigPnd had bit 8 set; utime advanced from 200 to 302 jiffies. The control image returned one 24-byte directory entry and exited 0. With this patch both the cycle image and the control return the same 24-byte entry and terminate; the patched kernel was booted twice for the cycle image and twice for the control. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: ForestHYS --- Validation: scripts/checkpatch.pl --no-tree --strict reports no errors or warnings, and git apply --check succeeds. The patched kernel was built and booted in QEMU; the cycle and control images both terminated. diff --git a/fs/befs/btree.c b/fs/befs/btree.c index aa24f1dac..8e0728331 100644 --- a/fs/befs/btree.c +++ b/fs/befs/btree.c @@ -87,6 +87,13 @@ struct befs_btree_node { /* local constants */ static const befs_off_t BEFS_BT_INVAL = 0xffffffffffffffffULL; +/* + * Bound interior-node descents so a corrupt tree that encodes a cycle in + * its overflow/value-array links terminates. A well-formed BeFS directory + * B+tree is far shallower than this. + */ +#define BEFS_BT_MAX_TRAVERSE_NODES 4096 + /* local functions */ static int befs_btree_seekleaf(struct super_block *sb, const befs_data_stream *ds, befs_btree_super * bt_super, @@ -249,6 +263,7 @@ befs_btree_find(struct super_block *sb, const befs_data_stream *ds, befs_btree_super bt_super; befs_off_t node_off; int res; + unsigned int depth = 0; befs_debug(sb, "---> %s Key: %s", __func__, key); @@ -276,6 +291,13 @@ befs_btree_find(struct super_block *sb, const befs_data_stream *ds, } while (!befs_leafnode(this_node)) { + if (++depth > BEFS_BT_MAX_TRAVERSE_NODES) { + befs_error(sb, "%s: corrupt tree: node-link cycle at %llu", + __func__, (unsigned long long)node_off); + brelse(this_node->bh); + this_node->bh = NULL; + goto error_alloc; + } res = befs_find_key(sb, this_node, key, &node_off); /* if no key set, try the overflow node */ if (res == BEFS_BT_OVERFLOW) @@ -470,6 +492,20 @@ befs_btree_read(struct super_block *sb, const befs_data_stream *ds, return BEFS_BT_END; } + /* + * An empty non-terminal leaf cannot advance key_sum; reject it + * rather than loop on a corrupt right-link cycle. + */ + if (this_node->head.all_key_count == 0) { + befs_error(sb, "%s: empty non-terminal leaf at %llu", + __func__, (unsigned long long)node_off); + *keysize = 0; + *value = 0; + brelse(this_node->bh); + kfree(this_node); + return BEFS_ERR; + } + key_sum += this_node->head.all_key_count; node_off = this_node->head.right; @@ -543,6 +582,7 @@ befs_btree_seekleaf(struct super_block *sb, const befs_data_stream *ds, struct befs_btree_node *this_node, befs_off_t * node_off) { + unsigned int depth = 0; befs_debug(sb, "---> %s", __func__); @@ -560,6 +600,14 @@ befs_btree_seekleaf(struct super_block *sb, const befs_data_stream *ds, while (!befs_leafnode(this_node)) { + if (++depth > BEFS_BT_MAX_TRAVERSE_NODES) { + befs_error(sb, "%s: corrupt tree: node-link cycle at %llu", + __func__, (unsigned long long)*node_off); + brelse(this_node->bh); + this_node->bh = NULL; + goto error; + } + if (this_node->head.all_key_count == 0) { befs_debug(sb, "%s encountered " "an empty interior node: %llu. Using Overflow " Thanks, Forest Huang