* [PATCH 2.6.21-rc5] bnx2: fix buffer overrun in bnx2_nvram_write
@ 2007-03-29 22:47 Cureington, Tony
2007-03-30 1:27 ` Michael Chan
0 siblings, 1 reply; 2+ messages in thread
From: Cureington, Tony @ 2007-03-29 22:47 UTC (permalink / raw)
To: netdev; +Cc: mchan, Cureington, Tony
A buffer overrun issue exists in the bnx2_nvram_write function. This
issue
is exposed by calling ethtool to write to the eeprom as shown below.
ethtool -E eth4 magic 0x669955AA offset 0x137 value 0xC4
The problem happens when align_buf is allocated only 2 bytes in the
kmalloc
call and later 4 bytes written to it. The len32 variable is calculated
in the
below code segment at around line 3102.
if ((align_start = (offset32 & 3))) {
offset32 &= ~3;
len32 += (4 - align_start);
:
The next "if" conditional in the code that could alter these values is
not
entered, so we end up with:
len32=2
align_start=3
offset32=308
This results in align_buf being allocated 2 bytes, and overwritten with
4 bytes in the "if (align_start)" segment just below the allocation.
I made a change adjust the len32 to the next 4 byte boundary.
I also made changes so the call to erase the eeprom page is not called
unless
the page will be written. To achieve this I just moved the code inside
the
conditional that writes the page. After doing this I realized the page
erase
functions were no-ops since the same conditional checked for before the
page
is written in this function is checked in the erase function. I left the
changes in because it makes it more logical and removes unneeded calls.
Also note that if len32 is not on a 4 byte boundary the, data_end
variable
(offset32 + len32 in this case) will never equal data_start plus a
multiple of 4 to cause the BNX2_NVM_COMMAND_LAST to be issued to the
board
at around line 3220.
Signed-Off-By: Tony Cureington (tony.cureington@hp.com)
--- linux-2.6.21-rc5.orig/drivers/net/bnx2.c 2007-03-29
12:49:33.000000000 -0500
+++ linux/drivers/net/bnx2.c 2007-03-29 17:06:36.000000000 -0500
@@ -3116,6 +3116,10 @@
}
if (align_start || align_end) {
+ if (len32&3) {
+ // adjust to 4 byte boundary
+ len32 += 4-(len32&3);
+ }
align_buf = kmalloc(len32, GFP_KERNEL);
if (align_buf == NULL)
return -ENOMEM;
@@ -3187,17 +3191,17 @@
if ((rc = bnx2_enable_nvram_write(bp)) != 0)
goto nvram_write_end;
- /* Erase the page */
- if ((rc = bnx2_nvram_erase_page(bp, page_start)) != 0)
- goto nvram_write_end;
-
- /* Re-enable the write again for the actual write */
- bnx2_enable_nvram_write(bp);
-
/* Loop to write back the buffer data from page_start to
* data_start */
i = 0;
if (bp->flash_info->buffered == 0) {
+ /* Erase the page */
+ if ((rc = bnx2_nvram_erase_page(bp, page_start))
!= 0)
+ goto nvram_write_end;
+
+ /* Re-enable the write again for the actual
write */
+ bnx2_enable_nvram_write(bp);
+
for (addr = page_start; addr < data_start;
addr += 4, i += 4) {
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH 2.6.21-rc5] bnx2: fix buffer overrun in bnx2_nvram_write 2007-03-29 22:47 [PATCH 2.6.21-rc5] bnx2: fix buffer overrun in bnx2_nvram_write Cureington, Tony @ 2007-03-30 1:27 ` Michael Chan 0 siblings, 0 replies; 2+ messages in thread From: Michael Chan @ 2007-03-30 1:27 UTC (permalink / raw) To: Cureington, Tony, davem; +Cc: netdev On Thu, 2007-03-29 at 22:47 +0000, Cureington, Tony wrote: > A buffer overrun issue exists in the bnx2_nvram_write function. Tony, thanks for the patch. The alignment logic is indeed still broken despite trying to fix it before. I think the following patch is better in fixing the alignment logic. I agree with you about moving the code to erase the page, and that part of your patch is preserved. [BNX2]: Fix nvram write logic. The nvram dword alignment logic was broken when writing less than 4 bytes on a non-aligned offset. It was missing logic to round the length to 4 bytes. The page erase code is also moved so that it is only called when using non-buffered flash for better code clarity. Update version to 1.5.7. Based on initial patch from Tony Cureington <tony.cureington@hp.com>. Signed-off-by: Michael Chan <mchan@broadcom.com> diff --git a/drivers/net/bnx2.c b/drivers/net/bnx2.c index d43fe28..0b7aded 100644 --- a/drivers/net/bnx2.c +++ b/drivers/net/bnx2.c @@ -54,8 +54,8 @@ #define DRV_MODULE_NAME "bnx2" #define PFX DRV_MODULE_NAME ": " -#define DRV_MODULE_VERSION "1.5.6" -#define DRV_MODULE_RELDATE "March 28, 2007" +#define DRV_MODULE_VERSION "1.5.7" +#define DRV_MODULE_RELDATE "March 29, 2007" #define RUN_AT(x) (jiffies + (x)) @@ -3099,20 +3099,18 @@ bnx2_nvram_write(struct bnx2 *bp, u32 offset, u8 *data_buf, if ((align_start = (offset32 & 3))) { offset32 &= ~3; - len32 += (4 - align_start); + len32 += align_start; + if (len32 < 4) + len32 = 4; if ((rc = bnx2_nvram_read(bp, offset32, start, 4))) return rc; } if (len32 & 3) { - if ((len32 > 4) || !align_start) { - align_end = 4 - (len32 & 3); - len32 += align_end; - if ((rc = bnx2_nvram_read(bp, offset32 + len32 - 4, - end, 4))) { - return rc; - } - } + align_end = 4 - (len32 & 3); + len32 += align_end; + if ((rc = bnx2_nvram_read(bp, offset32 + len32 - 4, end, 4))) + return rc; } if (align_start || align_end) { @@ -3187,17 +3185,17 @@ bnx2_nvram_write(struct bnx2 *bp, u32 offset, u8 *data_buf, if ((rc = bnx2_enable_nvram_write(bp)) != 0) goto nvram_write_end; - /* Erase the page */ - if ((rc = bnx2_nvram_erase_page(bp, page_start)) != 0) - goto nvram_write_end; - - /* Re-enable the write again for the actual write */ - bnx2_enable_nvram_write(bp); - /* Loop to write back the buffer data from page_start to * data_start */ i = 0; if (bp->flash_info->buffered == 0) { + /* Erase the page */ + if ((rc = bnx2_nvram_erase_page(bp, page_start)) != 0) + goto nvram_write_end; + + /* Re-enable the write again for the actual write */ + bnx2_enable_nvram_write(bp); + for (addr = page_start; addr < data_start; addr += 4, i += 4) { ^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-03-30 0:43 UTC | newest] Thread overview: 2+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2007-03-29 22:47 [PATCH 2.6.21-rc5] bnx2: fix buffer overrun in bnx2_nvram_write Cureington, Tony 2007-03-30 1:27 ` Michael Chan
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox