netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] net: filter: be more defensive on div/mod by X==0
@ 2014-04-04 23:04 Daniel Borkmann
  2014-04-07 16:55 ` David Miller
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Borkmann @ 2014-04-04 23:04 UTC (permalink / raw)
  To: davem; +Cc: ast, netdev

The old interpreter behaviour was that we returned with 0
whenever we found a division by 0 would take place. In the new
interpreter we would currently just skip that instead and
continue execution.

It's true that a value of 0 as return might not be appropriate
in all cases, but current users (socket filters -> drop
packet, seccomp -> SECCOMP_RET_KILL, cls_bpf -> unclassified,
etc) seem fine with that behaviour. Better this than undefined
BPF program behaviour as it's expected that A contains the
result of the division. In future, as more use cases open up,
we could further adapt this return value to our needs, if
necessary.

So reintroduce return of 0 for division by 0 as in the old
interpreter. Also in case of K which is guaranteed to be 32bit
wide, sk_chk_filter() already takes care of preventing division
by 0 invoked through K, so we can generally spare us these tests.

Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Reviewed-by: Alexei Starovoitov <ast@plumgrid.com>
---
 net/core/filter.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index 765556b..e08b382 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -295,43 +295,43 @@ select_insn:
 		(*(s64 *) &A) >>= K;
 		CONT;
 	BPF_ALU64_BPF_MOD_BPF_X:
+		if (unlikely(X == 0))
+			return 0;
 		tmp = A;
-		if (X)
-			A = do_div(tmp, X);
+		A = do_div(tmp, X);
 		CONT;
 	BPF_ALU_BPF_MOD_BPF_X:
+		if (unlikely(X == 0))
+			return 0;
 		tmp = (u32) A;
-		if (X)
-			A = do_div(tmp, (u32) X);
+		A = do_div(tmp, (u32) X);
 		CONT;
 	BPF_ALU64_BPF_MOD_BPF_K:
 		tmp = A;
-		if (K)
-			A = do_div(tmp, K);
+		A = do_div(tmp, K);
 		CONT;
 	BPF_ALU_BPF_MOD_BPF_K:
 		tmp = (u32) A;
-		if (K)
-			A = do_div(tmp, (u32) K);
+		A = do_div(tmp, (u32) K);
 		CONT;
 	BPF_ALU64_BPF_DIV_BPF_X:
-		if (X)
-			do_div(A, X);
+		if (unlikely(X == 0))
+			return 0;
+		do_div(A, X);
 		CONT;
 	BPF_ALU_BPF_DIV_BPF_X:
+		if (unlikely(X == 0))
+			return 0;
 		tmp = (u32) A;
-		if (X)
-			do_div(tmp, (u32) X);
+		do_div(tmp, (u32) X);
 		A = (u32) tmp;
 		CONT;
 	BPF_ALU64_BPF_DIV_BPF_K:
-		if (K)
-			do_div(A, K);
+		do_div(A, K);
 		CONT;
 	BPF_ALU_BPF_DIV_BPF_K:
 		tmp = (u32) A;
-		if (K)
-			do_div(tmp, (u32) K);
+		do_div(tmp, (u32) K);
 		A = (u32) tmp;
 		CONT;
 	BPF_ALU_BPF_END_BPF_TO_BE:
-- 
1.7.11.7

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net] net: filter: be more defensive on div/mod by X==0
  2014-04-04 23:04 [PATCH net] net: filter: be more defensive on div/mod by X==0 Daniel Borkmann
@ 2014-04-07 16:55 ` David Miller
  0 siblings, 0 replies; 2+ messages in thread
From: David Miller @ 2014-04-07 16:55 UTC (permalink / raw)
  To: dborkman; +Cc: ast, netdev

From: Daniel Borkmann <dborkman@redhat.com>
Date: Sat,  5 Apr 2014 01:04:03 +0200

> The old interpreter behaviour was that we returned with 0
> whenever we found a division by 0 would take place. In the new
> interpreter we would currently just skip that instead and
> continue execution.
> 
> It's true that a value of 0 as return might not be appropriate
> in all cases, but current users (socket filters -> drop
> packet, seccomp -> SECCOMP_RET_KILL, cls_bpf -> unclassified,
> etc) seem fine with that behaviour. Better this than undefined
> BPF program behaviour as it's expected that A contains the
> result of the division. In future, as more use cases open up,
> we could further adapt this return value to our needs, if
> necessary.
> 
> So reintroduce return of 0 for division by 0 as in the old
> interpreter. Also in case of K which is guaranteed to be 32bit
> wide, sk_chk_filter() already takes care of preventing division
> by 0 invoked through K, so we can generally spare us these tests.
> 
> Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
> Reviewed-by: Alexei Starovoitov <ast@plumgrid.com>

Applied, thanks Daniel.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-04-07 16:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-04 23:04 [PATCH net] net: filter: be more defensive on div/mod by X==0 Daniel Borkmann
2014-04-07 16:55 ` David Miller

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