Linux I2C development
 help / color / mirror / Atom feed
From: willemb@google.com
To: netdev-bot+sashiko@kernel.org, kenny_hsieh@jabil.com
Cc: netdev@vger.kernel.org, jk@codeconstruct.com.au,
	matt@codeconstruct.com.au,  andi.shyti@kernel.org,
	peda@lysator.liu.se, andrew+netdev@lunn.ch,  davem@davemloft.net,
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
	 linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next v4] mctp i2c: add sysfs control for mux child netdevs
Date: Thu, 03 Dec 2026 21:03:02 -0500	[thread overview]
Message-ID: <willemb.875d5a1a4520@google.com> (raw)
In-Reply-To: <179592208656.3146.6280669830648235309@kernel.org>

netdev-bot+sashiko@ wrote:
> Thank you for your contribution! Sashiko AI review found 3 potential
> issue(s) to consider.

> Critical: 0 · High: 0 (1) · Medium: 2 · Low: 1

> If you believe none of the issues are legitimate, please reply to each
> issue of Medium severity or higher and explain why the AI is wrong.
> If any of them are legitimate and you plan to respin, please let patchwork
> know by including "pw-bot: cr" as a separate line at the end of your reply
> (one such reply per series is enough).

> - [Medium] This net-next feature patch also fixes two races in existing
>    code, but it has no Fixes: tag and is not sent to net.
> - [Medium] A sysfs add can attach a netdev to an I2C adapter whose
>    device_add() later fails.
> - [Low] The new sysfs ABI does not document which network namespace the
>    netdev lands in.

> Pre-existing issues:
> - [High] This problem wasn't introduced by this patch, but the new sysfs
>    path makes it easier to hit.

> --

> mctp i2c: add sysfs control for mux child netdevs

> This patch adds two write-only attributes, add_mctp_controller and
> remove_mctp_controller, to the mctp-i2c controller device. They let
> userspace create or remove MCTP netdevs for busses in the controller's mux
> tree that have no devicetree node. It also makes mctp_i2c_add_netdev()  
> skip
> adapters that already have a netdev, and handles BUS_NOTIFY_REMOVED_DEVICE
> in the bus notifier.

> > diff --git a/Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp  
> b/Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp
> > new file mode 100644
> > index 0000000000000..4efbc888c7bb8
> > --- /dev/null
> > +++ b/Documentation/ABI/testing/sysfs-bus-i2c-devices-mctp
> > @@ -0,0 +1,27 @@

> [ ... ]

> > +Description:
> > +		Write-only, on each bound mctp-i2c-controller device.
> > +
> > +		Writing an I2C bus name such as "i2c-30" to add_mctp_controller
> > +		creates an MCTP network device for that bus, and writing it to
> > +		remove_mctp_controller removes it. The bus must be the
> > +		controller's root bus or a mux child below it. Adding a bus
> > +		that already has a network device fails with EEXIST, and
> > +		removing one that has none fails with ENODEV.

> [Severity: Low]
> Which network namespace does the new netdev end up in? The add path runs
> mctp_i2c_controller_write()->mctp_i2c_add_netdev(), which does:

> 	dev_net_set(ndev, current->nsproxy->net_ns);

> So the netdev for the physical bus is created in the writer's netns. That
> happens, for example, under ip netns exec or inside a privileged  
> container.

> The capable(CAP_NET_ADMIN) check only tests the initial user namespace.
> remove_mctp_controller looks the netdev up in mcli->devs and never  
> compares
> namespaces. A writer in any netns can therefore remove a netdev that lives
> in another one, including devicetree-created netdevs in init_net.

> Could the ABI description and the commit message say this? Right now only
> the v3 changelog below the --- line says "The netdev goes in the writer's
> netns now", and that text won't reach git history.

> [ ... ]

> > diff --git a/drivers/net/mctp/mctp-i2c.c b/drivers/net/mctp/mctp-i2c.c
> > index b02476d338bbf..ecc3036d54a35 100644
> > --- a/drivers/net/mctp/mctp-i2c.c
> > +++ b/drivers/net/mctp/mctp-i2c.c

