From: Oliver Hartkopp bcm_notify() takes lock_sock() of every CAN_BCM socket in the namespace of a vanishing CAN device while holding RTNL. Any path that holds the socket lock while accessing user space can therefore block RTNL. With "can: bcm: copy the CAN frames outside of lock_sock()" the CAN frames of bcm_sendmsg() are copied 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. CAN_BCM only uses lock_sock() to serialize the binding and the bcm_op lists. It does not use the socket backlog, as the BCM messages are queued directly to the socket. Replace lock_sock() with the mutex conf_lock in struct bcm_sock. The generic socket options do not take this mutex, so they can not block bcm_notify() anymore. No user space access happens while the mutex is held. The memory allocations for the bcm_ops and the CAN receivers are still done under the mutex, as they have been under lock_sock() before. bcm_release() removes the procfs entry before taking the mutex, as remove_proc_entry() waits for readers that may stall in their copy to user space. The socket has already left the notifier list at this point. Fixes: ffd980f976e7 ("[CAN]: Add broadcast manager (bcm) protocol") Cc: stable@vger.kernel.org # depends on "can: bcm: copy the CAN frames outside of lock_sock()" Tested-by: Oliver Hartkopp Signed-off-by: Oliver Hartkopp Link: https://patch.msgid.link/20261117-can-bcmlock-memcg-v6-5-6826a6227d36@hartkopp.net Cc: Vincent Mailhol Cc: Marc Kleine-Budde Cc: Oleksij Rempel Cc: Zhu Ling Cc: linux-can@vger.kernel.org Signed-off-by: Marc Kleine-Budde --- net/can/bcm.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/net/can/bcm.c b/net/can/bcm.c index 1595b7720c98..dc9e2890490a 100644 --- a/net/can/bcm.c +++ b/net/can/bcm.c @@ -145,6 +145,7 @@ struct bcm_sock { unsigned long dropped_usr_msgs; struct proc_dir_entry *bcm_proc_read; char procname [32]; /* inode number in decimal with \0 */ + struct mutex conf_lock; /* protects bind and the bcm_op lists */ }; static LIST_HEAD(bcm_notifier_list); @@ -1673,8 +1674,8 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) if ((size - MHSIZ) % cfsiz) return -EINVAL; - /* Copy the CAN frames from user space before lock_sock(), so a - * stalled copy can not block bcm_notify() waiting for lock_sock() + /* Copy the CAN frames from user space before taking bo->conf_lock, so a + * stalled copy can not block bcm_notify() waiting for bo->conf_lock * under RTNL. Only the frames the opcode handler reads are copied. * The handlers keep their checks and read the frames from fmsg. */ @@ -1714,12 +1715,12 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) kv.iov_len = flen; iov_iter_kvec(&fmsg.msg_iter, ITER_SOURCE, &kv, 1, flen); - lock_sock(sk); + mutex_lock(&bo->conf_lock); - /* Re-validate under the socket lock: a concurrent bcm_notify() + /* Re-validate under bo->conf_lock: a concurrent bcm_notify() * may have unbound this socket (device removal) after the * lockless fast-path check above. bo->ifindex is only ever - * mutated under lock_sock(), so reading it here - instead of + * mutated under bo->conf_lock, so reading it here - instead of * before taking the lock - guarantees it can't be observed * torn against bo->bound. */ @@ -1819,7 +1820,7 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) } out_release: - release_sock(sk); + mutex_unlock(&bo->conf_lock); out_free: if (frames != &sframe) kvfree(frames); @@ -1843,7 +1844,7 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg, switch (msg) { case NETDEV_UNREGISTER: - lock_sock(sk); + mutex_lock(&bo->conf_lock); /* rx_ops: remove device specific receive entries */ list_for_each_entry(op, &bo->rx_ops, list) { @@ -1877,7 +1878,7 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg, notify_enodev = 1; } - release_sock(sk); + mutex_unlock(&bo->conf_lock); if (notify_enodev) { sk->sk_err = ENODEV; @@ -1930,6 +1931,7 @@ static int bcm_init(struct sock *sk) bo->ifindex = 0; bo->dropped_usr_msgs = 0; bo->bcm_proc_read = NULL; + mutex_init(&bo->conf_lock); INIT_LIST_HEAD(&bo->tx_ops); INIT_LIST_HEAD(&bo->rx_ops); @@ -1969,15 +1971,14 @@ static int bcm_release(struct socket *sock) list_del(&bo->notifier); spin_unlock(&bcm_notifier_lock); - /* bcm_notify() can not access this socket anymore */ - lock_sock(sk); - #if IS_ENABLED(CONFIG_PROC_FS) /* remove procfs entry */ if (net->can.bcmproc_dir && bo->bcm_proc_read) remove_proc_entry(bo->procname, net->can.bcmproc_dir); #endif /* CONFIG_PROC_FS */ + mutex_lock(&bo->conf_lock); + list_for_each_entry_safe(op, next, &bo->tx_ops, list) { list_del_rcu(&op->list); bcm_remove_op(op); @@ -2012,10 +2013,11 @@ static int bcm_release(struct socket *sock) WRITE_ONCE(bo->ifindex, 0); } + mutex_unlock(&bo->conf_lock); + sock_orphan(sk); sock->sk = NULL; - release_sock(sk); sock_prot_inuse_add(net, sk->sk_prot, -1); sock_put(sk); @@ -2034,7 +2036,7 @@ static int bcm_connect(struct socket *sock, struct sockaddr_unsized *uaddr, int if (len < BCM_MIN_NAMELEN) return -EINVAL; - lock_sock(sk); + mutex_lock(&bo->conf_lock); if (bo->bound) { ret = -EISCONN; @@ -2086,7 +2088,7 @@ static int bcm_connect(struct socket *sock, struct sockaddr_unsized *uaddr, int WRITE_ONCE(bo->bound, 1); fail: - release_sock(sk); + mutex_unlock(&bo->conf_lock); return ret; } -- 2.53.0