From: "Denis Efremov (Oracle)" <efremov@linux.com>
To: "Kanishka De Silva (HEXER)" <kpskanna1915@gmail.com>
Cc: axboe@kernel.dk, linux-block@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] floppy: fix copy_buffer() OOB access due to ignored clamped size
Date: Tue, 24 Nov 2026 00:31:40 +0400 [thread overview]
Message-ID: <06ed8001-24fd-46e9-9481-b0862dca90a3@linux.com> (raw)
In-Reply-To: <179514990028.24686.6400570561985206166@hexer.is-a.dev>
Hello,
Thank you for the patch and the report.
Applied,
https://github.com/evdenis/linux-floppy/commit/d4f804348d99f6b79066f593a67d585ac0fc4fd2
Please, use plain text git send-email next time.
Thanks,
Denis
On 20/11/2026 08:45, Kanishka De Silva (HEXER) wrote:
> Hi Denis,
>
> As discussed, here is the formal mainline patch for the copy_buffer()
> clamp/copy mismatch.
>
> Root cause
> ----------
>
> copy_buffer() correctly clamps the per-segment transfer length:
>
> size = bv.bv_len;
> SUPBOUND(size, remaining);
> if (dma_buffer + size > floppy_track_buffer + (max_buffer_sectors << 10) ||
> dma_buffer < floppy_track_buffer)
> break;
>
> if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
> memcpy_to_bvec(&bv, dma_buffer);
> else
> memcpy_from_bvec(dma_buffer, &bv);
>
> But memcpy_to_bvec() / memcpy_from_bvec() (include/linux/bvec.h) ignore
> `size` and always copy bvec->bv_len:
>
> static inline void memcpy_from_bvec(char *to, struct bio_vec *bvec)
> {
> memcpy_from_page(to, bvec->bv_page, bvec->bv_offset, bvec->bv_len);
> }
>
> When remaining < bv.bv_len (track / current_count_sectors clip), the
> bounds check validates dma_buffer + size only, while the copy uses
> bv.bv_len. If room to the end of the validated region is in
> [remaining, bv_len), the check passes and the copy overruns.
>
> Impact (corrected from my earlier private note)
> -----------------------------------------------
>
> The overrun is on the floppy_track_buffer / DMA side, not the bio page:
>
> FD_READ: heap OOB read from floppy_track_buffer past the validated
> DMA region / allocation end
> FD_WRITE: heap OOB write into floppy_track_buffer past the validated
> DMA region / allocation end
>
> The bio page is allocated for bv_len and stays in-bounds on both
> directions. One bug, two directions — please do not split into separate
> FD_READ / FD_WRITE issues without coordinator input.
>
> Affected
> --------
>
> Introduced by:
>
> 13d4ef0f66b7 ("floppy: use memcpy_{to,from}_bvec", 2022-03-03)
>
> Still present at:
>
> aebb7274476403040d582c61478df8f4a7bf9cd3 (Linux 7.4-rc3)
>
> Verification
> ------------
>
> 1) Proposed patch applies clean on the revision above and builds with
> CONFIG_BLK_DEV_FD=y (floppy.o hashes differ unpatched vs patched).
>
> 2) AddressSanitizer userspace model of the copy_buffer() inner loop
> (faithful clamp + bounds check + memcpy_*_bvec helpers):
>
> Scenario: 16 KiB track buffer, dma_off=15360, remaining=1024,
> bv_len=4096 → size=1024 passes the bounds check; helpers copy 4096.
>
> FD_READ UNPATCHED:
> ERROR: AddressSanitizer: heap-buffer-overflow
> READ of size 4096 at 0 bytes after 16384-byte region
> in memcpy_to_bvec
>
> FD_WRITE UNPATCHED:
> ERROR: AddressSanitizer: heap-buffer-overflow
> WRITE of size 4096 at 0 bytes after 16384-byte region
> in memcpy_from_bvec
>
> Patched (both directions): clean, exit 0.
>
> 3) floppy.o with CONFIG_KASAN_GENERIC builds without warnings. A QEMU
> guest with CONFIG_BLK_DEV_FD=y + KASAN ran I/O against a 2.88M ED
> floppy but did not produce a KASAN report: ED uses a 64 KiB track
> buffer, so room stays >= 4096 for standard geometries. Live kernel
> reachability of remaining < bv_len with room < bv_len is therefore
> not yet demonstrated end-to-end; the clamp/copy bug itself is
> confirmed by source + ASan model.
>
> Conditions for the overrun
> --------------------------
>
> CONFIG_BLK_DEV_FD=y and a floppy request whose transfer is clipped so
> that remaining < bv_len while dma + size still passes the track-buffer
> bounds check (short current_count_sectors near the validated end).
> Requires local floppy (or equivalent) access to submit such I/O.
>
> Fix
> ---
>
> Open-code the copy with memcpy_to_page() / memcpy_from_page(..., size)
> so the already-clamped length is used. Restores pre-13d4ef0f66b7
> semantics for these two call sites only.
>
> Patch is attached (also applies as a plain git am of the body below).
>
> ---
> HEXER (Kanishka De Silva)
> HEXERLAB
> contact@hexer.is-a.dev
> https://hexer.is-a.dev
>
> -----BEGIN PATCH-----
> From 67606424cf8dc4bd1baa690494a2644095beddb7 Mon Sep 17 00:00:00 2001
> From: Kanishka De Silva <contact@hexer.is-a.dev>
> Date: Fri, 20 Nov 2026 06:45:35 +0530
> Subject: [PATCH] floppy: fix copy_buffer() OOB access due to ignored clamped
> size
>
> In copy_buffer(), when iterating over bio segments, the transfer size
> is correctly clamped:
>
> size = bv.bv_len;
> SUPBOUND(size, remaining);
>
> However the subsequent memcpy_to_bvec() / memcpy_from_bvec() helpers
> both ignore `size` and always copy bv.bv_len bytes:
>
> static inline void memcpy_from_bvec(char *to, struct bio_vec *bvec)
> {
> memcpy_from_page(to, bvec->bv_page, bvec->bv_offset, bvec->bv_len);
> }
>
> When a transfer is clipped so that remaining < bv.bv_len, the clamped
> value is discarded and the full bvec length is used for the
> floppy_track_buffer memcpy. The bounds check above only validates
> dma_buffer + size, so dma_buffer + bv.bv_len may pass the check while
> still overrunning the validated DMA region / allocation:
>
> FD_READ: out-of-bounds read from floppy_track_buffer (source side)
> FD_WRITE: out-of-bounds write into floppy_track_buffer (dest side)
>
> The bio page itself is sized for bv_len and is in-bounds; the overrun
> is on the track-buffer side.
>
> Fix by open-coding the page copy with the already-clamped `size` via
> memcpy_to_page() / memcpy_from_page(), which accept an explicit length.
>
> Fixes: 13d4ef0f66b7 ("floppy: use memcpy_{to,from}_bvec")
> Cc: stable@vger.kernel.org
> Signed-off-by: Kanishka De Silva <contact@hexer.is-a.dev>
> ---
> drivers/block/floppy.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
> index c677d7838..b492e35ca 100644
> --- a/drivers/block/floppy.c
> +++ b/drivers/block/floppy.c
> @@ -2488,9 +2488,9 @@ static void copy_buffer(int ssize, int max_sector, int max_sector_2)
> }
>
> if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
> - memcpy_to_bvec(&bv, dma_buffer);
> + memcpy_to_page(bv.bv_page, bv.bv_offset, dma_buffer, size);
> else
> - memcpy_from_bvec(dma_buffer, &bv);
> + memcpy_from_page(dma_buffer, bv.bv_page, bv.bv_offset, size);
>
> remaining -= size;
> dma_buffer += size;
> --
> 2.43.0
> -----END PATCH-----
prev parent reply other threads:[~2026-11-23 20:31 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 4:45 [PATCH] floppy: fix copy_buffer() OOB access due to ignored clamped size Kanishka De Silva (HEXER)
2026-11-23 20:31 ` Denis Efremov (Oracle) [this message]
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=06ed8001-24fd-46e9-9481-b0862dca90a3@linux.com \
--to=efremov@linux.com \
--cc=axboe@kernel.dk \
--cc=kpskanna1915@gmail.com \
--cc=linux-block@vger.kernel.org \
--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