Skip to contentSkip to conversation

vhost-user-scsi: avoid unlink(NULL) with fd passing

PATCH Superseded Stefan Hajnoczi 5 participants 6 messages Apr 27, 2022 – May 16, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost-user-scsi: avoid unlink(NULL) with fd passing

Commit 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi") introduced fd-passing support as part of implementing the vhost-user backend program conventions.

When fd passing is used the UNIX domain socket path is NULL and we must not call unlink(2).

Fixes Coverity CID 1488353
Fixes 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi")
---
 contrib/vhost-user-scsi/vhost-user-scsi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
contrib/vhost-user-scsi/vhost-user-scsi.c +3 -1
index b2c0f98253..08335d4b2b 100644
@@ -433,7 +433,9 @@ out:
     if (vdev_scsi) {
         g_main_loop_unref(vdev_scsi->loop);
         g_free(vdev_scsi);
-        unlink(opt_socket_path);
+        if (opt_socket_path) {
+            unlink(opt_socket_path);
+        }
     }
     if (csock >= 0) {
         close(csock);
Signature · 1 line
2.35.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Stefan Hajnoczi

Commit 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi") introduced fd-passing support as part of implementing the vhost-user backend program conventions.

When fd passing is used the UNIX domain socket path is NULL and we must not call unlink(2).

Fixes Coverity CID 1488353
Fixes 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi")
Quoted 22 lines
---
 contrib/vhost-user-scsi/vhost-user-scsi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
contrib/vhost-user-scsi/vhost-user-scsi.c +3 -1
index b2c0f98253..08335d4b2b 100644
@@ -433,7 +433,9 @@ out:
     if (vdev_scsi) {
         g_main_loop_unref(vdev_scsi->loop);
         g_free(vdev_scsi);
-        unlink(opt_socket_path);
+        if (opt_socket_path) {
+            unlink(opt_socket_path);
+        }
     }
     if (csock >= 0) {
         close(csock);
Signature · 1 line
2.35.1
Reviewed-by Philippe Mathieu-Daudé on v1 1/1
#2 lore raw
In reply to Stefan Hajnoczi
Quoted 7 lines · Stefan Hajnoczi

Commit 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi") introduced fd-passing support as part of implementing the vhost-user backend program conventions.

When fd passing is used the UNIX domain socket path is NULL and we must not call unlink(2).

Quoted 6 lines
Fixes Coverity CID 1488353
Fixes 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi")
---
  contrib/vhost-user-scsi/vhost-user-scsi.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
#3 lore raw
In reply to Stefan Hajnoczi
Quoted 20 lines · Stefan Hajnoczi

Commit 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi") introduced fd-passing support as part of implementing the vhost-user backend program conventions.

When fd passing is used the UNIX domain socket path is NULL and we must not call unlink(2).

Fixes Coverity CID 1488353
Fixes 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi")
---
 contrib/vhost-user-scsi/vhost-user-scsi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
contrib/vhost-user-scsi/vhost-user-scsi.c +0 -0
index b2c0f98253..08335d4b2b 100644

contrib/vhost-user-scsi/vhost-user-scsi.c out: · line 439

Quoted 8 lines
contrib/vhost-user-scsi/vhost-user-scsi.c +3 -1
@@ -433,7 +433,9 @@ out:
     if (vdev_scsi) {
         g_main_loop_unref(vdev_scsi->loop);
         g_free(vdev_scsi);
-        unlink(opt_socket_path);
+        if (opt_socket_path) {
+            unlink(opt_socket_path);
+        }
     }

Shouldn't this check-and-unlink be one level up, outside the "if (vdev_scsi)" ? There are error exit paths which get us to the 'out:' label where we have called unix_sock_new() but not yet done the g_new0() of vdev_scsi(). The only thing that needs to be guarded by "if (vdev_scsi)" is the g_main_loop_unref() (the g_free of vdev_scsi itself could be inside or outside, since g_free(NULL) is a nop).

thanks
-- PMM

#4 lore raw
Quoted 38 lines · Peter Maydell
Quoted 20 lines · Stefan Hajnoczi

Commit 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi") introduced fd-passing support as part of implementing the vhost-user backend program conventions.

When fd passing is used the UNIX domain socket path is NULL and we must not call unlink(2).

Fixes Coverity CID 1488353
Fixes 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi")
---
 contrib/vhost-user-scsi/vhost-user-scsi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
contrib/vhost-user-scsi/vhost-user-scsi.c +0 -0
index b2c0f98253..08335d4b2b 100644

contrib/vhost-user-scsi/vhost-user-scsi.c out: · line 439

Quoted 8 lines
contrib/vhost-user-scsi/vhost-user-scsi.c +3 -1
@@ -433,7 +433,9 @@ out:
     if (vdev_scsi) {
         g_main_loop_unref(vdev_scsi->loop);
         g_free(vdev_scsi);
-        unlink(opt_socket_path);
+        if (opt_socket_path) {
+            unlink(opt_socket_path);
+        }
     }

Shouldn't this check-and-unlink be one level up, outside the "if (vdev_scsi)" ? There are error exit paths which get us to the 'out:' label where we have called unix_sock_new() but not yet done the g_new0() of vdev_scsi(). The only thing that needs to be guarded by "if (vdev_scsi)" is the g_main_loop_unref() (the g_free of vdev_scsi itself could be inside or outside, since g_free(NULL) is a nop).

Quoted 2 lines

thanks
-- PMM

Stefan, want to respond?

Signature · 1 line
MST
#5 lore raw
In reply to Peter Maydell
Quoted 30 lines · Peter Maydell
Quoted 20 lines · Stefan Hajnoczi

Commit 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi") introduced fd-passing support as part of implementing the vhost-user backend program conventions.

When fd passing is used the UNIX domain socket path is NULL and we must not call unlink(2).

Fixes Coverity CID 1488353
Fixes 747421e949fc1eb3ba66b5fcccdb7ba051918241 ("Implements Backend Program conventions for vhost-user-scsi")
---
 contrib/vhost-user-scsi/vhost-user-scsi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
contrib/vhost-user-scsi/vhost-user-scsi.c +0 -0
index b2c0f98253..08335d4b2b 100644

contrib/vhost-user-scsi/vhost-user-scsi.c out: · line 439

Quoted 8 lines
contrib/vhost-user-scsi/vhost-user-scsi.c +3 -1
@@ -433,7 +433,9 @@ out:
     if (vdev_scsi) {
         g_main_loop_unref(vdev_scsi->loop);
         g_free(vdev_scsi);
-        unlink(opt_socket_path);
+        if (opt_socket_path) {
+            unlink(opt_socket_path);
+        }
     }
Quoted 7 lines

Shouldn't this check-and-unlink be one level up, outside the "if (vdev_scsi)" ? There are error exit paths which get us to the 'out:' label where we have called unix_sock_new() but not yet done the g_new0() of vdev_scsi(). The only thing that needs to be guarded by "if (vdev_scsi)" is the g_main_loop_unref() (the g_free of vdev_scsi itself could be inside or outside, since g_free(NULL) is a nop).

Sorry, I was offline last week due to illness. Now I'm back and agree with what you found. I have sent a v2.

Stefan