Skip to contentSkip to conversation

FORTIFY_SOURCE followup

PATCH Applied Juan Quintela 3 participants 13 messages Mar 4, 2010 – Mar 9, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover FORTIFY_SOURCE followup

Hi

Series updated with yesterday comments

v2:
- drop un-needed slirp exit() patch (kevin)
- add qemu_write_full() documentation (danp)
- use strerror(-errno) (kevin, pbonzini)

Please review and apply.

Later, Juan.

v1:
This series make:
- all block *_create() functions return -errno instead of -1
- this makes that we can end writting errno/error at bdrv_create() callers (qemu-img)
- once there found a double free problem in the error handling of vmdk, fixed it.
- slirp: also check that system() was able to fork (amit noticed it)
- daemonize: if we are unable to write into the pipe, print a message and exit. We can't really recover from that error (amit noticed it).

Juan Quintela (10):
cow: return errno instead of -1
slirp: check system() success
qcow2: return errno instead of -1
qcow: return errno instead of -1
vmdk: return errno instead of -1
vmdk: make vmdk_snapshot_create return -errno
vmdk: fix double free
vmdk: share cleanup code
block: print errno on error
documentation: qemu_write_full don't work with non-blocking fd's

 block/cow.c   |    5 +--
 block/qcow.c  |    8 ++--
 block/qcow2.c |   18 +++++-----
 block/vmdk.c  |  106 +++++++++++++++++++++++++++++++++++++--------------------
 net/slirp.c   |    2 +-
 osdep.c       |    5 +++
 qemu-img.c    |    4 +-
 7 files changed, 92 insertions(+), 56 deletions(-)
#3 lore raw

PATCH v2 2/10 slirp: check system() success

we shouldn't call W*() macros until we check that fork worked.

---
 net/slirp.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
net/slirp.c +1 -1
index 317cca7..7f846ec 100644
@@ -472,7 +472,7 @@ static void slirp_smb_cleanup(SlirpState *s)
     if (s->smb_dir[0] != '\0') {
         snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir);
         ret = system(cmd);
-        if (!WIFEXITED(ret)) {
+        if (ret == -1 || !WIFEXITED(ret)) {
             qemu_error("'%s' failed.\n", cmd);
         } else if (WEXITSTATUS(ret)) {
             qemu_error("'%s' failed. Error code: %d\n",
Signature · 1 line
1.6.6.1
#5 lore raw

PATCH v2 3/10 qcow2: return errno instead of -1

In reply to Juan Quintela
---
 block/qcow2.c |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)
block/qcow2.c +9 -9
index bf8170e..5b6dad9 100644
@@ -851,7 +851,7 @@ static int qcow_create2(const char *filename, int64_t total_size,
 
     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
     if (fd < 0)
-        return -1;
+        return -errno;
     memset(&header, 0, sizeof(header));
     header.magic = cpu_to_be32(QCOW_MAGIC);
     header.version = cpu_to_be32(QCOW_VERSION);
@@ -930,7 +930,7 @@ static int qcow_create2(const char *filename, int64_t total_size,
     /* write all the data */
     ret = qemu_write_full(fd, &header, sizeof(header));
     if (ret != sizeof(header)) {
-        ret = -1;
+        ret = -errno;
         goto exit;
     }
     if (backing_file) {
@@ -943,25 +943,25 @@ static int qcow_create2(const char *filename, int64_t total_size,
             cpu_to_be32s(&ext_bf.len);
             ret = qemu_write_full(fd, &ext_bf, sizeof(ext_bf));
             if (ret != sizeof(ext_bf)) {
-                ret = -1;
+                ret = -errno;
                 goto exit;
             }
             ret = qemu_write_full(fd, backing_format, backing_format_len);
             if (ret != backing_format_len) {
-                ret = -1;
+                ret = -errno;
                 goto exit;
             }
             if (padding > 0) {
                 ret = qemu_write_full(fd, zero, padding);
                 if (ret != padding) {
-                    ret = -1;
+                    ret = -errno;
                     goto exit;
                 }
             }
         }
         ret = qemu_write_full(fd, backing_file, backing_filename_len);
         if (ret != backing_filename_len) {
-            ret = -1;
+            ret = -errno;
             goto exit;
         }
     }
@@ -970,14 +970,14 @@ static int qcow_create2(const char *filename, int64_t total_size,
     for(i = 0;i < l1_size; i++) {
         ret = qemu_write_full(fd, &tmp, sizeof(tmp));
         if (ret != sizeof(tmp)) {
-            ret = -1;
+            ret = -errno;
             goto exit;
         }
     }
     lseek(fd, s->refcount_table_offset, SEEK_SET);
     ret = qemu_write_full(fd, s->refcount_table, s->cluster_size);
     if (ret != s->cluster_size) {
-        ret = -1;
+        ret = -errno;
         goto exit;
     }
 
@@ -985,7 +985,7 @@ static int qcow_create2(const char *filename, int64_t total_size,
     ret = qemu_write_full(fd, s->refcount_block,
 		    ref_clusters * s->cluster_size);
     if (ret != ref_clusters * s->cluster_size) {
-        ret = -1;
+        ret = -errno;
         goto exit;
     }
 
Signature · 1 line
1.6.6.1
#1 lore raw

PATCH v2 1/10 cow: return errno instead of -1

In reply to Juan Quintela

Remove not needed ret = 0 assignment.

---
 block/cow.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
block/cow.c +2 -3
index 3733385..97e9745 100644
@@ -224,7 +224,7 @@ static int cow_create(const char *filename, QEMUOptionParameter *options)
     cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
               0644);
     if (cow_fd < 0)
-        return -1;
+        return -errno;
     memset(&cow_header, 0, sizeof(cow_header));
     cow_header.magic = cpu_to_be32(COW_MAGIC);
     cow_header.version = cpu_to_be32(COW_VERSION);
@@ -251,7 +251,7 @@ static int cow_create(const char *filename, QEMUOptionParameter *options)
     cow_header.size = cpu_to_be64(image_sectors * 512);
     ret = qemu_write_full(cow_fd, &cow_header, sizeof(cow_header));
     if (ret != sizeof(cow_header)) {
-        ret = -1;
+        ret = -errno;
         goto exit;
     }
 
@@ -262,7 +262,6 @@ static int cow_create(const char *filename, QEMUOptionParameter *options)
         goto exit;
     }
 
-    ret = 0;
 exit:
     close(cow_fd);
     return ret;
Signature · 1 line
1.6.6.1
#6 lore raw

PATCH v2 4/10 qcow: return errno instead of -1

In reply to Juan Quintela
---
 block/qcow.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
block/qcow.c +4 -4
index 003db1e..c619984 100644
@@ -766,7 +766,7 @@ static int qcow_create(const char *filename, QEMUOptionParameter *options)
 
     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
     if (fd < 0)
-        return -1;
+        return -errno;
     memset(&header, 0, sizeof(header));
     header.magic = cpu_to_be32(QCOW_MAGIC);
     header.version = cpu_to_be32(QCOW_VERSION);
@@ -804,14 +804,14 @@ static int qcow_create(const char *filename, QEMUOptionParameter *options)
     /* write all the data */
     ret = qemu_write_full(fd, &header, sizeof(header));
     if (ret != sizeof(header)) {
-        ret = -1;
+        ret = -errno;
         goto exit;
     }
 
     if (backing_file) {
         ret = qemu_write_full(fd, backing_file, backing_filename_len);
         if (ret != backing_filename_len) {
-            ret = -1;
+            ret = -errno;
             goto exit;
         }
 
@@ -821,7 +821,7 @@ static int qcow_create(const char *filename, QEMUOptionParameter *options)
     for(i = 0;i < l1_size; i++) {
         ret = qemu_write_full(fd, &tmp, sizeof(tmp));
         if (ret != sizeof(tmp)) {
-            ret = -1;
+            ret = -errno;
             goto exit;
         }
     }
Signature · 1 line
1.6.6.1
#7 lore raw

PATCH v2 5/10 vmdk: return errno instead of -1

In reply to Juan Quintela
---
 block/vmdk.c |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)
block/vmdk.c +7 -7
index 56c28a0..5b1d197 100644
@@ -740,7 +740,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options)
     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
               0644);
     if (fd < 0)
-        return -1;
+        return -errno;
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
     header.version = cpu_to_le32(1);
@@ -777,18 +777,18 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options)
     /* write all the data */
     ret = qemu_write_full(fd, &magic, sizeof(magic));
     if (ret != sizeof(magic)) {
-        ret = -1;
+        ret = -errno;
         goto exit;
     }
     ret = qemu_write_full(fd, &header, sizeof(header));
     if (ret != sizeof(header)) {
-        ret = -1;
+        ret = -errno;
         goto exit;
     }
 
     ret = ftruncate(fd, header.grain_offset << 9);
     if (ret < 0) {
-        ret = -1;
+        ret = -errno;
         goto exit;
     }
 
@@ -798,7 +798,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options)
          i < gt_count; i++, tmp += gt_size) {
         ret = qemu_write_full(fd, &tmp, sizeof(tmp));
         if (ret != sizeof(tmp)) {
-            ret = -1;
+            ret = -errno;
             goto exit;
         }
     }
