Skip to contentSkip to conversation

hw/sd/sdhci: Do not set Buf Wr Ena before writing block (CVE-2022-3872)

PATCH Applied Philippe Mathieu-Daudé 5 participants 13 messages Nov 7, 2022 – Feb 24

lore
Share & tools
lore
#0 lore raw

cover hw/sd/sdhci: Do not set Buf Wr Ena before writing block (CVE-2022-3872)

Fix for CVE-2022-3872. See description in first patch.

Philippe Mathieu-Daudé (2):
hw/sd/sdhci: Do not set Buf Wr Ena before writing block

    (CVE-2022-3872)

hw/sd/sdhci: Factor common Present State bits in sdhci_data_transfer()

 hw/sd/sdhci.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/2 hw/sd/sdhci: Do not set Buf Wr Ena before writing block (CVE-2022-3872)

When sdhci_write_block_to_card() is called to transfer data from the FIFO to the SD bus, the data is already present in the buffer and we have to consume it directly.

See the description of the 'Buffer Write Enable' bit from the 'Present State' register (prnsts::SDHC_SPACE_AVAILABLE) in Table 2.14 from the SDHCI spec v2:

Buffer Write Enable

This status is used for non-DMA write transfers.

The Host Controller can implement multiple buffers to transfer data efficiently. This read only flag indicates if space is available for write data. If this bit is 1, data can be written to the buffer. A change of this bit from 1 to 0 occurs when all the block data is written to the buffer. A change of this bit from 0 to 1 occurs when top of block data can be written to the buffer and generates the Buffer Write Ready interrupt.

In our case, we do not want to overwrite the buffer, so we want this bit to be 0, then set it to 1 once the data is written onto the bus.

This is probably a copy/paste error from commit d7dfca0807 ("hw/sdhci: introduce standard SD host controller").

Reproducer:
https://lore.kernel.org/qemu-devel/CAA8xKjXrmS0fkr28AKvNNpyAtM0y0B+5FichpsrhD+mUgnuyKg@mail.gmail.com/

---
 hw/sd/sdhci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/sdhci.c +1 -1
index 306070c872..f230e7475f 100644
@@ -954,7 +954,7 @@ static void sdhci_data_transfer(void *opaque)
             sdhci_read_block_from_card(s);
         } else {
             s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
-                    SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
+                                           SDHC_DATA_INHIBIT;
             sdhci_write_block_to_card(s);
         }
     }
Signature · 1 line
2.38.1
Tested-by on v1 1/2
#2 lore raw
Quoted 54 lines · Philippe Mathieu-Daudé

When sdhci_write_block_to_card() is called to transfer data from the FIFO to the SD bus, the data is already present in the buffer and we have to consume it directly.

See the description of the 'Buffer Write Enable' bit from the 'Present State' register (prnsts::SDHC_SPACE_AVAILABLE) in Table 2.14 from the SDHCI spec v2:

Buffer Write Enable

This status is used for non-DMA write transfers.

The Host Controller can implement multiple buffers to transfer data efficiently. This read only flag indicates if space is available for write data. If this bit is 1, data can be written to the buffer. A change of this bit from 1 to 0 occurs when all the block data is written to the buffer. A change of this bit from 0 to 1 occurs when top of block data can be written to the buffer and generates the Buffer Write Ready interrupt.

In our case, we do not want to overwrite the buffer, so we want this bit to be 0, then set it to 1 once the data is written onto the bus.

This is probably a copy/paste error from commit d7dfca0807 ("hw/sdhci: introduce standard SD host controller").

Reproducer:
https://lore.kernel.org/qemu-devel/CAA8xKjXrmS0fkr28AKvNNpyAtM0y0B+5FichpsrhD+mUgnuyKg@mail.gmail.com/

---
 hw/sd/sdhci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/sdhci.c +1 -2
index 306070c872..f230e7475f 100644
@@ -954,7 +954,7 @@ static void sdhci_data_transfer(void *opaque)
             sdhci_read_block_from_card(s);
         } else {
             s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
-                    SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
+                                           SDHC_DATA_INHIBIT;
             sdhci_write_block_to_card(s);
         }
     }
--
Quoted 2 lines

2.38.1

Thank you,

--
Mauro Matteo Cascella
Red Hat Product Security
PGP-Key ID: BB3410B0

#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 27 lines · Philippe Mathieu-Daudé

