The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Zijun Hu <zijun_hu@icloud.com>
To: Rob Herring <robh@kernel.org>,
	Saravana Kannan <saravanak@google.com>,
	 Maxime Ripard <mripard@kernel.org>,
	Robin Murphy <robin.murphy@arm.com>,
	 Grant Likely <grant.likely@secretlab.ca>
Cc: Zijun Hu <zijun_hu@icloud.com>,
	devicetree@vger.kernel.org,  linux-kernel@vger.kernel.org,
	Zijun Hu <quic_zijuhu@quicinc.com>
Subject: [PATCH v2 5/7] of: Fix available buffer size calculating error in API of_device_uevent_modalias()
Date: Mon, 16 Dec 2024 08:40:44 +0800	[thread overview]
Message-ID: <20241216-of_core_fix-v2-5-e69b8f60da63@quicinc.com> (raw)
In-Reply-To: <20241216-of_core_fix-v2-0-e69b8f60da63@quicinc.com>

From: Zijun Hu <quic_zijuhu@quicinc.com>

of_device_uevent_modalias() saves MODALIAS value from offset
(@env->buflen - 1), so the available buffer size should be
(sizeof(@env->buf) - @env->buflen + 1), but it uses the wrong
size (sizeof(@env->buf) - @env->buflen).

Fix by using size of space from char '\0' inclusive which ends "MODALIAS=".

Fixes: dd27dcda37f0 ("of/device: merge of_device_uevent")
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
---
 drivers/of/device.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/drivers/of/device.c b/drivers/of/device.c
index edf3be1972658f6dc165f577da53b10c7eebc116..f24c19e7aba8e01656f503ae328a4e08aab5a5f3 100644
--- a/drivers/of/device.c
+++ b/drivers/of/device.c
@@ -257,6 +257,7 @@ EXPORT_SYMBOL_GPL(of_device_uevent);
 int of_device_uevent_modalias(const struct device *dev, struct kobj_uevent_env *env)
 {
 	int sl;
+	int pos;
 
 	if ((!dev) || (!dev->of_node) || dev->of_node_reused)
 		return -ENODEV;
@@ -265,13 +266,18 @@ int of_device_uevent_modalias(const struct device *dev, struct kobj_uevent_env *
 	if (add_uevent_var(env, "MODALIAS="))
 		return -ENOMEM;
 
-	sl = of_modalias(dev->of_node, &env->buf[env->buflen-1],
-			 sizeof(env->buf) - env->buflen);
+	/*
+	 * @env->buflen is pointing to the char after '\0' now
+	 * override the '\0' to save MODALIAS value.
+	 */
+	pos = env->buflen - 1;
+	sl = of_modalias(dev->of_node, &env->buf[pos],
+			 sizeof(env->buf) - pos);
 	if (sl < 0)
 		return sl;
-	if (sl >= (sizeof(env->buf) - env->buflen))
+	if (sl >= (sizeof(env->buf) - pos))
 		return -ENOMEM;
-	env->buflen += sl;
+	env->buflen = pos + sl + 1;
 
 	return 0;
 }

-- 
2.34.1


  parent reply	other threads:[~2024-12-16  0:41 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-16  0:40 [PATCH v2 0/7] of: fix bugs and improve codes Zijun Hu
2024-12-16  0:40 ` [PATCH v2 1/7] of: Fix API of_find_node_opts_by_path() finding OF device node failure Zijun Hu
2024-12-16  0:40 ` [PATCH v2 2/7] of: unittest: Add a test case for API of_find_node_opts_by_path() Zijun Hu
2024-12-16 19:23   ` Rob Herring
2024-12-16 21:29     ` Rob Herring
2024-12-16  0:40 ` [PATCH v2 3/7] of: Correct child specifier used as input of the 2nd nexus node Zijun Hu
2024-12-16  0:40 ` [PATCH v2 4/7] of: Exchange implementation between of_property_present() and of_property_read_bool() Zijun Hu
2024-12-16  0:40 ` Zijun Hu [this message]
2024-12-16  0:40 ` [PATCH v2 6/7] of: Fix potential wrong MODALIAS uevent value Zijun Hu
2024-12-16  3:42   ` kernel test robot
2024-12-16  3:52   ` kernel test robot
2024-12-16  7:02   ` kernel test robot
2024-12-16  0:40 ` [PATCH v2 7/7] of: Do not expose of_alias_scan() and correct its comments Zijun Hu

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=20241216-of_core_fix-v2-5-e69b8f60da63@quicinc.com \
    --to=zijun_hu@icloud.com \
    --cc=devicetree@vger.kernel.org \
    --cc=grant.likely@secretlab.ca \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mripard@kernel.org \
    --cc=quic_zijuhu@quicinc.com \
    --cc=robh@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=saravanak@google.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