Skip to contentSkip to conversation

Avoid warning on bpf_sock_addr padding access

PATCH Applied Paul Chaignon 2 participants 5 messages Sep 17, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover Avoid warning on bpf_sock_addr padding access

This patchset fixes bpf_sock_addr padding access to avoid a kernel warning and improves our selftests coverage for these ctx padding cases.

Changes in v2:
- Rebased on top of bpf-next.
- Added selftests for paddings in bpf_sock and sk_reuseport_md.
- Simplified sock_addr_is_valid_access's logic, as suggested by Daniel.
- Removed a tab copied from existing code and spotted by Eduard.

Paul Chaignon (3):
bpf: Explicitly check accesses to bpf_sock_addr
selftests/bpf: Move macros to bpf_misc.h
selftest/bpf: Test accesses to ctx padding

 net/core/filter.c                             | 16 ++++++----
 tools/testing/selftests/bpf/progs/bpf_misc.h  |  4 +++
 .../selftests/bpf/progs/test_cls_redirect.c   |  4 +--
 .../bpf/progs/test_tcp_hdr_options.c          |  5 +--
 .../selftests/bpf/progs/verifier_ctx.c        | 32 +++++++++++++++++--
 .../selftests/bpf/progs/verifier_sock.c       |  4 ---
 6 files changed, 46 insertions(+), 19 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 bpf: Explicitly check accesses to bpf_sock_addr

Syzkaller found a kernel warning on the following sock_addr program:

    0: r0 = 0
    1: r2 = *(u32 *)(r1 +60)
    2: exit

which triggers:

verifier bug: error during ctx access conversion (0)

This is happening because offset 60 in bpf_sock_addr corresponds to an implicit padding of 4 bytes, right after msg_src_ip4. Access to this padding isn't rejected in sock_addr_is_valid_access and it thus later fails to convert the access.

This patch fixes it by explicitly checking the various fields of bpf_sock_addr in sock_addr_is_valid_access.

I checked the other ctx structures and is_valid_access functions and didn't find any other similar cases. Other cases of (properly handled) padding are covered in new tests in a subsequent patch.

---
 net/core/filter.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
net/core/filter.c +10 -6
index 63f3baee2daf..8342f810ad85 100644
@@ -9284,13 +9284,17 @@ static bool sock_addr_is_valid_access(int off, int size,
 			return false;
 		info->reg_type = PTR_TO_SOCKET;
 		break;
-	default:
-		if (type == BPF_READ) {
-			if (size != size_default)
-				return false;
-		} else {
+	case bpf_ctx_range(struct bpf_sock_addr, user_family):
+	case bpf_ctx_range(struct bpf_sock_addr, family):
+	case bpf_ctx_range(struct bpf_sock_addr, type):
+	case bpf_ctx_range(struct bpf_sock_addr, protocol):
+		if (type != BPF_READ)
 			return false;
-		}
+		if (size != size_default)
+			return false;
+		break;
+	default:
+		return false;
 	}
 
 	return true;
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/3 selftests/bpf: Move macros to bpf_misc.h

In reply to Paul Chaignon

Move the sizeof_field and offsetofend macros from individual test files to the common bpf_misc.h to avoid duplication.

---
 tools/testing/selftests/bpf/progs/bpf_misc.h             | 4 ++++
 tools/testing/selftests/bpf/progs/test_cls_redirect.c    | 4 +---
 tools/testing/selftests/bpf/progs/test_tcp_hdr_options.c | 5 +----
 tools/testing/selftests/bpf/progs/verifier_ctx.c         | 2 --
 tools/testing/selftests/bpf/progs/verifier_sock.c        | 4 ----
 5 files changed, 6 insertions(+), 13 deletions(-)
tools/testing/selftests/bpf/progs/bpf_misc.h +4 -0
index 7905396c9cc4..1004c4a64aaf 100644
@@ -167,6 +167,10 @@
 #define __imm_ptr(name) [name]"r"(&name)
 #define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
 
