Skip to content

Commit 3832ba4

Browse files
rnavmpe
authored andcommitted
powerpc/bpf: Validate branch ranges
Add checks to ensure that we never emit branch instructions with truncated branch offsets. Suggested-by: Michael Ellerman <[email protected]> Signed-off-by: Naveen N. Rao <[email protected]> Tested-by: Johan Almbladh <[email protected]> Reviewed-by: Christophe Leroy <[email protected]> Acked-by: Song Liu <[email protected]> Acked-by: Johan Almbladh <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/71d33a6b7603ec1013c9734dd8bdd4ff5e929142.1633464148.git.naveen.n.rao@linux.vnet.ibm.com
1 parent 4549c3e commit 3832ba4

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

arch/powerpc/net/bpf_jit.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,30 @@
2424
#define EMIT(instr) PLANT_INSTR(image, ctx->idx, instr)
2525

2626
/* Long jump; (unconditional 'branch') */
27-
#define PPC_JMP(dest) EMIT(PPC_INST_BRANCH | \
28-
(((dest) - (ctx->idx * 4)) & 0x03fffffc))
27+
#define PPC_JMP(dest) \
28+
do { \
29+
long offset = (long)(dest) - (ctx->idx * 4); \
30+
if (!is_offset_in_branch_range(offset)) { \
31+
pr_err_ratelimited("Branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \
32+
return -ERANGE; \
33+
} \
34+
EMIT(PPC_INST_BRANCH | (offset & 0x03fffffc)); \
35+
} while (0)
36+
2937
/* blr; (unconditional 'branch' with link) to absolute address */
3038
#define PPC_BL_ABS(dest) EMIT(PPC_INST_BL | \
3139
(((dest) - (unsigned long)(image + ctx->idx)) & 0x03fffffc))
3240
/* "cond" here covers BO:BI fields. */
33-
#define PPC_BCC_SHORT(cond, dest) EMIT(PPC_INST_BRANCH_COND | \
34-
(((cond) & 0x3ff) << 16) | \
35-
(((dest) - (ctx->idx * 4)) & \
36-
0xfffc))
41+
#define PPC_BCC_SHORT(cond, dest) \
42+
do { \
43+
long offset = (long)(dest) - (ctx->idx * 4); \
44+
if (!is_offset_in_cond_branch_range(offset)) { \
45+
pr_err_ratelimited("Conditional branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \
46+
return -ERANGE; \
47+
} \
48+
EMIT(PPC_INST_BRANCH_COND | (((cond) & 0x3ff) << 16) | (offset & 0xfffc)); \
49+
} while (0)
50+
3751
/* Sign-extended 32-bit immediate load */
3852
#define PPC_LI32(d, i) do { \
3953
if ((int)(uintptr_t)(i) >= -32768 && \

arch/powerpc/net/bpf_jit_comp.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
210210
/* Now build the prologue, body code & epilogue for real. */
211211
cgctx.idx = 0;
212212
bpf_jit_build_prologue(code_base, &cgctx);
213-
bpf_jit_build_body(fp, code_base, &cgctx, addrs, extra_pass);
213+
if (bpf_jit_build_body(fp, code_base, &cgctx, addrs, extra_pass)) {
214+
bpf_jit_binary_free(bpf_hdr);
215+
fp = org_fp;
216+
goto out_addrs;
217+
}
214218
bpf_jit_build_epilogue(code_base, &cgctx);
215219

216220
if (bpf_jit_enable > 1)

arch/powerpc/net/bpf_jit_comp32.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 fun
200200
}
201201
}
202202

203-
static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
203+
static int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
204204
{
205205
/*
206206
* By now, the eBPF program has already setup parameters in r3-r6
@@ -261,7 +261,9 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32
261261
bpf_jit_emit_common_epilogue(image, ctx);
262262

263263
EMIT(PPC_RAW_BCTR());
264+
264265
/* out: */
266+
return 0;
265267
}
266268

267269
/* Assemble the body code between the prologue & epilogue */
@@ -1090,7 +1092,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
10901092
*/
10911093
case BPF_JMP | BPF_TAIL_CALL:
10921094
ctx->seen |= SEEN_TAILCALL;
1093-
bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
1095+
ret = bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
1096+
if (ret < 0)
1097+
return ret;
10941098
break;
10951099

10961100
default:

arch/powerpc/net/bpf_jit_comp64.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 fun
206206
EMIT(PPC_RAW_BCTRL());
207207
}
208208

209-
static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
209+
static int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
210210
{
211211
/*
212212
* By now, the eBPF program has already setup parameters in r3, r4 and r5
@@ -267,7 +267,9 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32
267267
bpf_jit_emit_common_epilogue(image, ctx);
268268

269269
EMIT(PPC_RAW_BCTR());
270+
270271
/* out: */
272+
return 0;
271273
}
272274

273275
/* Assemble the body code between the prologue & epilogue */
@@ -993,7 +995,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
993995
*/
994996
case BPF_JMP | BPF_TAIL_CALL:
995997
ctx->seen |= SEEN_TAILCALL;
996-
bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
998+
ret = bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
999+
if (ret < 0)
1000+
return ret;
9971001
break;
9981002

9991003
default:

0 commit comments

Comments
 (0)