Skip to contentSkip to conversation

mfd: dln2: Fix use-after-free risk in list iterator

PATCH In review Riccardo Oliva 3 participants 6 messages Oct 12 – Nov 4 Multifunction devices (MFD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mfd: dln2: Fix use-after-free risk in list iterator

In dln2_unregister_event_cb(), the iterator variable 'i' is used after the list_for_each_entry() loop terminates to free memory.

Fix this by introducing a dedicated 'target' pointer to hold the entry, isolating the iterator to the loop scope and removing the 'found' bool.

Assisted-by coccinelle sparse
---
 drivers/mfd/dln2.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/mfd/dln2.c +4 -5
index 0b1e7fd7f..7cd64aad1 100644
@@ -157,25 +157,24 @@ EXPORT_SYMBOL(dln2_register_event_cb);
 void dln2_unregister_event_cb(struct platform_device *pdev, u16 id)
 {
 	struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
-	struct dln2_event_cb_entry *i;
+	struct dln2_event_cb_entry *i, *target = NULL;
 	unsigned long flags;
-	bool found = false;
 
 	spin_lock_irqsave(&dln2->event_cb_lock, flags);
 
 	list_for_each_entry(i, &dln2->event_cb_list, list) {
 		if (i->id == id) {
 			list_del_rcu(&i->list);
-			found = true;
+			target = i;
 			break;
 		}
 	}
 
 	spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
 
-	if (found) {
+	if (target) {
 		synchronize_rcu();
-		kfree(i);
+		kfree(target);
 	}
 }
 EXPORT_SYMBOL(dln2_unregister_event_cb);
Signature · 1 line
2.43.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

In dln2_unregister_event_cb(), the iterator variable 'i' is used after the list_for_each_entry() loop terminates to free memory.

Fix this by introducing a dedicated 'target' pointer to hold the entry, isolating the iterator to the loop scope and removing the 'found' bool.

Assisted-by coccinelle sparse

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Riccardo Oliva
Quoted 2 lines · Riccardo Oliva

In dln2_unregister_event_cb(), the iterator variable 'i' is used after the list_for_each_entry() loop terminates to free memory.

How is this a use-after-free risk? 'i' is only ever used after the loop if 'found' is true, which means we broke out of the loop early while 'i' still points to a valid entry.

Is this an actual bug or are we just silencing a static checker false positive?

Quoted 15 lines

Fix this by introducing a dedicated 'target' pointer to hold the entry, isolating the iterator to the loop scope and removing the 'found' bool.

Assisted-by coccinelle sparse
---
 drivers/mfd/dln2.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/mfd/dln2.c +0 -0
index 0b1e7fd7f..7cd64aad1 100644

drivers/mfd/dln2.c EXPORT_SYMBOL(dln2_register_event_cb); · line 160

Quoted 5 lines
drivers/mfd/dln2.c +1 -1
@@ -157,25 +157,24 @@ EXPORT_SYMBOL(dln2_register_event_cb);
 void dln2_unregister_event_cb(struct platform_device *pdev, u16 id)
 {
 	struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
-	struct dln2_event_cb_entry *i;
+	struct dln2_event_cb_entry *i, *target = NULL;

If we do keep this change as a cleanup, does it make sense to use 'entry' instead of 'target' to match 'dln2_register_event_cb()' above?

Quoted 27 lines
drivers/mfd/dln2.c +3 -4
 	unsigned long flags;
-	bool found = false;
 
 	spin_lock_irqsave(&dln2->event_cb_lock, flags);
 
 	list_for_each_entry(i, &dln2->event_cb_list, list) {
 		if (i->id == id) {
 			list_del_rcu(&i->list);
-			found = true;
+			target = i;
 			break;
 		}
 	}
 
 	spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
 
-	if (found) {
+	if (target) {
 		synchronize_rcu();
-		kfree(i);
+		kfree(target);
 	}
 }
 EXPORT_SYMBOL(dln2_unregister_event_cb);
Signature · 1 line
2.43.0
Signature · 1 line
Lee Jones
#3 lore raw

PATCH v2 1/1 mfd: dln2: Clean up dln2_unregister_event_cb() loop logic

Streamline dln2_unregister_event_cb() by using a dedicated 'entry' pointer to store the matching event callback. This removes the need for the 'found' boolean flag, restricts the iterator scope, and matches the pattern used in dln2_register_event_cb().

Assisted-by coccinelle sparse

---
v2:
- Rephrase commit log: Clarified this is a cleanup rather than a bug fix
- Rename 'target' to 'entry' for consistency

 drivers/mfd/dln2.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/mfd/dln2.c +4 -5
index 0b1e7fd7f..2a9d83d32 100644
@@ -157,25 +157,24 @@ EXPORT_SYMBOL(dln2_register_event_cb);
 void dln2_unregister_event_cb(struct platform_device *pdev, u16 id)
 {
 	struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
-	struct dln2_event_cb_entry *i;
+	struct dln2_event_cb_entry *i, *entry = NULL;
 	unsigned long flags;
-	bool found = false;
 
 	spin_lock_irqsave(&dln2->event_cb_lock, flags);
 
 	list_for_each_entry(i, &dln2->event_cb_list, list) {
 		if (i->id == id) {
 			list_del_rcu(&i->list);
-			found = true;
+			entry = i;
 			break;
 		}
 	}
 
 	spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
 
-	if (found) {
+	if (entry) {
 		synchronize_rcu();
-		kfree(i);
+		kfree(entry);
 	}
 }
 EXPORT_SYMBOL(dln2_unregister_event_cb);
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v2] mfd: dln2: Clean up dln2_unregister_event_cb() loop logic

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

Streamline dln2_unregister_event_cb() by using a dedicated 'entry' pointer to store the matching event callback. This removes the need for the 'found' boolean flag, restricts the iterator scope, and matches the pattern used in dln2_register_event_cb().

Assisted-by coccinelle sparse

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#5 lore raw

Re: (subset) [PATCH v2] mfd: dln2: Clean up dln2_unregister_event_cb() loop logic

In reply to Riccardo Oliva
Quoted 4 lines · Riccardo Oliva

Streamline dln2_unregister_event_cb() by using a dedicated 'entry' pointer to store the matching event callback. This removes the need for the 'found' boolean flag, restricts the iterator scope, and matches the pattern used in dln2_register_event_cb().

Applied, thanks!

[1/1] mfd: dln2: Clean up dln2_unregister_event_cb() loop logic

      commit: dad66a38052d2481dd5aabad636b05652a328573

--
Lee Jones [李琼斯]