When sdhci_write_block_to_card() is called to transfer data from the FIFO to the SD bus, the data is already present in the buffer and we have to consume it directly.

See the description of the 'Buffer Write Enable' bit from the 'Present State' register (prnsts::SDHC_SPACE_AVAILABLE) in Table 2.14 from the SDHCI spec v2:

Buffer Write Enable

This status is used for non-DMA write transfers.

The Host Controller can implement multiple buffers to transfer data efficiently. This read only flag indicates if space is available for write data. If this bit is 1, data can be written to the buffer. A change of this bit from 1 to 0 occurs when all the block data is written to the buffer. A change of this bit from 0 to 1 occurs when top of block data can be written to the buffer and generates the Buffer Write Ready interrupt.

In our case, we do not want to overwrite the buffer, so we want this bit to be 0, then set it to 1 once the data is written onto the bus.

This is probably a copy/paste error from commit d7dfca0807 ("hw/sdhci: introduce standard SD host controller").

Quoted 8 lines

Reproducer:
https://lore.kernel.org/qemu-devel/CAA8xKjXrmS0fkr28AKvNNpyAtM0y0B+5FichpsrhD+mUgnuyKg@mail.gmail.com/

Seems like OSS-Fuzz also found this, not sure why it never made it into a gitlab issue:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=45986#c4

Slightly shorter reproducer:

cat << EOF | ./qemu-system-i386 -display none -machine accel=qtest, -m \
512M -nodefaults -device sdhci-pci -device sd-card,drive=mydrive -drive \
if=none,index=0,file=null-co://,format=raw,id=mydrive -nographic -qtest \
stdio
outl 0xcf8 0x80001010
outl 0xcfc 0xe0000000
outl 0xcf8 0x80001001
outl 0xcfc 0x06000000
write 0xe0000058 0x1 0x6e
write 0xe0000059 0x1 0x5a
write 0xe0000028 0x1 0x10
write 0xe000002c 0x1 0x05
write 0x5a6e 0x1 0x21
write 0x5a75 0x1 0x20
write 0xe0000005 0x1 0x02
write 0xe000000c 0x1 0x01
write 0xe000000e 0x1 0x20
write 0xe000000f 0x1 0x00
write 0xe000000c 0x1 0x00
write 0xe0000020 0x1 0x00
EOF

#4 lore raw
Quoted 56 lines · Alexander Bulekov
Quoted 27 lines · Philippe Mathieu-Daudé

When sdhci_write_block_to_card() is called to transfer data from the FIFO to the SD bus, the data is already present in the buffer and we have to consume it directly.

See the description of the 'Buffer Write Enable' bit from the 'Present State' register (prnsts::SDHC_SPACE_AVAILABLE) in Table 2.14 from the SDHCI spec v2:

Buffer Write Enable

This status is used for non-DMA write transfers.

The Host Controller can implement multiple buffers to transfer data efficiently. This read only flag indicates if space is available for write data. If this bit is 1, data can be written to the buffer. A change of this bit from 1 to 0 occurs when all the block data is written to the buffer. A change of this bit from 0 to 1 occurs when top of block data can be written to the buffer and generates the Buffer Write Ready interrupt.

In our case, we do not want to overwrite the buffer, so we want this bit to be 0, then set it to 1 once the data is written onto the bus.

This is probably a copy/paste error from commit d7dfca0807 ("hw/sdhci: introduce standard SD host controller").

Quoted 8 lines

Reproducer:
https://lore.kernel.org/qemu-devel/CAA8xKjXrmS0fkr28AKvNNpyAtM0y0B+5FichpsrhD+mUgnuyKg@mail.gmail.com/

Seems like OSS-Fuzz also found this, not sure why it never made it into a gitlab issue:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=45986#c4

Slightly shorter reproducer:

cat << EOF | ./qemu-system-i386 -display none -machine accel=qtest, -m \
512M -nodefaults -device sdhci-pci -device sd-card,drive=mydrive -drive \
if=none,index=0,file=null-co://,format=raw,id=mydrive -nographic -qtest \
stdio
outl 0xcf8 0x80001010
outl 0xcfc 0xe0000000
outl 0xcf8 0x80001001
outl 0xcfc 0x06000000
write 0xe0000058 0x1 0x6e
write 0xe0000059 0x1 0x5a
write 0xe0000028 0x1 0x10
write 0xe000002c 0x1 0x05
write 0x5a6e 0x1 0x21

