Skip to contentSkip to conversation

hw: misc: edu: fix 2 off-by-one errors

PATCH Superseded Chris Friedt 6 participants 14 messages Oct 15, 2022 – Nov 1, 2022

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hw: misc: edu: fix 2 off-by-one errors

From: Christopher Friedt <[email protected]>

In the case that size1 was zero, because of the explicit 'end1 > addr' check, the range check would fail and the error message would read as shown below. The correct comparison is 'end1 >= addr' (or 'addr <= end1').

EDU: DMA range 0x40000-0x3ffff out of bounds (0x40000-0x40fff)!

At the opposite end, in the case that size1 was 4096, within() would fail because of the non-inclusive check 'end1 < end2', which should have been 'end1 <= end2'. The error message would previously say

EDU: DMA range 0x40000-0x40fff out of bounds (0x40000-0x40fff)!

This change
1. renames local variables to be more less ambiguous
2. fixes the two off-by-one errors described above.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1254

---
 hw/misc/edu.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
hw/misc/edu.c +12 -13
index e935c418d4..52afbd792a 100644
@@ -103,25 +103,24 @@ static void edu_lower_irq(EduState *edu, uint32_t val)
     }
 }
 
-static bool within(uint64_t addr, uint64_t start, uint64_t end)
+static void edu_check_range(uint64_t xfer_start, uint64_t xfer_size,
+                uint64_t dma_start, uint64_t dma_size)
 {
-    return start <= addr && addr < end;
-}
-
-static void edu_check_range(uint64_t addr, uint64_t size1, uint64_t start,
-                uint64_t size2)
-{
-    uint64_t end1 = addr + size1;
-    uint64_t end2 = start + size2;
-
-    if (within(addr, start, end2) &&
-            end1 > addr && within(end1, start, end2)) {
+    uint64_t xfer_end = xfer_start + xfer_size;
+    uint64_t dma_end = dma_start + dma_size;
+
+    /*
+     * 1. ensure we aren't overflowing
+     * 2. ensure that xfer is within dma address range
+     */
+    if (dma_end >= dma_start && xfer_end >= xfer_start &&
+        xfer_start >= dma_start && xfer_end <= dma_end) {
         return;
     }
 
     hw_error("EDU: DMA range 0x%016"PRIx64"-0x%016"PRIx64
              " out of bounds (0x%016"PRIx64"-0x%016"PRIx64")!",
-            addr, end1 - 1, start, end2 - 1);
+            xfer_start, xfer_end - 1, dma_start, dma_end - 1);
 }
 
 static dma_addr_t edu_clamp_addr(const EduState *edu, dma_addr_t addr)
Signature · 1 line
2.36.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 16 lines · Chris Friedt

From: Christopher Friedt <[email protected]>

In the case that size1 was zero, because of the explicit 'end1 > addr' check, the range check would fail and the error message would read as shown below. The correct comparison is 'end1 >= addr' (or 'addr <= end1').

EDU: DMA range 0x40000-0x3ffff out of bounds (0x40000-0x40fff)!

At the opposite end, in the case that size1 was 4096, within() would fail because of the non-inclusive check 'end1 < end2', which should have been 'end1 <= end2'. The error message would previously say

EDU: DMA range 0x40000-0x40fff out of bounds (0x40000-0x40fff)!

Quoted 7 lines

This change
1. renames local variables to be more less ambiguous
2. fixes the two off-by-one errors described above.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1254

As a side-note, seems strange that edu_check_range will abort the entire VM if the check fails, rather than handling the error more elegantly. Maybe that's useful for students developing kernel drivers against the device.

Quoted 50 lines
---
 hw/misc/edu.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
hw/misc/edu.c +12 -13
index e935c418d4..52afbd792a 100644
@@ -103,25 +103,24 @@ static void edu_lower_irq(EduState *edu, uint32_t val)
     }
 }
 
-static bool within(uint64_t addr, uint64_t start, uint64_t end)
+static void edu_check_range(uint64_t xfer_start, uint64_t xfer_size,
+                uint64_t dma_start, uint64_t dma_size)
 {
-    return start <= addr && addr < end;
-}
-
-static void edu_check_range(uint64_t addr, uint64_t size1, uint64_t start,
-                uint64_t size2)
-{
-    uint64_t end1 = addr + size1;
-    uint64_t end2 = start + size2;
-
-    if (within(addr, start, end2) &&
-            end1 > addr && within(end1, start, end2)) {
+    uint64_t xfer_end = xfer_start + xfer_size;
+    uint64_t dma_end = dma_start + dma_size;
+
+    /*
+     * 1. ensure we aren't overflowing
+     * 2. ensure that xfer is within dma address range
+     */
+    if (dma_end >= dma_start && xfer_end >= xfer_start &&
+        xfer_start >= dma_start && xfer_end <= dma_end) {
         return;
     }
 
     hw_error("EDU: DMA range 0x%016"PRIx64"-0x%016"PRIx64
              " out of bounds (0x%016"PRIx64"-0x%016"PRIx64")!",
-            addr, end1 - 1, start, end2 - 1);
+            xfer_start, xfer_end - 1, dma_start, dma_end - 1);
 }
 
 static dma_addr_t edu_clamp_addr(const EduState *edu, dma_addr_t addr)
