From nobody Tue Feb 10 16:57:56 2026 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 15163808828431019.8378893523105; Fri, 19 Jan 2018 08:54:42 -0800 (PST) Received: from localhost ([::1]:59112 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ecZwA-0006FV-3d for importer@patchew.org; Fri, 19 Jan 2018 11:54:42 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46667) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ecZf8-0007rl-Oa for qemu-devel@nongnu.org; Fri, 19 Jan 2018 11:37:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ecZf8-00081j-2c for qemu-devel@nongnu.org; Fri, 19 Jan 2018 11:37:06 -0500 Received: from mx1.redhat.com ([209.132.183.28]:42116) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ecZf7-00081D-Sh for qemu-devel@nongnu.org; Fri, 19 Jan 2018 11:37:05 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0FA6021BB0; Fri, 19 Jan 2018 16:37:05 +0000 (UTC) Received: from localhost (ovpn-116-6.gru2.redhat.com [10.97.116.6]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5C39060A9D; Fri, 19 Jan 2018 16:37:01 +0000 (UTC) From: Eduardo Habkost To: Peter Maydell , qemu-devel@nongnu.org Date: Fri, 19 Jan 2018 14:33:45 -0200 Message-Id: <20180119163345.10649-20-ehabkost@redhat.com> In-Reply-To: <20180119163345.10649-1-ehabkost@redhat.com> References: <20180119163345.10649-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 19 Jan 2018 16:37:05 +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] [PULL v2 19/19] fw_cfg: fix memory corruption when all fw_cfg slots are used 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: Marcel Apfelbaum Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Marcel Apfelbaum When all the fw_cfg slots are used, a write is made outside the bounds of the fw_cfg files array as part of the sort algorithm. Fix it by avoiding an unnecessary array element move. Fix also an assert while at it. Signed-off-by: Marcel Apfelbaum Message-Id: <20180108215007.46471-1-marcel@redhat.com> Reviewed-by: Laszlo Ersek Signed-off-by: Eduardo Habkost --- hw/nvram/fw_cfg.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hw/nvram/fw_cfg.c b/hw/nvram/fw_cfg.c index 753ac0e4ea..4313484b21 100644 --- a/hw/nvram/fw_cfg.c +++ b/hw/nvram/fw_cfg.c @@ -784,7 +784,7 @@ void fw_cfg_add_file_callback(FWCfgState *s, const cha= r *filename, * index and "i - 1" is the one being copied from, thus the * unusual start and end in the for statement. */ - for (i =3D count + 1; i > index; i--) { + for (i =3D count; i > index; i--) { s->files->f[i] =3D s->files->f[i - 1]; s->files->f[i].select =3D cpu_to_be16(FW_CFG_FILE_FIRST + i); s->entries[0][FW_CFG_FILE_FIRST + i] =3D @@ -833,7 +833,6 @@ void *fw_cfg_modify_file(FWCfgState *s, const char *fil= ename, assert(s->files); =20 index =3D be32_to_cpu(s->files->count); - assert(index < fw_cfg_file_slots(s)); =20 for (i =3D 0; i < index; i++) { if (strcmp(filename, s->files->f[i].name) =3D=3D 0) { @@ -843,6 +842,9 @@ void *fw_cfg_modify_file(FWCfgState *s, const char *fil= ename, return ptr; } } + + assert(index < fw_cfg_file_slots(s)); + /* add new one */ fw_cfg_add_file_callback(s, filename, NULL, NULL, NULL, data, len, tru= e); return NULL; --=20 2.14.3