Skip to content

Commit f4b16fc

Browse files
zlimdavem330
authored andcommitted
arm64: bpf: fix buffer pointer
During code review, I noticed we were passing a bad buffer pointer to bpf_load_pointer helper function called by jitted code. Point to the buffer allocated by JIT, so we don't silently corrupt other parts of the stack. Signed-off-by: Zi Shen Lim <zlim.lnx@gmail.com> Acked-by: Yang Shi <yang.shi@linaro.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 508dc06 commit f4b16fc

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

arch/arm64/net/bpf_jit_comp.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ static inline int epilogue_offset(const struct jit_ctx *ctx)
139139
/* Stack must be multiples of 16B */
140140
#define STACK_ALIGN(sz) (((sz) + 15) & ~15)
141141

142+
#define _STACK_SIZE \
143+
(MAX_BPF_STACK \
144+
+ 4 /* extra for skb_copy_bits buffer */)
145+
146+
#define STACK_SIZE STACK_ALIGN(_STACK_SIZE)
147+
142148
static void build_prologue(struct jit_ctx *ctx)
143149
{
144150
const u8 r6 = bpf2a64[BPF_REG_6];
@@ -150,10 +156,6 @@ static void build_prologue(struct jit_ctx *ctx)
150156
const u8 rx = bpf2a64[BPF_REG_X];
151157
const u8 tmp1 = bpf2a64[TMP_REG_1];
152158
const u8 tmp2 = bpf2a64[TMP_REG_2];
153-
int stack_size = MAX_BPF_STACK;
154-
155-
stack_size += 4; /* extra for skb_copy_bits buffer */
156-
stack_size = STACK_ALIGN(stack_size);
157159

158160
/*
159161
* BPF prog stack layout
@@ -165,12 +167,13 @@ static void build_prologue(struct jit_ctx *ctx)
165167
* | ... | callee saved registers
166168
* +-----+
167169
* | | x25/x26
168-
* BPF fp register => -80:+-----+
170+
* BPF fp register => -80:+-----+ <= (BPF_FP)
169171
* | |
170172
* | ... | BPF prog stack
171173
* | |
172-
* | |
173-
* current A64_SP => +-----+
174+
* +-----+ <= (BPF_FP - MAX_BPF_STACK)
175+
* |RSVD | JIT scratchpad
176+
* current A64_SP => +-----+ <= (BPF_FP - STACK_SIZE)
174177
* | |
175178
* | ... | Function call stack
176179
* | |
@@ -196,7 +199,7 @@ static void build_prologue(struct jit_ctx *ctx)
196199
emit(A64_MOV(1, fp, A64_SP), ctx);
197200

198201
/* Set up function call stack */
199-
emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx);
202+
emit(A64_SUB_I(1, A64_SP, A64_SP, STACK_SIZE), ctx);
200203

201204
/* Clear registers A and X */
202205
emit_a64_mov_i64(ra, 0, ctx);
@@ -213,13 +216,9 @@ static void build_epilogue(struct jit_ctx *ctx)
213216
const u8 fp = bpf2a64[BPF_REG_FP];
214217
const u8 tmp1 = bpf2a64[TMP_REG_1];
215218
const u8 tmp2 = bpf2a64[TMP_REG_2];
216-
int stack_size = MAX_BPF_STACK;
217-
218-
stack_size += 4; /* extra for skb_copy_bits buffer */
219-
stack_size = STACK_ALIGN(stack_size);
220219

221220
/* We're done with BPF stack */
222-
emit(A64_ADD_I(1, A64_SP, A64_SP, stack_size), ctx);
221+
emit(A64_ADD_I(1, A64_SP, A64_SP, STACK_SIZE), ctx);
223222

224223
/* Restore fs (x25) and x26 */
225224
emit(A64_POP(fp, A64_R(26), A64_SP), ctx);
@@ -658,7 +657,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
658657
return -EINVAL;
659658
}
660659
emit_a64_mov_i64(r3, size, ctx);
661-
emit(A64_ADD_I(1, r4, fp, MAX_BPF_STACK), ctx);
660+
emit(A64_SUB_I(1, r4, fp, STACK_SIZE), ctx);
662661
emit_a64_mov_i64(r5, (unsigned long)bpf_load_pointer, ctx);
663662
emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
664663
emit(A64_MOV(1, A64_FP, A64_SP), ctx);

0 commit comments

Comments
 (0)