From nobody Thu May 2 07:55:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1543517936458702.2684301322741; Thu, 29 Nov 2018 10:58:56 -0800 (PST) Received: from localhost ([::1]:55797 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gSRWZ-0003Ul-9m for importer@patchew.org; Thu, 29 Nov 2018 13:58:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48315) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gSRVj-00034f-Nr for qemu-devel@nongnu.org; Thu, 29 Nov 2018 13:58:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gSRVg-0007gY-Vb for qemu-devel@nongnu.org; Thu, 29 Nov 2018 13:58:03 -0500 Received: from mx1.redhat.com ([209.132.183.28]:59750) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gSRVg-0007gL-Ob for qemu-devel@nongnu.org; Thu, 29 Nov 2018 13:58:00 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7449EA04B3 for ; Thu, 29 Nov 2018 18:57:59 +0000 (UTC) Received: from localhost (ovpn-116-21.gru2.redhat.com [10.97.116.21]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0A2601750D; Thu, 29 Nov 2018 18:57:55 +0000 (UTC) From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Thu, 29 Nov 2018 16:57:54 -0200 Message-Id: <20181129185754.7009-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 29 Nov 2018 18:57:59 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH] hostmem: Validate host-nodes before setting bitmap X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Igor Mammedov , Markus Armbruster , David Hildenbrand Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 Signed-off-by: Eduardo Habkost Reviewed-by: Markus Armbruster --- 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, Visit= or *v, const char *name, { #ifdef CONFIG_NUMA HostMemoryBackend *backend =3D MEMORY_BACKEND(obj); - uint16List *l =3D NULL; + uint16List *l, *host_nodes =3D NULL; =20 - visit_type_uint16List(v, name, &l, errp); + visit_type_uint16List(v, name, &host_nodes, errp); + + for (l =3D host_nodes; l; l =3D l->next) { + if (l->value >=3D MAX_NODES) { + error_setg(errp, "Invalid host-nodes value: %d", l->value); + return; + } + } =20 - while (l) { + for (l =3D host_nodes; l; l =3D l->next) { bitmap_set(backend->host_nodes, l->value, 1); l =3D l->next; } diff --git a/tests/acceptance/host-nodes-limit.py b/tests/acceptance/host-n= odes-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 +# +# 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 =3D 128 + +class HostNodesValidation(Test): + def test_large_host_nodes(self): + p =3D Popen([self.qemu_bin, '-display', 'none', '-nodefaults', + '-object', 'memory-backend-ram,id=3Dm0,' + 'size=3D4096,host-nodes=3D%d' % (MAX_NODES)], + stderr=3DPIPE, stdout=3DPIPE) + stdout,stderr =3D 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 =3D Popen([self.qemu_bin, '-display', 'none', '-nodefaults', + '-object', 'memory-backend-ram,id=3Dm0,' + 'size=3D4096,host-nodes=3D%d' % (MAX_NODES -= 1)], + stderr=3DPIPE, stdout=3DPIPE) + stdout,stderr =3D p.communicate() + + self.assertIn(b'host-nodes must be empty', stderr) + self.assertEquals(p.returncode, 1) --=20 2.18.0.rc1.1.g3f1ff2140