[PATCH v2] virQEMUDriverGetDomainCapabilities: Validate arguments

Michal Privoznik posted 1 patch 1 year, 4 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/c25afbd3fff3598a5aef3f3f07b8cb3e80c0fcc2.1671710350.git.mprivozn@redhat.com
src/qemu/qemu_conf.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
[PATCH v2] virQEMUDriverGetDomainCapabilities: Validate arguments
Posted by Michal Privoznik 1 year, 4 months ago
When calling virConnectGetDomainCapabilities() (exposed as virsh
domcapabilities) users have option to specify whatever sub-set of
{ emulatorbin, arch, machine, virttype } they want. Then we have
a logic (hidden in virQEMUCapsCacheLookupDefault()) that picks
qemuCaps that satisfy values passed by user. And whatever was not
specified is then set to the default value as specified by picked
qemuCaps. For instance: if no machine type was provided but
emulatorbin was, then the machine type is set to the default one
as defined by the emulatorbin.

Or, when just virttype was set then the remaining three values
are set to their respective defaults. Except, we have a crasher
in this case:

  # virsh domcapabilities --virttype hvf
  error: Disconnected from qemu:///system due to end of file
  error: failed to get emulator capabilities
  error: End of file while reading data: Input/output error

This is because for 'hvf' virttype (at least my) QEMU does not
have any machine type. Therefore, @machine is set to NULL and the
rest of the code does not expect that.

What we can do about this is to validate all arguments. Well,
except for the emulatorbin which is obtained from passed
qemuCaps. This also fixes the issue when domcapabilities for a
virttype of a different driver are requested, or a different
arch.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---

v2 of:

https://listman.redhat.com/archives/libvir-list/2022-December/236460.html

diff to v1:
- validate @arch, @virttype as well, not just @machine

 src/qemu/qemu_conf.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index ae5bbcd138..6760bef14c 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1454,6 +1454,27 @@ virQEMUDriverGetDomainCapabilities(virQEMUDriver *driver,
     g_autoptr(virDomainCaps) domCaps = NULL;
     const char *path = virQEMUCapsGetBinary(qemuCaps);
 
+    if (!virQEMUCapsIsArchSupported(qemuCaps, arch)) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("Emulator '%s' does not support arch '%s'"),
+                       path, virArchToString(arch));
+        return NULL;
+    }
+
+    if (!virQEMUCapsIsVirtTypeSupported(qemuCaps, virttype)) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("Emulator '%s' does not support virt type '%s'"),
+                       path, virDomainVirtTypeToString(virttype));
+        return NULL;
+    }
+
+    if (!virQEMUCapsIsMachineSupported(qemuCaps, virttype, machine)) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("Emulator '%s' does not support machine type '%s'"),
+                       path, NULLSTR(machine));
+        return NULL;
+    }
+
     if (!(domCaps = virDomainCapsNew(path, machine, arch, virttype)))
         return NULL;
 
-- 
2.38.2
Re: [PATCH v2] virQEMUDriverGetDomainCapabilities: Validate arguments
Posted by Martin Kletzander 1 year, 3 months ago
On Thu, Dec 22, 2022 at 01:00:06PM +0100, Michal Privoznik wrote:
>When calling virConnectGetDomainCapabilities() (exposed as virsh
>domcapabilities) users have option to specify whatever sub-set of
>{ emulatorbin, arch, machine, virttype } they want. Then we have
>a logic (hidden in virQEMUCapsCacheLookupDefault()) that picks
>qemuCaps that satisfy values passed by user. And whatever was not
>specified is then set to the default value as specified by picked
>qemuCaps. For instance: if no machine type was provided but
>emulatorbin was, then the machine type is set to the default one
>as defined by the emulatorbin.
>
>Or, when just virttype was set then the remaining three values
>are set to their respective defaults. Except, we have a crasher
>in this case:
>
>  # virsh domcapabilities --virttype hvf
>  error: Disconnected from qemu:///system due to end of file
>  error: failed to get emulator capabilities
>  error: End of file while reading data: Input/output error
>
>This is because for 'hvf' virttype (at least my) QEMU does not
>have any machine type. Therefore, @machine is set to NULL and the
>rest of the code does not expect that.
>
>What we can do about this is to validate all arguments. Well,
>except for the emulatorbin which is obtained from passed
>qemuCaps. This also fixes the issue when domcapabilities for a
>virttype of a different driver are requested, or a different
>arch.
>
>Signed-off-by: Michal Privoznik <mprivozn@redhat.com>

Reviewed-by: Martin Kletzander <mkletzan@redhat.com>