netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ameer Hamza <amhamza.mgc@gmail.com>
To: davem@davemloft.net, kuba@kernel.org, mcoquelin.stm32@gmail.com,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: amhamza.mgc@gmail.com, h.assmann@pengutronix.de
Subject: [PATCH v2] net: stmmac: Fix possible division by zero
Date: Mon,  6 Dec 2021 19:23:37 +0500	[thread overview]
Message-ID: <20211206142337.28602-1-amhamza.mgc@gmail.com> (raw)
In-Reply-To: <20211203173708.7fdbed06@kicinski-fedora-pc1c0hjn.dhcp.thefacebook.com>

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


  reply	other threads:[~2021-12-06 14:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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   ` Ameer Hamza [this message]
2021-12-07  8:31     ` [PATCH v2] " Leon Romanovsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211206142337.28602-1-amhamza.mgc@gmail.com \
    --to=amhamza.mgc@gmail.com \
    --cc=davem@davemloft.net \
    --cc=h.assmann@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).