Skip to contentSkip to conversation

Make default invocation of block drivers safer (v2)

PATCH In review Anthony Liguori 3 participants 5 messages Jul 14, 2010 – Jul 15, 2010 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Make default invocation of block drivers safer (v2)

CVE-2008-2004 described a vulnerability in QEMU whereas a malicious user could trick the block probing code into accessing arbitrary files in a guest. To mitigate this, we added an explicit format parameter to -drive which disabling block probing.

Fast forward to today, and the vast majority of users do not use this parameter. libvirt does not use this by default nor does virt-manager.

Most users want block probing so we should try to make it safer.

This patch adds some logic to the raw device which attempts to detect a write operation to the beginning of a raw device. If the first 4 bytes happen to match an image file that has a backing file that we support, it scrubs the signature to all zeros. If a user specifies an explicit format parameter, this behavior is disabled.

I contend that while a legitimate guest could write such a signature to the header, we would behave incorrectly anyway upon the next invocation of QEMU. This simply changes the incorrect behavior to not involve a security vulnerability.

I've tested this pretty extensively both in the positive and negative case. I'm not 100% confident in the block layer's ability to deal with zero sized writes particularly with respect to the aio functions so some additional eyes would be appreciated.

Even in the case of a single sector write, we have to make sure to invoked the completion from a bottom half so just removing the zero sized write is not an option.

---
v1 -> v2
- be more paranoid about empty iovecs

---
 block.c     |    4 ++
 block/raw.c |  129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 block_int.h |    1 +
 3 files changed, 134 insertions(+), 0 deletions(-)
