Skip to contentSkip to conversation

hw/ide: Don't divide by zero if guest specifies 0 sectors

PATCH In review Peter Maydell 4 participants 6 messages Jun 30 – Aug 4

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/ide: Don't divide by zero if guest specifies 0 sectors

In ide_set_sector() if we're using CHS addressing the way we update the drive state involves dividing by (s->heads * s->sectors). These values can be set by the guest using the INITIALIZE DRIVE PARAMETERS command, which means that s->sectors can be 0. (s->heads can't be 0 because the command passes a heads-1 value.) INITIALIZE DRIVE PARAMETERS is specified to not check the input values for validity; instead no error is posted until some other command makes an illegal access. So we have to cope with a 0 divisor here.

Special-case s->sectors being zero.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2399

---
We don't seem to ever actually check that the guest's read/write ops are within the CHS limits it sets with INITIALIZE DRIVE PARAMETERS -- ide_sect_range_ok() only checks against the underlying block device geometry. Maybe we should? Then a zero-sectors misconfig would result in our reporting the error on the read/write as the spec suggests we should. This patch fixes the division-by-zero, at least, though.

---
 hw/ide/core.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
hw/ide/core.c +21 -7
index f78b00220b..b030531534 100644
@@ -646,13 +646,27 @@ void ide_set_sector(IDEState *s, int64_t sector_num)
         }
     } else {
         /* CHS */
-        cyl = sector_num / (s->heads * s->sectors);
-        r = sector_num % (s->heads * s->sectors);
-        s->hcyl = cyl >> 8;
-        s->lcyl = cyl;
-        s->select = (s->select & ~(ATA_DEV_HS)) |
-            ((r / s->sectors) & (ATA_DEV_HS));
-        s->sector = (r % s->sectors) + 1;
+        if (s->sectors == 0) {
+            /*
+             * s->sectors is under guest control via INITIALIZE DRIVE
+             * PARAMETERS and can be 0; this is an error but can't
+             * be reported at INITIALIZE DRIVE PARAMETERS time so we
+             * have to cope with the bad value here.
+             * (s->heads is always at least 1.)
+             */
+            s->hcyl = 0;
+            s->lcyl = 0;
+            s->select = s->select & ~(ATA_DEV_HS);
+            s->sector = 1;
+        } else {
+            cyl = sector_num / (s->heads * s->sectors);
+            r = sector_num % (s->heads * s->sectors);
+            s->hcyl = cyl >> 8;
+            s->lcyl = cyl;
+            s->select = (s->select & ~(ATA_DEV_HS)) |
+                ((r / s->sectors) & (ATA_DEV_HS));
+            s->sector = (r % s->sectors) + 1;
+        }
     }
 }
 
Signature · 1 line
2.43.0
#1 lore raw

Ping -- would anybody on the qemu-block side like to review this?

thanks
-- PMM

Quoted 65 lines · Peter Maydell

In ide_set_sector() if we're using CHS addressing the way we update the drive state involves dividing by (s->heads * s->sectors). These values can be set by the guest using the INITIALIZE DRIVE PARAMETERS command, which means that s->sectors can be 0. (s->heads can't be 0 because the command passes a heads-1 value.) INITIALIZE DRIVE PARAMETERS is specified to not check the input values for validity; instead no error is posted until some other command makes an illegal access. So we have to cope with a 0 divisor here.

Special-case s->sectors being zero.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2399

---
We don't seem to ever actually check that the guest's read/write ops are within the CHS limits it sets with INITIALIZE DRIVE PARAMETERS -- ide_sect_range_ok() only checks against the underlying block device geometry. Maybe we should? Then a zero-sectors misconfig would result in our reporting the error on the read/write as the spec suggests we should. This patch fixes the division-by-zero, at least, though.

---
 hw/ide/core.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
hw/ide/core.c +21 -7
index f78b00220b..b030531534 100644
@@ -646,13 +646,27 @@ void ide_set_sector(IDEState *s, int64_t sector_num)
         }
     } else {
         /* CHS */
-        cyl = sector_num / (s->heads * s->sectors);
-        r = sector_num % (s->heads * s->sectors);
-        s->hcyl = cyl >> 8;
-        s->lcyl = cyl;
-        s->select = (s->select & ~(ATA_DEV_HS)) |
-            ((r / s->sectors) & (ATA_DEV_HS));
-        s->sector = (r % s->sectors) + 1;
+        if (s->sectors == 0) {
+            /*
+             * s->sectors is under guest control via INITIALIZE DRIVE
+             * PARAMETERS and can be 0; this is an error but can't
+             * be reported at INITIALIZE DRIVE PARAMETERS time so we
+             * have to cope with the bad value here.
+             * (s->heads is always at least 1.)
+             */
+            s->hcyl = 0;
+            s->lcyl = 0;
+            s->select = s->select & ~(ATA_DEV_HS);
+            s->sector = 1;
+        } else {
+            cyl = sector_num / (s->heads * s->sectors);
+            r = sector_num % (s->heads * s->sectors);
+            s->hcyl = cyl >> 8;
+            s->lcyl = cyl;
+            s->select = (s->select & ~(ATA_DEV_HS)) |
+                ((r / s->sectors) & (ATA_DEV_HS));
+            s->sector = (r % s->sectors) + 1;
+        }
     }
 }
#2 lore raw

Ping^2 ?

thanks
-- PMM

Quoted 72 lines · Peter Maydell

Ping -- would anybody on the qemu-block side like to review this?

thanks
-- PMM

Quoted 65 lines · Peter Maydell

In ide_set_sector() if we're using CHS addressing the way we update the drive state involves dividing by (s->heads * s->sectors). These values can be set by the guest using the INITIALIZE DRIVE PARAMETERS command, which means that s->sectors can be 0. (s->heads can't be 0 because the command passes a heads-1 value.) INITIALIZE DRIVE PARAMETERS is specified to not check the input values for validity; instead no error is posted until some other command makes an illegal access. So we have to cope with a 0 divisor here.

Special-case s->sectors being zero.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2399

---
We don't seem to ever actually check that the guest's read/write ops are within the CHS limits it sets with INITIALIZE DRIVE PARAMETERS -- ide_sect_range_ok() only checks against the underlying block device geometry. Maybe we should? Then a zero-sectors misconfig would result in our reporting the error on the read/write as the spec suggests we should. This patch fixes the division-by-zero, at least, though.

---
 hw/ide/core.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
hw/ide/core.c +21 -7
index f78b00220b..b030531534 100644
@@ -646,13 +646,27 @@ void ide_set_sector(IDEState *s, int64_t sector_num)
         }
     } else {
         /* CHS */
-        cyl = sector_num / (s->heads * s->sectors);
-        r = sector_num % (s->heads * s->sectors);
-        s->hcyl = cyl >> 8;
-        s->lcyl = cyl;
-        s->select = (s->select & ~(ATA_DEV_HS)) |
-            ((r / s->sectors) & (ATA_DEV_HS));
-        s->sector = (r % s->sectors) + 1;
+        if (s->sectors == 0) {
+            /*
+             * s->sectors is under guest control via INITIALIZE DRIVE
+             * PARAMETERS and can be 0; this is an error but can't
+             * be reported at INITIALIZE DRIVE PARAMETERS time so we
+             * have to cope with the bad value here.
+             * (s->heads is always at least 1.)
+             */
+            s->hcyl = 0;
+            s->lcyl = 0;
+            s->select = s->select & ~(ATA_DEV_HS);
+            s->sector = 1;
+        } else {
+            cyl = sector_num / (s->heads * s->sectors);
+            r = sector_num % (s->heads * s->sectors);
+            s->hcyl = cyl >> 8;
+            s->lcyl = cyl;
+            s->select = (s->select & ~(ATA_DEV_HS)) |
+                ((r / s->sectors) & (ATA_DEV_HS));
+            s->sector = (r % s->sectors) + 1;
+        }
     }
 }
#3 lore raw

Denis, would you be interested?

Quoted 78 lines · Peter Maydell

Ping^2 ?

thanks
-- PMM

Quoted 72 lines · Peter Maydell

Ping -- would anybody on the qemu-block side like to review this?

thanks
-- PMM

Quoted 65 lines · Peter Maydell

In ide_set_sector() if we're using CHS addressing the way we update the drive state involves dividing by (s->heads * s->sectors). These values can be set by the guest using the INITIALIZE DRIVE PARAMETERS command, which means that s->sectors can be 0. (s->heads can't be 0 because the command passes a heads-1 value.) INITIALIZE DRIVE PARAMETERS is specified to not check the input values for validity; instead no error is posted until some other command makes an illegal access. So we have to cope with a 0 divisor here.

Special-case s->sectors being zero.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2399

---
We don't seem to ever actually check that the guest's read/write ops are within the CHS limits it sets with INITIALIZE DRIVE PARAMETERS -- ide_sect_range_ok() only checks against the underlying block device geometry. Maybe we should? Then a zero-sectors misconfig would result in our reporting the error on the read/write as the spec suggests we should. This patch fixes the division-by-zero, at least, though.

---
 hw/ide/core.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
hw/ide/core.c +21 -7
index f78b00220b..b030531534 100644
@@ -646,13 +646,27 @@ void ide_set_sector(IDEState *s, int64_t sector_num)
         }
     } else {
         /* CHS */
-        cyl = sector_num / (s->heads * s->sectors);
-        r = sector_num % (s->heads * s->sectors);
-        s->hcyl = cyl >> 8;
-        s->lcyl = cyl;
-        s->select = (s->select & ~(ATA_DEV_HS)) |
-            ((r / s->sectors) & (ATA_DEV_HS));
-        s->sector = (r % s->sectors) + 1;
+        if (s->sectors == 0) {
+            /*
+             * s->sectors is under guest control via INITIALIZE DRIVE
+             * PARAMETERS and can be 0; this is an error but can't
+             * be reported at INITIALIZE DRIVE PARAMETERS time so we
+             * have to cope with the bad value here.
+             * (s->heads is always at least 1.)
+             */
+            s->hcyl = 0;
+            s->lcyl = 0;
+            s->select = s->select & ~(ATA_DEV_HS);
+            s->sector = 1;
+        } else {
+            cyl = sector_num / (s->heads * s->sectors);
+            r = sector_num % (s->heads * s->sectors);
+            s->hcyl = cyl >> 8;
+            s->lcyl = cyl;
+            s->select = (s->select & ~(ATA_DEV_HS)) |
+                ((r / s->sectors) & (ATA_DEV_HS));
+            s->sector = (r % s->sectors) + 1;
+        }
     }
 }
