backends/hostmem.c | 13 +++++++--- tests/acceptance/host-nodes-limit.py | 36 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/acceptance/host-nodes-limit.py
host_memory_backend_set_host_nodes() was not validating
host-nodes before writing to backend->host_nodes, making QEMU
write beyond the end of the bitmap.
Fix the crash and add a simple regression test for the fix.
Reported-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
backends/hostmem.c | 13 +++++++---
tests/acceptance/host-nodes-limit.py | 36 ++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 3 deletions(-)
create mode 100644 tests/acceptance/host-nodes-limit.py
diff --git a/backends/hostmem.c b/backends/hostmem.c
index 1a89342039..ef199d32fd 100644
--- a/backends/hostmem.c
+++ b/backends/hostmem.c
@@ -103,11 +103,18 @@ host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
{
#ifdef CONFIG_NUMA
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
- uint16List *l = NULL;
+ uint16List *l, *host_nodes = NULL;
- visit_type_uint16List(v, name, &l, errp);
+ visit_type_uint16List(v, name, &host_nodes, errp);
+
+ for (l = host_nodes; l; l = l->next) {
+ if (l->value >= MAX_NODES) {
+ error_setg(errp, "Invalid host-nodes value: %d", l->value);
+ return;
+ }
+ }
- while (l) {
+ for (l = host_nodes; l; l = l->next) {
bitmap_set(backend->host_nodes, l->value, 1);
l = l->next;
}
diff --git a/tests/acceptance/host-nodes-limit.py b/tests/acceptance/host-nodes-limit.py
new file mode 100644
index 0000000000..e803e10104
--- /dev/null
+++ b/tests/acceptance/host-nodes-limit.py
@@ -0,0 +1,36 @@
+# Regression test for host-nodes limit validation
+#
+# Copyright (c) 2018 Red Hat, Inc.
+#
+# Author:
+# Eduardo Habkost <ehabkost@redhat.com>
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or
+# later. See the COPYING file in the top-level directory.
+
+from avocado_qemu import Test
+from subprocess import Popen, PIPE
+
+MAX_NODES = 128
+
+class HostNodesValidation(Test):
+ def test_large_host_nodes(self):
+ p = Popen([self.qemu_bin, '-display', 'none', '-nodefaults',
+ '-object', 'memory-backend-ram,id=m0,'
+ 'size=4096,host-nodes=%d' % (MAX_NODES)],
+ stderr=PIPE, stdout=PIPE)
+ stdout,stderr = p.communicate()
+
+ self.assertIn(b'Invalid host-nodes', stderr)
+ self.assertEquals(stdout, b'')
+ self.assertEquals(p.returncode, 1)
+
+ def test_valid_host_nodes(self):
+ p = Popen([self.qemu_bin, '-display', 'none', '-nodefaults',
+ '-object', 'memory-backend-ram,id=m0,'
+ 'size=4096,host-nodes=%d' % (MAX_NODES - 1)],
+ stderr=PIPE, stdout=PIPE)
+ stdout,stderr = p.communicate()
+
+ self.assertIn(b'host-nodes must be empty', stderr)
+ self.assertEquals(p.returncode, 1)
--
2.18.0.rc1.1.g3f1ff2140
On Thu, Nov 29, 2018 at 7:59 PM Eduardo Habkost <ehabkost@redhat.com> wrote:
>
> host_memory_backend_set_host_nodes() was not validating
> host-nodes before writing to backend->host_nodes, making QEMU
> write beyond the end of the bitmap.
>
> Fix the crash and add a simple regression test for the fix.
>
> Reported-by: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
> ---
> backends/hostmem.c | 13 +++++++---
> tests/acceptance/host-nodes-limit.py | 36 ++++++++++++++++++++++++++++
> 2 files changed, 46 insertions(+), 3 deletions(-)
> create mode 100644 tests/acceptance/host-nodes-limit.py
>
> diff --git a/backends/hostmem.c b/backends/hostmem.c
> index 1a89342039..ef199d32fd 100644
> --- a/backends/hostmem.c
> +++ b/backends/hostmem.c
> @@ -103,11 +103,18 @@ host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
> {
> #ifdef CONFIG_NUMA
> HostMemoryBackend *backend = MEMORY_BACKEND(obj);
> - uint16List *l = NULL;
> + uint16List *l, *host_nodes = NULL;
>
> - visit_type_uint16List(v, name, &l, errp);
> + visit_type_uint16List(v, name, &host_nodes, errp);
> +
> + for (l = host_nodes; l; l = l->next) {
> + if (l->value >= MAX_NODES) {
> + error_setg(errp, "Invalid host-nodes value: %d", l->value);
> + return;
> + }
> + }
>
> - while (l) {
> + for (l = host_nodes; l; l = l->next) {
> bitmap_set(backend->host_nodes, l->value, 1);
> l = l->next;
> }
Using the for loop instead of the while, maybe we need to remove the l
= l->next in the for body.
Cheers,
Stefano
> diff --git a/tests/acceptance/host-nodes-limit.py b/tests/acceptance/host-nodes-limit.py
> new file mode 100644
> index 0000000000..e803e10104
> --- /dev/null
> +++ b/tests/acceptance/host-nodes-limit.py
> @@ -0,0 +1,36 @@
> +# Regression test for host-nodes limit validation
> +#
> +# Copyright (c) 2018 Red Hat, Inc.
> +#
> +# Author:
> +# Eduardo Habkost <ehabkost@redhat.com>
> +#
> +# This work is licensed under the terms of the GNU GPL, version 2 or
> +# later. See the COPYING file in the top-level directory.
> +
> +from avocado_qemu import Test
> +from subprocess import Popen, PIPE
> +
> +MAX_NODES = 128
> +
> +class HostNodesValidation(Test):
> + def test_large_host_nodes(self):
> + p = Popen([self.qemu_bin, '-display', 'none', '-nodefaults',
> + '-object', 'memory-backend-ram,id=m0,'
> + 'size=4096,host-nodes=%d' % (MAX_NODES)],
> + stderr=PIPE, stdout=PIPE)
> + stdout,stderr = p.communicate()
> +
> + self.assertIn(b'Invalid host-nodes', stderr)
> + self.assertEquals(stdout, b'')
> + self.assertEquals(p.returncode, 1)
> +
> + def test_valid_host_nodes(self):
> + p = Popen([self.qemu_bin, '-display', 'none', '-nodefaults',
> + '-object', 'memory-backend-ram,id=m0,'
> + 'size=4096,host-nodes=%d' % (MAX_NODES - 1)],
> + stderr=PIPE, stdout=PIPE)
> + stdout,stderr = p.communicate()
> +
> + self.assertIn(b'host-nodes must be empty', stderr)
> + self.assertEquals(p.returncode, 1)
> --
> 2.18.0.rc1.1.g3f1ff2140
>
>
--
Stefano Garzarella
Red Hat
On Fri, Nov 30, 2018 at 09:32:21AM +0100, Stefano Garzarella wrote:
> On Thu, Nov 29, 2018 at 7:59 PM Eduardo Habkost <ehabkost@redhat.com> wrote:
> >
> > host_memory_backend_set_host_nodes() was not validating
> > host-nodes before writing to backend->host_nodes, making QEMU
> > write beyond the end of the bitmap.
> >
> > Fix the crash and add a simple regression test for the fix.
> >
> > Reported-by: Markus Armbruster <armbru@redhat.com>
> > Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
> > ---
> > backends/hostmem.c | 13 +++++++---
> > tests/acceptance/host-nodes-limit.py | 36 ++++++++++++++++++++++++++++
> > 2 files changed, 46 insertions(+), 3 deletions(-)
> > create mode 100644 tests/acceptance/host-nodes-limit.py
> >
> > diff --git a/backends/hostmem.c b/backends/hostmem.c
> > index 1a89342039..ef199d32fd 100644
> > --- a/backends/hostmem.c
> > +++ b/backends/hostmem.c
> > @@ -103,11 +103,18 @@ host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
> > {
> > #ifdef CONFIG_NUMA
> > HostMemoryBackend *backend = MEMORY_BACKEND(obj);
> > - uint16List *l = NULL;
> > + uint16List *l, *host_nodes = NULL;
> >
> > - visit_type_uint16List(v, name, &l, errp);
> > + visit_type_uint16List(v, name, &host_nodes, errp);
> > +
> > + for (l = host_nodes; l; l = l->next) {
> > + if (l->value >= MAX_NODES) {
> > + error_setg(errp, "Invalid host-nodes value: %d", l->value);
> > + return;
> > + }
> > + }
> >
> > - while (l) {
> > + for (l = host_nodes; l; l = l->next) {
> > bitmap_set(backend->host_nodes, l->value, 1);
> > l = l->next;
> > }
>
> Using the for loop instead of the while, maybe we need to remove the l
> = l->next in the for body.
Yeah, we must remove it. I did remove it locally but I think I
forgot to amend the commit before submitting. Thanks for
catching!
--
Eduardo
Eduardo Habkost <ehabkost@redhat.com> writes:
> host_memory_backend_set_host_nodes() was not validating
> host-nodes before writing to backend->host_nodes, making QEMU
> write beyond the end of the bitmap.
>
> Fix the crash and add a simple regression test for the fix.
>
> Reported-by: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
> ---
> backends/hostmem.c | 13 +++++++---
> tests/acceptance/host-nodes-limit.py | 36 ++++++++++++++++++++++++++++
> 2 files changed, 46 insertions(+), 3 deletions(-)
> create mode 100644 tests/acceptance/host-nodes-limit.py
>
> diff --git a/backends/hostmem.c b/backends/hostmem.c
> index 1a89342039..ef199d32fd 100644
> --- a/backends/hostmem.c
> +++ b/backends/hostmem.c
> @@ -103,11 +103,18 @@ host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
> {
> #ifdef CONFIG_NUMA
> HostMemoryBackend *backend = MEMORY_BACKEND(obj);
> - uint16List *l = NULL;
> + uint16List *l, *host_nodes = NULL;
>
> - visit_type_uint16List(v, name, &l, errp);
> + visit_type_uint16List(v, name, &host_nodes, errp);
> +
> + for (l = host_nodes; l; l = l->next) {
> + if (l->value >= MAX_NODES) {
> + error_setg(errp, "Invalid host-nodes value: %d", l->value);
> + return;
> + }
> + }
>
> - while (l) {
> + for (l = host_nodes; l; l = l->next) {
> bitmap_set(backend->host_nodes, l->value, 1);
> l = l->next;
> }
Pre-existing: leaks the list created by visit_type_uint16List(), or am I
confused?
host_memory_backend_set_host_nodes() looks like it leaks, too.
The function to free such lists is qapi_free_uint16List().
> diff --git a/tests/acceptance/host-nodes-limit.py b/tests/acceptance/host-nodes-limit.py
> new file mode 100644
> index 0000000000..e803e10104
> --- /dev/null
> +++ b/tests/acceptance/host-nodes-limit.py
> @@ -0,0 +1,36 @@
> +# Regression test for host-nodes limit validation
> +#
> +# Copyright (c) 2018 Red Hat, Inc.
> +#
> +# Author:
> +# Eduardo Habkost <ehabkost@redhat.com>
> +#
> +# This work is licensed under the terms of the GNU GPL, version 2 or
> +# later. See the COPYING file in the top-level directory.
> +
> +from avocado_qemu import Test
> +from subprocess import Popen, PIPE
> +
> +MAX_NODES = 128
> +
> +class HostNodesValidation(Test):
> + def test_large_host_nodes(self):
> + p = Popen([self.qemu_bin, '-display', 'none', '-nodefaults',
> + '-object', 'memory-backend-ram,id=m0,'
> + 'size=4096,host-nodes=%d' % (MAX_NODES)],
> + stderr=PIPE, stdout=PIPE)
> + stdout,stderr = p.communicate()
> +
> + self.assertIn(b'Invalid host-nodes', stderr)
> + self.assertEquals(stdout, b'')
> + self.assertEquals(p.returncode, 1)
> +
> + def test_valid_host_nodes(self):
> + p = Popen([self.qemu_bin, '-display', 'none', '-nodefaults',
> + '-object', 'memory-backend-ram,id=m0,'
> + 'size=4096,host-nodes=%d' % (MAX_NODES - 1)],
> + stderr=PIPE, stdout=PIPE)
> + stdout,stderr = p.communicate()
> +
> + self.assertIn(b'host-nodes must be empty', stderr)
> + self.assertEquals(p.returncode, 1)
Perhaps this test should become part of a more comprehensive
menory-backend-ram test.
Reviewed-by: Markus Armbruster <armbru@redhat.com>
On Fri, Nov 30, 2018 at 09:37:49AM +0100, Markus Armbruster wrote:
> Eduardo Habkost <ehabkost@redhat.com> writes:
>
> > host_memory_backend_set_host_nodes() was not validating
> > host-nodes before writing to backend->host_nodes, making QEMU
> > write beyond the end of the bitmap.
> >
> > Fix the crash and add a simple regression test for the fix.
> >
> > Reported-by: Markus Armbruster <armbru@redhat.com>
> > Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
> > ---
> > backends/hostmem.c | 13 +++++++---
> > tests/acceptance/host-nodes-limit.py | 36 ++++++++++++++++++++++++++++
> > 2 files changed, 46 insertions(+), 3 deletions(-)
> > create mode 100644 tests/acceptance/host-nodes-limit.py
> >
> > diff --git a/backends/hostmem.c b/backends/hostmem.c
> > index 1a89342039..ef199d32fd 100644
> > --- a/backends/hostmem.c
> > +++ b/backends/hostmem.c
> > @@ -103,11 +103,18 @@ host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
> > {
> > #ifdef CONFIG_NUMA
> > HostMemoryBackend *backend = MEMORY_BACKEND(obj);
> > - uint16List *l = NULL;
> > + uint16List *l, *host_nodes = NULL;
> >
> > - visit_type_uint16List(v, name, &l, errp);
> > + visit_type_uint16List(v, name, &host_nodes, errp);
> > +
> > + for (l = host_nodes; l; l = l->next) {
> > + if (l->value >= MAX_NODES) {
> > + error_setg(errp, "Invalid host-nodes value: %d", l->value);
> > + return;
> > + }
> > + }
> >
> > - while (l) {
> > + for (l = host_nodes; l; l = l->next) {
> > bitmap_set(backend->host_nodes, l->value, 1);
> > l = l->next;
> > }
>
> Pre-existing: leaks the list created by visit_type_uint16List(), or am I
> confused?
>
> host_memory_backend_set_host_nodes() looks like it leaks, too.
>
> The function to free such lists is qapi_free_uint16List().
Thanks, I will fix it in v2.
>
> > diff --git a/tests/acceptance/host-nodes-limit.py b/tests/acceptance/host-nodes-limit.py
> > new file mode 100644
> > index 0000000000..e803e10104
> > --- /dev/null
> > +++ b/tests/acceptance/host-nodes-limit.py
> > @@ -0,0 +1,36 @@
> > +# Regression test for host-nodes limit validation
> > +#
> > +# Copyright (c) 2018 Red Hat, Inc.
> > +#
> > +# Author:
> > +# Eduardo Habkost <ehabkost@redhat.com>
> > +#
> > +# This work is licensed under the terms of the GNU GPL, version 2 or
> > +# later. See the COPYING file in the top-level directory.
> > +
> > +from avocado_qemu import Test
> > +from subprocess import Popen, PIPE
> > +
> > +MAX_NODES = 128
> > +
> > +class HostNodesValidation(Test):
> > + def test_large_host_nodes(self):
> > + p = Popen([self.qemu_bin, '-display', 'none', '-nodefaults',
> > + '-object', 'memory-backend-ram,id=m0,'
> > + 'size=4096,host-nodes=%d' % (MAX_NODES)],
> > + stderr=PIPE, stdout=PIPE)
> > + stdout,stderr = p.communicate()
> > +
> > + self.assertIn(b'Invalid host-nodes', stderr)
> > + self.assertEquals(stdout, b'')
> > + self.assertEquals(p.returncode, 1)
> > +
> > + def test_valid_host_nodes(self):
> > + p = Popen([self.qemu_bin, '-display', 'none', '-nodefaults',
> > + '-object', 'memory-backend-ram,id=m0,'
> > + 'size=4096,host-nodes=%d' % (MAX_NODES - 1)],
> > + stderr=PIPE, stdout=PIPE)
> > + stdout,stderr = p.communicate()
> > +
> > + self.assertIn(b'host-nodes must be empty', stderr)
> > + self.assertEquals(p.returncode, 1)
>
> Perhaps this test should become part of a more comprehensive
> menory-backend-ram test.
If such a test existed, yes.
>
> Reviewed-by: Markus Armbruster <armbru@redhat.com>
Thanks!
(Although this version is broken due to the extra "l = l->next"
line and we need v2)
--
Eduardo
On 30.11.18 09:37, Markus Armbruster wrote:
> Eduardo Habkost <ehabkost@redhat.com> writes:
>
>> host_memory_backend_set_host_nodes() was not validating
>> host-nodes before writing to backend->host_nodes, making QEMU
>> write beyond the end of the bitmap.
>>
>> Fix the crash and add a simple regression test for the fix.
>>
>> Reported-by: Markus Armbruster <armbru@redhat.com>
>> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
>> ---
>> backends/hostmem.c | 13 +++++++---
>> tests/acceptance/host-nodes-limit.py | 36 ++++++++++++++++++++++++++++
>> 2 files changed, 46 insertions(+), 3 deletions(-)
>> create mode 100644 tests/acceptance/host-nodes-limit.py
>>
>> diff --git a/backends/hostmem.c b/backends/hostmem.c
>> index 1a89342039..ef199d32fd 100644
>> --- a/backends/hostmem.c
>> +++ b/backends/hostmem.c
>> @@ -103,11 +103,18 @@ host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
>> {
>> #ifdef CONFIG_NUMA
>> HostMemoryBackend *backend = MEMORY_BACKEND(obj);
>> - uint16List *l = NULL;
>> + uint16List *l, *host_nodes = NULL;
>>
>> - visit_type_uint16List(v, name, &l, errp);
>> + visit_type_uint16List(v, name, &host_nodes, errp);
>> +
>> + for (l = host_nodes; l; l = l->next) {
>> + if (l->value >= MAX_NODES) {
>> + error_setg(errp, "Invalid host-nodes value: %d", l->value);
>> + return;
>> + }
>> + }
>>
>> - while (l) {
>> + for (l = host_nodes; l; l = l->next) {
>> bitmap_set(backend->host_nodes, l->value, 1);
>> l = l->next;
>> }
>
> Pre-existing: leaks the list created by visit_type_uint16List(), or am I
> confused?
>
I think you're right.
--
Thanks,
David / dhildenb
On 29.11.18 19:57, Eduardo Habkost wrote:
> host_memory_backend_set_host_nodes() was not validating
> host-nodes before writing to backend->host_nodes, making QEMU
> write beyond the end of the bitmap.
>
> Fix the crash and add a simple regression test for the fix.
>
> Reported-by: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
> ---
> backends/hostmem.c | 13 +++++++---
> tests/acceptance/host-nodes-limit.py | 36 ++++++++++++++++++++++++++++
> 2 files changed, 46 insertions(+), 3 deletions(-)
> create mode 100644 tests/acceptance/host-nodes-limit.py
>
> diff --git a/backends/hostmem.c b/backends/hostmem.c
> index 1a89342039..ef199d32fd 100644
> --- a/backends/hostmem.c
> +++ b/backends/hostmem.c
> @@ -103,11 +103,18 @@ host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
> {
> #ifdef CONFIG_NUMA
> HostMemoryBackend *backend = MEMORY_BACKEND(obj);
> - uint16List *l = NULL;
> + uint16List *l, *host_nodes = NULL;
>
> - visit_type_uint16List(v, name, &l, errp);
> + visit_type_uint16List(v, name, &host_nodes, errp);
> +
> + for (l = host_nodes; l; l = l->next) {
> + if (l->value >= MAX_NODES) {
> + error_setg(errp, "Invalid host-nodes value: %d", l->value);
> + return;
> + }
> + }
>
An alternative would be to use a temporary bitmap (on the stack)
instead and copy it only on success. E.g.
for (l = host_nodes; l; l = l->next) {
if (l->value >= MAX_NODE) {
error_setg(&local_err, "Invalid host-nodes value: %d", l->value);
break;
}
bitmap_set(tmp, l->value, 1);
}
if (!local_err) {
bitmap_copy(backend->host_nodes, tmp, MAX_NODE);
}
error_propagate...
free...
> - while (l) {
> + for (l = host_nodes; l; l = l->next) {
> bitmap_set(backend->host_nodes, l->value, 1);
> l = l->next;
> }
> diff --git a/tests/acceptance/host-nodes-limit.py b/tests/acceptance/host-nodes-limit.py
> new file mode 100644
> index 0000000000..e803e10104
> --- /dev/null
> +++ b/tests/acceptance/host-nodes-limit.py
> @@ -0,0 +1,36 @@
> +# Regression test for host-nodes limit validation
> +#
> +# Copyright (c) 2018 Red Hat, Inc.
> +#
> +# Author:
> +# Eduardo Habkost <ehabkost@redhat.com>
> +#
> +# This work is licensed under the terms of the GNU GPL, version 2 or
> +# later. See the COPYING file in the top-level directory.
> +
> +from avocado_qemu import Test
> +from subprocess import Popen, PIPE
> +
> +MAX_NODES = 128
> +
> +class HostNodesValidation(Test):
> + def test_large_host_nodes(self):
> + p = Popen([self.qemu_bin, '-display', 'none', '-nodefaults',
> + '-object', 'memory-backend-ram,id=m0,'
> + 'size=4096,host-nodes=%d' % (MAX_NODES)],
> + stderr=PIPE, stdout=PIPE)
> + stdout,stderr = p.communicate()
> +
> + self.assertIn(b'Invalid host-nodes', stderr)
> + self.assertEquals(stdout, b'')
> + self.assertEquals(p.returncode, 1)
> +
> + def test_valid_host_nodes(self):
> + p = Popen([self.qemu_bin, '-display', 'none', '-nodefaults',
> + '-object', 'memory-backend-ram,id=m0,'
> + 'size=4096,host-nodes=%d' % (MAX_NODES - 1)],
> + stderr=PIPE, stdout=PIPE)
> + stdout,stderr = p.communicate()
> +
> + self.assertIn(b'host-nodes must be empty', stderr)
> + self.assertEquals(p.returncode, 1)
>
--
Thanks,
David / dhildenb
© 2016 - 2026 Red Hat, Inc.