Skip to contentSkip to conversation

Fix some coverity issues on VDUSE

PATCH Superseded Xie Yongji 3 participants 17 messages Jun 27, 2022 – Jun 29, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Xie Yongji

author
#0 lore raw

cover Fix some coverity issues on VDUSE

This series fixes some issues reported by coverity.

Patch 1 fixes an incorrect function name.

Patch 2 fixes Coverity CID 1490224.

Patch 3 fixes Coverity CID 1490226, 1490223.

Patch 4 fixes Coverity CID 1490222, 1490227.

Xie Yongji (4):
libvduse: Fix the incorrect function name
libvduse: Replace strcpy() with strncpy()
libvduse: Pass positive value to strerror()
libvduse: Check the return value of some ioctls

 subprojects/libvduse/libvduse.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH 1/4 libvduse: Fix the incorrect function name

In vduse_name_is_valid(), we actually check whether the name is invalid or not. So let's change the function name to vduse_name_is_invalid() to match the behavior.

---
 subprojects/libvduse/libvduse.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
subprojects/libvduse/libvduse.c +3 -3
index 9a2bcec282..6374933881 100644
@@ -1193,7 +1193,7 @@ static int vduse_dev_init(VduseDev *dev, const char *name,
     return 0;
 }
 
-static inline bool vduse_name_is_valid(const char *name)
+static inline bool vduse_name_is_invalid(const char *name)
 {
     return strlen(name) >= VDUSE_NAME_MAX || strstr(name, "..");
 }
