mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] lib/stackdepot: off by one in depot_fetch_stack()
@ 2024-02-23 14:20 Dan Carpenter
  2024-02-28 17:10 ` Andrey Konovalov
  0 siblings, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2024-02-23 14:20 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Andrew Morton, Alexander Potapenko, linux-kernel, kernel-janitors

The stack_pools[] array has DEPOT_MAX_POOLS.  The "pools_num" tracks the
number of pools which are initialized.  See depot_init_pool() for more
details.

If pool_index == pools_num_cached, this will read one element beyond what
we want.  If not all the pools are initialized, then the pool will be
NULL, triggering a WARN(), and if they are all initialized it will read
one element beyond the end of the array.

Fixes: b29d31885814 ("lib/stackdepot: store free stack records in a freelist")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
From static analysis.  What seems to have happened is that originally
we stored the highest index instead of the number of elements and when
we changed the > to >= comparison was overlooked.

 lib/stackdepot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/stackdepot.c b/lib/stackdepot.c
index 8c795bb20afb..af6cc19a2003 100644
--- a/lib/stackdepot.c
+++ b/lib/stackdepot.c
@@ -447,7 +447,7 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle)
 
 	lockdep_assert_not_held(&pool_lock);
 
-	if (pool_index > pools_num_cached) {
+	if (pool_index >= pools_num_cached) {
 		WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n",
 		     pool_index, pools_num_cached, handle);
 		return NULL;
-- 
2.43.0


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

* Re: [PATCH] lib/stackdepot: off by one in depot_fetch_stack()
  2024-02-23 14:20 [PATCH] lib/stackdepot: off by one in depot_fetch_stack() Dan Carpenter
@ 2024-02-28 17:10 ` Andrey Konovalov
  2024-02-29  7:22   ` Dan Carpenter
  0 siblings, 1 reply; 4+ messages in thread
From: Andrey Konovalov @ 2024-02-28 17:10 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Andrew Morton, Alexander Potapenko, linux-kernel,
	kernel-janitors, Marco Elver

On Fri, Feb 23, 2024 at 3:20 PM Dan Carpenter <dan.carpenter@linaro.org> wrote:
>
> The stack_pools[] array has DEPOT_MAX_POOLS.  The "pools_num" tracks the
> number of pools which are initialized.  See depot_init_pool() for more
> details.
>
> If pool_index == pools_num_cached, this will read one element beyond what
> we want.  If not all the pools are initialized, then the pool will be
> NULL, triggering a WARN(), and if they are all initialized it will read
> one element beyond the end of the array.
>
> Fixes: b29d31885814 ("lib/stackdepot: store free stack records in a freelist")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---
> From static analysis.  What seems to have happened is that originally
> we stored the highest index instead of the number of elements and when
> we changed the > to >= comparison was overlooked.
>
>  lib/stackdepot.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/lib/stackdepot.c b/lib/stackdepot.c
> index 8c795bb20afb..af6cc19a2003 100644
> --- a/lib/stackdepot.c
> +++ b/lib/stackdepot.c
> @@ -447,7 +447,7 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle)
>
>         lockdep_assert_not_held(&pool_lock);
>
> -       if (pool_index > pools_num_cached) {
> +       if (pool_index >= pools_num_cached) {
>                 WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n",
>                      pool_index, pools_num_cached, handle);
>                 return NULL;
> --
> 2.43.0
>

Hi Dan,

This patch needs to be rebased onto "lib/stackdepot: Fix first entry
having a 0-handle", which is now in mm-stable.

Thank you!

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

* Re: [PATCH] lib/stackdepot: off by one in depot_fetch_stack()
  2024-02-28 17:10 ` Andrey Konovalov
@ 2024-02-29  7:22   ` Dan Carpenter
  2024-03-01 19:23     ` Andrey Konovalov
  0 siblings, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2024-02-29  7:22 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Andrew Morton, Alexander Potapenko, linux-kernel,
	kernel-janitors, Marco Elver

On Wed, Feb 28, 2024 at 06:10:31PM +0100, Andrey Konovalov wrote:
> On Fri, Feb 23, 2024 at 3:20 PM Dan Carpenter <dan.carpenter@linaro.org> wrote:
> >
> > The stack_pools[] array has DEPOT_MAX_POOLS.  The "pools_num" tracks the
> > number of pools which are initialized.  See depot_init_pool() for more
> > details.
> >
> > If pool_index == pools_num_cached, this will read one element beyond what
> > we want.  If not all the pools are initialized, then the pool will be
> > NULL, triggering a WARN(), and if they are all initialized it will read
> > one element beyond the end of the array.
> >
> > Fixes: b29d31885814 ("lib/stackdepot: store free stack records in a freelist")
> > Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> > ---
> > From static analysis.  What seems to have happened is that originally
> > we stored the highest index instead of the number of elements and when
> > we changed the > to >= comparison was overlooked.
> >
> >  lib/stackdepot.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/lib/stackdepot.c b/lib/stackdepot.c
> > index 8c795bb20afb..af6cc19a2003 100644
> > --- a/lib/stackdepot.c
> > +++ b/lib/stackdepot.c
> > @@ -447,7 +447,7 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle)
> >
> >         lockdep_assert_not_held(&pool_lock);
> >
> > -       if (pool_index > pools_num_cached) {
> > +       if (pool_index >= pools_num_cached) {
> >                 WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n",
> >                      pool_index, pools_num_cached, handle);
> >                 return NULL;
> > --
> > 2.43.0
> >
> 
> Hi Dan,
> 
> This patch needs to be rebased onto "lib/stackdepot: Fix first entry
> having a 0-handle", which is now in mm-stable.

I wrote it on top of that patch...  Backports will need to be adjusted
to handle it, I guess.  The "lib/stackdepot: fix first entry having a
0-handle" commit has this note:

    This bug has been lurking since the very beginning of stackdepot, but no
    one really cared as it seems.  Because of that I am not adding a Fixes
    tag.

I don't really know the code very well so I can't respond to that.

regards,
dan carpenter


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

* Re: [PATCH] lib/stackdepot: off by one in depot_fetch_stack()
  2024-02-29  7:22   ` Dan Carpenter
@ 2024-03-01 19:23     ` Andrey Konovalov
  0 siblings, 0 replies; 4+ messages in thread
From: Andrey Konovalov @ 2024-03-01 19:23 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Andrew Morton, Alexander Potapenko, linux-kernel,
	kernel-janitors, Marco Elver

On Thu, Feb 29, 2024 at 8:22 AM Dan Carpenter <dan.carpenter@linaro.org> wrote:
>
> I wrote it on top of that patch...

Ah, right, my bad.

> Backports will need to be adjusted
> to handle it, I guess.  The "lib/stackdepot: fix first entry having a
> 0-handle" commit has this note:
>
>     This bug has been lurking since the very beginning of stackdepot, but no
>     one really cared as it seems.  Because of that I am not adding a Fixes
>     tag.
>
> I don't really know the code very well so I can't respond to that.

Your patch looks good to me, thank you!

Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>

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

end of thread, other threads:[~2024-03-01 19:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-23 14:20 [PATCH] lib/stackdepot: off by one in depot_fetch_stack() Dan Carpenter
2024-02-28 17:10 ` Andrey Konovalov
2024-02-29  7:22   ` Dan Carpenter
2024-03-01 19:23     ` Andrey Konovalov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®