Skip to contentSkip to conversation

Prevent xc_domain_restore() returning success despite errors

PATCH In review Andrew Cooper 3 participants 13 messages Jan 27, 2014 – Feb 6, 2014

lore
Share & tools
lore
#0 lore raw

cover Prevent xc_domain_restore() returning success despite errors

When looking through the XenServer patch queue, I noticed two bugfixes which really should make their way upstream.

Both of these errors have been discovered by xc_domain_restore() returning success after suffering a fatal error during migration, leading to the toolstack believing that the VM migrated successfully.

Regarding 4.4:

I know this is quite late in the 4.4 cycle, and I apologise for not noticing and upstreaming earlier, but I believe that these two fixes should be considered for inclusion into 4.4.

They are both real errors found by XenRT causing mismanagement of migrated domains. Both patches are small, concise, and (I believe) well explained.

The the use of various '*rc' variables can be easily viewed using grep "rc " xc_domain_restore.c. The risks are that I have made a mistake which could result in further migration errors. However, that risk is mitigated by functionally-similar fixes being present in XenServer, and hopefully from the obvious nature of the patches. Futhermore, the changes themselves are for error paths.

On the other hand, if they are deemed too risky (or buggy given review), it will not be the end of the world if not included, although I hope that is not the case.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC George Dunlap <[email protected]>
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 1/2 tools/libxc: goto correct label on error paths

Both of these "goto finish;" statements are actually errors, and need to "goto out;" instead, which will correctly destroy the domain and return an error, rather than trying to finish the migration (and in at least one scenario, return success).

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC George Dunlap <[email protected]>
---
 tools/libxc/xc_domain_restore.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/libxc/xc_domain_restore.c +2 -2
index ca2fb51..5ba47d7 100644
@@ -1778,14 +1778,14 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
 
     if ( pagebuf_get(xch, ctx, &pagebuf, io_fd, dom) ) {
         PERROR("error when buffering batch, finishing");
-        goto finish;
+        goto out;
     }
     memset(&tmptail, 0, sizeof(tmptail));
     tmptail.ishvm = hvm;
     if ( buffer_tail(xch, ctx, &tmptail, io_fd, max_vcpu_id, vcpumap,
                      ext_vcpucontext, vcpuextstate, vcpuextstate_size) < 0 ) {
         ERROR ("error buffering image tail, finishing");
-        goto finish;
+        goto out;
     }
     tailbuf_free(&tailbuf);
     memcpy(&tailbuf, &tmptail, sizeof(tailbuf));
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [Patch 1/2] tools/libxc: goto correct label on error paths

Quoted 9 lines · Andrew Cooper

Both of these "goto finish;" statements are actually errors, and need to "goto out;" instead, which will correctly destroy the domain and return an error, rather than trying to finish the migration (and in at least one scenario, return success).

CC Ian Campbell<[email protected]>
CC Ian Jackson<[email protected]>
CC George Dunlap<[email protected]>

Right -- I can't imagine any goodness coming from jumping to "finish" in those cases...

Quoted 25 lines
---
  tools/libxc/xc_domain_restore.c |    4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/libxc/xc_domain_restore.c +2 -2
index ca2fb51..5ba47d7 100644
@@ -1778,14 +1778,14 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
 
      if ( pagebuf_get(xch, ctx, &pagebuf, io_fd, dom) ) {
          PERROR("error when buffering batch, finishing");
-        goto finish;
+        goto out;
      }
      memset(&tmptail, 0, sizeof(tmptail));
      tmptail.ishvm = hvm;
      if ( buffer_tail(xch, ctx, &tmptail, io_fd, max_vcpu_id, vcpumap,
                       ext_vcpucontext, vcpuextstate, vcpuextstate_size) < 0 ) {
          ERROR ("error buffering image tail, finishing");
-        goto finish;
+        goto out;
      }
      tailbuf_free(&tailbuf);
      memcpy(&tailbuf, &tmptail, sizeof(tailbuf));
#3 lore raw

Re: [Patch 1/2] tools/libxc: goto correct label on error paths

Quoted 11 lines · George Dunlap
Quoted 9 lines · Andrew Cooper

Both of these "goto finish;" statements are actually errors, and need to "goto out;" instead, which will correctly destroy the domain and return an error, rather than trying to finish the migration (and in at least one scenario, return success).

