* [PATCH] net: stmmac: Fix possible division by zero
@ 2021-12-02 22:37 Ameer Hamza
2021-12-04 1:37 ` Jakub Kicinski
0 siblings, 1 reply; 4+ messages in thread
From: Ameer Hamza @ 2021-12-02 22:37 UTC (permalink / raw)
To: davem, kuba, mcoquelin.stm32, netdev, linux-kernel; +Cc: amhamza.mgc
Fix for divide by zero error reported by Coverity.
Addresses-Coverity: 1494557 ("Division or modulo by zero")
Signed-off-by: Ameer Hamza <amhamza.mgc@gmail.com>
---
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
index da8306f60730..f44400323407 100644
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
@@ -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;
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] net: stmmac: Fix possible division by zero 2021-12-02 22:37 [PATCH] net: stmmac: Fix possible division by zero Ameer Hamza @ 2021-12-04 1:37 ` Jakub Kicinski 2021-12-06 14:23 ` [PATCH v2] " Ameer Hamza 0 siblings, 1 reply; 4+ messages in thread From: Jakub Kicinski @ 2021-12-04 1:37 UTC (permalink / raw) To: Ameer Hamza; +Cc: davem, mcoquelin.stm32, netdev, linux-kernel 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 <amhamza.mgc@gmail.com> 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. ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] net: stmmac: Fix possible division by zero 2021-12-04 1:37 ` Jakub Kicinski @ 2021-12-06 14:23 ` Ameer Hamza 2021-12-07 8:31 ` Leon Romanovsky 0 siblings, 1 reply; 4+ messages in thread From: Ameer Hamza @ 2021-12-06 14:23 UTC (permalink / raw) To: davem, kuba, mcoquelin.stm32, netdev, linux-kernel; +Cc: amhamza.mgc, h.assmann 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") Signed-off-by: Ameer Hamza <amhamza.mgc@gmail.com> --- 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(-) diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c index da8306f60730..f44400323407 100644 --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c @@ -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; -- 2.25.1 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] net: stmmac: Fix possible division by zero 2021-12-06 14:23 ` [PATCH v2] " Ameer Hamza @ 2021-12-07 8:31 ` Leon Romanovsky 0 siblings, 0 replies; 4+ messages in thread From: Leon Romanovsky @ 2021-12-07 8:31 UTC (permalink / raw) To: Ameer Hamza; +Cc: davem, kuba, mcoquelin.stm32, netdev, linux-kernel, h.assmann On Mon, Dec 06, 2021 at 07:23:37PM +0500, Ameer Hamza wrote: > 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") > Signed-off-by: Ameer Hamza <amhamza.mgc@gmail.com> > > --- > 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(-) > > diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c > index da8306f60730..f44400323407 100644 > --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c > +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c > @@ -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): diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c index 4e05c1d92935..e2e232bff511 100644 --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c @@ -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; > > /* Store sub second increment for later use */ > priv->sub_second_inc = sec_inc; > -- > 2.25.1 > ^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-12-07 8:31 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2021-12-02 22:37 [PATCH] net: stmmac: Fix possible division by zero Ameer Hamza 2021-12-04 1:37 ` Jakub Kicinski 2021-12-06 14:23 ` [PATCH v2] " Ameer Hamza 2021-12-07 8:31 ` Leon Romanovsky
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox