Commit 2321a959 authored by Sachin Kumar's avatar Sachin Kumar Committed by Alexei Starovoitov
Browse files

bpf: Fix constant blinding for PROBE_MEM32 stores



BPF_ST | BPF_PROBE_MEM32 immediate stores are not handled by
bpf_jit_blind_insn(), allowing user-controlled 32-bit immediates to
survive unblinded into JIT-compiled native code when bpf_jit_harden >= 1.

The root cause is that convert_ctx_accesses() rewrites BPF_ST|BPF_MEM
to BPF_ST|BPF_PROBE_MEM32 for arena pointer stores during verification,
before bpf_jit_blind_constants() runs during JIT compilation. The
blinding switch only matches BPF_ST|BPF_MEM (mode 0x60), not
BPF_ST|BPF_PROBE_MEM32 (mode 0xa0). The instruction falls through
unblinded.

Add BPF_ST|BPF_PROBE_MEM32 cases to bpf_jit_blind_insn() alongside the
existing BPF_ST|BPF_MEM cases. The blinding transformation is identical:
load the blinded immediate into BPF_REG_AX via mov+xor, then convert
the immediate store to a register store (BPF_STX).

The rewritten STX instruction must preserve the BPF_PROBE_MEM32 mode so
the architecture JIT emits the correct arena addressing (R12-based on
x86-64). Cannot use the BPF_STX_MEM() macro here because it hardcodes
BPF_MEM mode; construct the instruction directly instead.

Fixes: 6082b6c3 ("bpf: Recognize addr_space_cast instruction in the verifier.")
Reviewed-by: default avatarPuranjay Mohan <puranjay@kernel.org>
Reviewed-by: default avatarEmil Tsalapatis <emil@etsalapatis.com>
Signed-off-by: default avatarSachin Kumar <xcyfun@protonmail.com>
Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/r/Y6IT5VvNRchPBLI5D7JZHBzZrU9rb0ycRJPJzJSXGj7kJlX8RJwZFSM2YZjcDxoQKABkxt1T8Os2gi23PYyFuQe6KkZGWVyfz8K5afdy9ak=@protonmail.com


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent ac72464b
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -1422,6 +1422,27 @@ static int bpf_jit_blind_insn(const struct bpf_insn *from,
		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
		*to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
		break;

	case BPF_ST | BPF_PROBE_MEM32 | BPF_DW:
	case BPF_ST | BPF_PROBE_MEM32 | BPF_W:
	case BPF_ST | BPF_PROBE_MEM32 | BPF_H:
	case BPF_ST | BPF_PROBE_MEM32 | BPF_B:
		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^
				      from->imm);
		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
		/*
		 * Cannot use BPF_STX_MEM() macro here as it
		 * hardcodes BPF_MEM mode, losing PROBE_MEM32
		 * and breaking arena addressing in the JIT.
		 */
		*to++ = (struct bpf_insn) {
			.code  = BPF_STX | BPF_PROBE_MEM32 |
				 BPF_SIZE(from->code),
			.dst_reg = from->dst_reg,
			.src_reg = BPF_REG_AX,
			.off   = from->off,
		};
		break;
	}
out:
	return to - to_buff;