Skip to contentSkip to conversation

rcu: Fix buffer overlow in print_cpu_stall_info()

PATCH Superseded Nikita Kiryushin 3 participants 10 messages Mar 28, 2024 – Apr 1, 2024 RCU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rcu: Fix buffer overlow in print_cpu_stall_info()

rcuc info output in print_cpu_stall_info() contains posiible buffer overflow in the case of huge jiffies difference. The situation seems improbable, but, buffer overflow, still. Also, unsigned jiffies difference printed as (signed) %ld (which can be a bad format, if the values are huge).

Change sprintf to snprintf and change %ld to %lu in format.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 245a62982502 ("rcu: Dump rcuc kthread status for CPUs not reporting quiescent state")
---
 kernel/rcu/tree_stall.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/tree_stall.h +1 -1
index 5d666428546b..d4542c6e7c60 100644
@@ -504,7 +504,7 @@ static void print_cpu_stall_info(int cpu)
 			rcu_dynticks_in_eqs(rcu_dynticks_snap(cpu));
 	rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
 	if (rcuc_starved)
-		sprintf(buf, " rcuc=%ld jiffies(starved)", j);
+		snprintf(buf, sizeof(buf), " rcuc=%lu jiffies(starved)", j);
 	pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",
 	       cpu,
 	       "O."[!!cpu_online(cpu)],
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 8 lines · Nikita Kiryushin

rcuc info output in print_cpu_stall_info() contains posiible buffer overflow in the case of huge jiffies difference. The situation seems improbable, but, buffer overflow, still. Also, unsigned jiffies difference printed as (signed) %ld (which can be a bad format, if the values are huge).

Change sprintf to snprintf and change %ld to %lu in format.

Good catch!!!

However, the signed output is intentional. The idea is that if the timekeeping code is confused enough to run the jiffies counter backwards, we see a small negative number rather than a huge positive number. For example, -132 is immediately obvious, while the 64-bit unsigned equivalent of 18446744073709551484 might not be.

would you like to resend keeping the buffer-overflow fix but leaving out the signed-to-unsigned conversion?

Thanx, Paul

Quoted 24 lines

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 245a62982502 ("rcu: Dump rcuc kthread status for CPUs not reporting quiescent state")
---
 kernel/rcu/tree_stall.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/tree_stall.h +1 -1
index 5d666428546b..d4542c6e7c60 100644
@@ -504,7 +504,7 @@ static void print_cpu_stall_info(int cpu)
 			rcu_dynticks_in_eqs(rcu_dynticks_snap(cpu));
 	rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
 	if (rcuc_starved)
-		sprintf(buf, " rcuc=%ld jiffies(starved)", j);
+		snprintf(buf, sizeof(buf), " rcuc=%lu jiffies(starved)", j);
 	pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",
 	       cpu,
 	       "O."[!!cpu_online(cpu)],
Signature · 1 line
2.34.1
#2 lore raw

Thank you for the feedback!

Quoted 3 lines

would you like to resend keeping the buffer-overflow fix but leaving out the signed-to-unsigned conversion?

I will make a second version of the patch, without conversion as it is intentional.

Quoted 5 lines

However, the signed output is intentional. The idea is that if the timekeeping code is confused enough to run the jiffies counter backwards, we see a small negative number rather than a huge positive number. For example, -132 is immediately obvious, while the 64-bit unsigned equivalent of 18446744073709551484 might not be.

I had suspicions that was the case, however, I did not find the pointers in the code or in the commit message, that it was intentional, so I assumed a mistake.
Maybe, it would be a good idea for me to add a comment with intent clarification, to reduce possibility of the same confusion in the future, while I am at it? If so, should I do it in the same patch, or make a separate one?

#3 lore raw
Quoted 2 lines · On Fri, 29 Mar 2024 20:56:16 +0300 Nikita Kiryushin <[email protected]> wrote:

Maybe, it would be a good idea for me to add a comment with intent clarification, to reduce possibility of the same confusion in the future,

Yes please do.

Quoted 1 line

while I am at it? If so, should I do it in the same patch, or make a separate one?

