Skip to contentSkip to conversation

xen/9pfs: fix two resource leaks on error paths, discovered by Coverity

PATCH Applied Stefano Stabellini 4 participants 13 messages May 9, 2017 – May 16, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 xen/9pfs: fix two resource leaks on error paths, discovered by Coverity

CID: 1374836

---
 hw/9pfs/xen-9p-backend.c | 2 ++
 1 file changed, 2 insertions(+)
hw/9pfs/xen-9p-backend.c +2 -0
index 9c7f41a..a1fdede 100644
@@ -332,12 +332,14 @@ static int xen_9pfs_connect(struct XenDevice *xendev)
         str = g_strdup_printf("ring-ref%u", i);
         if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
                                  &xen_9pdev->rings[i].ref) == -1) {
+            g_free(str);
             goto out;
         }
         g_free(str);
         str = g_strdup_printf("event-channel-%u", i);
         if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
                                  &xen_9pdev->rings[i].evtchn) == -1) {
+            g_free(str);
             goto out;
         }
         g_free(str);
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

PATCH v2 2/3 Check the return value of fcntl in qemu_set_cloexec

Assert that the return value is not an error. This issue was found by Coverity.

CID: 1374831

---
 util/oslib-posix.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
util/oslib-posix.c +3 -1
index 4d9189e..16894ad 100644
@@ -182,7 +182,9 @@ void qemu_set_cloexec(int fd)
 {
     int f;
     f = fcntl(fd, F_GETFD);
-    fcntl(fd, F_SETFD, f | FD_CLOEXEC);
+    assert(f != -1);
+    f = fcntl(fd, F_SETFD, f | FD_CLOEXEC);
+    assert(f != -1);
 }
 
 /*
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v2 2/3
#2 lore raw

Re: [Qemu-devel] [PATCH v2 2/3] Check the return value of fcntl in qemu_set_cloexec

Quoted 5 lines · Stefano Stabellini

Assert that the return value is not an error. This issue was found by Coverity.

CID: 1374831

Quoted 7 lines
---
 util/oslib-posix.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v2 2/3
#3 lore raw

Re: [Qemu-devel] [PATCH v2 2/3] Check the return value of fcntl in qemu_set_cloexec

In reply to Stefano Stabellini
Quoted 5 lines · On Tue, 9 May 2017 12:04:52 -0700 Stefano Stabellini <[email protected]>…

Assert that the return value is not an error. This issue was found by Coverity.

CID: 1374831

Quoted 8 lines
---
 util/oslib-posix.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 5 lines
util/oslib-posix.c +0 -0
index 4d9189e..16894ad 100644

util/oslib-posix.c void qemu_set_cloexec(int fd) · line 190

Quoted 10 lines
util/oslib-posix.c +3 -1
@@ -182,7 +182,9 @@ void qemu_set_cloexec(int fd)
 {
     int f;
     f = fcntl(fd, F_GETFD);
-    fcntl(fd, F_SETFD, f | FD_CLOEXEC);
+    assert(f != -1);
+    f = fcntl(fd, F_SETFD, f | FD_CLOEXEC);
+    assert(f != -1);
 }
 
 /*

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw

Re: [Qemu-devel] [PATCH v2 2/3] Check the return value of fcntl in qemu_set_cloexec

In reply to Stefano Stabellini
Quoted 9 lines · Stefano Stabellini

Assert that the return value is not an error. This issue was found by Coverity.

CID: 1374831

Queued, thanks.

Paolo

Quoted 10 lines
---
 util/oslib-posix.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
util/oslib-posix.c +0 -0
index 4d9189e..16894ad 100644

util/oslib-posix.c void qemu_set_cloexec(int fd) · line 190

Quoted 10 lines
util/oslib-posix.c +3 -1
@@ -182,7 +182,9 @@ void qemu_set_cloexec(int fd)
 {
     int f;
     f = fcntl(fd, F_GETFD);
-    fcntl(fd, F_SETFD, f | FD_CLOEXEC);
+    assert(f != -1);
+    f = fcntl(fd, F_SETFD, f | FD_CLOEXEC);
+    assert(f != -1);
 }
 
 /*

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw

Re: [Qemu-devel] [PATCH v2 2/3] Check the return value of fcntl in qemu_set_cloexec

Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · Stefano Stabellini

Assert that the return value is not an error. This issue was found by Coverity.

Quoted 8 lines
Quoted 6 lines

CID: 1374831

Queued, thanks.

I am about to send a pull request with the rest of the series, but I'll leave this one to you.

Cheers,

Stefano

Quoted 18 lines
Quoted 18 lines
---
 util/oslib-posix.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
util/oslib-posix.c +3 -1
index 4d9189e..16894ad 100644
@@ -182,7 +182,9 @@ void qemu_set_cloexec(int fd)
 {
     int f;
     f = fcntl(fd, F_GETFD);
-    fcntl(fd, F_SETFD, f | FD_CLOEXEC);
+    assert(f != -1);
+    f = fcntl(fd, F_SETFD, f | FD_CLOEXEC);
+    assert(f != -1);
 }
 
Quoted 3 lines

util/oslib-posix.c void qemu_set_cloexec(int fd) · line 190

Quoted 2 lines
util/oslib-posix.c +0 -0
 /*

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#6 lore raw

PATCH v2 3/3 xen: call qemu_set_cloexec instead of fcntl

In reply to Stefano Stabellini

Use the common utility function, which contains checks on return values, instead of manually calling fcntl.

CID: 1374831

---
 hw/9pfs/xen-9p-backend.c | 2 +-
 hw/xen/xen_backend.c     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/9pfs/xen-9p-backend.c +1 -1
index a1fdede..5df97c9 100644
@@ -380,7 +380,7 @@ static int xen_9pfs_connect(struct XenDevice *xendev)
         if (xen_9pdev->rings[i].evtchndev == NULL) {
             goto out;
         }
-        fcntl(xenevtchn_fd(xen_9pdev->rings[i].evtchndev), F_SETFD, FD_CLOEXEC);
+        qemu_set_cloexec(xenevtchn_fd(xen_9pdev->rings[i].evtchndev));
         xen_9pdev->rings[i].local_port = xenevtchn_bind_interdomain
                                             (xen_9pdev->rings[i].evtchndev,
                                              xendev->dom,
hw/xen/xen_backend.c +1 -1
index c85f163..2cac47d 100644
@@ -147,7 +147,7 @@ static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
         qdev_unplug(DEVICE(xendev), NULL);
         return NULL;
     }
-    fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
+    qemu_set_cloexec(xenevtchn_fd(xendev->evtchndev));
 
     if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
         xendev->gnttabdev = xengnttab_open(NULL, 0);
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v2 3/3
#7 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] xen: call qemu_set_cloexec instead of fcntl

Quoted 10 lines · Stefano Stabellini

Use the common utility function, which contains checks on return values, instead of manually calling fcntl.

CID: 1374831

Quoted 5 lines
---
 hw/9pfs/xen-9p-backend.c | 2 +-
 hw/xen/xen_backend.c     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v2 3/3
#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] xen: call qemu_set_cloexec instead of fcntl

In reply to Stefano Stabellini
Quoted 1 line · On Tue, 9 May 2017 12:04:53 -0700 Stefano Stabellini <[email protected]>…

Use the common utility function, which contains checks on return values,

... and first calls F_GETFD as recommended by POSIX.1-2001.

http://pubs.opengroup.org/onlinepubs/009695399/functions/fcntl.html

"The arg values to F_GETFD, F_SETFD, F_GETFL, and F_SETFL all represent flag values to allow for future growth. Applications using these functions should do a read-modify-write operation on them, rather than assuming that only the values defined by this volume of IEEE Std 1003.1-2001 are valid. It is a common error to forget this, particularly in the case of F_SETFD."

Quoted 9 lines

instead of manually calling fcntl.

CID: 1374831

Quoted 5 lines
---
 hw/9pfs/xen-9p-backend.c | 2 +-
 hw/xen/xen_backend.c     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

Anyway,

Quoted 19 lines
hw/9pfs/xen-9p-backend.c +1 -1
index a1fdede..5df97c9 100644
@@ -380,7 +380,7 @@ static int xen_9pfs_connect(struct XenDevice *xendev)
         if (xen_9pdev->rings[i].evtchndev == NULL) {
             goto out;
         }
-        fcntl(xenevtchn_fd(xen_9pdev->rings[i].evtchndev), F_SETFD,
 FD_CLOEXEC);
+        qemu_set_cloexec(xenevtchn_fd(xen_9pdev->rings[i].evtchndev));
         xen_9pdev->rings[i].local_port = xenevtchn_bind_interdomain
                                             (xen_9pdev->rings[i].evtchndev,
                                              xendev->dom,
hw/xen/xen_backend.c +0 -0
index c85f163..2cac47d 100644

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char · line 153

Quoted 8 lines
hw/xen/xen_backend.c +1 -1
@@ -147,7 +147,7 @@ static struct XenDevice *xen_be_get_xendev(const char
 *type, int dom, int dev, qdev_unplug(DEVICE(xendev), NULL);
         return NULL;
     }
-    fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
+    qemu_set_cloexec(xenevtchn_fd(xendev->evtchndev));
 
     if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
         xendev->gnttabdev = xengnttab_open(NULL, 0);

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v2 1/3
#9 lore raw
In reply to Stefano Stabellini
Quoted 9 lines · Stefano Stabellini

CID: 1374836

---
 hw/9pfs/xen-9p-backend.c | 2 ++
 1 file changed, 2 insertions(+)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#10 lore raw
Quoted 11 lines · Eric Blake
Quoted 9 lines · Stefano Stabellini

CID: 1374836

---
 hw/9pfs/xen-9p-backend.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 1 line

By the way, you forgot to send a 0/3 cover letter (at least, there was no In-Reply-To: header in your 1/3 mail). That makes it harder to automate handling of your series; I was going to reply to the series as a whole.

While it is inconvenient for human readers, it is even worse for some of the automated patch tooling we have that expects cover letters for any multi-patch series. More patch submission tips at
http://wiki.qemu.org/Contribute/SubmitAPatch include how to use 'git config' to automate the creation of a cover letter.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#11 lore raw
Quoted 19 lines · Eric Blake
Quoted 11 lines · Eric Blake
Quoted 9 lines · Stefano Stabellini

CID: 1374836

---
 hw/9pfs/xen-9p-backend.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 1 line

By the way, you forgot to send a 0/3 cover letter (at least, there was no In-Reply-To: header in your 1/3 mail). That makes it harder to automate handling of your series; I was going to reply to the series as a whole.

Quoted 5 lines

While it is inconvenient for human readers, it is even worse for some of the automated patch tooling we have that expects cover letters for any multi-patch series. More patch submission tips at
http://wiki.qemu.org/Contribute/SubmitAPatch include how to use 'git config' to automate the creation of a cover letter.

Sorry about that, and thanks for the review.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v2 1/3
#12 lore raw
In reply to Stefano Stabellini
Quoted 15 lines · On Tue, 9 May 2017 12:04:51 -0700 Stefano Stabellini <[email protected]>…

CID: 1374836

---
 hw/9pfs/xen-9p-backend.c | 2 ++
 1 file changed, 2 insertions(+)
hw/9pfs/xen-9p-backend.c +0 -0
index 9c7f41a..a1fdede 100644

hw/9pfs/xen-9p-backend.c static int xen_9pfs_connect(struct XenDevice *xendev) · line 338

Quoted 7 lines
hw/9pfs/xen-9p-backend.c +1 -0
@@ -332,12 +332,14 @@ static int xen_9pfs_connect(struct XenDevice *xendev)
         str = g_strdup_printf("ring-ref%u", i);
         if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
                                  &xen_9pdev->rings[i].ref) == -1) {
+            g_free(str);
             goto out;
         }
         g_free(str);

I would rather do something like:

    int ret;

    [...]

        str = g_strdup_printf("ring-ref%u", i);
        ret = xenstore_read_fe_int(&xen_9pdev->xendev, str,
                                   &xen_9pdev->rings[i].ref);
        g_free(str);
        if (ret ==  -1) {
            goto out;
        }

but this is a matter of taste and you own this code so:

hw/9pfs/xen-9p-backend.c static int xen_9pfs_connect(struct XenDevice *xendev) · line 345

Quoted 7 lines
hw/9pfs/xen-9p-backend.c +1 -0
         str = g_strdup_printf("event-channel-%u", i);
         if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
                                  &xen_9pdev->rings[i].evtchn) == -1) {
+            g_free(str);
             goto out;
         }
         g_free(str);

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel