The JIT code tracks which registers are used and remaps based on that, but currently does not account for the fact that function calls implicitly use registers because the called function will treat them as arguments. Therefore, if a function merely passes through one of its arguments to another function call, it will incorrectly be considered unused and may be overwritten with another register's contents. In practice, this is unlikely to affect main programs as they only get one argument (the context, which they are very likely to touch somehow in addition to passing it on), so they'll have to touch other registers if they want to pass them as arguments. Subprograms, however, are much likelier to hit this. Similarly, tail calls pass R1 to the callee, so mark it used as well. Fixes: 51c66ad849a7 ("powerpc/bpf: Implement extended BPF on PPC32") Fixes: a8936569a07b ("powerpc/bpf: Always reallocate BPF_REG_5, BPF_REG_AX and TMP_REG when possible") Signed-off-by: Lukas Stockner --- arch/powerpc/net/bpf_jit_comp32.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c index 9f55dd4bdfd6..7414e350d2d4 100644 --- a/arch/powerpc/net/bpf_jit_comp32.c +++ b/arch/powerpc/net/bpf_jit_comp32.c @@ -339,7 +339,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code { const struct bpf_insn *insn = fp->insnsi; int flen = fp->len; - int i, ret; + int i, ret, max_arg; /* Start of epilogue code - will only be valid 2nd pass onwards */ u32 exit_addr = addrs[flen]; @@ -1183,6 +1183,18 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code if (ret < 0) return ret; + /* + * We don't know which arguments the called function will use, + * so if this is a subprogram, assume the worst case where it + * uses all of them. Regular programs only get one argument, + * so any others would have been explicitly set. + */ + max_arg = ctx->is_subprog ? BPF_REG_5 : BPF_REG_1; + for (int reg = BPF_REG_1; reg <= max_arg; ++reg) { + bpf_set_seen_register(ctx, bpf_to_ppc(reg) - 1); + bpf_set_seen_register(ctx, bpf_to_ppc(reg)); + } + if (bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_5))) { EMIT(PPC_RAW_STW(bpf_to_ppc(BPF_REG_5) - 1, _R1, 8)); EMIT(PPC_RAW_STW(bpf_to_ppc(BPF_REG_5), _R1, 12)); @@ -1412,6 +1424,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code * Tail call */ case BPF_JMP | BPF_TAIL_CALL: + bpf_set_seen_register(ctx, bpf_to_ppc(BPF_REG_1) - 1); + bpf_set_seen_register(ctx, bpf_to_ppc(BPF_REG_1)); + ctx->seen |= SEEN_TAILCALL; ret = bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]); if (ret < 0) -- 2.56.0