Linux CAN drivers development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Oliver Hartkopp" <socketcan@hartkopp.net>
Cc: "Oleksij Rempel" <o.rempel@pengutronix.de>,
	linux-can@vger.kernel.org,
	"Marc Kleine-Budde" <mkl@pengutronix.de>,
	"Oliver Hartkopp" <socketcan@hartkopp.net>,
	"Vincent Mailhol" <mailhol@kernel.org>
Subject: Re: [PATCH v7 09/10] can: bcm: fix stale rx/tx ops after device removal
Date: Wed, 08 Jul 2026 15:40:38 +0000	[thread overview]
Message-ID: <20260708154039.347ED1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260708-bcm_fixes-v7-9-c78896b342e4@hartkopp.net>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Setting `op->ifindex = 0` for `tx_ops` on NETDEV_UNREGISTER orphans the operations, preventing reconfiguration and causing memory leaks.
- [Medium] `bcm_rx_setup()` silently ignores registration failures for existing operations, falsely reporting success to userspace.
--

--- Patch [9]: [PATCH v7 09/10] can: bcm: fix stale rx/tx ops after device removal ---
commit 685050073b3ab1afd17b250b634cb2dfd5941250
Author: Oliver Hartkopp <socketcan@hartkopp.net>

can: bcm: fix stale rx/tx ops after device removal

This commit modifies bcm_rx_setup() to correctly re-register an RX operation 
when a concurrent NETDEV_UNREGISTER has torn down its previous registration. 
It also updates bcm_notify() to stop the TX timer and clear op->ifindex 
for affected tx_ops when a device is unregistered, preventing cyclic frames 
from being transmitted to unrelated devices.

Link: https://lore.kernel.org/linux-can/20260708094536.DDF821F00A3A@smtp.kernel.org/

> diff --git a/net/can/bcm.c b/net/can/bcm.c
> index 4bdca72f9327f..9d01931708081 100644
> --- a/net/can/bcm.c
> +++ b/net/can/bcm.c

[ ... ]

> @@ -1473,18 +1485,21 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
>  				err = -ENODEV;
>  			}
>  
> -		} else
> +		} else {
>  			err = can_rx_register(sock_net(sk), NULL, op->can_id,
>  					      REGMASK(op->can_id),
>  					      bcm_rx_handler, op, "bcm", sk);
> -		if (err) {
> +		}
> +
> +		if (err && new_op) {

[Severity: Medium]
Does adding the new_op check here silently swallow registration errors for existing
operations?

If an existing operation (new_op == 0) attempts to re-register after being unregistered,
and dev_get_by_index() or can_rx_register() fails, err will be set (e.g., -ENODEV).
Because of the && new_op condition, the error is ignored and the function will return 
success to userspace, despite the comment above indicating "do not silently succeed 
without registering".

>  			/* this bcm rx op is broken -> remove it */
>  			bcm_remove_op(op);
>  			return err;
>  		}

[ ... ]

> @@ -1704,11 +1719,25 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
>  	case NETDEV_UNREGISTER:
>  		lock_sock(sk);
>  
> -		/* remove device specific receive entries */
> +		/* rx_ops: remove device specific receive entries */
>  		list_for_each_entry(op, &bo->rx_ops, list)
>  			if (op->rx_reg_dev == dev)
>  				bcm_rx_unreg(dev, op);
>  
> +		/* tx_ops: stop device specific cyclic transmissions on the
> +		 * vanishing ifindex and invalidate the op->ifindex.
> +		 */
> +		list_for_each_entry(op, &bo->tx_ops, list) {
> +			if (op->ifindex != dev->ifindex)
> +				continue;
> +
> +			spin_lock_bh(&op->bcm_tx_lock);
> +			op->ifindex = 0;

[Severity: High]
Does setting op->ifindex = 0 permanently orphan the operation and cause a memory leak?

The commit message states the index is cleared "so that a later TX_SETUP update can 
reconfigure it". However, bcm_tx_setup() rejects an ifindex of 0 with -ENODEV:

bcm_tx_setup() {
    ...
	if (!ifindex)
		return -ENODEV;
    ...
}

Furthermore, bcm_find_op() strictly requires an exact match on ifindex. 
Since userspace can't pass 0, and passing the old valid index will no longer match 
the zeroed op->ifindex, any subsequent TX_SETUP will fail to find it and will allocate a 
duplicate bcm_op. A TX_DELETE will similarly fail to find and remove the operation, 
leaving it as a zombie.

> +			spin_unlock_bh(&op->bcm_tx_lock);
> +
> +			hrtimer_cancel(&op->timer);
> +		}
> +

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260708-bcm_fixes-v7-0-c78896b342e4@hartkopp.net?part=9

  reply	other threads:[~2026-07-08 15:40 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-08 15:21 [PATCH v7 00/10] can: bcm: collected fixes - final2 :-D Oliver Hartkopp via B4 Relay
2026-07-08 15:21 ` [PATCH v7 01/10] can: bcm: defer rx_op deallocation to workqueue to fix thrtimer UAF Oliver Hartkopp via B4 Relay
2026-07-08 15:36   ` sashiko-bot
2026-07-08 15:21 ` [PATCH v7 02/10] can: bcm: fix lockless bound/ifindex race and silent RX_SETUP failure Oliver Hartkopp via B4 Relay
2026-07-08 15:36   ` sashiko-bot
2026-07-08 15:21 ` [PATCH v7 03/10] can: bcm: add locking when updating filter and timer values Oliver Hartkopp via B4 Relay
2026-07-08 15:21 ` [PATCH v7 04/10] can: bcm: fix CAN frame rx/tx statistics Oliver Hartkopp via B4 Relay
2026-07-08 15:34   ` sashiko-bot
2026-07-08 15:21 ` [PATCH v7 05/10] can: bcm: add missing rcu list annotations and operations Oliver Hartkopp via B4 Relay
2026-07-08 15:35   ` sashiko-bot
2026-07-08 15:21 ` [PATCH v7 06/10] can: bcm: extend bcm_tx_lock usage for data and timer updates Oliver Hartkopp via B4 Relay
2026-07-08 15:34   ` sashiko-bot
2026-07-08 15:21 ` [PATCH v7 07/10] can: bcm: validate frame length in bcm_rx_setup() for RTR replies Oliver Hartkopp via B4 Relay
2026-07-08 15:21 ` [PATCH v7 08/10] can: bcm: add missing device refcount for CAN filter removal Oliver Hartkopp via B4 Relay
2026-07-08 15:21 ` [PATCH v7 09/10] can: bcm: fix stale rx/tx ops after device removal Oliver Hartkopp via B4 Relay
2026-07-08 15:40   ` sashiko-bot [this message]
2026-07-08 15:21 ` [PATCH v7 10/10] can: bcm: fix data race on rx_stamp/rx_ifindex in bcm_rx_handler() Oliver Hartkopp via B4 Relay
2026-07-08 15:48   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260708154039.347ED1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=o.rempel@pengutronix.de \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=socketcan@hartkopp.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox