Skip to contentSkip to conversation

time/timgr: Fix wrong reference when level 0 group allocation failed

PATCH Superseded Levi Yun 6 participants 14 messages May 1, 2024 – May 8, 2024

Subsystems (2)
lore
Share & tools
lore

Levi Yun

author
#0 lore raw

PATCH 1/1 time/timgr: Fix wrong reference when level 0 group allocation failed

When tmigr_setup_groups() failed level 0 group allocation, next do while loop refers wrong local stack array location.

Changing group init do while loop with while loop to fix this problem.

---
 kernel/time/timer_migration.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/timer_migration.c +2 -2
index ccba875d2234..84413114db5c 100644
@@ -1596,7 +1596,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 
 	} while (i < tmigr_hierarchy_levels);
 
-	do {
+	while (i > 0) {
 		group = stack[--i];
 
 		if (err < 0) {
@@ -1645,7 +1645,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 				tmigr_connect_child_parent(child, group);
 			}
 		}
-	} while (i > 0);
+	}
 
 	kfree(stack);
 
Signature · 1 line
2.41.0
author
#1 lore raw

Gentle ping

Quoted 36 lines · Levi Yun

When tmigr_setup_groups() failed level 0 group allocation, next do while loop refers wrong local stack array location.

Changing group init do while loop with while loop to fix this problem.

---
 kernel/time/timer_migration.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/timer_migration.c +2 -3
index ccba875d2234..84413114db5c 100644
@@ -1596,7 +1596,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 
        } while (i < tmigr_hierarchy_levels);
 
-       do {
+       while (i > 0) {
                group = stack[--i];
 
                if (err < 0) {
@@ -1645,7 +1645,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
                                tmigr_connect_child_parent(child, group);
                        }
                }
-       } while (i > 0);
+       }
 
        kfree(stack);
 
--

2.41.0

#2 lore raw
In reply to Levi Yun
Quoted 1 line

Changing group init do while loop with while loop to fix this problem.

* Please convert such a change description to an improved imperative wording.

* Would you like to add the tag “Fixes” accordingly?

Regards,
Markus

author
#3 lore raw

PATCH RESEND 1/1 time/timgr: Fix wrong reference when level 0 group allocation failed

In reply to Levi Yun

When tmigr_setup_groups() failed level 0 group allocation,
wrong reference happens on local stack array while intializing timer hierarchy.

To prevent this, Check loop condition first before intializing timer hierarchy.

Fixes 7ee988770326 ("timers: Implement the hierarchical pull model")
---
 kernel/time/timer_migration.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/timer_migration.c +2 -2
index ccba875d2234..84413114db5c 100644
@@ -1596,7 +1596,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 
 	} while (i < tmigr_hierarchy_levels);
 
-	do {
+	while (i > 0) {
 		group = stack[--i];
 
 		if (err < 0) {
@@ -1645,7 +1645,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 				tmigr_connect_child_parent(child, group);
 			}
 		}
-	} while (i > 0);
+	}
 
 	kfree(stack);
 

--
2.41.0

#4 lore raw

…

Quoted 1 line

To prevent this, Check loop condition first before intializing timer hierarchy.

…

Quoted 1 line
Fixes 7ee988770326 ("timers: Implement the hierarchical pull model")

…

Does this change approach represent a subsequent patch version instead of a “RESEND”?

How do you think about to add a patch changelog accordingly?

Regards,
Markus

author
#5 lore raw

Hi Markus :)

Quoted 4 lines

Does this change approach represent a subsequent patch version instead of a “RESEND”?

How do you think about to add a patch changelog accordingly?

Thanks I'll do

Thanks.

author
#6 lore raw

PATCH v2 1/1 time/timgr: Fix wrong reference when level 0 group allocation failed

In reply to Levi Yun

When tmigr_setup_groups() failed level 0 group allocation,
wrong reference happens on local stack array while intializing timer hierarchy.

To prevent this, Check loop condition first before intializing timer hierarchy.

Fixes 7ee988770326 ("timers: Implement the hierarchical pull model")

---

v2:
- Modify commit message.

 kernel/time/timer_migration.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/timer_migration.c +2 -2
index ccba875d2234..84413114db5c 100644
@@ -1596,7 +1596,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 
 	} while (i < tmigr_hierarchy_levels);
 
-	do {
+	while (i > 0) {
 		group = stack[--i];
 
 		if (err < 0) {
@@ -1645,7 +1645,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 				tmigr_connect_child_parent(child, group);
 			}
 		}
-	} while (i > 0);
+	}
 
 	kfree(stack);
 

--
2.41.0

#7 lore raw

…

Quoted 1 line

To prevent this, Check loop condition first before intializing timer hierarchy.

I suggest to avoid another typo (also in the email address for Frederic Weisbecker).

Regards,
Markus

author
#8 lore raw

PATCH v3 1/1 time/timgr: Fix wrong reference when level 0 group allocation failed

In reply to Levi Yun

When tmigr_setup_groups() failed level 0 group allocation,
wrong reference happens on local stack array while intializing timer hierarchy.

To prevent this, Check loop condition first before initializing timer hierarchy.

Fixes 7ee988770326 ("timers: Implement the hierarchical pull model")

---
v3:
- Fix typo.

v2:
- Modify commit message.

 kernel/time/timer_migration.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/timer_migration.c +2 -2
index ccba875d2234..84413114db5c 100644
@@ -1596,7 +1596,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 
 	} while (i < tmigr_hierarchy_levels);
 
