block/pflash_cfi02: Fix memory leak and potential use-after-free
PATCH 1/1 block/pflash_cfi02: Fix memory leak and potential use-after-free
Don't dynamically allocate the pflash's timer. But do use timer_del in an unrealize function to make sure that the timer can't fire after the pflash_t has been freed.
--- hw/block/pflash_cfi02.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
hw/block/pflash_cfi02.c +11 -4
@@ -84,7 +84,7 @@ struct pflash_t {uint16_t unlock_addr0;uint16_t unlock_addr1;uint8_t cfi_table[0x52 ]; -QEMUTimer *timer; +QEMUTimer timer;/* The device replicates the flash memory across its memory space. Emulate * that by having a container (.mem) filled with an array of aliases * (.mem_mappings) pointing to the flash memory (.orig_mem).
@@ -429,7 +429,7 @@ static void pflash_write (pflash_t *pfl, hwaddr offset, } pfl->status =0x00 ;/* Let's wait 5 seconds before chip erase is done */ -timer_mod (pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + +timer_mod (&pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + (NANOSECONDS_PER_SECOND *5 ));break ;case 0x30 :
@@ -444,7 +444,7 @@ static void pflash_write (pflash_t *pfl, hwaddr offset, } pfl->status =0x00 ;/* Let's wait 1/2 second before sector erase is done */ -timer_mod (pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + +timer_mod (&pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + (NANOSECONDS_PER_SECOND /2 ));break ;default :
@@ -596,7 +596,7 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp) pfl->rom_mode =1 ;sysbus_init_mmio (SYS_BUS_DEVICE (dev), &pfl->mem); - pfl->timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , pflash_timer, pfl); +timer_init_ns (&pfl->timer,QEMU_CLOCK_VIRTUAL , pflash_timer, pfl); pfl->wcycle =0 ; pfl->cmd =0 ; pfl->status =0 ;
@@ -695,11 +695,18 @@ static Property pflash_cfi02_properties[] = { DEFINE_PROP_END_OF_LIST(), }; +static void pflash_cfi02_unrealize (DeviceState *dev,Error **errp) +{ +pflash_t *pfl =CFI_PFLASH02 (dev); +timer_del (&pfl->timer); +} +static void pflash_cfi02_class_init (ObjectClass *klass,void *data) {DeviceClass *dc =DEVICE_CLASS (klass); dc->realize = pflash_cfi02_realize; + dc->unrealize = pflash_cfi02_unrealize; dc->props = pflash_cfi02_properties;set_bit (DEVICE_CATEGORY_STORAGE , dc->categories); }
Signature · 1 line
2.17.2 (Apple Git-113)
Quoted 41 lines · Stephen Checkoway
Don't dynamically allocate the pflash's timer. But do use
timer_delin an unrealize function to make sure that the timer can't fire after thepflash_thas been freed.Signed-off-by Stephen Checkoway <[email protected]>--- hw/block/pflash_cfi02.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)hw/block/pflash_cfi02.c +3
-3@@ -84,7 +84,7 @@ struct pflash_t {uint16_t unlock_addr0;uint16_t unlock_addr1;uint8_t cfi_table[0x52 ]; -QEMUTimer *timer; +QEMUTimer timer;/* The device replicates the flash memory across its memory space. Emulate * that by having a container (.mem) filled with an array of aliases * (.mem_mappings) pointing to the flash memory (.orig_mem). @@ -429,7 +429,7 @@ static void pflash_write (pflash_t *pfl, hwaddr offset, } pfl->status =0x00 ;/* Let's wait 5 seconds before chip erase is done */ -timer_mod (pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + +timer_mod (&pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + (NANOSECONDS_PER_SECOND *5 ));break ;case 0x30 :@@ -444,7 +444,7 @@ static void pflash_write (pflash_t *pfl, hwaddr offset, } pfl->status =0x00 ;/* Let's wait 1/2 second before sector erase is done */ -timer_mod (pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + +timer_mod (&pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + (NANOSECONDS_PER_SECOND /2 ));break ;default :
hw/block/pflash_cfi02.c static void pflash_cfi02_realize(DeviceState *dev, Error **errp) · line 599
Quoted 5 lines
hw/block/pflash_cfi02.c +1
-1@@ -596,7 +596,7 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp) pfl->rom_mode =1 ;sysbus_init_mmio (SYS_BUS_DEVICE (dev), &pfl->mem); - pfl->timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , pflash_timer, pfl); +timer_init_ns (&pfl->timer,QEMU_CLOCK_VIRTUAL , pflash_timer, pfl);
Quoted 23 lines
hw/block/pflash_cfi02.c +7
-0pfl->wcycle =0 ; pfl->cmd =0 ; pfl->status =0 ;@@ -695,11 +695,18 @@ static Property pflash_cfi02_properties[] = { DEFINE_PROP_END_OF_LIST(), }; +static void pflash_cfi02_unrealize (DeviceState *dev,Error **errp) +{ +pflash_t *pfl =CFI_PFLASH02 (dev); +timer_del (&pfl->timer); +} +static void pflash_cfi02_class_init (ObjectClass *klass,void *data) {DeviceClass *dc =DEVICE_CLASS (klass); dc->realize = pflash_cfi02_realize; + dc->unrealize = pflash_cfi02_unrealize; dc->props = pflash_cfi02_properties;set_bit (DEVICE_CATEGORY_STORAGE , dc->categories); }
Quoted 5 lines · Stephen Checkoway
Don't dynamically allocate the pflash's timer. But do use
timer_delin an unrealize function to make sure that the timer can't fire after thepflash_thas been freed.Signed-off-by Stephen Checkoway <[email protected]>
Quoted 66 lines
---
hw/block/pflash_cfi02.c| 15 +++++++++++----1 file changed, 11 insertions(+), 4 deletions(-)hw/block/pflash_cfi02.c +11
-4@@ -84,7 +84,7 @@ struct pflash_t {uint16_t unlock_addr0;uint16_t unlock_addr1;uint8_t cfi_table[0x52 ]; -QEMUTimer *timer; +QEMUTimer timer;/* The device replicates the flash memory across its memory space. Emulate * that by having a container (.mem) filled with an array of aliases * (.mem_mappings) pointing to the flash memory (.orig_mem). @@ -429,7 +429,7 @@ static void pflash_write (pflash_t *pfl, hwaddr offset, } pfl->status =0x00 ;/* Let's wait 5 seconds before chip erase is done */ -timer_mod (pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + +timer_mod (&pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + (NANOSECONDS_PER_SECOND *5 ));break ;case 0x30 :@@ -444,7 +444,7 @@ static void pflash_write (pflash_t *pfl, hwaddr offset, } pfl->status =0x00 ;/* Let's wait 1/2 second before sector erase is done */ -timer_mod (pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + +timer_mod (&pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + (NANOSECONDS_PER_SECOND /2 ));break ;default :@@ -596,7 +596,7 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp) pfl->rom_mode =1 ;sysbus_init_mmio (SYS_BUS_DEVICE (dev), &pfl->mem); - pfl->timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , pflash_timer, pfl); +timer_init_ns (&pfl->timer,QEMU_CLOCK_VIRTUAL , pflash_timer, pfl); pfl->wcycle =0 ; pfl->cmd =0 ; pfl->status =0 ;@@ -695,11 +695,18 @@ static Property pflash_cfi02_properties[] = { DEFINE_PROP_END_OF_LIST(), }; +static void pflash_cfi02_unrealize (DeviceState *dev,Error **errp) +{ +pflash_t *pfl =CFI_PFLASH02 (dev); +timer_del (&pfl->timer); +} +static void pflash_cfi02_class_init (ObjectClass *klass,void *data) {DeviceClass *dc =DEVICE_CLASS (klass); dc->realize = pflash_cfi02_realize; + dc->unrealize = pflash_cfi02_unrealize; dc->props = pflash_cfi02_properties;set_bit (DEVICE_CATEGORY_STORAGE , dc->categories); }Signature · 1 line
2.17.2 (Apple Git-113)
Signature · 2 lines
Wei Yang Help you, Help me
Laurent Vivier
AppliedQuoted 59 lines · Stephen Checkoway
Don't dynamically allocate the pflash's timer. But do use
timer_delin an unrealize function to make sure that the timer can't fire after thepflash_thas been freed.Signed-off-by Stephen Checkoway <[email protected]>--- hw/block/pflash_cfi02.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)hw/block/pflash_cfi02.c +9
-4@@ -84,7 +84,7 @@ struct pflash_t {uint16_t unlock_addr0;uint16_t unlock_addr1;uint8_t cfi_table[0x52 ]; -QEMUTimer *timer; +QEMUTimer timer;/* The device replicates the flash memory across its memory space. Emulate * that by having a container (.mem) filled with an array of aliases * (.mem_mappings) pointing to the flash memory (.orig_mem). @@ -429,7 +429,7 @@ static void pflash_write (pflash_t *pfl, hwaddr offset, } pfl->status =0x00 ;/* Let's wait 5 seconds before chip erase is done */ -timer_mod (pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + +timer_mod (&pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + (NANOSECONDS_PER_SECOND *5 ));break ;case 0x30 :@@ -444,7 +444,7 @@ static void pflash_write (pflash_t *pfl, hwaddr offset, } pfl->status =0x00 ;/* Let's wait 1/2 second before sector erase is done */ -timer_mod (pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + +timer_mod (&pfl->timer,qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + (NANOSECONDS_PER_SECOND /2 ));break ;default :@@ -596,7 +596,7 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp) pfl->rom_mode =1 ;sysbus_init_mmio (SYS_BUS_DEVICE (dev), &pfl->mem); - pfl->timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , pflash_timer, pfl); +timer_init_ns (&pfl->timer,QEMU_CLOCK_VIRTUAL , pflash_timer, pfl); pfl->wcycle =0 ; pfl->cmd =0 ; pfl->status =0 ;@@ -695,11 +695,18 @@ static Property pflash_cfi02_properties[] = { DEFINE_PROP_END_OF_LIST(), }; +static void pflash_cfi02_unrealize (DeviceState *dev,Error **errp) +{ +pflash_t *pfl =CFI_PFLASH02 (dev); +timer_del (&pfl->timer); +}
hw/block/pflash_cfi02.c static Property pflash_cfi02_properties[] = { · line 712
Quoted 10 lines
hw/block/pflash_cfi02.c +2
-0+static void pflash_cfi02_class_init (ObjectClass *klass,void *data) {DeviceClass *dc =DEVICE_CLASS (klass); dc->realize = pflash_cfi02_realize; + dc->unrealize = pflash_cfi02_unrealize; dc->props = pflash_cfi02_properties;set_bit (DEVICE_CATEGORY_STORAGE , dc->categories); }
Applied to my trivial-patches branch.
Thanks,
Laurent
Quoted 5 lines · Laurent Vivier
Applied to my trivial-patches branch.
Great, thanks!
Cheers,
Steve