* [PATCH] hfs: reject empty nodes in forward record traversal
@ 2026-11-30 6:56 Shawn Shao
2026-11-30 19:47 ` Viacheslav Dubeyko
[not found] ` <fcb208044e60bbeb29eb8a6f7eeb137fd83227b.camel@dubeyko.com>
0 siblings, 2 replies; 4+ messages in thread
From: Shawn Shao @ 2026-11-30 6:56 UTC (permalink / raw)
To: slava; +Cc: John Paul Adrian Glaubitz, Yangtao Li, linux-fsdevel, linux-kernel
Hello,
A malformed classic HFS catalog can contain an empty leaf whose `next`
link points to itself. When `getdents64()` advances from the root
folder-thread record to that leaf, `hfs_brec_goto()` subtracts zero
records and repeats the transition indefinitely. The task stays in
kernel context with the catalog B-tree lock held and does not exit after
`SIGKILL`.
The image mounts read-only. The issue requires a privileged mount of an
attacker-supplied HFS image and causes a local availability denial of
service. It does not demonstrate memory corruption, information
disclosure, or privilege escalation.
On the patched kernel, the first `getdents64()` returns the 48 bytes for
`.` and `..`; the next call returns `-EIO` promptly. The close control,
which changes only the empty leaf's `next` field from 2 to 0, returns the
dot entries and exits normally.
A related public report discusses malformed records in the separate
HFS+ driver and its unlink/writeback path. This reproducer is in the
classic HFS driver and the `readdir` path:
https://lkml.iu.edu/2606.3/02410.html
Tested source revision:
HEAD commit: 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046
source version: Linux 7.4.0-rc4
compiler: gcc 11.4.0, GNU ld 2.38
QEMU: 6.2.0, x86-64 TCG
Vulnerable console output:
https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/a1b616ee6d82742ae58cfe2ec23405550f49e257/vulnerable-cycle-console.txt
Control console output:
https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/066ea9864657394d6d6e728c68ccd3315ffc088a/vulnerable-control-console.txt
Patched console output:
https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/948aa7bd9c9a9a4b3964bfd1fab61544b6a3f401/fixed-two-call-console.txt
Kernel config:
https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/664d72f164fe325e8cc97119c3d592692e9c3357/kernel-config.txt
Reproducer:
https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/cca14b4b603fe8b0f12e72d2819f4bc02ff7be80/poc.c
Image builder:
https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/b858d02dc8c97a5b324771bd134f1c0b2603b3c2/make_image.py
Init script:
https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/b7dd3708e4a4b753e9de8941b4b2d1ce38afd934/init
Initramfs builder:
https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/fb2c2fb32fac59b77f7bb3ee9c23fe0d02ebb097/build_initramfs.sh
QEMU runner:
https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/44e5decf5788b8e73da0fe4d01cf70f41f6289b2/run_qemu.sh
Fixed-kernel check:
https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/18041ca2ebe658179b5de4e97e6e0d62c4ed75d6/fixed-check.c
Patch file:
https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/80c76dd1dea20d6ed3f8c67d2e64ec042e880535/fix.patch
The Gist also contains the fixed-kernel positive and control logs. To
reproduce, run:
work=$(mktemp -d /tmp/hfs-leaf.XXXXXX)
cd "$work"
gist_id=06dc92e4840aef9766cd82b8c644fe16
gist_revision=e7630526685aa318b25a981ea5044014607f1521
gist_base=https://gist.githubusercontent.com/shaos26
gist=$gist_base/$gist_id/raw/$gist_revision
for file in kernel-config.txt poc.c fixed-check.c make_image.py \
init build_initramfs.sh run_qemu.sh fix.patch; 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 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046
mkdir build
cp ../kernel-config.txt build/.config
make O=build olddefconfig
make O=build -j"$(nproc)" bzImage
cd ..
python3 make_image.py cycle.img cycle
python3 make_image.py control.img control
sh ./build_initramfs.sh "$PWD/rootfs" "$PWD/initramfs.cpio.gz"
sh ./run_qemu.sh linux/build/arch/x86/boot/bzImage \
initramfs.cpio.gz cycle.img cycle.log
sh ./run_qemu.sh linux/build/arch/x86/boot/bzImage \
initramfs.cpio.gz control.img control.log
To test the fix, run `git apply --check ../fix.patch` and `git apply
../fix.patch` from the Linux source directory, rebuild, then rerun the
cycle image. For the second-call check, build the initramfs with
`POC_SRC="$PWD/fixed-check.c"`.
Reject an invalid record cursor before following the next leaf link:
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Shawn Shao <shawnq1@163.com>
---
fs/hfs/bfind.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/fs/hfs/bfind.c b/fs/hfs/bfind.c
index d56e47bdc517d..c0af468651674 100644
--- a/fs/hfs/bfind.c
+++ b/fs/hfs/bfind.c
@@ -211,6 +211,11 @@ int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
fd->record -= cnt;
} else {
while (cnt >= bnode->num_recs - fd->record) {
+ if (unlikely(fd->record >= bnode->num_recs)) {
+ res = -EIO;
+ goto out;
+ }
+
cnt -= bnode->num_recs - fd->record;
fd->record = 0;
idx = bnode->next;
--
2.34.1
Thanks,
Shawn
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] hfs: reject empty nodes in forward record traversal 2026-11-30 6:56 [PATCH] hfs: reject empty nodes in forward record traversal Shawn Shao @ 2026-11-30 19:47 ` Viacheslav Dubeyko [not found] ` <fcb208044e60bbeb29eb8a6f7eeb137fd83227b.camel@dubeyko.com> 1 sibling, 0 replies; 4+ messages in thread From: Viacheslav Dubeyko @ 2026-11-30 19:47 UTC (permalink / raw) To: Shawn Shao Cc: John Paul Adrian Glaubitz, Yangtao Li, linux-fsdevel, linux-kernel On Mon, 2026-11-30 at 06:56 +0000, Shawn Shao wrote: > Hello, > > A malformed classic HFS catalog can contain an empty leaf whose > `next` > link points to itself. When `getdents64()` advances from the root > folder-thread record to that leaf, `hfs_brec_goto()` subtracts zero > records and repeats the transition indefinitely. The task stays in > kernel context with the catalog B-tree lock held and does not exit > after > `SIGKILL`. > > The image mounts read-only. The issue requires a privileged mount of > an > attacker-supplied HFS image and causes a local availability denial of > service. It does not demonstrate memory corruption, information > disclosure, or privilege escalation. > > On the patched kernel, the first `getdents64()` returns the 48 bytes > for > `.` and `..`; the next call returns `-EIO` promptly. The close > control, > which changes only the empty leaf's `next` field from 2 to 0, returns > the > dot entries and exits normally. Frankly speaking, I cannot connect the commit message explanation and the fix itself. It sounds confusing. > > A related public report discusses malformed records in the separate > HFS+ driver and its unlink/writeback path. This reproducer is in the > classic HFS driver and the `readdir` path: > https://lkml.iu.edu/2606.3/02410.html > > Tested source revision: > HEAD commit: 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046 > source version: Linux 7.4.0-rc4 > compiler: gcc 11.4.0, GNU ld 2.38 > QEMU: 6.2.0, x86-64 TCG > > Vulnerable console output: > https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/a1b616ee6d82742ae58cfe2ec23405550f49e257/vulnerable-cycle-console.txt > Control console output: > https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/066ea9864657394d6d6e728c68ccd3315ffc088a/vulnerable-control-console.txt > Patched console output: > https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/948aa7bd9c9a9a4b3964bfd1fab61544b6a3f401/fixed-two-call-console.txt > Kernel config: > https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/664d72f164fe325e8cc97119c3d592692e9c3357/kernel-config.txt > Reproducer: > https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/cca14b4b603fe8b0f12e72d2819f4bc02ff7be80/poc.c > Image builder: > https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/b858d02dc8c97a5b324771bd134f1c0b2603b3c2/make_image.py > Init script: > https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/b7dd3708e4a4b753e9de8941b4b2d1ce38afd934/init > Initramfs builder: > https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/fb2c2fb32fac59b77f7bb3ee9c23fe0d02ebb097/build_initramfs.sh > QEMU runner: > https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/44e5decf5788b8e73da0fe4d01cf70f41f6289b2/run_qemu.sh > Fixed-kernel check: > https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/18041ca2ebe658179b5de4e97e6e0d62c4ed75d6/fixed-check.c > Patch file: > https://gist.githubusercontent.com/shaos26/06dc92e4840aef9766cd82b8c644fe16/raw/80c76dd1dea20d6ed3f8c67d2e64ec042e880535/fix.patch > > The Gist also contains the fixed-kernel positive and control logs. > To > reproduce, run: > > work=$(mktemp -d /tmp/hfs-leaf.XXXXXX) > cd "$work" > gist_id=06dc92e4840aef9766cd82b8c644fe16 > gist_revision=e7630526685aa318b25a981ea5044014607f1521 > gist_base=https://gist.githubusercontent.com/shaos26 > gist=$gist_base/$gist_id/raw/$gist_revision > for file in kernel-config.txt poc.c fixed-check.c make_image.py \ > init build_initramfs.sh run_qemu.sh fix.patch; 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 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046 > mkdir build > cp ../kernel-config.txt build/.config > make O=build olddefconfig > make O=build -j"$(nproc)" bzImage > cd .. > python3 make_image.py cycle.img cycle > python3 make_image.py control.img control > sh ./build_initramfs.sh "$PWD/rootfs" "$PWD/initramfs.cpio.gz" > sh ./run_qemu.sh linux/build/arch/x86/boot/bzImage \ > initramfs.cpio.gz cycle.img cycle.log > sh ./run_qemu.sh linux/build/arch/x86/boot/bzImage \ > initramfs.cpio.gz control.img control.log > > To test the fix, run `git apply --check ../fix.patch` and `git apply > ../fix.patch` from the Linux source directory, rebuild, then rerun > the > cycle image. For the second-call check, build the initramfs with > `POC_SRC="$PWD/fixed-check.c"`. > > Reject an invalid record cursor before following the next leaf link: > > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > Signed-off-by: Shawn Shao <shawnq1@163.com> > --- > fs/hfs/bfind.c | 5 +++++ > 1 file changed, 5 insertions(+) > > diff --git a/fs/hfs/bfind.c b/fs/hfs/bfind.c > index d56e47bdc517d..c0af468651674 100644 > --- a/fs/hfs/bfind.c > +++ b/fs/hfs/bfind.c > @@ -211,6 +211,11 @@ int hfs_brec_goto(struct hfs_find_data *fd, int > cnt) > fd->record -= cnt; > } else { > while (cnt >= bnode->num_recs - fd->record) { We try to execute bnode->num_recs - fd->record already here. If the fd- >record, bnode->num_recs, or cnt are incorrect, then we should not to execute this check because we should have not correct condition check. And we could have any of these values incorrect. It looks to me that we need more precise refactoring of hfs_brec_goto(). > + if (unlikely(fd->record >= bnode->num_recs)) > { > + res = -EIO; > + goto out; > + } This check inside of the loop looks like not correct solution. Thanks, Slava. > + > cnt -= bnode->num_recs - fd->record; > fd->record = 0; > idx = bnode->next; ^ permalink raw reply [flat|nested] 4+ messages in thread
[parent not found: <fcb208044e60bbeb29eb8a6f7eeb137fd83227b.camel@dubeyko.com>]
* [PATCH v2] hfs: validate B-tree record cursor before arithmetic [not found] ` <fcb208044e60bbeb29eb8a6f7eeb137fd83227b.camel@dubeyko.com> @ 2026-12-01 14:18 ` Shawn Shao 2026-12-01 23:33 ` Viacheslav Dubeyko 0 siblings, 1 reply; 4+ messages in thread From: Shawn Shao @ 2026-12-01 14:18 UTC (permalink / raw) To: Viacheslav Dubeyko Cc: John Paul Adrian Glaubitz, Yangtao Li, linux-fsdevel, linux-kernel Hello Slava, You are right that the v1 check was too late: the loop condition had already subtracted the record cursor before validating it. This version checks the record count and cursor before the arithmetic in both traversal directions. It preserves the -1 cursor used when forward traversal starts before the first record and avoids an unsigned comparison when moving backward from that cursor. The cycle image now returns 48 bytes for `.` and `..` on the first `getdents64()` call, then returns `-EIO` on the second call. The control image, which changes only the empty leaf's `next` link to zero, returns the dot entries and then `-ENOENT` promptly. Tested on Linux 7.4-rc4, commit 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, tree 194227446ae61b4971eea1b7bed06856fe3cc477, with gcc 11.4.0 and QEMU 6.2.0. The x86-64 bzImage build completed. The initramfs, kernel configuration, updated patch, and console logs are collected here: https://gist.github.com/shaos26/06dc92e4840aef9766cd82b8c644fe16 Changes in v2: - Check the record cursor before arithmetic in both traversal directions. - Preserve the initial -1 cursor for forward traversal and handle it without unsigned arithmetic in the backward path. - Widen the negative count before negating it. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Thanks, Shawn Shao Signed-off-by: Shawn Shao <shawnq1@163.com> --- diff --git a/fs/hfs/bfind.c b/fs/hfs/bfind.c index d56e47bdc517d..92dab12dff40a 100644 --- a/fs/hfs/bfind.c +++ b/fs/hfs/bfind.c @@ -185,16 +185,34 @@ int hfs_brec_goto(struct hfs_find_data *fd, int cnt) struct hfs_btree *tree; struct hfs_bnode *bnode; int idx, res = 0; + u32 move; u16 off, len, keylen; bnode = fd->bnode; tree = bnode->tree; if (cnt < 0) { - cnt = -cnt; - while (cnt > fd->record) { - cnt -= fd->record + 1; - fd->record = bnode->num_recs - 1; + move = -(s64)cnt; + for (;;) { + if (unlikely(!bnode->num_recs)) { + if (fd->record != -1 && fd->record != 0) { + res = -EIO; + goto out; + } + res = bnode->prev ? -EIO : -ENOENT; + goto out; + } + if (unlikely(fd->record < -1 || + fd->record >= bnode->num_recs)) { + res = -EIO; + goto out; + } + if (fd->record >= 0 && move <= (u32)fd->record) { + fd->record -= move; + break; + } + if (fd->record >= 0) + move -= fd->record + 1; idx = bnode->prev; if (!idx) { res = -ENOENT; @@ -207,11 +225,30 @@ int hfs_brec_goto(struct hfs_find_data *fd, int cnt) bnode = NULL; goto out; } + fd->record = bnode->num_recs - 1; } - fd->record -= cnt; } else { - while (cnt >= bnode->num_recs - fd->record) { - cnt -= bnode->num_recs - fd->record; + move = cnt; + for (;;) { + if (unlikely(!bnode->num_recs)) { + if (fd->record != -1 && fd->record != 0) { + res = -EIO; + goto out; + } + res = bnode->next ? -EIO : -ENOENT; + goto out; + } + if (unlikely(fd->record < -1 || + fd->record >= bnode->num_recs || + (fd->record == -1 && !move))) { + res = -EIO; + goto out; + } + if (move < bnode->num_recs - fd->record) { + fd->record += move; + break; + } + move -= bnode->num_recs - fd->record; fd->record = 0; idx = bnode->next; if (!idx) { @@ -226,7 +263,6 @@ int hfs_brec_goto(struct hfs_find_data *fd, int cnt) goto out; } } - fd->record += cnt; } len = hfs_brec_lenoff(bnode, fd->record, &off); ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] hfs: validate B-tree record cursor before arithmetic 2026-12-01 14:18 ` [PATCH v2] hfs: validate B-tree record cursor before arithmetic Shawn Shao @ 2026-12-01 23:33 ` Viacheslav Dubeyko 0 siblings, 0 replies; 4+ messages in thread From: Viacheslav Dubeyko @ 2026-12-01 23:33 UTC (permalink / raw) To: Shawn Shao Cc: John Paul Adrian Glaubitz, Yangtao Li, linux-fsdevel, linux-kernel On Tue, 2026-12-01 at 14:18 +0000, Shawn Shao wrote: > Hello Slava, > > You are right that the v1 check was too late: the loop condition had > already subtracted the record cursor before validating it. This > version I cannot accept the patch with such commit message. Please, send it as formal patch. > checks the record count and cursor before the arithmetic in both > traversal > directions. It preserves the -1 cursor used when forward traversal > starts > before the first record and avoids an unsigned comparison when moving > backward from that cursor. > > The cycle image now returns 48 bytes for `.` and `..` on the first > `getdents64()` call, then returns `-EIO` on the second call. The > control > image, which changes only the empty leaf's `next` link to zero, > returns the > dot entries and then `-ENOENT` promptly. > > Tested on Linux 7.4-rc4, commit > 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, tree > 194227446ae61b4971eea1b7bed06856fe3cc477, with gcc 11.4.0 and QEMU > 6.2.0. > The x86-64 bzImage build completed. The initramfs, kernel > configuration, > updated patch, and console logs are collected here: > https://gist.github.com/shaos26/06dc92e4840aef9766cd82b8c644fe16 > > Changes in v2: > - Check the record cursor before arithmetic in both traversal > directions. > - Preserve the initial -1 cursor for forward traversal and handle it > without > unsigned arithmetic in the backward path. > - Widen the negative count before negating it. > > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > Thanks, > Shawn Shao > > Signed-off-by: Shawn Shao <shawnq1@163.com> > --- > diff --git a/fs/hfs/bfind.c b/fs/hfs/bfind.c > index d56e47bdc517d..92dab12dff40a 100644 > --- a/fs/hfs/bfind.c > +++ b/fs/hfs/bfind.c > @@ -185,16 +185,34 @@ int hfs_brec_goto(struct hfs_find_data *fd, int > cnt) > struct hfs_btree *tree; > struct hfs_bnode *bnode; > int idx, res = 0; > + u32 move; > u16 off, len, keylen; > > bnode = fd->bnode; > tree = bnode->tree; > > if (cnt < 0) { > - cnt = -cnt; > - while (cnt > fd->record) { > - cnt -= fd->record + 1; > - fd->record = bnode->num_recs - 1; > + move = -(s64)cnt; The move variable is u32 data type. What this -(s64)cnt means? > + for (;;) { I don't like the infinite loop. Please, return back the loop limited by max value. > + if (unlikely(!bnode->num_recs)) { > + if (fd->record != -1 && fd->record > != 0) { > + res = -EIO; > + goto out; > + } > + res = bnode->prev ? -EIO : -ENOENT; > + goto out; > + } > + if (unlikely(fd->record < -1 || > + fd->record >= bnode->num_recs)) > { > + res = -EIO; > + goto out; > + } > + if (fd->record >= 0 && move <= (u32)fd- > >record) { > + fd->record -= move; > + break; > + } > + if (fd->record >= 0) > + move -= fd->record + 1; The logic is complicated and not clear. Why do we check the same variables multiple times and pretty for the same values? > idx = bnode->prev; > if (!idx) { > res = -ENOENT; > @@ -207,11 +225,30 @@ int hfs_brec_goto(struct hfs_find_data *fd, int > cnt) > bnode = NULL; > goto out; > } > + fd->record = bnode->num_recs - 1; > } > - fd->record -= cnt; > } else { > - while (cnt >= bnode->num_recs - fd->record) { > - cnt -= bnode->num_recs - fd->record; > + move = cnt; > + for (;;) { > + if (unlikely(!bnode->num_recs)) { > + if (fd->record != -1 && fd->record > != 0) { > + res = -EIO; > + goto out; > + } > + res = bnode->next ? -EIO : -ENOENT; > + goto out; > + } > + if (unlikely(fd->record < -1 || > + fd->record >= bnode->num_recs > || > + (fd->record == -1 && !move))) { > + res = -EIO; > + goto out; > + } > + if (move < bnode->num_recs - fd->record) { > + fd->record += move; > + break; > + } > + move -= bnode->num_recs - fd->record; You repeat the same code twice. Please, rework it by introducing some function. Thanks, Slava. > > fd->record = 0; > idx = bnode->next; > if (!idx) { > @@ -226,7 +263,6 @@ int hfs_brec_goto(struct hfs_find_data *fd, int > cnt) > goto out; > } > } > - fd->record += cnt; > } > > len = hfs_brec_lenoff(bnode, fd->record, &off); ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-01 23:33 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 6:56 [PATCH] hfs: reject empty nodes in forward record traversal Shawn Shao
2026-11-30 19:47 ` Viacheslav Dubeyko
[not found] ` <fcb208044e60bbeb29eb8a6f7eeb137fd83227b.camel@dubeyko.com>
2026-12-01 14:18 ` [PATCH v2] hfs: validate B-tree record cursor before arithmetic Shawn Shao
2026-12-01 23:33 ` Viacheslav Dubeyko
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®