@@ -809,7 +809,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options)
          i < gt_count; i++, tmp += gt_size) {
         ret = qemu_write_full(fd, &tmp, sizeof(tmp));
         if (ret != sizeof(tmp)) {
-            ret = -1;
+            ret = -errno;
             goto exit;
         }
     }
@@ -831,7 +831,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options)
     lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
     ret = qemu_write_full(fd, desc, strlen(desc));
     if (ret != strlen(desc)) {
-        ret = -1;
+        ret = -errno;
         goto exit;
     }
 
Signature · 1 line
1.6.6.1
#8 lore raw

PATCH v2 6/10 vmdk: make vmdk_snapshot_create return -errno

In reply to Juan Quintela
---
 block/vmdk.c |   79 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 58 insertions(+), 21 deletions(-)
block/vmdk.c +58 -21
index 5b1d197..67a690e 100644
@@ -187,6 +187,7 @@ static int vmdk_is_cid_valid(BlockDriverState *bs)
 static int vmdk_snapshot_create(const char *filename, const char *backing_file)
 {
     int snp_fd, p_fd;
+    int ret;
     uint32_t p_cid;
     char *p_name, *gd_buf, *rgd_buf;
     const char *real_filename, *temp_str;
@@ -211,35 +212,49 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
 
     snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
     if (snp_fd < 0)
-        return -1;
+        return -errno;
     p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE);
     if (p_fd < 0) {
         close(snp_fd);
-        return -1;
+        return -errno;
     }
 
     /* read the header */
-    if (lseek(p_fd, 0x0, SEEK_SET) == -1)
+    if (lseek(p_fd, 0x0, SEEK_SET) == -1) {
+        ret = -errno;
         goto fail;
-    if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE)
+    }
+    if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE) {
+        ret = -errno;
         goto fail;
+    }
 
     /* write the header */
-    if (lseek(snp_fd, 0x0, SEEK_SET) == -1)
+    if (lseek(snp_fd, 0x0, SEEK_SET) == -1) {
+        ret = -errno;
         goto fail;
-    if (write(snp_fd, hdr, HEADER_SIZE) == -1)
+    }
+    if (write(snp_fd, hdr, HEADER_SIZE) == -1) {
+        ret = -errno;
         goto fail;
+    }
 
     memset(&header, 0, sizeof(header));
     memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC
 
-    if (ftruncate(snp_fd, header.grain_offset << 9))
+    if (ftruncate(snp_fd, header.grain_offset << 9)) {
+        ret = -errno;
         goto fail;
+    }
     /* the descriptor offset = 0x200 */
-    if (lseek(p_fd, 0x200, SEEK_SET) == -1)
+    if (lseek(p_fd, 0x200, SEEK_SET) == -1) {
+        ret = -errno;
         goto fail;
-    if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE)
+    }
+    if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE) {
+        ret = -errno;
         goto fail;
+    }
 
     if ((p_name = strstr(p_desc,"CID")) != NULL) {
         p_name += sizeof("CID");
@@ -258,10 +273,14 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
              (uint32_t)header.capacity, real_filename);
 
     /* write the descriptor */
-    if (lseek(snp_fd, 0x200, SEEK_SET) == -1)
+    if (lseek(snp_fd, 0x200, SEEK_SET) == -1) {
+        ret = -errno;
         goto fail;
-    if (write(snp_fd, s_desc, strlen(s_desc)) == -1)
+    }
+    if (write(snp_fd, s_desc, strlen(s_desc)) == -1) {
+        ret = -errno;
         goto fail;
+    }
 
     gd_offset = header.gd_offset * SECTOR_SIZE;     // offset of GD table
     rgd_offset = header.rgd_offset * SECTOR_SIZE;   // offset of RGD table
