Skip to contentSkip to conversation

hw/nvram/fw_cfg: Assert linked data is not NULL

PATCH In review Philippe Mathieu-Daudé 2 participants 2 messages Sep 16, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/nvram/fw_cfg: Assert linked data is not NULL

The linked data passed to the fw_cfg device must not be NULL. Add an assertion.

---
 hw/nvram/fw_cfg.c | 1 +
 1 file changed, 1 insertion(+)
hw/nvram/fw_cfg.c +1 -0
index f3a4728288e..40dcc08b57b 100644
@@ -711,6 +711,7 @@ static void fw_cfg_add_bytes_callback(FWCfgState *s, uint16_t key,
 
     key &= FW_CFG_ENTRY_MASK;
 
+    assert(data);
     assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX);
     assert(s->entries[arch][key].data == NULL); /* avoid key conflict */
 
Signature · 1 line
2.26.2
#1 lore raw

Hi Phil,

Quoted 15 lines · Philippe Mathieu-Daudé

The linked data passed to the fw_cfg device must not be NULL. Add an assertion.

---
 hw/nvram/fw_cfg.c | 1 +
 1 file changed, 1 insertion(+)
hw/nvram/fw_cfg.c +0 -0
index f3a4728288e..40dcc08b57b 100644

hw/nvram/fw_cfg.c static void fw_cfg_add_bytes_callback(FWCfgState *s, uint16_t key, · line 717

Quoted 7 lines
hw/nvram/fw_cfg.c +1 -0
@@ -711,6 +711,7 @@ static void fw_cfg_add_bytes_callback(FWCfgState *s, uint16_t key,
 
     key &= FW_CFG_ENTRY_MASK;
 
+    assert(data);
     assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX);
     assert(s->entries[arch][key].data == NULL); /* avoid key conflict */
 

(1) this assert() would fire without my

[PATCH] hw/nvram/fw_cfg: fix FWCfgDataGeneratorClass::get_data() consumption

so minimally we should apply your patch only after mine.

(2) I'm unsure if it's *always* a programming error to link a NULL datum under an fw_cfg key. The reader functions fw_cfg_data_read() and fw_cfg_dma_transfer() deal with NULL data gracefully.

I think we should add the assert() only if we can prove that "include/hw/nvram/fw_cfg.h" already forbids callers from passing in NULL data. Otherwise, we'll have to audit all the fw-cfg-add-xxx call sites, extend the comments in the header file, and *then* add the assert().

For now I'd only go with the patch I posted.

Thanks!
Laszlo