Signature · 1 line
2.36.1
#2 lore raw
Quoted 22 lines · Alexander Bulekov
Quoted 20 lines · Chris Friedt

From: Christopher Friedt <[email protected]>

In the case that size1 was zero, because of the explicit 'end1 > addr' check, the range check would fail and the error message would read as shown below. The correct comparison is 'end1 >= addr' (or 'addr <= end1').

EDU: DMA range 0x40000-0x3ffff out of bounds (0x40000-0x40fff)!

At the opposite end, in the case that size1 was 4096, within() would fail because of the non-inclusive check 'end1 < end2', which should have been 'end1 <= end2'. The error message would previously say

EDU: DMA range 0x40000-0x40fff out of bounds (0x40000-0x40fff)!

This change
1. renames local variables to be more less ambiguous
2. fixes the two off-by-one errors described above.

Quoted 8 lines
Quoted 3 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1254

As a side-note, seems strange that edu_check_range will abort the entire VM if the check fails, rather than handling the error more elegantly.

Yes, this is bad for a device model, though we have a lot of older device models that still do it. The preferred pattern is:
* for situations which are "if this happens there is a bug in QEMU itself", use assert. hw_error() is a kind of assert that prints a bunch of guest register state: sometimes you want that, but more often you just want normal assert()
* for situations where the guest has misprogrammed the device, log that with qemu_log_mask(LOG_GUEST_ERROR, ...) and continue with whatever the real hardware would do, or some reasonable choice if the h/w spec is vague
* for situations where the guest is correct but is trying to get the device to do something our model doesn't implement yet, same as above but with LOG_UNIMP.

Probably most hw_error() uses in the codebase should be replaced with one of the above options.

thanks
-- PMM

#3 lore raw
Quoted 46 lines · Peter Maydell
Quoted 22 lines · Alexander Bulekov
Quoted 20 lines · Chris Friedt

From: Christopher Friedt <[email protected]>

In the case that size1 was zero, because of the explicit 'end1 > addr' check, the range check would fail and the error message would read as shown below. The correct comparison is 'end1 >= addr' (or 'addr <= end1').

EDU: DMA range 0x40000-0x3ffff out of bounds (0x40000-0x40fff)!

At the opposite end, in the case that size1 was 4096, within() would fail because of the non-inclusive check 'end1 < end2', which should have been 'end1 <= end2'. The error message would previously say

EDU: DMA range 0x40000-0x40fff out of bounds (0x40000-0x40fff)!

This change
1. renames local variables to be more less ambiguous
2. fixes the two off-by-one errors described above.

Quoted 8 lines
Quoted 3 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1254

As a side-note, seems strange that edu_check_range will abort the entire VM if the check fails, rather than handling the error more elegantly.

Yes, this is bad for a device model, though we have a lot of older device models that still do it. The preferred pattern is:
* for situations which are "if this happens there is a bug in QEMU itself", use assert. hw_error() is a kind of assert that prints a bunch of guest register state: sometimes you want that, but more often you just want normal assert()
* for situations where the guest has misprogrammed the device, log that with qemu_log_mask(LOG_GUEST_ERROR, ...) and continue with whatever the real hardware would do, or some reasonable choice if the h/w spec is vague
* for situations where the guest is correct but is trying to get the device to do something our model doesn't implement yet, same as above but with LOG_UNIMP.

Quoted 2 lines

Probably most hw_error() uses in the codebase should be replaced with one of the above options.

We should probably document this best practice somewhere in docs/devel but I guess we really need a "guide to writing device emulation" section.

Quoted 3 lines

thanks
-- PMM

Signature · 1 line
Alex Bennée
#4 lore raw
Quoted 32 lines · Alex Bennée

>

Quoted 24 lines · Peter Maydell
Quoted 9 lines · Alexander Bulekov
Quoted 2 lines · Chris Friedt

From: Christopher Friedt <[email protected]>

As a side-note, seems strange that edu_check_range will abort the entire VM if the check fails, rather than handling the error more elegantly.

