Skip to content

Commit 60d8a79

Browse files
hartkoppgregkh
authored andcommitted
can: bcm: fix stale rx/tx ops after device removal
commit 3b762c0 upstream. RX: an RX_SETUP update(!) for an existing op skipped can_rx_register() unconditionally, even when a concurrent NETDEV_UNREGISTER had already torn down its registration (op->rx_reg_dev == NULL). This silently did not re-enable frame delivery for that updated filter. bcm_rx_setup() now re-registers in that case, while leaving rx_ops with ifindex = 0 (all CAN devices) which never carry a tracked rx_reg_dev registered as-is. TX: bcm_notify() only handled bo->rx_ops on NETDEV_UNREGISTER, leaving tx_ops with an active cyclic transmission re-arming its hrtimer indefinitely to execute bcm_tx_timeout_handler(). Cancelling the hrtimer prevents the runaway timer and any injection into a later reused ifindex, since nothing else calls bcm_can_tx() for the op until an explicit TX_SETUP update re-arms it. Unlike bcm_rx_unreg(), which clears the tracked rx_reg_dev for rx_ops, the ifindex is intentionally left unchanged for tx_ops. bcm_tx_setup() always rejects ifindex 0, so clearing it would strand the op: neither a later TX_SETUP (bcm_find_op()) nor TX_DELETE (bcm_delete_tx_op()) could ever find it again, since both require an exact ifindex match. Reported-by: sashiko-bot@kernel.org Closes: https://lore.kernel.org/linux-can/20260708094536.DDF821F00A3A@smtp.kernel.org/ Closes: https://lore.kernel.org/linux-can/20260708154039.347ED1F000E9@smtp.kernel.org/ Fixes: ffd980f ("[CAN]: Add broadcast manager (bcm) protocol") Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Link: https://patch.msgid.link/20260714-bcm_fixes-v15-9-562f7e3e42da@hartkopp.net Cc: stable@kernel.org Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de> Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 84aa480 commit 60d8a79

1 file changed

Lines changed: 44 additions & 10 deletions

File tree

net/can/bcm.c

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,7 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
12391239
struct bcm_sock *bo = bcm_sk(sk);
12401240
struct bcm_op *op;
12411241
int do_rx_register;
1242+
int new_op = 0;
12421243
int err = 0;
12431244

12441245
if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
@@ -1323,8 +1324,15 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
13231324
/* free temporary frames / kfree(NULL) is safe */
13241325
kfree(new_frames);
13251326

1326-
/* Only an update -> do not call can_rx_register() */
1327-
do_rx_register = 0;
1327+
/* Don't register a new CAN filter for the rx_op update unless
1328+
* a concurrent NETDEV_UNREGISTER notifier already tore down
1329+
* the previous registration. In this case the receiver needs
1330+
* to be re-registered here so that this update doesn't
1331+
* silently stop delivering frames for the given ifindex.
1332+
* Ops with ifindex = 0 (all CAN interfaces) never carry a
1333+
* tracked rx_reg_dev and stay registered as-is.
1334+
*/
1335+
do_rx_register = (ifindex && !op->rx_reg_dev) ? 1 : 0;
13281336

13291337
} else {
13301338
/* insert new BCM operation for the given can_id */
@@ -1394,6 +1402,7 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
13941402

13951403
/* call can_rx_register() */
13961404
do_rx_register = 1;
1405+
new_op = 1;
13971406

13981407
} /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
13991408

@@ -1407,7 +1416,7 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
14071416
if (op->flags & SETTIMER) {
14081417

14091418
/* set timers (locked) for newly created op */
1410-
if (do_rx_register) {
1419+
if (new_op) {
14111420
spin_lock_bh(&op->bcm_rx_update_lock);
14121421
op->ival1 = msg_head->ival1;
14131422
op->ival2 = msg_head->ival2;
@@ -1437,7 +1446,10 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
14371446
HRTIMER_MODE_REL_SOFT);
14381447
}
14391448

1440-
/* now we can register for can_ids, if we added a new bcm_op */
1449+
/* now we can register for can_ids, if we added a new bcm_op
1450+
* or need to re-register after a NETDEV_UNREGISTER tore down
1451+
* the previous registration of an existing op
1452+
*/
14411453
if (do_rx_register) {
14421454
if (ifindex) {
14431455
struct net_device *dev;
@@ -1467,18 +1479,32 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
14671479
err = -ENODEV;
14681480
}
14691481

1470-
} else
1482+
} else {
14711483
err = can_rx_register(sock_net(sk), NULL, op->can_id,
14721484
REGMASK(op->can_id),
14731485
bcm_rx_handler, op, "bcm", sk);
1486+
}
1487+
14741488
if (err) {
1475-
/* this bcm rx op is broken -> remove it */
1476-
bcm_remove_op(op);
1489+
/* newly created bcm rx op is broken -> remove it */
1490+
if (new_op) {
1491+
bcm_remove_op(op);
1492+
return err;
1493+
}
1494+
1495+
/* an existing op just stays unregistered.
1496+
* Cancel op->timer and (defensively) op->thrtimer.
1497+
* Other settings can't be reached until the next
1498+
* successful RX_SETUP.
1499+
*/
1500+
hrtimer_cancel(&op->timer);
1501+
hrtimer_cancel(&op->thrtimer);
14771502
return err;
14781503
}
14791504

1480-
/* add this bcm_op to the list of the rx_ops */
1481-
list_add_rcu(&op->list, &bo->rx_ops);
1505+
/* add a new bcm_op to the list of the rx_ops */
1506+
if (new_op)
1507+
list_add_rcu(&op->list, &bo->rx_ops);
14821508
}
14831509

14841510
return msg_head->nframes * op->cfsiz + MHSIZ;
@@ -1694,11 +1720,19 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
16941720
case NETDEV_UNREGISTER:
16951721
lock_sock(sk);
16961722

1697-
/* remove device specific receive entries */
1723+
/* rx_ops: remove device specific receive entries */
16981724
list_for_each_entry(op, &bo->rx_ops, list)
16991725
if (op->rx_reg_dev == dev)
17001726
bcm_rx_unreg(dev, op);
17011727

1728+
/* tx_ops: stop device specific cyclic transmissions on the
1729+
* vanishing ifindex. Cancelling the timer is enough to stop
1730+
* cyclic bcm_can_tx() calls as there is no re-arming.
1731+
*/
1732+
list_for_each_entry(op, &bo->tx_ops, list)
1733+
if (op->ifindex == dev->ifindex)
1734+
hrtimer_cancel(&op->timer);
1735+
17021736
/* remove device reference, if this is our bound device */
17031737
if (bo->bound && bo->ifindex == dev->ifindex) {
17041738
#if IS_ENABLED(CONFIG_PROC_FS)

0 commit comments

Comments
 (0)