Skip to content

Commit 32155c6

Browse files
committed
Daniel Borkmann says: ==================== pull-request: bpf-next 2025-06-27 We've added 6 non-merge commits during the last 8 day(s) which contain a total of 6 files changed, 120 insertions(+), 20 deletions(-). The main changes are: 1) Fix RCU usage in task_cls_state() for BPF programs using helpers like bpf_get_cgroup_classid_curr() outside of networking, from Charalampos Mitrodimas. 2) Fix a sockmap race between map_update and a pending workqueue from an earlier map_delete freeing the old psock where both pointed to the same psock->sk, from Jiayuan Chen. 3) Fix a data corruption issue when using bpf_msg_pop_data() in kTLS which failed to recalculate the ciphertext length, also from Jiayuan Chen. 4) Remove xdp_redirect_map{,_err} trace events since they are unused and also hide XDP trace events under CONFIG_BPF_SYSCALL, from Steven Rostedt. * tag 'for-netdev' of https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next: xdp: tracing: Hide some xdp events under CONFIG_BPF_SYSCALL xdp: Remove unused events xdp_redirect_map and xdp_redirect_map_err net, bpf: Fix RCU usage in task_cls_state() for BPF programs selftests/bpf: Add test to cover ktls with bpf_msg_pop_data bpf, ktls: Fix data corruption when using bpf_msg_pop_data() in ktls bpf, sockmap: Fix psock incorrectly pointing to sk ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 4cd9d22 + 16f3c7a commit 32155c6

File tree

6 files changed

+120
-20
lines changed

6 files changed

+120
-20
lines changed

include/trace/events/xdp.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,7 @@ DEFINE_EVENT(xdp_redirect_template, xdp_redirect_err,
168168
#define _trace_xdp_redirect_map_err(dev, xdp, to, map_type, map_id, index, err) \
169169
trace_xdp_redirect_err(dev, xdp, to, err, map_type, map_id, index)
170170

171-
/* not used anymore, but kept around so as not to break old programs */
172-
DEFINE_EVENT(xdp_redirect_template, xdp_redirect_map,
173-
TP_PROTO(const struct net_device *dev,
174-
const struct bpf_prog *xdp,
175-
const void *tgt, int err,
176-
enum bpf_map_type map_type,
177-
u32 map_id, u32 index),
178-
TP_ARGS(dev, xdp, tgt, err, map_type, map_id, index)
179-
);
180-
181-
DEFINE_EVENT(xdp_redirect_template, xdp_redirect_map_err,
182-
TP_PROTO(const struct net_device *dev,
183-
const struct bpf_prog *xdp,
184-
const void *tgt, int err,
185-
enum bpf_map_type map_type,
186-
u32 map_id, u32 index),
187-
TP_ARGS(dev, xdp, tgt, err, map_type, map_id, index)
188-
);
189-
171+
#ifdef CONFIG_BPF_SYSCALL
190172
TRACE_EVENT(xdp_cpumap_kthread,
191173

192174
TP_PROTO(int map_id, unsigned int processed, unsigned int drops,
@@ -300,6 +282,7 @@ TRACE_EVENT(xdp_devmap_xmit,
300282
__entry->sent, __entry->drops,
301283
__entry->err)
302284
);
285+
#endif /* CONFIG_BPF_SYSCALL */
303286

304287
/* Expect users already include <net/xdp.h>, but not xdp_priv.h */
305288
#include <net/xdp_priv.h>

net/core/netclassid_cgroup.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ static inline struct cgroup_cls_state *css_cls_state(struct cgroup_subsys_state
2121
struct cgroup_cls_state *task_cls_state(struct task_struct *p)
2222
{
2323
return css_cls_state(task_css_check(p, net_cls_cgrp_id,
24-
rcu_read_lock_bh_held()));
24+
rcu_read_lock_held() ||
25+
rcu_read_lock_bh_held() ||
26+
rcu_read_lock_trace_held()));
2527
}
2628
EXPORT_SYMBOL_GPL(task_cls_state);
2729

net/core/skmsg.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,13 @@ static void sk_psock_backlog(struct work_struct *work)
656656
bool ingress;
657657
int ret;
658658

659+
/* If sk is quickly removed from the map and then added back, the old
660+
* psock should not be scheduled, because there are now two psocks
661+
* pointing to the same sk.
662+
*/
663+
if (!sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED))
664+
return;
665+
659666
/* Increment the psock refcnt to synchronize with close(fd) path in
660667
* sock_map_close(), ensuring we wait for backlog thread completion
661668
* before sk_socket freed. If refcnt increment fails, it indicates

net/tls/tls_sw.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,19 @@ static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
872872
delta = msg->sg.size;
873873
psock->eval = sk_psock_msg_verdict(sk, psock, msg);
874874
delta -= msg->sg.size;
875+
876+
if ((s32)delta > 0) {
877+
/* It indicates that we executed bpf_msg_pop_data(),
878+
* causing the plaintext data size to decrease.
879+
* Therefore the encrypted data size also needs to
880+
* correspondingly decrease. We only need to subtract
881+
* delta to calculate the new ciphertext length since
882+
* ktls does not support block encryption.
883+
*/
884+
struct sk_msg *enc = &ctx->open_rec->msg_encrypted;
885+
886+
sk_msg_trim(sk, enc, enc->sg.size - delta);
887+
}
875888
}
876889
if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
877890
!enospc && !full_record) {

tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,95 @@ static void test_sockmap_ktls_tx_no_buf(int family, int sotype, bool push)
314314
test_sockmap_ktls__destroy(skel);
315315
}
316316

