net: stmmac: Fix possible division by zero
Subsystems (4)
PATCH 1/1 net: stmmac: Fix possible division by zero
Fix for divide by zero error reported by Coverity.
Addresses-Coverity: 1494557 ("Division or modulo by zero")
--- drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +1 -1
@@ -863,7 +863,7 @@ int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags)stmmac_config_sub_second_increment (priv, priv->ptpaddr, priv->plat->clk_ptp_rate, xmac, &sec_inc); - temp =div_u64 (1000000000ULL , sec_inc); + temp =div_u64 (1000000000ULL , (sec_inc >0 ) ? sec_inc :1 );/* Store sub second increment for later use */ priv->sub_second_inc = sec_inc;
Signature · 1 line
2.25.1
Jakub Kicinski
maintainerQuoted 5 lines · On Fri, 3 Dec 2021 03:37:29 +0500 Ameer Hamza wrote:
Fix for divide by zero error reported by Coverity.
Addresses-Coverity: 1494557 ("Division or modulo by zero")
Signed-off-by Ameer Hamza <[email protected]>
Please include justification that this issue can actually happen, Fixes tag pointing to the commit which introduced the problem, and CC the author and reviewers of that commit.
PATCH v2 1/1 net: stmmac: Fix possible division by zero
In stmmac_init_tstamp_counter() routine, there is a possiblity of division by zero. If priv->plat->clk_ptp_rate becomes greater than 1 GHz, config_sub_second_increment() subroutine may calculate sec_inc as zero depending upon the PTP_TCR_TSCFUPDT register value, which will cause divide by zero exception.
Addresses-Coverity: 1494557 ("Division or modulo by zero")
---
Changes in v2:
Added fix tag, bug justification, and commit author.
--- drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +1 -1
@@ -863,7 +863,7 @@ int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags)stmmac_config_sub_second_increment (priv, priv->ptpaddr, priv->plat->clk_ptp_rate, xmac, &sec_inc); - temp =div_u64 (1000000000ULL , sec_inc); + temp =div_u64 (1000000000ULL , (sec_inc >0 ) ? sec_inc :1 );/* Store sub second increment for later use */ priv->sub_second_inc = sec_inc;
Signature · 1 line
2.25.1
Quoted 22 lines · Ameer Hamza
In
stmmac_init_tstamp_counter()routine, there is a possiblity of division by zero. Ifpriv->plat->clk_ptp_ratebecomes greater than 1 GHz,config_sub_second_increment()subroutine may calculatesec_incas zero depending upon thePTP_TCR_TSCFUPDTregister value, which will cause divide by zero exception.Fixes a6da2bbb0005e ("net: stmmac: retain PTP clock time during SIOCSHWTSTAMP ioctls")Addresses-Coverity: 1494557 ("Division or modulo by zero")
Signed-off-by Ameer Hamza <[email protected]>---
Changes in v2:
Added fix tag, bug justification, and commit author.--- drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +0
-0
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags) · line 866
Quoted 5 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +1
-1@@ -863,7 +863,7 @@ int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags)stmmac_config_sub_second_increment (priv, priv->ptpaddr, priv->plat->clk_ptp_rate, xmac, &sec_inc); - temp =div_u64 (1000000000ULL , sec_inc); + temp =div_u64 (1000000000ULL , (sec_inc >0 ) ? sec_inc :1 );
It can be written cleanly with max(a,b):
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +1 -1
@@ -855,7 +855,7 @@ int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags)stmmac_config_sub_second_increment (priv, priv->ptpaddr, priv->plat->clk_ptp_rate, xmac, &sec_inc); - temp =div_u64 (1000000000ULL , sec_inc); + temp =div_u64 (1000000000ULL ,max (sec_inc,1 ));/* Store sub second increment for later use */ priv->sub_second_inc = sec_inc;
Quoted 6 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +0
-0/* Store sub second increment for later use */ priv->sub_second_inc = sec_inc;Signature · 1 line
2.25.1