Yes, this is bad for a device model, though we have a lot of older device models that still do it. The preferred pattern is:
* for situations which are "if this happens there is a bug in QEMU itself", use assert. hw_error() is a kind of assert that prints a bunch of guest register state: sometimes you want that, but more often you just want normal assert()
* for situations where the guest has misprogrammed the device, log that with qemu_log_mask(LOG_GUEST_ERROR, ...) and continue with whatever the real hardware would do, or some reasonable choice if the h/w spec is vague
* for situations where the guest is correct but is trying to get the device to do something our model doesn't implement yet, same as above but with LOG_UNIMP.

Quoted 6 lines
Quoted 2 lines

Probably most hw_error() uses in the codebase should be replaced with one of the above options.

We should probably document this best practice somewhere in docs/devel but I guess we really need a "guide to writing device emulation" section.

Should I make a separate PR for that or attach it to the existing series (at v3 now)?

Thanks,

C

#5 lore raw
Quoted 33 lines · Chris Friedt
Quoted 33 lines · Alex Bennée

>

Quoted 26 lines · Peter Maydell
Quoted 9 lines · Alexander Bulekov
Quoted 2 lines · Chris Friedt

From: Christopher Friedt <[email protected]>

As a side-note, seems strange that edu_check_range will abort the entire VM if the check fails, rather than handling the error more elegantly.

Yes, this is bad for a device model, though we have a lot of older device models that still do it. The preferred pattern is:
* for situations which are "if this happens there is a bug in QEMU itself", use assert. hw_error() is a kind of assert that prints a bunch of guest register state: sometimes you want that, but more often you just want normal assert()
* for situations where the guest has misprogrammed the device, log that with qemu_log_mask(LOG_GUEST_ERROR, ...) and continue with whatever the real hardware would do, or some reasonable choice if the h/w spec is vague
* for situations where the guest is correct but is trying to get the device to do something our model doesn't implement yet, same as above but with LOG_UNIMP.

Probably most hw_error() uses in the codebase should be replaced with one of the above options.

Quoted 6 lines
Quoted 3 lines

We should probably document this best practice somewhere in docs/devel but I guess we really need a "guide to writing device emulation" section.

Should I make a separate PR for that or attach it to the existing series (at v3 now)?

I don't think improving our developer documentation needs to be part of this fix. However if you want to take a run at improving it in a new series be my guest ;-)

Quoted 4 lines

Thanks,

C

Signature · 1 line
Alex Bennée
#6 lore raw
In reply to Peter Maydell
Quoted 4 lines · Peter Maydell

* for situations where the guest has misprogrammed the device, log that with qemu_log_mask(LOG_GUEST_ERROR, ...) and continue with whatever the real hardware would do, or some reasonable choice if the h/w spec is vague

As I wrote in the previous mail, can we stop the machine after the print somehow, for example? So that the students have to "cont" in the qemu console as an acknowledgment when this happens.

thanks,

Signature · 2 lines
js
suse labs
#7 lore raw
Quoted 9 lines · Jiri Slaby
Quoted 4 lines · Peter Maydell

* for situations where the guest has misprogrammed the device, log that with qemu_log_mask(LOG_GUEST_ERROR, ...) and continue with whatever the real hardware would do, or some reasonable choice if the h/w spec is vague

As I wrote in the previous mail, can we stop the machine after the print somehow, for example? So that the students have to "cont" in the qemu console as an acknowledgment when this happens.

You can bring the system to a halt with vm_stop(RUN_STATE_PAUSED) or possible RUN_STATE_DEBUG?

I don't know how obnoxious the message should be at this point (i.e. should it be masked by LOG_GUEST_ERROR) because if the system halts the user might not notice. However I guess with this device they would be expecting to check this sort of thing.

Quoted 2 lines

thanks,

Signature · 1 line
Alex Bennée
#8 lore raw
Quoted 10 lines · Alex Bennée
Quoted 6 lines · Jiri Slaby
Quoted 4 lines · Peter Maydell

* for situations where the guest has misprogrammed the device, log that with qemu_log_mask(LOG_GUEST_ERROR, ...) and continue with whatever the real hardware would do, or some reasonable choice if the h/w spec is vague

Quoted 6 lines
Quoted 3 lines

As I wrote in the previous mail, can we stop the machine after the print somehow, for example? So that the students have to "cont" in the qemu console as an acknowledgment when this happens.

You can bring the system to a halt with vm_stop(RUN_STATE_PAUSED) or possible RUN_STATE_DEBUG?

No, please don't do anything like that. This should not be special. Just log a message if the guest does something bad. There are an absolute ton of things that the guest can do wrong, and in general QEMU does not attempt to be an "identify all the ways the guest does something wrong in a friendly way" system.

thanks
-- PMM

