Article View: linux.kernel.netdev
Article #21623[PATCH net 03/12] can: raw: use a private mutex instead of lock_sock()
From: Marc Kleine-Budde
Date: Tue, 24 Nov 2026 08:51
Date: Tue, 24 Nov 2026 08:51
207 lines
17367 bytes
17367 bytes
From: Oliver Hartkopp <socketcan@hartkopp.net>
raw_notify() takes lock_sock() of the sockets that are bound to a
vanishing CAN device while holding RTNL. Any path that holds the socket
lock while accessing user space can therefore block RTNL. Since
commit 43c973a0887d ("can: raw, isotp: copy socket options outside of
lock_sock()") the CAN_RAW specific socket options copy their values
outside of the socket lock, but the generic SOL_SOCKET options in
sk_setsockopt() and sk_getsockopt() still access user space under
sockopt_lock_sock(), e.g. for SO_ATTACH_FILTER, SO_LINGER or SO_TXTIME.
A stalled copy, e.g. on a FUSE backed mapping, then blocks raw_notify()
and RTNL on the unregistration or namespace change of the bound CAN
device.
CAN_RAW only uses lock_sock() to serialize its own state: the binding,
the filters and the CAN_RAW socket options. It does not use the socket
backlog, as raw_rcv() queues the frames directly to the socket.
Replace lock_sock() with the mutex conf_lock in struct raw_sock. The
generic socket options do not take this mutex, so they can not block
raw_notify() anymore. No user space access happens while the mutex is
held. The memory allocations for the CAN receivers in can_rx_register()
are still done under the mutex, as they have been under lock_sock()
before.
Sashiko suggested to hold not only the filter copy in raw_getsockopt()
under the new conf_lock mutex but to also protect ro->raw_vcid_opts.
This change will be addressed in a follow-up patch as it goes beyond a
simple lock replacement intended for stable.
Fixes: c18ce101f2e4 ("[CAN]: Add raw protocol")
Cc: stable@vger.kernel.org # depends on "can: raw, isotp: copy socket options outside of lock_sock()"
Tested-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Link: https://patch.msgid.link/20261120-can-bcmlock-memcg-v7-3-f6b84f5257b1@hartkopp.net
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
net/can/raw.c | 41 ++++++++++++++++++++++-------------------
1 file changed, 22 insertions(+), 19 deletions(-)
diff --git a/net/can/raw.c b/net/can/raw.c
index f14270b56f52..b69eaadc5e93 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -103,6 +103,7 @@ struct raw_sock {
struct can_filter dfilter; /* default/single filter */
struct can_filter *filter; /* pointer to filter(s) */
struct uniqframe __percpu *uniq;
+ struct mutex conf_lock; /* protects bind, filters and setsockopt */
};
static LIST_HEAD(raw_notifier_list);
@@ -319,7 +320,7 @@ static void raw_notify(struct raw_sock *ro, unsigned long msg,
/* Lockless fast path, ro->bind_dev covers a concurrent bind().
* Read it before ro->dev, pairs with smp_store_release() in
- * raw_bind(). A stale ro->bind_dev only costs a lock_sock().
+ * raw_bind(). A stale ro->bind_dev only costs a mutex_lock().
*/
bind_dev = smp_load_acquire(&ro->bind_dev);
if (READ_ONCE(ro->dev) != dev &&
@@ -328,9 +329,9 @@ static void raw_notify(struct raw_sock *ro, unsigned long msg,
switch (msg) {
case NETDEV_UNREGISTER:
- lock_sock(sk);
+ mutex_lock(&ro->conf_lock);
if (ro->dev != dev) {
- release_sock(sk);
+ mutex_unlock(&ro->conf_lock);
break;
}
@@ -351,7 +352,7 @@ static void raw_notify(struct raw_sock *ro, unsigned long msg,
sk->sk_err = ENODEV;
if (!sock_flag(sk, SOCK_DEAD))
sk_error_report(sk);
- release_sock(sk);
+ mutex_unlock(&ro->conf_lock);
break;
case NETDEV_DOWN:
@@ -401,6 +402,7 @@ static int raw_init(struct sock *sk)
ro->ifindex = 0;
ro->dev = NULL;
ro->bind_dev = NULL;
+ mutex_init(&ro->conf_lock);
/* set default filter to single entry dfilter */
ro->dfilter.can_id = 0;
@@ -451,7 +453,7 @@ static int raw_release(struct socket *sock)
list_del(&ro->notifier);
spin_unlock(&raw_notifier_lock);
- lock_sock(sk);
+ mutex_lock(&ro->conf_lock);
/* remove current filters & unregister
* the tracked reference ro->dev keeps the device's receive lists
@@ -474,11 +476,11 @@ static int raw_release(struct socket *sock)
ro->dev = NULL;
ro->count = 0;
+ mutex_unlock(&ro->conf_lock);
+
sock_orphan(sk);
sock->sk = NULL;
- release_sock(sk);
-
sock_prot_inuse_add(net, sk->sk_prot, -1);
sock_put(sk);
@@ -500,8 +502,8 @@ static int raw_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int len
if (addr->can_family != AF_CAN)
return -EINVAL;
- /* lock_sock() serializes the binding with raw_notify() */
- lock_sock(sk);
+ /* ro->conf_lock serializes the binding with raw_notify() */
+ mutex_lock(&ro->conf_lock);
if (ro->bound && addr->can_ifindex == ro->ifindex)
goto out;
@@ -581,7 +583,7 @@ static int raw_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int len
sk_error_report(sk);
}
- release_sock(sk);
+ mutex_unlock(&ro->conf_lock);
return err;
}
@@ -621,7 +623,7 @@ static int raw_setsockopt_locked(struct socket *sock, int optname,
*
* The reg_state check is only a shortcut: On a concurrent
* unregistration or namespace change raw_notify() waits for
- * lock_sock() and removes the filters set here.
+ * ro->conf_lock and removes the filters set here.
*/
dev = ro->dev;
if (ro->bound && dev &&
@@ -730,6 +732,7 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
sockptr_t optval, unsigned int optlen)
{
struct sock *sk = sock->sk;
+ struct raw_sock *ro = raw_sk(sk);
struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
struct can_filter sfilter; /* single filter */
union {
@@ -744,9 +747,9 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
if (level != SOL_CAN_RAW)
return -EINVAL;
- /* Copy the option values from user space before lock_sock(), so a
- * stalled copy can not block raw_notify() waiting for lock_sock()
- * under RTNL.
+ /* Copy the option values from user space before taking ro->conf_lock,
+ * so a stalled copy can not block raw_notify() waiting for
+ * ro->conf_lock under RTNL.
*/
switch (optname) {
case CAN_RAW_FILTER:
@@ -798,11 +801,11 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
return -EFAULT;
}
- lock_sock(sk);
+ mutex_lock(&ro->conf_lock);
err = raw_setsockopt_locked(sock, optname, &val, filter, count);
- release_sock(sk);
+ mutex_unlock(&ro->conf_lock);
/* on error free the filters allocated for CAN_RAW_FILTER above */
if (err && count > 1)
@@ -831,7 +834,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
struct can_filter *filter = NULL;
int err = 0;
- lock_sock(sk);
+ mutex_lock(&ro->conf_lock);
if (ro->count > 0) {
int fsize = ro->count * sizeof(struct can_filter);
@@ -843,7 +846,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
} else {
if (len > fsize)
len = fsize;
- /* copy to user space after release_sock() */
+ /* copy to user space after mutex_unlock() */
filter = kmemdup(ro->filter, len, GFP_KERNEL);
if (!filter)
err = -ENOMEM;
@@ -851,7 +854,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
} else {
len = 0;
}
- release_sock(sk);
+ mutex_unlock(&ro->conf_lock);
if (filter) {
if (copy_to_iter(filter, len, &opt->iter_out) != len)
--
2.53.0
Technical Details
Message-ID:
<20261124085757.1093632-4-mkl@pengutronix.de>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370136-linux-kernel-netdev=dewy.ruReferences:
<20261124085757.1093632-1-mkl@pengutronix.de>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370136-linux-kernel-netdev=dewy.ru From: mkl@pengutronix.de (Marc Kleine-Budde) Newsgroups: linux.kernel.netdev Subject: [PATCH net 03/12] can: raw: use a private mutex instead of lock_sock() Date: Tue, 24 Nov 2026 09:51:30 +0100 Organization: Usenet Network Message-ID: <20261124085757.1093632-4-mkl@pengutronix.de> References: <20261124085757.1093632-1-mkl@pengutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="4094192"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:PhdhhxyzJknKrc3D8idZwSJgs9o= sha256:0CNT/8a37QwF7mmSBRfhI3xOJTWnlpZHprLdsdQIYwY= sha1:BFgda8YD0y/hS+ff01wEreY3uNk= sha256:c3V0j9TsxdYt2oJDNYk8fSu1MmPyjXrvtKx1hSlEDeI= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 87FDA125DE8; Tue, 24 Nov 2026 11:58:56 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.05 X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=pengutronix.de X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id x4jf4btefCYh; Tue, 24 Nov 2026 11:58:52 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-370136-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=pengutronix.de X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 5FD78125DE5; Tue, 24 Nov 2026 11:58:44 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 4CBB23F75B; Tue, 24 Nov 2026 08:58:14 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795510690; b=FyTx7Qltoa6Yp1fyCuU/qgcTOH6zcAijX+wGfhgygjdDfYWBs/uue4u43oGAZ97ekhEA c60QTk/NqxjH6P73crrkSeTj+7j5il+Z+b+StJn0e8qxQLGYVLlF9BSqs2D6Tzen+RLrP x61MV7ZIWvBPdV3BWMYT007srBrU519UdpFf7SmEvBBdpJQc0E7Isl57/izrXpuqrTDLt L7a94lH6BLvZ3z37/6JyxYATD+FLl8+ZUZPTVVyMn1A1uL01uSKXpNRZMITAt/PVIaOIW M3oiBukKN28hUE6WkiRPT9PFGSTf3UEA8fWSTaxxxcdcMX/vcq/lHd6mwGxvlbgLN7Q== X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795510690; h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:From:To: Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding; bh=CGS/rYgelltar460Hw1Px5oFFl0cLQ7MKv1pVz/e5To=; b=o6rFgZo1+U5FsIQuoDDL49RpHw7Dh/z2H2w7fb72VOaO8UGaMtKk4OCu0klWrvqbut5k gJFNnK8q0o64ndQTwSh0vfEHmazDeWcFijWzVSjFhebdZ6yYyirE7536Oid+bybEuLZ8D iigTIRgap6zuCktYq+2jL31ZlexgZUi2mjIooAyuuE4xu5fgI8EUBTFbUau+orGexphO5 UAxJ+/W6VzwhYXI7G2CxmPPNJLk7nM1LLUmwqB5T/4svhcHQNoY8eq0jyhuUVAE1RA2tt f19lxwH3oaZPvxCpLsato2cuE/qm/d+wD8O1wVFxyCCaAqw2aDy2+WYBBALQlzEoClA== X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=CAD9VTX7; dmarc=none header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D377960A2605; Tue, 24 Nov 2026 08:58:10 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=CAD9VTX7 X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795510690; b=tQO994NOlcvOan54lxpZXl+olgGyASib5ITxRqY3g57++7cmjb97/LrpSApeiTeihkXZ Au4FH5ZboHWntFsM8avUjzaZepiL9hDIPFzJ9kmxHxpxxgJpN2NZOMEV4Ga8h+rT5Qbos u59wBIDHK+nF77AG0vEl8YGess/yRkm1nWFFUifE6NNlUS0WeLj+WQKyH6INXHyZSLjTm PD2U+oCflGGa0mW+hXuJ8x/Cpc0sJc3Np0BDULb9GmZAMZ1RfGI5yNa7Habh8xtx0pZlu IhEzDalXrRYq+DTHEbJauq3sz072WB2Oat73paVUfsP2VifGZgT7kdKAcLNUGaWxh5A== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795510690; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=CGS/rYgelltar460Hw1Px5oFFl0cLQ7MKv1pVz/e5To=; b=BfSjT9pnz4qVF8y/JU5ebS07QynmUDL8cwc7RvfpMCWhLx12t2hkIyOvYIpMzYDPP/QI vder0fgQADAd4EbtCMz4LHMIJOhBW+sJX+suFi0Wr22g8F0oFur2Lglnd47jpInJKymcN JFl9d5fGe71uDWLawKBrC+dX0ecKnA6cxKq27rj/ixrkZKJbmbvKR8dTCFy3IwfO/Cd34 1rEJjNUIUaWt1gsT64uotDxTD7uKSnvOjta3aIh+hwdtMlBhmsXMFDyaaHIdOyM0rk33M FijGvM38K0ow0zdcAnGH8aebiP5ajdJkh4b4x31rDfaUw+GVCIYN/H5A55bGfXozQQg== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=CAD9VTX7; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13 X-Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B53E606A696; Tue, 24 Nov 2026 08:58:07 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4B53E606A696 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de X-Received: from pengutronix.de (p20030102af17cb00d2e697ee776c6466.dip0.t-ipconnect.de [IPv6:2003:102:af17:cb00:d2e6:97ee:776c:6466]) (Authenticated sender: mkl@pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 7A941205A21; Tue, 24 Nov 2026 09:58:05 +0100 (CET) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1795510685; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CGS/rYgelltar460Hw1Px5oFFl0cLQ7MKv1pVz/e5To=; b=CAD9VTX7wTNwGCUbjMSgGvlV2HL4KlzmkyiGcJyynQakHpw1Oi5uaJyMFyOLjpoBlKW3fj xlHEYkVO8tUprFthP7gF7kFPG9KUd5FsSZwot0eEnD534TyA8oFYt0/D+hqcuGyVoN+P7c d7M6hGwpX5VYgOA75QqIrPG7COpYObHDpWikeMFUr0oeHCmsSnR7NYdHTKhNwmeyJmWR4c iLOtO1z98vjCNK/lH0QVeztQTfuZ6/aAB3xR7O3Lu/Fpnyord5NV+nICi504OMjlSu66Hj FhmWjjFfMkm/fMS6IN34ZoLFewk9HPfrg7Vko/Hkc1XSE/1qfA4w5N9mbCIpcw== X-ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1795510685; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CGS/rYgelltar460Hw1Px5oFFl0cLQ7MKv1pVz/e5To=; b=g4hbPMkkepBs9c/E4WcqY1gPPvjHR+BYlE5EiELJ8xw8NgeDRnVAkhCn3kNQCwQNlTKheX YN4Hofcq4eeJ3LHB4yYiCbLty+yHzvba42cdQZ7bRm8ro+trrur/ZG+8/elsZW9VD4QbEZ oDf4btXSZ0BPZZIYOtb4/krV7eo1jOyz+ImGZw0bZs5gkSjqLgengs4DykwS50/7BluD73 lIt7F7y1rrKm1/EwiGZKM5R+9U17T/HDZh8vT1o3OnUJ9zI9w2sNgVng8FtjqElc1ENiw4 r2gYXZ16KWy+INwKpGUvNmx8/ySNDnFo9B9A0y6bB4DSpPXKTmY+nCdn3kaW3A== X-ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=mkl@pengutronix.de smtp.mailfrom=mkl@pengutronix.de X-ARC-Seal: i=1; s=20260414; d=pengutronix.de; t=1795510685; a=rsa-sha256; cv=none; b=bTCseoJTBc9ExQ6RvfJBPo+UW48YGg8c2+DYvSvYYN8AAT7zQkZP1LR++EtFwY1z1ljf0z 582kch7HFqcLy0BLO4Sw3El+AHKOfhD1ht/E3i5QlT5p+mpjppIo7AOwrTKnfIJTRAAN1Y DzKpiGYCYQkyu1vHPP3e1tHl4q/C0lSbbc/2sE3R8FVLDHlbdraBcX/XpHTQir4tWYdQ6f IE1KViG9+OEodLTz/czx7IHe34JQ2OhL340yUwza1c+bsmuQWKpwc0bV5IjGwqgnuD9lRU vUeqvKNVXlTm8EcuHzgYNAGLVyP9nvlRU+vKyEnyKyHJ24xSX4zr7f+wokGHtw== X-Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org, kernel@pengutronix.de, Oliver Hartkopp <socketcan@hartkopp.net>, stable@vger.kernel.org, Marc Kleine-Budde <mkl@pengutronix.de> X-Mailer: git-send-email 2.53.0 X-In-Reply-To: <20261124085757.1093632-1-mkl@pengutronix.de> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list