Article View: linux.kernel.netdev
Article #2648Re: [PATCH net] netdevsim: avoid NULL dereference after failed probe
From: Hangbin Liu
Date: Tue, 13 Oct 2026 06:47
Date: Tue, 13 Oct 2026 06:47
75 lines
8514 bytes
8514 bytes
Hi Jiacheng,
On Mon, Oct 12, 2026 at 09:59:41PM +0800, Jiacheng Xu wrote:
> device_register() reports whether device registration succeeded, not
> whether the matching driver's probe succeeded. If nsim_drv_probe()
> fails, the driver core leaves the nsim_bus_dev registered while the probe
> error path clears its driver data.
>
> new_device_store() subsequently marks the nsim_bus_dev initialized. A
> write to its new_port or del_port attribute therefore passes the init
> check and calls nsim_drv_port_add() or nsim_drv_port_del() with no valid
> nsim_dev. Both helpers pass the NULL driver data to priv_to_devlink(),
> leading to a NULL pointer dereference.
>
> This can be reproduced by creating a netdevsim device for which
> nsim_drv_probe() fails and then writing to the new_port or del_port
> attribute of the registered device.
>
> Serialize the driver data check and the port operation with the device
> lock, and reject the operation with -ENODEV when no driver data is
> present. This also prevents driver unbind from freeing nsim_dev between
> the check and its use, and follows the locking used by sriov_numvfs.
>
> Fixes: 794b2c05ca1c ("netdevsim: extend device attrs to support port addition and deletion")
> Cc: stable@vger.kernel.org
> Signed-off-by: Jiacheng Xu <stitch@zju.edu.cn>
> ---
> drivers/net/netdevsim/bus.c | 17 ++++++++++++++---
> 1 file changed, 14 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/net/netdevsim/bus.c b/drivers/net/netdevsim/bus.c
> index 41483e371f05..0ab51306ad0a 100644
> --- a/drivers/net/netdevsim/bus.c
> +++ b/drivers/net/netdevsim/bus.c
> @@ -93,8 +93,13 @@ new_port_store(struct device *dev, struct device_attribute *attr,
> return -EINVAL;
> }
>
> - ret = nsim_drv_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index,
> - addr_set ? eth_addr : NULL);
> + device_lock(dev);
> + if (!dev_get_drvdata(dev))
> + ret = -ENODEV;
> + else
> + ret = nsim_drv_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF,
> + port_index, addr_set ? eth_addr : NULL);
> + device_unlock(dev);
> return ret ? ret : count;
> }
>
> @@ -115,7 +120,13 @@ del_port_store(struct device *dev, struct device_attribute *attr,
> if (ret)
> return ret;
>
> - ret = nsim_drv_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
> + device_lock(dev);
> + if (!dev_get_drvdata(dev))
> + ret = -ENODEV;
> + else
> + ret = nsim_drv_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF,
> + port_index);
> + device_unlock(dev);
> return ret ? ret : count;
> }
>
I remember you had a reproducer in v1 patch
https://lore.kernel.org/netdev/42315c2c.15f85.1a01853d723.Coremail.stitch@zju.edu.cn/
In v2 we talked about try unregister the device if failed. But looks not easy.
This patch only rebase to latest net tree. But no version tag and change log.
Over all, the code change looks good to me.
Reviewed-by: Hangbin Liu <liuhangbin@kylinos.cn>
Technical Details
Message-ID:
<as3T_XTQtC5-S8Dq@fedora>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-345409-linux-kernel-netdev=dewy.ruReferences:
<20261012135941.78329-1-stitch@zju.edu.cn>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-345409-linux-kernel-netdev=dewy.ru From: hangbin.liu@linux.dev (Hangbin Liu) Newsgroups: linux.kernel.netdev Subject: Re: [PATCH net] netdevsim: avoid NULL dereference after failed probe Date: Tue, 13 Oct 2026 14:47:25 +0800 Organization: Usenet Network Message-ID: <as3T_XTQtC5-S8Dq@fedora> References: <20261012135941.78329-1-stitch@zju.edu.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: usenet.network; logging-data="1979736"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:Gp45YMzG9fmW6M4ziVZOIs3Z3kw= sha256:LFPYsUTioFRDm1BF6064Pm0XrA8CF9uOZ1lz5Q2Wl9A= sha1:hYOPdq0SeIuAlHGvWdDkDivVvQo= sha256:Ke/GX+g3vx+C9KcrkD4NciHZDmhc7gn4tsjws1WyWIY= Content-Disposition: inline List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 6A68310FD92; Tue, 13 Oct 2026 09:50:32 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -0.751 X-Spam-Status: No, score=-0.751 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key) header.d=linux.dev X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id VKG3Ywm7nEvk; Tue, 13 Oct 2026 09:50:24 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-345409-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 9D7F710FD86; Tue, 13 Oct 2026 09:50:14 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1]) by sea.lore.kernel.org (Postfix) with ESMTP id 7E0833A74D; Tue, 13 Oct 2026 06:47:44 +0000 (UTC) X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 10EEB3E172B; Tue, 13 Oct 2026 06:47:44 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Kailuf/9" X-Received: from mta0.migadu.com (out-165.mta0.migadu.com [91.218.175.165]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BCFA437A83D for <netdev@vger.kernel.org>; Tue, 13 Oct 2026 06:47:41 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.165 X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791874064; cv=none; b=dBr/xGJR1n2zoaacXTchbivh0+4xiAZT6czNrXwfhUWhxTRhCrheZrQ+5OyFDBXkEafVfl/ougzK1b0k3P0LxfpoT05ZnKc8liQ3HHyIlBFWJJkaCfd1Zb1OE1FJoAFhqGEo7jkDFXTcSY6KWJ+uhfCWf3SwAaRHoimOPxqRLjE= X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791874064; c=relaxed/simple; bh=mqDh8Taoi/5OosStcy5PCLFM6IlRW4x/WEjWfMzbF+o=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=QIXkIUYThDz7UYEM8tiqOF1aRA1YQ1UJV8TmcGUP58lk8q2JBpwKvOswqQi1Ai+Q0se0o8xO8Nq+jwaj/WNUI7JZG71OjdeUwqBiKbyBDT/IUnq08rksRGd3VttuCZJXZrzdQ5uHmX+MSYGRgwShj1LRzReGYOX5oh1hWiarXCY= X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Kailuf/9; arc=none smtp.client-ip=91.218.175.165 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Envelope-To: netdev@vger.kernel.org X-DKIM-Signature: a=rsa-sha256; bh=mqDh8Taoi/5OosStcy5PCLFM6IlRW4x/WEjWfMzbF+o=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1791874059; v=1; x=1792478859; b=Kailuf/9q2bG6WqYicwnQ+YVBXzXY9wEGzMcfX6H3w4EPpRr8/OaDT7tDjcJedZkZD+dAO9t +whBJOApDkGDuSLqswUqGnDCmUdxTWWfFFDp3eKcKF2GpTeUtK7z0XBDaLyDE03EceWqFdWoA+V QR3omZ1MsgpPBI88HlBXoLo8= X-Envelope-To: netdev@vger.kernel.org X-Received: by mta11.migadu.com with ESMTPS id 12800683dfe4e02b; Tue, 13 Oct 2026 06:47:39 +0000 X-Mizu-Trace-ID: 12800683dfe4e02b X-Migadu-Flow: FLOW_OUT X-Cc: kuba@kernel.org, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, pabeni@redhat.com, jiri@resnulli.us, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org X-Precedence: bulk X-Mailing-List: netdev@vger.kernel.org X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-In-Reply-To: <20261012135941.78329-1-stitch@zju.edu.cn>