Quoted 8 lines

write 0x5a75 0x1 0x20
write 0xe0000005 0x1 0x02
write 0xe000000c 0x1 0x01
write 0xe000000e 0x1 0x20
write 0xe000000f 0x1 0x00
write 0xe000000c 0x1 0x00
write 0xe0000020 0x1 0x00
EOF

Hi Philippe,
I ran the fuzzer with this series applied and it found:

cat << EOF | ./qemu-system-i386 -display none -machine accel=qtest, -m \
512M -nodefaults -device sdhci-pci -device sd-card,drive=mydrive -drive \
if=none,index=0,file=null-co://,format=raw,id=mydrive -nographic -qtest \
stdio
outl 0xcf8 0x80001010
outl 0xcfc 0xe0000000
outl 0xcf8 0x80001004
outw 0xcfc 0x06
write 0xe0000028 0x1 0x08
write 0xe000002c 0x1 0x05
write 0xe0000005 0x1 0x02
write 0x0 0x1 0x21
write 0x3 0x1 0x20
write 0xe000000c 0x1 0x01
write 0xe000000e 0x1 0x20
write 0xe000000f 0x1 0x00
write 0xe000000c 0x1 0x20
write 0xe0000020 0x1 0x00
EOF

The crash seems very similar, but it looks like instead of SDHC_TRNS_READ this reproducer sets SDHC_TRNS_MULTI -Alex

#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 30 lines · Philippe Mathieu-Daudé

When sdhci_write_block_to_card() is called to transfer data from the FIFO to the SD bus, the data is already present in the buffer and we have to consume it directly.

See the description of the 'Buffer Write Enable' bit from the 'Present State' register (prnsts::SDHC_SPACE_AVAILABLE) in Table 2.14 from the SDHCI spec v2:

Buffer Write Enable

This status is used for non-DMA write transfers.

The Host Controller can implement multiple buffers to transfer data efficiently. This read only flag indicates if space is available for write data. If this bit is 1, data can be written to the buffer. A change of this bit from 1 to 0 occurs when all the block data is written to the buffer. A change of this bit from 0 to 1 occurs when top of block data can be written to the buffer and generates the Buffer Write Ready interrupt.

In our case, we do not want to overwrite the buffer, so we want this bit to be 0, then set it to 1 once the data is written onto the bus.

This is probably a copy/paste error from commit d7dfca0807 ("hw/sdhci: introduce standard SD host controller").

Reproducer:
https://lore.kernel.org/qemu-devel/CAA8xKjXrmS0fkr28AKvNNpyAtM0y0B+5FichpsrhD+mUgnuyKg@mail.gmail.com/

Quoted 6 lines

Hi!

It's been almost 3 years since this patch.
Is it still relevant?
Or maybe the prob has been fixed by later changes in this area?

Thanks,

/mjt

Quoted 16 lines
  hw/sd/sdhci.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/sdhci.c +1 -1
index 306070c872..f230e7475f 100644
@@ -954,7 +954,7 @@ static void sdhci_data_transfer(void *opaque)
              sdhci_read_block_from_card(s);
          } else {
              s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
-                    SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
+                                           SDHC_DATA_INHIBIT;
              sdhci_write_block_to_card(s);
          }
      }
#6 lore raw
Quoted 39 lines · Michael Tokarev
Quoted 30 lines · Philippe Mathieu-Daudé

When sdhci_write_block_to_card() is called to transfer data from the FIFO to the SD bus, the data is already present in the buffer and we have to consume it directly.

See the description of the 'Buffer Write Enable' bit from the 'Present State' register (prnsts::SDHC_SPACE_AVAILABLE) in Table 2.14 from the SDHCI spec v2:

Buffer Write Enable

This status is used for non-DMA write transfers.

The Host Controller can implement multiple buffers to transfer data efficiently. This read only flag indicates if space is available for write data. If this bit is 1, data can be written to the buffer. A change of this bit from 1 to 0 occurs when all the block data is written to the buffer. A change of this bit from 0 to 1 occurs when top of block data can be written to the buffer and generates the Buffer Write Ready interrupt.

In our case, we do not want to overwrite the buffer, so we want this bit to be 0, then set it to 1 once the data is written onto the bus.

This is probably a copy/paste error from commit d7dfca0807 ("hw/sdhci: introduce standard SD host controller").

