OSDN Git Service

bpf, x86: Validate computation of branch displacements for x86-64
authorPiotr Krysiuk <piotras@gmail.com>
Mon, 5 Apr 2021 21:52:15 +0000 (22:52 +0100)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 8 Apr 2021 14:24:36 +0000 (16:24 +0200)
commite4d4d456436bfb2fe412ee2cd489f7658449b098
treefba70db61cd8469f3524666acd385f641dad30f3
parent144748eb0c445091466c9b741ebd0bfcc5914f3d
bpf, x86: Validate computation of branch displacements for x86-64

The branch displacement logic in the BPF JIT compilers for x86 assumes
that, for any generated branch instruction, the distance cannot
increase between optimization passes.

But this assumption can be violated due to how the distances are
computed. Specifically, whenever a backward branch is processed in
do_jit(), the distance is computed by subtracting the positions in the
machine code from different optimization passes. This is because part
of addrs[] is already updated for the current optimization pass, before
the branch instruction is visited.

And so the optimizer can expand blocks of machine code in some cases.

This can confuse the optimizer logic, where it assumes that a fixed
point has been reached for all machine code blocks once the total
program size stops changing. And then the JIT compiler can output
abnormal machine code containing incorrect branch displacements.

To mitigate this issue, we assert that a fixed point is reached while
populating the output image. This rejects any problematic programs.
The issue affects both x86-32 and x86-64. We mitigate separately to
ease backporting.

Signed-off-by: Piotr Krysiuk <piotras@gmail.com>
Reviewed-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
arch/x86/net/bpf_jit_comp.c