Blame SOURCES/kvm-block-pflash_cfi02-Fix-memory-leak-and-potential-use.patch

383d26
From 689898009c6930b8a9ce598e85678bfc0f131594 Mon Sep 17 00:00:00 2001
383d26
From: Markus Armbruster <armbru@redhat.com>
383d26
Date: Fri, 17 May 2019 06:50:51 +0200
383d26
Subject: [PATCH 24/53] block/pflash_cfi02: Fix memory leak and potential
383d26
 use-after-free
383d26
MIME-Version: 1.0
383d26
Content-Type: text/plain; charset=UTF-8
383d26
Content-Transfer-Encoding: 8bit
383d26
383d26
RH-Author: Markus Armbruster <armbru@redhat.com>
383d26
Message-id: <20190517065120.12028-3-armbru@redhat.com>
383d26
Patchwork-id: 87984
383d26
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH v3 02/31] block/pflash_cfi02: Fix memory leak and potential use-after-free
383d26
Bugzilla: 1624009
383d26
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
383d26
RH-Acked-by: Thomas Huth <thuth@redhat.com>
383d26
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
383d26
From: Stephen Checkoway <stephen.checkoway@oberlin.edu>
383d26
383d26
Don't dynamically allocate the pflash's timer. But do use timer_del in
383d26
an unrealize function to make sure that the timer can't fire after the
383d26
pflash_t has been freed.
383d26
383d26
Signed-off-by: Stephen Checkoway <stephen.checkoway@oberlin.edu>
383d26
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
383d26
Reviewed-by: Wei Yang <richardw.yang@linux.intel.com>
383d26
Message-Id: <20190219153727.62279-1-stephen.checkoway@oberlin.edu>
383d26
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
383d26
(cherry picked from commit d80cf1eb2e87df3a9bfb226bcc7fb3a1aa858817)
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 hw/block/pflash_cfi02.c | 15 +++++++++++----
383d26
 1 file changed, 11 insertions(+), 4 deletions(-)
383d26
383d26
diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c
383d26
index 75d1ae1..cbc3d4d 100644
383d26
--- a/hw/block/pflash_cfi02.c
383d26
+++ b/hw/block/pflash_cfi02.c
383d26
@@ -84,7 +84,7 @@ struct pflash_t {
383d26
     uint16_t unlock_addr0;
383d26
     uint16_t unlock_addr1;
383d26
     uint8_t cfi_table[0x52];
383d26
-    QEMUTimer *timer;
383d26
+    QEMUTimer timer;
383d26
     /* The device replicates the flash memory across its memory space.  Emulate
383d26
      * that by having a container (.mem) filled with an array of aliases
383d26
      * (.mem_mappings) pointing to the flash memory (.orig_mem).
383d26
@@ -431,7 +431,7 @@ static void pflash_write (pflash_t *pfl, hwaddr offset,
383d26
             }
383d26
             pfl->status = 0x00;
383d26
             /* Let's wait 5 seconds before chip erase is done */
383d26
-            timer_mod(pfl->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
383d26
+            timer_mod(&pfl->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
383d26
                       (NANOSECONDS_PER_SECOND * 5));
383d26
             break;
383d26
         case 0x30:
383d26
@@ -446,7 +446,7 @@ static void pflash_write (pflash_t *pfl, hwaddr offset,
383d26
             }
383d26
             pfl->status = 0x00;
383d26
             /* Let's wait 1/2 second before sector erase is done */
383d26
-            timer_mod(pfl->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
383d26
+            timer_mod(&pfl->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
383d26
                       (NANOSECONDS_PER_SECOND / 2));
383d26
             break;
383d26
         default:
383d26
@@ -658,7 +658,7 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp)
383d26
     pfl->rom_mode = 1;
383d26
     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem);
383d26
 
383d26
-    pfl->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, pflash_timer, pfl);
383d26
+    timer_init_ns(&pfl->timer, QEMU_CLOCK_VIRTUAL, pflash_timer, pfl);
383d26
     pfl->wcycle = 0;
383d26
     pfl->cmd = 0;
383d26
     pfl->status = 0;
383d26
@@ -757,11 +757,18 @@ static Property pflash_cfi02_properties[] = {
383d26
     DEFINE_PROP_END_OF_LIST(),
383d26
 };
383d26
 
383d26
+static void pflash_cfi02_unrealize(DeviceState *dev, Error **errp)
383d26
+{
383d26
+    pflash_t *pfl = CFI_PFLASH02(dev);
383d26
+    timer_del(&pfl->timer);
383d26
+}
383d26
+
383d26
 static void pflash_cfi02_class_init(ObjectClass *klass, void *data)
383d26
 {
383d26
     DeviceClass *dc = DEVICE_CLASS(klass);
383d26
 
383d26
     dc->realize = pflash_cfi02_realize;
383d26
+    dc->unrealize = pflash_cfi02_unrealize;
383d26
     dc->props = pflash_cfi02_properties;
383d26
     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
383d26
 }
383d26
-- 
383d26
1.8.3.1
383d26