@@ -271,33 +290,51 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
      * 512 GTE per GT, each GTE points to grain
      */
     gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE;
-    if (!gt_size)
+    if (!gt_size) {
+        ret = -EINVAL;
         goto fail;
+    }
     gde_entries = (uint32_t)(capacity / gt_size);  // number of gde/rgde
     gd_size = gde_entries * sizeof(uint32_t);
 
     /* write RGD */
     rgd_buf = qemu_malloc(gd_size);
-    if (lseek(p_fd, rgd_offset, SEEK_SET) == -1)
+    if (lseek(p_fd, rgd_offset, SEEK_SET) == -1) {
+        ret = -errno;
         goto fail_rgd;
-    if (read(p_fd, rgd_buf, gd_size) != gd_size)
+    }
+    if (read(p_fd, rgd_buf, gd_size) != gd_size) {
+        ret = -errno;
         goto fail_rgd;
-    if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1)
+    }
+    if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1) {
+        ret = -errno;
         goto fail_rgd;
-    if (write(snp_fd, rgd_buf, gd_size) == -1)
+    }
+    if (write(snp_fd, rgd_buf, gd_size) == -1) {
+        ret = -errno;
         goto fail_rgd;
+    }
     qemu_free(rgd_buf);
 
     /* write GD */
     gd_buf = qemu_malloc(gd_size);
-    if (lseek(p_fd, gd_offset, SEEK_SET) == -1)
+    if (lseek(p_fd, gd_offset, SEEK_SET) == -1) {
+        ret = -errno;
         goto fail_gd;
-    if (read(p_fd, gd_buf, gd_size) != gd_size)
+    }
+    if (read(p_fd, gd_buf, gd_size) != gd_size) {
+        ret = -errno;
         goto fail_gd;
-    if (lseek(snp_fd, gd_offset, SEEK_SET) == -1)
+    }
+    if (lseek(snp_fd, gd_offset, SEEK_SET) == -1) {
+        ret = -errno;
         goto fail_gd;
-    if (write(snp_fd, gd_buf, gd_size) == -1)
+    }
+    if (write(snp_fd, gd_buf, gd_size) == -1) {
+        ret = -errno;
         goto fail_gd;
+    }
     qemu_free(gd_buf);
 
     close(p_fd);
@@ -311,7 +348,7 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
     fail:
     close(p_fd);
     close(snp_fd);
-    return -1;
+    return ret;
 }
 
 static void vmdk_parent_close(BlockDriverState *bs)
Signature · 1 line
1.6.6.1
#9 lore raw

PATCH v2 7/10 vmdk: fix double free

In reply to Juan Quintela

fail_gd error case would also free rgd_buf that was already freed

---
 block/vmdk.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block/vmdk.c +1 -1
index 67a690e..819c1c9 100644
@@ -315,7 +315,6 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
         ret = -errno;
         goto fail_rgd;
     }
-    qemu_free(rgd_buf);
 
     /* write GD */
     gd_buf = qemu_malloc(gd_size);
@@ -336,6 +335,7 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
         goto fail_gd;
     }
     qemu_free(gd_buf);
+    qemu_free(rgd_buf);
 
     close(p_fd);
     close(snp_fd);
Signature · 1 line
1.6.6.1
#10 lore raw

PATCH v2 8/10 vmdk: share cleanup code

In reply to Juan Quintela

cleanup code is identical for error/success cases. Only difference are goto labels.

---
 block/vmdk.c |   13 ++++---------
 1 files changed, 4 insertions(+), 9 deletions(-)
block/vmdk.c +4 -9
index 819c1c9..007fca4 100644
@@ -334,18 +334,13 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
         ret = -errno;
         goto fail_gd;
     }
-    qemu_free(gd_buf);
-    qemu_free(rgd_buf);
-
-    close(p_fd);
-    close(snp_fd);
-    return 0;
+    ret = 0;
 
-    fail_gd:
+fail_gd:
     qemu_free(gd_buf);
-    fail_rgd:
+fail_rgd:
     qemu_free(rgd_buf);