> [ ... ]

> > @@ -876,6 +894,9 @@ static int mctp_i2c_add_netdev(struct  
> mctp_i2c_client *mcli,
> >  	}
> >
> >  	WARN_ON(!mutex_is_locked(&driver_clients_lock));
> > +	if (mctp_i2c_find_dev(mcli, adap))
> > +		return 0;
> > +

> [Severity: Medium]
> Does this early return also fix an existing race that has nothing to do
> with the new sysfs attributes?

> mctp_i2c_probe() walks the bus under driver_clients_lock:

> 	i2c_for_each_dev(mcli, mctp_i2c_client_try_attach);

> device_add() puts the adapter on the bus klist in bus_add_device() before
> it sends BUS_NOTIFY_ADD_DEVICE. So the probe walk can add a netdev first,
> and then mctp_i2c_notify_add() calls mctp_i2c_add_netdev() again for the
> same adapter.

> Before this patch, the second netdev failed registration with -EEXIST and
> the driver logged errors. If the probe and the adapter registration ran in
> different network namespaces, a second netdev was registered for the same
> adapter.

> The new BUS_NOTIFY_REMOVED_DEVICE case in mctp_i2c_notifier_call() looks
> similar:

> 	case BUS_NOTIFY_DEL_DEVICE:
> 	/* A sysfs write can add a netdev after DEL_DEVICE */
> 	case BUS_NOTIFY_REMOVED_DEVICE:

> device_del() sends BUS_NOTIFY_DEL_DEVICE but leaves the adapter on the bus
> klist until bus_remove_device(). i2c_del_adapter() also doesn't hold
> core_lock across device_unregister().

> A controller probe walk in that window can reach
> mctp_i2c_client_try_attach()->mctp_i2c_add_netdev() for the adapter being
> deleted, and takes a reference on adap->dev. Before this patch nothing
> removed that netdev, and i2c_del_adapter() then waited on  
> adap->dev_released
> forever.

> The code comment and the commit message credit only the sysfs write for  
> the
> REMOVED_DEVICE case. The probe walk duplicate is described as part of the
> feature rather than as a fix.

> Would it make sense to split these two fixes out and send them to net with
> Fixes: tags, so that stable kernels get them as well?

> [ ... ]

> > @@ -1077,6 +1089,79 @@ static void mctp_i2c_remove(struct i2c_client  
> *client)
> >  	mutex_unlock(&driver_clients_lock);
> >  }
> >
> > +/* Returns the named adapter in mcli's mux tree, with a reference held  
> */
> > +static struct i2c_adapter *mctp_i2c_find_named(struct mctp_i2c_client  
> *mcli,
> > +					       const char *name)
> > +{
> > +	struct i2c_adapter *adap = NULL, *root = NULL;
> > +	struct device *dev;
> > +
> > +	dev = bus_find_device_by_name(&i2c_bus_type, NULL, name);

> [Severity: Medium]
> Can this find an adapter whose device_add() hasn't finished yet and later
> fails?

> i2c_register_adapter()->device_add() puts the adapter on the bus klist in
> bus_add_device() and only then calls dpm_sysfs_add(). A write to
> add_mctp_controller can land in that window and find the adapter here.

> mctp_i2c_get_adapter() only checks dev->type, so mctp_i2c_add_netdev()  
> goes
> on to register a netdev. The midev holds a reference on adap->dev from
> mctp_i2c_midev_init(), and the netdev has adap->dev as its parent through
> SET_NETDEV_DEV().

> If dpm_sysfs_add() then fails, device_add() unwinds without sending any  
> bus
> notification:

> drivers/base/core.c:device_add() {
>      ...
>   DPMError:
> 	device_set_driver(dev, NULL);
> 	bus_remove_device(dev);
>      ...
> }

> BUS_NOTIFY_REMOVED_DEVICE is only sent from device_del(), so
> mctp_i2c_notifier_call() never calls mctp_i2c_notify_del() for this
> adapter. i2c_register_adapter() then blocks in its error path, because the
> netdev still holds its references:

> drivers/i2c/i2c-core-base.c:i2c_register_adapter() {
>      ...
> 	init_completion(&adap->dev_released);
> 	put_device(&adap->dev);
> 	wait_for_completion(&adap->dev_released);
>      ...
> }

> A write to remove_mctp_controller can't recover from this. The adapter is
> no longer on the klist, so mctp_i2c_find_named() returns -ENODEV. Only
> unbinding the controller frees the netdev.

> The existing probe walk has the same window, but this sysfs path adds a
> trigger that userspace controls.

> [ ... ]

> > +static ssize_t mctp_i2c_controller_write(struct device *dev, const  
> char *buf,
> > +					 size_t count, bool add)
> > +{
> > +	struct mctp_i2c_client *mcli = i2c_get_clientdata(to_i2c_client(dev));
> > +	struct i2c_adapter *adap;
> > +	int rc = 0;
> > +
> > +	if (!capable(CAP_NET_ADMIN))
> > +		return -EPERM;
> > +
> > +	mutex_lock(&driver_clients_lock);
> > +	adap = mctp_i2c_find_named(mcli, buf);
> > +	if (IS_ERR(adap)) {
> > +		rc = PTR_ERR(adap);
> > +		goto out;
> > +	}
> > +
> > +	if (add && mctp_i2c_find_dev(mcli, adap))
> > +		rc = -EEXIST;
> > +	else if (add)
> > +		rc = mctp_i2c_add_netdev(mcli, adap);

> [Severity: High]
> This isn't a bug introduced by this patch, but this path lets userspace
> call mctp_i2c_add_netdev() as often as it likes. That makes an existing
> double free in its error path easier to hit.

> Suppose mctp_register_netdev() fails after register_netdevice() has passed
> the name check. register_netdevice() then calls ndo_uninit, which for this
> driver is mctp_i2c_ndo_uninit()->mctp_i2c_midev_free(midev).

> That covers failures in netdev_check_ops, netdev_do_alloc_pcpu_stats,
> dev_index_reserve, NETDEV_POST_INIT and netdev_register_kobject. It also
> covers a NETDEV_REGISTER notifier veto, such as
> mctp_dev_notify()->mctp_register() returning -ENOMEM, which goes through
> unregister_netdevice_queue(). A name collision doesn't trigger it, since
> dev_get_valid_name() fails before ndo_init.

> mctp_i2c_add_netdev() then frees midev a second time:

> drivers/net/mctp/mctp-i2c.c:mctp_i2c_add_netdev() {
>      ...
> err:
> 	if (midev)
> 		mctp_i2c_midev_free(midev);
> 	if (ndev)
> 		free_netdev(ndev);
> 	return rc;
> }

> The second mctp_i2c_midev_free() calls list_del() on an entry that is
> already poisoned. It also calls put_device() on midev->adapter->dev and
> mcli->client->dev again, which can underflow those refcounts and free the
> devices early.

> The changelog says the double free is fixed separately in net (now at v5),
> but that fix isn't in this tree. If this patch lands first, can the bug be
> reached through add_mctp_controller?

Please respond to the sashiko findings

      reply	other threads:[~2026-12-04  2:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  2:54 [PATCH net-next v4] mctp i2c: add sysfs control for mux child netdevs Kenny Hsieh
2026-11-29  3:14 ` netdev-bot+sashiko
2026-12-04  2:03   ` willemb [this message]

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=willemb.875d5a1a4520@google.com \
    --to=willemb@google.com \
    --cc=andi.shyti@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=jk@codeconstruct.com.au \
    --cc=kenny_hsieh@jabil.com \
    --cc=kuba@kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matt@codeconstruct.com.au \
    --cc=netdev-bot+sashiko@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=peda@lysator.liu.se \
    /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