Skip to content

Commit 232ac70

Browse files
borkmanngregkh
authored andcommitted
bpf: enable access to ax register also from verifier rewrite
[ commit 9b73bfd upstream ] Right now we are using BPF ax register in JIT for constant blinding as well as in interpreter as temporary variable. Verifier will not be able to use it simply because its use will get overridden from the former in bpf_jit_blind_insn(). However, it can be made to work in that blinding will be skipped if there is prior use in either source or destination register on the instruction. Taking constraints of ax into account, the verifier is then open to use it in rewrites under some constraints. Note, ax register already has mappings in every eBPF JIT. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent b855e31 commit 232ac70

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

‎include/linux/filter.h‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ struct sock_reuseport;
5353
#define BPF_REG_D BPF_REG_8 /* data, callee-saved */
5454
#define BPF_REG_H BPF_REG_9 /* hlen, callee-saved */
5555

56-
/* Kernel hidden auxiliary/helper register for hardening step.
57-
* Only used by eBPF JITs. It's nothing more than a temporary
58-
* register that JITs use internally, only that here it's part
59-
* of eBPF instructions that have been rewritten for blinding
60-
* constants. See JIT pre-step in bpf_jit_blind_constants().
61-
*/
56+
/* Kernel hidden auxiliary/helper register. */
6257
#define BPF_REG_AX MAX_BPF_REG
6358
#define MAX_BPF_EXT_REG (MAX_BPF_REG + 1)
6459
#define MAX_BPF_JIT_REG MAX_BPF_EXT_REG

‎kernel/bpf/core.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,26 @@ static int bpf_jit_blind_insn(const struct bpf_insn *from,
643643
BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
644644
BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
645645

646+
/* Constraints on AX register:
647+
*
648+
* AX register is inaccessible from user space. It is mapped in
649+
* all JITs, and used here for constant blinding rewrites. It is
650+
* typically "stateless" meaning its contents are only valid within
651+
* the executed instruction, but not across several instructions.
652+
* There are a few exceptions however which are further detailed
653+
* below.
654+
*
655+
* Constant blinding is only used by JITs, not in the interpreter.
656+
* The interpreter uses AX in some occasions as a local temporary
657+
* register e.g. in DIV or MOD instructions.
658+
*
659+
* In restricted circumstances, the verifier can also use the AX
660+
* register for rewrites as long as they do not interfere with
661+
* the above cases!
662+
*/
663+
if (from->dst_reg == BPF_REG_AX || from->src_reg == BPF_REG_AX)
664+
goto out;
665+
646666
if (from->imm == 0 &&
647667
(from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
648668
from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {

0 commit comments

Comments
 (0)