Skip to contentSkip to conversation

block: Collection of unrelated simple fixes

PATCH In review Kevin Wolf 5 participants 15 messages Oct 26, 2011 – Oct 27, 2011 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

cover block: Collection of unrelated simple fixes

This fixes some misc block layer bugs that Coverity pointed at.

Kevin Wolf (7):
block: Remove dead code
block: Fix bdrv_open use after free
qcow: Fix bdrv_write_compressed error handling
ide: Fix off-by-one error in array index check
vmdk: Fix use of uninitialised value
vmdk: Improve error handling
vmdk: Fix possible segfaults

 block.c       |    8 ++------
 block/qcow.c  |   30 +++++++++++++++++++-----------
 block/vmdk.c  |   30 ++++++++++++++++++++++--------
 hw/ide/core.c |    2 +-
 4 files changed, 44 insertions(+), 26 deletions(-)
Signature · 1 line
1.7.6.4
author
#1 lore raw

PATCH 1/7 block: Remove dead code

---
 block.c |    6 +-----
 1 files changed, 1 insertions(+), 5 deletions(-)
block.c +1 -5
index 0b7bc06..86d450c 100644
@@ -2039,11 +2039,7 @@ const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
 void bdrv_get_backing_filename(BlockDriverState *bs,
                                char *filename, int filename_size)
 {
-    if (!bs->backing_file) {
-        pstrcpy(filename, filename_size, "");
-    } else {
-        pstrcpy(filename, filename_size, bs->backing_file);
-    }
+    pstrcpy(filename, filename_size, bs->backing_file);
 }
 
 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
Signature · 1 line
1.7.6.4
author
#5 lore raw

PATCH 2/7 block: Fix bdrv_open use after free

In reply to Kevin Wolf

tmp_filename was used outside the block it was defined in, i.e. after it went out of scope. Move its declaration to the top level.

---
 block.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block.c +1 -1
index 86d450c..9dbb572 100644
@@ -571,6 +571,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
               BlockDriver *drv)
 {
     int ret;
+    char tmp_filename[PATH_MAX];
 
     if (flags & BDRV_O_SNAPSHOT) {
         BlockDriverState *bs1;
@@ -578,7 +579,6 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
         int is_protocol = 0;
         BlockDriver *bdrv_qcow2;
         QEMUOptionParameter *options;
-        char tmp_filename[PATH_MAX];
         char backing_filename[PATH_MAX];
 
         /* if snapshot, we create a temporary backing file and open it
Signature · 1 line
1.7.6.4
author
#8 lore raw

PATCH 4/7 ide: Fix off-by-one error in array index check

In reply to Kevin Wolf
---
 hw/ide/core.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/ide/core.c +1 -1
index 280a117..29305d3 100644
@@ -2039,7 +2039,7 @@ static int ide_drive_pio_post_load(void *opaque, int version_id)
 {
     IDEState *s = opaque;
 
-    if (s->end_transfer_fn_idx > ARRAY_SIZE(transfer_end_table)) {
+    if (s->end_transfer_fn_idx >= ARRAY_SIZE(transfer_end_table)) {
         return -EINVAL;
     }
     s->end_transfer_func = transfer_end_table[s->end_transfer_fn_idx];
Signature · 1 line
1.7.6.4
author
#10 lore raw

PATCH 5/7 vmdk: Fix use of uninitialised value

In reply to Kevin Wolf

In error cases, cid is never set.

---
 block/vmdk.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block/vmdk.c +1 -1
index 6be592f..6cdbfb7 100644
@@ -208,7 +208,7 @@ static void vmdk_free_last_extent(BlockDriverState *bs)
 static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
 {
     char desc[DESC_SIZE];
-    uint32_t cid;
+    uint32_t cid = 0;
     const char *p_name, *cid_str;
     size_t cid_str_size;
     BDRVVmdkState *s = bs->opaque;
Signature · 1 line
1.7.6.4
author
#13 lore raw

PATCH 6/7 vmdk: Improve error handling

In reply to Kevin Wolf

Return the right error values in some more places.

---
 block/vmdk.c |   21 +++++++++++++++------
 1 files changed, 15 insertions(+), 6 deletions(-)
block/vmdk.c +15 -6
index 6cdbfb7..fa0e8bd 100644
@@ -212,8 +212,10 @@ static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
     const char *p_name, *cid_str;
     size_t cid_str_size;
     BDRVVmdkState *s = bs->opaque;
+    int ret;
 
-    if (bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE) != DESC_SIZE) {
+    ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
+    if (ret < 0) {
         return 0;
     }
 
@@ -239,10 +241,12 @@ static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
     char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
     char *p_name, *tmp_str;
     BDRVVmdkState *s = bs->opaque;
+    int ret;
 
     memset(desc, 0, sizeof(desc));
-    if (bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE) != DESC_SIZE) {
-        return -EIO;
+    ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
+    if (ret < 0) {
+        return ret;
     }
 
     tmp_str = strstr(desc, "parentCID");
@@ -254,9 +258,11 @@ static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
         pstrcat(desc, sizeof(desc), tmp_desc);
     }
 
-    if (bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE) < 0) {
-        return -EIO;
+    ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
+    if (ret < 0) {
+        return ret;
     }
+
     return 0;
 }
 
@@ -1109,7 +1115,10 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
         /* update CID on the first write every time the virtual disk is
          * opened */
         if (!s->cid_updated) {
-            vmdk_write_cid(bs, time(NULL));
+            ret = vmdk_write_cid(bs, time(NULL));
+            if (ret < 0) {
+                return ret;
+            }
             s->cid_updated = true;
         }
     }
Signature · 1 line
1.7.6.4
author
#14 lore raw

PATCH 7/7 vmdk: Fix possible segfaults

In reply to Kevin Wolf

Data we read from the disk isn't necessarily null terminated and may not contain the string we're looking for. The code needs to be a bit more careful here.

---
 block/vmdk.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
block/vmdk.c +6 -1
index fa0e8bd..8caaf0b 100644
@@ -227,6 +227,7 @@ static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
         cid_str_size = sizeof("CID");
     }
 
+    desc[DESC_SIZE - 1] = '\0';
     p_name = strstr(desc, cid_str);
     if (p_name != NULL) {
         p_name += cid_str_size;
@@ -243,13 +244,17 @@ static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
     BDRVVmdkState *s = bs->opaque;
     int ret;
 
-    memset(desc, 0, sizeof(desc));
     ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
     if (ret < 0) {
         return ret;
     }
 
+    desc[DESC_SIZE - 1] = '\0';
     tmp_str = strstr(desc, "parentCID");
+    if (tmp_str == NULL) {
+        return -EINVAL;
+    }
+
     pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
     p_name = strstr(desc, "CID");
     if (p_name != NULL) {
Signature · 1 line
1.7.6.4
author
#6 lore raw

PATCH 3/7 qcow: Fix bdrv_write_compressed error handling

In reply to Kevin Wolf
---
 block/qcow.c |   30 +++++++++++++++++++-----------
 1 files changed, 19 insertions(+), 11 deletions(-)
block/qcow.c +19 -11
index ab36b29..35e21eb 100644
@@ -736,8 +736,6 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
         return -EINVAL;
 
     out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
-    if (!out_buf)
-        return -1;
 
     /* best compression, small window, no zlib header */
     memset(&strm, 0, sizeof(strm));
@@ -745,8 +743,8 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
                        Z_DEFLATED, -12,
                        9, Z_DEFAULT_STRATEGY);
     if (ret != 0) {
-        g_free(out_buf);
-        return -1;
+        ret = -EINVAL;
+        goto fail;
     }
 
     strm.avail_in = s->cluster_size;
@@ -756,9 +754,9 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
 
     ret = deflate(&strm, Z_FINISH);
     if (ret != Z_STREAM_END && ret != Z_OK) {
-        g_free(out_buf);
         deflateEnd(&strm);
-        return -1;
+        ret = -EINVAL;
+        goto fail;
     }
     out_len = strm.next_out - out_buf;
 
@@ -766,19 +764,29 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
 
     if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
         /* could not compress: write normal cluster */
-        bdrv_write(bs, sector_num, buf, s->cluster_sectors);
+        ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
+        if (ret < 0) {
+            goto fail;
+        }
     } else {
         cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
                                             out_len, 0, 0);
+        if (cluster_offset == 0) {
+            ret = -EIO;
+            goto fail;
+        }
+
         cluster_offset &= s->cluster_offset_mask;