+#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
+#define offsetofend(TYPE, MEMBER) \
+	(offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
+
 /* Magic constants used with __retval() */
 #define POINTER_VALUE	0xbadcafe
 #define TEST_DATA_LEN	64
tools/testing/selftests/bpf/progs/test_cls_redirect.c +1 -3
index 823169fb6e4c..26a53e54b8fa 100644
@@ -22,6 +22,7 @@
 
 #include "bpf_compiler.h"
 #include "test_cls_redirect.h"
+#include "bpf_misc.h"
 
 #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
 
@@ -31,9 +32,6 @@
 #define INLINING __always_inline
 #endif
 
-#define offsetofend(TYPE, MEMBER) \
-	(offsetof(TYPE, MEMBER) + sizeof((((TYPE *)0)->MEMBER)))
-
 #define IP_OFFSET_MASK (0x1FFF)
 #define IP_MF (0x2000)
 
tools/testing/selftests/bpf/progs/test_tcp_hdr_options.c +1 -4
index 5f4e87ee949a..1ecdf4c54de4 100644
@@ -14,10 +14,7 @@
 #include <bpf/bpf_endian.h>
 #define BPF_PROG_TEST_TCP_HDR_OPTIONS
 #include "test_tcp_hdr_options.h"
-
-#ifndef sizeof_field
-#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
-#endif
+#include "bpf_misc.h"
 
 __u8 test_kind = TCPOPT_EXP;
 __u16 test_magic = 0xeB9F;
tools/testing/selftests/bpf/progs/verifier_ctx.c +0 -2
index 424463094760..b927906aa305 100644
@@ -5,8 +5,6 @@
 #include <bpf/bpf_helpers.h>
 #include "bpf_misc.h"
 
-#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
-
 SEC("tc")
 __description("context stores via BPF_ATOMIC")
 __failure __msg("BPF_ATOMIC stores into R1 ctx is not allowed")
tools/testing/selftests/bpf/progs/verifier_sock.c +0 -4
index 0d5e56dffabb..bf88c644eb30 100644
@@ -5,10 +5,6 @@
 #include <bpf/bpf_helpers.h>
 #include "bpf_misc.h"
 
-#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
-#define offsetofend(TYPE, MEMBER) \
-	(offsetof(TYPE, MEMBER)	+ sizeof_field(TYPE, MEMBER))
-
 struct {
 	__uint(type, BPF_MAP_TYPE_REUSEPORT_SOCKARRAY);
 	__uint(max_entries, 1);
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/3 selftest/bpf: Test accesses to ctx padding

In reply to Paul Chaignon

This patch adds tests covering the various paddings in ctx structures. In case of sk_lookup BPF programs, the behavior is a bit different because accesses to the padding are explicitly allowed. Other cases result in a clear reject from the verifier.

---
 .../selftests/bpf/progs/verifier_ctx.c        | 30 +++++++++++++++++++
 1 file changed, 30 insertions(+)
tools/testing/selftests/bpf/progs/verifier_ctx.c +30 -0
index b927906aa305..5ebf7d9bcc55 100644
@@ -262,4 +262,34 @@ narrow_load("sockops", bpf_sock_ops, skb_hwtstamp);
 unaligned_access("flow_dissector", __sk_buff, data);
 unaligned_access("netfilter", bpf_nf_ctx, skb);
 
+#define padding_access(type, ctx, prev_field, sz)			\
+	SEC(type)							\
+	__description("access on " #ctx " padding after " #prev_field)	\
+	__naked void padding_ctx_access_##ctx(void)			\
+	{								\
+		asm volatile ("						\
+		r1 = *(u%[size] *)(r1 + %[off]);			\
+		r0 = 0;							\
+		exit;"							\
+		:							\
+		: __imm_const(size, sz * 8),				\
+		  __imm_const(off, offsetofend(struct ctx, prev_field))	\
+		: __clobber_all);					\
+	}
+
+__failure __msg("invalid bpf_context access")
+padding_access("cgroup/bind4", bpf_sock_addr, msg_src_ip6[3], 4);
+
+__success
+padding_access("sk_lookup", bpf_sk_lookup, remote_port, 2);
+
+__failure __msg("invalid bpf_context access")
+padding_access("tc", __sk_buff, tstamp_type, 2);
+
+__failure __msg("invalid bpf_context access")
+padding_access("cgroup/post_bind4", bpf_sock, dst_port, 2);
+
+__failure __msg("invalid bpf_context access")
+padding_access("sk_reuseport", sk_reuseport_md, hash, 4);
+
 char _license[] SEC("license") = "GPL";
Signature · 1 line
2.43.0
#4 lore raw
In reply to Paul Chaignon
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 10 lines · On Wed, 17 Sep 2025 10:07:08 +0200 you wrote:

This patchset fixes bpf_sock_addr padding access to avoid a kernel warning and improves our selftests coverage for these ctx padding cases.

Changes in v2:
- Rebased on top of bpf-next.
- Added selftests for paddings in bpf_sock and sk_reuseport_md.
- Simplified sock_addr_is_valid_access's logic, as suggested by Daniel.
- Removed a tab copied from existing code and spotted by Eduard.

[...]

Here is the summary with links:
- [bpf-next,v2,1/3] bpf: Explicitly check accesses to bpf_sock_addr

    https://git.kernel.org/bpf/bpf-next/c/6fabca2fc94d
  - [bpf-next,v2,2/3] selftests/bpf: Move macros to bpf_misc.h
    https://git.kernel.org/bpf/bpf-next/c/7c60f6e488b7
  - [bpf-next,v2,3/3] selftest/bpf: Test accesses to ctx padding
    https://git.kernel.org/bpf/bpf-next/c/180a46bc1a1c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html