diff mbox series

[v2] net: neighbor: fix a crash caused by mod zero

Message ID 20201221130754.12628-1-weichen.chen@linux.alibaba.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [v2] net: neighbor: fix a crash caused by mod zero | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Guessed tree name to be net-next
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cc_maintainers warning 2 maintainers not CCed: nikolay@cumulusnetworks.com viro@zeniv.linux.org.uk
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 3 this patch: 3
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 16 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 3 this patch: 3
netdev/header_inline success Link
netdev/stable success Stable not CCed

Commit Message

weichenchen Dec. 21, 2020, 1:07 p.m. UTC
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(-)

Comments

Jakub Kicinski Dec. 21, 2020, 7:32 p.m. UTC | #1
On Mon, 21 Dec 2020 21:07:44 +0800 weichenchen wrote:
> 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.

In terms of not breaking abstraction? The decision to call 
this helper or not is made in the caller. And both callers 
do a NEIGH_VAR(p, PROXY_DELAY) == 0 check before making the 
call.

It seems like if the caller used READ_ONCE and passed the value 
in we would save ourselves the potentially surprising code flow.

> 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);
diff mbox series

Patch

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);