Skip to contentSkip to conversation

Fix: 'xl vncviewer' accesses port 0 by any invalid domid

PATCH Superseded Chunyan Liu 3 participants 3 messages Jul 17, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix: 'xl vncviewer' accesses port 0 by any invalid domid

Currently, with command:
xl vncviewer invalid_domid
it always brings user to the domU using vncport 5900. The invalid domid could be an non-existing one or Dom0. It's better to report error in this case.

This patch corrects two places:
* libxl_domain_qualifier_to_domid() in tools/libxl/libxl_utils.c blindly converts the input string id to a uint32_t. It doesn't check if the domid is valid or not, so it will depends on later processing to find the error. Correct it with checking the domid validness.
* libxl_vncviewer_exec: when vncport is NULL, it still continues and will show vncport 5900. So, with 'xl vncviewer 0' it also wrongly shows domU using vncport 5900. Correct it to report error if vncport is NULL.

---
 tools/libxl/libxl.c       | 7 ++++++-
 tools/libxl/libxl_utils.c | 5 +++++
 2 files changed, 11 insertions(+), 1 deletion(-)
tools/libxl/libxl.c +6 -1
index a9205d1..22602e8 100644
@@ -1719,8 +1719,13 @@ int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
     vnc_port = libxl__xs_read(gc, XBT_NULL,
                             libxl__sprintf(gc,
                             "/local/domain/%d/console/vnc-port", domid));
-    if ( vnc_port )
+    if ( vnc_port ) {
         port = atoi(vnc_port) - 5900;
+    } else {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                   "Cannot get vnc-port of domain %d", domid);
+        goto x_fail;
+    }
 
     vnc_listen = libxl__xs_read(gc, XBT_NULL,
                                 libxl__sprintf(gc,
tools/libxl/libxl_utils.c +5 -0
index 1fdf5ea..c689de3 100644
@@ -100,6 +100,11 @@ int libxl_domain_qualifier_to_domid(libxl_ctx *ctx, const char *name,
         }
     }
     *domid = strtoul(name, NULL, 10);
+
+    /* this could be an invalid domid */
+    if (!libxl_domid_to_name(ctx, *domid))
+        return -1;
+
     return 0;
 
  nondigit_found:
Signature · 1 line
1.8.4.5
#1 lore raw

On Thu, Jul 17, 2014 at 02:35:51PM +0800, Chunyan Liu wrote:
[...]

tools/libxl/libxl_utils.c int libxl_domain_qualifier_to_domid(libxl_ctx *ctx, const char *name, · line 107

Quoted 13 lines
tools/libxl/libxl_utils.c +5 -0
index 1fdf5ea..c689de3 100644
@@ -100,6 +100,11 @@ int libxl_domain_qualifier_to_domid(libxl_ctx *ctx, const char *name,
         }
     }
     *domid = strtoul(name, NULL, 10);
+
+    /* this could be an invalid domid */
+    if (!libxl_domid_to_name(ctx, *domid))
+        return -1;
+

I think it should return ERROR_*.

Wei.

#2 lore raw
In reply to Chunyan Liu
Quoted 29 lines · Chunyan Liu

Currently, with command:
xl vncviewer invalid_domid
it always brings user to the domU using vncport 5900. The invalid domid could be an non-existing one or Dom0. It's better to report error in this case.

This patch corrects two places:
* libxl_domain_qualifier_to_domid() in tools/libxl/libxl_utils.c blindly converts the input string id to a uint32_t. It doesn't check if the domid is valid or not, so it will depends on later processing to find the error. Correct it with checking the domid validness.
* libxl_vncviewer_exec: when vncport is NULL, it still continues and will show vncport 5900. So, with 'xl vncviewer 0' it also wrongly shows domU using vncport 5900. Correct it to report error if vncport is NULL.

---
 tools/libxl/libxl.c       | 7 ++++++-
 tools/libxl/libxl_utils.c | 5 +++++
 2 files changed, 11 insertions(+), 1 deletion(-)
tools/libxl/libxl.c +0 -0
index a9205d1..22602e8 100644
@@ -1719,8 +1719,13 @@ int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
     vnc_port = libxl__xs_read(gc, XBT_NULL,

tools/libxl/libxl.c int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass) · line 1728

Quoted 10 lines
tools/libxl/libxl.c +6 -1
                             libxl__sprintf(gc,
                             "/local/domain/%d/console/vnc-port", domid));
-    if ( vnc_port )
+    if ( vnc_port ) {
         port = atoi(vnc_port) - 5900;
+    } else {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                   "Cannot get vnc-port of domain %d", domid);
+        goto x_fail;
+    }

This would be simpler as

	if (!vnc_port) {
	    LOG(ERROR, "Cannot... domain %d\n", domid);
            goto x_fail
        }

        port = atoi...

        vnc_listen = libxl_

Rather than putting the error case in an else.

Also note the coding style (no spaces inside the expr of an if ()) and the use of LOG() instead of LIBXL__LOG.

Quoted 7 lines
tools/libxl/libxl.c +0 -0
     vnc_listen = libxl__xs_read(gc, XBT_NULL,
                                 libxl__sprintf(gc,
tools/libxl/libxl_utils.c +0 -0
index 1fdf5ea..c689de3 100644

tools/libxl/libxl_utils.c int libxl_domain_qualifier_to_domid(libxl_ctx *ctx, const char *name, · line 106

Quoted 7 lines
tools/libxl/libxl_utils.c +4 -0
@@ -100,6 +100,11 @@ int libxl_domain_qualifier_to_domid(libxl_ctx *ctx, const char *name,
         }
     }
     *domid = strtoul(name, NULL, 10);
+
+    /* this could be an invalid domid */
+    if (!libxl_domid_to_name(ctx, *domid))
+        return -1;

We also need to be mindful of the domain disappearing (i.e. being destroyed) after this check is done (unless there was some locking, which I don't think there is around this sort of thing)

All that means is that the "later processing" you refer to in the commit message still needs to cope with this case. Was that code already correct or are you masking the issue in 99% of the cases with this?

Given the potential for masking races I'm wondering if we might be better to omit this check and force ourselves to make the rest of the code robust.

Ian.