317+
static void test_sockmap_ktls_tx_pop(int family, int sotype)
318+
{
319+
char msg[37] = "0123456789abcdefghijklmnopqrstuvwxyz\0";
320+
int c = 0, p = 0, one = 1, sent, recvd;
321+
struct test_sockmap_ktls *skel;
322+
int prog_fd, map_fd;
323+
char rcv[50] = {0};
324+
int err;
325+
int i, m, r;
326+
327+
skel = test_sockmap_ktls__open_and_load();
328+
if (!ASSERT_TRUE(skel, "open ktls skel"))
329+
return;
330+
331+
err = create_pair(family, sotype, &c, &p);
332+
if (!ASSERT_OK(err, "create_pair()"))
333+
goto out;
334+
335+
prog_fd = bpf_program__fd(skel->progs.prog_sk_policy);
336+
map_fd = bpf_map__fd(skel->maps.sock_map);
337+
338+
err = bpf_prog_attach(prog_fd, map_fd, BPF_SK_MSG_VERDICT, 0);
339+
if (!ASSERT_OK(err, "bpf_prog_attach sk msg"))
340+
goto out;
341+
342+
err = bpf_map_update_elem(map_fd, &one, &c, BPF_NOEXIST);
343+
if (!ASSERT_OK(err, "bpf_map_update_elem(c)"))
344+
goto out;
345+
346+
err = init_ktls_pairs(c, p);
347+
if (!ASSERT_OK(err, "init_ktls_pairs(c, p)"))
348+
goto out;
349+
350+
struct {
351+
int pop_start;
352+
int pop_len;
353+
} pop_policy[] = {
354+
/* trim the start */
355+
{0, 2},
356+
{0, 10},
357+
{1, 2},
358+
{1, 10},
359+
/* trim the end */
360+
{35, 2},
361+
/* New entries should be added before this line */
362+
{-1, -1},
363+
};
364+
365+
i = 0;
366+
while (pop_policy[i].pop_start >= 0) {
367+
skel->bss->pop_start = pop_policy[i].pop_start;
368+
skel->bss->pop_end = pop_policy[i].pop_len;
369+
370+
sent = send(c, msg, sizeof(msg), 0);
371+
if (!ASSERT_EQ(sent, sizeof(msg), "send(msg)"))
372+
goto out;
373+
374+
recvd = recv_timeout(p, rcv, sizeof(rcv), MSG_DONTWAIT, 1);
375+
if (!ASSERT_EQ(recvd, sizeof(msg) - pop_policy[i].pop_len, "pop len mismatch"))
376+
goto out;
377+
378+
/* verify the data
379+
* msg: 0123456789a bcdefghij klmnopqrstuvwxyz
380+
* | |
381+
* popped data
382+
*/
383+
for (m = 0, r = 0; m < sizeof(msg);) {
384+
/* skip checking the data that has been popped */
385+
if (m >= pop_policy[i].pop_start &&
386+
m <= pop_policy[i].pop_start + pop_policy[i].pop_len - 1) {
387+
m++;
388+
continue;
389+
}
390+
391+
if (!ASSERT_EQ(msg[m], rcv[r], "data mismatch"))
392+
goto out;
393+
m++;
394+
r++;
395+
}
396+
i++;
397+
}
398+
out:
399+
if (c)
400+
close(c);
401+
if (p)
402+
close(p);
403+
test_sockmap_ktls__destroy(skel);
404+
}
405+
317406
static void run_tests(int family, enum bpf_map_type map_type)
318407
{
319408
int map;
@@ -338,6 +427,8 @@ static void run_ktls_test(int family, int sotype)
338427
test_sockmap_ktls_tx_cork(family, sotype, true);
339428
if (test__start_subtest("tls tx egress with no buf"))
340429
test_sockmap_ktls_tx_no_buf(family, sotype, true);
430+
if (test__start_subtest("tls tx with pop"))
431+
test_sockmap_ktls_tx_pop(family, sotype);
341432
}
342433

343434
void test_sockmap_ktls(void)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ int cork_byte;
77
int push_start;
88
int push_end;
99
int apply_bytes;
10+
int pop_start;
11+
int pop_end;
1012

1113
struct {
1214
__uint(type, BPF_MAP_TYPE_SOCKMAP);
@@ -22,6 +24,8 @@ int prog_sk_policy(struct sk_msg_md *msg)
2224
bpf_msg_cork_bytes(msg, cork_byte);
2325
if (push_start > 0 && push_end > 0)
2426
bpf_msg_push_data(msg, push_start, push_end, 0);
27+
if (pop_start >= 0 && pop_end > 0)
28+
bpf_msg_pop_data(msg, pop_start, pop_end, 0);
2529

2630
return SK_PASS;
2731
}

0 commit comments

Comments
 (0)