Skip to contentSkip to conversation

dmaengine: qcom: hidma: use sysfs_emit() in sysfs show callbacks

PATCH Applied Hungyu Lin 5 participants 5 messages Jun 7 – Jun 11

Subsystems (3)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 dmaengine: qcom: hidma: use sysfs_emit() in sysfs show callbacks

Replace sprintf() and strlen() patterns in sysfs show callbacks with sysfs_emit().

sysfs_emit() is the preferred helper for formatting sysfs output and simplifies the implementation.

---
 drivers/dma/qcom/hidma.c          |  6 ++----
 drivers/dma/qcom/hidma_mgmt_sys.c | 19 ++++++++-----------
 2 files changed, 10 insertions(+), 15 deletions(-)
drivers/dma/qcom/hidma.c +2 -4
index 5a8dca8db5ce..7a7f302a9699 100644
@@ -624,12 +624,10 @@ static ssize_t hidma_show_values(struct device *dev,
 {
 	struct hidma_dev *mdev = dev_get_drvdata(dev);
 
-	buf[0] = 0;
-
 	if (strcmp(attr->attr.name, "chid") == 0)
-		sprintf(buf, "%d\n", mdev->chidx);
+		return sysfs_emit(buf, "%d\n", mdev->chidx);
 
-	return strlen(buf);
+	return 0;
 }
 
 static inline void  hidma_sysfs_uninit(struct hidma_dev *dev)
drivers/dma/qcom/hidma_mgmt_sys.c +8 -11
index 930eae0a6257..9672ef9ee8fc 100644
@@ -102,15 +102,12 @@ static ssize_t show_values(struct device *dev, struct device_attribute *attr,
 	struct hidma_mgmt_dev *mdev = dev_get_drvdata(dev);
 	unsigned int i;
 
-	buf[0] = 0;
-
 	for (i = 0; i < ARRAY_SIZE(hidma_mgmt_files); i++) {
-		if (strcmp(attr->attr.name, hidma_mgmt_files[i].name) == 0) {
-			sprintf(buf, "%d\n", hidma_mgmt_files[i].get(mdev));
-			break;
-		}
+		if (strcmp(attr->attr.name, hidma_mgmt_files[i].name) == 0)
+			return sysfs_emit(buf, "%d\n",
+					hidma_mgmt_files[i].get(mdev));
 	}
-	return strlen(buf);
+	return 0;
 }
 
 static ssize_t set_values(struct device *dev, struct device_attribute *attr,
@@ -143,15 +140,15 @@ static ssize_t show_values_channel(struct kobject *kobj,
 	struct hidma_chan_attr *chattr;
 	struct hidma_mgmt_dev *mdev;
 
-	buf[0] = 0;
 	chattr = container_of(attr, struct hidma_chan_attr, attr);
 	mdev = chattr->mdev;
+
 	if (strcmp(attr->attr.name, "priority") == 0)
-		sprintf(buf, "%d\n", mdev->priority[chattr->index]);
+		return sysfs_emit(buf, "%d\n", mdev->priority[chattr->index]);
 	else if (strcmp(attr->attr.name, "weight") == 0)
-		sprintf(buf, "%d\n", mdev->weight[chattr->index]);
+		return sysfs_emit(buf, "%d\n", mdev->weight[chattr->index]);
 
-	return strlen(buf);
+	return 0;
 }
 
 static ssize_t set_values_channel(struct kobject *kobj,
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Missing kobject_put on error paths and driver removal leaves dangling sysfs files, leading to Use-After-Free when accessed. --

commit fad7d7a1746d0d84aca95c02e2a6fd4400ce44f9
Author: Hungyu Lin <[email protected]>

dmaengine: qcom: hidma: use sysfs_emit() in sysfs show callbacks

This commit replaces sprintf() and strlen() patterns in the sysfs show callbacks of the hidma driver with the preferred sysfs_emit() helper.

Quoted 4 lines
drivers/dma/qcom/hidma_mgmt_sys.c +0 -0
index 930eae0a6257c..9672ef9ee8fc0 100644

[ ... ]

drivers/dma/qcom/hidma_mgmt_sys.c static ssize_t show_values_channel(struct kobject *kobj, · line 144

Quoted 7 lines
drivers/dma/qcom/hidma_mgmt_sys.c +0 -1
@@ -143,15 +140,15 @@ static ssize_t show_values_channel(struct kobject *kobj,
 	struct hidma_chan_attr *chattr;
 	struct hidma_mgmt_dev *mdev;
 
-	buf[0] = 0;
 	chattr = container_of(attr, struct hidma_chan_attr, attr);
 	mdev = chattr->mdev;

[Severity: Critical]
This is a pre-existing issue, but while reviewing these sysfs callbacks, could there be a use-after-free triggered here when mdev or chattr are accessed?

Looking at hidma_mgmt_init_sys(), kobjects are created in loops:

	chanops = kobject_create_and_add("chanops", &mdev->pdev->dev.kobj);

However, there do not appear to be any matching kobject_put() calls on the error paths, nor a remove callback to tear them down on driver unbind. Since the structures backing these attributes (mdev and chattr) are managed by devm_ allocations, they are automatically freed upon probe failure or device unbind.

Does this allow the sysfs files to remain accessible in the filesystem, triggering a use-after-free when reading or writing to them dereferences the freed memory in show_values_channel()?

Quoted 4 lines
drivers/dma/qcom/hidma_mgmt_sys.c +2 -1
+
 	if (strcmp(attr->attr.name, "priority") == 0)
-		sprintf(buf, "%d\n", mdev->priority[chattr->index]);
+		return sysfs_emit(buf, "%d\n", mdev->priority[chattr->index]);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Hungyu Lin
Quoted 12 lines · Hungyu Lin

Replace sprintf() and strlen() patterns in sysfs show callbacks with sysfs_emit().

sysfs_emit() is the preferred helper for formatting sysfs output and simplifies the implementation.

---
 drivers/dma/qcom/hidma.c          |  6 ++----
 drivers/dma/qcom/hidma_mgmt_sys.c | 19 ++++++++-----------
 2 files changed, 10 insertions(+), 15 deletions(-)
Signature · 2 lines
With best wishes
Dmitry
Reviewed-by on v1 1/1
#3 lore raw
In reply to Hungyu Lin
Quoted 10 lines · Hungyu Lin

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Replace sprintf() and strlen() patterns in sysfs show callbacks with sysfs_emit().

sysfs_emit() is the preferred helper for formatting sysfs output and simplifies the implementation.

---

Quoted 70 lines
 drivers/dma/qcom/hidma.c          |  6 ++----
 drivers/dma/qcom/hidma_mgmt_sys.c | 19 ++++++++-----------
 2 files changed, 10 insertions(+), 15 deletions(-)
drivers/dma/qcom/hidma.c +2 -4
index 5a8dca8db5ce..7a7f302a9699 100644
@@ -624,12 +624,10 @@ static ssize_t hidma_show_values(struct device *dev,
 {
        struct hidma_dev *mdev = dev_get_drvdata(dev);
 
-       buf[0] = 0;
-
        if (strcmp(attr->attr.name, "chid") == 0)
-               sprintf(buf, "%d\n", mdev->chidx);
+               return sysfs_emit(buf, "%d\n", mdev->chidx);
 
-       return strlen(buf);
+       return 0;
 }
 
 static inline void  hidma_sysfs_uninit(struct hidma_dev *dev)
drivers/dma/qcom/hidma_mgmt_sys.c +8 -12
index 930eae0a6257..9672ef9ee8fc 100644
@@ -102,15 +102,12 @@ static ssize_t show_values(struct device *dev, struct device_attribute *attr,
        struct hidma_mgmt_dev *mdev = dev_get_drvdata(dev);
        unsigned int i;
 
-       buf[0] = 0;
-
        for (i = 0; i < ARRAY_SIZE(hidma_mgmt_files); i++) {
-               if (strcmp(attr->attr.name, hidma_mgmt_files[i].name) == 0) {
-                       sprintf(buf, "%d\n", hidma_mgmt_files[i].get(mdev));
-                       break;
-               }
+               if (strcmp(attr->attr.name, hidma_mgmt_files[i].name) == 0)
+                       return sysfs_emit(buf, "%d\n",
+                                       hidma_mgmt_files[i].get(mdev));
        }
-       return strlen(buf);
+       return 0;
 }
 
 static ssize_t set_values(struct device *dev, struct device_attribute *attr,
@@ -143,15 +140,15 @@ static ssize_t show_values_channel(struct kobject *kobj,
        struct hidma_chan_attr *chattr;
        struct hidma_mgmt_dev *mdev;
 
-       buf[0] = 0;
        chattr = container_of(attr, struct hidma_chan_attr, attr);
        mdev = chattr->mdev;
+
        if (strcmp(attr->attr.name, "priority") == 0)
-               sprintf(buf, "%d\n", mdev->priority[chattr->index]);
+               return sysfs_emit(buf, "%d\n", mdev->priority[chattr->index]);
        else if (strcmp(attr->attr.name, "weight") == 0)
-               sprintf(buf, "%d\n", mdev->weight[chattr->index]);
+               return sysfs_emit(buf, "%d\n", mdev->weight[chattr->index]);
 
-       return strlen(buf);
+       return 0;
 }
 
 static ssize_t set_values_channel(struct kobject *kobj,
--

2.34.1

maintainer Applied
#4 lore raw
In reply to Hungyu Lin
Quoted 8 lines · Hungyu Lin

Replace sprintf() and strlen() patterns in sysfs show callbacks with sysfs_emit().

sysfs_emit() is the preferred helper for formatting sysfs output and simplifies the implementation.

[...]

Applied, thanks!

[1/1] dmaengine: qcom: hidma: use sysfs_emit() in sysfs show callbacks

      commit: 26f926b44dbfc035d5ba0ccfc4387a40aa9947c1

Best regards,

Signature · 1 line
~Vinod