bpf, x86: Validate computation of branch displacements for x86-32
authorPiotr Krysiuk <piotras@gmail.com>
Tue, 6 Apr 2021 20:59:39 +0000 (21:59 +0100)
committerSalvatore Bonaccorso <carnil@debian.org>
Fri, 9 Apr 2021 18:17:58 +0000 (19:17 +0100)
commit07878c870ba218f8167a3d5ff762805f994a9963
tree4e821fdeca0b11c3398f950aa246c346e68c344c
parentd6baa74fbae28d15e4f84a8e8ad3294887e67042
bpf, x86: Validate computation of branch displacements for x86-32

Origin: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git/patch/?id=26f55a59dc65ff77cd1c4b37991e26497fc68049
Bug-Debian-Security: https://security-tracker.debian.org/tracker/CVE-2021-29154

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>
Gbp-Pq: Topic bugfix/x86
Gbp-Pq: Name 0002-bpf-x86-Validate-computation-of-branch-displacements.patch
arch/x86/net/bpf_jit_comp32.c