Skip to content

Commit 2f5976f

Browse files
hartkoppmarckleinebudde
authored andcommitted
can: bcm: track a single source interface for ANYDEV timeout/throttle ops
An ANYDEV rx op (ifindex == 0) with an active RX timeout and/or throttle timer has no defined semantics when matching frames arrive from several interfaces: bcm_rx_handler() can run concurrently for the same op on different CPUs, racing hrtimer_cancel()/ bcm_rx_starttimer() against bcm_rx_timeout_handler() and causing spurious RX_TIMEOUT notifications and last_frames corruption. The same concurrency lets throttled multiplex frames from different interfaces clobber the single rx_ifindex/rx_stamp fields shared by the op. Add op->if_detected to track the first interface that delivers a matching frame while a timeout/throttle timer is configured, and reject frames from any other interface for that op. The claim is decided in bcm_rx_handler() before hrtimer_cancel() touches op->timer, so a rejected frame can never disturb the claimed interface's watchdog. RTR-mode ops are excluded via RX_RTR_FRAME, independent of kt_ival1/kt_ival2, since those may briefly hold a stale value from an earlier non-RTR configuration. The claim is released in bcm_notify() on NETDEV_UNREGISTER and in bcm_rx_setup() when SETTIMER reconfigures the timer values. A (re-)claim is only possible on CAN devices in NETREG_REGISTERED dev->reg_state to cover the release in bcm_notify() where reg_state becomes NETREG_UNREGISTERING until synchronize_net(). Fixes: ffd980f ("[CAN]: Add broadcast manager (bcm) protocol") Reported-by: sashiko-bot@kernel.org Closes: https://lore.kernel.org/linux-can/20260709105031.1A39C1F000E9@smtp.kernel.org/ Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Link: https://patch.msgid.link/20260714-bcm_fixes-v15-11-562f7e3e42da@hartkopp.net Cc: stable@kernel.org Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent 58fd6cb commit 2f5976f

1 file changed

Lines changed: 44 additions & 5 deletions

File tree

net/can/bcm.c

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ struct bcm_op {
117117
struct hrtimer timer, thrtimer;
118118
ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
119119
int rx_ifindex;
120+
int if_detected; /* first received ifindex in ANYDEV rx_op mode */
120121
int cfsiz;
121122
u32 count;
122123
u32 nframes;
@@ -797,6 +798,33 @@ static void bcm_rx_handler(struct sk_buff *skb, void *data)
797798
return;
798799
}
799800

801+
/* An ANYDEV op with an active RX timeout and/or throttle timer
802+
* tracks a single source interface: claim the first interface that
803+
* delivers a matching frame and reject frames from any other one,
804+
* before hrtimer_cancel() below can touch op->timer - this avoids
805+
* racing bcm_rx_timeout_handler() across concurrent interfaces.
806+
* RX_RTR_FRAME ops are excluded, as kt_ival1/kt_ival2 may briefly
807+
* hold a stale value from an earlier non-RTR configuration.
808+
*/
809+
if (!op->ifindex) {
810+
spin_lock_bh(&op->bcm_rx_update_lock);
811+
812+
if (!(op->flags & RX_RTR_FRAME) &&
813+
(op->kt_ival1 || op->kt_ival2)) {
814+
/* don't claim to vanishing interface */
815+
if (!op->if_detected &&
816+
READ_ONCE(skb->dev->reg_state) == NETREG_REGISTERED)
817+
op->if_detected = skb->dev->ifindex;
818+
819+
if (op->if_detected != skb->dev->ifindex) {
820+
spin_unlock_bh(&op->bcm_rx_update_lock);
821+
return;
822+
}
823+
}
824+
825+
spin_unlock_bh(&op->bcm_rx_update_lock);
826+
}
827+
800828
/* disable timeout */
801829
hrtimer_cancel(&op->timer);
802830

@@ -831,10 +859,9 @@ static void bcm_rx_handler(struct sk_buff *skb, void *data)
831859
traffic_flags |= RX_OWN;
832860
}
833861

834-
/* save rx timestamp and originator for recvfrom() under lock.
835-
* For an op subscribed on all interfaces (ifindex == 0)
836-
* bcm_rx_handler() can run concurrently on different CPUs so
837-
* the CAN content and the meta data must be bundled correctly.
862+
/* save rx timestamp and originator for recvfrom() under lock: an
863+
* ANYDEV op without an active timer can still run concurrently on
864+
* different CPUs, so content and meta data must be bundled here.
838865
*/
839866
op->rx_stamp = skb->tstamp;
840867
op->rx_ifindex = skb->dev->ifindex;
@@ -1369,6 +1396,7 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
13691396
op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
13701397
op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
13711398
op->kt_lastmsg = 0;
1399+
op->if_detected = 0; /* reclaim ifindex in ANYDEV mode */
13721400
}
13731401
spin_unlock_bh(&op->bcm_rx_update_lock);
13741402

@@ -1775,10 +1803,21 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
17751803
lock_sock(sk);
17761804

17771805
/* rx_ops: remove device specific receive entries */
1778-
list_for_each_entry(op, &bo->rx_ops, list)
1806+
list_for_each_entry(op, &bo->rx_ops, list) {
17791807
if (op->rx_reg_dev == dev)
17801808
bcm_rx_unreg(dev, op);
17811809

1810+
/* release an ANYDEV op's claim (see bcm_rx_handler())
1811+
* on this now confirmed-gone interface.
1812+
*/
1813+
if (!op->ifindex) {
1814+
spin_lock_bh(&op->bcm_rx_update_lock);
1815+
if (op->if_detected == dev->ifindex)
1816+
op->if_detected = 0;
1817+
spin_unlock_bh(&op->bcm_rx_update_lock);
1818+
}
1819+
}
1820+
17821821
/* tx_ops: stop device specific cyclic transmissions on the
17831822
* vanishing ifindex. Cancelling the timer is enough to stop
17841823
* cyclic bcm_can_tx() calls as there is no re-arming.

0 commit comments

Comments
 (0)