[PATCH] affs: bound directory hash-chain walks

From: Forest Huang

Date: Mon Nov 30 2026 - 02:47:40 EST


Hello,

affs_find_entry() follows the on-disk hash_chain field in an unbounded
for (;;) loop. A crafted AFFS image whose bucket entry points at a
header block with hash_chain equal to its own block number makes a
lookup of an absent name repeat forever in kernel mode. The task
stays in state R and consumes CPU; the loop holds the directory
i_hash_lock, so a second lookup of the same name blocks behind it.
The test config has CONFIG_SOFTLOCKUP_DETECTOR disabled, so watchdog
escalation was not tested.

The trigger is a crafted 64 KiB AFFS image mounted read-only; stat()
of an absent name enters the loop. A close control with
hash_chain == 0 returns ENOENT immediately, and a matching-name image
resolves the entry. Mounting the image requires the privilege to
mount a filesystem; this is a local denial of service, not privilege
escalation or memory corruption.

The same unbounded hash-chain walk exists in affs_insert_hash(),
affs_remove_hash(), and the chain loop in affs_readdir(). This patch
bounds all four hash-chain walkers.

A valid AFFS hash chain is acyclic and uses distinct blocks in the
s_partition_size - s_reserved range, and the directory header block
cannot be a node in its own chain. A walk that visits that many nodes
is therefore corrupt. affs_hash_chain_max() returns that bound; each
walker returns -EIO when the bound is exhausted. affs_find_entry()
keeps the existing NULL/ENOENT result for an empty bucket, and
affs_readdir() keeps the remaining count in struct affs_dir_data so it
survives across calls.


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/3e8e00523f1bf2658cbf06ff51a3952d/raw/e74177a1c18e952a00900a1e2ea55e9b1ec92fe0/console-output.txt
Kernel config:
https://gist.githubusercontent.com/ForestHYS/3e8e00523f1bf2658cbf06ff51a3952d/raw/e74177a1c18e952a00900a1e2ea55e9b1ec92fe0/kernel-config.txt
PoC source:
https://gist.githubusercontent.com/ForestHYS/3e8e00523f1bf2658cbf06ff51a3952d/raw/e74177a1c18e952a00900a1e2ea55e9b1ec92fe0/poc.c
Init harness:
https://gist.githubusercontent.com/ForestHYS/3e8e00523f1bf2658cbf06ff51a3952d/raw/e74177a1c18e952a00900a1e2ea55e9b1ec92fe0/init.c
Lookup-bound fixed-kernel console:
https://gist.githubusercontent.com/ForestHYS/3e8e00523f1bf2658cbf06ff51a3952d/raw/e74177a1c18e952a00900a1e2ea55e9b1ec92fe0/fixed-console-output.txt

To reproduce, 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 PoC and the PID 1 wrapper, then create an initramfs:

gcc -static -O2 -Wall -Wextra -Werror -o affs-poc poc.c
gcc -static -O2 -Wall -Wextra -o init init.c
mkdir -p rootfs/dev rootfs/proc rootfs/tmp rootfs/mnt/affs
cp affs-poc init rootfs/
chmod 0755 rootfs/init rootfs/affs-poc
(cd rootfs && find . -print0 | cpio --null -o -H newc | \
gzip -9) > initramfs.cpio.gz

Boot the guest:

qemu-system-x86_64 -machine q35 -cpu max -smp 2 -m 2048 \
-kernel build/arch/x86/boot/bzImage \
-initrd initramfs.cpio.gz \
-append 'console=ttyS0 loglevel=7 rdinit=/init panic=1 oops=panic' \
-nographic -no-reboot -monitor none -display none

The PoC creates the image in guest RAM and mounts it through
/dev/loop0. The default run is the cyclic lookup. For the close
controls, add an empty /mode-terminate or /mode-match file to the
initramfs root before packing it.

On the unmodified kernel the positive run stayed in state R with stime
growing from 100 to 302 to 605 ticks; SIGKILL did not stop the lookup,
and the SysRq NMI backtrace shows affs_lookup.

With this patch thensame cyclic image returned: the lookup child was a
zombie with stime
unchanged (POSITIVE_FAIL), and the terminating and matching-name
controls passed (CONTROL_PASS and MATCH_PASS).

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: ForestHYS <foresthuang3@xxxxxxxxx>
---

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 cyclic image returned and the terminating
and matching-name controls passed. The insert/remove/readdir paths
were similar and thus not separately exercised.

diff --git a/fs/affs/affs.h b/fs/affs/affs.h
index 64c5bae92..8662f9a07 100644
--- a/fs/affs/affs.h
+++ b/fs/affs/affs.h
@@ -225,6 +225,16 @@ static inline bool affs_validblock(struct
super_block *sb, int block)
block < AFFS_SB(sb)->s_partition_size);
}