Reproducer:
https://lore.kernel.org/qemu-devel/CAA8xKjXrmS0fkr28AKvNNpyAtM0y0B+5FichpsrhD+mUgnuyKg@mail.gmail.com/

Quoted 6 lines
Quoted 5 lines

Hi!

It's been almost 3 years since this patch.
Is it still relevant?
Or maybe the prob has been fixed by later changes in this area?

SDHC_SPACE_AVAILABLE is still set in the write path:
https://gitlab.com/qemu-project/qemu/-/blob/master/hw/sd/sdhci.c?ref_type=heads#L988

If that is a bug, this patch should be applied regardless of its security implications (if any).

Quoted 21 lines

Thanks,

/mjt

Quoted 16 lines
  hw/sd/sdhci.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/sdhci.c +1 -1
index 306070c872..f230e7475f 100644
@@ -954,7 +954,7 @@ static void sdhci_data_transfer(void *opaque)
              sdhci_read_block_from_card(s);
          } else {
              s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
-                    SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
+                                           SDHC_DATA_INHIBIT;
              sdhci_write_block_to_card(s);
          }
      }
Signature · 3 lines
Mauro Matteo Cascella
Red Hat Product Security
PGP-Key ID: BB3410B0
#7 lore raw
In reply to Philippe Mathieu-Daudé

Ping once again?

Thanks,

/mjt

Quoted 53 lines · Philippe Mathieu-Daudé

When sdhci_write_block_to_card() is called to transfer data from the FIFO to the SD bus, the data is already present in the buffer and we have to consume it directly.

See the description of the 'Buffer Write Enable' bit from the 'Present State' register (prnsts::SDHC_SPACE_AVAILABLE) in Table 2.14 from the SDHCI spec v2:

Buffer Write Enable

This status is used for non-DMA write transfers.

The Host Controller can implement multiple buffers to transfer data efficiently. This read only flag indicates if space is available for write data. If this bit is 1, data can be written to the buffer. A change of this bit from 1 to 0 occurs when all the block data is written to the buffer. A change of this bit from 0 to 1 occurs when top of block data can be written to the buffer and generates the Buffer Write Ready interrupt.

In our case, we do not want to overwrite the buffer, so we want this bit to be 0, then set it to 1 once the data is written onto the bus.

This is probably a copy/paste error from commit d7dfca0807 ("hw/sdhci: introduce standard SD host controller").

Reproducer:
https://lore.kernel.org/qemu-devel/CAA8xKjXrmS0fkr28AKvNNpyAtM0y0B+5FichpsrhD+mUgnuyKg@mail.gmail.com/

---
  hw/sd/sdhci.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/sdhci.c +1 -1
index 306070c872..f230e7475f 100644
@@ -954,7 +954,7 @@ static void sdhci_data_transfer(void *opaque)
              sdhci_read_block_from_card(s);
          } else {
              s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
-                    SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
+                                           SDHC_DATA_INHIBIT;
              sdhci_write_block_to_card(s);
          }
      }
#8 lore raw
Quoted 2 lines · Michael Tokarev

Ping once again?

FWIW, none of the reproducers in the thread work for me anymore and OSS-Fuzz claims the issue was fixed sometime in April 2024:
https://issues.oss-fuzz.com/issues/42524205#comment5
-Alex

Quoted 59 lines

Thanks,

/mjt

Quoted 53 lines · Philippe Mathieu-Daudé

When sdhci_write_block_to_card() is called to transfer data from the FIFO to the SD bus, the data is already present in the buffer and we have to consume it directly.

See the description of the 'Buffer Write Enable' bit from the 'Present State' register (prnsts::SDHC_SPACE_AVAILABLE) in Table 2.14 from the SDHCI spec v2:

Buffer Write Enable

This status is used for non-DMA write transfers.

The Host Controller can implement multiple buffers to transfer data efficiently. This read only flag indicates if space is available for write data. If this bit is 1, data can be written to the buffer. A change of this bit from 1 to 0 occurs when all the block data is written to the buffer. A change of this bit from 0 to 1 occurs when top of block data can be written to the buffer and generates the Buffer Write Ready interrupt.

In our case, we do not want to overwrite the buffer, so we want this bit to be 0, then set it to 1 once the data is written onto the bus.

This is probably a copy/paste error from commit d7dfca0807 ("hw/sdhci: introduce standard SD host controller").

