From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795922088; b=iDKLi1QCDjmz2ohCr61SdgLehdHo6IhPhh5liTMUSSNCRvmi146rPG3WzhV14DXRi+6U zrc7AVTdm/kM3NjYTC0baqoT7dPizeyo8m3z0pK6wmnxoUqyIOdIVDVGI7gsgtNExqCQy fw1F8h3+agQIzqs/YzKnHzgM+dlkGc9DZqbGPZIFtZ09YUzmRZFK803lh++QsDKU2Y5cH GnZ1eKz47eqQKMJkdaHCW4jX0z9o6/TpqyeM83ihDwwjFNAOVoAU/D5au5MyASGG26ujG vMKoTuQrKz7wpSH5wGLSWoZ6yLk6L3IPo2nx3QicueTTRjAk6FkIGSKQ7y4WzGnxT6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795922088; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=XOGvYCeAY5h45oQbPN7+O5UbrGIPcNgHYbT16X0FJJI=; b=j2/4kwiX85EoOc9rIqKGvsPuzeAzT4ZFMlBdZPeGpopRSxWUGR0XvgtYvawttoVWpSWO ZAkc7//E52FKVGqwUbq+VBV/KLtovAwQ//zRSThyPGXYAnsWts4sYPxEOyCuIGp3O6q13 97Pd9/Xr+KLLCWpCVtdEhksVB5mhByvIBKkkg1Cavtfk8Eo4KjxuwyLvVf4kNWxz/Ldfk ZXlH/nDo+HgHyEytN9HZL9QfgFQ41hxL3g5aE2wc8qmHReK1gX+PwO4Rcu06EfWLVuAsg n2GljQC/KnwAHDHctcMrY29PojLxg6WUZlVPUNVPecuxopV4HgdIjwn/DB2Vpvzo9/A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=B2O3a7fj; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1940E606A32F; Sun, 29 Nov 2026 03:14:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=B2O3a7fj Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0381D1F000C1; Sun, 29 Nov 2026 03:14:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795922087; bh=XOGvYCeAY5h45oQbPN7+O5UbrGIPcNgHYbT16X0FJJI=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=B2O3a7fjnDSZi4zMUQlLr+Mx1icRZ6OikRDx77Xhy6qa9+pegAMjNtA267lS+/xUg 0G7kXK+2X8GIq321nI/t0BoVoXxfojnZ+QAJd6Hak9KjPYP1dXZjksLATegKmV8H9u PU6ndhySxJkSTcy3rFWv7NHAGoseDW51v4kZPFF38OUjJMtD4o3ckA1C51Og9mzaOb W8mYO+FpNfMQgVVG+7ElYd67RhBpiImKbt6bT10L7yPXEf5Cs9wDP6ddGazXTTtHav 6rqFbBcqxl6YV3+SXilIWNUyul1ngakJHg82uryIEPXEad+PTeQz4U2g7a+NIKvWP3 Qp/QRqOYxXMqg== Subject: Re: [PATCH net-next v4] mctp i2c: add sysfs control for mux child netdevs From: netdev-bot+sashiko@kernel.org To: 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 Date: Sun, 29 Nov 2026 03:14:46 +0000 Message-ID: <179592208656.3146.6280669830648235309@kernel.org> In-Reply-To: <20261125025411.986998-1-kenny_hsieh@jabil.com> References: <20261125025411.986998-1-kenny_hsieh@jabil.com> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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? [ ... ] -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125025411.986998-1-kenny_hsieh%40jabil.com