netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: weichenchen <weichen.chen@linux.alibaba.com>
To: kuba@kernel.org
Cc: splendidsky.cwc@alibaba-inc.com, yanxu.zw@alibaba-inc.com,
	weichenchen <weichen.chen@linux.alibaba.com>,
	"David S. Miller" <davem@davemloft.net>,
	Hangbin Liu <liuhangbin@gmail.com>,
	David Ahern <dsahern@kernel.org>,
	Roopa Prabhu <roopa@cumulusnetworks.com>,
	Roman Mashak <mrv@mojatatu.com>,
	Vasily Averin <vvs@virtuozzo.com>, Jeff Dike <jdike@akamai.com>,
	Li RongQing <lirongqing@baidu.com>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2] net: neighbor: fix a crash caused by mod zero
Date: Mon, 21 Dec 2020 21:07:44 +0800	[thread overview]
Message-ID: <20201221130754.12628-1-weichen.chen@linux.alibaba.com> (raw)
In-Reply-To: <20201219102116.3cc0d74c@kicinski-fedora-pc1c0hjn.dhcp.thefacebook.com>

pneigh_enqueue() tries to obtain a random delay by mod
NEIGH_VAR(p, PROXY_DELAY). However, NEIGH_VAR(p, PROXY_DELAY)
migth be zero at that point because someone could write zero
to /proc/sys/net/ipv4/neigh/[device]/proxy_delay after the
callers check it.

This patch double-checks NEIGH_VAR(p, PROXY_DELAY) in
pneigh_enqueue() to ensure not to take zero as modulus.

Signed-off-by: weichenchen <weichen.chen@linux.alibaba.com>
---
V2:
    - Use READ_ONCE() to prevent the complier from re-reading
      NEIGH_VAR(p, PROXY_DELAY).
    - Give a hint to the complier that delay <= 0 is unlikely
      to happen.

Note: I don't think having the caller pass in the value is a
good idea mainly because delay should be only decided by
/proc/sys/net/ipv4/neigh/[device]/proxy_delay rather than the
caller.
---
 net/core/neighbour.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/net/core/neighbour.c b/net/core/neighbour.c
index 9500d28a43b0..7b03d3f129c0 100644
--- a/net/core/neighbour.c
+++ b/net/core/neighbour.c
@@ -1570,9 +1570,14 @@ void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
 		    struct sk_buff *skb)
 {
 	unsigned long now = jiffies;
+	unsigned long sched_next;
 
-	unsigned long sched_next = now + (prandom_u32() %
-					  NEIGH_VAR(p, PROXY_DELAY));
+	int delay = READ_ONCE(NEIGH_VAR(p, PROXY_DELAY));
+
+	if (unlikely(delay <= 0))
+		sched_next = now;
+	else
+		sched_next = now + (prandom_u32() % delay);
 
 	if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) {
 		kfree_skb(skb);
-- 
2.20.1 (Apple Git-117)


  reply	other threads:[~2020-12-21 13:09 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-18  4:20 [PATCH] net: neighbor: fix a crash caused by mod zero weichenchen
2020-12-19 18:21 ` Jakub Kicinski
2020-12-21 13:07   ` weichenchen [this message]
2020-12-21 19:32     ` [PATCH v2] " Jakub Kicinski
2020-12-22 12:38       ` [PATCH v3] " weichenchen
2020-12-22 16:34         ` Eric Dumazet
2020-12-25  5:44           ` [PATCH v4] " weichenchen
2020-12-28 22:51             ` David Miller

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=20201221130754.12628-1-weichen.chen@linux.alibaba.com \
    --to=weichen.chen@linux.alibaba.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=jdike@akamai.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lirongqing@baidu.com \
    --cc=liuhangbin@gmail.com \
    --cc=mrv@mojatatu.com \
    --cc=netdev@vger.kernel.org \
    --cc=roopa@cumulusnetworks.com \
    --cc=splendidsky.cwc@alibaba-inc.com \
    --cc=vvs@virtuozzo.com \
    --cc=yanxu.zw@alibaba-inc.com \
    /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).