Skip to contentSkip to conversation

tools/libxl/libxl_qmp.c: Close fds on error path in qmp_open

PATCH In review PRAMOD DEVENDRA 3 participants 3 messages Mar 18, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 tools/libxl/libxl_qmp.c: Close fds on error path in qmp_open

From: Pramod Devendra <[email protected]>

CC Ian Jackson <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
CC Wei Liu <[email protected]>
---
 tools/libxl/libxl_qmp.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/libxl/libxl_qmp.c +14 -3
index c7324e6..992c1ee 100644
@@ -365,14 +365,24 @@ static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
         return -1;
     }
     ret = libxl_fd_set_nonblock(qmp->ctx, qmp->qmp_fd, 1);
-    if (ret) return -1;
+    if (ret) {
+        close(qmp->qmp_fd);
+        return -1;
+    }
     ret = libxl_fd_set_cloexec(qmp->ctx, qmp->qmp_fd, 1);
-    if (ret) return -1;
+    if (ret) {
+        close(qmp->qmp_fd);
+        return -1;
+    }
 
+    if(sizeof (qmp->addr.sun_path) <= strlen(qmp_socket_path)) {
+        close(qmp->qmp_fd);
+        return -1;
+    }
     memset(&qmp->addr, 0, sizeof (qmp->addr));
     qmp->addr.sun_family = AF_UNIX;
     strncpy(qmp->addr.sun_path, qmp_socket_path,
-            sizeof (qmp->addr.sun_path));
+            sizeof (qmp->addr.sun_path)-1);
 
     do {
         ret = connect(qmp->qmp_fd, (struct sockaddr *) &qmp->addr,
@@ -384,6 +394,7 @@ static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
              * ECONNREFUSED : Leftover socket hasn't been removed yet */
             continue;
         }
+        close(qmp->qmp_fd);
         return -1;
     } while ((++i / 5 <= timeout) && (usleep(200 * 1000) <= 0));
 
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 7 lines · PRAMOD DEVENDRA

From: Pramod Devendra <[email protected]>

CC Ian Jackson <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
CC Wei Liu <[email protected]>

Thanks.

The patch content looks good.

However the patch title and commit message are not descriptive enough.

Can you change the title to:

libxl/libxl_qmp.c: fix qmp_open

And then in commit message:

This patch does following things:

1. Make sure sun_path does not overflow.
2. Close qmp_fd on error.

For more general information on patch submission, please refer to

http://wiki.xenproject.org/wiki/Submitting_Xen_Project_Patches

Wei.

Quoted 46 lines
---
 tools/libxl/libxl_qmp.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/libxl/libxl_qmp.c +14 -3
index c7324e6..992c1ee 100644
@@ -365,14 +365,24 @@ static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
         return -1;
     }
     ret = libxl_fd_set_nonblock(qmp->ctx, qmp->qmp_fd, 1);
-    if (ret) return -1;
+    if (ret) {
+        close(qmp->qmp_fd);
+        return -1;
+    }
     ret = libxl_fd_set_cloexec(qmp->ctx, qmp->qmp_fd, 1);
-    if (ret) return -1;
+    if (ret) {
+        close(qmp->qmp_fd);
+        return -1;
+    }
 
+    if(sizeof (qmp->addr.sun_path) <= strlen(qmp_socket_path)) {
+        close(qmp->qmp_fd);
+        return -1;
+    }
     memset(&qmp->addr, 0, sizeof (qmp->addr));
     qmp->addr.sun_family = AF_UNIX;
     strncpy(qmp->addr.sun_path, qmp_socket_path,
-            sizeof (qmp->addr.sun_path));
+            sizeof (qmp->addr.sun_path)-1);
 
     do {
         ret = connect(qmp->qmp_fd, (struct sockaddr *) &qmp->addr,
@@ -384,6 +394,7 @@ static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
              * ECONNREFUSED : Leftover socket hasn't been removed yet */
             continue;
         }
+        close(qmp->qmp_fd);
         return -1;
     } while ((++i / 5 <= timeout) && (usleep(200 * 1000) <= 0));
 
Signature · 1 line
1.7.10.4
#2 lore raw
Quoted 9 lines · Wei Liu
Quoted 7 lines · PRAMOD DEVENDRA

From: Pramod Devendra <[email protected]>

CC Ian Jackson <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
CC Wei Liu <[email protected]>
Quoted 3 lines

Thanks.

The patch content looks good.

Actually I think it should use the goto out error handling style, rather than closing the fd on every single path.

i.e. the tail should be:

out:
if (ret == -1 && qmp->qmp_fd > -1) close(qmp->qmp_fd);

return ret;

and ret should be initialised to -1 at the top, and each return should be a goto.