net: can: isotp-fixes
Subsystems (2)
Oliver Hartkopp
maintainerv4 cover net: can: isotp-fixes
As sashiko-bot was not able to check the second patch this bundle is re-posted with b4 preparation.
It covers two fixes reported by Nico Yip and sashiko-bot.
---
Changes in v4:
- patch 2: use a simpler method to reject a re-binding that can be implemented inside the lock: No waitqueue waiting just exit.
- Link to v3: https://patch.msgid.link/[email protected]
Changes in v3:
- patch 2: go back to initial idea without hard resetting the state machine and timers but let these come to a graceful end. Even a new bind() at netdev shutdown now leads to this graceful statemachine shutdown via the wait queue.
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- patch 2: shutdown state machine to fix sashiko-bot complains
- Link to v1: https://patch.msgid.link/[email protected]
---
Oliver Hartkopp (2):
can: isotp: use unconditional synchronize_rcu() in isotp_release()
can: isotp: fix use-after-free race with concurrent NETDEV_UNREGISTER
net/can/isotp.c | 96 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 29 deletions(-)
---
base-commit: a635d6748234582ea287c5ffeae28b9b23f91c7e
change-id: 20260710-isotp-fixes-ead7480d67bf
Best regards,
Signature · 1 line
Oliver Hartkopp <[email protected]>
Oliver Hartkopp
maintainerPATCH v4 1/2 can: isotp: use unconditional synchronize_rcu() in isotp_release()
isotp_notify() unregisters the (RCU) CAN filters via can_rx_unregister() and clears so->bound without waiting for a grace period. isotp_release() uses so->bound to decide whether it needs to call synchronize_rcu() before cancelling so->rxtimer, so when NETDEV_UNREGISTER runs first it skips that synchronize_rcu() and can cancel the timer while an in-flight isotp_rcv() is still executing and about to re-arm it via isotp_send_fc(), leading to a use-after-free timer callback on the freed socket.
sakisho-bot remarked a problem with rtnl_lock held in isotp_notify(), therefore make isotp_release() always call synchronize_rcu() before cancelling the timers, regardless of so->bound. This still closes the original race (isotp_notify() clearing so->bound without waiting for in-flight isotp_rcv() callers before isotp_release() cancels the RX timer) without adding any RCU wait to the netdevice notifier path.
--- net/can/isotp.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
net/can/isotp.c +8 -1
@@ -1235,15 +1235,22 @@ static int isotp_release(struct socket *sock)can_rx_unregister (net, dev, so->txid,SINGLE_MASK (so->txid), isotp_rcv_echo, sk);dev_put (dev); -synchronize_rcu (); } } } +/* 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 +* for in-flight isotp_rcv() callers to finish, so this call must not +* be skipped just because so->bound is already 0 here. +*/ +synchronize_rcu (); +hrtimer_cancel (&so->txfrtimer);hrtimer_cancel (&so->txtimer);hrtimer_cancel (&so->rxtimer); so->ifindex =0 ;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v4 1/2] can: isotp: use unconditional synchronize_rcu() in isotp_release()
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Race condition during network device unregistration causes isotp_release() to skip CAN filter unregistration, leading to a memory leak and a Use-After-Free.
- [High] Data race on so->rx.state and so->tx.state between hrtimer callbacks and socket operations.
--
--- Patch [1]: [PATCH v4 1/2] can: isotp: use unconditional synchronize_rcu() in isotp_release() --- commit 6feb0d6dc5d6600aa28544bf14c75b5f268d9b41
Author: Oliver Hartkopp <[email protected]>
can: isotp: use unconditional synchronize_rcu() in isotp_release()
isotp_notify() unregisters the (RCU) CAN filters via can_rx_unregister() and clears so->bound without waiting for a grace period. isotp_release() uses so->bound to decide whether it needs to call synchronize_rcu() before cancelling so->rxtimer. When NETDEV_UNREGISTER runs first, it skips that synchronize_rcu() and can cancel the timer while an in-flight isotp_rcv() is executing, leading to a use-after-free timer callback.
net/can/isotp.c static int isotp_release(struct socket *sock) · line 1242
Quoted 12 lines
net/can/isotp.c +0
-1@@ -1237,11 +1237,18 @@ static int isotp_release(struct socket *sock)SINGLE_MASK (so->txid), isotp_rcv_echo, sk);dev_put (dev); -synchronize_rcu (); } } }
[Severity: High]
This isn't a bug introduced by this patch, but could this cause a memory leak and a Use-After-Free during concurrent network device unregistration?
In isotp_release(), the socket is prematurely removed from the notifier list:
list_del(&so->notifier);
If the device is concurrently being unregistered, this subsequent lookup fails:
dev = dev_get_by_index(net, so->ifindex);
if (dev) {This failure causes the critical can_rx_unregister() steps to be skipped.
Since the socket is no longer on the notifier list, the subsequent NETDEV_UNREGISTER broadcast will not call isotp_notify() to clean up the filters either.
The socket memory is subsequently freed, leaving the stale filter in the device's receiver list, which retains a dangling pointer to the socket, leading to a Use-After-Free when a delayed CAN frame is processed.
[Severity: High]
This is a pre-existing issue, but does this non-atomic assignment race with concurrent protocol state machine transitions?
In isotp_rx_timer_handler():
so->rx.state = ISOTP_IDLE;
This modifies the RX state without holding so->rx_lock, racing with concurrent CAN frame reception in sendmsg and softirq RX handlers.
For example, isotp_rcv() acquires the lock and begins processing a frame. If isotp_rcv_cf() observes ISOTP_WAIT_DATA, it proceeds to cancel the timer. hrtimer_cancel() blocks until the racing timer callback finishes. Once the timer finishes, it has already changed the state to ISOTP_IDLE and signaled an ETIMEDOUT error. However, isotp_rcv_cf() then resumes and processes the frame into the buffer as if the state were still valid, hopelessly corrupting the sequence.
A similar data race occurs on so->tx.state in isotp_tx_timer_handler():
so->tx.state = ISOTP_IDLE;
This uses plain unprotected writes against concurrent atomic operations (cmpxchg) and RX softirq modifications, leading to unexpected packet drops, infinite wait stalls, and protocol failure.
Quoted 11 lines
net/can/isotp.c +8
-0+/* 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 +* for in-flight isotp_rcv() callers to finish, so this call must not +* be skipped just because so->bound is already 0 here. +*/ +synchronize_rcu (); +hrtimer_cancel (&so->txfrtimer);hrtimer_cancel (&so->txtimer);hrtimer_cancel (&so->rxtimer);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Oliver Hartkopp
maintainerPATCH v4 2/2 can: isotp: fix use-after-free race with concurrent NETDEV_UNREGISTER
In reply to Oliver Hartkoppisotp_release() looked up the bound network device via dev_get_by_index() using the stored ifindex. During device unregistration the device is unlisted from the ifindex hash before the NETDEV_UNREGISTER notifier chain runs, so a concurrent isotp_release() could find no device, skip can_rx_unregister() entirely, and still proceed to free the socket. Since isotp_release() had already removed itself from the isotp notifier list at that point, isotp_notify() would never get a chance to clean up either, leaving a stale CAN filter that keeps pointing at the freed socket.
Fix this the same way raw.c already does: hold a tracked reference to the bound net_device in the socket (so->dev/so->dev_tracker) from bind() onward instead of re-resolving it from the ifindex, and serialize bind()/release() with rtnl_lock() so that so->dev is always consistent with what the NETDEV_UNREGISTER notifier sees. so->dev stays valid regardless of ifindex-hash unlisting, and is only ever cleared by whichever of isotp_release()/isotp_notify() gets there first, so the filter is always removed exactly once.
isotp_bind() now rejects a (re)bind with -EAGAIN while so->tx.state isn't ISOTP_IDLE yet, so a timer left running by a prior NETDEV_UNREGISTER can't act on a newly bound so->ifindex. Both checks share the same lock_sock() section, so there is no window in which a concurrent isotp_notify() clearing so->bound could be missed.
--- net/can/isotp.c | 87 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 28 deletions(-)
net/can/isotp.c +59 -28
@@ -150,10 +150,12 @@ struct tpcon {struct isotp_sock {struct sock sk;int bound;int ifindex; +struct net_device *dev; +netdevice_tracker dev_tracker;canid_t txid;canid_t rxid;ktime_t tx_gap;ktime_t lastrxcf_tstamp;struct hrtimer rxtimer, txtimer, txfrtimer;
@@ -976,10 +978,18 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) err =wait_event_interruptible (so->wait, so->tx.state ==ISOTP_IDLE );if (err) goto err_event_drop; } +/* so->bound is only checked once above - a wakeup may have +* unbound/rebound the socket meanwhile, so re-validate it +*/ +if (!so->bound) { + err = -EADDRNOTAVAIL ; + goto err_out_drop; + } +/* PDU size > default => try max_pdu_size */ if (size > so->tx.buflen && so->tx.buflen < max_pdu_size) {u8 *newbuf =kmalloc (max_pdu_size,GFP_KERNEL );if (newbuf) {
@@ -1217,32 +1227,34 @@ static int isotp_release(struct socket *sock)spin_lock (&isotp_notifier_lock); }list_del (&so->notifier);spin_unlock (&isotp_notifier_lock); +rtnl_lock ();lock_sock (sk); -/* remove current filters & unregister */ -if (so->bound) { -if (so->ifindex) { -struct net_device *dev; - - dev =dev_get_by_index (net, so->ifindex); -if (dev) { -if (isotp_register_rxid (so)) -can_rx_unregister (net, dev, so->rxid, -SINGLE_MASK (so->rxid), - isotp_rcv, sk); - -can_rx_unregister (net, dev, so->txid, -SINGLE_MASK (so->txid), - isotp_rcv_echo, sk); -dev_put (dev); - } - } +/* remove current filters & unregister +* tracked reference so->dev is taken at bind() time with rtnl_lock +*/ +if (so->bound && so->dev) { +if (isotp_register_rxid (so)) +can_rx_unregister (net, so->dev, so->rxid, +SINGLE_MASK (so->rxid), + isotp_rcv, sk); + +can_rx_unregister (net, so->dev, so->txid, +SINGLE_MASK (so->txid), + isotp_rcv_echo, sk); +netdev_put (so->dev, &so->dev_tracker); } + so->ifindex =0 ; + so->bound =0 ; + so->dev =NULL ; + +rtnl_unlock (); +/* 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 * for in-flight isotp_rcv() callers to finish, so this call must not * be skipped just because so->bound is already 0 here.
@@ -1251,13 +1263,10 @@ static int isotp_release(struct socket *sock)hrtimer_cancel (&so->txfrtimer);hrtimer_cancel (&so->txtimer);hrtimer_cancel (&so->rxtimer); - so->ifindex =0 ; - so->bound =0 ; -sock_orphan (sk); sock->sk =NULL ;release_sock (sk);sock_prot_inuse_add (net, sk->sk_prot,-1 );
@@ -1308,17 +1317,29 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l }if (!addr->can_ifindex)return -ENODEV ; +rtnl_lock ();lock_sock (sk);if (so->bound) { err = -EINVAL ; goto out; } +/* A transmission that outlived a previous binding (unbound by +* NETDEV_UNREGISTER) may still be draining; the FC/echo watchdog +* timers bound how long this takes. Checked together with +* so->bound in the same lock_sock() section above, so there is no +* window in which a concurrent isotp_notify() could be missed. +*/ +if (so->tx.state !=ISOTP_IDLE ) { + err = -EAGAIN ; + goto out; + } +/* ensure different CAN IDs when the rx_id is to be registered */ if (isotp_register_rxid (so) && rx_id == tx_id) { err = -EADDRNOTAVAIL ; goto out; }
@@ -1327,18 +1348,16 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int lif (!dev) { err = -ENODEV ; goto out; }if (dev->type !=ARPHRD_CAN ) { -dev_put (dev); err = -ENODEV ; - goto out; + goto out_put_dev; }if (READ_ONCE (dev->mtu) < so->ll.mtu) { -dev_put (dev); err = -EINVAL ; - goto out; + goto out_put_dev; }if (!(dev->flags &IFF_UP )) notify_enetdown =1 ; ifindex = dev->ifindex;
@@ -1352,20 +1371,29 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l/* register for echo skb's */ can_rx_register (net, dev, tx_id,SINGLE_MASK (tx_id), isotp_rcv_echo, sk,"isotpe" , sk); -dev_put (dev); -/* switch to new settings */ so->ifindex = ifindex; so->rxid = rx_id; so->txid = tx_id; so->bound =1 ; +/* bind() ok -> hold a reference for so->dev so that isotp_release() +* can safely reach the device later, even if a concurrent +* NETDEV_UNREGISTER has already unlisted it by ifindex. +*/ + so->dev = dev; +netdev_hold (so->dev, &so->dev_tracker,GFP_KERNEL ); + +out_put_dev: +/* remove potential reference from dev_get_by_index() */ +dev_put (dev); out:release_sock (sk); +rtnl_unlock ();if (notify_enetdown) { sk->sk_err =ENETDOWN ;if (!sock_flag (sk,SOCK_DEAD ))sk_error_report (sk);
@@ -1564,11 +1592,11 @@ static void isotp_notify(struct isotp_sock *so, unsigned long msg,struct sock *sk = &so->sk;if (!net_eq (dev_net (dev),sock_net (sk)))return ; -if (so->ifindex != dev->ifindex) +if (so->dev != dev)return ;switch (msg) {case NETDEV_UNREGISTER :lock_sock (sk);
@@ -1580,14 +1608,16 @@ static void isotp_notify(struct isotp_sock *so, unsigned long msg, isotp_rcv, sk);can_rx_unregister (dev_net (dev), dev, so->txid,SINGLE_MASK (so->txid), isotp_rcv_echo, sk); +netdev_put (so->dev, &so->dev_tracker); } so->ifindex =0 ; so->bound =0 ; + so->dev =NULL ;release_sock (sk); sk->sk_err =ENODEV ;if (!sock_flag (sk,SOCK_DEAD ))sk_error_report (sk);
@@ -1643,10 +1673,11 @@ static int isotp_init(struct sock *sk) {struct isotp_sock *so =isotp_sk (sk); so->ifindex =0 ; so->bound =0 ; + so->dev =NULL ; so->opt.flags =CAN_ISOTP_DEFAULT_FLAGS ; so->opt.ext_address =CAN_ISOTP_DEFAULT_EXT_ADDRESS ; so->opt.rx_ext_address =CAN_ISOTP_DEFAULT_EXT_ADDRESS ; so->opt.rxpad_content =CAN_ISOTP_DEFAULT_PAD_CONTENT ;
Signature · 1 line
2.53.0