Skip to contentSkip to conversation

net: stmmac: Fix possible division by zero

PATCH Superseded Ameer Hamza 3 participants 4 messages Dec 2, 2021 – Dec 7, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

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
index da8306f60730..f44400323407 100644
@@ -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
maintainer
#1 lore raw
Quoted 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")

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.

#2 lore raw

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.

Fixes a6da2bbb0005e ("net: stmmac: retain PTP clock time during SIOCSHWTSTAMP ioctls")

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
index da8306f60730..f44400323407 100644
@@ -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
#3 lore raw
Quoted 22 lines · Ameer Hamza

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.

Fixes a6da2bbb0005e ("net: stmmac: retain PTP clock time during SIOCSHWTSTAMP ioctls")

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 +0 -0
index da8306f60730..f44400323407 100644

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
index 4e05c1d92935..e2e232bff511 100644
@@ -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