2024-03-27 16:48:58

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH v2] rcu-tasks: Update show_rcu_tasks_trace_gp_kthread buffer size

On Wed, 27 Mar 2024 19:36:56 +0300
Nikita Kiryushin <[email protected]> wrote:

> diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
> index 147b5945d67a..963ecae3c8e6 100644
> --- a/kernel/rcu/tasks.h
> +++ b/kernel/rcu/tasks.h
> @@ -1994,7 +1994,7 @@ void show_rcu_tasks_trace_gp_kthread(void)
> {
> char buf[64];
>
> - sprintf(buf, "N%lu h:%lu/%lu/%lu",
> + snprintf(buf, ARRAY_SIZE(buf), "N%lu h:%lu/%lu/%lu",

Nit, but I would have used sizeof(buf) instead of ARRAY_SIZE(buf) as that's
more common practice for this type of code.

[ do a: git grep 'sizeof(buf)' ]

-- Steve

> data_race(n_trc_holdouts),
> data_race(n_heavy_reader_ofl_updates),
> data_race(n_heavy_reader_updates),


2024-03-27 18:26:30

by Nikita Kiryushin

[permalink] [raw]
Subject: [PATCH v4] rcu-tasks: Fix show_rcu_tasks_trace_gp_kthread buffer overflow

There is a possibility of buffer overflow in
show_rcu_tasks_trace_gp_kthread() if counters, passed
to sprintf() are huge. Counter numbers, needed for this
are unrealistically high, but buffer overflow is still
possible.

Use snprintf() with buffer size instead of sprintf().

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

Fixes: edf3775f0ad6 ("rcu-tasks: Add count for idle tasks on offline CPUs")
Signed-off-by: Nikita Kiryushin <[email protected]>
---
v4: Change ARRAY_SIZE to sizeof() as more idiomatic
as Steven Rostedt <[email protected]> suggested
v3: Fixed commit message
v2: Use snprintf() as
Steven Rostedt <[email protected]> suggested.
kernel/rcu/tasks.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
index 147b5945d67a..2a453de9f3d9 100644
--- a/kernel/rcu/tasks.h
+++ b/kernel/rcu/tasks.h
@@ -1994,7 +1994,7 @@ void show_rcu_tasks_trace_gp_kthread(void)
{
char buf[64];

- sprintf(buf, "N%lu h:%lu/%lu/%lu",
+ snprintf(buf, sizeof(buf), "N%lu h:%lu/%lu/%lu",
data_race(n_trc_holdouts),
data_race(n_heavy_reader_ofl_updates),
data_race(n_heavy_reader_updates),
--
2.34.1