Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp880098pxk; Thu, 17 Sep 2020 20:10:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyV90q0NrZYibgsgJnuBHuz3hR6ftFOkRZ+eWPgSEYo20HLP/EHpOH5diQPpJqeoxhJzJj+ X-Received: by 2002:aa7:d5d2:: with SMTP id d18mr35646437eds.115.1600398650918; Thu, 17 Sep 2020 20:10:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600398650; cv=none; d=google.com; s=arc-20160816; b=kzFMU3g1hn7NC0Tv4nE01odEoXpR8IcOA046yCH7tOd34+Q8YQ4EV0lsuiwXnorchX F4MCeOxJl16N604vZwWYlOTti5dV7R7Hl788YcD8HKkmhJPu5Mh4E9gUuihMBvyYd/cz l3oEVBlAWM8zxaUWbx5QswIFouT/WDwz04AjvUawdzOL5zlo8O7RbcY0/bI0qTb/87nc rnJVQY8aJjjv3Ah4CPo/jItS4rudWHw0o6dEk5wkRr/U6L9yZHh30+nrJzps2FCGiOPZ EpIzs8Df6uUoo0xPDPOi0IHmMldLK9ESA1DKxnaDoPNuk1MK9yWtCA1+pbKjeeyvZ59Z G6LQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=E85RfnBnh33JLec3HD8w/QxHabI/bDXlKu/Hh0uvid4=; b=o+DukkCYlguXY+qU0UaOLuGAiQI1Eu6yjl+TeHACg0ncwSmLFbTvi9bbJ5ohVKn/K6 zZP31DzCCxDJtVbZcC2UsKKr/bO6I/C5JPmBYl1/KaOKEsKO9Zz5FLqAVL+TDZuS8kXp mYuoWfBf5bagA6hEcI7FheZzTIkt8jDvzOqyhrLx6KRZZAP4W3OrVYr6PnYiC8RF9a0h xVBWx1R455hiwgH5wvQ+fFUyzpNWCtS/RXnMcZo4MrpSdwjjyWKlDjoyNOqDQiW8PGCM kaj5FrxKU6iKdkRPpwRPaaExdb9cff+N2eQSmF3VlNkR2tNQgAVSlLLPfEP3o+wr0wyY 1hlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KdzZ4vbo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q6si1088541edw.446.2020.09.17.20.10.28; Thu, 17 Sep 2020 20:10:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KdzZ4vbo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728590AbgIRDIH (ORCPT + 99 others); Thu, 17 Sep 2020 23:08:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:50036 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727021AbgIRCDf (ORCPT ); Thu, 17 Sep 2020 22:03:35 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id F20A02344C; Fri, 18 Sep 2020 02:03:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394614; bh=NMvL6F44uJivjVEt6UaYAQiao54USLY1I/4idihbcdA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KdzZ4vbou74zL80smsL52yJlPTedGNJq1ww9XAyX0kUdVj79gy1zIA3DVr6eqKjBx LZ0HR99H88LRaFkfMdCO9Fjm4GEto2xJGt8H+SC3KNdJYQdYtx8JkW9s0QCgbjiOpE pivlWZ9f7Z3EQqkTo1Qo4UaGWq3GmoVpADVj0fNA= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Qian Cai , "David S . Miller" , Sasha Levin , netdev@vger.kernel.org Subject: [PATCH AUTOSEL 5.4 117/330] skbuff: fix a data race in skb_queue_len() Date: Thu, 17 Sep 2020 21:57:37 -0400 Message-Id: <20200918020110.2063155-117-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qian Cai [ 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 Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- 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 955e1370f033d..7afbbc7eaa4f4 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -1816,6 +1816,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 @@ -2021,7 +2033,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 b3369d678f1af..ecadd9e482c46 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -189,11 +189,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; @@ -1724,7 +1730,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