Skip to content

Commit 951fb7b

Browse files
rnavgregkh
authored andcommitted
powerpc/bpf: Validate branch ranges
upstream commit 3832ba4 Add checks to ensure that we never emit branch instructions with truncated branch offsets. Suggested-by: Michael Ellerman <mpe@ellerman.id.au> Signed-off-by: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com> Tested-by: Johan Almbladh <johan.almbladh@anyfinetworks.com> Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu> Acked-by: Song Liu <songliubraving@fb.com> Acked-by: Johan Almbladh <johan.almbladh@anyfinetworks.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/71d33a6b7603ec1013c9734dd8bdd4ff5e929142.1633464148.git.naveen.n.rao@linux.vnet.ibm.com [drop ppc32 changes] Signed-off-by: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 51cf71d commit 951fb7b

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

‎arch/powerpc/net/bpf_jit.h‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <asm/types.h>
1414
#include <asm/ppc-opcode.h>
15+
#include <asm/code-patching.h>
1516

1617
#ifdef PPC64_ELF_ABI_v1
1718
#define FUNCTION_DESCR_SIZE 24
@@ -24,13 +25,26 @@
2425
#define EMIT(instr) PLANT_INSTR(image, ctx->idx, instr)
2526

2627
/* Long jump; (unconditional 'branch') */
27-
#define PPC_JMP(dest) EMIT(PPC_INST_BRANCH | \
28-
(((dest) - (ctx->idx * 4)) & 0x03fffffc))
28+
#define PPC_JMP(dest) \
29+
do { \
30+
long offset = (long)(dest) - (ctx->idx * 4); \
31+
if (!is_offset_in_branch_range(offset)) { \
32+
pr_err_ratelimited("Branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \
33+
return -ERANGE; \
34+
} \
35+
EMIT(PPC_INST_BRANCH | (offset & 0x03fffffc)); \
36+
} while (0)
2937
/* "cond" here covers BO:BI fields. */
30-
#define PPC_BCC_SHORT(cond, dest) EMIT(PPC_INST_BRANCH_COND | \
31-
(((cond) & 0x3ff) << 16) | \
32-
(((dest) - (ctx->idx * 4)) & \
33-
0xfffc))
38+
#define PPC_BCC_SHORT(cond, dest) \
39+
do { \
40+
long offset = (long)(dest) - (ctx->idx * 4); \
41+
if (!is_offset_in_cond_branch_range(offset)) { \
42+
pr_err_ratelimited("Conditional branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \
43+
return -ERANGE; \
44+
} \
45+
EMIT(PPC_INST_BRANCH_COND | (((cond) & 0x3ff) << 16) | (offset & 0xfffc)); \
46+
} while (0)
47+
3448
/* Sign-extended 32-bit immediate load */
3549
#define PPC_LI32(d, i) do { \
3650
if ((int)(uintptr_t)(i) >= -32768 && \

‎arch/powerpc/net/bpf_jit_comp64.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ static void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx,
224224
EMIT(PPC_RAW_BLRL());
225225
}
226226

227-
static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
227+
static int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
228228
{
229229
/*
230230
* By now, the eBPF program has already setup parameters in r3, r4 and r5
@@ -285,7 +285,9 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32
285285
bpf_jit_emit_common_epilogue(image, ctx);
286286

287287
EMIT(PPC_RAW_BCTR());
288+
288289
/* out: */
290+
return 0;
289291
}
290292

291293
/* Assemble the body code between the prologue & epilogue */
@@ -1010,7 +1012,9 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
10101012
*/
10111013
case BPF_JMP | BPF_TAIL_CALL:
10121014
ctx->seen |= SEEN_TAILCALL;
1013-
bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
1015+
ret = bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
1016+
if (ret < 0)
1017+
return ret;
10141018
break;
10151019

10161020
default:

0 commit comments

Comments
 (0)