watchdog: dw_wdt: Fix buffer overflow when get timeout
PATCH 1/1 watchdog: dw_wdt: Fix buffer overflow when get timeout
The top_val can be obtained from device-tree, if it is not configured correctly, there will be buffer overflow.
--- drivers/watchdog/dw_wdt.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/watchdog/dw_wdt.c +6 -0
@@ -155,6 +155,9 @@ static unsigned int dw_wdt_get_min_timeout(struct dw_wdt *dw_wdt)break ; } +if (WARN_ON_ONCE (idx ==DW_WDT_NUM_TOPS )) + idx =DW_WDT_NUM_TOPS -1 ; +return dw_wdt->timeouts[idx].sec; }
@@ -178,6 +181,9 @@ static unsigned int dw_wdt_get_timeout(struct dw_wdt *dw_wdt)break ; } +if (WARN_ON_ONCE (idx ==DW_WDT_NUM_TOPS )) + idx =DW_WDT_NUM_TOPS -1 ; +/* * In IRQ mode due to the two stages counter, the actual timeout is * twice greater than the TOP setting.
Signature · 1 line
2.29.0
Guenter Roeck
maintainerQuoted 13 lines · Schspa Shi
The
top_valcan be obtained from device-tree, if it is not configured correctly, there will be buffer overflow.Signed-off-by Schspa Shi <[email protected]>--- drivers/watchdog/dw_wdt.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/watchdog/dw_wdt.c +0
-0
drivers/watchdog/dw_wdt.c static unsigned int dw_wdt_get_min_timeout(struct dw_wdt *dw_wdt) · line 160
Quoted 6 lines
drivers/watchdog/dw_wdt.c +3
-0@@ -155,6 +155,9 @@ static unsigned int dw_wdt_get_min_timeout(struct dw_wdt *dw_wdt)break ; } +if (WARN_ON_ONCE (idx ==DW_WDT_NUM_TOPS )) + idx =DW_WDT_NUM_TOPS -1 ; +
dw_wdt_get_min_timeout() returns the lowest non-0 configurable timeout. The last entry in the timeout array must not be 0, meaning there must be at least one entry in the array where the timeout is not 0. Therefore this situation can not happen.
drivers/watchdog/dw_wdt.c static unsigned int dw_wdt_get_timeout(struct dw_wdt *dw_wdt) · line 186
Quoted 10 lines
drivers/watchdog/dw_wdt.c +3
-0return dw_wdt->timeouts[idx].sec; }@@ -178,6 +181,9 @@ static unsigned int dw_wdt_get_timeout(struct dw_wdt *dw_wdt)break ; } +if (WARN_ON_ONCE (idx ==DW_WDT_NUM_TOPS )) + idx =DW_WDT_NUM_TOPS -1 ; +
idx is derived from a top_val value written into WDOG_TIMEOUT_RANGE_REG_OFFSET, and the value written is derived from an entry in the timeouts array. This array contains an entry for each possible top_val. While the array is not sorted by top_val, dw_wdt_handle_tops() still guarantees that an entry exists.
I do not see how bad devicetree data can circumvent that. If it does, please provide an example and explain.
Thanks,
Guenter
Quoted 13 lines · Guenter Roeck
Quoted 12 lines · Schspa Shi
The
top_valcan be obtained from device-tree, if it is not configured correctly, there will be buffer overflow.Signed-off-by Schspa Shi <[email protected]>--- drivers/watchdog/dw_wdt.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/watchdog/dw_wdt.c +0
-0@@ -155,6 +155,9 @@ static unsigned int dw_wdt_get_min_timeout(struct dw_wdt *dw_wdt)break ;
Quoted 9 lines
drivers/watchdog/dw_wdt.c static unsigned int dw_wdt_get_min_timeout(struct dw_wdt *dw_wdt) · line 160
Quoted 4 lines
drivers/watchdog/dw_wdt.c +2
-0} + if (WARN_ON_ONCE(idx == DW_WDT_NUM_TOPS)) + idx = DW_WDT_NUM_TOPS - 1; +
dw_wdt_get_min_timeout()returns the lowest non-0 configurable timeout. The last entry in the timeout array must not be 0, meaning there must be at least one entry in the array where the timeout is not 0. Therefore this situation can not happen.
Yes, I have seen this, you are correct, sorry for the bad patch.
Quoted 10 lines
drivers/watchdog/dw_wdt.c
Quoted 9 lines
drivers/watchdog/dw_wdt.c +0
-0return dw_wdt->timeouts[idx].sec; } @@ -178,6 +181,9 @@ static unsigned int dw_wdt_get_timeout(struct dw_wdt
*dw_wdt)break; }+ if (
WARN_ON_ONCE(idx == DW_WDT_NUM_TOPS))drivers/watchdog/dw_wdt.c +2
-0+ idx =DW_WDT_NUM_TOPS -1 ; +
Quoted 8 lines
idx is derived from a
top_valvalue written intoWDOG_TIMEOUT_RANGE_REG_OFFSET, and the value written is derived from an entry in the timeouts array. This array contains an entry for each possibletop_val. While the array is not sorted bytop_val,dw_wdt_handle_tops()still guarantees that an entry exists.I do not see how bad devicetree data can circumvent that. If it does, please provide an example and explain.
My bad, there is no problem.
Quoted 2 lines
Thanks,
Guenter
Signature · 2 lines
BRs Schspa Shi