@@ -1242,7 +1242,7 @@ VduseDev *vduse_dev_create_by_name(const char *name, uint16_t num_queues,
     VduseDev *dev;
     int ret;
 
-    if (!name || vduse_name_is_valid(name) || !ops ||
+    if (!name || vduse_name_is_invalid(name) || !ops ||
         !ops->enable_queue || !ops->disable_queue) {
         fprintf(stderr, "Invalid parameter for vduse\n");
         return NULL;
@@ -1276,7 +1276,7 @@ VduseDev *vduse_dev_create(const char *name, uint32_t device_id,
     struct vduse_dev_config *dev_config;
     size_t size = offsetof(struct vduse_dev_config, config);
 
-    if (!name || vduse_name_is_valid(name) ||
+    if (!name || vduse_name_is_invalid(name) ||
         !has_feature(features,  VIRTIO_F_VERSION_1) || !config ||
         !config_size || !ops || !ops->enable_queue || !ops->disable_queue) {
         fprintf(stderr, "Invalid parameter for vduse\n");
Signature · 1 line
2.20.1
author
#3 lore raw

PATCH 2/4 libvduse: Replace strcpy() with strncpy()

In reply to Xie Yongji

Coverity reported a string overflow issue since we copied "name" to "dev_config->name" without checking the length. This should be a false positive since we already checked the length of "name" in vduse_name_is_invalid(). But anyway, let's replace strcpy() with strncpy() to fix the coverity complaint.

Fixes Coverity CID 1490224
---
 subprojects/libvduse/libvduse.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
subprojects/libvduse/libvduse.c +2 -1
index 6374933881..1e36227388 100644
@@ -1309,7 +1309,8 @@ VduseDev *vduse_dev_create(const char *name, uint32_t device_id,
         goto err_dev;
     }
 
-    strcpy(dev_config->name, name);
+    strncpy(dev_config->name, name, VDUSE_NAME_MAX);
+    dev_config->name[VDUSE_NAME_MAX - 1] = '\0';
     dev_config->device_id = device_id;
     dev_config->vendor_id = vendor_id;
     dev_config->features = features;
Signature · 1 line
2.20.1
#4 lore raw

Re: [PATCH 2/4] libvduse: Replace strcpy() with strncpy()

subprojects/libvduse/libvduse.c VduseDev *vduse_dev_create(const char *name, uint32_t device_id, · line 1313

Quoted 3 lines · Xie Yongji
subprojects/libvduse/libvduse.c +2 -1
-    strcpy(dev_config->name, name);
+    strncpy(dev_config->name, name, VDUSE_NAME_MAX);
+    dev_config->name[VDUSE_NAME_MAX - 1] = '\0';

g_strlcpy

r~

author
#5 lore raw

Re: [PATCH 2/4] libvduse: Replace strcpy() with strncpy()

Quoted 8 lines · Richard Henderson

subprojects/libvduse/libvduse.c VduseDev *vduse_dev_create(const char *name, uint32_t device_id, · line 1313

Quoted 3 lines · Xie Yongji
subprojects/libvduse/libvduse.c +2 -1
-    strcpy(dev_config->name, name);
+    strncpy(dev_config->name, name, VDUSE_NAME_MAX);
+    dev_config->name[VDUSE_NAME_MAX - 1] = '\0';

g_strlcpy

Now we don't have a dependency on glib, so we use strncpy here.

Thanks,
Yongji

Reviewed-by on v1 2/4
#6 lore raw

Re: [PATCH 2/4] libvduse: Replace strcpy() with strncpy()

In reply to Xie Yongji
Quoted 6 lines · Xie Yongji

Coverity reported a string overflow issue since we copied "name" to "dev_config->name" without checking the length. This should be a false positive since we already checked the length of "name" in vduse_name_is_invalid(). But anyway, let's replace strcpy() with strncpy() to fix the coverity complaint.

Mention why you can't use something nicer from GLib?

Quoted 2 lines
Fixes Coverity CID 1490224
author
#7 lore raw

PATCH 3/4 libvduse: Pass positive value to strerror()

In reply to Xie Yongji

The value passed to strerror() should be positive. So let's fix it.

Fixes Coverity CID 1490226, 1490223
---
 subprojects/libvduse/libvduse.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
subprojects/libvduse/libvduse.c +2 -2
index 1e36227388..1a5981445c 100644
@@ -1257,7 +1257,7 @@ VduseDev *vduse_dev_create_by_name(const char *name, uint16_t num_queues,
     ret = vduse_dev_init(dev, name, num_queues, ops, priv);
     if (ret < 0) {
         fprintf(stderr, "Failed to init vduse device %s: %s\n",
-                name, strerror(ret));
+                name, strerror(-ret));
         free(dev);
         return NULL;
     }
@@ -1331,7 +1331,7 @@ VduseDev *vduse_dev_create(const char *name, uint32_t device_id,
     ret = vduse_dev_init(dev, name, num_queues, ops, priv);
     if (ret < 0) {
         fprintf(stderr, "Failed to init vduse device %s: %s\n",
-                name, strerror(ret));
+                name, strerror(-ret));
         goto err;
     }
 
Signature · 1 line
2.20.1
author
#10 lore raw

PATCH 4/4 libvduse: Check the return value of some ioctls

In reply to Xie Yongji

Coverity pointed out (CID 1490222, 1490227) that we called ioctl somewhere without checking the return value. This patch fixes these issues.

Fixes Coverity CID 1490222, 1490227
---
 subprojects/libvduse/libvduse.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
subprojects/libvduse/libvduse.c +8 -2
index 1a5981445c..bf7302c60a 100644
@@ -947,7 +947,10 @@ static void vduse_queue_disable(VduseVirtq *vq)
 
     eventfd.index = vq->index;
     eventfd.fd = VDUSE_EVENTFD_DEASSIGN;
-    ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd);
+    if (ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd)) {
+        fprintf(stderr, "Failed to disable eventfd for vq[%d]: %s\n",
+                vq->index, strerror(errno));
+    }
     close(vq->fd);
 
     assert(vq->inuse == 0);
@@ -1337,7 +1340,10 @@ VduseDev *vduse_dev_create(const char *name, uint32_t device_id,
 
     return dev;
 err:
-    ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name);
+    if (ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name)) {
+        fprintf(stderr, "Failed to destroy vduse device %s: %s\n",
+                name, strerror(errno));
+    }
 err_dev:
     close(ctrl_fd);
 err_ctrl:
Signature · 1 line
2.20.1
#11 lore raw

Re: [PATCH 4/4] libvduse: Check the return value of some ioctls

Quoted 28 lines · Xie Yongji

Coverity pointed out (CID 1490222, 1490227) that we called ioctl somewhere without checking the return value. This patch fixes these issues.

Fixes Coverity CID 1490222, 1490227
---
 subprojects/libvduse/libvduse.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
subprojects/libvduse/libvduse.c +4 -1
index 1a5981445c..bf7302c60a 100644
@@ -947,7 +947,10 @@ static void vduse_queue_disable(VduseVirtq *vq)
 
     eventfd.index = vq->index;
     eventfd.fd = VDUSE_EVENTFD_DEASSIGN;
-    ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd);
+    if (ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd)) {
+        fprintf(stderr, "Failed to disable eventfd for vq[%d]: %s\n",
+                vq->index, strerror(errno));
+    }
     close(vq->fd);
 
     assert(vq->inuse == 0);
@@ -1337,7 +1340,10 @@ VduseDev *vduse_dev_create(const char *name, uint32_t device_id,
 

subprojects/libvduse/libvduse.c VduseDev *vduse_dev_create(const char *name, uint32_t device_id, · line 1349

Quoted 10 lines
subprojects/libvduse/libvduse.c +4 -1
     return dev;
 err:
-    ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name);
+    if (ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name)) {
+        fprintf(stderr, "Failed to destroy vduse device %s: %s\n",
+                name, strerror(errno));
+    }
 err_dev:
     close(ctrl_fd);
 err_ctrl:

Both errors are during cleanup that can't fail. The program continues as if they didn't happen. Does the user need to know?

author
#12 lore raw

Re: [PATCH 4/4] libvduse: Check the return value of some ioctls

Quoted 31 lines · Markus Armbruster
Quoted 28 lines · Xie Yongji

Coverity pointed out (CID 1490222, 1490227) that we called ioctl somewhere without checking the return value. This patch fixes these issues.

Fixes Coverity CID 1490222, 1490227
---
 subprojects/libvduse/libvduse.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
subprojects/libvduse/libvduse.c +4 -1
index 1a5981445c..bf7302c60a 100644
@@ -947,7 +947,10 @@ static void vduse_queue_disable(VduseVirtq *vq)
 
     eventfd.index = vq->index;
     eventfd.fd = VDUSE_EVENTFD_DEASSIGN;
-    ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd);
+    if (ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd)) {
+        fprintf(stderr, "Failed to disable eventfd for vq[%d]: %s\n",
+                vq->index, strerror(errno));
+    }
     close(vq->fd);
 
     assert(vq->inuse == 0);
@@ -1337,7 +1340,10 @@ VduseDev *vduse_dev_create(const char *name, uint32_t device_id,
 
Quoted 14 lines

subprojects/libvduse/libvduse.c VduseDev *vduse_dev_create(const char *name, uint32_t device_id, · line 1349

Quoted 10 lines
subprojects/libvduse/libvduse.c +4 -1
     return dev;
 err:
-    ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name);
+    if (ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name)) {
+        fprintf(stderr, "Failed to destroy vduse device %s: %s\n",
+                name, strerror(errno));
+    }
 err_dev:
     close(ctrl_fd);
 err_ctrl:

Both errors are during cleanup that can't fail. The program continues as if they didn't happen. Does the user need to know?

So I printed some error messages. I didn't find any other good way to notify the users.

Thanks,
Yongji

#13 lore raw

Re: [PATCH 4/4] libvduse: Check the return value of some ioctls

Quoted 47 lines · Yongji Xie
Quoted 31 lines · Markus Armbruster
Quoted 28 lines · Xie Yongji

Coverity pointed out (CID 1490222, 1490227) that we called ioctl somewhere without checking the return value. This patch fixes these issues.

Fixes Coverity CID 1490222, 1490227
---
 subprojects/libvduse/libvduse.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
subprojects/libvduse/libvduse.c +4 -1
index 1a5981445c..bf7302c60a 100644
@@ -947,7 +947,10 @@ static void vduse_queue_disable(VduseVirtq *vq)
 
     eventfd.index = vq->index;
     eventfd.fd = VDUSE_EVENTFD_DEASSIGN;
-    ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd);
+    if (ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd)) {
+        fprintf(stderr, "Failed to disable eventfd for vq[%d]: %s\n",
+                vq->index, strerror(errno));
+    }
     close(vq->fd);
 
     assert(vq->inuse == 0);
