Commit 75672dda authored by Jiong Wang's avatar Jiong Wang Committed by Daniel Borkmann

bpf: fix BPF_ALU32 | BPF_ARSH on BE arches

Yauheni reported the following code do not work correctly on BE arches:

       ALU_ARSH_X:
               DST = (u64) (u32) ((*(s32 *) &DST) >> SRC);
               CONT;
       ALU_ARSH_K:
               DST = (u64) (u32) ((*(s32 *) &DST) >> IMM);
               CONT;

and are causing failure of test_verifier test 'arsh32 on imm 2' on BE
arches.

The code is taking address and interpreting memory directly, so is not
endianness neutral. We should instead perform standard C type casting on
the variable. A u64 to s32 conversion will drop the high 32-bit and reserve
the low 32-bit as signed integer, this is all we want.

Fixes: 2dc6b100 ("bpf: interpreter support BPF_ALU | BPF_ARSH")
Reported-by: default avatarYauheni Kaliuta <yauheni.kaliuta@redhat.com>
Reviewed-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: default avatarJiong Wang <jiong.wang@netronome.com>
Acked-by: default avatarSong Liu <songliubraving@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 6c6874f4
...@@ -1364,10 +1364,10 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack) ...@@ -1364,10 +1364,10 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
insn++; insn++;
CONT; CONT;
ALU_ARSH_X: ALU_ARSH_X:
DST = (u64) (u32) ((*(s32 *) &DST) >> SRC); DST = (u64) (u32) (((s32) DST) >> SRC);
CONT; CONT;
ALU_ARSH_K: ALU_ARSH_K:
DST = (u64) (u32) ((*(s32 *) &DST) >> IMM); DST = (u64) (u32) (((s32) DST) >> IMM);
CONT; CONT;
ALU64_ARSH_X: ALU64_ARSH_X:
(*(s64 *) &DST) >>= SRC; (*(s64 *) &DST) >>= SRC;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment