mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: shaos <shawnq1@163.com>
To: luisbg@kernel.org, salah.triki@gmail.com
Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH] befs: fix indirect index in double-indirect mapper
Date: Sat, 28 Nov 2026 11:15:45 +0000	[thread overview]
Message-ID: <20261128111545.3210159-1-shawnq1@163.com> (raw)

Hello,

befs_find_brun_dblindirect() computes the index within the indirect
block as dblindir_leftover / diblklen.  Since dblindir_leftover is the
remainder modulo diblklen, that quotient is always zero.  The divisor
must be iblklen, the number of file blocks described by one indirect
entry.

With a valid-layout image whose direct and indirect ranges are both
non-empty, reads from the second data run in the double-indirect range
return data from the first run plus the logical offset.  The PoC reads
file blocks 6 and 7 as WRONG_BLOCK_024 and WRONG_BLOCK_025; the correct
blocks contain RUN1_BLOCK_200 and RUN1_BLOCK_201.

The trigger requires a crafted BeFS image to be mounted from a block
device, which requires mount privilege.  The reproducer mounts the
image read-only and reads a regular file.  It demonstrates incorrect
file contents; it does not demonstrate a panic, memory disclosure, or
privilege escalation.

HEAD commit:    8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
git tree:       fe970b05333fb8e41da21e9e289f652abcc7cba7
source version: v7.3-23088-g8d7fe9885c62f (Linux 7.4.0-rc4)
compiler:       gcc 11.4.0, GNU ld 2.38

Console output:
https://gist.githubusercontent.com/shaos26/6be5e0efeaa44a6715bdba598ca6c2f6/raw/c1fcaab9327372c7021788ebfcdddac66810cb26/console-output.txt
Kernel config:
https://gist.githubusercontent.com/shaos26/6be5e0efeaa44a6715bdba598ca6c2f6/raw/c1fcaab9327372c7021788ebfcdddac66810cb26/kernel-config.txt
PoC:
https://gist.githubusercontent.com/shaos26/6be5e0efeaa44a6715bdba598ca6c2f6/raw/c1fcaab9327372c7021788ebfcdddac66810cb26/poc.c
Image builder:
https://gist.githubusercontent.com/shaos26/6be5e0efeaa44a6715bdba598ca6c2f6/raw/c1fcaab9327372c7021788ebfcdddac66810cb26/make_befs_image.py

Build the kernel from the revision above using kernel-config.txt:

    work=$(mktemp -d /tmp/befs-dblindir.XXXXXX)
    cd "$work"
    gist_id=6be5e0efeaa44a6715bdba598ca6c2f6
    gist_revision=c1fcaab9327372c7021788ebfcdddac66810cb26
    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 \
        make_befs_image.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 ..

Build the image and initramfs:

    gcc -O2 -static -Wall -Wextra -o poc poc.c
    python3 make_befs_image.py befs.img
    mkdir -p root/dev root/mnt
    cp poc root/init
    (cd root && find . -print0 | cpio --null -o -H newc) | \
        gzip -9 > initramfs.cpio.gz

Run the positive case with the image attached read-only:

    timeout 30 qemu-system-x86_64 -machine pc,accel=tcg -cpu max \
        -m 1024M -smp 2 -display none -monitor none -serial stdio \
        -no-reboot -kernel linux/build/arch/x86/boot/bzImage \
        -initrd initramfs.cpio.gz \
        -append 'console=ttyS0 rdinit=/init panic=-1 loglevel=7' \
        -drive file=befs.img,if=virtio,format=raw,readonly=on

The unfixed kernel reads WRONG_BLOCK_024 and WRONG_BLOCK_025 for file
blocks 6 and 7.  The same image and initramfs on the fixed kernel read
RUN1_BLOCK_200 and RUN1_BLOCK_201.  Blocks in the direct range, the
first indirect range, and the first double-indirect run match on both
kernels.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: shaos <shawnq1@163.com>

Thanks,

shaos

---
 fs/befs/datastream.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/befs/datastream.c b/fs/befs/datastream.c
index 4f5825a336f1c..9f610e775321a 100644
--- a/fs/befs/datastream.c
+++ b/fs/befs/datastream.c
@@ -456,7 +456,7 @@ befs_find_brun_dblindirect(struct super_block *sb,
 
 	dblindir_indx = dbl_indir_off / diblklen;
 	dblindir_leftover = dbl_indir_off % diblklen;
-	indir_indx = dblindir_leftover / diblklen;
+	indir_indx = dblindir_leftover / iblklen;
 
 	/* Read double indirect block */
 	dbl_which_block = dblindir_indx / befs_iaddrs_per_block(sb);
-- 
2.34.1


                 reply	other threads:[~2026-11-28 11:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128111545.3210159-1-shawnq1@163.com \
    --to=shawnq1@163.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luisbg@kernel.org \
    --cc=salah.triki@gmail.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®