@@ -1337,7 +1340,10 @@ VduseDev *vduse_dev_create(const char *name, uint32_t device_id,
 
Quoted 14 lines

subprojects/libvduse/libvduse.c VduseDev *vduse_dev_create(const char *name, uint32_t device_id, · line 1349

Quoted 10 lines
subprojects/libvduse/libvduse.c +4 -1
     return dev;
 err:
-    ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name);
+    if (ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name)) {
+        fprintf(stderr, "Failed to destroy vduse device %s: %s\n",
+                name, strerror(errno));
+    }
 err_dev:
     close(ctrl_fd);
 err_ctrl:

Both errors are during cleanup that can't fail. The program continues as if they didn't happen. Does the user need to know?

Quoted 2 lines

So I printed some error messages. I didn't find any other good way to notify the users.

I can think of another way, either. But my question wasn't about "how", it was about "why". The answer depends on the impact of these errors. Which I can't judge. Can you?

author
#14 lore raw

Re: [PATCH 4/4] libvduse: Check the return value of some ioctls

Quoted 50 lines · Markus Armbruster
Quoted 47 lines · Yongji Xie
Quoted 45 lines · Markus Armbruster
Quoted 28 lines · Xie Yongji

Coverity pointed out (CID 1490222, 1490227) that we called ioctl somewhere without checking the return value. This patch fixes these issues.

