Skip to contentSkip to conversation

xl: free config_data on error in domain_create

PATCH In review Ian Campbell 2 participants 8 messages Feb 17, 2016 – Feb 19, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 xl: free config_data on error in domain_create

CID: 1055898

---
 tools/libxl/xl_cmdimpl.c | 2 ++
 1 file changed, 2 insertions(+)
tools/libxl/xl_cmdimpl.c +2 -0
index d5a397a..e819ee6 100644
@@ -2795,6 +2795,7 @@ static uint32_t create_domain(struct domain_create *dom_info)
         if (!restoring && extra_config && strlen(extra_config)) {
             if (config_len > INT_MAX - (strlen(extra_config) + 2 + 1)) {
                 fprintf(stderr, "Failed to attach extra configuration\n");
+                free(config_data);
                 return ERROR_FAIL;
             }
             /* allocate space for the extra config plus two EOLs plus \0 */
@@ -6525,6 +6526,7 @@ int main_networkattach(int argc, char **argv)
     }
 
     xlu_cfg_destroy(config);
+    config = NULL;
 
     if (argc > 0) return 1; /* Parse error above */
 
Signature · 1 line
2.1.4
#1 lore raw

PATCH 2/3 xl: use xrealloc in domain create

Using bare realloc risks leaking the old pointer if the realloc fails.

Since xrealloc exits on such failures, drop the error handling.

Noticed while fixing, but not related to, CID 1055898.

---
 tools/libxl/xl_cmdimpl.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
tools/libxl/xl_cmdimpl.c +1 -5
index e819ee6..7ba40c0 100644
@@ -2799,12 +2799,8 @@ static uint32_t create_domain(struct domain_create *dom_info)
                 return ERROR_FAIL;
             }
             /* allocate space for the extra config plus two EOLs plus \0 */
-            config_data = realloc(config_data, config_len
+            config_data = xrealloc(config_data, config_len
                 + strlen(extra_config) + 2 + 1);
-            if (!config_data) {
-                fprintf(stderr, "Failed to realloc config_data\n");
-                return ERROR_FAIL;
-            }
             config_len += sprintf(config_data + config_len, "\n%s\n",
                 extra_config);
         }
Signature · 1 line
2.1.4
#2 lore raw

PATCH 3/3 xl: create: close restore_fd_to_close on error

Currently the fd is opened and then later closed and restore_fd_to_close set back to -1, however there are several goto out and goto error_out paths in the interim.

Since the code resets restore_fd_to_close to -1 it is OK to check this and close on the out path too.

CID: 1055897

---
 tools/libxl/xl_cmdimpl.c | 7 +++++++
 1 file changed, 7 insertions(+)
tools/libxl/xl_cmdimpl.c +7 -0
index 7ba40c0..514f5a8 100644
@@ -3069,6 +3069,13 @@ error_out:
     }
 
 out:
+    if (restore_fd_to_close >= 0) {
+        if (close(restore_fd_to_close))
+            fprintf(stderr, "Failed to close restoring file, fd %d, errno %d\n",
+                    restore_fd_to_close, errno);
+        restore_fd_to_close = -1;
+    }
+
     if (logfile != 2)
         close(logfile);
 
Signature · 1 line
2.1.4
#5 lore raw
In reply to Ian Campbell

Ian Campbell writes ("[PATCH 1/3] xl: free config_data on error in domain_create"):

Quoted 1 line

CID: 1055898

...

tools/libxl/xl_cmdimpl.c static uint32_t create_domain(struct domain_create *dom_info) · line 2799

Quoted 10 lines
tools/libxl/xl_cmdimpl.c +1 -0
index d5a397a..e819ee6 100644
@@ -2795,6 +2795,7 @@ static uint32_t create_domain(struct domain_create *dom_info)
         if (!restoring && extra_config && strlen(extra_config)) {
             if (config_len > INT_MAX - (strlen(extra_config) + 2 + 1)) {
                 fprintf(stderr, "Failed to attach extra configuration\n");
+                free(config_data);
                 return ERROR_FAIL;

This LGTM.

tools/libxl/xl_cmdimpl.c int main_networkattach(int argc, char **argv) · line 6531

Quoted 8 lines
tools/libxl/xl_cmdimpl.c +1 -0
             /* allocate space for the extra config plus two EOLs plus \0 */
@@ -6525,6 +6526,7 @@ int main_networkattach(int argc, char **argv)
     }
 
     xlu_cfg_destroy(config);
+    config = NULL;
 
     if (argc > 0) return 1; /* Parse error above */

But this doesn't apply and the current code in main_networkattach doesn't seem to have a bug that something this would fix.

Ian.

Acked-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] xl: use xrealloc in domain create

In reply to Ian Campbell

Ian Campbell writes ("[PATCH 2/3] xl: use xrealloc in domain create"):

Quoted 7 lines

Using bare realloc risks leaking the old pointer if the realloc fails.

Since xrealloc exits on such failures, drop the error handling.

Noticed while fixing, but not related to, CID 1055898.

And queued for push.

Ian.

Acked-by on v1 3/3
#3 lore raw

Re: [PATCH 3/3] xl: create: close restore_fd_to_close on error

In reply to Ian Campbell

Ian Campbell writes ("[PATCH 3/3] xl: create: close restore_fd_to_close on error"):

Quoted 8 lines

Currently the fd is opened and then later closed and restore_fd_to_close set back to -1, however there are several goto out and goto error_out paths in the interim.

Since the code resets restore_fd_to_close to -1 it is OK to check this and close on the out path too.

CID: 1055897

And queued for push.

Ian.

#6 lore raw
In reply to Ian Jackson
Quoted 20 lines · Ian Jackson

Ian Campbell writes ("[PATCH 1/3] xl: free config_data on error in domain_create"):

Quoted 1 line

CID: 1055898

...

Quoted 9 lines
tools/libxl/xl_cmdimpl.c +0 -0
index d5a397a..e819ee6 100644
@@ -2795,6 +2795,7 @@ static uint32_t create_domain(struct
 domain_create *dom_info)
          if (!restoring && extra_config && strlen(extra_config)) {
              if (config_len > INT_MAX - (strlen(extra_config) + 2 + 1))
 {
Quoted 4 lines

                 fprintf(stderr, "Failed to attach extra configuration\n");
+                free(config_data);
                 return ERROR_FAIL;

This LGTM.

Quoted 12 lines
Quoted 9 lines

             /* allocate space for the extra config plus two EOLs plus \0 */

tools/libxl/xl_cmdimpl.c +0 -0
@@ -6525,6 +6526,7 @@ int main_networkattach(int argc, char **argv)
      }


     xlu_cfg_destroy(config);
+    config = NULL;

     if (argc > 0) return 1; /* Parse error above */

But this doesn't apply and the current code in main_networkattach doesn't seem to have a bug that something this would fix.

Looks like a stray line which should have been in "xl: network-attach: free config object after use", sorry.

I'll wait for you to push the batch you have in hand and then rebase fixing this as I go.

Ian.

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

#7 lore raw
Quoted 14 lines · Ian Campbell

             /* allocate space for the extra config plus two EOLs

Quoted 4 lines
Quoted 4 lines

plus
\0 */

(quoted diff) +0 -0
@@ -6525,6 +6526,7 @@ int main_networkattach(int argc, char **argv)
      }
Quoted 8 lines
Quoted 5 lines


     xlu_cfg_destroy(config);
+    config = NULL;

     if (argc > 0) return 1; /* Parse error above */

But this doesn't apply and the current code in main_networkattach doesn't seem to have a bug that something this would fix.

Quoted 2 lines

Looks like a stray line which should have been in "xl: network-attach: free config object after use", sorry.

Which I see I somehow did send out!

Quoted 2 lines

I'll wait for you to push the batch you have in hand and then rebase fixing this as I go.

I'll still take this approach.

Ian.

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