#4 lore raw

Sure. Thanks.

Please let me know if we have something else stalled.

Den

Quoted 80 lines · Markus Armbruster

Denis, would you be interested?

Quoted 76 lines · Peter Maydell

Ping^2 ?

thanks
-- PMM

Quoted 70 lines · Peter Maydell

Ping -- would anybody on the qemu-block side like to review this?

thanks
-- PMM

Quoted 64 lines · Peter Maydell

In ide_set_sector() if we're using CHS addressing the way we update the drive state involves dividing by (s->heads * s->sectors). These values can be set by the guest using the INITIALIZE DRIVE PARAMETERS command, which means that s->sectors can be 0. (s->heads can't be 0 because the command passes a heads-1 value.) INITIALIZE DRIVE PARAMETERS is specified to not check the input values for validity; instead no error is posted until some other command makes an illegal access. So we have to cope with a 0 divisor here.

Special-case s->sectors being zero.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2399

---
We don't seem to ever actually check that the guest's read/write ops are within the CHS limits it sets with INITIALIZE DRIVE PARAMETERS -- ide_sect_range_ok() only checks against the underlying block device geometry. Maybe we should? Then a zero-sectors misconfig would result in our reporting the error on the read/write as the spec suggests we should. This patch fixes the division-by-zero, at least, though.

---
 hw/ide/core.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
hw/ide/core.c +21 -7
index f78b00220b..b030531534 100644
@@ -646,13 +646,27 @@ void ide_set_sector(IDEState *s, int64_t sector_num)
         }
     } else {
         /* CHS */
-        cyl = sector_num / (s->heads * s->sectors);
-        r = sector_num % (s->heads * s->sectors);
-        s->hcyl = cyl >> 8;
-        s->lcyl = cyl;
-        s->select = (s->select & ~(ATA_DEV_HS)) |
-            ((r / s->sectors) & (ATA_DEV_HS));
-        s->sector = (r % s->sectors) + 1;
+        if (s->sectors == 0) {
+            /*
+             * s->sectors is under guest control via INITIALIZE DRIVE
+             * PARAMETERS and can be 0; this is an error but can't
+             * be reported at INITIALIZE DRIVE PARAMETERS time so we
+             * have to cope with the bad value here.
+             * (s->heads is always at least 1.)
+             */
+            s->hcyl = 0;
+            s->lcyl = 0;
+            s->select = s->select & ~(ATA_DEV_HS);
+            s->sector = 1;
+        } else {
+            cyl = sector_num / (s->heads * s->sectors);
+            r = sector_num % (s->heads * s->sectors);
+            s->hcyl = cyl >> 8;
+            s->lcyl = cyl;
+            s->select = (s->select & ~(ATA_DEV_HS)) |
+                ((r / s->sectors) & (ATA_DEV_HS));
+            s->sector = (r % s->sectors) + 1;
+        }
     }
 }
#5 lore raw
In reply to Peter Maydell

I have spent some time on what the ATA contract actually says about INITIALIZE DEVICE PARAMETERS, mostly because of the reasoning quoted below, and ended up with a set of findings.

Quoted 4 lines · Peter Maydell

INITIALIZE DRIVE
PARAMETERS is specified to not check the input values for validity; instead no error is posted until some other command makes an illegal access. So we have to cope with a 0 divisor here.

1) That premise does not hold for ATA-2 and later. ATA-5 8.16.8, with the same wording in ATA-4 8.16.8:

If the host requests a CHS translation that is not supported by the device, the device shall return command aborted. [...] If the requested CHS translation is not supported, the device shall fail all media access commands with an ID Not Found error until a valid CHS translation is established.

ATA-2 7.18 speaks to the "no error is posted" behaviour directly in NOTE 26: previous ATA specifications were unclear, some implementations indicate no error at all, but most of those still fail media access commands. The command was dropped entirely in ATA-6 along with CHS addressing.

2) Zero sectors per logical track is an unsupported translation by definition, not a borderline case: ATA-5 6.2 numbers CHS sectors from one, and ATA-2 D.2.8 limits IDENTIFY DEVICE word 56 to values between 1 and 255. So 8.16.6 has us abort the command, and the error belongs there rather than on the later access.

Your question below the --- line, whether we should check the addresses a guest uses against the CHS limits it selected, is a real gap as well, but I would keep it apart from the crash fix.

For me this changes where the fix has to live. There are further consequences, one of them around migration, but they are much easier to discuss with the code in hand, so let me send that first.

Den