Skip to contentSkip to conversation

block/file-posix: fix g_file_get_contents return path

PATCH Superseded Sam Li 3 participants 8 messages Jun 4, 2023 – Jul 27, 2023 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Sam Li

author
#0 lore raw

PATCH 1/2 block/file-posix: fix g_file_get_contents return path

The g_file_get_contents() function returns a g_boolean. If it fails, the returned value will be 0 instead of -1. Solve the issue by skipping assigning ret value.

This issue was found by Matthew Rosato using virtio-blk-{pci,ccw} backed by an NVMe partition e.g. /dev/nvme0n1p1 on s390x.

---
 block/file-posix.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
block/file-posix.c +2 -4
index ac1ed54811..0d9d179a35 100644
@@ -1232,7 +1232,6 @@ static int hdev_get_max_hw_transfer(int fd, struct stat *st)
 static int get_sysfs_str_val(struct stat *st, const char *attribute,
                              char **val) {
     g_autofree char *sysfspath = NULL;
-    int ret;
     size_t len;
 
     if (!S_ISBLK(st->st_mode)) {
@@ -1242,8 +1241,7 @@ static int get_sysfs_str_val(struct stat *st, const char *attribute,
     sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/%s",
                                 major(st->st_rdev), minor(st->st_rdev),
                                 attribute);
-    ret = g_file_get_contents(sysfspath, val, &len, NULL);
-    if (ret == -1) {
+    if (!g_file_get_contents(sysfspath, val, &len, NULL)) {
         return -ENOENT;
     }
 
@@ -1253,7 +1251,7 @@ static int get_sysfs_str_val(struct stat *st, const char *attribute,
     if (*(p + len - 1) == '\n') {
         *(p + len - 1) = '\0';
     }
-    return ret;
+    return 0;
 }
 #endif
 
Signature · 1 line
2.40.1
author
#1 lore raw

PATCH 2/2 block/file-posix: fix wps checking in raw_co_prw

If the write operation fails and the wps is NULL, then accessing it will lead to data corruption.

Solving the issue by adding a nullptr checking in get_zones_wp() where the wps is used.

This issue is found by Peter Maydell using the Coverity Tool (CID 1512459).

---
 block/file-posix.c | 4 ++++
 1 file changed, 4 insertions(+)
block/file-posix.c +4 -0
index 0d9d179a35..620942bf40 100644
@@ -1340,6 +1340,10 @@ static int get_zones_wp(BlockDriverState *bs, int fd, int64_t offset,
     rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
     g_autofree struct blk_zone_report *rep = NULL;
 
+    if (!wps) {
+        return -1;
+    }
+
     rep = g_malloc(rep_size);
     blkz = (struct blk_zone *)(rep + 1);
     while (n < nrz) {
Signature · 1 line
2.40.1
#2 lore raw

Re: [PATCH 2/2] block/file-posix: fix wps checking in raw_co_prw

Quoted 19 lines · Sam Li

If the write operation fails and the wps is NULL, then accessing it will lead to data corruption.

Solving the issue by adding a nullptr checking in get_zones_wp() where the wps is used.

This issue is found by Peter Maydell using the Coverity Tool (CID 1512459).

---
 block/file-posix.c | 4 ++++
 1 file changed, 4 insertions(+)
block/file-posix.c +0 -0
index 0d9d179a35..620942bf40 100644

block/file-posix.c static int get_zones_wp(BlockDriverState *bs, int fd, int64_t offset, · line 1345

Quoted 6 lines
block/file-posix.c +3 -0
@@ -1340,6 +1340,10 @@ static int get_zones_wp(BlockDriverState *bs, int fd, int64_t offset,
     rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
     g_autofree struct blk_zone_report *rep = NULL;
 
+    if (!wps) {
+        return -1;
+    }

An error will be printed every time this happens on a non-zoned device:

  static void update_zones_wp(BlockDriverState *bs, int fd, int64_t offset,
                              unsigned int nrz)
  {
      if (get_zones_wp(bs, fd, offset, nrz, 0) < 0) {
          error_report("update zone wp failed");

Please change the following code to avoid the call to update_zones_wp():

#if defined(CONFIG_BLKZONED)

  {
      BlockZoneWps *wps = bs->wps;
      if (ret == 0) {
          if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND))
              && wps && bs->bl.zone_size) {
              uint64_t *wp = &wps->wp[offset / bs->bl.zone_size];
              if (!BDRV_ZT_IS_CONV(*wp)) {
                  if (type & QEMU_AIO_ZONE_APPEND) {
                      *s->offset = *wp;
                      trace_zbd_zone_append_complete(bs, *s->offset
                          >> BDRV_SECTOR_BITS);
                  }
                  /* Advance the wp if needed */
                  if (offset + bytes > *wp) {
                      *wp = offset + bytes;
                  }
              }
          }
      } else {
-         if (type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) {
+         if (wps && (type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND))) {
              update_zones_wp(bs, s->fd, 0, 1);
          }
      }

Stefan

Reviewed-by on v1 1/2
#3 lore raw
In reply to Sam Li
Quoted 8 lines · Sam Li

The g_file_get_contents() function returns a g_boolean. If it fails, the returned value will be 0 instead of -1. Solve the issue by skipping assigning ret value.

This issue was found by Matthew Rosato using virtio-blk-{pci,ccw} backed by an NVMe partition e.g. /dev/nvme0n1p1 on s390x.

Also tested to verify that this fix resolves the reported issue. Thanks!

Reviewed-by on v1 1/2
#4 lore raw
In reply to Sam Li
Quoted 11 lines · Sam Li

The g_file_get_contents() function returns a g_boolean. If it fails, the returned value will be 0 instead of -1. Solve the issue by skipping assigning ret value.

This issue was found by Matthew Rosato using virtio-blk-{pci,ccw} backed by an NVMe partition e.g. /dev/nvme0n1p1 on s390x.

---
 block/file-posix.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

The number of bytes returned was never used, so changing the return value to 0 or -errno is fine:

#5 lore raw
In reply to Sam Li
Quoted 8 lines · Sam Li

The g_file_get_contents() function returns a g_boolean. If it fails, the returned value will be 0 instead of -1. Solve the issue by skipping assigning ret value.

This issue was found by Matthew Rosato using virtio-blk-{pci,ccw} backed by an NVMe partition e.g. /dev/nvme0n1p1 on s390x.

Polite ping on this patch -- this issue still exists in master as of today and this patch resolves it for me. Just want to make sure it gets into 8.1

Thanks,
Matt

Quoted 35 lines
---
 block/file-posix.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
block/file-posix.c +2 -4
index ac1ed54811..0d9d179a35 100644
@@ -1232,7 +1232,6 @@ static int hdev_get_max_hw_transfer(int fd, struct stat *st)
 static int get_sysfs_str_val(struct stat *st, const char *attribute,
                              char **val) {
     g_autofree char *sysfspath = NULL;
-    int ret;
     size_t len;
 
     if (!S_ISBLK(st->st_mode)) {
@@ -1242,8 +1241,7 @@ static int get_sysfs_str_val(struct stat *st, const char *attribute,
     sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/%s",
                                 major(st->st_rdev), minor(st->st_rdev),
                                 attribute);
-    ret = g_file_get_contents(sysfspath, val, &len, NULL);
-    if (ret == -1) {
+    if (!g_file_get_contents(sysfspath, val, &len, NULL)) {
         return -ENOENT;
     }
 
@@ -1253,7 +1251,7 @@ static int get_sysfs_str_val(struct stat *st, const char *attribute,
     if (*(p + len - 1) == '\n') {
         *(p + len - 1) = '\0';
     }
-    return ret;
+    return 0;
 }
 #endif
 
#6 lore raw
Quoted 5 lines · Matthew Rosato
Quoted 4 lines · Sam Li

The g_file_get_contents() function returns a g_boolean. If it fails, the returned value will be 0 instead of -1. Solve the issue by skipping assigning ret value.

Quoted 7 lines
Quoted 4 lines

This issue was found by Matthew Rosato using virtio-blk-{pci,ccw} backed by an NVMe partition e.g. /dev/nvme0n1p1 on s390x.

Polite ping on this patch -- this issue still exists in master as of today and this patch resolves it for me. Just want to make sure it gets into 8.1

Ping -- I can still reproduce this crash on -rc1. Any chance this patch can get picked up for the 8.1 release?

@Sam I see you sent a v2 of only patch #2 in this series ('block/file-posix: fix wps checking in raw_co_prw').. I wonder if this one just got forgotten since it wasn't sent as part of v2. Maybe try a resend of this patch by itself (plus the review tags added)?

Thanks,
Matt

Quoted 38 lines
Quoted 36 lines
---
 block/file-posix.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
block/file-posix.c +2 -4
index ac1ed54811..0d9d179a35 100644
@@ -1232,7 +1232,6 @@ static int hdev_get_max_hw_transfer(int fd, struct stat *st)
 static int get_sysfs_str_val(struct stat *st, const char *attribute,
                              char **val) {
     g_autofree char *sysfspath = NULL;
-    int ret;
     size_t len;
 
     if (!S_ISBLK(st->st_mode)) {
@@ -1242,8 +1241,7 @@ static int get_sysfs_str_val(struct stat *st, const char *attribute,
     sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/%s",
                                 major(st->st_rdev), minor(st->st_rdev),
                                 attribute);
-    ret = g_file_get_contents(sysfspath, val, &len, NULL);
-    if (ret == -1) {
+    if (!g_file_get_contents(sysfspath, val, &len, NULL)) {
         return -ENOENT;
     }
 
@@ -1253,7 +1251,7 @@ static int get_sysfs_str_val(struct stat *st, const char *attribute,
     if (*(p + len - 1) == '\n') {
         *(p + len - 1) = '\0';
     }
-    return ret;
+    return 0;
 }
 #endif
 
author
#7 lore raw
Quoted 10 lines · Matthew Rosato <[email protected]> 于2023年7月27日周四 19:46写道:
Quoted 8 lines · Matthew Rosato
Quoted 7 lines · Sam Li

The g_file_get_contents() function returns a g_boolean. If it fails, the returned value will be 0 instead of -1. Solve the issue by skipping assigning ret value.

This issue was found by Matthew Rosato using virtio-blk-{pci,ccw} backed by an NVMe partition e.g. /dev/nvme0n1p1 on s390x.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Polite ping on this patch -- this issue still exists in master as of today and this patch resolves it for me. Just want to make sure it gets into 8.1

Ping -- I can still reproduce this crash on -rc1. Any chance this patch can get picked up for the 8.1 release?

@Sam I see you sent a v2 of only patch #2 in this series ('block/file-posix: fix wps checking in raw_co_prw').. I wonder if this one just got forgotten since it wasn't sent as part of v2. Maybe try a resend of this patch by itself (plus the review tags added)?

Ok, I will resend it as a separate patch.

Quoted 43 lines

Thanks,
Matt

Quoted 38 lines
Quoted 36 lines
---
 block/file-posix.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
block/file-posix.c +2 -4
index ac1ed54811..0d9d179a35 100644
@@ -1232,7 +1232,6 @@ static int hdev_get_max_hw_transfer(int fd, struct stat *st)
 static int get_sysfs_str_val(struct stat *st, const char *attribute,
                              char **val) {
     g_autofree char *sysfspath = NULL;
-    int ret;
     size_t len;
 
     if (!S_ISBLK(st->st_mode)) {
@@ -1242,8 +1241,7 @@ static int get_sysfs_str_val(struct stat *st, const char *attribute,
     sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/%s",
                                 major(st->st_rdev), minor(st->st_rdev),
                                 attribute);
-    ret = g_file_get_contents(sysfspath, val, &len, NULL);
-    if (ret == -1) {
+    if (!g_file_get_contents(sysfspath, val, &len, NULL)) {
         return -ENOENT;
     }
 
@@ -1253,7 +1251,7 @@ static int get_sysfs_str_val(struct stat *st, const char *attribute,
     if (*(p + len - 1) == '\n') {
         *(p + len - 1) = '\0';
     }
-    return ret;
+    return 0;
 }
 #endif