orinoco: improve robustness of firmware load
v2 cover orinoco: improve robustness of firmware load
We don't currently do any verification of the firmware that we attempt to load, and it is possible to cause a kernel oops. For example, see
<http://marc.info/?l=orinoco-users&m=123411762524637>
This series introduces a few nominal checks to reject files which are incorrectly formatted, and then makes sure that we do not attempt to read data outside the firmware.
It is still possible to load a duff firmware. However it shouldn't be possible to cause any permanent damage. Hopefully it can't oops either.
---
David Kilroy (2):
orinoco: validate firmware header
orinoco: prevent accessing memory outside the firmware image
drivers/net/wireless/orinoco/fw.c | 49 +++++++++++++-- drivers/net/wireless/orinoco/hermes_dld.c | 99 +++++++++++++++-------------- drivers/net/wireless/orinoco/hermes_dld.h | 12 +++- 3 files changed, 104 insertions(+), 56 deletions(-)
PATCH v2 1/2 orinoco: validate firmware header
Check the Agere firmware headers for validity before attempting to download it.
--- drivers/net/wireless/orinoco/fw.c | 37 +++++++++++++++++++++++++++++++++++++ 1 files changed, 37 insertions(+), 0 deletions(-)
drivers/net/wireless/orinoco/fw.c +37 -0
@@ -43,6 +43,33 @@ struct orinoco_fw_header {char signature[0 ];/* FW signature length headersize-20 */ } __attribute__ ((packed)); +/* Check the range of various header entries. Return a pointer to a +* description of the problem, or NULL if everything checks out. */ +static const char *validate_fw (const struct orinoco_fw_header *hdr,size_t len) +{ +u16 hdrsize; + +if (len <sizeof (*hdr)) +return "image too small" ; +if (memcmp (hdr->hdr_vers,"HFW" ,3 ) !=0 ) +return "format not recognised" ; + + hdrsize =le16_to_cpu (hdr->headersize); +if (hdrsize > len) +return "bad headersize" ; +if ((hdrsize +le32_to_cpu (hdr->block_offset)) > len) +return "bad block offset" ; +if ((hdrsize +le32_to_cpu (hdr->pdr_offset)) > len) +return "bad PDR offset" ; +if ((hdrsize +le32_to_cpu (hdr->pri_offset)) > len) +return "bad PRI offset" ; +if ((hdrsize +le32_to_cpu (hdr->compat_offset)) > len) +return "bad compat offset" ; + +/* TODO: consider adding a checksum or CRC to the firmware format */ +return NULL ; +} +/* Download either STA or AP firmware into the card. */ static int orinoco_dl_firmware(struct orinoco_private *priv,
@@ -58,6 +85,7 @@ orinoco_dl_firmware(struct orinoco_private *priv,const unsigned char *first_block;const unsigned char *end;const char *firmware; +const char *fw_err;struct net_device *dev = priv->ndev;int err =0 ;
@@ -93,6 +121,15 @@ orinoco_dl_firmware(struct orinoco_private *priv, hdr = (const struct orinoco_fw_header *) fw_entry->data; + fw_err =validate_fw (hdr, fw_entry->size); +if (fw_err) { +printk (KERN_WARNING "%s: Invalid firmware image detected (%s). " +"Aborting download\n" , + dev->name, fw_err); + err = -EINVAL ; + goto abort; + } +/* Enable aux port to allow programming */ err =hermesi_program_init (hw,le32_to_cpu (hdr->entry_point));printk (KERN_DEBUG "%s: Program init returned %d\n" , dev->name, err);
Signature · 1 line
1.6.0.6
PATCH v2 2/2 orinoco: prevent accessing memory outside the firmware image
In reply to David KilroyDo this by indicating the end of the appropriate regions of memory.
Note that MAX_PDA_SIZE should only apply to the PDA block read from flash/EEPROM, and has been erronously applied to the pdr elements. Remove the macro, and use the actual PDA size passed down by the caller.
We also fix up some of the types used, marking as much as possible const, and using void* for the end pointers.
--- drivers/net/wireless/orinoco/fw.c | 12 ++-- drivers/net/wireless/orinoco/hermes_dld.c | 99 +++++++++++++++-------------- drivers/net/wireless/orinoco/hermes_dld.h | 12 +++- 3 files changed, 67 insertions(+), 56 deletions(-)
drivers/net/wireless/orinoco/fw.c +7 -5
@@ -83,7 +83,7 @@ orinoco_dl_firmware(struct orinoco_private *priv,const struct firmware *fw_entry;const struct orinoco_fw_header *hdr;const unsigned char *first_block; -const unsigned char *end; +const void *end;const char *firmware;const char *fw_err;struct net_device *dev = priv->ndev;
@@ -152,7 +152,8 @@ orinoco_dl_firmware(struct orinoco_private *priv,le16_to_cpu (hdr->headersize) + le32_to_cpu(hdr->pdr_offset)); - err =hermes_apply_pda_with_defaults (hw, first_block, pda); + err =hermes_apply_pda_with_defaults (hw, first_block, end, pda, + &pda[fw->pda_size /sizeof (*pda)]);printk (KERN_DEBUG "%s: Apply PDA returned %d\n" , dev->name, err);if (err) goto abort;
@@ -184,7 +185,7 @@ free:*/ static int symbol_dl_image (struct orinoco_private *priv,const struct fw_info *fw, -const unsigned char *image,const unsigned char *end, +const unsigned char *image,const void *end,int secondary) {hermes_t *hw = &priv->hw;
@@ -225,9 +226,10 @@ symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,/* Write the PDA to the adapter */ if (secondary) { -size_t len =hermes_blocks_length (first_block); +size_t len =hermes_blocks_length (first_block, end); ptr = first_block + len; - ret =hermes_apply_pda (hw, ptr, pda); + ret =hermes_apply_pda (hw, ptr, end, pda, + &pda[fw->pda_size /sizeof (*pda)]);kfree (pda);if (ret)return ret;
drivers/net/wireless/orinoco/hermes_dld.c +52 -47
215 lines not on this page · raw message
drivers/net/wireless/orinoco/hermes_dld.h +8 -4
@@ -29,7 +29,7 @@int hermesi_program_init (hermes_t *hw,u32 offset);int hermesi_program_end (hermes_t *hw); -int hermes_program (hermes_t *hw,const char *first_block,const char *end); +int hermes_program (hermes_t *hw,const char *first_block,const void *end); int hermes_read_pda(hermes_t *hw, __le16 *pda,
@@ -38,11 +38,15 @@ int hermes_read_pda(hermes_t *hw, int use_eeprom);int hermes_apply_pda (hermes_t *hw,const char *first_pdr, -const __le16 *pda); +const void *pdr_end, +const __le16 *pda, +const void *pda_end);int hermes_apply_pda_with_defaults (hermes_t *hw,const char *first_pdr, -const __le16 *pda); +const void *pdr_end, +const __le16 *pda, +const void *pda_end); -size_t hermes_blocks_length (const char *first_block); +size_t hermes_blocks_length (const char *first_block,const void *end);#endif /* _HERMES_DLD_H */
Signature · 1 line
1.6.0.6