All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rcu: Fix buffer overlow in print_cpu_stall_info()
@ 2024-03-28 18:19 Nikita Kiryushin
  2024-03-29 17:43 ` Paul E. McKenney
  0 siblings, 1 reply; 10+ messages in thread
From: Nikita Kiryushin @ 2024-03-28 18:19 UTC (permalink / raw)
  To: Paul E. McKenney
  Cc: Nikita Kiryushin, Frederic Weisbecker, Neeraj Upadhyay,
	Joel Fernandes, Josh Triplett, Boqun Feng, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Zqiang, rcu, linux-kernel,
	lvc-project

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")
Signed-off-by: Nikita Kiryushin <kiryushin@ancud.ru>
---
 kernel/rcu/tree_stall.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h
index 5d666428546b..d4542c6e7c60 100644
--- a/kernel/rcu/tree_stall.h
+++ b/kernel/rcu/tree_stall.h
@@ -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)],
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] rcu: Fix buffer overlow in print_cpu_stall_info()
  2024-03-28 18:19 [PATCH] rcu: Fix buffer overlow in print_cpu_stall_info() Nikita Kiryushin
@ 2024-03-29 17:43 ` Paul E. McKenney
  2024-03-29 17:56   ` Nikita Kiryushin
  0 siblings, 1 reply; 10+ messages in thread
From: Paul E. McKenney @ 2024-03-29 17:43 UTC (permalink / raw)
  To: Nikita Kiryushin
  Cc: Frederic Weisbecker, Neeraj Upadhyay, Joel Fernandes,
	Josh Triplett, Boqun Feng, Steven Rostedt, Mathieu Desnoyers,
	Lai Jiangshan, Zqiang, rcu, linux-kernel, lvc-project

On Thu, Mar 28, 2024 at 09:19:14PM +0300, Nikita Kiryushin 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 (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

> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> Fixes: 245a62982502 ("rcu: Dump rcuc kthread status for CPUs not reporting quiescent state")
> Signed-off-by: Nikita Kiryushin <kiryushin@ancud.ru>
> ---
>  kernel/rcu/tree_stall.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h
> index 5d666428546b..d4542c6e7c60 100644
> --- a/kernel/rcu/tree_stall.h
> +++ b/kernel/rcu/tree_stall.h
> @@ -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)],
> -- 
> 2.34.1
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] rcu: Fix buffer overlow in print_cpu_stall_info()
  2024-03-29 17:43 ` Paul E. McKenney
@ 2024-03-29 17:56   ` Nikita Kiryushin
  2024-03-29 18:32     ` Steven Rostedt
  0 siblings, 1 reply; 10+ messages in thread
From: Nikita Kiryushin @ 2024-03-29 17:56 UTC (permalink / raw)
  To: paulmck
  Cc: Frederic Weisbecker, Neeraj Upadhyay, Joel Fernandes,
	Josh Triplett, Boqun Feng, Steven Rostedt, Mathieu Desnoyers,
	Lai Jiangshan, Zqiang, rcu, linux-kernel, lvc-project


Thank you for the feedback!
> 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.
> 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?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] rcu: Fix buffer overlow in print_cpu_stall_info()
  2024-03-29 17:56   ` Nikita Kiryushin
@ 2024-03-29 18:32     ` Steven Rostedt
  2024-03-29 22:21       ` Paul E. McKenney
  0 siblings, 1 reply; 10+ messages in thread
From: Steven Rostedt @ 2024-03-29 18:32 UTC (permalink / raw)
  To: Nikita Kiryushin
  Cc: paulmck, Frederic Weisbecker, Neeraj Upadhyay, Joel Fernandes,
	Josh Triplett, Boqun Feng, Mathieu Desnoyers, Lai Jiangshan,
	Zqiang, rcu, linux-kernel, lvc-project

On Fri, 29 Mar 2024 20:56:16 +0300
Nikita Kiryushin <kiryushin@ancud.ru> 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.

> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] rcu: Fix buffer overlow in print_cpu_stall_info()
  2024-03-29 18:32     ` Steven Rostedt
