All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ian Jackson <ian.jackson@eu.citrix.com>
To: xen-devel@lists.xensource.com
Cc: Ian Jackson <Ian.Jackson@eu.citrix.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>
Subject: [PATCH 2/6] libxl: fix error handling (xenstore transaction leak) in libxl__domain_make
Date: Thu, 27 Jan 2011 17:41:00 +0000	[thread overview]
Message-ID: <1296150064-31991-3-git-send-email-ian.jackson@eu.citrix.com> (raw)
In-Reply-To: <1296150064-31991-2-git-send-email-ian.jackson@eu.citrix.com>

libxl__domain_make could under some circumstances leak the xenstore
transaction (stored in the variable t).  Also, failures to commit the
xenstore transaction for reasons other than EAGAIN would be ignored (!)

Fix this as follows:

  * Initialise t to 0 (not a valid transaction id), and when the
    transaction is successfully committed or rolled back, reset it.

  * Change all the instances of:       libxl__free_all(&gc); return error;
    to instead do:                     rc=error;  goto out;

  * Use the out stanza for exiting, setting rc=0 on success first.

  * Explicitly abort the transaction in the out stanza.

Also add a note by the calls manipulating the gc, to note that as this
is an internal function, the gc should really be set up and destroyed
by its caller.  But let's not do that at this stage of the 4.1 release
cycle.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
---
 tools/libxl/libxl_create.c |   49 ++++++++++++++++++++++++++------------------
 1 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index e2bd8d0..0d0c84f 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -284,7 +284,7 @@ out:
 int libxl__domain_make(libxl_ctx *ctx, libxl_domain_create_info *info,
                        uint32_t *domid)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl__gc gc = LIBXL_INIT_GC(ctx); /* fixme: should be done by caller */
     int flags, ret, i, rc;
     char *uuid_string;
     char *rw_paths[] = { "device", "device/suspend/event-channel" , "data"};
@@ -293,13 +293,13 @@ int libxl__domain_make(libxl_ctx *ctx, libxl_domain_create_info *info,
     char *dom_path, *vm_path;
     struct xs_permissions roperm[2];
     struct xs_permissions rwperm[1];
-    xs_transaction_t t;
+    xs_transaction_t t = 0;
     xen_domain_handle_t handle;
 
     uuid_string = libxl__uuid2string(&gc, info->uuid);
     if (!uuid_string) {
-        libxl__free_all(&gc);
-        return ERROR_NOMEM;
+        rc = ERROR_NOMEM;
+        goto out;
     }
 
     flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest : 0;
@@ -313,28 +313,28 @@ int libxl__domain_make(libxl_ctx *ctx, libxl_domain_create_info *info,
     ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
     if (ret < 0) {
         LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, ret, "domain creation fail");
-        libxl__free_all(&gc);
-        return ERROR_FAIL;
+        rc = ERROR_FAIL;
+        goto out;
     }
 
     ret = xc_cpupool_movedomain(ctx->xch, info->poolid, *domid);
     if (ret < 0) {
         LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, ret, "domain move fail");
-        libxl__free_all(&gc);
-        return ERROR_FAIL;
+        rc = ERROR_FAIL;
+        goto out;
     }
 
     dom_path = libxl__xs_get_dompath(&gc, *domid);
     if (!dom_path) {
-        libxl__free_all(&gc);
-        return ERROR_FAIL;
+        rc = ERROR_FAIL;
+        goto out;
     }
 
     vm_path = libxl__sprintf(&gc, "/vm/%s", uuid_string);
     if (!vm_path) {
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "cannot allocate create paths");
-        libxl__free_all(&gc);
-        return ERROR_FAIL;
+        rc = ERROR_FAIL;
+        goto out;
     }
 
     roperm[0].id = 0;
@@ -356,10 +356,8 @@ retry_transaction:
 
     xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/vm", dom_path), vm_path, strlen(vm_path));
     rc = libxl_domain_rename(ctx, *domid, 0, info->name, t);
-    if (rc) {
-        libxl__free_all(&gc);
-        return rc;
-    }
+    if (rc)
+        goto out;
 
     for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
         char *path = libxl__sprintf(&gc, "%s/%s", dom_path, rw_paths[i]);
@@ -381,12 +379,23 @@ retry_transaction:
     libxl__xs_writev(&gc, t, libxl__sprintf(&gc, "%s/platform", dom_path), info->platformdata);
 
     xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
-    if (!xs_transaction_end(ctx->xsh, t, 0))
-        if (errno == EAGAIN)
+    if (!xs_transaction_end(ctx->xsh, t, 0)) {
+        if (errno == EAGAIN) {
+            t = 0;
             goto retry_transaction;
+        }
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "domain creation "
+                         "xenstore transaction commit failed");
+        rc = ERROR_FAIL;
+        goto out;
+    }
+    t = 0;
 
-    libxl__free_all(&gc);
-    return 0;
+    rc = 0;
+ out:
+    if (t) xs_transaction_end(ctx->xsh, t, 1);
+    libxl__free_all(&gc); /* fixme: should be done by caller */
+    return rc;
 }
 
 static int do_domain_create(libxl_ctx *ctx, libxl_domain_config *d_config,
-- 
1.5.6.5

  reply	other threads:[~2011-01-27 17:41 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-27 17:40 [PATCH 0/6] libxl, xl: fix domain name uniqueness check Ian Jackson
2011-01-27 17:40 ` [PATCH 1/6] xl: Revert "xl: avoid creating domains with duplicate names" Ian Jackson
2011-01-27 17:41   ` Ian Jackson [this message]
2011-01-27 17:41     ` [PATCH 3/6] libxl, xl: fixes to domain creation cleanup logic (domid values) Ian Jackson
2011-01-27 17:41       ` [PATCH 4/6] libxl: internals: document the error behaviour of various libxl__xs_* functions Ian Jackson
2011-01-27 17:41         ` [PATCH 5/6] libxl: during domain destruction, do not complain if no devices dir to destroy Ian Jackson
2011-01-27 17:41           ` [PATCH 6/6] libxl: prevent creation of domains with duplicate names Ian Jackson
2011-01-27 18:33       ` [PATCH 3/6] libxl, xl: fixes to domain creation cleanup logic (domid values) Stefano Stabellini
2011-01-27 18:59         ` Ian Jackson
2011-01-27 20:01           ` Ian Jackson
2011-01-27 20:13             ` Gianni Tedesco
2011-01-28 12:22             ` Stefano Stabellini
2011-01-28 12:28               ` Ian Jackson
2011-01-28 13:27                 ` Stefano Stabellini
2011-01-28 17:38                   ` Ian Jackson
2011-01-28 17:52                     ` Stefano Stabellini
2011-01-28 18:39                       ` Ian Jackson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1296150064-31991-3-git-send-email-ian.jackson@eu.citrix.com \
    --to=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.