[libvirt] [PATCH 15/35] qemu: block: Validate node-names for use with qemu

Peter Krempa posted 35 patches 7 years ago
[libvirt] [PATCH 15/35] qemu: block: Validate node-names for use with qemu
Posted by Peter Krempa 7 years ago
qemu declares node-name as a 32 byte buffer and silently truncates
anything longer than that. This is unacceptable for libvirt, so we need
to make sure that we won't ever supply a node-name exceeding 31 chars.

Add a function which will do the validation and use it to validate
storage-protocol node names.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/qemu/qemu_block.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
index 29fc7edf61..e3ee169368 100644
--- a/src/qemu/qemu_block.c
+++ b/src/qemu/qemu_block.c
@@ -27,6 +27,24 @@

 #define VIR_FROM_THIS VIR_FROM_QEMU

+/* qemu declares the buffer for node names as a 32 byte array */
+static const size_t qemuBlockNodeNameBufSize = 32;
+
+static int
+qemuBlockNodeNameValidate(const char *nn)
+{
+    if (!nn)
+        return 0;
+
+    if (strlen(nn) >= qemuBlockNodeNameBufSize) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("node-name '%s' too long for qemu"), nn);
+        return -1;
+    }
+
+    return 0;
+}
+

 static int
 qemuBlockNamedNodesArrayToHash(size_t pos ATTRIBUTE_UNUSED,
@@ -1107,7 +1125,8 @@ qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
         break;
     }

-    if (virJSONValueObjectAdd(fileprops, "S:node-name", src->nodestorage, NULL) < 0) {
+    if (qemuBlockNodeNameValidate(src->nodestorage) < 0 ||
+        virJSONValueObjectAdd(fileprops, "S:node-name", src->nodestorage, NULL) < 0) {
         virJSONValueFree(fileprops);
         return NULL;
     }
-- 
2.16.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 15/35] qemu: block: Validate node-names for use with qemu
Posted by John Ferlan 7 years ago

On 04/25/2018 11:15 AM, Peter Krempa wrote:
> qemu declares node-name as a 32 byte buffer and silently truncates
> anything longer than that. This is unacceptable for libvirt, so we need
> to make sure that we won't ever supply a node-name exceeding 31 chars.
> 
> Add a function which will do the validation and use it to validate
> storage-protocol node names.
> 
> Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> ---
>  src/qemu/qemu_block.c | 21 ++++++++++++++++++++-
>  1 file changed, 20 insertions(+), 1 deletion(-)
> 

Reviewed-by: John Ferlan <jferlan@redhat.com>

John

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list