Skip to contentSkip to conversation

watchdog: avoid usage of iterator after loop

PATCH Superseded Jakob Koschel 2 participants 4 messages Mar 1, 2023 – Mar 2, 2023 Watchdog device drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 watchdog: avoid usage of iterator after loop

If potentially no valid element is found, 'p' would contain an invalid pointer past the iterator loop. To ensure 'p' is valid under any circumstances, the kfree() should be within the loop body.

Additionally, Linus proposed to avoid any use of the list iterator variable after the loop, in the attempt to move the list iterator variable declaration into the marcro to avoid any potential misuse after the loop [1].

---
 drivers/watchdog/watchdog_pretimeout.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/watchdog/watchdog_pretimeout.c +3 -3
index 376a495ab80c..d8c78696eaf5 100644
@@ -207,10 +207,10 @@ void watchdog_unregister_pretimeout(struct watchdog_device *wdd)
 	list_for_each_entry_safe(p, t, &pretimeout_list, entry) {
 		if (p->wdd == wdd) {
 			list_del(&p->entry);
-			break;
+			spin_unlock_irq(&pretimeout_lock);
+			kfree(p);
+			return;
 		}
 	}
 	spin_unlock_irq(&pretimeout_lock);
-
-	kfree(p);
 }

---
base-commit: c0927a7a5391f7d8e593e5e50ead7505a23cadf9
change-id: 20230301-watchdog-avoid-iter-after-loop-a197bf201435

Best regards,

Signature · 1 line
Jakob Koschel <[email protected]>
maintainer
#1 lore raw
Quoted 7 lines · Jakob Koschel

If potentially no valid element is found, 'p' would contain an invalid pointer past the iterator loop. To ensure 'p' is valid under any circumstances, the kfree() should be within the loop body.

Additionally, Linus proposed to avoid any use of the list iterator variable after the loop, in the attempt to move the list iterator variable declaration into the marcro to avoid any potential misuse after

macro

Quoted 13 lines

the loop [1].

---
  drivers/watchdog/watchdog_pretimeout.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/watchdog/watchdog_pretimeout.c +0 -0
index 376a495ab80c..d8c78696eaf5 100644

drivers/watchdog/watchdog_pretimeout.c void watchdog_unregister_pretimeout(struct watchdog_device *wdd) · line 212

Quoted 7 lines
drivers/watchdog/watchdog_pretimeout.c +3 -1
@@ -207,10 +207,10 @@ void watchdog_unregister_pretimeout(struct watchdog_device *wdd)
  	list_for_each_entry_safe(p, t, &pretimeout_list, entry) {
  		if (p->wdd == wdd) {
  			list_del(&p->entry);
-			break;
+			spin_unlock_irq(&pretimeout_lock);
+			kfree(p);
+			return;

Please just make it

			kfree(p);
			break;

There is no need to drop the spinlock here and/or to return directly.

Thanks,
Guenter

Quoted 12 lines
drivers/watchdog/watchdog_pretimeout.c +0 -3
  		}
  	}
  	spin_unlock_irq(&pretimeout_lock);
-
-	kfree(p);
  }
 
---

base-commit: c0927a7a5391f7d8e593e5e50ead7505a23cadf9
change-id: 20230301-watchdog-avoid-iter-after-loop-a197bf201435

Best regards,

#2 lore raw
Quoted 33 lines · Guenter Roeck
Quoted 7 lines · Jakob Koschel

If potentially no valid element is found, 'p' would contain an invalid pointer past the iterator loop. To ensure 'p' is valid under any circumstances, the kfree() should be within the loop body.

Additionally, Linus proposed to avoid any use of the list iterator variable after the loop, in the attempt to move the list iterator variable declaration into the marcro to avoid any potential misuse after

macro

Quoted 13 lines

the loop [1].

---
  drivers/watchdog/watchdog_pretimeout.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/watchdog/watchdog_pretimeout.c +0 -0
index 376a495ab80c..d8c78696eaf5 100644

drivers/watchdog/watchdog_pretimeout.c void watchdog_unregister_pretimeout(struct watchdog_device *wdd) · line 212

Quoted 7 lines
drivers/watchdog/watchdog_pretimeout.c +3 -1
@@ -207,10 +207,10 @@ void watchdog_unregister_pretimeout(struct watchdog_device *wdd)
  	list_for_each_entry_safe(p, t, &pretimeout_list, entry) {
  		if (p->wdd == wdd) {
  			list_del(&p->entry);
-			break;
+			spin_unlock_irq(&pretimeout_lock);
+			kfree(p);
+			return;

Please just make it

Quoted 5 lines
			kfree(p);
			break;

There is no need to drop the spinlock here and/or to return directly.

Ok great, I'll fix that in v2. I wasn't sure if something breaks if 'p' is released if the spinlock is still hold.

~ jakob

Quoted 17 lines

Thanks,
Guenter

Quoted 12 lines
drivers/watchdog/watchdog_pretimeout.c +0 -3
  		}
  	}
  	spin_unlock_irq(&pretimeout_lock);
-
-	kfree(p);
  }
 
---

base-commit: c0927a7a5391f7d8e593e5e50ead7505a23cadf9
change-id: 20230301-watchdog-avoid-iter-after-loop-a197bf201435

Best regards,

maintainer
#3 lore raw
Quoted 37 lines · Jakob Koschel
Quoted 36 lines · Guenter Roeck
Quoted 7 lines · Jakob Koschel

If potentially no valid element is found, 'p' would contain an invalid pointer past the iterator loop. To ensure 'p' is valid under any circumstances, the kfree() should be within the loop body.

Additionally, Linus proposed to avoid any use of the list iterator variable after the loop, in the attempt to move the list iterator variable declaration into the marcro to avoid any potential misuse after

macro

Quoted 13 lines

the loop [1].

---
   drivers/watchdog/watchdog_pretimeout.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
drivers/watchdog/watchdog_pretimeout.c +0 -0
index 376a495ab80c..d8c78696eaf5 100644

drivers/watchdog/watchdog_pretimeout.c void watchdog_unregister_pretimeout(struct watchdog_device *wdd) · line 212

Quoted 7 lines
drivers/watchdog/watchdog_pretimeout.c +3 -1
@@ -207,10 +207,10 @@ void watchdog_unregister_pretimeout(struct watchdog_device *wdd)
   	list_for_each_entry_safe(p, t, &pretimeout_list, entry) {
   		if (p->wdd == wdd) {
   			list_del(&p->entry);
-			break;
+			spin_unlock_irq(&pretimeout_lock);
+			kfree(p);
+			return;

Please just make it

			kfree(p);
			break;
Quoted 5 lines
Quoted 2 lines

There is no need to drop the spinlock here and/or to return directly.

Ok great, I'll fix that in v2. I wasn't sure if something breaks if 'p' is released if the spinlock is still hold.

Ah, interesting question. Looking into it, I don't think that is a problem. Just to be sure, I wrote a little coccinelle script to find calls to kfree() under spinlock_irq() and found several instances.

Guenter