block.c +4 -0
index 65cf4dc..f837876 100644
@@ -511,6 +511,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
               BlockDriver *drv)
 {
     int ret;
+    int probed = 0;
 
     if (flags & BDRV_O_SNAPSHOT) {
         BlockDriverState *bs1;
@@ -571,6 +572,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
     /* Find the right image format driver */
     if (!drv) {
         drv = find_image_format(filename);
+        probed = 1;
     }
 
     if (!drv) {
@@ -584,6 +586,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
         goto unlink_and_fail;
     }
 
+    bs->probed = probed;
+
     /* If there is a backing file, use it */
     if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
         char backing_filename[PATH_MAX];
block/raw.c +129 -0

154 lines not on this page · raw message

block_int.h +1 -0
index 877e1e5..96ff4cf 100644
@@ -144,6 +144,7 @@ struct BlockDriverState {
     int encrypted; /* if true, the media is encrypted */
     int valid_key; /* if true, a valid encryption key has been set */
     int sg;        /* if true, the device is a /dev/sg* */
+    int probed;    /* if true, format was probed automatically */
     /* event callback when inserting/removing */
     void (*change_cb)(void *opaque);
     void *change_opaque;
Signature · 1 line
1.7.0.4
#1 lore raw

Am 14.07.2010 19:54, schrieb Anthony Liguori:

Quoted 48 lines

CVE-2008-2004 described a vulnerability in QEMU whereas a malicious user could trick the block probing code into accessing arbitrary files in a guest. To mitigate this, we added an explicit format parameter to -drive which disabling block probing.

Fast forward to today, and the vast majority of users do not use this parameter. libvirt does not use this by default nor does virt-manager.

Most users want block probing so we should try to make it safer.

This patch adds some logic to the raw device which attempts to detect a write operation to the beginning of a raw device. If the first 4 bytes happen to match an image file that has a backing file that we support, it scrubs the signature to all zeros. If a user specifies an explicit format parameter, this behavior is disabled.

I contend that while a legitimate guest could write such a signature to the header, we would behave incorrectly anyway upon the next invocation of QEMU. This simply changes the incorrect behavior to not involve a security vulnerability.

I've tested this pretty extensively both in the positive and negative case. I'm not 100% confident in the block layer's ability to deal with zero sized writes particularly with respect to the aio functions so some additional eyes would be appreciated.

Even in the case of a single sector write, we have to make sure to invoked the completion from a bottom half so just removing the zero sized write is not an option.

---
v1 -> v2
- be more paranoid about empty iovecs

---
 block.c     |    4 ++
 block/raw.c |  129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 block_int.h |    1 +
 3 files changed, 134 insertions(+), 0 deletions(-)

static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque)
{

block/raw.c +3 -0
+    const uint8_t *first_buf;
+    int first_buf_index = 0, i;
+

block/raw.c static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs, · line 135

Quoted 10 lines
block/raw.c +10 -0
+    /* This is probably being paranoid, but handle cases of zero size
+       vectors. */
+    for (i = 0; i < qiov->niov; i++) {
+        if (qiov->iov[i].iov_len) {
+            first_buf_index = i;
+            break;
+        }
+    }
+
+    first_buf = qiov->iov[first_buf_index].iov_base;

It's still not paranoid enough for the case where the magic is spread over multiple buffers. We should probably have a qemu_iovec_to_buffer() with limited size so that you can just get 4 bytes into a temporary buffer.

Kevin

#2 lore raw
Quoted 57 lines · Kevin Wolf

Am 14.07.2010 19:54, schrieb Anthony Liguori:

Quoted 55 lines

CVE-2008-2004 described a vulnerability in QEMU whereas a malicious user could trick the block probing code into accessing arbitrary files in a guest. To mitigate this, we added an explicit format parameter to -drive which disabling block probing.

Fast forward to today, and the vast majority of users do not use this parameter. libvirt does not use this by default nor does virt-manager.

Most users want block probing so we should try to make it safer.

This patch adds some logic to the raw device which attempts to detect a write operation to the beginning of a raw device. If the first 4 bytes happen to match an image file that has a backing file that we support, it scrubs the signature to all zeros. If a user specifies an explicit format parameter, this behavior is disabled.

I contend that while a legitimate guest could write such a signature to the header, we would behave incorrectly anyway upon the next invocation of QEMU. This simply changes the incorrect behavior to not involve a security vulnerability.

I've tested this pretty extensively both in the positive and negative case. I'm not 100% confident in the block layer's ability to deal with zero sized writes particularly with respect to the aio functions so some additional eyes would be appreciated.

Even in the case of a single sector write, we have to make sure to invoked the completion from a bottom half so just removing the zero sized write is not an option.

---
v1 -> v2
- be more paranoid about empty iovecs

---
  block.c     |    4 ++
  block/raw.c |  129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  block_int.h |    1 +
  3 files changed, 134 insertions(+), 0 deletions(-)

static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque)
{

block/raw.c +9 -0
+    const uint8_t *first_buf;
+    int first_buf_index = 0, i;
+
+    /* This is probably being paranoid, but handle cases of zero size
+       vectors. */
+    for (i = 0; i<  qiov->niov; i++) {
+        if (qiov->iov[i].iov_len) {
+            first_buf_index = i;
+            break;
Quoted 9 lines

block/raw.c static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs, · line 135

Quoted 5 lines
block/raw.c +4 -0
+        }
+    }
+
+    first_buf = qiov->iov[first_buf_index].iov_base;

It's still not paranoid enough for the case where the magic is spread over multiple buffers. We should probably have a qemu_iovec_to_buffer() with limited size so that you can just get 4 bytes into a temporary buffer.

I'm quite confident that iovec buffers are never less than 512 in size. While it could be more paranoid, I don't think the added complexity helps.

Regards,

Anthony Liguori

Quoted 2 lines

Kevin

#3 lore raw

Am 15.07.2010 14:28, schrieb Anthony Liguori:

Quoted 68 lines · Kevin Wolf
Quoted 57 lines

Am 14.07.2010 19:54, schrieb Anthony Liguori:

Quoted 55 lines

CVE-2008-2004 described a vulnerability in QEMU whereas a malicious user could trick the block probing code into accessing arbitrary files in a guest. To mitigate this, we added an explicit format parameter to -drive which disabling block probing.

Fast forward to today, and the vast majority of users do not use this parameter. libvirt does not use this by default nor does virt-manager.

Most users want block probing so we should try to make it safer.

This patch adds some logic to the raw device which attempts to detect a write operation to the beginning of a raw device. If the first 4 bytes happen to match an image file that has a backing file that we support, it scrubs the signature to all zeros. If a user specifies an explicit format parameter, this behavior is disabled.

I contend that while a legitimate guest could write such a signature to the header, we would behave incorrectly anyway upon the next invocation of QEMU. This simply changes the incorrect behavior to not involve a security vulnerability.

I've tested this pretty extensively both in the positive and negative case. I'm not 100% confident in the block layer's ability to deal with zero sized writes particularly with respect to the aio functions so some additional eyes would be appreciated.

Even in the case of a single sector write, we have to make sure to invoked the completion from a bottom half so just removing the zero sized write is not an option.

---
v1 -> v2
- be more paranoid about empty iovecs

---
  block.c     |    4 ++
  block/raw.c |  129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  block_int.h |    1 +
  3 files changed, 134 insertions(+), 0 deletions(-)

static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque)
{

block/raw.c +9 -0
+    const uint8_t *first_buf;
+    int first_buf_index = 0, i;
+
+    /* This is probably being paranoid, but handle cases of zero size
+       vectors. */
+    for (i = 0; i<  qiov->niov; i++) {
+        if (qiov->iov[i].iov_len) {
+            first_buf_index = i;
+            break;
Quoted 9 lines

block/raw.c static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs, · line 135

Quoted 5 lines
block/raw.c +4 -0
+        }
+    }
+
+    first_buf = qiov->iov[first_buf_index].iov_base;

It's still not paranoid enough for the case where the magic is spread over multiple buffers. We should probably have a qemu_iovec_to_buffer() with limited size so that you can just get 4 bytes into a temporary buffer.

Quoted 2 lines

I'm quite confident that iovec buffers are never less than 512 in size. While it could be more paranoid, I don't think the added complexity helps.

We rely on that anyway, we'd overflow iov_len otherwise. Maybe adding an assert there wouldn't hurt. But I'm fine either way.

Kevin

#4 lore raw
Quoted 75 lines · Kevin Wolf

Am 15.07.2010 14:28, schrieb Anthony Liguori:

Quoted 73 lines · Kevin Wolf
Quoted 72 lines

Am 14.07.2010 19:54, schrieb Anthony Liguori:

Quoted 53 lines

CVE-2008-2004 described a vulnerability in QEMU whereas a malicious user could trick the block probing code into accessing arbitrary files in a guest. To mitigate this, we added an explicit format parameter to -drive which disabling block probing.

Fast forward to today, and the vast majority of users do not use this parameter. libvirt does not use this by default nor does virt-manager.

Most users want block probing so we should try to make it safer.

This patch adds some logic to the raw device which attempts to detect a write operation to the beginning of a raw device. If the first 4 bytes happen to match an image file that has a backing file that we support, it scrubs the signature to all zeros. If a user specifies an explicit format parameter, this behavior is disabled.

I contend that while a legitimate guest could write such a signature to the header, we would behave incorrectly anyway upon the next invocation of QEMU. This simply changes the incorrect behavior to not involve a security vulnerability.

I've tested this pretty extensively both in the positive and negative case. I'm not 100% confident in the block layer's ability to deal with zero sized writes particularly with respect to the aio functions so some additional eyes would be appreciated.

Even in the case of a single sector write, we have to make sure to invoked the completion from a bottom half so just removing the zero sized write is not an option.

---
v1 -> v2
- be more paranoid about empty iovecs

---
   block.c     |    4 ++
   block/raw.c |  129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   block_int.h |    1 +
   3 files changed, 134 insertions(+), 0 deletions(-)

static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque)
{

block/raw.c +3 -0
+    const uint8_t *first_buf;
+    int first_buf_index = 0, i;
+

block/raw.c static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs, · line 135

Quoted 10 lines
block/raw.c +10 -0
+    /* This is probably being paranoid, but handle cases of zero size
+       vectors. */
+    for (i = 0; i<   qiov->niov; i++) {
+        if (qiov->iov[i].iov_len) {
+            first_buf_index = i;
+            break;
+        }
+    }
+
+    first_buf = qiov->iov[first_buf_index].iov_base;

It's still not paranoid enough for the case where the magic is spread over multiple buffers. We should probably have a qemu_iovec_to_buffer() with limited size so that you can just get 4 bytes into a temporary buffer.

Quoted 6 lines
Quoted 3 lines

I'm quite confident that iovec buffers are never less than 512 in size. While it could be more paranoid, I don't think the added complexity helps.

We rely on that anyway, we'd overflow iov_len otherwise. Maybe adding an assert there wouldn't hurt. But I'm fine either way.

An assert's a good idea.

Regards,

Anthony Liguori

Quoted 2 lines

Kevin