Skip to content

Commit a4f5759

Browse files
committed
Martin KaFai Lau says: ==================== pull-request: bpf-next 2025-07-24 We've added 3 non-merge commits during the last 3 day(s) which contain a total of 4 files changed, 40 insertions(+), 15 deletions(-). The main changes are: 1) Improved verifier error message for incorrect narrower load from pointer field in ctx, from Paul Chaignon. 2) Disabled migration in nf_hook_run_bpf to address a syzbot report, from Kuniyuki Iwashima. * tag 'for-netdev' of https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next: selftests/bpf: Test invalid narrower ctx load bpf: Reject narrower access to pointer ctx fields bpf: Disable migration in nf_hook_run_bpf(). ==================== Link: https://patch.msgid.link/20250724173306.3578483-1-martin.lau@linux.dev Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents 7dba0cc + ba578b8 commit a4f5759

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

kernel/bpf/cgroup.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,22 +2440,22 @@ static bool cg_sockopt_is_valid_access(int off, int size,
24402440
}
24412441

24422442
switch (off) {
2443-
case offsetof(struct bpf_sockopt, sk):
2443+
case bpf_ctx_range_ptr(struct bpf_sockopt, sk):
24442444
if (size != sizeof(__u64))
24452445
return false;
24462446
info->reg_type = PTR_TO_SOCKET;
24472447
break;
2448-
case offsetof(struct bpf_sockopt, optval):
2448+
case bpf_ctx_range_ptr(struct bpf_sockopt, optval):
24492449
if (size != sizeof(__u64))
24502450
return false;
24512451
info->reg_type = PTR_TO_PACKET;
24522452
break;
2453-
case offsetof(struct bpf_sockopt, optval_end):
2453+
case bpf_ctx_range_ptr(struct bpf_sockopt, optval_end):
24542454
if (size != sizeof(__u64))
24552455
return false;
24562456
info->reg_type = PTR_TO_PACKET_END;
24572457
break;
2458-
case offsetof(struct bpf_sockopt, retval):
2458+
case bpf_ctx_range(struct bpf_sockopt, retval):
24592459
if (size != size_default)
24602460
return false;
24612461
return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;

net/core/filter.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8699,7 +8699,7 @@ static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type
86998699
if (size != sizeof(__u64))
87008700
return false;
87018701
break;
8702-
case offsetof(struct __sk_buff, sk):
8702+
case bpf_ctx_range_ptr(struct __sk_buff, sk):
87038703
if (type == BPF_WRITE || size != sizeof(__u64))
87048704
return false;
87058705
info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
@@ -9277,7 +9277,7 @@ static bool sock_addr_is_valid_access(int off, int size,
92779277
return false;
92789278
}
92799279
break;
9280-
case offsetof(struct bpf_sock_addr, sk):
9280+
case bpf_ctx_range_ptr(struct bpf_sock_addr, sk):
92819281
if (type != BPF_READ)
92829282
return false;
92839283
if (size != sizeof(__u64))
@@ -9327,17 +9327,17 @@ static bool sock_ops_is_valid_access(int off, int size,
93279327
if (size != sizeof(__u64))
93289328
return false;
93299329
break;
9330-
case offsetof(struct bpf_sock_ops, sk):
9330+
case bpf_ctx_range_ptr(struct bpf_sock_ops, sk):
93319331
if (size != sizeof(__u64))
93329332
return false;
93339333
info->reg_type = PTR_TO_SOCKET_OR_NULL;
93349334
break;
9335-
case offsetof(struct bpf_sock_ops, skb_data):
9335+
case bpf_ctx_range_ptr(struct bpf_sock_ops, skb_data):
93369336
if (size != sizeof(__u64))
93379337
return false;
93389338
info->reg_type = PTR_TO_PACKET;
93399339
break;
9340-
case offsetof(struct bpf_sock_ops, skb_data_end):
9340+
case bpf_ctx_range_ptr(struct bpf_sock_ops, skb_data_end):
93419341
if (size != sizeof(__u64))
93429342
return false;
93439343
info->reg_type = PTR_TO_PACKET_END;
@@ -9346,7 +9346,7 @@ static bool sock_ops_is_valid_access(int off, int size,
93469346
bpf_ctx_record_field_size(info, size_default);
93479347
return bpf_ctx_narrow_access_ok(off, size,
93489348
size_default);
9349-
case offsetof(struct bpf_sock_ops, skb_hwtstamp):
9349+
case bpf_ctx_range(struct bpf_sock_ops, skb_hwtstamp):
93509350
if (size != sizeof(__u64))
93519351
return false;
93529352
break;
@@ -9416,17 +9416,17 @@ static bool sk_msg_is_valid_access(int off, int size,
94169416
return false;
94179417

94189418
switch (off) {
9419-
case offsetof(struct sk_msg_md, data):
9419+
case bpf_ctx_range_ptr(struct sk_msg_md, data):
94209420
info->reg_type = PTR_TO_PACKET;
94219421
if (size != sizeof(__u64))
94229422
return false;
94239423
break;
9424-
case offsetof(struct sk_msg_md, data_end):
9424+
case bpf_ctx_range_ptr(struct sk_msg_md, data_end):
94259425
info->reg_type = PTR_TO_PACKET_END;
94269426
if (size != sizeof(__u64))
94279427
return false;
94289428
break;
9429-
case offsetof(struct sk_msg_md, sk):
9429+
case bpf_ctx_range_ptr(struct sk_msg_md, sk):
94309430
if (size != sizeof(__u64))
94319431
return false;
94329432
info->reg_type = PTR_TO_SOCKET;
@@ -11632,7 +11632,7 @@ static bool sk_lookup_is_valid_access(int off, int size,
1163211632
return false;
1163311633

1163411634
switch (off) {
11635-
case offsetof(struct bpf_sk_lookup, sk):
11635+
case bpf_ctx_range_ptr(struct bpf_sk_lookup, sk):
1163611636
info->reg_type = PTR_TO_SOCKET_OR_NULL;
1163711637
return size == sizeof(__u64);
1163811638

net/netfilter/nf_bpf_link.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static unsigned int nf_hook_run_bpf(void *bpf_prog, struct sk_buff *skb,
1717
.skb = skb,
1818
};
1919

20-
return bpf_prog_run(prog, &ctx);
20+
return bpf_prog_run_pin_on_cpu(prog, &ctx);
2121
}
2222

2323
struct bpf_nf_link {

tools/testing/selftests/bpf/progs/verifier_ctx.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,29 @@ __naked void null_check_8_null_bind(void)
218218
: __clobber_all);
219219
}
220220

221+
#define narrow_load(type, ctx, field) \
222+
SEC(type) \
223+
__description("narrow load on field " #field " of " #ctx) \
224+
__failure __msg("invalid bpf_context access") \
225+
__naked void invalid_narrow_load##ctx##field(void) \
226+
{ \
227+
asm volatile (" \
228+
r1 = *(u32 *)(r1 + %[off]); \
229+
r0 = 0; \
230+
exit;" \
231+
: \
232+
: __imm_const(off, offsetof(struct ctx, field) + 4) \
233+
: __clobber_all); \
234+
}
235+
236+
narrow_load("cgroup/getsockopt", bpf_sockopt, sk);
237+
narrow_load("cgroup/getsockopt", bpf_sockopt, optval);
238+
narrow_load("cgroup/getsockopt", bpf_sockopt, optval_end);
239+
narrow_load("tc", __sk_buff, sk);
240+
narrow_load("cgroup/bind4", bpf_sock_addr, sk);
241+
narrow_load("sockops", bpf_sock_ops, sk);
242+
narrow_load("sockops", bpf_sock_ops, skb_data);
243+
narrow_load("sockops", bpf_sock_ops, skb_data_end);
244+
narrow_load("sockops", bpf_sock_ops, skb_hwtstamp);
245+
221246
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)