Skip to contentSkip to conversation

dmaengine: qcom: hidma-mgmt: Fix sysfs cleanup on setup failure

PATCH New Yuho Choi 2 participants 2 messages Jun 8

Subsystems (3)
lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 dmaengine: qcom: hidma-mgmt: Fix sysfs cleanup on setup failure

hidma_mgmt_init_sys() creates the chanops kobject, per-channel kobjects and sysfs files incrementally. If a later creation step fails, the function returns without tearing down the objects already created.

Those sysfs callbacks reference devm-managed driver data. A later probe failure can free that data while the sysfs entries and kobjects remain registered.

Track the chanops kobject in struct hidma_mgmt_dev, unwind the sysfs files and channel kobjects on setup failure, and register the same cleanup with devm after successful setup.

Fixes 7f8f209fd6e0 ("dmaengine: add Qualcomm Technologies HIDMA management driver")
---
 drivers/dma/qcom/hidma_mgmt.h     |  1 +
 drivers/dma/qcom/hidma_mgmt_sys.c | 65 +++++++++++++++++++++++++------
 2 files changed, 55 insertions(+), 11 deletions(-)
drivers/dma/qcom/hidma_mgmt.h +1 -0
index 30e8095988bf..4fb6759e3371 100644
@@ -24,6 +24,7 @@ struct hidma_mgmt_dev {
 	resource_size_t addrsize;
 
 	struct kobject **chroots;
+	struct kobject *chanops;
 	struct platform_device *pdev;
 };
 
drivers/dma/qcom/hidma_mgmt_sys.c +54 -11

105 lines not on this page · raw message

Signature · 1 line
2.43.0
#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:
- [High] Reliance on kobject_put without kobject_del for sysfs cleanup leads to use-after-free --

commit a6dc90d05cf8a69c255834fbadc8ca4eb60dbe56
Author: Yuho Choi <[email protected]>

dmaengine: qcom: hidma-mgmt: Fix sysfs cleanup on setup failure

hidma_mgmt_init_sys() creates the chanops kobject, per-channel kobjects and sysfs files incrementally. If a later creation step fails, the function returns without tearing down the objects already created.

Quoted 3 lines
drivers/dma/qcom/hidma_mgmt_sys.c +0 -0

[ ... ]

Quoted 8 lines
drivers/dma/qcom/hidma_mgmt_sys.c +4 -0
@@ -231,20 +231,52 @@ static int create_sysfs_entry_channel(struct hidma_mgmt_dev *mdev, char *name,
 	return sysfs_create_file(parent, &chattr->attr.attr);
 }
 
+static void hidma_mgmt_uninit_sys(struct hidma_mgmt_dev *mdev,
+				  unsigned int sysfs_count,
+				  unsigned int chroot_count)
+{

drivers/dma/qcom/hidma_mgmt_sys.c static int create_sysfs_entry_channel(struct hidma_mgmt_dev *mdev, char *name, · line 247

Quoted 10 lines
drivers/dma/qcom/hidma_mgmt_sys.c +10 -0
+	unsigned int i;
+
+	for (i = 0; i < sysfs_count; i++) {
+		struct attribute attr = { .name = hidma_mgmt_files[i].name };
+
+		sysfs_remove_file(&mdev->pdev->dev.kobj, &attr);
+	}
+
+	for (i = 0; i < chroot_count; i++) {
+		kobject_put(mdev->chroots[i]);

[Severity: High]
This is a pre-existing issue, but does this teardown logic still leave a potential use-after-free?

Since the sysfs attributes (chattr) and their names are allocated using devm_kmalloc() and devm_kstrdup() in create_sysfs_entry_channel(), devres will free them immediately upon probe failure or device unbind.

If userspace holds an open file descriptor to any of the per-channel sysfs files, or if CONFIG_DEBUG_KOBJECT_RELEASE is enabled, kobject_put() will only decrement the reference count and the implicit kobject_del() inside kobject_release() will be delayed. A subsequent read or write to the open sysfs file would then dereference the already freed chattr memory.

Should kobject_del() be explicitly called before kobject_put() here to synchronously remove the sysfs directory and files before devres frees the backing memory?

Quoted 8 lines
drivers/dma/qcom/hidma_mgmt_sys.c +8 -0
+		mdev->chroots[i] = NULL;
+	}
+
+	if (mdev->chanops) {
+		kobject_put(mdev->chanops);
+		mdev->chanops = NULL;
+	}
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1