#9 lore raw
Quoted 15 lines · Peter Maydell
Quoted 14 lines · Alex Bennée
Quoted 9 lines · Jiri Slaby
Quoted 4 lines · Peter Maydell

* for situations where the guest has misprogrammed the device, log that with qemu_log_mask(LOG_GUEST_ERROR, ...) and continue with whatever the real hardware would do, or some reasonable choice if the h/w spec is vague

As I wrote in the previous mail, can we stop the machine after the print somehow, for example? So that the students have to "cont" in the qemu console as an acknowledgment when this happens.

Quoted 8 lines
Quoted 2 lines

You can bring the system to a halt with vm_stop(RUN_STATE_PAUSED) or possible RUN_STATE_DEBUG?

No, please don't do anything like that. This should not be special. Just log a message if the guest does something bad. There are an absolute ton of things that the guest can do wrong, and in general QEMU does not attempt to be an "identify all the ways the guest does something wrong in a friendly way" system.

We should clean-up the other uses of vm_stop in hw/ then:

  ./hw/ppc/prep_systemio.c:78:        vm_stop(RUN_STATE_PAUSED);
  ./hw/ppc/vof.c:921:        vm_stop(RUN_STATE_PAUSED);
  ./hw/vfio/pci.c:2725:    vm_stop(RUN_STATE_INTERNAL_ERROR);
Quoted 3 lines

thanks
-- PMM

Signature · 1 line
Alex Bennée
#10 lore raw
In reply to Alexander Bulekov
Quoted 23 lines · Alexander Bulekov
Quoted 22 lines · Chris Friedt

From: Christopher Friedt <[email protected]>

In the case that size1 was zero, because of the explicit 'end1 > addr' check, the range check would fail and the error message would read as shown below. The correct comparison is 'end1 >= addr' (or 'addr <= end1').

EDU: DMA range 0x40000-0x3ffff out of bounds (0x40000-0x40fff)!

At the opposite end, in the case that size1 was 4096, within() would fail because of the non-inclusive check 'end1 < end2', which should have been 'end1 <= end2'. The error message would previously say

EDU: DMA range 0x40000-0x40fff out of bounds (0x40000-0x40fff)!

This change
1. renames local variables to be more less ambiguous
2. fixes the two off-by-one errors described above.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1254

Quoted 8 lines
Quoted 1 line

As a side-note, seems strange that edu_check_range will abort the entire VM if the check fails, rather than handling the error more elegantly. Maybe that's useful for students developing kernel drivers against the device.

Yes, that was exactly the intention. First, as a punishment as they do something really wrong. Second, so they notice -- writing something wrong to a register of a real HW often freezes a machine too. Especially when misprogramming a DMA controller.

OTOH, this sucks too. Ext4 (and other FS too) is fine, they don't lose data. However they need to freshly boot, repair FS and investigate/think a lot. This trial and run (and crash) takes several loops for some.

So I am for softening it a bit. But they still should be noticed in some obvious way.

thanks,

Signature · 2 lines
js
suse labs
#11 lore raw
In reply to Chris Friedt
Quoted 14 lines · Chris Friedt

From: Christopher Friedt <[email protected]>

In the case that size1 was zero, because of the explicit 'end1 > addr' check, the range check would fail and the error message would read as shown below. The correct comparison is 'end1 >= addr' (or 'addr <= end1').

EDU: DMA range 0x40000-0x3ffff out of bounds (0x40000-0x40fff)!

At the opposite end, in the case that size1 was 4096, within() would fail because of the non-inclusive check 'end1 < end2', which should have been 'end1 <= end2'. The error message would previously say

Quoted 5 lines

EDU: DMA range 0x40000-0x40fff out of bounds (0x40000-0x40fff)!

This change
1. renames local variables to be more less ambiguous
2. fixes the two off-by-one errors described above.

This should be split into two patches. This way, it's hard to review.

Quoted 1 line

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1254

thanks,

Signature · 2 lines
js
suse labs
#12 lore raw
Quoted 3 lines · Jiri Slaby
Quoted 1 line · Chris Friedt

From: Christopher Friedt <[email protected]>

This should be split into two patches. This way, it's hard to review.

I can do that :-)

Thanks for the review

#13 lore raw

Hi Jiri, Peter,

Are you able to review the more recent version of this change?

Look for the subject "[PATCH v4 x/3] hw: misc: edu: ..."

I believe I addressed all concerns.

Cheers,

C

Quoted 9 lines · Christopher Friedt
Quoted 3 lines · Jiri Slaby
Quoted 1 line · Chris Friedt

From: Christopher Friedt <[email protected]>

This should be split into two patches. This way, it's hard to review.

I can do that :-)

Thanks for the review