qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] fw_cfg: fix memory corruption when all fw_cfg slots are used
@ 2018-01-08 21:50 Marcel Apfelbaum
  2018-01-09 11:15 ` Marc-André Lureau
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Marcel Apfelbaum @ 2018-01-08 21:50 UTC (permalink / raw)
  To: qemu-devel; +Cc: marcel, mst, ehabkost, lersek, kraxel, qemu-stable

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 <marcel@redhat.com>
---
 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 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] =
@@ -833,7 +833,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) {
@@ -843,6 +842,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, NULL, data, len, true);
     return NULL;
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2018-01-12 19:47 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-01-08 21:50 [Qemu-devel] [PATCH] fw_cfg: fix memory corruption when all fw_cfg slots are used Marcel Apfelbaum
2018-01-09 11:15 ` Marc-André Lureau
2018-01-09 12:36   ` Marcel Apfelbaum
2018-01-09 12:54     ` Laszlo Ersek
2018-01-09 12:51 ` Laszlo Ersek
2018-01-09 13:48   ` Marcel Apfelbaum
2018-01-09 13:09 ` Laszlo Ersek
2018-01-09 13:18   ` Marcel Apfelbaum
2018-01-09 13:33     ` Laszlo Ersek
2018-01-09 13:35       ` Laszlo Ersek
2018-01-09 13:36         ` Laszlo Ersek
2018-01-09 13:41           ` Marcel Apfelbaum
2018-01-12 19:47 ` Eduardo Habkost

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).