linux/net/core
Jiayuan Chen 3d1e878f70 bpf, sockmap: Fix psock incorrectly pointing to sk
[ Upstream commit 76be5fae32 ]

We observed an issue from the latest selftest: sockmap_redir where
sk_psock(psock->sk) != psock in the backlog. The root cause is the special
behavior in sockmap_redir - it frequently performs map_update() and
map_delete() on the same socket. During map_update(), we create a new
psock and during map_delete(), we eventually free the psock via rcu_work
in sk_psock_drop(). However, pending workqueues might still exist and not
be processed yet. If users immediately perform another map_update(), a new
psock will be allocated for the same sk, resulting in two psocks pointing
to the same sk.

When the pending workqueue is later triggered, it uses the old psock to
access sk for I/O operations, which is incorrect.

Timing Diagram:

cpu0                        cpu1

map_update(sk):
    sk->psock = psock1
    psock1->sk = sk
map_delete(sk):
   rcu_work_free(psock1)

map_update(sk):
    sk->psock = psock2
    psock2->sk = sk
                            workqueue:
                                wakeup with psock1, but the sk of psock1
                                doesn't belong to psock1
rcu_handler:
    clean psock1
    free(psock1)

Previously, we used reference counting to address the concurrency issue
between backlog and sock_map_close(). This logic remains necessary as it
prevents the sk from being freed while processing the backlog. But this
patch prevents pending backlogs from using a psock after it has been
stopped.

Note: We cannot call cancel_delayed_work_sync() in map_delete() since this
might be invoked in BPF context by BPF helper, and the function may sleep.

Fixes: 604326b41a ("bpf, sockmap: convert to generic sk_msg interface")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20250609025908.79331-1-jiayuan.chen@linux.dev
Signed-off-by: Sasha Levin <sashal@kernel.org>
History 2025-08-15 12:04:56 +02:00
..
bpf_sk_storage.c bpf: Refactor some inode/task/sk storage functions for reuse 2024-07-18 13:18:34 +02:00
datagram.c net: fix rc7's __skb_datagram_iter() 2024-07-18 13:18:33 +02:00
dev.c net: Add non-RCU dev_getbyhwaddr() helper 2025-03-07 16:56:36 +01:00
dev.h net: fix removing a namespace with conflicting altnames 2024-01-31 16:17:04 -08:00
dev_addr_lists.c net: extract a few internals from netdevice.h 2022-04-07 20:32:09 -07:00
dev_addr_lists_test.c net: kunit: add a test for dev_addr_lists 2021-11-20 12:25:57 +00:00
dev_ioctl.c net: dev: Convert sa_data to flexible array in struct sockaddr 2024-03-01 13:26:36 +01:00
drop_monitor.c drop_monitor: fix incorrect initialization order 2025-03-07 16:56:38 +01:00
dst.c net: do not delay dst_entries_add() in dst_release() 2024-11-14 13:15:17 +01:00
dst_cache.c wireguard: device: reset peer src endpoint when netns exits 2021-11-29 19:50:45 -08:00
failover.c net: rename reference+tracking helpers 2022-06-09 21:52:55 -07:00
fib_notifier.c net: fib_notifier: propagate extack down to the notifier block callback 2019-10-04 11:10:56 -07:00
fib_rules.c fib: expand fib_rule_policy 2021-12-16 07:18:35 -08:00
filter.c bpf: Fix L4 csum update on IPv6 in CHECKSUM_COMPLETE 2025-06-27 11:07:38 +01:00
flow_dissector.c flow_dissector: Fix port range key handling in BPF conversion 2025-03-07 16:56:35 +01:00
flow_offload.c flow_offload: Introduce flow_match_l2tpv3 2022-09-20 09:13:38 +02:00
gen_estimator.c net: use unrcu_pointer() helper 2024-12-14 19:53:33 +01:00
gen_stats.c net: sched: fix misuse of qcpu->backlog in gnet_stats_add_queue_cpu 2022-08-16 19:38:20 -07:00
gro.c net: Clear old fragment checksum value in napi_reuse_skb 2025-03-07 16:56:45 +01:00
gro_cells.c net: drop the weight argument from netif_napi_add 2022-09-28 18:57:14 -07:00
hwbm.c net: hwbm: Make the hwbm_pool lock a mutex 2019-06-09 19:40:10 -07:00
link_watch.c net: linkwatch: use system_unbound_wq 2024-08-14 13:52:44 +02:00
lwt_bpf.c lwt: Fix return values of BPF xmit ops 2023-09-13 09:42:33 +02:00
lwtunnel.c net: lwtunnel: disable BHs when required 2025-05-02 07:46:54 +02:00
Makefile devlink: move code to a dedicated directory 2023-08-30 16:11:00 +02:00
neighbour.c net/neighbor: add missing policy for NDTPA_QUEUE_LENBYTES 2025-03-28 21:59:00 +01:00
net-procfs.c net: extract a few internals from netdevice.h 2022-04-07 20:32:09 -07:00
net-sysfs.c ethtool: check device is present when getting link settings 2024-09-04 13:25:01 +02:00
net-sysfs.h net-sysfs: add netdev_change_owner() 2020-02-26 20:07:25 -08:00
net-traces.c tcp: add tracepoint for checksum errors 2021-05-14 15:26:03 -07:00
net_namespace.c net: add exit_batch_rtnl() method 2025-01-23 17:17:09 +01:00
netclassid_cgroup.c core: Variable type completion 2022-08-31 09:40:34 +01:00
netevent.c net: core: Correct function name netevent_unregister_notifier() in the kerneldoc 2021-03-28 17:56:56 -07:00
netpoll.c netpoll: hold rcu read lock in __netpoll_send_skb() 2025-03-28 21:58:49 +01:00
netprio_cgroup.c bpf, cgroups: Fix cgroup v2 fallback on v1/v2 mixed mode 2021-09-13 16:35:58 -07:00
of_net.c Revert "of: net: support NVMEM cells with MAC in text format" 2022-01-12 14:14:36 +00:00
page_pool.c page_pool: avoid infinite loop to schedule delayed worker 2025-04-25 10:43:29 +02:00
pktgen.c net: pktgen: fix access outside of user given buffer in pktgen_thread_write() 2025-06-04 14:40:12 +02:00
ptp_classifier.c ptp: Add generic PTP is_sync() function 2022-03-07 11:31:34 +00:00
request_sock.c tcp: make sure init the accept_queue's spinlocks once 2024-01-31 16:17:03 -08:00
rtnetlink.c rtnetlink: Allocate vfinfo size for VF GUIDs when supported 2025-04-10 14:33:37 +02:00
scm.c af_unix: Add dead flag to struct scm_fp_list. 2025-06-04 14:40:24 +02:00
secure_seq.c tcp: Fix data-races around sysctl knobs related to SYN option. 2022-07-20 10:14:49 +01:00
selftests.c net: selftests: fix TCP packet checksum 2025-07-06 10:58:00 +02:00
skbuff.c ipvs: Always clear ipvs_property flag in skb_scrub_packet() 2025-03-07 16:56:45 +01:00
skmsg.c bpf, sockmap: Fix psock incorrectly pointing to sk 2025-08-15 12:04:56 +02:00
sock.c sock: Correct error checking condition for (assign|release)_proto_idx() 2025-06-27 11:07:35 +01:00
sock_destructor.h skb_expand_head() adjust skb->truesize incorrectly 2021-10-22 12:35:51 -07:00
sock_diag.c net: use unrcu_pointer() helper 2024-12-14 19:53:33 +01:00
sock_map.c bpf, sockmap: Fix race between element replace and close() 2025-01-17 13:34:36 +01:00
sock_reuseport.c soreuseport: Fix socket selection for SO_INCOMING_CPU. 2022-12-31 13:32:04 +01:00
stream.c net: Return error from sk_stream_wait_connect() if sk_wait_event() fails 2024-01-01 12:38:56 +00:00
sysctl_net_core.c net: set the minimum for net_hotdata.netdev_budget_usecs 2025-03-07 16:56:43 +01:00
timestamping.c net: Introduce a new MII time stamping interface. 2019-12-25 19:51:33 -08:00
tso.c net: tso: add UDP segmentation support 2020-06-18 20:46:23 -07:00
utils.c net: Fix checksum update for ILA adj-transport 2025-06-27 11:07:38 +01:00
xdp.c xdp: fix invalid wait context of page_pool_destroy() 2024-08-03 08:49:09 +02:00