[libvirt] [PATCH] virsh: forbid negative vcpu argument to vcpupin.

vcpupin will allow argument --vcpu as a signed number, and pass it to virDomainPinVcpu directlly without checking if this value is positive(valid).
virsh vcpupin r7 -1 0 error: numerical overflow: input too large: 4294967295
This message is inaccurate, and the negative vcpu is non-valuable. So forbid vcpu argument as a negative. Signed-off-by: Jincheng Miao <jmiao@redhat.com> --- tools/virsh-domain.c | 24 ++++++++++-------------- 1 files changed, 10 insertions(+), 14 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 84a6706..d9804cc 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -5797,7 +5797,7 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd) { virDomainInfo info; virDomainPtr dom; - int vcpu = -1; + unsigned int vcpu; const char *cpulist = NULL; bool ret = false; unsigned char *cpumap = NULL; @@ -5830,29 +5830,25 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd) query = !cpulist; - /* In query mode, "vcpu" is optional */ - if (vshCommandOptInt(cmd, "vcpu", &vcpu) < !query) { + /* In query mode, "vcpu" is optional*/ + if (vshCommandOptUInt(cmd, "vcpu", &vcpu) < !query) { vshError(ctl, "%s", _("vcpupin: Invalid or missing vCPU number.")); - virDomainFree(dom); - return false; - } - - if ((maxcpu = vshNodeGetCPUCount(ctl->conn)) < 0) { - virDomainFree(dom); - return false; + goto cleanup; } if (virDomainGetInfo(dom, &info) != 0) { vshError(ctl, "%s", _("vcpupin: failed to get domain information.")); - virDomainFree(dom); - return false; + goto cleanup; } if (vcpu >= info.nrVirtCpu) { vshError(ctl, "%s", _("vcpupin: Invalid vCPU number.")); - virDomainFree(dom); - return false; + goto cleanup; + } + + if ((maxcpu = vshNodeGetCPUCount(ctl->conn)) < 0) { + goto cleanup; } cpumaplen = VIR_CPU_MAPLEN(maxcpu); -- 1.7.1

On 05/28/14 11:33, Jincheng Miao wrote:
vcpupin will allow argument --vcpu as a signed number, and pass it to virDomainPinVcpu directlly without checking if this value is positive(valid).
virsh vcpupin r7 -1 0 error: numerical overflow: input too large: 4294967295
This message is inaccurate, and the negative vcpu is non-valuable. So forbid vcpu argument as a negative.
Signed-off-by: Jincheng Miao <jmiao@redhat.com> --- tools/virsh-domain.c | 24 ++++++++++-------------- 1 files changed, 10 insertions(+), 14 deletions(-)
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 84a6706..d9804cc 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -5797,7 +5797,7 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd) { virDomainInfo info; virDomainPtr dom; - int vcpu = -1; + unsigned int vcpu; const char *cpulist = NULL; bool ret = false; unsigned char *cpumap = NULL;
This change will break the output code that is present later that relies on vcpu being set to -1 if it wasn't specified by the user: for (i = 0; i < ncpus; i++) { if (vcpu != -1 && i != vcpu) continue; vshPrint(ctl, "%4zu: ", i); ret = vshPrintPinInfo(cpumaps, cpumaplen, maxcpu, i); vshPrint(ctl, "\n"); if (!ret) break; } Also breaks the output: $ virsh vcpupin gl VCPU: CPU Affinity ---------------------------------- 0: 0-3 1: 0-3 After patching: $ tools/virsh vcpupin gl error: vcpupin: Invalid vCPU number. Peter
participants (2)
-
Jincheng Miao
-
Peter Krempa