Reproducer:
https://lore.kernel.org/qemu-devel/CAA8xKjXrmS0fkr28AKvNNpyAtM0y0B+5FichpsrhD+mUgnuyKg@mail.gmail.com/

---
  hw/sd/sdhci.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/sdhci.c +1 -1
index 306070c872..f230e7475f 100644
@@ -954,7 +954,7 @@ static void sdhci_data_transfer(void *opaque)
              sdhci_read_block_from_card(s);
          } else {
              s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
-                    SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
+                                           SDHC_DATA_INHIBIT;
              sdhci_write_block_to_card(s);
          }
      }
#9 lore raw
Quoted 7 lines · Alexander Bulekov
Quoted 2 lines · Michael Tokarev

Ping once again?

FWIW, none of the reproducers in the thread work for me anymore and OSS-Fuzz claims the issue was fixed sometime in April 2024:
https://issues.oss-fuzz.com/issues/42524205#comment5

ok. I bisected using the reproducer from
https://lore.kernel.org/qemu-devel/CAA8xKjXrmS0fkr28AKvNNpyAtM0y0B+5FichpsrhD+mUgnuyKg@mail.gmail.com/ - and it looks like first, the invalid write were replaced with an
assertion failure after this commit, which is itself is a fix for
CVE-2024-3447:

commit 9e4b27ca6bf4974f169bbca7f3dca117b1208b6f (v9.0.0-rc2-64-g9e4b27ca6b)

Author: Philippe Mathieu-Daudé <[email protected]>
Date:   Tue Apr 9 16:19:27 2024 +0200

hw/sd/sdhci: Do not update TRNMOD when Command Inhibit (DAT) is set

https://gitlab.com/qemu-project/qemu/-/commit/9e4b27ca6bf4974f169bbca7f3dca117b1208b6f

So yes, this is in Apr-24, and it is in v9.0.0.

After this commit, the invalid write has gone, instead, we started hitting assertion failure in sdhci_write_dataport():

hw/sd/sdhci.c:565: sdhci_write_dataport: Assertion `s->data_count <
s->buf_maxsz' failed.

next, in

commit ed5a159c3de48a581f46de4c8c02b4b295e6c52d (v9.1.0-rc0-80-ged5a159c3d)

Author: Philippe Mathieu-Daudé <[email protected]>
Date:   Tue Jul 30 10:41:25 2024 +0200

hw/sd/sdhci: Reset @data_count index on invalid ADMA transfers

https://gitlab.com/qemu-project/qemu/-/commit/ed5a159c3de48a581f46de4c8c02b4b295e6c52d

the assertion failure has gone for good, at least with this reproducer.

(the first commit is v7.2.10-56-g2429cb7a9f in 7.2.x series, second - v7.2.13-25-gfc2e706f4c).

So it looks like the issue has been fixed for good in v9.1.0 and v7.2.14.

Please take a look at the original bug report and the final logic as per this message - does it look sane?

Quoted 61 lines
Quoted 60 lines

Thanks,

/mjt

Quoted 53 lines · Philippe Mathieu-Daudé

When sdhci_write_block_to_card() is called to transfer data from the FIFO to the SD bus, the data is already present in the buffer and we have to consume it directly.

See the description of the 'Buffer Write Enable' bit from the 'Present State' register (prnsts::SDHC_SPACE_AVAILABLE) in Table 2.14 from the SDHCI spec v2:

Buffer Write Enable

This status is used for non-DMA write transfers.

The Host Controller can implement multiple buffers to transfer data efficiently. This read only flag indicates if space is available for write data. If this bit is 1, data can be written to the buffer. A change of this bit from 1 to 0 occurs when all the block data is written to the buffer. A change of this bit from 0 to 1 occurs when top of block data can be written to the buffer and generates the Buffer Write Ready interrupt.

In our case, we do not want to overwrite the buffer, so we want this bit to be 0, then set it to 1 once the data is written onto the bus.

This is probably a copy/paste error from commit d7dfca0807 ("hw/sdhci: introduce standard SD host controller").

Reproducer:
https://lore.kernel.org/qemu-devel/CAA8xKjXrmS0fkr28AKvNNpyAtM0y0B+5FichpsrhD+mUgnuyKg@mail.gmail.com/

---
   hw/sd/sdhci.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/sdhci.c +1 -1
index 306070c872..f230e7475f 100644
@@ -954,7 +954,7 @@ static void sdhci_data_transfer(void *opaque)
               sdhci_read_block_from_card(s);
           } else {
               s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
-                    SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
+                                           SDHC_DATA_INHIBIT;
               sdhci_write_block_to_card(s);
           }
       }
#10 lore raw
Quoted 45 lines · Michael Tokarev
Quoted 7 lines · Alexander Bulekov
Quoted 2 lines · Michael Tokarev

Ping once again?

FWIW, none of the reproducers in the thread work for me anymore and OSS-Fuzz claims the issue was fixed sometime in April 2024:
https://issues.oss-fuzz.com/issues/42524205#comment5

ok.  I bisected using the reproducer from
https://lore.kernel.org/qemu-devel/ CAA8xKjXrmS0fkr28AKvNNpyAtM0y0B+5FichpsrhD+mUgnuyKg@mail.gmail.com/ - and it looks like first, the invalid write were replaced with an
assertion failure after this commit, which is itself is a fix for
CVE-2024-3447:

commit 9e4b27ca6bf4974f169bbca7f3dca117b1208b6f (v9.0.0-rc2-64-g9e4b27ca6b)
Author: Philippe Mathieu-Daudé <[email protected]>
Date:   Tue Apr 9 16:19:27 2024 +0200

    hw/sd/sdhci: Do not update TRNMOD when Command Inhibit (DAT) is set

https://gitlab.com/qemu-project/qemu/-/ commit/9e4b27ca6bf4974f169bbca7f3dca117b1208b6f

So yes, this is in Apr-24, and it is in v9.0.0.

After this commit, the invalid write has gone, instead, we started hitting assertion failure in sdhci_write_dataport():

 hw/sd/sdhci.c:565: sdhci_write_dataport: Assertion `s->data_count < s-

Quoted 1 line

buf_maxsz' failed.

next, in

commit ed5a159c3de48a581f46de4c8c02b4b295e6c52d (v9.1.0-rc0-80-ged5a159c3d)
Author: Philippe Mathieu-Daudé <[email protected]>
Date:   Tue Jul 30 10:41:25 2024 +0200

    hw/sd/sdhci: Reset @data_count index on invalid ADMA transfers

https://gitlab.com/qemu-project/qemu/-/commit/ ed5a159c3de48a581f46de4c8c02b4b295e6c52d

the assertion failure has gone for good, at least with this reproducer.

Quoted 8 lines

(the first commit is v7.2.10-56-g2429cb7a9f in 7.2.x series, second - v7.2.13-25-gfc2e706f4c).

So it looks like the issue has been fixed for good in v9.1.0 and v7.2.14.

Please take a look at the original bug report and the final logic as per this message - does it look sane?

Hi! Can someone more knowlegeable in this area than me actually take a look there please? Does this change actually fix the prob?

Thanks,

/mjt

#11 lore raw

PATCH 2/2 hw/sd/sdhci: Factor common Present State bits in sdhci_data_transfer()

In reply to Philippe Mathieu-Daudé

When we do a non-DMA transfer, we have to set the 'DAT Line Active' and 'Command Inhibit (DAT)' bits in the 'Present State' register.

Factor that common code to ease code review.

---
 hw/sd/sdhci.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/sd/sdhci.c +5 -4
index f230e7475f..e9bb3576f4 100644
@@ -909,6 +909,7 @@ static void sdhci_data_transfer(void *opaque)
     SDHCIState *s = (SDHCIState *)opaque;
 
     if (s->trnmod & SDHC_TRNS_DMA) {
+        /* DMA Data transfer: DMA functionality available and enabled */
         switch (SDHC_DMA_TYPE(s->hostctl1)) {
         case SDHC_CTRL_SDMA:
             if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) {
@@ -948,13 +949,13 @@ static void sdhci_data_transfer(void *opaque)
             break;
         }
     } else {
+        /* Non-DMA data transfer: DMA functionality not available or disabled */
+        s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DATA_INHIBIT;
         if ((s->trnmod & SDHC_TRNS_READ) && sdbus_data_ready(&s->sdbus)) {
-            s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
-                    SDHC_DAT_LINE_ACTIVE;
+            s->prnsts |= SDHC_DOING_READ;
             sdhci_read_block_from_card(s);
         } else {
-            s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
-                                           SDHC_DATA_INHIBIT;
+            s->prnsts |= SDHC_DOING_WRITE;
             sdhci_write_block_to_card(s);
         }
     }
Signature · 1 line
2.38.1