CC Ian Campbell<[email protected]>
CC Ian Jackson<[email protected]>
CC George Dunlap<[email protected]>
Quoted 4 lines

Right -- I can't imagine any goodness coming from jumping to "finish" in those cases...

Acked + applied, thanks.

#4 lore raw

PATCH 2/2 tools/libxc: Prevent erroneous success from xc_domain_restore

In reply to Andrew Cooper

The variable 'rc' is set to 1 at the top of xc_domain_restore, and for the most part is left alone until success, at which point it is set to 0.

There is a separate 'frc' which for the most part is used to check function calls, keeping errors separate from 'rc'.

For a toolstack which sets callbacks->toolstack_restore(), and the function returns 0, any subsequent error will end up with code flow going to "out;", resulting in the migration being declared a success.

For consistency, update the callsites of xc_dom_gnttab{,_hvm}_seed() to use 'frc', even though their use of 'rc' is currently safe.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC George Dunlap <[email protected]>

---

Regarding 4.4: If the two "for consistency" changes to xc_dom_gnttab{,_hvm}_seed() are considered too risky, they can be dropped without affecting the bugfix nature of the patch, but I would argue that leaving some examples of "rc = function_call()" leaves a bad precident which is likely to lead to similar bugs in the future.

---
 tools/libxc/xc_domain_restore.c |   19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
tools/libxc/xc_domain_restore.c +9 -10
index 5ba47d7..817054d 100644
@@ -2240,9 +2240,9 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
         memcpy(ctx->live_p2m, ctx->p2m, dinfo->p2m_size * sizeof(xen_pfn_t));
     munmap(ctx->live_p2m, P2M_FL_ENTRIES * PAGE_SIZE);
 
