peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame qemu-0.9.1-block-rw-range-check.patch

Daniel P. Berrange b80713
diff --git a/block.c b/block.c
Daniel P. Berrange b80713
index 0f8ad7b..d7f1114 100644
Daniel P. Berrange b80713
--- a/block.c
Daniel P. Berrange b80713
+++ b/block.c
Daniel P. Berrange b80713
@@ -123,6 +123,24 @@ void path_combine(char *dest, int dest_size,
Daniel P. Berrange b80713
     }
Daniel P. Berrange b80713
 }
Daniel P. Berrange b80713
 
Daniel P. Berrange b80713
+static int bdrv_rw_badreq_sectors(BlockDriverState *bs,
Daniel P. Berrange b80713
+				int64_t sector_num, int nb_sectors)
Daniel P. Berrange b80713
+{
Daniel P. Berrange b80713
+    return
Daniel P. Berrange b80713
+	nb_sectors < 0 ||
Daniel P. Berrange b80713
+	nb_sectors > bs->total_sectors ||
Daniel P. Berrange b80713
+	sector_num > bs->total_sectors - nb_sectors;
Daniel P. Berrange b80713
+}
Daniel P. Berrange b80713
+
Daniel P. Berrange b80713
+static int bdrv_rw_badreq_bytes(BlockDriverState *bs,
Daniel P. Berrange b80713
+				  int64_t offset, int count)
Daniel P. Berrange b80713
+{
Daniel P. Berrange b80713
+    int64_t size = bs->total_sectors << SECTOR_BITS;
Daniel P. Berrange b80713
+    return
Daniel P. Berrange b80713
+	count < 0 ||
Daniel P. Berrange b80713
+	count > size ||
Daniel P. Berrange b80713
+	offset > size - count;
Daniel P. Berrange b80713
+}
Daniel P. Berrange b80713
 
Daniel P. Berrange b80713
 static void bdrv_register(BlockDriver *bdrv)
Daniel P. Berrange b80713
 {
Daniel P. Berrange b80713
@@ -375,6 +393,7 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
Daniel P. Berrange b80713
     }
Daniel P. Berrange b80713
     bs->drv = drv;
Daniel P. Berrange b80713
     bs->opaque = qemu_mallocz(drv->instance_size);
Daniel P. Berrange b80713
+    bs->total_sectors = 0; /* driver will set if it does not do getlength */
Daniel P. Berrange b80713
     if (bs->opaque == NULL && drv->instance_size > 0)
Daniel P. Berrange b80713
         return -1;
Daniel P. Berrange b80713
     /* Note: for compatibility, we open disk image files as RDWR, and
Daniel P. Berrange b80713
@@ -440,6 +459,7 @@ void bdrv_close(BlockDriverState *bs)
Daniel P. Berrange b80713
         bs->drv = NULL;
Daniel P. Berrange b80713
 
Daniel P. Berrange b80713
         /* call the change callback */
Daniel P. Berrange b80713
+	bs->total_sectors = 0;
Daniel P. Berrange b80713
         bs->media_changed = 1;
Daniel P. Berrange b80713
         if (bs->change_cb)
Daniel P. Berrange b80713
             bs->change_cb(bs->change_opaque);