-        if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
-            g_free(out_buf);
-            return -1;
+        ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
+        if (ret < 0) {
+            goto fail;
         }
     }
 
+    ret = 0;
+fail:
     g_free(out_buf);
-    return 0;
+    return ret;
 }
 
 static coroutine_fn int qcow_co_flush(BlockDriverState *bs)
Signature · 1 line
1.7.6.4
#11 lore raw

Re: [Qemu-devel] [PATCH 5/7] vmdk: Fix use of uninitialised value

In reply to Kevin Wolf
Quoted 3 lines · Kevin Wolf

In error cases, cid is never set.

This fix is already in the qemu-trivial queue:
http://repo.or.cz/w/qemu/stefanha.git/commit/8379e46d1fd681b8aa4714382e2cdab05e5d0575

Signature · 1 line
Pavel
Quoted 22 lines

---
 block/vmdk.c |    2 +-

 1 files changed, 1 insertions(+), 1 deletions(-)
block/vmdk.c +1 -2
index 6be592f..6cdbfb7 100644
@@ -208,7 +208,7 @@ static void vmdk_free_last_extent(BlockDriverState *bs)
  static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
  {
     char desc[DESC_SIZE];
-    uint32_t cid;
+    uint32_t cid = 0;
     const char *p_name, *cid_str;
     size_t cid_str_size;
     BDRVVmdkState *s = bs->opaque;
--

1.7.6.4

author
#12 lore raw

Re: [Qemu-devel] [PATCH 5/7] vmdk: Fix use of uninitialised value

Am 26.10.2011 15:21, schrieb Pavel Borzenkov:

Quoted 7 lines · Kevin Wolf
Quoted 3 lines

In error cases, cid is never set.

This fix is already in the qemu-trivial queue:
http://repo.or.cz/w/qemu/stefanha.git/commit/8379e46d1fd681b8aa4714382e2cdab05e5d0575

Oh, okay, didn't see that. I guess git will just drop my patch when I rebase after the qemu-trivial one has been applied.

Kevin

Reviewed-by on v1 1/7
#2 lore raw

Re: [Qemu-devel] [PATCH 1/7] block: Remove dead code

In reply to Kevin Wolf
Reviewed-by [email protected]

2011/10/26 Kevin Wolf <[email protected]>:

Quoted 27 lines

---
 block.c |    6 +-----

 1 files changed, 1 insertions(+), 5 deletions(-)
block.c +1 -6
index 0b7bc06..86d450c 100644
@@ -2039,11 +2039,7 @@ const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
  void bdrv_get_backing_filename(BlockDriverState *bs,
                                char *filename, int filename_size)
  {
-    if (!bs->backing_file) {
-        pstrcpy(filename, filename_size, "");
-    } else {
-        pstrcpy(filename, filename_size, bs->backing_file);
-    }
+    pstrcpy(filename, filename_size, bs->backing_file);
  }
 
  int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
--

1.7.6.4

Signature · 2 lines
Regards
Robert Wang
Reviewed-by on v1 4/7
#9 lore raw

Re: [Qemu-devel] [PATCH 4/7] ide: Fix off-by-one error in array index check

In reply to Kevin Wolf
Quoted 10 lines · Kevin Wolf
---
  hw/ide/core.c |    2 +-
  1 files changed, 1 insertions(+), 1 deletions(-)
hw/ide/core.c +0 -0
index 280a117..29305d3 100644

hw/ide/core.c static int ide_drive_pio_post_load(void *opaque, int version_id) · line 2045

Quoted 8 lines
hw/ide/core.c +1 -1
@@ -2039,7 +2039,7 @@ static int ide_drive_pio_post_load(void *opaque, int version_id)
  {
      IDEState *s = opaque;
 
-    if (s->end_transfer_fn_idx>  ARRAY_SIZE(transfer_end_table)) {
+    if (s->end_transfer_fn_idx>= ARRAY_SIZE(transfer_end_table)) {
          return -EINVAL;
      }
      s->end_transfer_func = transfer_end_table[s->end_transfer_fn_idx];
Reviewed-by on v1 3/7
#7 lore raw

Re: [Qemu-devel] [PATCH 3/7] qcow: Fix bdrv_write_compressed error handling

In reply to Kevin Wolf
Quoted 66 lines · Kevin Wolf
---
  block/qcow.c |   30 +++++++++++++++++++-----------
  1 files changed, 19 insertions(+), 11 deletions(-)
block/qcow.c +16 -10
index ab36b29..35e21eb 100644
@@ -736,8 +736,6 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
          return -EINVAL;
 
      out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
-    if (!out_buf)
-        return -1;
 
      /* best compression, small window, no zlib header */
      memset(&strm, 0, sizeof(strm));
@@ -745,8 +743,8 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
                         Z_DEFLATED, -12,
                         9, Z_DEFAULT_STRATEGY);
      if (ret != 0) {
-        g_free(out_buf);
-        return -1;
+        ret = -EINVAL;
+        goto fail;
      }
 
      strm.avail_in = s->cluster_size;
@@ -756,9 +754,9 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
 
      ret = deflate(&strm, Z_FINISH);
      if (ret != Z_STREAM_END&&  ret != Z_OK) {
-        g_free(out_buf);
          deflateEnd(&strm);
-        return -1;
+        ret = -EINVAL;
+        goto fail;
      }
      out_len = strm.next_out - out_buf;
 
@@ -766,19 +764,29 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
 
      if (ret != Z_STREAM_END || out_len>= s->cluster_size) {
          /* could not compress: write normal cluster */
-        bdrv_write(bs, sector_num, buf, s->cluster_sectors);
+        ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
+        if (ret<  0) {
+            goto fail;
+        }
      } else {
          cluster_offset = get_cluster_offset(bs, sector_num<<  9, 2,
                                              out_len, 0, 0);
+        if (cluster_offset == 0) {
+            ret = -EIO;
+            goto fail;
+        }
+
          cluster_offset&= s->cluster_offset_mask;
-        if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
-            g_free(out_buf);
-            return -1;
+        ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
+        if (ret<  0) {
+            goto fail;
          }

block/qcow.c static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, · line 792

Quoted 10 lines
block/qcow.c +3 -1
      }
 
+    ret = 0;
+fail:
      g_free(out_buf);
-    return 0;
+    return ret;
  }
 
  static coroutine_fn int qcow_co_flush(BlockDriverState *bs)
#3 lore raw

Re: [Qemu-devel] [PATCH 1/7] block: Remove dead code

In reply to Kevin Wolf

block.c const char *bdrv_get_encrypted_filename(BlockDriverState *bs) · line 2043

Quoted 11 lines · Kevin Wolf
block.c +1 -5
@@ -2039,11 +2039,7 @@ const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
 void bdrv_get_backing_filename(BlockDriverState *bs,
                                char *filename, int filename_size)
 {
-    if (!bs->backing_file) {
-        pstrcpy(filename, filename_size, "");
-    } else {
-        pstrcpy(filename, filename_size, bs->backing_file);
-    }
+    pstrcpy(filename, filename_size, bs->backing_file);
 }

I think this points to another problem:

bs->backing_file[] is never cleared across bdrv_close()/bdrv_open().

If we open an image file that uses a backing file, then close the BlockDriverState, and then open a file which does not use a backing file we're left with the old backing file!

Stefan

author
#4 lore raw

Re: [Qemu-devel] [PATCH 1/7] block: Remove dead code

Am 27.10.2011 09:37, schrieb Stefan Hajnoczi:

Quoted 15 lines · Kevin Wolf

block.c const char *bdrv_get_encrypted_filename(BlockDriverState *bs) · line 2043

Quoted 11 lines
block.c +1 -5
@@ -2039,11 +2039,7 @@ const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
 void bdrv_get_backing_filename(BlockDriverState *bs,
                                char *filename, int filename_size)
 {
-    if (!bs->backing_file) {
-        pstrcpy(filename, filename_size, "");
-    } else {
-        pstrcpy(filename, filename_size, bs->backing_file);
-    }
+    pstrcpy(filename, filename_size, bs->backing_file);
 }

I think this points to another problem:

Quoted 5 lines

bs->backing_file[] is never cleared across bdrv_close()/bdrv_open().

If we open an image file that uses a backing file, then close the BlockDriverState, and then open a file which does not use a backing file we're left with the old backing file!

Ouch! Care to send a fix?

Did you check if there are more fields in BlockDriverState that should be cleared?

Kevin