I would keep it the same patch, but it really is Paul's decision.

-- Steve

maintainer
#4 lore raw
Quoted 11 lines · Steven Rostedt
Quoted 2 lines · On Fri, 29 Mar 2024 20:56:16 +0300 Nikita Kiryushin <[email protected]> wrote:

Maybe, it would be a good idea for me to add a comment with intent clarification, to reduce possibility of the same confusion in the future,

Yes please do.

Quoted 1 line

while I am at it? If so, should I do it in the same patch, or make a separate one?

I would keep it the same patch, but it really is Paul's decision.

I am with Steve on both questions.

Thanx, Paul

#5 lore raw

PATCH v2 1/1 rcu: Fix buffer overlow in print_cpu_stall_info()

rcuc info output in print_cpu_stall_info() contains possible buffer overflow in the case of huge jiffies difference. The situation seems improbable, but, buffer overflow, still.

Also, unsigned jiffies difference printed as (signed) %ld. This is intentional for debugging purposes, but it is not obvious from the code.

Change sprintf to snprintf and add clarifying comment about intention of %ld format.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 245a62982502 ("rcu: Dump rcuc kthread status for CPUs not reporting quiescent state")

---
v2: Remove signed to unsigned print format change as Paul E. McKenney <[email protected]> suggested, add format intention clarification comment

 kernel/rcu/tree_stall.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/rcu/tree_stall.h +2 -1
index 5d666428546b..b972fe9f07a6 100644
@@ -504,7 +504,8 @@ static void print_cpu_stall_info(int cpu)
 			rcu_dynticks_in_eqs(rcu_dynticks_snap(cpu));
 	rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
 	if (rcuc_starved)
-		sprintf(buf, " rcuc=%ld jiffies(starved)", j);
+		/* %ld is intentional, for easier bug detection */
+		snprintf(buf, sizeof(buf), " rcuc=%ld jiffies(starved)", j);
 	pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",
 	       cpu,
 	       "O."[!!cpu_online(cpu)],
Signature · 1 line
2.34.1
#6 lore raw

kernel/rcu/tree_stall.h static void print_cpu_stall_info(int cpu) · line 507

Quoted 8 lines · On Mon, 1 Apr 2024 21:54:54 +0300 Nikita Kiryushin <[email protected]> wrote:
kernel/rcu/tree_stall.h +1 -1
@@ -504,7 +504,8 @@ static void print_cpu_stall_info(int cpu)
 			rcu_dynticks_in_eqs(rcu_dynticks_snap(cpu));
 	rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
 	if (rcuc_starved)
-		sprintf(buf, " rcuc=%ld jiffies(starved)", j);
+		/* %ld is intentional, for easier bug detection */

The above still has assumptions of what is going on for the reviewer. I would suggest something a bit more obvious like:

		/* Print signed value, as negative means it is likely a bug */

kernel/rcu/tree_stall.h static void print_cpu_stall_info(int cpu) · line 509

Quoted 2 lines
kernel/rcu/tree_stall.h +1 -0
+		snprintf(buf, sizeof(buf), " rcuc=%ld jiffies(starved)", j);
 	pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",

-- Steve

#7 lore raw

PATCH v3 1/1 rcu: Fix buffer overlow in print_cpu_stall_info()

rcuc info output in print_cpu_stall_info() contains posiible buffer overflow in the case of huge jiffies difference. The situation seems improbable, but, buffer overflow, still.

Also, unsigned jiffies difference printed as (signed) %ld. This is intentional for debugging purposes, but it is not obvious from the code.

Change sprintf to snprintf and add clarifying comment about intention of %ld format.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 245a62982502 ("rcu: Dump rcuc kthread status for CPUs not reporting quiescent state")

---
v3: Change intention comment wording as
Steven Rostedt <[email protected]> suggested
v2: Remove signed to unsigned print format change as Paul E. McKenney <[email protected]> suggested, add format intention clarification comment

 kernel/rcu/tree_stall.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/rcu/tree_stall.h +2 -1
index 5d666428546b..320440b8384e 100644
@@ -504,7 +504,8 @@ static void print_cpu_stall_info(int cpu)
 			rcu_dynticks_in_eqs(rcu_dynticks_snap(cpu));
 	rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
 	if (rcuc_starved)
-		sprintf(buf, " rcuc=%ld jiffies(starved)", j);
+		/* Print signed value, as negative means it is likely a bug */
+		snprintf(buf, sizeof(buf), " rcuc=%ld jiffies(starved)", j);
 	pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",
 	       cpu,
 	       "O."[!!cpu_online(cpu)],
Signature · 1 line
2.34.1
reviewer Reviewed-by on v3 1/1
#8 lore raw
Quoted 30 lines · On Mon, 1 Apr 2024 22:43:15 +0300 Nikita Kiryushin <[email protected]> wrote:

rcuc info output in print_cpu_stall_info() contains posiible buffer overflow in the case of huge jiffies difference. The situation seems improbable, but, buffer overflow, still.

Also, unsigned jiffies difference printed as (signed) %ld. This is intentional for debugging purposes, but it is not obvious from the code.

Change sprintf to snprintf and add clarifying comment about intention of %ld format.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 245a62982502 ("rcu: Dump rcuc kthread status for CPUs not reporting quiescent state")

---
v3: Change intention comment wording as
Steven Rostedt <[email protected]> suggested
v2: Remove signed to unsigned print format change as Paul E. McKenney <[email protected]> suggested, add format intention clarification comment

 kernel/rcu/tree_stall.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/rcu/tree_stall.h +0 -0
index 5d666428546b..320440b8384e 100644

kernel/rcu/tree_stall.h static void print_cpu_stall_info(int cpu) · line 511

Quoted 9 lines
kernel/rcu/tree_stall.h +2 -1
@@ -504,7 +504,8 @@ static void print_cpu_stall_info(int cpu)
 			rcu_dynticks_in_eqs(rcu_dynticks_snap(cpu));
 	rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
 	if (rcuc_starved)
-		sprintf(buf, " rcuc=%ld jiffies(starved)", j);
+		/* Print signed value, as negative means it is likely a bug */
+		snprintf(buf, sizeof(buf), " rcuc=%ld jiffies(starved)", j);
 	pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",
 	       cpu,
 	       "O."[!!cpu_online(cpu)],

-- Steve

maintainer Applied
#9 lore raw
Quoted 37 lines · Steven Rostedt
Quoted 34 lines · On Mon, 1 Apr 2024 22:43:15 +0300 Nikita Kiryushin <[email protected]> wrote:

rcuc info output in print_cpu_stall_info() contains posiible buffer overflow in the case of huge jiffies difference. The situation seems improbable, but, buffer overflow, still.

Also, unsigned jiffies difference printed as (signed) %ld. This is intentional for debugging purposes, but it is not obvious from the code.

Change sprintf to snprintf and add clarifying comment about intention of %ld format.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 245a62982502 ("rcu: Dump rcuc kthread status for CPUs not reporting quiescent state")

---
v3: Change intention comment wording as
Steven Rostedt <[email protected]> suggested
v2: Remove signed to unsigned print format change as Paul E. McKenney <[email protected]> suggested, add format intention clarification comment

 kernel/rcu/tree_stall.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/rcu/tree_stall.h +0 -1
index 5d666428546b..320440b8384e 100644
@@ -504,7 +504,8 @@ static void print_cpu_stall_info(int cpu)
 			rcu_dynticks_in_eqs(rcu_dynticks_snap(cpu));
 	rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
 	if (rcuc_starved)
-		sprintf(buf, " rcuc=%ld jiffies(starved)", j);
Quoted 8 lines

kernel/rcu/tree_stall.h static void print_cpu_stall_info(int cpu) · line 511

Quoted 5 lines
kernel/rcu/tree_stall.h +2 -0
+		/* Print signed value, as negative means it is likely a bug */
+		snprintf(buf, sizeof(buf), " rcuc=%ld jiffies(starved)", j);
 	pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",
 	       cpu,
 	       "O."[!!cpu_online(cpu)],

Queued for v6.10, thank you both!

Thanx, Paul