[PATCH] util: don't validate empty params

Oleg Vasilev posted 1 patch 10 months, 4 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20230623092202.1994230-1-oleg.vasilev@virtuozzo.com
src/util/virtypedparam.c | 4 ++++
1 file changed, 4 insertions(+)
[PATCH] util: don't validate empty params
Posted by Oleg Vasilev 10 months, 4 weeks ago
If there are no parameters, there is nothing to validate.
If params == NULL, memcpy below results in memcpy(sorted, NULL, 0),
which is UB.

Found by UBSAN. Example of this codepath: virDomainBlockCopy()
(where nparams == 0 is valid) -> qemuDomainBlockCopy()

Signed-off-by: Oleg Vasilev <oleg.vasilev@virtuozzo.com>
---
 src/util/virtypedparam.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index 3bb8b125e9..ef3b8052f6 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -68,6 +68,10 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
     g_autofree virTypedParameterPtr sorted = NULL;
     g_autofree virTypedParameterPtr keys = NULL;
 
+    if (!nparams) {
+        return 0;
+    }
+
     va_start(ap, nparams);
 
     sorted = g_new0(virTypedParameter, nparams);
-- 
2.41.0
Re: [PATCH] util: don't validate empty params
Posted by Kristina Hanicova 10 months, 3 weeks ago
On Fri, Jun 23, 2023 at 11:22 AM Oleg Vasilev <oleg.vasilev@virtuozzo.com>
wrote:

> If there are no parameters, there is nothing to validate.
> If params == NULL, memcpy below results in memcpy(sorted, NULL, 0),
> which is UB.
>
> Found by UBSAN. Example of this codepath: virDomainBlockCopy()
> (where nparams == 0 is valid) -> qemuDomainBlockCopy()
>
> Signed-off-by: Oleg Vasilev <oleg.vasilev@virtuozzo.com>
> ---
>  src/util/virtypedparam.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
>
Reviewed-by: Kristina Hanicova <khanicov@redhat.com>

Kristina