-    rc = xc_dom_gnttab_seed(xch, dom, *console_mfn, *store_mfn,
-                            console_domid, store_domid);
-    if (rc != 0)
+    frc = xc_dom_gnttab_seed(xch, dom, *console_mfn, *store_mfn,
+                             console_domid, store_domid);
+    if (frc != 0)
     {
         ERROR("error seeding grant table");
         goto out;
@@ -2257,16 +2257,15 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     {
         if ( callbacks != NULL && callbacks->toolstack_restore != NULL )
         {
-            rc = callbacks->toolstack_restore(dom, tdata.data, tdata.len,
-                        callbacks->data);
+            frc = callbacks->toolstack_restore(dom, tdata.data, tdata.len,
+                                               callbacks->data);
             free(tdata.data);
-            if ( rc < 0 )
+            if ( frc < 0 )
             {
                 PERROR("error calling toolstack_restore");
                 goto out;
             }
         } else {
-            rc = -1;
             ERROR("toolstack data available but no callback provided\n");
             free(tdata.data);
             goto out;
@@ -2326,9 +2325,9 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
         goto out;
     }
 
-    rc = xc_dom_gnttab_hvm_seed(xch, dom, *console_mfn, *store_mfn,
-                                console_domid, store_domid);
-    if (rc != 0)
+    frc = xc_dom_gnttab_hvm_seed(xch, dom, *console_mfn, *store_mfn,
+                                 console_domid, store_domid);
+    if (frc != 0)
     {
         ERROR("error seeding grant table");
         goto out;
Signature · 1 line
1.7.10.4
#5 lore raw

Re: [Patch 2/2] tools/libxc: Prevent erroneous success from xc_domain_restore

Quoted 10 lines · Andrew Cooper

The variable 'rc' is set to 1 at the top of xc_domain_restore, and for the most part is left alone until success, at which point it is set to 0.

There is a separate 'frc' which for the most part is used to check function calls, keeping errors separate from 'rc'.

For a toolstack which sets callbacks->toolstack_restore(), and the function returns 0, any subsequent error will end up with code flow going to "out;", resulting in the migration being declared a success.

Quoted 7 lines

For consistency, update the callsites of xc_dom_gnttab{,_hvm}_seed() to use 'frc', even though their use of 'rc' is currently safe.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC George Dunlap <[email protected]>

Ping?

Quoted 62 lines

---

Regarding 4.4: If the two "for consistency" changes to xc_dom_gnttab{,_hvm}_seed() are considered too risky, they can be dropped without affecting the bugfix nature of the patch, but I would argue that leaving some examples of "rc = function_call()" leaves a bad precident which is likely to lead to similar bugs in the future.

---
 tools/libxc/xc_domain_restore.c |   19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
tools/libxc/xc_domain_restore.c +9 -10
index 5ba47d7..817054d 100644
@@ -2240,9 +2240,9 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
         memcpy(ctx->live_p2m, ctx->p2m, dinfo->p2m_size * sizeof(xen_pfn_t));
     munmap(ctx->live_p2m, P2M_FL_ENTRIES * PAGE_SIZE);
 
-    rc = xc_dom_gnttab_seed(xch, dom, *console_mfn, *store_mfn,
-                            console_domid, store_domid);
-    if (rc != 0)
+    frc = xc_dom_gnttab_seed(xch, dom, *console_mfn, *store_mfn,
+                             console_domid, store_domid);
+    if (frc != 0)
     {
         ERROR("error seeding grant table");
         goto out;
@@ -2257,16 +2257,15 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     {
         if ( callbacks != NULL && callbacks->toolstack_restore != NULL )
         {
-            rc = callbacks->toolstack_restore(dom, tdata.data, tdata.len,
-                        callbacks->data);
+            frc = callbacks->toolstack_restore(dom, tdata.data, tdata.len,
+                                               callbacks->data);
             free(tdata.data);
-            if ( rc < 0 )
+            if ( frc < 0 )
             {
                 PERROR("error calling toolstack_restore");
                 goto out;
             }
         } else {
-            rc = -1;
             ERROR("toolstack data available but no callback provided\n");
             free(tdata.data);
             goto out;
@@ -2326,9 +2325,9 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
         goto out;
     }
 
-    rc = xc_dom_gnttab_hvm_seed(xch, dom, *console_mfn, *store_mfn,
-                                console_domid, store_domid);
-    if (rc != 0)
+    frc = xc_dom_gnttab_hvm_seed(xch, dom, *console_mfn, *store_mfn,
+                                 console_domid, store_domid);
+    if (frc != 0)
     {
         ERROR("error seeding grant table");
         goto out;
#6 lore raw

Re: [Patch 2/2] tools/libxc: Prevent erroneous success from xc_domain_restore

Quoted 4 lines · Andrew Cooper
Quoted 4 lines
                 goto out;
             }
         } else {

- rc = -1;

Mostly looks good but I'm not sure about this change

We get here on input error (toolstack data available but no callback provided) which is neither migration success nor failure, it's a bug in the caller. So arguably returning a separate failure from success/unsuccess makes sense.

I'd have thought it ought to set errno (too EINVAL perhaps) too, but lets not mess with that now.

Ian.

#7 lore raw

Re: [Patch 2/2] tools/libxc: Prevent erroneous success from xc_domain_restore

Quoted 12 lines · Ian Campbell
Quoted 4 lines · Andrew Cooper
Quoted 4 lines
                 goto out;
             }
         } else {

- rc = -1;

Mostly looks good but I'm not sure about this change

We get here on input error (toolstack data available but no callback provided) which is neither migration success nor failure, it's a bug in the caller. So arguably returning a separate failure from success/unsuccess makes sense.

Quoted 6 lines

I'd have thought it ought to set errno (too EINVAL perhaps) too, but lets not mess with that now.

Ian.

Hilariously, it turns out that xc_domain_restore() is specified to return 0 on success and -1 on failure. From what I can tell, this is the sole action which would cause xc_domain_restore() to return anything other than 0 or 1.

I think fixing this should fall into the bucket of "sanitisation of libxc error paths".

~Andrew

#8 lore raw

Re: [Patch 2/2] tools/libxc: Prevent erroneous success from xc_domain_restore

Quoted 20 lines · Andrew Cooper
Quoted 12 lines · Ian Campbell
Quoted 4 lines · Andrew Cooper
Quoted 4 lines
                 goto out;
             }
         } else {

- rc = -1;

Mostly looks good but I'm not sure about this change

We get here on input error (toolstack data available but no callback provided) which is neither migration success nor failure, it's a bug in the caller. So arguably returning a separate failure from success/unsuccess makes sense.

Quoted 6 lines

I'd have thought it ought to set errno (too EINVAL perhaps) too, but lets not mess with that now.

Ian.

Quoted 7 lines

Hilariously, it turns out that xc_domain_restore() is specified to return 0 on success and -1 on failure. From what I can tell, this is the sole action which would cause xc_domain_restore() to return anything other than 0 or 1.

I think fixing this should fall into the bucket of "sanitisation of libxc error paths".

Right, so please leave the rc = -1 alone.

Ian.

#9 lore raw

PATCH v2 1/1 tools/libxc: Prevent erroneous success from xc_domain_restore

The variable 'rc' is set to 1 at the top of xc_domain_restore, and for the most part is left alone until success, at which point it is set to 0.

There is a separate 'frc' which for the most part is used to check function calls, keeping errors separate from 'rc'.

For a toolstack which sets callbacks->toolstack_restore(), and the function returns 0, any subsequent error will end up with code flow going to "out;", resulting in the migration being declared a success.

For consistency, update the callsites of xc_dom_gnttab{,_hvm}_seed() to use 'frc', even though their use of 'rc' is currently safe.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC George Dunlap <[email protected]>

---

Changes in v2:
* Dont drop rc = -1 from toolstack_restore().

Regarding 4.4: If the two "for consistency" changes to xc_dom_gnttab{,_hvm}_seed() are considered too risky, they can be dropped without affecting the bugfix nature of the patch, but I would argue that leaving some examples of "rc = function_call()" leaves a bad precident which is likely to lead to similar bugs in the future.

---
 tools/libxc/xc_domain_restore.c |   18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
tools/libxc/xc_domain_restore.c +9 -9
index 5ba47d7..1f6ce50 100644
@@ -2240,9 +2240,9 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
         memcpy(ctx->live_p2m, ctx->p2m, dinfo->p2m_size * sizeof(xen_pfn_t));
     munmap(ctx->live_p2m, P2M_FL_ENTRIES * PAGE_SIZE);
 
-    rc = xc_dom_gnttab_seed(xch, dom, *console_mfn, *store_mfn,
-                            console_domid, store_domid);
-    if (rc != 0)
+    frc = xc_dom_gnttab_seed(xch, dom, *console_mfn, *store_mfn,
+                             console_domid, store_domid);
+    if (frc != 0)
     {
         ERROR("error seeding grant table");
         goto out;
@@ -2257,10 +2257,10 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     {
         if ( callbacks != NULL && callbacks->toolstack_restore != NULL )
         {
-            rc = callbacks->toolstack_restore(dom, tdata.data, tdata.len,
-                        callbacks->data);
+            frc = callbacks->toolstack_restore(dom, tdata.data, tdata.len,
+                                               callbacks->data);
             free(tdata.data);
-            if ( rc < 0 )
+            if ( frc < 0 )
             {
                 PERROR("error calling toolstack_restore");
                 goto out;
@@ -2326,9 +2326,9 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
         goto out;
     }
 
-    rc = xc_dom_gnttab_hvm_seed(xch, dom, *console_mfn, *store_mfn,
-                                console_domid, store_domid);
-    if (rc != 0)
+    frc = xc_dom_gnttab_hvm_seed(xch, dom, *console_mfn, *store_mfn,
+                                 console_domid, store_domid);
+    if (frc != 0)
     {
         ERROR("error seeding grant table");
         goto out;
Signature · 1 line
1.7.10.4
Acked-by on v2 1/1
#10 lore raw

Re: [PATCH v2] tools/libxc: Prevent erroneous success from xc_domain_restore

Quoted 6 lines · Andrew Cooper

The variable 'rc' is set to 1 at the top of xc_domain_restore, and for the most part is left alone until success, at which point it is set to 0.

There is a separate 'frc' which for the most part is used to check function calls, keeping errors separate from 'rc'.

Quoted 8 lines

For a toolstack which sets callbacks->toolstack_restore(), and the function returns 0, any subsequent error will end up with code flow going to "out;", resulting in the migration being declared a success.

For consistency, update the callsites of xc_dom_gnttab{,_hvm}_seed() to use 'frc', even though their use of 'rc' is currently safe.

Quoted 61 lines
CC Ian Jackson <[email protected]>
CC George Dunlap <[email protected]>

---

Changes in v2:
* Dont drop rc = -1 from toolstack_restore().

Regarding 4.4: If the two "for consistency" changes to xc_dom_gnttab{,_hvm}_seed() are considered too risky, they can be dropped without affecting the bugfix nature of the patch, but I would argue that leaving some examples of "rc = function_call()" leaves a bad precident which is likely to lead to similar bugs in the future.

---
 tools/libxc/xc_domain_restore.c |   18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
tools/libxc/xc_domain_restore.c +9 -9
index 5ba47d7..1f6ce50 100644
@@ -2240,9 +2240,9 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
         memcpy(ctx->live_p2m, ctx->p2m, dinfo->p2m_size * sizeof(xen_pfn_t));
     munmap(ctx->live_p2m, P2M_FL_ENTRIES * PAGE_SIZE);
 
-    rc = xc_dom_gnttab_seed(xch, dom, *console_mfn, *store_mfn,
-                            console_domid, store_domid);
-    if (rc != 0)
+    frc = xc_dom_gnttab_seed(xch, dom, *console_mfn, *store_mfn,
+                             console_domid, store_domid);
+    if (frc != 0)
     {
         ERROR("error seeding grant table");
         goto out;
@@ -2257,10 +2257,10 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     {
         if ( callbacks != NULL && callbacks->toolstack_restore != NULL )
         {
-            rc = callbacks->toolstack_restore(dom, tdata.data, tdata.len,
-                        callbacks->data);
+            frc = callbacks->toolstack_restore(dom, tdata.data, tdata.len,
+                                               callbacks->data);
             free(tdata.data);
-            if ( rc < 0 )
+            if ( frc < 0 )
             {
                 PERROR("error calling toolstack_restore");
                 goto out;
@@ -2326,9 +2326,9 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
         goto out;
     }
 
-    rc = xc_dom_gnttab_hvm_seed(xch, dom, *console_mfn, *store_mfn,
-                                console_domid, store_domid);
-    if (rc != 0)
+    frc = xc_dom_gnttab_hvm_seed(xch, dom, *console_mfn, *store_mfn,
+                                 console_domid, store_domid);
+    if (frc != 0)
     {
         ERROR("error seeding grant table");
         goto out;
#11 lore raw

Re: [PATCH v2] tools/libxc: Prevent erroneous success from xc_domain_restore

In reply to Andrew Cooper
Quoted 20 lines · Andrew Cooper

The variable 'rc' is set to 1 at the top of xc_domain_restore, and for the most part is left alone until success, at which point it is set to 0.

There is a separate 'frc' which for the most part is used to check function calls, keeping errors separate from 'rc'.

For a toolstack which sets callbacks->toolstack_restore(), and the function returns 0, any subsequent error will end up with code flow going to "out;", resulting in the migration being declared a success.

For consistency, update the callsites of xc_dom_gnttab{,_hvm}_seed() to use 'frc', even though their use of 'rc' is currently safe.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC George Dunlap <[email protected]>

---

Quoted 8 lines

Changes in v2:
* Dont drop rc = -1 from toolstack_restore().

Regarding 4.4: If the two "for consistency" changes to xc_dom_gnttab{,_hvm}_seed() are considered too risky, they can be dropped without affecting the bugfix nature of the patch, but I would argue that leaving some examples of "rc = function_call()" leaves a bad precident which is likely to lead to similar bugs in the future.

Yes, these are all pretty clear bug fixes.

Applied
#12 lore raw

Re: [PATCH v2] tools/libxc: Prevent erroneous success from xc_domain_restore

Quoted 30 lines · George Dunlap
Quoted 20 lines · Andrew Cooper

The variable 'rc' is set to 1 at the top of xc_domain_restore, and for the most part is left alone until success, at which point it is set to 0.

There is a separate 'frc' which for the most part is used to check function calls, keeping errors separate from 'rc'.

For a toolstack which sets callbacks->toolstack_restore(), and the function returns 0, any subsequent error will end up with code flow going to "out;", resulting in the migration being declared a success.

For consistency, update the callsites of xc_dom_gnttab{,_hvm}_seed() to use 'frc', even though their use of 'rc' is currently safe.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC George Dunlap <[email protected]>

---

Quoted 8 lines

Changes in v2:
* Dont drop rc = -1 from toolstack_restore().

Regarding 4.4: If the two "for consistency" changes to xc_dom_gnttab{,_hvm}_seed() are considered too risky, they can be dropped without affecting the bugfix nature of the patch, but I would argue that leaving some examples of "rc = function_call()" leaves a bad precident which is likely to lead to similar bugs in the future.

Quoted 3 lines

Yes, these are all pretty clear bug fixes.

Applied.