bpf: Reject narrower access to pointer ctx fields
PATCH 1/1 bpf: Reject narrower access to pointer ctx fields
From: Paul Chaignon <[email protected]>
commit e09299225d5ba3916c91ef70565f7d2187e4cca0 upstream.
The following BPF program, simplified from a syzkaller repro, causes a kernel warning:
r0 = *(u8 *)(r1 + 169);
exit;With pointer field sk being at offset 168 in __sk_buff. This access is detected as a narrower read in bpf_skb_is_valid_access because it doesn't match offsetof(struct __sk_buff, sk). It is therefore allowed and later proceeds to bpf_convert_ctx_access. Note that for the "is_narrower_load" case in the convert_ctx_accesses(), the insn->off is aligned, so the cnt may not be 0 because it matches the offsetof(struct __sk_buff, sk) in the bpf_convert_ctx_access. However, the target_size stays 0 and the verifier errors with a kernel warning:
verifier bug: error during ctx access conversion(1)
This patch fixes that to return a proper "invalid bpf_context access off=X size=Y" error on the load instruction.
The same issue affects multiple other fields in context structures that allow narrow access. Some other non-affected fields (for sk_msg, sk_lookup, and sockopt) were also changed to use bpf_ctx_range_ptr for consistency.
Note this syzkaller crash was reported in the "Closes" link below, which used to be about a different bug, fixed in
commit fce7bd8e385a ("bpf/verifier: Handle BPF_LOAD_ACQ instructions in insn_def_regno()"). Because syzbot somehow confused the two bugs, the new crash and repro didn't get reported to the mailing list.
[shung-hsi.yu: offset(struct bpf_sock_ops, skb_hwtstamp) case was dropped becasuse it was only added in v6.2 with commit 9bb053490f1a ("bpf: Add hwtstamp field for the sockops prog")]
---
Backport for 6.12 and 6.6 separately sent[1].
Commit ba578b87fe2b ("selftests/bpf: Test invalid narrower ctx load") for the correspond BPF selftest was not backported along because it depends on commit fcd36964f22b ("selftests/bpf: verifier/ctx converted to inline assembly") added in v6.4.
1: https://lore.kernel.org/stable/[email protected]/
--- kernel/bpf/cgroup.c | 8 ++++---- net/core/filter.c | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-)
kernel/bpf/cgroup.c +4 -4
@@ -2384,22 +2384,22 @@ static bool cg_sockopt_is_valid_access(int off, int size, }switch (off) { -case offsetof(struct bpf_sockopt , sk): + case bpf_ctx_range_ptr(struct bpf_sockopt, sk):if (size !=sizeof (__u64))return false; info->reg_type =PTR_TO_SOCKET ;break ; -case offsetof(struct bpf_sockopt , optval): + case bpf_ctx_range_ptr(struct bpf_sockopt, optval):if (size !=sizeof (__u64))return false; info->reg_type =PTR_TO_PACKET ;break ; -case offsetof(struct bpf_sockopt , optval_end): + case bpf_ctx_range_ptr(struct bpf_sockopt, optval_end):if (size !=sizeof (__u64))return false; info->reg_type =PTR_TO_PACKET_END ;break ; -case offsetof(struct bpf_sockopt , retval): + case bpf_ctx_range(struct bpf_sockopt, retval):if (size != size_default)return false;return prog->expected_attach_type ==BPF_CGROUP_GETSOCKOPT ;
net/core/filter.c +9 -9
@@ -8515,7 +8515,7 @@ static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type typeif (size !=sizeof (__u64))return false;break ; -case offsetof(struct __sk_buff , sk): + case bpf_ctx_range_ptr(struct __sk_buff, sk):if (type ==BPF_WRITE || size !=sizeof (__u64))return false; info->reg_type =PTR_TO_SOCK_COMMON_OR_NULL ;
@@ -9099,7 +9099,7 @@ static bool sock_addr_is_valid_access(int off, int size,return false; }break ; -case offsetof(struct bpf_sock_addr , sk): + case bpf_ctx_range_ptr(struct bpf_sock_addr, sk):if (type !=BPF_READ )return false;if (size !=sizeof (__u64))
@@ -9153,17 +9153,17 @@ static bool sock_ops_is_valid_access(int off, int size,if (size !=sizeof (__u64))return false;break ; -case offsetof(struct bpf_sock_ops , sk): + case bpf_ctx_range_ptr(struct bpf_sock_ops, sk):if (size !=sizeof (__u64))return false; info->reg_type =PTR_TO_SOCKET_OR_NULL ;break ; -case offsetof(struct bpf_sock_ops , skb_data): + case bpf_ctx_range_ptr(struct bpf_sock_ops, skb_data):if (size !=sizeof (__u64))return false; info->reg_type =PTR_TO_PACKET ;break ; -case offsetof(struct bpf_sock_ops , skb_data_end): + case bpf_ctx_range_ptr(struct bpf_sock_ops, skb_data_end):if (size !=sizeof (__u64))return false; info->reg_type =PTR_TO_PACKET_END ;
@@ -9238,17 +9238,17 @@ static bool sk_msg_is_valid_access(int off, int size,return false;switch (off) { -case offsetof(struct sk_msg_md , data): + case bpf_ctx_range_ptr(struct sk_msg_md, data): info->reg_type =PTR_TO_PACKET ;if (size !=sizeof (__u64))return false;break ; -case offsetof(struct sk_msg_md , data_end): + case bpf_ctx_range_ptr(struct sk_msg_md, data_end): info->reg_type =PTR_TO_PACKET_END ;if (size !=sizeof (__u64))return false;break ; -case offsetof(struct sk_msg_md , sk): + case bpf_ctx_range_ptr(struct sk_msg_md, sk):if (size !=sizeof (__u64))return false; info->reg_type =PTR_TO_SOCKET ;
@@ -11437,7 +11437,7 @@ static bool sk_lookup_is_valid_access(int off, int size,return false;switch (off) { -case offsetof(struct bpf_sk_lookup , sk): + case bpf_ctx_range_ptr(struct bpf_sk_lookup, sk): info->reg_type =PTR_TO_SOCKET_OR_NULL ;return size ==sizeof (__u64);
Signature · 1 line
2.52.0