From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Qian Cai <cai@lca.pw>, "David S . Miller" <davem@davemloft.net>,
Sasha Levin <sashal@kernel.org>,
netdev@vger.kernel.org
Subject: [PATCH AUTOSEL 4.19 071/206] skbuff: fix a data race in skb_queue_len()
Date: Thu, 17 Sep 2020 22:05:47 -0400 [thread overview]
Message-ID: <20200918020802.2065198-71-sashal@kernel.org> (raw)
In-Reply-To: <20200918020802.2065198-1-sashal@kernel.org>
From: Qian Cai <cai@lca.pw>
[ Upstream commit 86b18aaa2b5b5bb48e609cd591b3d2d0fdbe0442 ]
sk_buff.qlen can be accessed concurrently as noticed by KCSAN,
BUG: KCSAN: data-race in __skb_try_recv_from_queue / unix_dgram_sendmsg
read to 0xffff8a1b1d8a81c0 of 4 bytes by task 5371 on cpu 96:
unix_dgram_sendmsg+0x9a9/0xb70 include/linux/skbuff.h:1821
net/unix/af_unix.c:1761
____sys_sendmsg+0x33e/0x370
___sys_sendmsg+0xa6/0xf0
__sys_sendmsg+0x69/0xf0
__x64_sys_sendmsg+0x51/0x70
do_syscall_64+0x91/0xb47
entry_SYSCALL_64_after_hwframe+0x49/0xbe
write to 0xffff8a1b1d8a81c0 of 4 bytes by task 1 on cpu 99:
__skb_try_recv_from_queue+0x327/0x410 include/linux/skbuff.h:2029
__skb_try_recv_datagram+0xbe/0x220
unix_dgram_recvmsg+0xee/0x850
____sys_recvmsg+0x1fb/0x210
___sys_recvmsg+0xa2/0xf0
__sys_recvmsg+0x66/0xf0
__x64_sys_recvmsg+0x51/0x70
do_syscall_64+0x91/0xb47
entry_SYSCALL_64_after_hwframe+0x49/0xbe
Since only the read is operating as lockless, it could introduce a logic
bug in unix_recvq_full() due to the load tearing. Fix it by adding
a lockless variant of skb_queue_len() and unix_recvq_full() where
READ_ONCE() is on the read while WRITE_ONCE() is on the write similar to
the commit d7d16a89350a ("net: add skb_queue_empty_lockless()").
Signed-off-by: Qian Cai <cai@lca.pw>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
include/linux/skbuff.h | 14 +++++++++++++-
net/unix/af_unix.c | 11 +++++++++--
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 25407c206e732..f0474ea735695 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -1688,6 +1688,18 @@ static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
return list_->qlen;
}
+/**
+ * skb_queue_len_lockless - get queue length
+ * @list_: list to measure
+ *
+ * Return the length of an &sk_buff queue.
+ * This variant can be used in lockless contexts.
+ */
+static inline __u32 skb_queue_len_lockless(const struct sk_buff_head *list_)
+{
+ return READ_ONCE(list_->qlen);
+}
+
/**
* __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
* @list: queue to initialize
@@ -1895,7 +1907,7 @@ static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
{
struct sk_buff *next, *prev;
- list->qlen--;
+ WRITE_ONCE(list->qlen, list->qlen - 1);
next = skb->next;
prev = skb->prev;
skb->next = skb->prev = NULL;
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 2318e2e2748f4..2020306468af4 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -192,11 +192,17 @@ static inline int unix_may_send(struct sock *sk, struct sock *osk)
return unix_peer(osk) == NULL || unix_our_peer(sk, osk);
}
-static inline int unix_recvq_full(struct sock const *sk)
+static inline int unix_recvq_full(const struct sock *sk)
{
return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
}
+static inline int unix_recvq_full_lockless(const struct sock *sk)
+{
+ return skb_queue_len_lockless(&sk->sk_receive_queue) >
+ READ_ONCE(sk->sk_max_ack_backlog);
+}
+
struct sock *unix_peer_get(struct sock *s)
{
struct sock *peer;
@@ -1788,7 +1794,8 @@ restart_locked:
* - unix_peer(sk) == sk by time of get but disconnected before lock
*/
if (other != sk &&
- unlikely(unix_peer(other) != sk && unix_recvq_full(other))) {
+ unlikely(unix_peer(other) != sk &&
+ unix_recvq_full_lockless(other))) {
if (timeo) {
timeo = unix_wait_for_peer(other, timeo);
--
2.25.1
next prev parent reply other threads:[~2020-09-18 2:09 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20200918020802.2065198-1-sashal@kernel.org>
2020-09-18 2:04 ` [PATCH AUTOSEL 4.19 004/206] ath10k: fix array out-of-bounds access Sasha Levin
2020-09-18 2:04 ` [PATCH AUTOSEL 4.19 005/206] ath10k: fix memory leak for tpc_stats_final Sasha Levin
2020-09-18 2:04 ` [PATCH AUTOSEL 4.19 017/206] net: silence data-races on sk_backlog.tail Sasha Levin
2020-09-18 2:05 ` [PATCH AUTOSEL 4.19 043/206] neigh_stat_seq_next() should increase position index Sasha Levin
2020-09-18 2:05 ` [PATCH AUTOSEL 4.19 044/206] rt_cpu_seq_next " Sasha Levin
2020-09-18 2:05 ` [PATCH AUTOSEL 4.19 045/206] ipv6_route_seq_next " Sasha Levin
2020-09-18 2:05 ` [PATCH AUTOSEL 4.19 048/206] sctp: move trace_sctp_probe_path into sctp_outq_sack Sasha Levin
2020-09-18 2:05 ` [PATCH AUTOSEL 4.19 061/206] ar5523: Add USB ID of SMCWUSBT-G2 wireless adapter Sasha Levin
2020-09-18 2:05 ` [PATCH AUTOSEL 4.19 065/206] Bluetooth: Fix refcount use-after-free issue Sasha Levin
2020-09-18 2:05 ` [PATCH AUTOSEL 4.19 068/206] Bluetooth: prefetch channel before killing sock Sasha Levin
2020-09-18 2:05 ` Sasha Levin [this message]
2020-09-18 2:05 ` [PATCH AUTOSEL 4.19 079/206] mt76: clear skb pointers from rx aggregation reorder buffer during cleanup Sasha Levin
2020-09-18 2:06 ` [PATCH AUTOSEL 4.19 087/206] bpf: Remove recursion prevention from rcu free callback Sasha Levin
2020-09-18 2:06 ` [PATCH AUTOSEL 4.19 095/206] Bluetooth: guard against controllers sending zero'd events Sasha Levin
2020-09-18 2:06 ` [PATCH AUTOSEL 4.19 102/206] ath10k: use kzalloc to read for ath10k_sdio_hif_diag_read Sasha Levin
2020-09-18 2:06 ` [PATCH AUTOSEL 4.19 104/206] Bluetooth: L2CAP: handle l2cap config request during open state Sasha Levin
2020-09-18 2:06 ` [PATCH AUTOSEL 4.19 130/206] SUNRPC: Fix a potential buffer overflow in 'svc_print_xprts()' Sasha Levin
2020-09-18 2:06 ` [PATCH AUTOSEL 4.19 131/206] svcrdma: Fix leak of transport addresses Sasha Levin
2020-09-18 2:07 ` [PATCH AUTOSEL 4.19 149/206] net: openvswitch: use u64 for meter bucket Sasha Levin
2020-09-18 2:07 ` [PATCH AUTOSEL 4.19 155/206] atm: fix a memory leak of vcc->user_back Sasha Levin
2020-09-18 2:07 ` [PATCH AUTOSEL 4.19 160/206] Bluetooth: Handle Inquiry Cancel error after Inquiry Complete Sasha Levin
2020-09-18 2:07 ` [PATCH AUTOSEL 4.19 162/206] tipc: fix memory leak in service subscripting Sasha Levin
2020-09-18 2:07 ` [PATCH AUTOSEL 4.19 170/206] e1000: Do not perform reset in reset_task if we are already down Sasha Levin
2020-09-18 2:07 ` [PATCH AUTOSEL 4.19 183/206] perf metricgroup: Free metric_events on error Sasha Levin
2020-09-18 2:07 ` [PATCH AUTOSEL 4.19 186/206] wlcore: fix runtime pm imbalance in wl1271_tx_work Sasha Levin
2020-09-18 2:07 ` [PATCH AUTOSEL 4.19 187/206] wlcore: fix runtime pm imbalance in wlcore_regdomain_config Sasha Levin
2020-09-18 2:08 ` [PATCH AUTOSEL 4.19 205/206] net: openvswitch: use div_u64() for 64-by-32 divisions Sasha Levin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200918020802.2065198-71-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=cai@lca.pw \
--cc=davem@davemloft.net \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).