Daniel P. Berrange b80713
@@ -505,6 +525,8 @@ int bdrv_read(BlockDriverState *bs, int64_t sector_num,
Daniel P. Berrange b80713
     if (!drv)
Daniel P. Berrange b80713
         return -ENOMEDIUM;
Daniel P. Berrange b80713
 
Daniel P. Berrange b80713
+    if (bdrv_rw_badreq_sectors(bs, sector_num, nb_sectors))
Daniel P. Berrange b80713
+	return -EDOM;
Daniel P. Berrange b80713
     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
Daniel P. Berrange b80713
             memcpy(buf, bs->boot_sector_data, 512);
Daniel P. Berrange b80713
         sector_num++;
Daniel P. Berrange b80713
@@ -545,6 +567,8 @@ int bdrv_write(BlockDriverState *bs, int64_t sector_num,
Daniel P. Berrange b80713
         return -ENOMEDIUM;
Daniel P. Berrange b80713
     if (bs->read_only)
Daniel P. Berrange b80713
         return -EACCES;
Daniel P. Berrange b80713
+    if (bdrv_rw_badreq_sectors(bs, sector_num, nb_sectors))
Daniel P. Berrange b80713
+	return -EDOM;
Daniel P. Berrange b80713
     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
Daniel P. Berrange b80713
         memcpy(bs->boot_sector_data, buf, 512);
Daniel P. Berrange b80713
     }
Daniel P. Berrange b80713
@@ -670,6 +694,8 @@ int bdrv_pread(BlockDriverState *bs, int64_t offset,
Daniel P. Berrange b80713
         return -ENOMEDIUM;
Daniel P. Berrange b80713
     if (!drv->bdrv_pread)
Daniel P. Berrange b80713
         return bdrv_pread_em(bs, offset, buf1, count1);
Daniel P. Berrange b80713
+    if (bdrv_rw_badreq_bytes(bs, offset, count1))
Daniel P. Berrange b80713
+	return -EDOM;
Daniel P. Berrange b80713
     return drv->bdrv_pread(bs, offset, buf1, count1);
Daniel P. Berrange b80713
 }
Daniel P. Berrange b80713
 
Daniel P. Berrange b80713
@@ -685,6 +711,8 @@ int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
Daniel P. Berrange b80713
         return -ENOMEDIUM;
Daniel P. Berrange b80713
     if (!drv->bdrv_pwrite)
Daniel P. Berrange b80713
         return bdrv_pwrite_em(bs, offset, buf1, count1);
Daniel P. Berrange b80713
+    if (bdrv_rw_badreq_bytes(bs, offset, count1))
Daniel P. Berrange b80713
+	return -EDOM;
Daniel P. Berrange b80713
     return drv->bdrv_pwrite(bs, offset, buf1, count1);
Daniel P. Berrange b80713
 }
Daniel P. Berrange b80713
 
Daniel P. Berrange b80713
@@ -951,6 +979,8 @@ int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
Daniel P. Berrange b80713
         return -ENOMEDIUM;
Daniel P. Berrange b80713
     if (!drv->bdrv_write_compressed)
Daniel P. Berrange b80713
         return -ENOTSUP;
Daniel P. Berrange b80713
+    if (bdrv_rw_badreq_sectors(bs, sector_num, nb_sectors))
Daniel P. Berrange b80713
+	return -EDOM;
Daniel P. Berrange b80713
     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
Daniel P. Berrange b80713
 }
Daniel P. Berrange b80713
 
Daniel P. Berrange b80713
@@ -1097,6 +1127,8 @@ BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
Daniel P. Berrange b80713
 
Daniel P. Berrange b80713
     if (!drv)
Daniel P. Berrange b80713
         return NULL;
Daniel P. Berrange b80713
+    if (bdrv_rw_badreq_sectors(bs, sector_num, nb_sectors))
Daniel P. Berrange b80713
+	return NULL;
Daniel P. Berrange b80713
 
Daniel P. Berrange b80713
     /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
Daniel P. Berrange b80713
     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
Daniel P. Berrange b80713
@@ -1128,6 +1160,8 @@ BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
Daniel P. Berrange b80713
         return NULL;
Daniel P. Berrange b80713
     if (bs->read_only)
Daniel P. Berrange b80713
         return NULL;
Daniel P. Berrange b80713
+    if (bdrv_rw_badreq_sectors(bs, sector_num, nb_sectors))
Daniel P. Berrange b80713
+	return NULL;
Daniel P. Berrange b80713
     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
Daniel P. Berrange b80713
         memcpy(bs->boot_sector_data, buf, 512);
Daniel P. Berrange b80713
     }