-	do {
+	while (i > 0) {
 		group = stack[--i];
 
 		if (err < 0) {
@@ -1645,7 +1645,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 				tmigr_connect_child_parent(child, group);
 			}
 		}
-	} while (i > 0);
+	}
 
 	kfree(stack);
 

--
2.41.0

#9 lore raw
Quoted 31 lines · Levi Yun

When tmigr_setup_groups() failed level 0 group allocation,
wrong reference happens on local stack array while intializing timer hierarchy.

To prevent this, Check loop condition first before initializing timer hierarchy.

Fixes 7ee988770326 ("timers: Implement the hierarchical pull model")

---
v3:
- Fix typo.

v2:
- Modify commit message.

 kernel/time/timer_migration.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/timer_migration.c +1 -1
index ccba875d2234..84413114db5c 100644
@@ -1596,7 +1596,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 
 	} while (i < tmigr_hierarchy_levels);
 
-	do {
+	while (i > 0) {
 		group = stack[--i];
 
 		if (err < 0) {

kernel/time/timer_migration.c static int tmigr_setup_groups(unsigned int cpu, unsigned int node) · line 1648

Quoted 5 lines
kernel/time/timer_migration.c +1 -1
@@ -1645,7 +1645,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 				tmigr_connect_child_parent(child, group);
 			}
 		}
-	} while (i > 0);
+	}

Looks good to me. But let's wait for Anna-Maria's second look. The group setup is not my favourite area...

Thanks.

Quoted 5 lines
kernel/time/timer_migration.c +0 -1
 	kfree(stack);
 
--

2.41.0

maintainer Reviewed-by
#10 lore raw
Quoted 30 lines · Frederic Weisbecker
Quoted 29 lines · Levi Yun

When tmigr_setup_groups() failed level 0 group allocation,
wrong reference happens on local stack array while intializing timer hierarchy.

To prevent this, Check loop condition first before initializing timer hierarchy.

Fixes 7ee988770326 ("timers: Implement the hierarchical pull model")

---
v3:
- Fix typo.

v2:
- Modify commit message.

 kernel/time/timer_migration.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/timer_migration.c +1 -1
index ccba875d2234..84413114db5c 100644
@@ -1596,7 +1596,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 
 	} while (i < tmigr_hierarchy_levels);
 
-	do {
+	while (i > 0) {
 		group = stack[--i];
 
Quoted 11 lines

kernel/time/timer_migration.c static int tmigr_setup_groups(unsigned int cpu, unsigned int node) · line 1648

Quoted 7 lines
kernel/time/timer_migration.c +1 -1
 		if (err < 0) {
@@ -1645,7 +1645,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 				tmigr_connect_child_parent(child, group);
 			}
 		}
-	} while (i > 0);
+	}

Looks good to me. But let's wait for Anna-Maria's second look. The group setup is not my favourite area...

Thanks for the fix!

#11 lore raw

…

Quoted 1 line
Quoted 1 line
Quoted 1 line

To prevent this, Check loop condition first before initializing timer hierarchy.

…

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

---
v3:
- Fix typo.

v2:
- Modify commit message.

 kernel/time/timer_migration.c | 4 ++--

…

Quoted 1 line

Do you find any remaining wording concerns less relevant fur such a changelog?

Regards,
Markus

merge bot Applied
#12 lore raw

[tip: timers/urgent] timers/migration: Prevent out of bounds access on failure

In reply to Levi Yun
The following commit has been merged into the timers/urgent branch of tip:

The following commit has been merged into the timers/urgent branch of tip:

Commit-ID:     d7ad05c86e2191bd66e5b62fca8da53c4a53484f
Gitweb:        https://git.kernel.org/tip/d7ad05c86e2191bd66e5b62fca8da53c4a53484f
Author:        Levi Yun <[email protected]>
AuthorDate:    Mon, 06 May 2024 05:10:59 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 08 May 2024 11:19:43 +02:00

timers/migration: Prevent out of bounds access on failure

When tmigr_setup_groups() fails the level 0 group allocation, then the cleanup derefences index -1 of the local stack array.

Prevent this by checking the loop condition first.

Fixes 7ee988770326 ("timers: Implement the hierarchical pull model")
---
 kernel/time/timer_migration.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/timer_migration.c +2 -2
index ccba875..8441311 100644
@@ -1596,7 +1596,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 
 	} while (i < tmigr_hierarchy_levels);
 
-	do {
+	while (i > 0) {
 		group = stack[--i];
 
 		if (err < 0) {
@@ -1645,7 +1645,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 				tmigr_connect_child_parent(child, group);
 			}
 		}
-	} while (i > 0);
+	}
 
 	kfree(stack);
 
#13 lore raw

Re: [tip: timers/urgent] timers/migration: Prevent out of bounds access on failure

Quoted 1 line · On Wed, May 08, 2024 at 09:45:41AM -0000, tip-bot2 for Levi Yun wrote:

The following commit has been merged into the timers/urgent branch of tip:

(Yes, I noted above)

...

kernel/time/timer_migration.c static int tmigr_setup_groups(unsigned int cpu, unsigned int node) · line 1600

Quoted 3 lines
kernel/time/timer_migration.c +1 -1
-	do {
+	while (i > 0) {
 		group = stack[--i];

Looking at this and most used patterns for cleanup loops, I would amend this to

	while (i--) {
		group = stack[i];

which seems to me an equivalent.

Quoted 7 lines
kernel/time/timer_migration.c +1 -1
 		if (err < 0) {
@@ -1645,7 +1645,7 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 				tmigr_connect_child_parent(child, group);
 			}
 		}
-	} while (i > 0);
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko