From: Quan Sun <2022090917019@std.uestc.edu.cn>
To: netdev@vger.kernel.org, maxime.chevallier@bootlin.com, andrew@lunn.ch
Cc: kuba@kernel.org, edumazet@google.com, pabeni@redhat.com,
Quan Sun <2022090917019@std.uestc.edu.cn>
Subject: [PATCH net] net: ethtool: fix NULL pointer dereference in phy_reply_size
Date: Wed, 6 May 2026 20:01:31 +0800 [thread overview]
Message-ID: <20260506120131.767679-1-2022090917019@std.uestc.edu.cn> (raw)
In phy_prepare_data(), the strings rep_data->name and rep_data->drvname
are allocated using kstrdup(). However, the return values of these
allocations are not checked.
If kstrdup() fails to allocate memory, it returns NULL. The function
phy_prepare_data() will still return 0 (success). Subsequently, the
handler ethnl_default_doit() continues the execution flow and calls
phy_reply_size() to calculate the size of the reply message. This
unconditionally executes strlen(rep_data->name), leading to a kernel
NULL pointer dereference and panic.
Fix this by properly checking the return values of kstrdup() for both
`name` and `drvname`, and returning -ENOMEM if the allocation fails.
Signed-off-by: Quan Sun <2022090917019@std.uestc.edu.cn>
---
net/ethtool/phy.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/net/ethtool/phy.c b/net/ethtool/phy.c
index d4e6887055ab1..6cf3df1df8659 100644
--- a/net/ethtool/phy.c
+++ b/net/ethtool/phy.c
@@ -88,8 +88,17 @@ static int phy_prepare_data(const struct ethnl_req_info *req_info,
return -EOPNOTSUPP;
rep_data->phyindex = phydev->phyindex;
+
rep_data->name = kstrdup(dev_name(&phydev->mdio.dev), GFP_KERNEL);
+ if (!rep_data->name)
+ return -ENOMEM;
+
rep_data->drvname = kstrdup(phydev->drv->name, GFP_KERNEL);
+ if (!rep_data->drvname) {
+ kfree(rep_data->name);
+ return -ENOMEM;
+ }
+
rep_data->upstream_type = pdn->upstream_type;
if (pdn->upstream_type == PHY_UPSTREAM_PHY) {
--
2.43.0
next reply other threads:[~2026-05-06 12:02 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-06 12:01 Quan Sun [this message]
2026-05-06 12:23 ` [PATCH net] net: ethtool: fix NULL pointer dereference in phy_reply_size Maxime Chevallier
2026-05-06 13:33 ` Quan Sun
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=20260506120131.767679-1-2022090917019@std.uestc.edu.cn \
--to=2022090917019@std.uestc.edu.cn \
--cc=andrew@lunn.ch \
--cc=edumazet@google.com \
--cc=kuba@kernel.org \
--cc=maxime.chevallier@bootlin.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
/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