From bd03bbaa869e5e2bbee2bae1d5e74bd15ba17760 Mon Sep 17 00:00:00 2001 From: Marcel Apfelbaum Date: Tue, 16 Jan 2018 13:58:32 +0100 Subject: [PATCH 1/2] fw_cfg: fix memory corruption when all fw_cfg slots are used MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RH-Author: Marcel Apfelbaum Message-id: <20180116135832.99402-1-marcel@redhat.com> Patchwork-id: 78627 O-Subject: [RHEL-7.4.z qemu-kvm-rhev PATCH] fw_cfg: fix memory corruption when all fw_cfg slots are used Bugzilla: 1534649 RH-Acked-by: Marc-André Lureau RH-Acked-by: Eduardo Habkost RH-Acked-by: Igor Mammedov 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 (cherry picked from commit 45eda6c8eb45107630da670bc993074cf85ef64c) Signed-off-by: Marcel Apfelbaum Signed-off-by: Miroslav Rezanina --- 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 6fc63d2..e1444f9 100644 --- a/hw/nvram/fw_cfg.c +++ b/hw/nvram/fw_cfg.c @@ -828,7 +828,7 @@ void fw_cfg_add_file_callback(FWCfgState *s, const char *filename, * index and "i - 1" is the one being copied from, thus the * unusual start and end in the for statement. */ - for (i = count + 1; i > index; i--) { + for (i = count; i > index; i--) { s->files->f[i] = s->files->f[i - 1]; s->files->f[i].select = cpu_to_be16(FW_CFG_FILE_FIRST + i); s->entries[0][FW_CFG_FILE_FIRST + i] = @@ -876,7 +876,6 @@ void *fw_cfg_modify_file(FWCfgState *s, const char *filename, assert(s->files); index = be32_to_cpu(s->files->count); - assert(index < fw_cfg_file_slots(s)); for (i = 0; i < index; i++) { if (strcmp(filename, s->files->f[i].name) == 0) { @@ -886,6 +885,9 @@ void *fw_cfg_modify_file(FWCfgState *s, const char *filename, return ptr; } } + + assert(index < fw_cfg_file_slots(s)); + /* add new one */ fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, true); return NULL; -- 1.8.3.1