Fixes Coverity CID 1490222, 1490227
---
 subprojects/libvduse/libvduse.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
subprojects/libvduse/libvduse.c +4 -1
index 1a5981445c..bf7302c60a 100644
@@ -947,7 +947,10 @@ static void vduse_queue_disable(VduseVirtq *vq)
 
     eventfd.index = vq->index;
     eventfd.fd = VDUSE_EVENTFD_DEASSIGN;
-    ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd);
+    if (ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd)) {
+        fprintf(stderr, "Failed to disable eventfd for vq[%d]: %s\n",
+                vq->index, strerror(errno));
+    }
     close(vq->fd);
 
     assert(vq->inuse == 0);
@@ -1337,7 +1340,10 @@ VduseDev *vduse_dev_create(const char *name, uint32_t device_id,
 

subprojects/libvduse/libvduse.c VduseDev *vduse_dev_create(const char *name, uint32_t device_id, · line 1349

Quoted 10 lines
subprojects/libvduse/libvduse.c +4 -1
     return dev;
 err:
-    ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name);
+    if (ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name)) {
+        fprintf(stderr, "Failed to destroy vduse device %s: %s\n",
+                name, strerror(errno));
+    }
 err_dev:
     close(ctrl_fd);
 err_ctrl:

Both errors are during cleanup that can't fail. The program continues as if they didn't happen. Does the user need to know?

Quoted 7 lines
Quoted 2 lines

So I printed some error messages. I didn't find any other good way to notify the users.

I can think of another way, either. But my question wasn't about "how", it was about "why". The answer depends on the impact of these errors. Which I can't judge. Can you?

OK, I get your point. Actually users might have no way to handle those errors. And there is no real impact on users since those errors mean the resources have been cleaned up in other places or by other processes. So I choose to ignore this error, but it triggers a Coverity warning.

Thanks,
Yongji

#15 lore raw

Re: [PATCH 4/4] libvduse: Check the return value of some ioctls

Quoted 59 lines · Yongji Xie
Quoted 50 lines · Markus Armbruster
Quoted 47 lines · Yongji Xie
Quoted 45 lines · Markus Armbruster
Quoted 28 lines · Xie Yongji

Coverity pointed out (CID 1490222, 1490227) that we called ioctl somewhere without checking the return value. This patch fixes these issues.

Fixes Coverity CID 1490222, 1490227
---
 subprojects/libvduse/libvduse.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
subprojects/libvduse/libvduse.c +4 -1
index 1a5981445c..bf7302c60a 100644
@@ -947,7 +947,10 @@ static void vduse_queue_disable(VduseVirtq *vq)
 
     eventfd.index = vq->index;
     eventfd.fd = VDUSE_EVENTFD_DEASSIGN;
