From: Kevin Wolf <kwolf@redhat.com>
To: "Alex Bennée" <alex.bennee@linaro.org>
Cc: qemu-devel@nongnu.org, John Snow <jsnow@redhat.com>,
Hanna Reitz <hreitz@redhat.com>,
"open list:Floppy" <qemu-block@nongnu.org>
Subject: Re: [RFC PATCH] hw/block: validate the fdc sector position fits within bounds
Date: Tue, 17 Nov 2026 16:48:23 +0100 [thread overview]
Message-ID: <avx3R-1e8EKwHvf4@redhat.com> (raw)
In-Reply-To: <20260812172842.60524-1-alex.bennee@linaro.org>
Am 12.08.2026 um 19:28 hat Alex Bennée geschrieben:
> Previously the guest controlled the value of last_sec and wasn't
> particularly careful to check it didn't exceed the media size. We
> can't re-use drv->last_sec as that changes as we do operations so we
> set media_last_sect when the geometry is probed.
>
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3800
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
So first of all, I disagree with the report. If you attach a whole image
file to a device, then you have to expect that the device uses it and
can't make your security depend on the device not actually making use of
it.
> hw/block/fdc-internal.h | 1 +
> hw/block/fdc.c | 28 ++++++++++++++++++++++------
> 2 files changed, 23 insertions(+), 6 deletions(-)
>
> diff --git a/hw/block/fdc-internal.h b/hw/block/fdc-internal.h
> index e219623dc7a..81775fe55aa 100644
> --- a/hw/block/fdc-internal.h
> +++ b/hw/block/fdc-internal.h
> @@ -83,6 +83,7 @@ typedef struct FDrive {
> uint8_t max_track; /* Nb of tracks */
> uint16_t bps; /* Bytes per sector */
> uint8_t ro; /* Is read-only */
> + uint8_t media_last_sect; /* last sector of current track according to media */
> uint8_t media_changed; /* Is media changed */
> uint8_t media_rate; /* Data rate of medium */
>
> diff --git a/hw/block/fdc.c b/hw/block/fdc.c
> index 1178b959a64..18a61f5ce87 100644
> --- a/hw/block/fdc.c
> +++ b/hw/block/fdc.c
> @@ -192,6 +192,7 @@ static void fd_init(FDrive *drv)
> drv->max_track = 0;
> drv->ro = true;
> drv->media_changed = 1;
> + drv->media_last_sect = 0;
> }
>
> #define NUM_SIDES(drv) ((drv)->flags & FDISK_DBL_SIDES ? 2 : 1)
> @@ -373,6 +374,7 @@ static int pick_geometry(FDrive *drv)
> }
> drv->max_track = parse->max_track;
> drv->last_sect = parse->last_sect;
> + drv->media_last_sect = parse->last_sect;
> drv->disk = parse->drive;
> drv->media_rate = parse->rate;
> return 0;
> @@ -1905,6 +1907,17 @@ static void fdctrl_handle_partid(FDCtrl *fdctrl, int direction)
> fdctrl_to_result_phase(fdctrl, 1);
> }
>
> +static bool fd_validate_last_sect(FDrive *drv, uint8_t new_last_sect_val)
> +{
> + if (drv->media_validated && new_last_sect_val > drv->media_last_sect) {
> + qemu_log_mask(LOG_GUEST_ERROR,
> + "FDC: Guest attempted to set last_sect to %u, exceeding valid media max of %u\n",
> + new_last_sect_val, drv->media_last_sect);
> + return false;
> + }
> + return true;
> +}
> +
> static void fdctrl_handle_restore(FDCtrl *fdctrl, int direction)
> {
> FDrive *cur_drv = get_cur_drv(fdctrl);
> @@ -1919,6 +1932,10 @@ static void fdctrl_handle_restore(FDCtrl *fdctrl, int direction)
> /* timers */
> fdctrl->timer0 = fdctrl->fifo[7];
> fdctrl->timer1 = fdctrl->fifo[8];
> + if (!fd_validate_last_sect(cur_drv, fdctrl->fifo[9])) {
> + fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00);
> + return;
> + }
> cur_drv->last_sect = fdctrl->fifo[9];
Does having a separate drv->media_last_sect actually have a practical
advantage here compared to just not letting the guest change
drv->last_sect? The spec only talks about restoring values that you got
from SAVE.
Are there any guests that would abuse RESTORE to temporarily set a
smaller number of sectors? I don't expect working with the floppy with a
wrong number of sectors could possibly work?
> fdctrl->lock = fdctrl->fifo[10] >> 7;
> cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF;
> @@ -1983,13 +2000,12 @@ static void fdctrl_handle_format_track(FDCtrl *fdctrl, int direction)
> fdctrl->data_state &= ~FD_STATE_MULTI;
> cur_drv->bps =
> fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2];
> -#if 0
> - cur_drv->last_sect =
> - cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] :
> - fdctrl->fifo[3] / 2;
> -#else
> +
> + if (!fd_validate_last_sect(cur_drv, fdctrl->fifo[3])) {
> + fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00);
> + return;
> + }
I think it's perfectly valid to start with a medium formatted as a 720k
disk and then reformat it as 1.44M. And yes, after formatting you'll be
able to access parts of the image that weren't accessible before. I
don't even consider this a bug at all, let alone a security relevant
one.
Some questions that sound more valid to me would be:
1. Should FORMAT TRACK only accept formats from fd_formats instead of
completely arbitrary values? Can physical floppy drives really do any
arbitrary format? And maybe even more importantly, if you format a
floppy with an unknown format, the next start of QEMU won't detect
the same format again, but present something else.
2. Related to the same problem, should fdc truncate the image to the new
size so that the next startup will detect the new format?
3. When formatting the floppy, should we zero out the whole image?
Kevin
prev parent reply other threads:[~2026-11-17 15:49 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-12 17:28 [RFC PATCH] hw/block: validate the fdc sector position fits within bounds Alex Bennée
2026-11-16 22:05 ` Alex Bennée
2026-11-16 22:06 ` Alex Bennée
2026-11-17 15:48 ` Kevin Wolf [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=avx3R-1e8EKwHvf4@redhat.com \
--to=kwolf@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=hreitz@redhat.com \
--cc=jsnow@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.