@ 2024-03-29 22:21       ` Paul E. McKenney
  2024-04-01 18:54         ` [PATCH v2] " Nikita Kiryushin
  0 siblings, 1 reply; 10+ messages in thread
From: Paul E. McKenney @ 2024-03-29 22:21 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Nikita Kiryushin, Frederic Weisbecker, Neeraj Upadhyay,
	Joel Fernandes, Josh Triplett, Boqun Feng, Mathieu Desnoyers,
	Lai Jiangshan, Zqiang, rcu, linux-kernel, lvc-project

On Fri, Mar 29, 2024 at 02:32:05PM -0400, Steven Rostedt wrote:
> On Fri, 29 Mar 2024 20:56:16 +0300
> Nikita Kiryushin <kiryushin@ancud.ru> 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.
> 
> > 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v2] rcu: Fix buffer overlow in print_cpu_stall_info()
  2024-03-29 22:21       ` Paul E. McKenney
@ 2024-04-01 18:54         ` Nikita Kiryushin
  2024-04-01 19:05           ` Steven Rostedt
  0 siblings, 1 reply; 10+ messages in thread
From: Nikita Kiryushin @ 2024-04-01 18:54 UTC (permalink / raw)
  To: Paul E. McKenney
  Cc: Nikita Kiryushin, Frederic Weisbecker, Neeraj Upadhyay,
	Joel Fernandes, Josh Triplett, Boqun Feng, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Zqiang, rcu, linux-kernel,
	lvc-project

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")
Signed-off-by: Nikita Kiryushin <kiryushin@ancud.ru>
---
v2: Remove signed to unsigned print format change as
Paul E. McKenney <paulmck@kernel.org> suggested, add format
intention clarification comment
 kernel/rcu/tree_stall.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h
index 5d666428546b..b972fe9f07a6 100644
--- a/kernel/rcu/tree_stall.h
+++ b/kernel/rcu/tree_stall.h
@@ -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)],
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] rcu: Fix buffer overlow in print_cpu_stall_info()
  2024-04-01 18:54         ` [PATCH v2] " Nikita Kiryushin
@ 2024-04-01 19:05           ` Steven Rostedt
  2024-04-01 19:43             ` [PATCH v3] " Nikita Kiryushin
  0 siblings, 1 reply; 10+ messages in thread
From: Steven Rostedt @ 2024-04-01 19:05 UTC (permalink / raw)
  To: Nikita Kiryushin
  Cc: Paul E. McKenney, Frederic Weisbecker, Neeraj Upadhyay,
	Joel Fernandes, Josh Triplett, Boqun Feng, Mathieu Desnoyers,
	Lai Jiangshan, Zqiang, rcu, linux-kernel, lvc-project

On Mon,  1 Apr 2024 21:54:54 +0300
Nikita Kiryushin <kiryushin@ancud.ru> wrote:

> --- a/kernel/rcu/tree_stall.h
> +++ b/kernel/rcu/tree_stall.h
> @@ -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 */


> +		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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v3] rcu: Fix buffer overlow in print_cpu_stall_info()
  2024-04-01 19:05           ` Steven Rostedt
@ 2024-04-01 19:43             ` Nikita Kiryushin
  2024-04-01 20:03               ` Steven Rostedt
  0 siblings, 1 reply; 10+ messages in thread
From: Nikita Kiryushin @ 2024-04-01 19:43 UTC (permalink / raw)
  To: Paul E. McKenney
  Cc: Nikita Kiryushin, Frederic Weisbecker, Neeraj Upadhyay,
	Joel Fernandes, Josh Triplett, Boqun Feng, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Zqiang, rcu, linux-kernel,
	lvc-project

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")
Signed-off-by: Nikita Kiryushin <kiryushin@ancud.ru>
---
v3: Change intention comment wording as
Steven Rostedt <rostedt@goodmis.org> suggested
v2: Remove signed to unsigned print format change as
Paul E. McKenney <paulmck@kernel.org> suggested, add format
intention clarification comment
 kernel/rcu/tree_stall.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h
index 5d666428546b..320440b8384e 100644
--- a/kernel/rcu/tree_stall.h
+++ b/kernel/rcu/tree_stall.h
@@ -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)],
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v3] rcu: Fix buffer overlow in print_cpu_stall_info()
  2024-04-01 19:43             ` [PATCH v3] " Nikita Kiryushin
@ 2024-04-01 20:03               ` Steven Rostedt
  2024-04-01 20:54                 ` Paul E. McKenney
  0 siblings, 1 reply; 10+ messages in thread
From: Steven Rostedt @ 2024-04-01 20:03 UTC (permalink / raw)
  To: Nikita Kiryushin
  Cc: Paul E. McKenney, Frederic Weisbecker, Neeraj Upadhyay,
	Joel Fernandes, Josh Triplett, Boqun Feng, Mathieu Desnoyers,
	Lai Jiangshan, Zqiang, rcu, linux-kernel, lvc-project

On Mon,  1 Apr 2024 22:43:15 +0300
Nikita Kiryushin <kiryushin@ancud.ru> 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")
> Signed-off-by: Nikita Kiryushin <kiryushin@ancud.ru>
> ---
> v3: Change intention comment wording as
> Steven Rostedt <rostedt@goodmis.org> suggested
> v2: Remove signed to unsigned print format change as
> Paul E. McKenney <paulmck@kernel.org> suggested, add format
> intention clarification comment
>  kernel/rcu/tree_stall.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h
> index 5d666428546b..320440b8384e 100644
> --- a/kernel/rcu/tree_stall.h
> +++ b/kernel/rcu/tree_stall.h
> @@ -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)],


Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>

-- Steve

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3] rcu: Fix buffer overlow in print_cpu_stall_info()
  2024-04-01 20:03               ` Steven Rostedt
@ 2024-04-01 20:54                 ` Paul E. McKenney
  0 siblings, 0 replies; 10+ messages in thread
From: Paul E. McKenney @ 2024-04-01 20:54 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Nikita Kiryushin, Frederic Weisbecker, Neeraj Upadhyay,
	Joel Fernandes, Josh Triplett, Boqun Feng, Mathieu Desnoyers,
	Lai Jiangshan, Zqiang, rcu, linux-kernel, lvc-project

On Mon, Apr 01, 2024 at 04:03:12PM -0400, Steven Rostedt wrote:
> On Mon,  1 Apr 2024 22:43:15 +0300
> Nikita Kiryushin <kiryushin@ancud.ru> 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")
> > Signed-off-by: Nikita Kiryushin <kiryushin@ancud.ru>
> > ---
> > v3: Change intention comment wording as
> > Steven Rostedt <rostedt@goodmis.org> suggested
> > v2: Remove signed to unsigned print format change as
> > Paul E. McKenney <paulmck@kernel.org> suggested, add format
> > intention clarification comment
> >  kernel/rcu/tree_stall.h | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h
> > index 5d666428546b..320440b8384e 100644
> > --- a/kernel/rcu/tree_stall.h
> > +++ b/kernel/rcu/tree_stall.h
> > @@ -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)],
> 
> 
> Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>

Queued for v6.10, thank you both!

							Thanx, Paul

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-04-01 20:54 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-28 18:19 [PATCH] rcu: Fix buffer overlow in print_cpu_stall_info() Nikita Kiryushin
2024-03-29 17:43 ` Paul E. McKenney
2024-03-29 17:56   ` Nikita Kiryushin
2024-03-29 18:32     ` Steven Rostedt
2024-03-29 22:21       ` Paul E. McKenney
2024-04-01 18:54         ` [PATCH v2] " Nikita Kiryushin
2024-04-01 19:05           ` Steven Rostedt
2024-04-01 19:43             ` [PATCH v3] " Nikita Kiryushin
2024-04-01 20:03               ` Steven Rostedt
2024-04-01 20:54                 ` Paul E. McKenney

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.