shithub: riscv

Download patch

ref: 09f2ec3bbc9aeb432f4f2be3056ac0dfa74039ac
parent: 081b58a69e5efc2bbf49ee31099372527edb3fa7
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Feb 7 17:33:37 EST 2024

disk/qcow2: fix crash with misaligned sectors

When looping over the pages in a misaligned sector,
we would null out the disk, rather than simply checking
whether the translation was done correctly.

diff: cannot open b/sys/src/cmd/disk/test//null: file does not exist: 'b/sys/src/cmd/disk/test//null'
--- a/sys/src/cmd/disk/qcowfs.c
+++ b/sys/src/cmd/disk/qcowfs.c
@@ -463,14 +463,15 @@
 	rem = len;
 	while(rem != 0){
 		phys_off = xlate(d, off, nil);
-		if(phys_off <= 0)
-			d = nil;
+		if(phys_off == -1){
+			responderror(r);
+			return;
+		}
 		cluster_off = off % disk->clustersz;
 		sz = disk->clustersz - cluster_off;
 		if(sz > rem)
 			sz = rem;
-
-		if(!d)
+		if(phys_off == 0)
 			memset(buf, 0, sz);
 		else
 			sz = pread(d->fd, buf, sz, phys_off);
--- /dev/null
+++ b/sys/src/cmd/disk/test/basic.rc
@@ -1,0 +1,28 @@
+#!/bin/rc
+
+# read fresh disk
+../$O.qcowfs -n 4294967296 /tmp/qcow
+dd -ibs 389120 -skip 1 -obs 8192 -count 1 < /mnt/qcow/data >/dev/null
+../$O.qcowfs -n 4294967296 /tmp/qcow
+dd -ibs 389120 -skip 1 -obs 15473 -count 1 < /mnt/qcow/data >/dev/null
+
+# write
+../$O.qcowfs -n 4294967296 /tmp/qcow
+dd -ibs 389120 -skip 1 -obs 8192 -count 1 -if /dev/random -of /mnt/qcow/data # start, power of two
+dd -ibs 389120 -skip 1 -obs 15473 -count 1 -if /dev/random -of /mnt/qcow/data # random prime
+dd -ibs 389120 -skip 1 -obs 8192 -seek 524287 -count 1 -if /dev/random -of /mnt/qcow/data  # end
+
+# read written disk
+dd -ibs 389120 -skip 1 -obs 8192 -count 1 < /mnt/qcow/data >/dev/null
+dd -ibs 389120 -skip 1 -obs 15473 -count 1 < /mnt/qcow/data >/dev/null
+
+# read and write some data
+cat /bin/cat > /mnt/qcow/data
+sz = `{walk -es /bin/cat}
+xhash=`{cat /bin/cat | sha1sum}
+rhash=`{read -c $sz /mnt/qcow/data | sha1sum}
+if(! ~ $xhash $rhash){
+	>[1=2] echo mismatched data
+	exit mishash
+}
+exit ''
--- /dev/null
+++ b/sys/src/cmd/disk/test/mkfile
@@ -1,0 +1,6 @@
+</$objtype/mkfile
+
+TEST=\
+	basic
+
+</sys/src/cmd/mktest