Skip to content

Commit c3062bd

Browse files
iii-igregkh
authored andcommitted
s390/bpf: Fix bpf_plt pointer arithmetic
[ Upstream commit 7ded842 ] Kui-Feng Lee reported a crash on s390x triggered by the dummy_st_ops/dummy_init_ptr_arg test [1]: [<0000000000000002>] 0x2 [<00000000009d5cde>] bpf_struct_ops_test_run+0x156/0x250 [<000000000033145a>] __sys_bpf+0xa1a/0xd00 [<00000000003319dc>] __s390x_sys_bpf+0x44/0x50 [<0000000000c4382c>] __do_syscall+0x244/0x300 [<0000000000c59a40>] system_call+0x70/0x98 This is caused by GCC moving memcpy() after assignments in bpf_jit_plt(), resulting in NULL pointers being written instead of the return and the target addresses. Looking at the GCC internals, the reordering is allowed because the alias analysis thinks that the memcpy() destination and the assignments' left-hand-sides are based on different objects: new_plt and bpf_plt_ret/bpf_plt_target respectively, and therefore they cannot alias. This is in turn due to a violation of the C standard: When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object ... From the C's perspective, bpf_plt_ret and bpf_plt are distinct objects and cannot be subtracted. In the practical terms, doing so confuses the GCC's alias analysis. The code was written this way in order to let the C side know a few offsets defined in the assembly. While nice, this is by no means necessary. Fix the noncompliance by hardcoding these offsets. [1] https://lore.kernel.org/bpf/[email protected]/ Fixes: f1d5df8 ("s390/bpf: Implement bpf_arch_text_poke()") Signed-off-by: Ilya Leoshkevich <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 54d38a5 commit c3062bd

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

arch/s390/net/bpf_jit_comp.c

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,12 @@ static void bpf_skip(struct bpf_jit *jit, int size)
516516
* PLT for hotpatchable calls. The calling convention is the same as for the
517517
* ftrace hotpatch trampolines: %r0 is return address, %r1 is clobbered.
518518
*/
519-
extern const char bpf_plt[];
520-
extern const char bpf_plt_ret[];
521-
extern const char bpf_plt_target[];
522-
extern const char bpf_plt_end[];
523-
#define BPF_PLT_SIZE 32
519+
struct bpf_plt {
520+
char code[16];
521+
void *ret;
522+
void *target;
523+
} __packed;
524+
extern const struct bpf_plt bpf_plt;
524525
asm(
525526
".pushsection .rodata\n"
526527
" .balign 8\n"
@@ -531,15 +532,14 @@ asm(
531532
" .balign 8\n"
532533
"bpf_plt_ret: .quad 0\n"
533534
"bpf_plt_target: .quad 0\n"
534-
"bpf_plt_end:\n"
535535
" .popsection\n"
536536
);
537537

538-
static void bpf_jit_plt(void *plt, void *ret, void *target)
538+
static void bpf_jit_plt(struct bpf_plt *plt, void *ret, void *target)
539539
{
540-
memcpy(plt, bpf_plt, BPF_PLT_SIZE);
541-
*(void **)((char *)plt + (bpf_plt_ret - bpf_plt)) = ret;
542-
*(void **)((char *)plt + (bpf_plt_target - bpf_plt)) = target ?: ret;
540+
memcpy(plt, &bpf_plt, sizeof(*plt));
541+
plt->ret = ret;
542+
plt->target = target;
543543
}
544544

545545
/*
@@ -662,9 +662,9 @@ static void bpf_jit_epilogue(struct bpf_jit *jit, u32 stack_depth)
662662
jit->prg = ALIGN(jit->prg, 8);
663663
jit->prologue_plt = jit->prg;
664664
if (jit->prg_buf)
665-
bpf_jit_plt(jit->prg_buf + jit->prg,
665+
bpf_jit_plt((struct bpf_plt *)(jit->prg_buf + jit->prg),
666666
jit->prg_buf + jit->prologue_plt_ret, NULL);
667-
jit->prg += BPF_PLT_SIZE;
667+
jit->prg += sizeof(struct bpf_plt);
668668
}
669669

670670
static int get_probe_mem_regno(const u8 *insn)
@@ -1901,9 +1901,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
19011901
struct bpf_jit jit;
19021902
int pass;
19031903

1904-
if (WARN_ON_ONCE(bpf_plt_end - bpf_plt != BPF_PLT_SIZE))
1905-
return orig_fp;
1906-
19071904
if (!fp->jit_requested)
19081905
return orig_fp;
19091906

@@ -2009,14 +2006,11 @@ bool bpf_jit_supports_far_kfunc_call(void)
20092006
int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
20102007
void *old_addr, void *new_addr)
20112008
{
2009+
struct bpf_plt expected_plt, current_plt, new_plt, *plt;
20122010
struct {
20132011
u16 opc;
20142012
s32 disp;
20152013
} __packed insn;
2016-
char expected_plt[BPF_PLT_SIZE];
2017-
char current_plt[BPF_PLT_SIZE];
2018-
char new_plt[BPF_PLT_SIZE];
2019-
char *plt;
20202014
char *ret;
20212015
int err;
20222016

@@ -2035,18 +2029,18 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
20352029
*/
20362030
} else {
20372031
/* Verify the PLT. */
2038-
plt = (char *)ip + (insn.disp << 1);
2039-
err = copy_from_kernel_nofault(current_plt, plt, BPF_PLT_SIZE);
2032+
plt = ip + (insn.disp << 1);
2033+
err = copy_from_kernel_nofault(&current_plt, plt,
2034+
sizeof(current_plt));
20402035
if (err < 0)
20412036
return err;
20422037
ret = (char *)ip + 6;
2043-
bpf_jit_plt(expected_plt, ret, old_addr);
2044-
if (memcmp(current_plt, expected_plt, BPF_PLT_SIZE))
2038+
bpf_jit_plt(&expected_plt, ret, old_addr);
2039+
if (memcmp(&current_plt, &expected_plt, sizeof(current_plt)))
20452040
return -EINVAL;
20462041
/* Adjust the call address. */
2047-
bpf_jit_plt(new_plt, ret, new_addr);
2048-
s390_kernel_write(plt + (bpf_plt_target - bpf_plt),
2049-
new_plt + (bpf_plt_target - bpf_plt),
2042+
bpf_jit_plt(&new_plt, ret, new_addr);
2043+
s390_kernel_write(&plt->target, &new_plt.target,
20502044
sizeof(void *));
20512045
}
20522046

0 commit comments

Comments
 (0)