@@ -662,6 +662,11 @@ static void build_epilogue(struct jit_ctx *ctx)
662662 ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative : func) : \
663663 func##_positive)
664664
665+ static bool is_bad_offset (int b_off )
666+ {
667+ return b_off > 0x1ffff || b_off < -0x20000 ;
668+ }
669+
665670static int build_body (struct jit_ctx * ctx )
666671{
667672 const struct bpf_prog * prog = ctx -> skf ;
@@ -728,7 +733,10 @@ static int build_body(struct jit_ctx *ctx)
728733 /* Load return register on DS for failures */
729734 emit_reg_move (r_ret , r_zero , ctx );
730735 /* Return with error */
731- emit_b (b_imm (prog -> len , ctx ), ctx );
736+ b_off = b_imm (prog -> len , ctx );
737+ if (is_bad_offset (b_off ))
738+ return - E2BIG ;
739+ emit_b (b_off , ctx );
732740 emit_nop (ctx );
733741 break ;
734742 case BPF_LD | BPF_W | BPF_IND :
@@ -775,8 +783,10 @@ static int build_body(struct jit_ctx *ctx)
775783 emit_jalr (MIPS_R_RA , r_s0 , ctx );
776784 emit_reg_move (MIPS_R_A0 , r_skb , ctx ); /* delay slot */
777785 /* Check the error value */
778- emit_bcond (MIPS_COND_NE , r_ret , 0 ,
779- b_imm (prog -> len , ctx ), ctx );
786+ b_off = b_imm (prog -> len , ctx );
787+ if (is_bad_offset (b_off ))
788+ return - E2BIG ;
789+ emit_bcond (MIPS_COND_NE , r_ret , 0 , b_off , ctx );
780790 emit_reg_move (r_ret , r_zero , ctx );
781791 /* We are good */
782792 /* X <- P[1:K] & 0xf */
@@ -855,17 +865,21 @@ static int build_body(struct jit_ctx *ctx)
855865 /* A /= X */
856866 ctx -> flags |= SEEN_X | SEEN_A ;
857867 /* Check if r_X is zero */
858- emit_bcond (MIPS_COND_EQ , r_X , r_zero ,
859- b_imm (prog -> len , ctx ), ctx );
868+ b_off = b_imm (prog -> len , ctx );
869+ if (is_bad_offset (b_off ))
870+ return - E2BIG ;
871+ emit_bcond (MIPS_COND_EQ , r_X , r_zero , b_off , ctx );
860872 emit_load_imm (r_ret , 0 , ctx ); /* delay slot */
861873 emit_div (r_A , r_X , ctx );
862874 break ;
863875 case BPF_ALU | BPF_MOD | BPF_X :
864876 /* A %= X */
865877 ctx -> flags |= SEEN_X | SEEN_A ;
866878 /* Check if r_X is zero */
867- emit_bcond (MIPS_COND_EQ , r_X , r_zero ,
868- b_imm (prog -> len , ctx ), ctx );
879+ b_off = b_imm (prog -> len , ctx );
880+ if (is_bad_offset (b_off ))
881+ return - E2BIG ;
882+ emit_bcond (MIPS_COND_EQ , r_X , r_zero , b_off , ctx );
869883 emit_load_imm (r_ret , 0 , ctx ); /* delay slot */
870884 emit_mod (r_A , r_X , ctx );
871885 break ;
@@ -926,7 +940,10 @@ static int build_body(struct jit_ctx *ctx)
926940 break ;
927941 case BPF_JMP | BPF_JA :
928942 /* pc += K */
929- emit_b (b_imm (i + k + 1 , ctx ), ctx );
943+ b_off = b_imm (i + k + 1 , ctx );
944+ if (is_bad_offset (b_off ))
945+ return - E2BIG ;
946+ emit_b (b_off , ctx );
930947 emit_nop (ctx );
931948 break ;
932949 case BPF_JMP | BPF_JEQ | BPF_K :
@@ -1056,12 +1073,16 @@ static int build_body(struct jit_ctx *ctx)
10561073 break ;
10571074 case BPF_RET | BPF_A :
10581075 ctx -> flags |= SEEN_A ;
1059- if (i != prog -> len - 1 )
1076+ if (i != prog -> len - 1 ) {
10601077 /*
10611078 * If this is not the last instruction
10621079 * then jump to the epilogue
10631080 */
1064- emit_b (b_imm (prog -> len , ctx ), ctx );
1081+ b_off = b_imm (prog -> len , ctx );
1082+ if (is_bad_offset (b_off ))
1083+ return - E2BIG ;
1084+ emit_b (b_off , ctx );
1085+ }
10651086 emit_reg_move (r_ret , r_A , ctx ); /* delay slot */
10661087 break ;
10671088 case BPF_RET | BPF_K :
@@ -1075,7 +1096,10 @@ static int build_body(struct jit_ctx *ctx)
10751096 * If this is not the last instruction
10761097 * then jump to the epilogue
10771098 */
1078- emit_b (b_imm (prog -> len , ctx ), ctx );
1099+ b_off = b_imm (prog -> len , ctx );
1100+ if (is_bad_offset (b_off ))
1101+ return - E2BIG ;
1102+ emit_b (b_off , ctx );
10791103 emit_nop (ctx );
10801104 }
10811105 break ;
@@ -1133,8 +1157,10 @@ static int build_body(struct jit_ctx *ctx)
11331157 /* Load *dev pointer */
11341158 emit_load_ptr (r_s0 , r_skb , off , ctx );
11351159 /* error (0) in the delay slot */
1136- emit_bcond (MIPS_COND_EQ , r_s0 , r_zero ,
1137- b_imm (prog -> len , ctx ), ctx );
1160+ b_off = b_imm (prog -> len , ctx );
1161+ if (is_bad_offset (b_off ))
1162+ return - E2BIG ;
1163+ emit_bcond (MIPS_COND_EQ , r_s0 , r_zero , b_off , ctx );
11381164 emit_reg_move (r_ret , r_zero , ctx );
11391165 if (code == (BPF_ANC | SKF_AD_IFINDEX )) {
11401166 BUILD_BUG_ON (sizeof_field (struct net_device , ifindex ) != 4 );
@@ -1244,7 +1270,10 @@ void bpf_jit_compile(struct bpf_prog *fp)
12441270
12451271 /* Generate the actual JIT code */
12461272 build_prologue (& ctx );
1247- build_body (& ctx );
1273+ if (build_body (& ctx )) {
1274+ module_memfree (ctx .target );
1275+ goto out ;
1276+ }
12481277 build_epilogue (& ctx );
12491278
12501279 /* Update the icache */
0 commit comments