+/*
+ * A valid directory hash chain is acyclic and uses distinct blocks in the
+ * s_partition_size - s_reserved range, so a chain that visits this many
+ * nodes is corrupt.
+ */
+static inline int affs_hash_chain_max(struct super_block *sb)
+{
+ return AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
+}
+
static inline struct buffer_head *
affs_bread(struct super_block *sb, int block)
{
diff --git a/fs/affs/amigaffs.c b/fs/affs/amigaffs.c
index affa20d2b..e4f0dc6f8 100644
--- a/fs/affs/amigaffs.c
+++ b/fs/affs/amigaffs.c
@@ -29,6 +29,7 @@ affs_insert_hash(struct inode *dir, struct buffer_head *bh)
struct buffer_head *dir_bh;
u32 ino, hash_ino;
int offset;
+ int limit;

ino = bh->b_blocknr;
offset = affs_hash_name(sb, AFFS_TAIL(sb, bh)->name + 1,
AFFS_TAIL(sb, bh)->name[0]);
@@ -40,7 +41,12 @@ affs_insert_hash(struct inode *dir, struct buffer_head *bh)
return -EIO;

hash_ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[offset]);
+ limit = affs_hash_chain_max(sb);
while (hash_ino) {
+ if (--limit <= 0) {
+ affs_brelse(dir_bh);
+ return -EIO;
+ }
affs_brelse(dir_bh);
dir_bh = affs_bread(sb, hash_ino);
if (!dir_bh)
@@ -79,6 +85,7 @@ affs_remove_hash(struct inode *dir, struct
buffer_head *rem_bh)
u32 rem_ino, hash_ino;
__be32 ino;
int offset, retval;
+ int limit;

sb = dir->i_sb;
rem_ino = rem_bh->b_blocknr;
@@ -92,6 +99,7 @@ affs_remove_hash(struct inode *dir, struct
buffer_head *rem_bh)

retval = -ENOENT;
hash_ino = be32_to_cpu(AFFS_HEAD(bh)->table[offset]);
+ limit = affs_hash_chain_max(sb);
while (hash_ino) {
if (hash_ino == rem_ino) {
ino = AFFS_TAIL(sb, rem_bh)->hash_chain;
@@ -105,6 +113,10 @@ affs_remove_hash(struct inode *dir, struct
buffer_head *rem_bh)
retval = 0;
break;
}
+ if (--limit <= 0) {
+ affs_brelse(bh);
+ return -EIO;
+ }
affs_brelse(bh);
bh = affs_bread(sb, hash_ino);
if (!bh)
diff --git a/fs/affs/dir.c b/fs/affs/dir.c
index d2b897ebb..bee86528e 100644
--- a/fs/affs/dir.c
+++ b/fs/affs/dir.c
@@ -21,6 +21,7 @@
struct affs_dir_data {
unsigned long ino;
u64 cookie;
+ int chain_remaining;
};

static int affs_readdir(struct file *, struct dir_context *);
@@ -89,6 +90,7 @@ affs_readdir(struct file *file, struct dir_context *ctx)
int chain_pos;
u32 ino;
int error = 0;
+ int limit;

pr_debug("%s(ino=%llu,f_pos=%llx)\n", __func__, inode->i_ino, ctx->pos);

@@ -99,6 +101,7 @@ affs_readdir(struct file *file, struct dir_context *ctx)
}

affs_lock_dir(inode);
+ limit = affs_hash_chain_max(sb);
chain_pos = (ctx->pos - 2) & 0xffff;
hash_pos = (ctx->pos - 2) >> 16;
if (chain_pos == 0xffff) {
@@ -123,7 +126,15 @@ affs_readdir(struct file *file, struct dir_context *ctx)
goto done;

ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
+ data->chain_remaining = limit;
for (i = 0; ino && i < chain_pos; i++) {
+ if (data->chain_remaining <= 0) {
+ affs_error(sb, "readdir",
+ "hash chain cycle in directory %llu",
+ inode->i_ino);
+ error = -EIO;
+ goto out_brelse_dir;
+ }
fh_bh = affs_bread(sb, ino);
if (!fh_bh) {
affs_error(sb, "readdir","Cannot read block %d", i);
@@ -133,6 +144,7 @@ affs_readdir(struct file *file, struct dir_context *ctx)
ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
affs_brelse(fh_bh);
fh_bh = NULL;
+ data->chain_remaining--;
}
if (ino)
goto inside;
@@ -143,8 +155,16 @@ affs_readdir(struct file *file, struct dir_context *ctx)
if (!ino)
continue;
ctx->pos = (hash_pos << 16) + 2;
+ data->chain_remaining = limit;
inside:
do {
+ if (data->chain_remaining <= 0) {
+ affs_error(sb, "readdir",
+ "hash chain cycle in directory %llu",
+ inode->i_ino);
+ error = -EIO;
+ goto out_brelse_dir;
+ }
fh_bh = affs_bread(sb, ino);
if (!fh_bh) {
affs_error(sb, "readdir",
@@ -164,6 +184,7 @@ affs_readdir(struct file *file, struct dir_context *ctx)
ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
affs_brelse(fh_bh);
fh_bh = NULL;
+ data->chain_remaining--;
} while (ino);
}
done:
diff --git a/fs/affs/namei.c b/fs/affs/namei.c
index 2e32899a3..cea5ce2fa 100644
--- a/fs/affs/namei.c
+++ b/fs/affs/namei.c
@@ -173,6 +173,7 @@ affs_find_entry(struct inode *dir, struct dentry *dentry)
struct buffer_head *bh;
toupper_t fn = affs_get_toupper(sb);
u32 key;
+ int limit;

pr_debug("%s(\"%pd\")\n", __func__, dentry);

@@ -181,11 +182,14 @@ affs_find_entry(struct inode *dir, struct dentry *dentry)
return ERR_PTR(-EIO);

key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb,
dentry->d_name.name, dentry->d_name.len)]);
+ limit = affs_hash_chain_max(sb);

- for (;;) {
+ while (key) {
+ if (--limit <= 0) {
+ affs_brelse(bh);
+ return ERR_PTR(-EIO);
+ }
affs_brelse(bh);
- if (key == 0)
- return NULL;
bh = affs_bread(sb, key);
if (!bh)
return ERR_PTR(-EIO);
@@ -193,6 +197,8 @@ affs_find_entry(struct inode *dir, struct dentry *dentry)
return bh;
key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
}
+ affs_brelse(bh);
+ return NULL;
}

struct dentry *

Thanks,

Forest Huang