-    ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd);
+    if (ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd)) {
+        fprintf(stderr, "Failed to disable eventfd for vq[%d]: %s\n",
+                vq->index, strerror(errno));
+    }
     close(vq->fd);
 
     assert(vq->inuse == 0);
@@ -1337,7 +1340,10 @@ VduseDev *vduse_dev_create(const char *name, uint32_t device_id,
 

subprojects/libvduse/libvduse.c VduseDev *vduse_dev_create(const char *name, uint32_t device_id, · line 1349

Quoted 10 lines
subprojects/libvduse/libvduse.c +4 -1
     return dev;
 err:
-    ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name);
+    if (ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name)) {
+        fprintf(stderr, "Failed to destroy vduse device %s: %s\n",
+                name, strerror(errno));
+    }
 err_dev:
     close(ctrl_fd);
 err_ctrl:

Both errors are during cleanup that can't fail. The program continues as if they didn't happen. Does the user need to know?

Quoted 7 lines
Quoted 2 lines

So I printed some error messages. I didn't find any other good way to notify the users.

I can think of another way, either. But my question wasn't about "how", it was about "why". The answer depends on the impact of these errors. Which I can't judge. Can you?

Quoted 5 lines

OK, I get your point. Actually users might have no way to handle those errors. And there is no real impact on users since those errors mean the resources have been cleaned up in other places or by other processes. So I choose to ignore this error, but it triggers a Coverity warning.

If we genuinely want to ignore errors from ioctl(), we can mark the Coverity complaint as false positive.

author
#16 lore raw

Re: [PATCH 4/4] libvduse: Check the return value of some ioctls

Quoted 62 lines · Markus Armbruster
Quoted 59 lines · Yongji Xie
Quoted 57 lines · Markus Armbruster
Quoted 47 lines · Yongji Xie
Quoted 31 lines · Markus Armbruster
Quoted 28 lines · Xie Yongji

Coverity pointed out (CID 1490222, 1490227) that we called ioctl somewhere without checking the return value. This patch fixes these issues.

Fixes Coverity CID 1490222, 1490227
---
 subprojects/libvduse/libvduse.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
subprojects/libvduse/libvduse.c +4 -1
index 1a5981445c..bf7302c60a 100644
@@ -947,7 +947,10 @@ static void vduse_queue_disable(VduseVirtq *vq)
 
     eventfd.index = vq->index;
     eventfd.fd = VDUSE_EVENTFD_DEASSIGN;
-    ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd);
+    if (ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd)) {
+        fprintf(stderr, "Failed to disable eventfd for vq[%d]: %s\n",
+                vq->index, strerror(errno));
+    }
     close(vq->fd);
 
     assert(vq->inuse == 0);
@@ -1337,7 +1340,10 @@ VduseDev *vduse_dev_create(const char *name, uint32_t device_id,
 
Quoted 14 lines

subprojects/libvduse/libvduse.c VduseDev *vduse_dev_create(const char *name, uint32_t device_id, · line 1349

Quoted 10 lines
subprojects/libvduse/libvduse.c +4 -1
     return dev;
 err:
-    ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name);
+    if (ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name)) {
+        fprintf(stderr, "Failed to destroy vduse device %s: %s\n",
+                name, strerror(errno));
+    }
 err_dev:
     close(ctrl_fd);
 err_ctrl:

Both errors are during cleanup that can't fail. The program continues as if they didn't happen. Does the user need to know?

Quoted 2 lines

So I printed some error messages. I didn't find any other good way to notify the users.

I can think of another way, either. But my question wasn't about "how", it was about "why". The answer depends on the impact of these errors. Which I can't judge. Can you?

Quoted 9 lines
Quoted 5 lines

OK, I get your point. Actually users might have no way to handle those errors. And there is no real impact on users since those errors mean the resources have been cleaned up in other places or by other processes. So I choose to ignore this error, but it triggers a Coverity warning.

If we genuinely want to ignore errors from ioctl(), we can mark the Coverity complaint as false positive.

OK, if so, I think I can drop this patch.

Thanks,
Yongji