Skip to contentSkip to conversation

tools/xl: refuse to set number of vcpus to 0 via xl vcpu-set

PATCH In review Juergen Gross 2 participants 5 messages Sep 3, 2018 – Sep 5, 2018

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tools/xl: refuse to set number of vcpus to 0 via xl vcpu-set

Trying to set the number of vcpus of a domain to 0 isn't refused. We should not allow that.

---
 tools/libxl/libxl_domain.c | 6 ++++++
 tools/xl/xl_vcpu.c         | 5 +++--
 2 files changed, 9 insertions(+), 2 deletions(-)
tools/libxl/libxl_domain.c +6 -0
index 533bcdf240..3377bba994 100644
@@ -1369,6 +1369,12 @@ int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, libxl_bitmap *cpumap)
     }
 
     maxcpus = libxl_bitmap_count_set(cpumap);
+    if (maxcpus == 0)
+    {
+        LOGED(ERROR, domid, "Requested 0 VCPUs!");
+        rc = ERROR_FAIL;
+        goto out;
+    }
     if (maxcpus > info.vcpu_max_id + 1)
     {
         LOGED(ERROR, domid, "Requested %d VCPUs, however maxcpus is %d!",
tools/xl/xl_vcpu.c +3 -2
index aef486864c..3057c22aae 100644
@@ -331,13 +331,14 @@ int main_vcpupin(int argc, char **argv)
 static int vcpuset(uint32_t domid, const char* nr_vcpus, int check_host)
 {
     char *endptr;
-    unsigned int max_vcpus, i;
+    unsigned int i;
+    unsigned long max_vcpus;
     libxl_bitmap cpumap;
     int rc;
 
     libxl_bitmap_init(&cpumap);
     max_vcpus = strtoul(nr_vcpus, &endptr, 10);
-    if (nr_vcpus == endptr) {
+    if (nr_vcpus == endptr || max_vcpus > INT_MAX) {
         fprintf(stderr, "Error: Invalid argument.\n");
         return 1;
     }
Signature · 7 lines
2.16.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw
In reply to Juergen Gross
Quoted 34 lines · Juergen Gross

Trying to set the number of vcpus of a domain to 0 isn't refused. We should not allow that.

---
 tools/libxl/libxl_domain.c | 6 ++++++
 tools/xl/xl_vcpu.c         | 5 +++--
 2 files changed, 9 insertions(+), 2 deletions(-)
tools/libxl/libxl_domain.c +6 -0
index 533bcdf240..3377bba994 100644
@@ -1369,6 +1369,12 @@ int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, libxl_bitmap *cpumap)
     }
 
     maxcpus = libxl_bitmap_count_set(cpumap);
+    if (maxcpus == 0)
+    {
+        LOGED(ERROR, domid, "Requested 0 VCPUs!");
+        rc = ERROR_FAIL;
+        goto out;
+    }
     if (maxcpus > info.vcpu_max_id + 1)
     {
         LOGED(ERROR, domid, "Requested %d VCPUs, however maxcpus is %d!",
tools/xl/xl_vcpu.c +0 -0
index aef486864c..3057c22aae 100644
@@ -331,13 +331,14 @@ int main_vcpupin(int argc, char **argv)
 static int vcpuset(uint32_t domid, const char* nr_vcpus, int check_host)
 {
     char *endptr;

tools/xl/xl_vcpu.c int main_vcpupin(int argc, char **argv) · line 341

Quoted 10 lines
tools/xl/xl_vcpu.c +3 -2
-    unsigned int max_vcpus, i;
+    unsigned int i;
+    unsigned long max_vcpus;
     libxl_bitmap cpumap;
     int rc;
 
     libxl_bitmap_init(&cpumap);
     max_vcpus = strtoul(nr_vcpus, &endptr, 10);
-    if (nr_vcpus == endptr) {
+    if (nr_vcpus == endptr || max_vcpus > INT_MAX) {

e -I/build/tools/xl/../../tools/include -D__XEN_TOOLS__ -I/build/tools/xl/../../tools/include -Wshadow -include /build/tools/xl/../../tools/config.h -c -o xl_cdrom.o xl_cdrom.c
xl_vcpu.c: In function 'vcpuset':
xl_vcpu.c:341:43: error: 'INT_MAX' undeclared (first use in this function)

     if (nr_vcpus == endptr || max_vcpus > INT_MAX) {
                                           ^~~~~~~
xl_vcpu.c:341:43: note: each undeclared identifier is reported only once for each function it appears in
xl_vcpu.c:361:29: error: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Werror=format=]
             fprintf(stderr, "You are overcommmitting! You have %d physical" \
                             ^
xl_vcpu.c:379:74: error: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Werror=format=]
         fprintf(stderr, "libxl_set_vcpuonline failed domid=%u max_vcpus=%d," \

Missing a header?

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by
#3 lore raw
Quoted 59 lines · Wei Liu
Quoted 34 lines · Juergen Gross

Trying to set the number of vcpus of a domain to 0 isn't refused. We should not allow that.

---
 tools/libxl/libxl_domain.c | 6 ++++++
 tools/xl/xl_vcpu.c         | 5 +++--
 2 files changed, 9 insertions(+), 2 deletions(-)
tools/libxl/libxl_domain.c +6 -0
index 533bcdf240..3377bba994 100644
@@ -1369,6 +1369,12 @@ int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, libxl_bitmap *cpumap)
     }
 
     maxcpus = libxl_bitmap_count_set(cpumap);
+    if (maxcpus == 0)
+    {
+        LOGED(ERROR, domid, "Requested 0 VCPUs!");
+        rc = ERROR_FAIL;
+        goto out;
+    }
     if (maxcpus > info.vcpu_max_id + 1)
     {
         LOGED(ERROR, domid, "Requested %d VCPUs, however maxcpus is %d!",
tools/xl/xl_vcpu.c +0 -0
index aef486864c..3057c22aae 100644
@@ -331,13 +331,14 @@ int main_vcpupin(int argc, char **argv)
 static int vcpuset(uint32_t domid, const char* nr_vcpus, int check_host)
 {
     char *endptr;

tools/xl/xl_vcpu.c int main_vcpupin(int argc, char **argv) · line 341

Quoted 10 lines
tools/xl/xl_vcpu.c +3 -2
-    unsigned int max_vcpus, i;
+    unsigned int i;
+    unsigned long max_vcpus;
     libxl_bitmap cpumap;
     int rc;
 
     libxl_bitmap_init(&cpumap);
     max_vcpus = strtoul(nr_vcpus, &endptr, 10);
-    if (nr_vcpus == endptr) {
+    if (nr_vcpus == endptr || max_vcpus > INT_MAX) {

e -I/build/tools/xl/../../tools/include -D__XEN_TOOLS__ -I/build/tools/xl/../../tools/include -Wshadow -include /build/tools/xl/../../tools/config.h -c -o xl_cdrom.o xl_cdrom.c
xl_vcpu.c: In function 'vcpuset':
xl_vcpu.c:341:43: error: 'INT_MAX' undeclared (first use in this function)

     if (nr_vcpus == endptr || max_vcpus > INT_MAX) {
                                           ^~~~~~~
xl_vcpu.c:341:43: note: each undeclared identifier is reported only once for each function it appears in
xl_vcpu.c:361:29: error: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Werror=format=]
             fprintf(stderr, "You are overcommmitting! You have %d physical" \
                             ^
xl_vcpu.c:379:74: error: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Werror=format=]
         fprintf(stderr, "libxl_set_vcpuonline failed domid=%u max_vcpus=%d," \
Quoted 2 lines

Missing a header?

Add limits.h and use %ld to print max_vcpus.

---8<---
From 2c0b1824b1cb33a2610f3f55299247f9e0464466 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Mon, 3 Sep 2018 14:59:42 +0200
Subject: [PATCH] tools/xl: refuse to set number of vcpus to 0 via xl vcpu-set

Trying to set the number of vcpus of a domain to 0 isn't refused. We should not allow that.

---
 tools/libxl/libxl_domain.c |  6 ++++++
 tools/xl/xl_vcpu.c         | 10 ++++++----
 2 files changed, 12 insertions(+), 4 deletions(-)
tools/libxl/libxl_domain.c +6 -0
index 533bcdf240..3377bba994 100644
@@ -1369,6 +1369,12 @@ int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, libxl_bitmap *cpumap)
     }
 
     maxcpus = libxl_bitmap_count_set(cpumap);
+    if (maxcpus == 0)
+    {
+        LOGED(ERROR, domid, "Requested 0 VCPUs!");
+        rc = ERROR_FAIL;
+        goto out;
+    }
     if (maxcpus > info.vcpu_max_id + 1)
     {
         LOGED(ERROR, domid, "Requested %d VCPUs, however maxcpus is %d!",
tools/xl/xl_vcpu.c +6 -4
index aef486864c..71d3a5c96d 100644
@@ -13,6 +13,7 @@
  */
 
 #include <stdlib.h>
+#include <limits.h>
 
 #include <libxl.h>
 #include <libxl_utils.h>
@@ -331,13 +332,14 @@ int main_vcpupin(int argc, char **argv)
 static int vcpuset(uint32_t domid, const char* nr_vcpus, int check_host)
 {
     char *endptr;
-    unsigned int max_vcpus, i;
+    unsigned int i;
+    unsigned long max_vcpus;
     libxl_bitmap cpumap;
     int rc;
 
     libxl_bitmap_init(&cpumap);
     max_vcpus = strtoul(nr_vcpus, &endptr, 10);
-    if (nr_vcpus == endptr) {
+    if (nr_vcpus == endptr || max_vcpus > INT_MAX) {
         fprintf(stderr, "Error: Invalid argument.\n");
         return 1;
     }
@@ -358,7 +360,7 @@ static int vcpuset(uint32_t domid, const char* nr_vcpus, int check_host)
 
         if (max_vcpus > online_vcpus && max_vcpus > host_cpu) {
             fprintf(stderr, "You are overcommmitting! You have %d physical" \
-                    " CPUs and want %d vCPUs! Aborting, use --ignore-host to" \
+                    " CPUs and want %ld vCPUs! Aborting, use --ignore-host to" \
                     " continue\n", host_cpu, max_vcpus);
             return 1;
         }
@@ -375,7 +377,7 @@ static int vcpuset(uint32_t domid, const char* nr_vcpus, int check_host)
     if (rc == ERROR_DOMAIN_NOTFOUND)
         fprintf(stderr, "Domain %u does not exist.\n", domid);
     else if (rc)
-        fprintf(stderr, "libxl_set_vcpuonline failed domid=%u max_vcpus=%d," \
+        fprintf(stderr, "libxl_set_vcpuonline failed domid=%u max_vcpus=%ld," \
                 " rc: %d\n", domid, max_vcpus, rc);
 
     libxl_bitmap_dispose(&cpumap);
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw
Quoted 60 lines · Wei Liu
Quoted 59 lines · Wei Liu
Quoted 34 lines · Juergen Gross

Trying to set the number of vcpus of a domain to 0 isn't refused. We should not allow that.

---
 tools/libxl/libxl_domain.c | 6 ++++++
 tools/xl/xl_vcpu.c         | 5 +++--
 2 files changed, 9 insertions(+), 2 deletions(-)
tools/libxl/libxl_domain.c +6 -0
index 533bcdf240..3377bba994 100644
@@ -1369,6 +1369,12 @@ int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, libxl_bitmap *cpumap)
     }
 
     maxcpus = libxl_bitmap_count_set(cpumap);
+    if (maxcpus == 0)
+    {
+        LOGED(ERROR, domid, "Requested 0 VCPUs!");
+        rc = ERROR_FAIL;
+        goto out;
+    }
     if (maxcpus > info.vcpu_max_id + 1)
     {
         LOGED(ERROR, domid, "Requested %d VCPUs, however maxcpus is %d!",
tools/xl/xl_vcpu.c +0 -0
index aef486864c..3057c22aae 100644
@@ -331,13 +331,14 @@ int main_vcpupin(int argc, char **argv)
 static int vcpuset(uint32_t domid, const char* nr_vcpus, int check_host)
 {
     char *endptr;

tools/xl/xl_vcpu.c int main_vcpupin(int argc, char **argv) · line 341

Quoted 10 lines
tools/xl/xl_vcpu.c +3 -2
-    unsigned int max_vcpus, i;
+    unsigned int i;
+    unsigned long max_vcpus;
     libxl_bitmap cpumap;
     int rc;
 
     libxl_bitmap_init(&cpumap);
     max_vcpus = strtoul(nr_vcpus, &endptr, 10);
-    if (nr_vcpus == endptr) {
+    if (nr_vcpus == endptr || max_vcpus > INT_MAX) {

e -I/build/tools/xl/../../tools/include -D__XEN_TOOLS__ -I/build/tools/xl/../../tools/include -Wshadow -include /build/tools/xl/../../tools/config.h -c -o xl_cdrom.o xl_cdrom.c
xl_vcpu.c: In function 'vcpuset':
xl_vcpu.c:341:43: error: 'INT_MAX' undeclared (first use in this function)

     if (nr_vcpus == endptr || max_vcpus > INT_MAX) {
                                           ^~~~~~~
xl_vcpu.c:341:43: note: each undeclared identifier is reported only once for each function it appears in
xl_vcpu.c:361:29: error: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Werror=format=]
             fprintf(stderr, "You are overcommmitting! You have %d physical" \
                             ^
xl_vcpu.c:379:74: error: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Werror=format=]
         fprintf(stderr, "libxl_set_vcpuonline failed domid=%u max_vcpus=%d," \
Quoted 4 lines
Quoted 2 lines

Missing a header?

Add limits.h and use %ld to print max_vcpus.

Yes. No idea why I didn't spot that.

Juergen

Quoted 75 lines

---8<---
From 2c0b1824b1cb33a2610f3f55299247f9e0464466 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Mon, 3 Sep 2018 14:59:42 +0200
Subject: [PATCH] tools/xl: refuse to set number of vcpus to 0 via xl vcpu-set

Trying to set the number of vcpus of a domain to 0 isn't refused. We should not allow that.

---
 tools/libxl/libxl_domain.c |  6 ++++++
 tools/xl/xl_vcpu.c         | 10 ++++++----
 2 files changed, 12 insertions(+), 4 deletions(-)
tools/libxl/libxl_domain.c +6 -0
index 533bcdf240..3377bba994 100644
@@ -1369,6 +1369,12 @@ int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, libxl_bitmap *cpumap)
     }
 
     maxcpus = libxl_bitmap_count_set(cpumap);
+    if (maxcpus == 0)
+    {
+        LOGED(ERROR, domid, "Requested 0 VCPUs!");
+        rc = ERROR_FAIL;
+        goto out;
+    }
     if (maxcpus > info.vcpu_max_id + 1)
     {
         LOGED(ERROR, domid, "Requested %d VCPUs, however maxcpus is %d!",
tools/xl/xl_vcpu.c +5 -3
index aef486864c..71d3a5c96d 100644
@@ -13,6 +13,7 @@
  */
 
 #include <stdlib.h>
+#include <limits.h>
 
 #include <libxl.h>
 #include <libxl_utils.h>
@@ -331,13 +332,14 @@ int main_vcpupin(int argc, char **argv)
 static int vcpuset(uint32_t domid, const char* nr_vcpus, int check_host)
 {
     char *endptr;
-    unsigned int max_vcpus, i;
+    unsigned int i;
+    unsigned long max_vcpus;
     libxl_bitmap cpumap;
     int rc;
 
     libxl_bitmap_init(&cpumap);
     max_vcpus = strtoul(nr_vcpus, &endptr, 10);
-    if (nr_vcpus == endptr) {
+    if (nr_vcpus == endptr || max_vcpus > INT_MAX) {
         fprintf(stderr, "Error: Invalid argument.\n");
         return 1;
     }
@@ -358,7 +360,7 @@ static int vcpuset(uint32_t domid, const char* nr_vcpus, int check_host)
 
         if (max_vcpus > online_vcpus && max_vcpus > host_cpu) {
             fprintf(stderr, "You are overcommmitting! You have %d physical" \
-                    " CPUs and want %d vCPUs! Aborting, use --ignore-host to" \
+                    " CPUs and want %ld vCPUs! Aborting, use --ignore-host to" \
                     " continue\n", host_cpu, max_vcpus);
             return 1;
         }

tools/xl/xl_vcpu.c static int vcpuset(uint32_t domid, const char* nr_vcpus, int check_host) · line 383

Quoted 8 lines
tools/xl/xl_vcpu.c +1 -1
@@ -375,7 +377,7 @@ static int vcpuset(uint32_t domid, const char* nr_vcpus, int check_host)
     if (rc == ERROR_DOMAIN_NOTFOUND)
         fprintf(stderr, "Domain %u does not exist.\n", domid);
     else if (rc)
-        fprintf(stderr, "libxl_set_vcpuonline failed domid=%u max_vcpus=%d," \
+        fprintf(stderr, "libxl_set_vcpuonline failed domid=%u max_vcpus=%ld," \
                 " rc: %d\n", domid, max_vcpus, rc);
 
     libxl_bitmap_dispose(&cpumap);

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel