* [PATCH] floppy: fix copy_buffer() OOB access due to ignored clamped size
@ 2026-11-20 4:45 Kanishka De Silva (HEXER)
0 siblings, 0 replies; only message in thread
From: Kanishka De Silva (HEXER) @ 2026-11-20 4:45 UTC (permalink / raw)
To: efremov; +Cc: axboe, linux-block, stable
[-- Attachment #1: Type: text/plain, Size: 5954 bytes --]
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-----
[-- Attachment #2: 0001-floppy-fix-copy_buffer-OOB.patch --]
[-- Type: application/octet-stream, Size: 2174 bytes --]
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
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-20 4:45 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 4:45 [PATCH] floppy: fix copy_buffer() OOB access due to ignored clamped size Kanishka De Silva (HEXER)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox