summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorAlexei Starovoitov <ast@kernel.org>2026-06-06 16:44:47 -0700
committerAlexei Starovoitov <ast@kernel.org>2026-06-06 16:49:04 -0700
commitf6cd665c10f1577eca9eef643c9d10ec0435fd61 (patch)
tree3a23faf49bfbf0af449081a92e80ff62b969d68c /kernel
parent8ddce416797b7454ba1df855821b02c6e43b5a0e (diff)
parent3ce6b42458f0e2176350fccf86b954d322591ff7 (diff)
Merge branch 'bpf-verifier-fix-ptr_to_flow_keys-constant-offset-oob'
Nuoqi Gui says: ==================== bpf, verifier: fix PTR_TO_FLOW_KEYS constant-offset OOB A constant offset added to a PTR_TO_FLOW_KEYS register lands in reg->var_off, but check_flow_keys_access() bounds-checks only insn->off and never folds reg->var_off.value. A BPF_PROG_TYPE_FLOW_DISSECTOR program can therefore do "flow_keys += 0x1000; *(flow_keys + 0)" and have it accepted, then read/write kernel stack past struct bpf_flow_keys at runtime. Patch 1 folds reg->var_off.value into the offset (and rejects non-constant offsets), mirroring check_ctx_access(); patch 2 adds verifier selftests. This is a regression introduced in the 7.1 development cycle by commit 022ac0750883 ("bpf: use reg->var_off instead of reg->off for pointers"), which moved the constant offset from reg->off (folded generically before 022ac0750883) into reg->var_off without updating the flow_keys path. No released kernel is affected: v7.0.x rejects the program above, and the bug reproduces only on v7.1-rc1..rc5, so no stable backport is needed. It was first reported privately to security@kernel.org; per their guidance it is handled in the open as a normal regression fix. Found by manual verifier audit and confirmed dynamically in a disposable QEMU/KVM guest: the load above is accepted, a runtime read leaked a kernel-stack pointer 0x1000 past bpf_flow_keys, and a runtime write of a marker faulted the guest in net_rx_action. An alternative -- forbidding pointer arithmetic on PTR_TO_FLOW_KEYS outright by dropping "if (known) break;" in adjust_ptr_min_max_vals() -- was rejected because v7.0.x accepted (and correctly bounds-checked) constant arithmetic on the keys pointer; restoring the fold preserves that behaviour while closing the divergence. Signed-off-by: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn> --- v2 -> v3: - Pass existing reg/argno context into check_flow_keys_access(), avoiding a stale regno reference in check_mem_access(). - Add a variable-offset selftest using bpf_get_prandom_u32(). v1 -> v2: - Target bpf-next instead of bpf (per reviewer feedback). - Base-commit updated to bpf-next/master. v2: https://lore.kernel.org/bpf/20260604180730.2518088-1-gnq25@mails.tsinghua.edu.cn/ v1: https://lore.kernel.org/bpf/20260604150755.2487555-1-gnq25@mails.tsinghua.edu.cn/ ==================== Link: https://patch.msgid.link/20260606-c3-01-v3-v3-0-97c51f592f15@mails.tsinghua.edu.cn Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'kernel')
-rw-r--r--kernel/bpf/verifier.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 935595138aa0..68ddd465584c 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -4728,9 +4728,21 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, struct b
return err;
}
-static int check_flow_keys_access(struct bpf_verifier_env *env, int off,
- int size)
+static int check_flow_keys_access(struct bpf_verifier_env *env,
+ struct bpf_reg_state *reg, argno_t argno,
+ int off, int size)
{
+ /* Only a constant offset is allowed here; fold it into off. */
+ if (!tnum_is_const(reg->var_off)) {
+ char tn_buf[48];
+
+ tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
+ verbose(env, "%s invalid variable offset to flow keys: off=%d, var_off=%s\n",
+ reg_arg_name(env, argno), off, tn_buf);
+ return -EACCES;
+ }
+ off += reg->var_off.value;
+
if (size < 0 || off < 0 ||
(u64)off + size > sizeof(struct bpf_flow_keys)) {
verbose(env, "invalid access to flow keys off=%d size=%d\n",
@@ -6239,7 +6251,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, struct b
return -EACCES;
}
- err = check_flow_keys_access(env, off, size);
+ err = check_flow_keys_access(env, reg, argno, off, size);
if (!err && t == BPF_READ && value_regno >= 0)
mark_reg_unknown(env, regs, value_regno);
} else if (type_is_sk_pointer(reg->type)) {