-    fail:
+fail:
     close(p_fd);
     close(snp_fd);
     return ret;
Signature · 1 line
1.6.6.1
#11 lore raw

PATCH v2 9/10 block: print errno on error

In reply to Juan Quintela

Now that we changed all create calls to return errno, just print it.

---
 qemu-img.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
qemu-img.c +2 -2
index 0c9f2d4..e51b40c 100644
@@ -374,7 +374,7 @@ static int img_create(int argc, char **argv)
         } else if (ret == -EFBIG) {
             error("The image size is too large for file format '%s'", fmt);
         } else {
-            error("Error while formatting");
+            error("%s: error while creating %s: %s", filename, fmt, strerror(-ret));
         }
     }
     return 0;
@@ -687,7 +687,7 @@ static int img_convert(int argc, char **argv)
         } else if (ret == -EFBIG) {
             error("The image size is too large for file format '%s'", out_fmt);
         } else {
-            error("Error while formatting '%s'", out_filename);
+            error("%s: error while converting %s: %s", out_filename, out_fmt, strerror(-ret));
         }
     }
 
Signature · 1 line
1.6.6.1
#12 lore raw

PATCH v2 10/10 documentation: qemu_write_full don't work with non-blocking fd's

In reply to Juan Quintela
---
 osdep.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
osdep.c +5 -0
index 5bf3c00..abbc8a2 100644
@@ -261,6 +261,11 @@ int qemu_open(const char *name, int flags, ...)
  *
  * Return the number of bytes transferred.
  * Set errno if fewer than `count' bytes are written.
+ *
+ * This function don't work with non-blocking fd's.
+ * Any of the possibilities with non-bloking fd's is bad:
+ *   - return a short write (then name is wrong)
+ *   - busy wait adding (errno == EAGAIN) to the loop
  */
 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
 {
Signature · 1 line
1.6.6.1
Acked-by on v2 2/10
#4 lore raw

[Qemu-devel] Re: [PATCH 02/10] slirp: check system() success

In reply to Juan Quintela
Quoted 3 lines · Juan Quintela

we shouldn't call W*() macros until we check that fork worked.

Quoted 21 lines
---
 net/slirp.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
net/slirp.c +1 -1
index 317cca7..7f846ec 100644
@@ -472,7 +472,7 @@ static void slirp_smb_cleanup(SlirpState *s)
     if (s->smb_dir[0] != '\0') {
         snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir);
         ret = system(cmd);
-        if (!WIFEXITED(ret)) {
+        if (ret == -1 || !WIFEXITED(ret)) {
             qemu_error("'%s' failed.\n", cmd);
         } else if (WEXITSTATUS(ret)) {
             qemu_error("'%s' failed. Error code: %d\n",
Signature · 1 line
1.6.6.1
#2 lore raw

Re: [Qemu-devel] [PATCH 01/10] cow: return errno instead of -1

In reply to Juan Quintela
Quoted 4 lines · Juan Quintela

Remove not needed ret = 0 assignment.

Applied all. Thanks.

Regards,

Anthony Liguori

Quoted 35 lines
---
  block/cow.c |    5 ++---
  1 files changed, 2 insertions(+), 3 deletions(-)
block/cow.c +2 -3
index 3733385..97e9745 100644
@@ -224,7 +224,7 @@ static int cow_create(const char *filename, QEMUOptionParameter *options)
      cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
                0644);
      if (cow_fd<  0)
-        return -1;
+        return -errno;
      memset(&cow_header, 0, sizeof(cow_header));
      cow_header.magic = cpu_to_be32(COW_MAGIC);
      cow_header.version = cpu_to_be32(COW_VERSION);
@@ -251,7 +251,7 @@ static int cow_create(const char *filename, QEMUOptionParameter *options)
      cow_header.size = cpu_to_be64(image_sectors * 512);
      ret = qemu_write_full(cow_fd,&cow_header, sizeof(cow_header));
      if (ret != sizeof(cow_header)) {
-        ret = -1;
+        ret = -errno;
          goto exit;
      }
 
@@ -262,7 +262,6 @@ static int cow_create(const char *filename, QEMUOptionParameter *options)
          goto exit;
      }
 
-    ret = 0;
  exit:
      close(cow_fd);
      return ret;