bpf: prevent r10 register from being marked as precise
Subsystems (4)
Andrii Nakryiko
maintainerPATCH v2 1/2 bpf: prevent r10 register from being marked as precise
r10 is a special register that is not under BPF program's control and is always effectively precise. The rest of precision logic assumes that only r0-r9 SCALAR registers are marked as precise, so prevent r10 from being marked precise.
This can happen due to signed cast instruction allowing to do something like r0 = (s8)r10;, which later, if r0 needs to be precise, would lead to an attempt to mark r10 as precise.
Prevent this with an extra check during instruction backtracking.
--- kernel/bpf/verifier.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +4 -2
@@ -3615,7 +3615,8 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,* sreg needs precision before this insn */ bt_clear_reg (bt, dreg); -bt_set_reg (bt, sreg); +if (sreg !=BPF_REG_FP ) +bt_set_reg (bt, sreg); }else {/* dreg = K * dreg needs precision after this insn.
@@ -3631,7 +3632,8 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,* both dreg and sreg need precision * before this insn */ -bt_set_reg (bt, sreg); +if (sreg !=BPF_REG_FP ) +bt_set_reg (bt, sreg); }/* else dreg += K * dreg still needs precision before this insn */
Signature · 1 line
2.43.0
Andrii Nakryiko
maintainerPATCH v2 2/2 selftests/bpf: add fp-leaking precise subprog result tests
Add selftests validating that BPF verifier handles precision marking for SCALAR registers derived from r10 (fp) register correctly.
Given r0 = (s8)r10; syntax is not supported by older Clang compilers, use the raw BPF instruction syntax to maximize compatibility.
--- tools/include/linux/filter.h | 18 ++++ .../bpf/progs/verifier_subprog_precision.c | 89 +++++++++++++++++++ 2 files changed, 107 insertions(+)
tools/include/linux/filter.h +18 -0
@@ -111,6 +111,24 @@ .off = 0, \ .imm = IMM }) +/* Short form of movsx, dst_reg = (s8,s16,s32)src_reg */ + +#define BPF_MOVSX64_REG (DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +#define BPF_MOVSX32_REG (DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU | BPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) +/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */ #define BPF_MOV64_RAW (TYPE ,DST ,SRC ,IMM ) \
tools/testing/selftests/bpf/progs/verifier_subprog_precision.c +89 -0
101 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 bpf-next 2/2] selftests/bpf: add fp-leaking precise subprog result tests
Quoted 7 lines · Andrii Nakryiko
Add selftests validating that BPF verifier handles precision marking for SCALAR registers derived from r10 (fp) register correctly.
Given
r0 = (s8)r10;syntax is not supported by older Clang compilers, use the raw BPF instruction syntax to maximize compatibility.Signed-off-by Andrii Nakryiko <[email protected]>
Quoted 9 lines · Andrii Nakryiko
r10 is a special register that is not under BPF program's control and is always effectively precise. The rest of precision logic assumes that only r0-r9 SCALAR registers are marked as precise, so prevent r10 from being marked precise.
This can happen due to signed cast instruction allowing to do something like
r0 = (s8)r10;, which later, if r0 needs to be precise, would lead to an attempt to mark r10 as precise.
Quoted 5 lines
Prevent this with an extra check during instruction backtracking.
Fixes 8100928c8814 ("bpf: Support new sign-extension mov insns")Reported-by [email protected]Signed-off-by Andrii Nakryiko <[email protected]>
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 10 lines · On Thu, 4 Apr 2024 14:45:35 -0700 you wrote:
r10 is a special register that is not under BPF program's control and is always effectively precise. The rest of precision logic assumes that only r0-r9 SCALAR registers are marked as precise, so prevent r10 from being marked precise.
This can happen due to signed cast instruction allowing to do something like
r0 = (s8)r10;, which later, if r0 needs to be precise, would lead to an attempt to mark r10 as precise.[...]
Here is the summary with links:
- [v2,bpf-next,1/2] bpf: prevent r10 register from being marked as precise
https://git.kernel.org/bpf/bpf-next/c/1f2a74b41ea8 - [v2,bpf-next,2/2] selftests/bpf: add fp-leaking precise subprog result tests https://git.kernel.org/bpf/bpf-next/c/343ca8131c35
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html