From: Oliver Hartkopp isotp_notify() takes lock_sock() of a socket that is 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_ISOTP 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. CAN_ISOTP only uses lock_sock() to serialize the binding and the CAN_ISOTP socket options. It does not use the socket backlog, as the received PDUs are queued directly to the socket. Replace lock_sock() with the mutex conf_lock in struct isotp_sock. The generic socket options do not take this mutex, so they can not block isotp_notify() anymore. No user space access happens while the mutex is held. The memory allocations for the CAN receivers in isotp_bind() are still done under the mutex, as they have been under lock_sock() before. isotp_release() drops the mutex before synchronize_rcu() and the hrtimer cancellation, as the socket has already left the notifier list at this point. Following a Sashiko feedback for raw.c it would make sense to copy the content in isotp_getsockopt() under the new conf_lock mutex too. This change will be addressed in a follow-up patch as it goes beyond a simple lock replacement intended for stable. Fixes: e057dd3fc20f ("can: add ISO 15765-2:2016 transport protocol") Cc: stable@vger.kernel.org # depends on "can: raw, isotp: copy socket options outside of lock_sock()" Tested-by: Oliver Hartkopp Signed-off-by: Oliver Hartkopp Link: https://patch.msgid.link/20261120-can-bcmlock-memcg-v7-4-f6b84f5257b1@hartkopp.net Signed-off-by: Marc Kleine-Budde --- net/can/isotp.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/net/can/isotp.c b/net/can/isotp.c index 7a90ba3f1cf1..8229b6a69ecd 100644 --- a/net/can/isotp.c +++ b/net/can/isotp.c @@ -182,6 +182,7 @@ struct isotp_sock { struct list_head notifier; wait_queue_head_t wait; spinlock_t rx_lock; /* protect single thread state machine */ + struct mutex conf_lock; /* protects bind and setsockopt */ }; static LIST_HEAD(isotp_notifier_list); @@ -1497,7 +1498,7 @@ static int isotp_release(struct socket *sock) list_del(&so->notifier); spin_unlock(&isotp_notifier_lock); - lock_sock(sk); + mutex_lock(&so->conf_lock); /* remove current filters & unregister * the tracked reference so->dev keeps the device's receive lists @@ -1519,6 +1520,8 @@ static int isotp_release(struct socket *sock) WRITE_ONCE(so->bound, 0); so->dev = NULL; + mutex_unlock(&so->conf_lock); + /* Always wait for a grace period before touching the timers below. * A concurrent NETDEV_UNREGISTER may have already unregistered our * filters and cleared so->bound in isotp_notify() without waiting @@ -1535,7 +1538,6 @@ static int isotp_release(struct socket *sock) sock_orphan(sk); sock->sk = NULL; - release_sock(sk); sock_prot_inuse_add(net, sk->sk_prot, -1); sock_put(sk); @@ -1585,8 +1587,8 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l if (!addr->can_ifindex) return -ENODEV; - /* lock_sock() serializes the binding with isotp_notify() */ - lock_sock(sk); + /* so->conf_lock serializes the binding with isotp_notify() */ + mutex_lock(&so->conf_lock); if (so->bound) { err = -EINVAL; @@ -1596,7 +1598,7 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l /* A transmission or reception that outlived a previous binding * (unbound by NETDEV_UNREGISTER) may still be draining; the FC/echo * and RX watchdog timers bound how long this takes. Checked together - * with so->bound in the same lock_sock() section above, so there is + * with so->bound in the same so->conf_lock section above, so there is * no window in which a concurrent isotp_notify() could be missed. */ if (READ_ONCE(so->tx.state) != ISOTP_IDLE || @@ -1686,7 +1688,7 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l sk_error_report(sk); } - release_sock(sk); + mutex_unlock(&so->conf_lock); return err; } @@ -1790,6 +1792,7 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname, { struct sock *sk = sock->sk; + struct isotp_sock *so = isotp_sk(sk); union { struct can_isotp_options opt; struct can_isotp_fc_options rxfc; @@ -1802,7 +1805,7 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname, if (level != SOL_CAN_ISOTP) return -EINVAL; - /* copy the option values from user space before lock_sock() */ + /* copy the option values from user space before taking so->conf_lock */ switch (optname) { case CAN_ISOTP_OPTS: size = sizeof(val.opt); @@ -1831,9 +1834,9 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname, if (copy_from_sockptr(&val, optval, size)) return -EFAULT; - lock_sock(sk); + mutex_lock(&so->conf_lock); ret = isotp_setsockopt_locked(sock, optname, &val); - release_sock(sk); + mutex_unlock(&so->conf_lock); return ret; } @@ -1906,9 +1909,9 @@ static void isotp_notify(struct isotp_sock *so, unsigned long msg, switch (msg) { case NETDEV_UNREGISTER: - lock_sock(sk); + mutex_lock(&so->conf_lock); if (so->dev != dev) { - release_sock(sk); + mutex_unlock(&so->conf_lock); break; } @@ -1932,7 +1935,7 @@ static void isotp_notify(struct isotp_sock *so, unsigned long msg, sk->sk_err = ENODEV; if (!sock_flag(sk, SOCK_DEAD)) sk_error_report(sk); - release_sock(sk); + mutex_unlock(&so->conf_lock); break; case NETDEV_DOWN: @@ -1991,6 +1994,7 @@ static int isotp_init(struct sock *sk) so->ifindex = 0; so->bound = 0; so->dev = NULL; + mutex_init(&so->conf_lock); so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS; so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS; -- 2.53.0