hw/sd/sdcard: Fix CVE-2020-13253 (Do not allow invalid SD card sizes)
cover hw/sd/sdcard: Fix CVE-2020-13253 (Do not allow invalid SD card sizes)
Part 1 is already reviewed:
https://www.mail-archive.com/[email protected]/msg718150.html
However the CVE fix break Linux guests:
https://www.mail-archive.com/[email protected]/msg720737.html
This series fixes that, by checking the SD card image size is correct.
Based-on: <[email protected]>
Philippe Mathieu-Daudé (2):
tests/acceptance/boot_linux: Truncate SD card image to power of 2 hw/sd/sdcard: Do not allow invalid SD card sizes
hw/sd/sd.c | 16 ++++++++++++++++ tests/acceptance/boot_linux_console.py | 15 +++++++++++++++ 2 files changed, 31 insertions(+)
Signature · 1 line
2.21.3
PATCH 1/2 tests/acceptance/boot_linux: Truncate SD card image to power of 2
In the next commit we won't allow SD card images with invalid size (not aligned to a power of 2). Prepare the tests: add the pow2ceil() and image_pow2ceil_truncate() methods and truncate the images of the tests using SD cards.
--- tests/acceptance/boot_linux_console.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
tests/acceptance/boot_linux_console.py +15 -0
@@ -28,6 +28,18 @@ except CmdNotFoundError: P7ZIP_AVAILABLE = False +# round up to next power of 2 +def pow2ceil(x): + return 1 if x == 0 else 2**(x - 1).bit_length() + +# truncate file size to next power of 2 +def image_pow2ceil_truncate(path): + size = os.path.getsize(path) + size_aligned = pow2ceil(size) + if size != size_aligned: + with open(path, 'ab+') as fd: + fd.truncate(size_aligned) + class LinuxKernelTest(Test): KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
@@ -635,6 +647,7 @@ def test_arm_orangepi_sd(self): rootfs_path_xz = self.fetch_asset(rootfs_url, asset_hash=rootfs_hash) rootfs_path = os.path.join(self.workdir, 'rootfs.cpio') archive.lzma_uncompress(rootfs_path_xz, rootfs_path) + image_pow2ceil_truncate(rootfs_path) self.vm.set_console() kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
@@ -679,6 +692,7 @@ def test_arm_orangepi_bionic(self): image_name = 'Armbian_19.11.3_Orangepipc_bionic_current_5.3.9.img' image_path = os.path.join(self.workdir, image_name) process.run("7z e -o%s %s" % (self.workdir, image_path_7z)) + image_pow2ceil_truncate(image_path) self.vm.set_console() self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',
@@ -728,6 +742,7 @@ def test_arm_orangepi_uboot_netbsd9(self): image_hash = '2babb29d36d8360adcb39c09e31060945259917a' image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash) image_path = os.path.join(self.workdir, 'armv7.img') + image_pow2ceil_truncate(image_path) image_drive_args = 'if=sd,format=raw,snapshot=on,file=' + image_path archive.gzip_uncompress(image_path_gz, image_path)
Signature · 1 line
2.21.3
Re: [PATCH 1/2] tests/acceptance/boot_linux: Truncate SD card image to power of 2
Quoted 20 lines · Philippe Mathieu-Daudé
In the next commit we won't allow SD card images with invalid size (not aligned to a power of 2). Prepare the tests: add the
pow2ceil()andimage_pow2ceil_truncate()methods and truncate the images of the tests using SD cards.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- tests/acceptance/boot_linux_console.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+)tests/acceptance/boot_linux_console.py +1
-0@@ -28,6 +28,18 @@ except CmdNotFoundError: P7ZIP_AVAILABLE = False +# round up to next power of 2
tests/acceptance/boot_linux_console.py line 41
Quoted 10 lines
tests/acceptance/boot_linux_console.py +10
-0+def pow2ceil(x): + return 1 if x == 0 else 2**(x - 1).bit_length() + +# truncate file size to next power of 2 +def image_pow2ceil_truncate(path): + size = os.path.getsize(path) + size_aligned = pow2ceil(size) + if size != size_aligned: + with open(path, 'ab+') as fd: + fd.truncate(size_aligned)
Why truncate the image, can't we expand it instead?
Alistair
Quoted 32 lines
tests/acceptance/boot_linux_console.py +4
-1+ class LinuxKernelTest(Test): KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '@@ -635,6 +647,7 @@ def test_arm_orangepi_sd(self): rootfs_path_xz = self.fetch_asset(rootfs_url, asset_hash=rootfs_hash) rootfs_path = os.path.join(self.workdir, 'rootfs.cpio') archive.lzma_uncompress(rootfs_path_xz, rootfs_path) + image_pow2ceil_truncate(rootfs_path) self.vm.set_console() kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +@@ -679,6 +692,7 @@ def test_arm_orangepi_bionic(self): image_name = 'Armbian_19.11.3_Orangepipc_bionic_current_5.3.9.img' image_path = os.path.join(self.workdir, image_name) process.run("7z e -o%s %s" % (self.workdir, image_path_7z)) + image_pow2ceil_truncate(image_path) self.vm.set_console() self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',@@ -728,6 +742,7 @@ def test_arm_orangepi_uboot_netbsd9(self): image_hash = '2babb29d36d8360adcb39c09e31060945259917a' image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash) image_path = os.path.join(self.workdir, 'armv7.img') + image_pow2ceil_truncate(image_path) image_drive_args = 'if=sd,format=raw,snapshot=on,file=' + image_path archive.gzip_uncompress(image_path_gz, image_path) --2.21.3
Re: [PATCH 1/2] tests/acceptance/boot_linux: Truncate SD card image to power of 2
Quoted 20 lines · Alistair Francis
Quoted 19 lines · Philippe Mathieu-Daudé
In the next commit we won't allow SD card images with invalid size (not aligned to a power of 2). Prepare the tests: add the
pow2ceil()andimage_pow2ceil_truncate()methods and truncate the images of the tests using SD cards.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- tests/acceptance/boot_linux_console.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+)tests/acceptance/boot_linux_console.py +0
-0@@ -28,6 +28,18 @@ except CmdNotFoundError: P7ZIP_AVAILABLE = False
Quoted 13 lines
tests/acceptance/boot_linux_console.py line 41
Quoted 11 lines
tests/acceptance/boot_linux_console.py +11
-0+# round up to next power of 2 +def pow2ceil(x): + return 1 if x == 0 else 2**(x - 1).bit_length() + +# truncate file size to next power of 2 +def image_pow2ceil_truncate(path): + size = os.path.getsize(path) + size_aligned = pow2ceil(size) + if size != size_aligned: + with open(path, 'ab+') as fd: + fd.truncate(size_aligned)Why truncate the image, can't we expand it instead?
pow2ceil() round UP to the next power of 2. I think this Python truncate() is a simple wrapper around the ftruncate() syscall. IOW we only "expand the image". Note, these are test images copied in tmpdir and discarded after the tests ran. I'll improve the description.
Quoted 36 lines
Alistair
Quoted 32 lines
tests/acceptance/boot_linux_console.py +4
-1+ class LinuxKernelTest(Test): KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '@@ -635,6 +647,7 @@ def test_arm_orangepi_sd(self): rootfs_path_xz = self.fetch_asset(rootfs_url, asset_hash=rootfs_hash) rootfs_path = os.path.join(self.workdir, 'rootfs.cpio') archive.lzma_uncompress(rootfs_path_xz, rootfs_path) + image_pow2ceil_truncate(rootfs_path) self.vm.set_console() kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +@@ -679,6 +692,7 @@ def test_arm_orangepi_bionic(self): image_name = 'Armbian_19.11.3_Orangepipc_bionic_current_5.3.9.img' image_path = os.path.join(self.workdir, image_name) process.run("7z e -o%s %s" % (self.workdir, image_path_7z)) + image_pow2ceil_truncate(image_path) self.vm.set_console() self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',@@ -728,6 +742,7 @@ def test_arm_orangepi_uboot_netbsd9(self): image_hash = '2babb29d36d8360adcb39c09e31060945259917a' image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash) image_path = os.path.join(self.workdir, 'armv7.img') + image_pow2ceil_truncate(image_path) image_drive_args = 'if=sd,format=raw,snapshot=on,file=' + image_path archive.gzip_uncompress(image_path_gz, image_path) --2.21.3
Re: [PATCH 1/2] tests/acceptance/boot_linux: Truncate SD card image to power of 2
In reply to Philippe Mathieu-DaudéQuoted 61 lines · Philippe Mathieu-Daudé
In the next commit we won't allow SD card images with invalid size (not aligned to a power of 2). Prepare the tests: add the
pow2ceil()andimage_pow2ceil_truncate()methods and truncate the images of the tests using SD cards.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- tests/acceptance/boot_linux_console.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+)tests/acceptance/boot_linux_console.py +13
-0@@ -28,6 +28,18 @@ except CmdNotFoundError: P7ZIP_AVAILABLE = False +# round up to next power of 2 +def pow2ceil(x): + return 1 if x == 0 else 2**(x - 1).bit_length() + +# truncate file size to next power of 2 +def image_pow2ceil_truncate(path): + size = os.path.getsize(path) + size_aligned = pow2ceil(size) + if size != size_aligned: + with open(path, 'ab+') as fd: + fd.truncate(size_aligned) + class LinuxKernelTest(Test): KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '@@ -635,6 +647,7 @@ def test_arm_orangepi_sd(self): rootfs_path_xz = self.fetch_asset(rootfs_url, asset_hash=rootfs_hash) rootfs_path = os.path.join(self.workdir, 'rootfs.cpio') archive.lzma_uncompress(rootfs_path_xz, rootfs_path) + image_pow2ceil_truncate(rootfs_path) self.vm.set_console()
kernel_command_line= (self.KERNEL_COMMON_COMMAND_LINE +tests/acceptance/boot_linux_console.py +1
-0@@ -679,6 +692,7 @@ def test_arm_orangepi_bionic(self): image_name = 'Armbian_19.11.3_Orangepipc_bionic_current_5.3.9.img' image_path = os.path.join(self.workdir, image_name) process.run("7z e -o%s %s" % (self.workdir, image_path_7z)) + image_pow2ceil_truncate(image_path) self.vm.set_console() self.vm.add_args('-drive', 'file=' + image_path +',if=sd,format=raw',
tests/acceptance/boot_linux_console.py +1
-0@@ -728,6 +742,7 @@ def test_arm_orangepi_uboot_netbsd9(self): image_hash = '2babb29d36d8360adcb39c09e31060945259917a' image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash) image_path = os.path.join(self.workdir, 'armv7.img') + image_pow2ceil_truncate(image_path) image_drive_args = 'if=sd,format=raw,snapshot=on,file=' +
image_path
archive.gzip_uncompress(image_path_gz, image_path)
Quoted 4 lines
--
2.21.3
Hi Philippe,
This patch works OK for the linux part, but the NetBSD didn't work, it prints this error:
(5/5)
tests/acceptance/boot_linux_console.py:BootLinuxConsole.test_arm_orangepi_uboot_netbsd9:
ERROR: [Errno 2] No such file or directory:
'/var/tmp/avocado_6hoo815w/avocado_job_40aayif8/
5-tests_acceptance_boot_linux_console.py_BootLinuxConsole.test_arm_orangepi_uboot_netbsd9/armv7.img' (0.18 s)
Basically the truncate should just be moved after the uncompress to fix it. And the lines that we use before
to extend the image size can be removed now. That was needed to avoid conflict with the partition size inside image.
So with these small changes, I got it working fine:
(quoted diff) +0 -0
b/tests/acceptance/boot_linux_console.py
index f4d4e3635f..69607a5840 100644
tests/acceptance/boot_linux_console.py +0 -1
@@ -684,7 +684,7 @@ class BootLinuxConsole(LinuxKernelTest): :avocado: tags=machine:orangepi-pc """ - # This test download a 196MB compressed image and expand it to
932MB...
+ # This test download a 196MB compressed image and expand it to 1G
image_url = ('https://dl.armbian.com/orangepipc/archive/'
'Armbian_19.11.3_Orangepipc_bionic_current_5.3.9.7z')
image_hash = '196a8ffb72b0123d92cea4a070894813d305c71e'
@@ -725,7 +725,7 @@ class BootLinuxConsole(LinuxKernelTest):
:avocado: tags=arch:arm
:avocado: tags=machine:orangepi-pc
"""- # This test download a 304MB compressed image and expand it to
1.3GB...
+ # This test download a 304MB compressed image and expand it to
2GB...
deb_url = ('http://snapshot.debian.org/archive/debian/'
'20200108T145233Z/pool/main/u/u-boot/'
'u-boot-sunxi_2020.01%2Bdfsg-1_armhf.deb')@@ -742,9 +742,9 @@ class BootLinuxConsole(LinuxKernelTest):
image_hash = '2babb29d36d8360adcb39c09e31060945259917a' image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash) image_path = os.path.join(self.workdir, 'armv7.img')
- image_pow2ceil_truncate(image_path)
image_drive_args = 'if=sd,format=raw,snapshot=on,file=' +image_path
archive.gzip_uncompress(image_path_gz, image_path)
+ image_pow2ceil_truncate(image_path)
# dd if=u-boot-sunxi-with-spl.bin of=armv7.img bs=1K seek=8
conv=notrunc
with open(uboot_path, 'rb') as f_in:
@@ -754,9 +754,9 @@ class BootLinuxConsole(LinuxKernelTest):# Extend image, to avoid that NetBSD thinks the partition # inside the image is larger than device size itself
- f_out.seek(0, 2) - f_out.seek(64 * 1024 * 1024, 1) - f_out.write(bytearray([0x00]))
Signature · 1 line
Niek Linnenbank
PATCH 2/2 hw/sd/sdcard: Do not allow invalid SD card sizes
In reply to Philippe Mathieu-DaudéQEMU allows to create SD card with unrealistic sizes. This could work, but some guests (at least Linux) consider sizes that are not a power of 2 as a firmware bug and fix the card size to the next power of 2.
Before CVE-2020-13253 fix, this would allow OOB read/write accesses past the image size end.
CVE-2020-13253 has been fixed as:
Read command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR occurred and no data transfer is performed.
Write command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR occurred and no data transfer is performed.
WP_VIOLATION errors are not modified: the error bit is set, we stay in receive-data state, wait for a stop command. All further data transfer is ignored. See the check on sd->card_status at the beginning of sd_read_data() and sd_write_data().
While this is the correct behavior, in case QEMU create smaller SD cards, guests still try to access past the image size end, and QEMU considers this is an invalid address, thus "all further data transfer is ignored". This is wrong and make the guest looping until eventually timeouts.
Fix by not allowing invalid SD card sizes. Suggesting the expected size as a hint:
$ qemu-system-arm -M orangepi-pc -drive file=rootfs.ext2,if=sd,format=raw qemu-system-arm: Invalid SD card size: 60 MiB (expecting at least 64 MiB)
--- hw/sd/sd.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
hw/sd/sd.c +16 -0
@@ -32,6 +32,7 @@#include "qemu/osdep.h" #include "qemu/units.h" +#include "qemu/cutils.h" #include "hw/irq.h" #include "hw/registerfields.h" #include "sysemu/block-backend.h"
@@ -2130,11 +2131,26 @@ static void sd_realize(DeviceState *dev, Error **errp) }if (sd->blk) { +int64_t blk_size; +if (blk_is_read_only (sd->blk)) {error_setg (errp,"Cannot use read-only drive as SD card" );return ; } + blk_size =blk_getlength (sd->blk); +if (blk_size >0 && !is_power_of_2 (blk_size)) { +int64_t blk_size_aligned =pow2ceil (blk_size); +char *blk_size_str =size_to_str (blk_size); +char *blk_size_aligned_str =size_to_str (blk_size_aligned); + +error_setg (errp,"Invalid SD card size: %s (expecting at least %s)" , + blk_size_str, blk_size_aligned_str); +g_free (blk_size_str); +g_free (blk_size_aligned_str); +return ; + } + ret =blk_set_perm (sd->blk,BLK_PERM_CONSISTENT_READ |BLK_PERM_WRITE ,BLK_PERM_ALL , errp);if (ret <0 ) {
Signature · 1 line
2.21.3
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Quoted 59 lines · Philippe Mathieu-Daudé
QEMU allows to create SD card with unrealistic sizes. This could work, but some guests (at least Linux) consider sizes that are not a power of 2 as a firmware bug and fix the card size to the next power of 2.
Before CVE-2020-13253 fix, this would allow OOB read/write accesses past the image size end.
CVE-2020-13253 has been fixed as:
Read command is rejected if
BLOCK_LEN_ERRORorADDRESS_ERRORoccurred and no data transfer is performed.Write command is rejected if
BLOCK_LEN_ERRORorADDRESS_ERRORoccurred and no data transfer is performed.
WP_VIOLATIONerrors are not modified: the error bit is set, we stay in receive-data state, wait for a stop command. All further data transfer is ignored. See the check onsd->card_statusat the beginning ofsd_read_data()andsd_write_data().While this is the correct behavior, in case QEMU create smaller SD cards, guests still try to access past the image size end, and QEMU considers this is an invalid address, thus "all further data transfer is ignored". This is wrong and make the guest looping until eventually timeouts.
Fix by not allowing invalid SD card sizes. Suggesting the expected size as a hint:
$ qemu-system-arm -M orangepi-pc -drive file=rootfs.ext2,if=sd,format=raw qemu-system-arm: Invalid SD card size: 60 MiB (expecting at least 64 MiB)Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/sd/sd.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)hw/sd/sd.c +3
-0@@ -32,6 +32,7 @@#include "qemu/osdep.h" #include "qemu/units.h" +#include "qemu/cutils.h" #include "hw/irq.h" #include "hw/registerfields.h" #include "sysemu/block-backend.h" @@ -2130,11 +2131,26 @@ static void sd_realize(DeviceState *dev, Error **errp) }if (sd->blk) { +int64_t blk_size; +if (blk_is_read_only (sd->blk)) {error_setg (errp,"Cannot use read-only drive as SD card" );return ;
hw/sd/sd.c static void sd_realize(DeviceState *dev, Error **errp) · line 2148
Quoted 10 lines
hw/sd/sd.c +8
-0} + blk_size =blk_getlength (sd->blk); +if (blk_size >0 && !is_power_of_2 (blk_size)) { +int64_t blk_size_aligned =pow2ceil (blk_size); +char *blk_size_str =size_to_str (blk_size); +char *blk_size_aligned_str =size_to_str (blk_size_aligned); + +error_setg (errp,"Invalid SD card size: %s (expecting at least %s)" , + blk_size_str, blk_size_aligned_str);
Should we print that we expect a power of 2? This isn't always obvious from the message.
Alistair
Quoted 12 lines
hw/sd/sd.c +5
-1+g_free (blk_size_str); +g_free (blk_size_aligned_str); +return ; + } + ret =blk_set_perm (sd->blk,BLK_PERM_CONSISTENT_READ |BLK_PERM_WRITE ,BLK_PERM_ALL , errp);if (ret <0 ) { --2.21.3
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Quoted 63 lines · Alistair Francis
Quoted 61 lines · Philippe Mathieu-Daudé
QEMU allows to create SD card with unrealistic sizes. This could work, but some guests (at least Linux) consider sizes that are not a power of 2 as a firmware bug and fix the card size to the next power of 2.
Before CVE-2020-13253 fix, this would allow OOB read/write accesses past the image size end.
CVE-2020-13253 has been fixed as:
Read command is rejected if
BLOCK_LEN_ERRORorADDRESS_ERRORoccurred and no data transfer is performed.Write command is rejected if
BLOCK_LEN_ERRORorADDRESS_ERRORoccurred and no data transfer is performed.
WP_VIOLATIONerrors are not modified: the error bit is set, we stay in receive-data state, wait for a stop command. All further data transfer is ignored. See the check onsd->card_statusat the beginning ofsd_read_data()andsd_write_data().While this is the correct behavior, in case QEMU create smaller SD cards, guests still try to access past the image size end, and QEMU considers this is an invalid address, thus "all further data transfer is ignored". This is wrong and make the guest looping until eventually timeouts.
Fix by not allowing invalid SD card sizes. Suggesting the expected size as a hint:
$ qemu-system-arm -M orangepi-pc -drive file=rootfs.ext2,if=sd,format=raw qemu-system-arm: Invalid SD card size: 60 MiB (expecting at least 64 MiB)Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/sd/sd.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)hw/sd/sd.c +3
-0@@ -32,6 +32,7 @@#include "qemu/osdep.h" #include "qemu/units.h" +#include "qemu/cutils.h" #include "hw/irq.h" #include "hw/registerfields.h" #include "sysemu/block-backend.h" @@ -2130,11 +2131,26 @@ static void sd_realize(DeviceState *dev, Error **errp) }if (sd->blk) { +int64_t blk_size; +if (blk_is_read_only (sd->blk)) {error_setg (errp,"Cannot use read-only drive as SD card" );return ; }
Quoted 11 lines
hw/sd/sd.c static void sd_realize(DeviceState *dev, Error **errp) · line 2148
Quoted 8 lines
hw/sd/sd.c +8
-0+ blk_size =blk_getlength (sd->blk); +if (blk_size >0 && !is_power_of_2 (blk_size)) { +int64_t blk_size_aligned =pow2ceil (blk_size); +char *blk_size_str =size_to_str (blk_size); +char *blk_size_aligned_str =size_to_str (blk_size_aligned); + +error_setg (errp,"Invalid SD card size: %s (expecting at least %s)" , + blk_size_str, blk_size_aligned_str);Should we print that we expect a power of 2? This isn't always obvious from the message.
Mmm, I was thinking that. Perhaps
"expecting a power of 2, e.g. %s"
?
thanks
-- PMM
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Quoted 76 lines · Peter Maydell
Quoted 63 lines · Alistair Francis
Quoted 61 lines · Philippe Mathieu-Daudé
QEMU allows to create SD card with unrealistic sizes. This could work, but some guests (at least Linux) consider sizes that are not a power of 2 as a firmware bug and fix the card size to the next power of 2.
Before CVE-2020-13253 fix, this would allow OOB read/write accesses past the image size end.
CVE-2020-13253 has been fixed as:
Read command is rejected if
BLOCK_LEN_ERRORorADDRESS_ERRORoccurred and no data transfer is performed.Write command is rejected if
BLOCK_LEN_ERRORorADDRESS_ERRORoccurred and no data transfer is performed.
WP_VIOLATIONerrors are not modified: the error bit is set, we stay in receive-data state, wait for a stop command. All further data transfer is ignored. See the check onsd->card_statusat the beginning ofsd_read_data()andsd_write_data().While this is the correct behavior, in case QEMU create smaller SD cards, guests still try to access past the image size end, and QEMU considers this is an invalid address, thus "all further data transfer is ignored". This is wrong and make the guest looping until eventually timeouts.
Fix by not allowing invalid SD card sizes. Suggesting the expected size as a hint:
$ qemu-system-arm -M orangepi-pc -drive file=rootfs.ext2,if=sd,format=raw qemu-system-arm: Invalid SD card size: 60 MiB (expecting at least 64 MiB)Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/sd/sd.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)hw/sd/sd.c +3
-0@@ -32,6 +32,7 @@#include "qemu/osdep.h" #include "qemu/units.h" +#include "qemu/cutils.h" #include "hw/irq.h" #include "hw/registerfields.h" #include "sysemu/block-backend.h" @@ -2130,11 +2131,26 @@ static void sd_realize(DeviceState *dev, Error **errp) }if (sd->blk) { +int64_t blk_size; +if (blk_is_read_only (sd->blk)) {error_setg (errp,"Cannot use read-only drive as SD card" );return ; }Quoted 11 lines
hw/sd/sd.c static void sd_realize(DeviceState *dev, Error **errp) · line 2148
Quoted 8 lines
hw/sd/sd.c +8
-0+ blk_size =blk_getlength (sd->blk); +if (blk_size >0 && !is_power_of_2 (blk_size)) { +int64_t blk_size_aligned =pow2ceil (blk_size); +char *blk_size_str =size_to_str (blk_size); +char *blk_size_aligned_str =size_to_str (blk_size_aligned); + +error_setg (errp,"Invalid SD card size: %s (expecting at least %s)" , + blk_size_str, blk_size_aligned_str);Should we print that we expect a power of 2? This isn't always obvious from the message.
Quoted 3 lines
Mmm, I was thinking that. Perhaps
"expecting a power of 2, e.g. %s"
?
OK, thanks guys!
Quoted 4 lines
thanks
-- PMM
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Hi Philippe,
Just tried out your patch on latest master, and I noticed I couldn't apply it without getting this error:
$ git am ~/Downloads/patches/\[PATCH\ 2_2\]\ hw_sd_sdcard\:\ Do\ not\ allow\ invalid\ SD\ card\ sizes\ -\ Philippe\ Mathieu-Daudé\ \< [email protected]\>\ -\ 2020-07-07\ 1521.eml Applying: hw/sd/sdcard: Do not allow invalid SD card sizes error: patch failed: hw/sd/sd.c:2130 error: hw/sd/sd.c: patch does not apply Patch failed at 0001 hw/sd/sdcard: Do not allow invalid SD card sizes Use 'git am --show-current-patch' to see the failed patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".
The first patch did go OK. Maybe this one just needs to be rebased, or I made a mistake.
So I manually copy & pasted the change into hw/sd/sd.c to test it. It looks like the check works, but my concern is that with this change, we will be getting this error on 'off-the-shelf' images as well. For example, the latest Raspbian image size also isn't a power of two:
$ ./arm-softmmu/qemu-system-arm -M raspi2 -sd
~/Downloads/2020-05-27-raspios-buster-lite-armhf.img -nographic
WARNING: Image format was not specified for
'/home/me/Downloads/2020-05-27-raspios-buster-lite-armhf.img' and probing
guessed raw.
Automatically detecting the format is dangerous for raw images,
write operations on block 0 will be restricted.
Specify the 'raw' format explicitly to remove the restrictions.
qemu-system-arm: Invalid SD card size: 1.73 GiB (expecting at least 2 GiB)If we do decide that the change is needed, I would like to propose that we also give the user some instructions
on how to fix it, maybe some 'dd' command? In my opinion that should also go in some of the documentation file(s),
possibly also in the one for the OrangePi PC at docs/system/arm/orangepi.rst (I can also provide a patch for that if you wish).
Kind regards,
Niek
Quoted 95 lines · Philippe Mathieu-Daudé
Quoted 1 line · Peter Maydell
On Tue, 7 Jul 2020 at 17:04, Alistair Francis <[email protected]>
Quoted 2 lines · wrote:
Quoted 2 lines
On Tue, Jul 7, 2020 at 6:22 AM Philippe Mathieu-Daudé <[email protected]>
Quoted 27 lines · wrote:
Quoted 27 lines
Quoted 27 lines
QEMU allows to create SD card with unrealistic sizes. This could work, but some guests (at least Linux) consider sizes that are not a power of 2 as a firmware bug and fix the card size to the next power of 2.
Before CVE-2020-13253 fix, this would allow OOB read/write accesses past the image size end.
CVE-2020-13253 has been fixed as:
Read command is rejected if
BLOCK_LEN_ERRORorADDRESS_ERRORoccurred and no data transfer is performed.Write command is rejected if
BLOCK_LEN_ERRORorADDRESS_ERRORoccurred and no data transfer is performed.
WP_VIOLATIONerrors are not modified: the error bit is set, we stay in receive-data state, wait for a stop command. All further data transfer is ignored. See the check onsd->card_statusat the beginning ofsd_read_data()andsd_write_data().While this is the correct behavior, in case QEMU create smaller SD cards, guests still try to access past the image size end, and QEMU considers this is an invalid address, thus "all further data transfer is ignored". This is wrong and make the guest looping until eventually timeouts.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Fix by not allowing invalid SD card sizes. Suggesting the expected size as a hint:
$ qemu-system-arm -M orangepi-pc -drivefile=rootfs.ext2,if=sd,format=raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
qemu-system-arm: Invalid SD card size: 60 MiB (expecting at least 64
MiB)
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/sd/sd.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)hw/sd/sd.c +0
-0@@ -32,6 +32,7 @@Quoted 7 lines
Quoted 7 lines
hw/sd/sd.c static void sd_realize(DeviceState *dev, Error
Quoted 7 lines
hw/sd/sd.c +1
-0#include "qemu/osdep.h" #include "qemu/units.h" +#include "qemu/cutils.h" #include "hw/irq.h" #include "hw/registerfields.h" #include "sysemu/block-backend.h" @@ -2130,11 +2131,26 @@ static void sd_realize(DeviceState *dev, Error**errp)
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
hw/sd/sd.c +2
-0}if (sd->blk) { +int64_t blk_size; +if (blk_is_read_only (sd->blk)) {error_setg (errp,"Cannot use read-only drive as SD card" );return ; }Quoted 5 lines
Quoted 5 lines
hw/sd/sd.c static void sd_realize(DeviceState *dev, Error **errp)
Quoted 5 lines
hw/sd/sd.c +5
-0+ blk_size =blk_getlength (sd->blk); +if (blk_size >0 && !is_power_of_2 (blk_size)) { +int64_t blk_size_aligned =pow2ceil (blk_size); +char *blk_size_str =size_to_str (blk_size); + char *blk_size_aligned_str =
size_to_str(blk_size_aligned);Quoted 2 lines
Quoted 2 lines
hw/sd/sd.c static void sd_realize(DeviceState *dev, Error · line 2147
Quoted 2 lines
hw/sd/sd.c +2
-0+ + error_setg(errp, "Invalid SD card size: %s (expecting atleast %s)",
Quoted 8 lines
Quoted 4 lines
hw/sd/sd.c static void sd_realize(DeviceState *dev, Error **errp) · line 2148
Quoted 1 line
hw/sd/sd.c +1
-0+ blk_size_str, blk_size_aligned_str);Should we print that we expect a power of 2? This isn't always obvious from the message.
Mmm, I was thinking that. Perhaps
"expecting a power of 2, e.g. %s"
?OK, thanks guys!
Quoted 4 lines
thanks
-- PMM
Signature · 1 line
Niek Linnenbank
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Quoted 17 lines · Niek Linnenbank
Hi Philippe,
Just tried out your patch on latest master, and I noticed I couldn't apply it without getting this error:
$ git am ~/Downloads/patches/\[PATCH\ 2_2\]\ hw_sd_sdcard\:\ Do\ not\ allow\ invalid\ SD\ card\ sizes\ -\ Philippe\ Mathieu-Daudé\ \<[email protected] <mailto:[email protected]>\>\ -\ 2020-07-07\ 1521.eml Applying: hw/sd/sdcard: Do not allow invalid SD card sizes error: patch failed: hw/sd/sd.c:2130 error: hw/sd/sd.c: patch does not apply Patch failed at 0001 hw/sd/sdcard: Do not allow invalid SD card sizes Use 'git am --show-current-patch' to see the failed patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".
Quoted 2 lines
The first patch did go OK. Maybe this one just needs to be rebased, or I made a mistake.
Sorry it was not clear on the cover:
Part 1 is already reviewed:
https://www.mail-archive.com/[email protected]/msg718150.html
Based-on: <[email protected]>
This series is based on the "Part 1".
Quoted 15 lines
So I manually copy & pasted the change into
hw/sd/sd.cto test it. It looks like the check works, but my concern is that with this change, we will be getting this error on 'off-the-shelf' images as well. For example, the latest Raspbian image size also isn't a power of two:$ ./arm-softmmu/qemu-system-arm -M raspi2 -sd ~/Downloads/2020-05-27-raspios-buster-lite-armhf.img -nographic WARNING: Image format was not specified for '/home/me/Downloads/2020-05-27-raspios-buster-lite-armhf.img' and probing guessed raw. Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted. Specify the 'raw' format explicitly to remove the restrictions. qemu-system-arm: Invalid SD card size: 1.73 GiB (expecting at least 2 GiB)
Quoted 3 lines
If we do decide that the change is needed, I would like to propose that we also give the user some instructions
on how to fix it, maybe some 'dd' command?
On POSIX we can suggest to use 'truncate -s 2G' from coreutils. This is not in the default Darwin packages.
On Windows I have no clue.
Quoted 5 lines
In my opinion that should
also go in some of the documentation file(s),
possibly also in the one for the OrangePi PC atdocs/system/arm/orangepi.rst(I can also provide a patch for that if you wish).
Good idea, if you can send that patch that would a precious help, and I'd include it with the other patches :)
Note that this was your orangepi-pc acceptance test that catched this bug!
See https://travis-ci.org/github/philmd/qemu/jobs/705653532#L5672:
CPU: ARMv7 Processor [410fc075] revision 5 (ARMv7), cr=50c5387d
OF: fdt: Machine model: Xunlong Orange Pi PC
Kernel command line: printk.time=0 console=ttyS0,115200
root=/dev/mmcblk0 rootwait rw panic=-1 noreboot
sunxi-mmc 1c0f000.mmc: Linked as a consumer to regulator.2 sunxi-mmc 1c0f000.mmc: Got CD GPIO
sunxi-mmc 1c0f000.mmc: initialized, max. request size: 16384 KB
mmc0: host does not support reading read-only switch, assuming write-enable
mmc0: Problem switching card into high-speed mode!
mmc0: new SD card at address 4567
mmcblk0: mmc0:4567 QEMU! 60.0 MiB
EXT4-fs (mmcblk0): mounting ext2 file system using the ext4 subsystem EXT4-fs (mmcblk0): mounted filesystem without journal. Opts: (null)
VFS: Mounted root (ext2 filesystem) on device 179:0.
EXT4-fs (mmcblk0): re-mounted. Opts: block_validity,barrier,user_xattr,acl Populating /dev using udev: udevd[204]: starting version 3.2.7 udevadm settle failed
done
udevd[205]: worker [208]
/devices/platform/soc/1c0f000.mmc/mmc_host/mmc0/mmc0:4567/block/mmcblk0 is taking a long time
Runner error occurred: Timeout reached
Original status: ERROR
(I'll add that in the commit description too).
Thanks for your testing/review!
Quoted 114 lines
Kind regards,
Niek
On Tue, Jul 7, 2020 at 6:11 PM Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:
On 7/7/20 6:06 PM, Peter Maydell wrote: > On Tue, 7 Jul 2020 at 17:04, Alistair Francis <[email protected] <mailto:[email protected]>> wrote: >> >> On Tue, Jul 7, 2020 at 6:22 AM Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote: >>> >>> QEMU allows to create SD card with unrealistic sizes. This could work, >>> but some guests (at least Linux) consider sizes that are not a power >>> of 2 as a firmware bug and fix the card size to the next power of 2. >>> >>> Before CVE-2020-13253 fix, this would allow OOB read/write accesses >>> past the image size end. >>> >>> CVE-2020-13253 has been fixed as: >>> >>> Read command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR >>> occurred and no data transfer is performed. >>> >>> Write command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR >>> occurred and no data transfer is performed. >>> >>> WP_VIOLATION errors are not modified: the error bit is set, we >>> stay in receive-data state, wait for a stop command. All further >>> data transfer is ignored. See the check on sd->card_status at the >>> beginning of sd_read_data() and sd_write_data(). >>> >>> While this is the correct behavior, in case QEMU create smaller SD >>> cards, guests still try to access past the image size end, and QEMU >>> considers this is an invalid address, thus "all further data transfer >>> is ignored". This is wrong and make the guest looping until >>> eventually timeouts. >>> >>> Fix by not allowing invalid SD card sizes. Suggesting the expected >>> size as a hint: >>> >>> $ qemu-system-arm -M orangepi-pc -drive file=rootfs.ext2,if=sd,format=raw >>> qemu-system-arm: Invalid SD card size: 60 MiB (expecting at least 64 MiB) >>> >>> Signed-off-by: Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> >>> --- >>> hw/sd/sd.c | 16 ++++++++++++++++ >>> 1 file changed, 16 insertions(+) >>> >>> diff --git a/hw/sd/sd.c b/hw/sd/sd.c >>> index cb81487e5c..c45106b78e 100644 >>> --- a/hw/sd/sd.c >>> +++ b/hw/sd/sd.c >>> @@ -32,6 +32,7 @@ >>> >>> #include "qemu/osdep.h" >>> #include "qemu/units.h" >>> +#include "qemu/cutils.h" >>> #include "hw/irq.h" >>> #include "hw/registerfields.h" >>> #include "sysemu/block-backend.h" >>> @@ -2130,11 +2131,26 @@ static void sd_realize(DeviceState *dev, Error **errp) >>> } >>> >>> if (sd->blk) { >>> + int64_t blk_size; >>> + >>> if (blk_is_read_only(sd->blk)) { >>> error_setg(errp, "Cannot use read-only drive as SD card"); >>> return; >>> } >>> >>> + blk_size = blk_getlength(sd->blk); >>> + if (blk_size > 0 && !is_power_of_2(blk_size)) { >>> + int64_t blk_size_aligned = pow2ceil(blk_size); >>> + char *blk_size_str = size_to_str(blk_size); >>> + char *blk_size_aligned_str = size_to_str(blk_size_aligned); >>> + >>> + error_setg(errp, "Invalid SD card size: %s (expecting at least %s)", >>> + blk_size_str, blk_size_aligned_str); >> >> Should we print that we expect a power of 2? This isn't always obvious >> from the message. > > Mmm, I was thinking that. Perhaps > "expecting a power of 2, e.g. %s" > ? OK, thanks guys! > > thanks > -- PMM >Signature · 1 line
Niek Linnenbank
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Quoted 17 lines · Philippe Mathieu-Daudé
Quoted 15 lines · Niek Linnenbank
So I manually copy & pasted the change into
hw/sd/sd.cto test it. It looks like the check works, but my concern is that with this change, we will be getting this error on 'off-the-shelf' images as well. For example, the latest Raspbian image size also isn't a power of two:$ ./arm-softmmu/qemu-system-arm -M raspi2 -sd ~/Downloads/2020-05-27-raspios-buster-lite-armhf.img -nographic WARNING: Image format was not specified for '/home/me/Downloads/2020-05-27-raspios-buster-lite-armhf.img' and probing guessed raw. Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted. Specify the 'raw' format explicitly to remove the restrictions. qemu-system-arm: Invalid SD card size: 1.73 GiB (expecting at least 2 GiB)
Quoted 7 lines
Quoted 3 lines
If we do decide that the change is needed, I would like to propose that we also give the user some instructions
on how to fix it, maybe some 'dd' command?On POSIX we can suggest to use 'truncate -s 2G' from coreutils. This is not in the default Darwin packages.
On Windows I have no clue.
dd/truncate etc won't work if the image file is not raw (eg if it's qcow2). The only chance you have of something that's actually generic would probably involve "qemu-img resize". But I'm a bit wary of having an error message that recommends that, because what if we got it wrong?
thanks
-- PMM
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Quoted 22 lines · Peter Maydell
Quoted 21 lines · Philippe Mathieu-Daudé
Quoted 18 lines · Niek Linnenbank
So I manually copy & pasted the change into
hw/sd/sd.cto test it. It looks like the check works, but my concern is that with this change, we will be getting this error on 'off-the-shelf' images as well. For example, the latest Raspbian image size also isn't a power of two:$ ./arm-softmmu/qemu-system-arm -M raspi2 -sd ~/Downloads/2020-05-27-raspios-buster-lite-armhf.img -nographic WARNING: Image format was not specified for '/home/me/Downloads/2020-05-27-raspios-buster-lite-armhf.img' and probing guessed raw. Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted. Specify the 'raw' format explicitly to remove the restrictions. qemu-system-arm: Invalid SD card size: 1.73 GiB (expecting at least 2 GiB)If we do decide that the change is needed, I would like to propose that we also give the user some instructions
on how to fix it, maybe some 'dd' command?
Quoted 6 lines
Quoted 3 lines
On POSIX we can suggest to use 'truncate -s 2G' from coreutils. This is not in the default Darwin packages.
On Windows I have no clue.dd/truncate etc won't work if the image file is not raw (eg if it's qcow2).
Good catch...
Quoted 4 lines
The only chance you have of something that's actually generic would probably involve "qemu-img resize". But I'm a bit wary of having an error message that recommends that, because what if we got it wrong?
I am not sure what to recommend then.
Would that work as hint?
qemu-system-arm -M raspi2 -sd ./buster-lite-armhf.img
qemu-system-arm: Invalid SD card size: 1.73 GiB SD card size has to be a power of 2, e.g. 2GiB.
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Quoted 38 lines · Philippe Mathieu-Daudé
Quoted 22 lines · Peter Maydell
Quoted 21 lines · Philippe Mathieu-Daudé
Quoted 18 lines · Niek Linnenbank
So I manually copy & pasted the change into
hw/sd/sd.cto test it. It looks like the check works, but my concern is that with this change, we will be getting this error on 'off-the-shelf' images as well. For example, the latest Raspbian image size also isn't a power of two:$ ./arm-softmmu/qemu-system-arm -M raspi2 -sd ~/Downloads/2020-05-27-raspios-buster-lite-armhf.img -nographic WARNING: Image format was not specified for '/home/me/Downloads/2020-05-27-raspios-buster-lite-armhf.img' and probing guessed raw. Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted. Specify the 'raw' format explicitly to remove the restrictions. qemu-system-arm: Invalid SD card size: 1.73 GiB (expecting at least 2 GiB)If we do decide that the change is needed, I would like to propose that we also give the user some instructions
on how to fix it, maybe some 'dd' command?Quoted 6 lines
Quoted 3 lines
On POSIX we can suggest to use 'truncate -s 2G' from coreutils. This is not in the default Darwin packages.
On Windows I have no clue.dd/truncate etc won't work if the image file is not raw (eg if it's qcow2).
Good catch...
Quoted 4 lines
The only chance you have of something that's actually generic would probably involve "qemu-img resize". But I'm a bit wary of having an error message that recommends that, because what if we got it wrong?
Quoted 7 lines
I am not sure what to recommend then.
Would that work as hint?
qemu-system-arm -M raspi2 -sd ./buster-lite-armhf.img
qemu-system-arm: Invalid SD card size: 1.73 GiB SD card size has to be a power of 2, e.g. 2GiB.
That sounds good to me. That's enough for a user to figure out the next step.
If you want you could also add: "qemu-img might be able to help." or something like that.
Alistair
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Quoted 13 lines · Alistair Francis
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Peter Maydell
The only chance you have of something that's actually generic would probably involve "qemu-img resize". But I'm a bit wary of having an error message that recommends that, because what if we got it wrong?
I am not sure what to recommend then.
Would that work as hint?
Quoted 8 lines
Quoted 3 lines
qemu-system-arm -M raspi2 -sd ./buster-lite-armhf.img
qemu-system-arm: Invalid SD card size: 1.73 GiB SD card size has to be a power of 2, e.g. 2GiB.That sounds good to me. That's enough for a user to figure out the next step.
If you want you could also add: "qemu-img might be able to help." or something like that.
Thinking about it a bit and talking to Philippe on IRC, I think we can usefully have the message recommend "qemu-img resize" to the user; I think we should avoid printing out an exact-cut-n-paste command line just in case it's wrong, but something like:
qemu-system-arm: Invalid SD card size: 1.73 GiB
SD card size has to be a power of 2, e.g. 2GiB.
You can resize disk images with 'qemu-img resize <imagefile> <new-size>' (note that this will lose data if you make the image smaller than it
currently is)
I think is a reasonable balance between pointing the user in the right direction and cautioning them to check what they're doing before they blithely perform the resize...
thanks
-- PMM
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
In reply to Peter MaydellQuoted 30 lines · Peter Maydell
Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Niek Linnenbank
So I manually copy & pasted the change into
hw/sd/sd.cto test it. It looks like the check works, but my concern is that with this change, we will be getting this error on 'off-the-shelf' images as well. For example, the latest Raspbian image size also isn't a power of two:Quoted 6 lines
Quoted 6 lines
$ ./arm-softmmu/qemu-system-arm -M raspi2 -sd ~/Downloads/2020-05-27-raspios-buster-lite-armhf.img -nographic WARNING: Image format was not specified for '/home/me/Downloads/2020-05-27-raspios-buster-lite-armhf.img' and probing guessed raw. Automatically detecting the format is dangerous for rawimages,
Quoted 2 lines
Quoted 2 lines
write operations on block 0 will be restricted.
Specify the 'raw' format explicitly to remove therestrictions.
Quoted 1 line
Quoted 1 line
qemu-system-arm: Invalid SD card size: 1.73 GiB (expecting at least 2
GiB)
Quoted 8 lines
Quoted 4 lines
If we do decide that the change is needed, I would like to propose that we also give the user some instructions
on how to fix it, maybe some 'dd' command?On POSIX we can suggest to use 'truncate -s 2G' from coreutils. This is not in the default Darwin packages.
On Windows I have no clue.
Quoted 6 lines
dd/truncate etc won't work if the image file is not raw (eg if it's qcow2). The only chance you have of something that's actually generic would probably involve "qemu-img resize". But I'm a bit wary of having an error message that recommends that, because what if we got it wrong?
Yeah good point Peter, I see what you mean. As I wrote to Philippe, i'll try to make a small patch with some instructions in the OrangePi board documentation,
so then we'll at least have something there to help the user.
Regards,
Niek
Quoted 4 lines
thanks
-- PMM
Signature · 1 line
Niek Linnenbank
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
In reply to Peter MaydellAm 09.07.2020 um 16:15 hat Peter Maydell geschrieben:
Quoted 26 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Niek Linnenbank
Quoted 15 lines
So I manually copy & pasted the change into
hw/sd/sd.cto test it. It looks like the check works, but my concern is that with this change, we will be getting this error on 'off-the-shelf' images as well. For example, the latest Raspbian image size also isn't a power of two:$ ./arm-softmmu/qemu-system-arm -M raspi2 -sd ~/Downloads/2020-05-27-raspios-buster-lite-armhf.img -nographic WARNING: Image format was not specified for '/home/me/Downloads/2020-05-27-raspios-buster-lite-armhf.img' and probing guessed raw. Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted. Specify the 'raw' format explicitly to remove the restrictions. qemu-system-arm: Invalid SD card size: 1.73 GiB (expecting at least 2 GiB)Quoted 7 lines
Quoted 3 lines
If we do decide that the change is needed, I would like to propose that we also give the user some instructions
on how to fix it, maybe some 'dd' command?On POSIX we can suggest to use 'truncate -s 2G' from coreutils. This is not in the default Darwin packages.
On Windows I have no clue.
Quoted 5 lines
dd/truncate etc won't work if the image file is not raw (eg if it's qcow2). The only chance you have of something that's actually generic would probably involve "qemu-img resize". But I'm a bit wary of having an error message that recommends that, because what if we got it wrong?
What is your concern that we might get wrong? The suggestion is always extending the size rather than shrinking, so it should be harmless and easy to undo. (Hm, we should finally make --shrink mandatory for shrinking. We've printed a deprecation warning for almost three years.)
Kevin
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Quoted 8 lines · Kevin Wolf
Am 09.07.2020 um 16:15 hat Peter Maydell geschrieben:
Quoted 5 lines
dd/truncate etc won't work if the image file is not raw (eg if it's qcow2). The only chance you have of something that's actually generic would probably involve "qemu-img resize". But I'm a bit wary of having an error message that recommends that, because what if we got it wrong?
Quoted 4 lines
What is your concern that we might get wrong? The suggestion is always extending the size rather than shrinking, so it should be harmless and easy to undo. (Hm, we should finally make --shrink mandatory for shrinking. We've printed a deprecation warning for almost three years.)
If there's a qemu-img command line that will always only extend the image size and never let the user accidentally shrink it and throw away data, then great. I'd happily recommend that.
thanks
-- PMM
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Am 10.07.2020 um 11:59 hat Peter Maydell geschrieben:
Quoted 14 lines · Kevin Wolf
Quoted 8 lines
Am 09.07.2020 um 16:15 hat Peter Maydell geschrieben:
Quoted 5 lines
dd/truncate etc won't work if the image file is not raw (eg if it's qcow2). The only chance you have of something that's actually generic would probably involve "qemu-img resize". But I'm a bit wary of having an error message that recommends that, because what if we got it wrong?
Quoted 4 lines
What is your concern that we might get wrong? The suggestion is always extending the size rather than shrinking, so it should be harmless and easy to undo. (Hm, we should finally make --shrink mandatory for shrinking. We've printed a deprecation warning for almost three years.)
Quoted 4 lines
If there's a qemu-img command line that will always only extend the image size and never let the user accidentally shrink it and throw away data, then great. I'd happily recommend that.
I think removing deprecated behaviour is a change that we can still make in the early freeze. So if you agree, I'll send a patch that makes shrinking an image without --shrink a hard error in 5.1.
Kevin
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
Quoted 16 lines · Kevin Wolf
Am 10.07.2020 um 11:59 hat Peter Maydell geschrieben:
Quoted 14 lines · Kevin Wolf
Quoted 12 lines
Am 09.07.2020 um 16:15 hat Peter Maydell geschrieben:
Quoted 5 lines
dd/truncate etc won't work if the image file is not raw (eg if it's qcow2). The only chance you have of something that's actually generic would probably involve "qemu-img resize". But I'm a bit wary of having an error message that recommends that, because what if we got it wrong?
What is your concern that we might get wrong? The suggestion is always extending the size rather than shrinking, so it should be harmless and easy to undo. (Hm, we should finally make --shrink mandatory for shrinking. We've printed a deprecation warning for almost three years.)
Quoted 8 lines
Quoted 4 lines
If there's a qemu-img command line that will always only extend the image size and never let the user accidentally shrink it and throw away data, then great. I'd happily recommend that.
I think removing deprecated behaviour is a change that we can still make in the early freeze. So if you agree, I'll send a patch that makes shrinking an image without --shrink a hard error in 5.1.
Happy to defer to your judgement on that; I agree that removal of deprecated behaviour is ok at this point in freeze.
-- PMM
Re: [PATCH 2/2] hw/sd/sdcard: Do not allow invalid SD card sizes
In reply to Philippe Mathieu-DaudéQuoted 53 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Niek Linnenbank
Hi Philippe,
Just tried out your patch on latest master, and I noticed I couldn't apply it without getting this error:
$ git am ~/Downloads/patches/\[PATCH\ 2_2\]\ hw_sd_sdcard\:\ Do\ not\ allow\ invalid\ SD\ card\ sizes\ -\ Philippe\ Mathieu-Daudé\ \<[email protected] <mailto:[email protected]>\>\ -\ 2020-07-07\ 1521.eml Applying: hw/sd/sdcard: Do not allow invalid SD card sizes error: patch failed: hw/sd/sd.c:2130 error: hw/sd/sd.c: patch does not apply Patch failed at 0001 hw/sd/sdcard: Do not allow invalid SD card sizes Use 'git am --show-current-patch' to see the failed patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".Quoted 2 lines
The first patch did go OK. Maybe this one just needs to be rebased, or I made a mistake.
Sorry it was not clear on the cover:
Part 1 is already reviewed:
https://www.mail-archive.com/[email protected]/msg718150.html
Based-on: <[email protected]>This series is based on the "Part 1".
Quoted 5 lines
So I manually copy & pasted the change into
hw/sd/sd.cto test it. It looks like the check works, but my concern is that with this change, we will be getting this error on 'off-the-shelf' images as well. For example, the latest Raspbian image size also isn't a power of two:Quoted 9 lines
$ ./arm-softmmu/qemu-system-arm -M raspi2 -sd ~/Downloads/2020-05-27-raspios-buster-lite-armhf.img -nographic WARNING: Image format was not specified for '/home/me/Downloads/2020-05-27-raspios-buster-lite-armhf.img' and probing guessed raw. Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted. Specify the 'raw' format explicitly to remove the restrictions. qemu-system-arm: Invalid SD card size: 1.73 GiB (expecting at least 2GiB)
Quoted 4 lines
If we do decide that the change is needed, I would like to propose that we also give the user some instructions
on how to fix it, maybe some 'dd' command?On POSIX we can suggest to use 'truncate -s 2G' from coreutils. This is not in the default Darwin packages.
On Windows I have no clue.
Quoted 9 lines
Quoted 5 lines
In my opinion that should
also go in some of the documentation file(s),
possibly also in the one for the OrangePi PC atdocs/system/arm/orangepi.rst(I can also provide a patch for that if you wish).Good idea, if you can send that patch that would a precious help, and I'd include it with the other patches :)
OK Philipe. Then I'll prepare a patch and try send it to the list somewhere this weekend.
Quoted 6 lines
Note that this was your orangepi-pc acceptance test that catched this bug!
See https://travis-ci.org/github/philmd/qemu/jobs/705653532#L5672:
Oh cool, that is great. Looks like it is working pretty well then. But lets be fair, I think it was you that contributed that part ;-)
Quoted 25 lines
CPU: ARMv7 Processor [
410fc075] revision 5 (ARMv7), cr=50c5387d
OF: fdt: Machine model: Xunlong Orange Pi PC
Kernel command line: printk.time=0 console=ttyS0,115200
root=/dev/mmcblk0 rootwait rw panic=-1 noreboot
sunxi-mmc 1c0f000.mmc: Linked as a consumer to regulator.2 sunxi-mmc 1c0f000.mmc: Got CD GPIO
sunxi-mmc 1c0f000.mmc: initialized, max. request size: 16384 KB
mmc0: host does not support reading read-only switch, assuming
write-enable
mmc0: Problem switching card into high-speed mode!
mmc0: new SD card at address 4567
mmcblk0: mmc0:4567 QEMU! 60.0 MiB
EXT4-fs (mmcblk0): mounting ext2 file system using the ext4 subsystem EXT4-fs (mmcblk0): mounted filesystem without journal. Opts: (null)
VFS: Mounted root (ext2 filesystem) on device 179:0.
EXT4-fs (mmcblk0): re-mounted. Opts: block_validity,barrier,user_xattr,acl Populating /dev using udev: udevd[204]: starting version 3.2.7 udevadm settle failed
done
udevd[205]: worker [208]
/devices/platform/soc/1c0f000.mmc/mmc_host/mmc0/mmc0:4567/block/mmcblk0 is taking a long time
Runner error occurred: Timeout reached
Original status: ERROR
Quoted 2 lines
(I'll add that in the commit description too).
OK thanks!
Quoted 126 lines
Thanks for your testing/review!
Quoted 12 lines
Kind regards,
Niek
On Tue, Jul 7, 2020 at 6:11 PM Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:
On 7/7/20 6:06 PM, Peter Maydell wrote:
> On Tue, 7 Jul 2020 at 17:04, Alistair Francis <[email protected] <mailto:[email protected]>> wrote:
>>Quoted 6 lines
>> On Tue, Jul 7, 2020 at 6:22 AM Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:
>>>
>>> QEMU allows to create SD card with unrealistic sizes. This could work,
>>> but some guests (at least Linux) consider sizes that are not apower
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
of 2 as a firmware bug and fix the card size to the next power
of 2.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Before CVE-2020-13253 fix, this would allow OOB read/write
accesses
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
past the image size end.
CVE-2020-13253 has been fixed as:
Read command is rejected if
BLOCK_LEN_ERRORorADDRESS_ERRORoccurred and no data transfer is performed.Write command is rejected if
BLOCK_LEN_ERRORorADDRESS_ERRORoccurred and no data transfer is performed.
WP_VIOLATIONerrors are not modified: the error bit is set,we
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
stay in receive-data state, wait for a stop command. All
further
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
data transfer is ignored. See the check on sd->card_statusat the
>>> beginning ofsd_read_data()andsd_write_data().
>>>
>>> While this is the correct behavior, in case QEMU create smallerSD
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
cards, guests still try to access past the image size end, and
QEMU
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
considers this is an invalid address, thus "all further data
transfer
>>> is ignored". This is wrong and make the guest looping until >>> eventually timeouts.
>>>
>>> Fix by not allowing invalid SD card sizes. Suggesting theexpected
Quoted 69 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
size as a hint:
$ qemu-system-arm -M orangepi-pc -drivefile=rootfs.ext2,if=sd,format=raw >>> qemu-system-arm: Invalid SD card size: 60 MiB (expecting at least 64 MiB) >>> >>> Signed-off-by: Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> >>> --- >>> hw/sd/sd.c | 16 ++++++++++++++++ >>> 1 file changed, 16 insertions(+) >>> >>> diff --git a/hw/sd/sd.c b/hw/sd/sd.c >>> index cb81487e5c..c45106b78e 100644 >>> --- a/hw/sd/sd.c >>> +++ b/hw/sd/sd.c >>> @@ -32,6 +32,7 @@ >>> >>> #include "qemu/osdep.h" >>> #include "qemu/units.h" >>> +#include "qemu/cutils.h" >>> #include "hw/irq.h" >>> #include "hw/registerfields.h" >>> #include "sysemu/block-backend.h" >>> @@ -2130,11 +2131,26 @@ static void sd_realize(DeviceState *dev, Error **errp) >>> } >>> >>> if (sd->blk) { >>> + int64_t blk_size; >>> + >>> if (blk_is_read_only(sd->blk)) { >>> error_setg(errp, "Cannot use read-only drive as SD card"); >>> return; >>> } >>> >>> + blk_size = blk_getlength(sd->blk); >>> + if (blk_size > 0 && !is_power_of_2(blk_size)) { >>> + int64_t blk_size_aligned = pow2ceil(blk_size); >>> + char *blk_size_str = size_to_str(blk_size); >>> + char *blk_size_aligned_str = size_to_str(blk_size_aligned); >>> + >>> + error_setg(errp, "Invalid SD card size: %s (expecting at least %s)", >>> + blk_size_str, blk_size_aligned_str); >> >> Should we print that we expect a power of 2? This isn't always obvious >> from the message. > > Mmm, I was thinking that. Perhaps > "expecting a power of 2, e.g. %s" > ? OK, thanks guys! > > thanks > -- PMM >--
Niek Linnenbank
Signature · 1 line
Niek Linnenbank