Skip to content

Commit aee636c

Browse files
edumazetdavem330
authored andcommitted
bpf: do not use reciprocal divide
At first Jakub Zawadzki noticed that some divisions by reciprocal_divide were not correct. (off by one in some cases) http://www.wireshark.org/~darkjames/reciprocal-buggy.c He could also show this with BPF: http://www.wireshark.org/~darkjames/set-and-dump-filter-k-bug.c The reciprocal divide in linux kernel is not generic enough, lets remove its use in BPF, as it is not worth the pain with current cpus. Signed-off-by: Eric Dumazet <edumazet@google.com> Reported-by: Jakub Zawadzki <darkjames-ws@darkjames.pl> Cc: Mircea Gherzan <mgherzan@gmail.com> Cc: Daniel Borkmann <dxchgb@gmail.com> Cc: Hannes Frederic Sowa <hannes@stressinduktion.org> Cc: Matt Evans <matt@ozlabs.org> Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Heiko Carstens <heiko.carstens@de.ibm.com> Cc: David S. Miller <davem@davemloft.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ba42fad commit aee636c

6 files changed

Lines changed: 45 additions & 46 deletions

File tree

‎arch/arm/net/bpf_jit_32.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,10 @@ static int build_body(struct jit_ctx *ctx)
641641
emit(ARM_MUL(r_A, r_A, r_X), ctx);
642642
break;
643643
case BPF_S_ALU_DIV_K:
644-
/* current k == reciprocal_value(userspace k) */
644+
if (k == 1)
645+
break;
645646
emit_mov_i(r_scratch, k, ctx);
646-
/* A = top 32 bits of the product */
647-
emit(ARM_UMULL(r_scratch, r_A, r_A, r_scratch), ctx);
647+
emit_udiv(r_A, r_A, r_scratch, ctx);
648648
break;
649649
case BPF_S_ALU_DIV_X:
650650
update_on_xread(ctx);

‎arch/powerpc/net/bpf_jit_comp.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,11 @@ static int bpf_jit_build_body(struct sk_filter *fp, u32 *image,
223223
}
224224
PPC_DIVWU(r_A, r_A, r_X);
225225
break;
226-
case BPF_S_ALU_DIV_K: /* A = reciprocal_divide(A, K); */
226+
case BPF_S_ALU_DIV_K: /* A /= K */
227+
if (K == 1)
228+
break;
227229
PPC_LI32(r_scratch1, K);
228-
/* Top 32 bits of 64bit result -> A */
229-
PPC_MULHWU(r_A, r_A, r_scratch1);
230+
PPC_DIVWU(r_A, r_A, r_scratch1);
230231
break;
231232
case BPF_S_ALU_AND_X:
232233
ctx->seen |= SEEN_XREG;

‎arch/s390/net/bpf_jit_comp.c‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,13 @@ static int bpf_jit_insn(struct bpf_jit *jit, struct sock_filter *filter,
371371
/* dr %r4,%r12 */
372372
EMIT2(0x1d4c);
373373
break;
374-
case BPF_S_ALU_DIV_K: /* A = reciprocal_divide(A, K) */
375-
/* m %r4,<d(K)>(%r13) */
376-
EMIT4_DISP(0x5c40d000, EMIT_CONST(K));
377-
/* lr %r5,%r4 */
378-
EMIT2(0x1854);
374+
case BPF_S_ALU_DIV_K: /* A /= K */
375+
if (K == 1)
376+
break;
377+
/* lhi %r4,0 */
378+
EMIT4(0xa7480000);
379+
/* d %r4,<d(K)>(%r13) */
380+
EMIT4_DISP(0x5d40d000, EMIT_CONST(K));
379381
break;
380382
case BPF_S_ALU_MOD_X: /* A %= X */
381383
jit->seen |= SEEN_XREG | SEEN_RET0;
@@ -391,6 +393,11 @@ static int bpf_jit_insn(struct bpf_jit *jit, struct sock_filter *filter,
391393
EMIT2(0x1854);
392394
break;
393395
case BPF_S_ALU_MOD_K: /* A %= K */
396+
if (K == 1) {
397+
/* lhi %r5,0 */
398+
EMIT4(0xa7580000);
399+
break;
400+
}
394401
/* lhi %r4,0 */
395402
EMIT4(0xa7480000);
396403
/* d %r4,<d(K)>(%r13) */

‎arch/sparc/net/bpf_jit_comp.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,20 @@ void bpf_jit_compile(struct sk_filter *fp)
497497
case BPF_S_ALU_MUL_K: /* A *= K */
498498
emit_alu_K(MUL, K);
499499
break;
500-
case BPF_S_ALU_DIV_K: /* A /= K */
501-
emit_alu_K(MUL, K);
502-
emit_read_y(r_A);
500+
case BPF_S_ALU_DIV_K: /* A /= K with K != 0*/
501+
if (K == 1)
502+
break;
503+
emit_write_y(G0);
504+
#ifdef CONFIG_SPARC32
505+
/* The Sparc v8 architecture requires
506+
* three instructions between a %y
507+
* register write and the first use.
508+
*/
509+
emit_nop();
510+
emit_nop();
511+
emit_nop();
512+
#endif
513+
emit_alu_K(DIV, K);
503514
break;
504515
case BPF_S_ALU_DIV_X: /* A /= X; */
505516
emit_cmpi(r_X, 0);

‎arch/x86/net/bpf_jit_comp.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,21 @@ void bpf_jit_compile(struct sk_filter *fp)
359359
EMIT2(0x89, 0xd0); /* mov %edx,%eax */
360360
break;
361361
case BPF_S_ALU_MOD_K: /* A %= K; */
362+
if (K == 1) {
363+
CLEAR_A();
364+
break;
365+
}
362366
EMIT2(0x31, 0xd2); /* xor %edx,%edx */
363367
EMIT1(0xb9);EMIT(K, 4); /* mov imm32,%ecx */
364368
EMIT2(0xf7, 0xf1); /* div %ecx */
365369
EMIT2(0x89, 0xd0); /* mov %edx,%eax */
366370
break;
367-
case BPF_S_ALU_DIV_K: /* A = reciprocal_divide(A, K); */
368-
EMIT3(0x48, 0x69, 0xc0); /* imul imm32,%rax,%rax */
369-
EMIT(K, 4);
370-
EMIT4(0x48, 0xc1, 0xe8, 0x20); /* shr $0x20,%rax */
371+
case BPF_S_ALU_DIV_K: /* A /= K */
372+
if (K == 1)
373+
break;
374+
EMIT2(0x31, 0xd2); /* xor %edx,%edx */
375+
EMIT1(0xb9);EMIT(K, 4); /* mov imm32,%ecx */
376+
EMIT2(0xf7, 0xf1); /* div %ecx */
371377
break;
372378
case BPF_S_ALU_AND_X:
373379
seen |= SEEN_XREG;

‎net/core/filter.c‎

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include <asm/uaccess.h>
3737
#include <asm/unaligned.h>
3838
#include <linux/filter.h>
39-
#include <linux/reciprocal_div.h>
4039
#include <linux/ratelimit.h>
4140
#include <linux/seccomp.h>
4241
#include <linux/if_vlan.h>
@@ -166,7 +165,7 @@ unsigned int sk_run_filter(const struct sk_buff *skb,
166165
A /= X;
167166
continue;
168167
case BPF_S_ALU_DIV_K:
169-
A = reciprocal_divide(A, K);
168+
A /= K;
170169
continue;
171170
case BPF_S_ALU_MOD_X:
172171
if (X == 0)
@@ -553,11 +552,6 @@ int sk_chk_filter(struct sock_filter *filter, unsigned int flen)
553552
/* Some instructions need special checks */
554553
switch (code) {
555554
case BPF_S_ALU_DIV_K:
556-
/* check for division by zero */
557-
if (ftest->k == 0)
558-
return -EINVAL;
559-
ftest->k = reciprocal_value(ftest->k);
560-
break;
561555
case BPF_S_ALU_MOD_K:
562556
/* check for division by zero */
563557
if (ftest->k == 0)
@@ -853,27 +847,7 @@ void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to)
853847
to->code = decodes[code];
854848
to->jt = filt->jt;
855849
to->jf = filt->jf;
856-
857-
if (code == BPF_S_ALU_DIV_K) {
858-
/*
859-
* When loaded this rule user gave us X, which was
860-
* translated into R = r(X). Now we calculate the
861-
* RR = r(R) and report it back. If next time this
862-
* value is loaded and RRR = r(RR) is calculated
863-
* then the R == RRR will be true.
864-
*
865-
* One exception. X == 1 translates into R == 0 and
866-
* we can't calculate RR out of it with r().
867-
*/
868-
869-
if (filt->k == 0)
870-
to->k = 1;
871-
else
872-
to->k = reciprocal_value(filt->k);
873-
874-
BUG_ON(reciprocal_value(to->k) != filt->k);
875-
} else
876-
to->k = filt->k;
850+
to->k = filt->k;
877851
}
878852

879853
int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, unsigned int len)

0 commit comments

Comments
 (0)