Skip to contentSkip to conversation

sh: dma-sysfs: use sysfs_emit{_at} in show functions

PATCH In review Thorsten Blum 2 participants 6 messages Apr 20 – Apr 22 SuperH

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 sh: dma-sysfs: use sysfs_emit{_at} in show functions

Replace sprintf() with sysfs_emit() and sysfs_emit_at() in sysfs show functions. sysfs_emit() and sysfs_emit_at() are preferred for formatting sysfs output because they provide safer bounds checking.

---
 arch/sh/drivers/dma/dma-sysfs.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
arch/sh/drivers/dma/dma-sysfs.c +7 -6
index 9f666280d80c..d4e16cfc7a12 100644
@@ -13,6 +13,7 @@
 #include <linux/platform_device.h>
 #include <linux/err.h>
 #include <linux/string.h>
+#include <linux/sysfs.h>
 #include <asm/dma.h>
 
 static const struct bus_type dma_subsys = {
@@ -33,9 +34,9 @@ static ssize_t dma_show_devices(struct device *dev,
 		if (unlikely(!info) || !channel)
 			continue;
 
-		len += sprintf(buf + len, "%2d: %14s    %s\n",
-			       channel->chan, info->name,
-			       channel->dev_id);
+		len += sysfs_emit_at(buf, len, "%2d: %14s    %s\n",
+				     channel->chan, info->name,
+				     channel->dev_id);
 	}
 
 	return len;
@@ -65,7 +66,7 @@ static ssize_t dma_show_dev_id(struct device *dev,
 				struct device_attribute *attr, char *buf)
 {
 	struct dma_channel *channel = to_dma_channel(dev);
-	return sprintf(buf, "%s\n", channel->dev_id);
+	return sysfs_emit(buf, "%s\n", channel->dev_id);
 }
 
 static ssize_t dma_store_dev_id(struct device *dev,
@@ -98,7 +99,7 @@ static ssize_t dma_show_mode(struct device *dev,
 				struct device_attribute *attr, char *buf)
 {
 	struct dma_channel *channel = to_dma_channel(dev);
-	return sprintf(buf, "0x%08x\n", channel->mode);
+	return sysfs_emit(buf, "0x%08x\n", channel->mode);
 }
 
 static ssize_t dma_store_mode(struct device *dev,
@@ -117,7 +118,7 @@ static ssize_t dma_show_##field(struct device *dev,		\
 				struct device_attribute *attr, char *buf)\
 {									\
 	struct dma_channel *channel = to_dma_channel(dev);		\
-	return sprintf(buf, fmt, channel->field);			\
+	return sysfs_emit(buf, fmt, channel->field);			\
 }									\
 static DEVICE_ATTR(field, S_IRUGO, dma_show_##field, NULL);
 
#1 lore raw

PATCH 2/3 sh: dma-sysfs: use strscpy in dma_store_dev_id

strcpy() has been deprecated¹ because it performs no bounds checking on the destination buffer, which can lead to buffer overflows. Replace it with the safer strscpy().

¹ https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy

---
 arch/sh/drivers/dma/dma-sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sh/drivers/dma/dma-sysfs.c +1 -1
index d4e16cfc7a12..d1d6b9bb0d7e 100644
@@ -74,7 +74,7 @@ static ssize_t dma_store_dev_id(struct device *dev,
 				const char *buf, size_t count)
 {
 	struct dma_channel *channel = to_dma_channel(dev);
-	strcpy(channel->dev_id, buf);
+	strscpy(channel->dev_id, buf);
 	return count;
 }
 
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] sh: dma-sysfs: use strscpy in dma_store_dev_id

Quoted 7 lines · Thorsten Blum

strcpy() has been deprecated¹ because it performs no bounds checking on the destination buffer, which can lead to buffer overflows. Replace it with the safer strscpy().

¹ https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#3 lore raw

PATCH 3/3 sh: dma: drop redundant size argument from strscpy in request_dma

In reply to Thorsten Blum

The strscpy() size argument is optional if the destination buffer has a fixed length. Use the two-argument version of strscpy() to copy 'dev_id' to simplify the code.

---
 arch/sh/drivers/dma/dma-api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sh/drivers/dma/dma-api.c +1 -1
index 87e5a8928873..15e2c10c9805 100644
@@ -96,7 +96,7 @@ int request_dma(unsigned int chan, const char *dev_id)
 	if (atomic_xchg(&channel->busy, 1))
 		return -EBUSY;
 
-	strscpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
+	strscpy(channel->dev_id, dev_id);
 
 	if (info->ops->request) {
 		result = info->ops->request(channel);
Reviewed-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] sh: dma: drop redundant size argument from strscpy in request_dma

Quoted 5 lines · Thorsten Blum

The strscpy() size argument is optional if the destination buffer has a fixed length. Use the two-argument version of strscpy() to copy 'dev_id' to simplify the code.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Reviewed-by on v1 1/3
#5 lore raw
In reply to Thorsten Blum
Quoted 5 lines · Thorsten Blum

Replace sprintf() with sysfs_emit() and sysfs_emit_at() in sysfs show functions. sysfs_emit() and sysfs_emit_at() are preferred for formatting sysfs output because they provide safer bounds checking.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds