[PATCH] firewire: core: clean up modalias buffer handling

Thorsten Blum posted 1 patch 3 days, 12 hours ago
drivers/firewire/core-device.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
[PATCH] firewire: core: clean up modalias buffer handling
Posted by Thorsten Blum 3 days, 12 hours ago
Use scnprintf() in get_modalias() to return the number of characters
actually written to the destination buffer.

Also reserve space for the trailing newline in modalias_show() and
append it explicitly, instead of using strcpy() and relying on the
formatted modalias to fit within PAGE_SIZE.

While the current code is safe, this makes the sysfs buffer handling
more explicit and consistent.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 drivers/firewire/core-device.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/firewire/core-device.c b/drivers/firewire/core-device.c
index c0f17da27a22..182eb4321e47 100644
--- a/drivers/firewire/core-device.c
+++ b/drivers/firewire/core-device.c
@@ -234,9 +234,9 @@ static int get_modalias(const struct fw_unit *unit, char *buffer, size_t buffer_
 
 	get_modalias_ids(unit, id);
 
-	return snprintf(buffer, buffer_size,
-			"ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
-			id[0], id[1], id[2], id[3]);
+	return scnprintf(buffer, buffer_size,
+			 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
+			 id[0], id[1], id[2], id[3]);
 }
 
 static int fw_unit_uevent(const struct device *dev, struct kobj_uevent_env *env)
@@ -435,8 +435,9 @@ static ssize_t modalias_show(struct device *dev,
 	struct fw_unit *unit = fw_unit(dev);
 	int length;
 
-	length = get_modalias(unit, buf, PAGE_SIZE);
-	strcpy(buf + length, "\n");
+	length = get_modalias(unit, buf, PAGE_SIZE - 1);
+	buf[length] = '\n';
+	buf[length + 1] = '\0';
 
 	return length + 1;
 }
Re: [PATCH] firewire: core: clean up modalias buffer handling
Posted by Takashi Sakamoto 3 days, 8 hours ago
Hi,

Thanks for sending your patch.

On Sun, Mar 29, 2026 at 11:19:40PM +0200, Thorsten Blum wrote:
> Use scnprintf() in get_modalias() to return the number of characters
> actually written to the destination buffer.
> 
> Also reserve space for the trailing newline in modalias_show() and
> append it explicitly, instead of using strcpy() and relying on the
> formatted modalias to fit within PAGE_SIZE.
> 
> While the current code is safe, this makes the sysfs buffer handling
> more explicit and consistent.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  drivers/firewire/core-device.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)

I previously considered using sysfs_emit(_at)() in the code, but decided
against it because the code is called in both sysfs and the workqueue
context of firewire devices. The buffer size differs between these cases
(either PAGE_SIZE or 64).

In any case, as you already noted, the current code is enough safe. The
total length of formatted string is fixed (= 41 + 1) by the format
template, so it is unlikely to cause issues. I have little motivation to
change it.


Thanks

Takashi Sakamoto