* [PATCH] ftrace: Use flexible array for hash buckets
@ 2026-05-20 22:00 Rosen Penev
2026-05-21 1:28 ` Steven Rostedt
0 siblings, 1 reply; 3+ messages in thread
From: Rosen Penev @ 2026-05-20 22:00 UTC (permalink / raw)
To: linux-trace-kernel
Cc: Steven Rostedt, Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers,
open list:FUNCTION HOOKS (FTRACE)
Store ftrace hash buckets in the ftrace_hash allocation instead of
allocating the bucket array separately.
This keeps the bucket storage tied to the hash lifetime and simplifies
the allocation and cleanup paths.
Assisted-by: Codex:GPT-5.5
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
kernel/trace/ftrace.c | 17 ++---------------
kernel/trace/trace.h | 2 +-
2 files changed, 3 insertions(+), 16 deletions(-)
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index b2611de3f594..25a9dca290dd 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -1082,10 +1082,7 @@ struct ftrace_func_probe {
* it all the time. These are in a read only section such that if
* anyone does try to modify it, it will cause an exception.
*/
-static const struct hlist_head empty_buckets[1];
-static const struct ftrace_hash empty_hash = {
- .buckets = (struct hlist_head *)empty_buckets,
-};
+static const struct ftrace_hash empty_hash = {};
#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
struct ftrace_ops global_ops = {
@@ -1295,7 +1292,6 @@ void free_ftrace_hash(struct ftrace_hash *hash)
if (!hash || hash == EMPTY_HASH)
return;
ftrace_hash_clear(hash);
- kfree(hash->buckets);
kfree(hash);
}
@@ -1333,20 +1329,11 @@ EXPORT_SYMBOL_GPL(ftrace_free_filter);
struct ftrace_hash *alloc_ftrace_hash(int size_bits)
{
struct ftrace_hash *hash;
- int size;
- hash = kzalloc_obj(*hash);
+ hash = kzalloc_flex(*hash, buckets, BIT(size_bits));
if (!hash)
return NULL;
- size = 1 << size_bits;
- hash->buckets = kzalloc_objs(*hash->buckets, size);
-
- if (!hash->buckets) {
- kfree(hash);
- return NULL;
- }
-
hash->size_bits = size_bits;
return hash;
diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
index 80fe152af1dd..5a3f81f17317 100644
--- a/kernel/trace/trace.h
+++ b/kernel/trace/trace.h
@@ -1000,10 +1000,10 @@ enum {
struct ftrace_hash {
unsigned long size_bits;
- struct hlist_head *buckets;
unsigned long count;
unsigned long flags;
struct rcu_head rcu;
+ struct hlist_head buckets[];
};
struct ftrace_func_entry *
--
2.54.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] ftrace: Use flexible array for hash buckets
2026-05-20 22:00 [PATCH] ftrace: Use flexible array for hash buckets Rosen Penev
@ 2026-05-21 1:28 ` Steven Rostedt
2026-05-21 1:39 ` Rosen Penev
0 siblings, 1 reply; 3+ messages in thread
From: Steven Rostedt @ 2026-05-21 1:28 UTC (permalink / raw)
To: Rosen Penev
Cc: linux-trace-kernel, Masami Hiramatsu, Mark Rutland,
Mathieu Desnoyers, open list:FUNCTION HOOKS (FTRACE), sashiko-bot,
sashiko-reviews
On Wed, 20 May 2026 15:00:30 -0700
Rosen Penev <rosenp@gmail.com> wrote:
> Store ftrace hash buckets in the ftrace_hash allocation instead of
> allocating the bucket array separately.
>
> This keeps the bucket storage tied to the hash lifetime and simplifies
> the allocation and cleanup paths.
>
> Assisted-by: Codex:GPT-5.5
I'll let the AI's duke it out!
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
> kernel/trace/ftrace.c | 17 ++---------------
> kernel/trace/trace.h | 2 +-
> 2 files changed, 3 insertions(+), 16 deletions(-)
>
> diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
> index b2611de3f594..25a9dca290dd 100644
> --- a/kernel/trace/ftrace.c
> +++ b/kernel/trace/ftrace.c
> @@ -1082,10 +1082,7 @@ struct ftrace_func_probe {
> * it all the time. These are in a read only section such that if
> * anyone does try to modify it, it will cause an exception.
> */
> -static const struct hlist_head empty_buckets[1];
> -static const struct ftrace_hash empty_hash = {
> - .buckets = (struct hlist_head *)empty_buckets,
> -};
> +static const struct ftrace_hash empty_hash = {};
> #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
According to Sashiko: https://sashiko.dev/#/patchset/20260520220030.16887-1-rosenp%40gmail.com
Could this conversion to a flexible array member cause an
out-of-bounds read when iterating over the empty hash? Because
empty_hash is now initialized as an empty struct, its flexible array
member buckets has a size of 0. However, empty_hash.size_bits is 0,
which means loop limits computing '1 << hash->size_bits' will
evaluate to 1. If functions like
prepare_direct_functions_for_ipmodify() iterate over a default
EMPTY_HASH without checking ftrace_hash_empty(), they will attempt
to read EMPTY_HASH->buckets[0]. This reads past the end of the
struct into adjacent memory in the .rodata section. If that adjacent
memory happens to be non-zero, the linked list loop could
dereference it and cause a kernel panic. Prior to this patch,
empty_buckets provided a safely zeroed array of size 1 to handle
this single iteration.
-- Steve
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] ftrace: Use flexible array for hash buckets
2026-05-21 1:28 ` Steven Rostedt
@ 2026-05-21 1:39 ` Rosen Penev
0 siblings, 0 replies; 3+ messages in thread
From: Rosen Penev @ 2026-05-21 1:39 UTC (permalink / raw)
To: Steven Rostedt
Cc: linux-trace-kernel, Masami Hiramatsu, Mark Rutland,
Mathieu Desnoyers, open list:FUNCTION HOOKS (FTRACE), sashiko-bot,
sashiko-reviews
On Wed, May 20, 2026 at 6:28 PM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Wed, 20 May 2026 15:00:30 -0700
> Rosen Penev <rosenp@gmail.com> wrote:
>
> > Store ftrace hash buckets in the ftrace_hash allocation instead of
> > allocating the bucket array separately.
> >
> > This keeps the bucket storage tied to the hash lifetime and simplifies
> > the allocation and cleanup paths.
> >
> > Assisted-by: Codex:GPT-5.5
>
> I'll let the AI's duke it out!
>
> > Signed-off-by: Rosen Penev <rosenp@gmail.com>
> > ---
> > kernel/trace/ftrace.c | 17 ++---------------
> > kernel/trace/trace.h | 2 +-
> > 2 files changed, 3 insertions(+), 16 deletions(-)
> >
> > diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
> > index b2611de3f594..25a9dca290dd 100644
> > --- a/kernel/trace/ftrace.c
> > +++ b/kernel/trace/ftrace.c
> > @@ -1082,10 +1082,7 @@ struct ftrace_func_probe {
> > * it all the time. These are in a read only section such that if
> > * anyone does try to modify it, it will cause an exception.
> > */
> > -static const struct hlist_head empty_buckets[1];
> > -static const struct ftrace_hash empty_hash = {
> > - .buckets = (struct hlist_head *)empty_buckets,
> > -};
> > +static const struct ftrace_hash empty_hash = {};
> > #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
>
>
> According to Sashiko: https://sashiko.dev/#/patchset/20260520220030.16887-1-rosenp%40gmail.com
>
> Could this conversion to a flexible array member cause an
> out-of-bounds read when iterating over the empty hash? Because
> empty_hash is now initialized as an empty struct, its flexible array
> member buckets has a size of 0. However, empty_hash.size_bits is 0,
> which means loop limits computing '1 << hash->size_bits' will
> evaluate to 1. If functions like
> prepare_direct_functions_for_ipmodify() iterate over a default
> EMPTY_HASH without checking ftrace_hash_empty(), they will attempt
> to read EMPTY_HASH->buckets[0]. This reads past the end of the
> struct into adjacent memory in the .rodata section. If that adjacent
> memory happens to be non-zero, the linked list loop could
> dereference it and cause a kernel panic. Prior to this patch,
> empty_buckets provided a safely zeroed array of size 1 to handle
> this single iteration.
Yeah this looks right. Might as well abandon.
>
> -- Steve
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-05-21 1:39 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-20 22:00 [PATCH] ftrace: Use flexible array for hash buckets Rosen Penev
2026-05-21 1:28 ` Steven Rostedt
2026-05-21 1:39 ` Rosen Penev
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox