All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC net] net: skbuff: fix stack variable out of bounds access
@ 2021-03-23 12:52 Arnd Bergmann
  2021-03-23 14:42 ` Willem de Bruijn
  0 siblings, 1 reply; 4+ messages in thread
From: Arnd Bergmann @ 2021-03-23 12:52 UTC (permalink / raw)
  To: David S. Miller, Jakub Kicinski, Willem de Bruijn
  Cc: Arnd Bergmann, Alexander Lobakin, Jonathan Lemon, Miaohe Lin,
	Guillaume Nault, linux-kernel, netdev

From: Arnd Bergmann <arnd@arndb.de>

gcc-11 warns that the TS_SKB_CB(&state)) cast in skb_find_text()
leads to an out-of-bounds access in skb_prepare_seq_read() after
the addition of a new struct member made skb_seq_state longer
than ts_state:

net/core/skbuff.c: In function ‘skb_find_text’:
net/core/skbuff.c:3498:26: error: array subscript ‘struct skb_seq_state[0]’ is partly outside array bounds of ‘struct ts_state[1]’ [-Werror=array-bounds]
 3498 |         st->lower_offset = from;
      |         ~~~~~~~~~~~~~~~~~^~~~~~
net/core/skbuff.c:3659:25: note: while referencing ‘state’
 3659 |         struct ts_state state;
      |                         ^~~~~

The warning is currently disabled globally, but I found this
instance during experimental build testing, and it seems
legitimate.

Make the textsearch buffer longer and add a compile-time check to
ensure the two remain the same length.

Fixes: 97550f6fa592 ("net: compound page support in skb_seq_read")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 include/linux/textsearch.h | 2 +-
 net/core/skbuff.c          | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/textsearch.h b/include/linux/textsearch.h
index 13770cfe33ad..6673e4d4ac2e 100644
--- a/include/linux/textsearch.h
+++ b/include/linux/textsearch.h
@@ -23,7 +23,7 @@ struct ts_config;
 struct ts_state
 {
 	unsigned int		offset;
-	char			cb[40];
+	char			cb[48];
 };
 
 /**
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 545a472273a5..dd10d4c5f4bf 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -3633,6 +3633,7 @@ static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
 					  struct ts_config *conf,
 					  struct ts_state *state)
 {
+	BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state->cb));
 	return skb_seq_read(offset, text, TS_SKB_CB(state));
 }
 
-- 
2.29.2


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

* Re: [RFC net] net: skbuff: fix stack variable out of bounds access
  2021-03-23 12:52 [RFC net] net: skbuff: fix stack variable out of bounds access Arnd Bergmann
@ 2021-03-23 14:42 ` Willem de Bruijn
  2021-03-23 16:29   ` Arnd Bergmann
  0 siblings, 1 reply; 4+ messages in thread
From: Willem de Bruijn @ 2021-03-23 14:42 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: David S. Miller, Jakub Kicinski, Arnd Bergmann,
	Alexander Lobakin, Jonathan Lemon, Miaohe Lin, Guillaume Nault,
	linux-kernel, Network Development

On Tue, Mar 23, 2021 at 8:52 AM Arnd Bergmann <arnd@kernel.org> wrote:
>
> From: Arnd Bergmann <arnd@arndb.de>
>
> gcc-11 warns that the TS_SKB_CB(&state)) cast in skb_find_text()
> leads to an out-of-bounds access in skb_prepare_seq_read() after
> the addition of a new struct member made skb_seq_state longer
> than ts_state:
>
> net/core/skbuff.c: In function ‘skb_find_text’:
> net/core/skbuff.c:3498:26: error: array subscript ‘struct skb_seq_state[0]’ is partly outside array bounds of ‘struct ts_state[1]’ [-Werror=array-bounds]
>  3498 |         st->lower_offset = from;
>       |         ~~~~~~~~~~~~~~~~~^~~~~~
> net/core/skbuff.c:3659:25: note: while referencing ‘state’
>  3659 |         struct ts_state state;
>       |                         ^~~~~
>
> The warning is currently disabled globally, but I found this
> instance during experimental build testing, and it seems
> legitimate.
>
> Make the textsearch buffer longer and add a compile-time check to
> ensure the two remain the same length.
>
> Fixes: 97550f6fa592 ("net: compound page support in skb_seq_read")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  include/linux/textsearch.h | 2 +-
>  net/core/skbuff.c          | 1 +
>  2 files changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/textsearch.h b/include/linux/textsearch.h
> index 13770cfe33ad..6673e4d4ac2e 100644
> --- a/include/linux/textsearch.h
> +++ b/include/linux/textsearch.h
> @@ -23,7 +23,7 @@ struct ts_config;
>  struct ts_state
>  {
>         unsigned int            offset;
> -       char                    cb[40];
> +       char                    cb[48];
>  };
>
>  /**
> diff --git a/net/core/skbuff.c b/net/core/skbuff.c
> index 545a472273a5..dd10d4c5f4bf 100644
> --- a/net/core/skbuff.c
> +++ b/net/core/skbuff.c
> @@ -3633,6 +3633,7 @@ static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
>                                           struct ts_config *conf,
>                                           struct ts_state *state)
>  {
> +       BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state->cb));
>         return skb_seq_read(offset, text, TS_SKB_CB(state));
>  }
>
> --
> 2.29.2
>

Thanks for addressing this.

A similar fix already landed in 5.12-rc3: commit b228c9b05876 ("net:
expand textsearch ts_state to fit skb_seq_state"). That fix landed in
5.12-rc3.

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

* Re: [RFC net] net: skbuff: fix stack variable out of bounds access
  2021-03-23 14:42 ` Willem de Bruijn
@ 2021-03-23 16:29   ` Arnd Bergmann
  2021-03-23 17:14     ` Willem de Bruijn
  0 siblings, 1 reply; 4+ messages in thread
From: Arnd Bergmann @ 2021-03-23 16:29 UTC (permalink / raw)
  To: Willem de Bruijn
  Cc: David S. Miller, Jakub Kicinski, Alexander Lobakin,
	Jonathan Lemon, Miaohe Lin, Guillaume Nault, linux-kernel,
	Network Development

On Tue, Mar 23, 2021 at 3:42 PM Willem de Bruijn
<willemdebruijn.kernel@gmail.com> wrote:
>
> On Tue, Mar 23, 2021 at 8:52 AM Arnd Bergmann <arnd@kernel.org> wrote:
> >>
> A similar fix already landed in 5.12-rc3: commit b228c9b05876 ("net:
> expand textsearch ts_state to fit skb_seq_state"). That fix landed in
> 5.12-rc3.

Ah nice, even the same BUILD_BUG_ON() ;-)

Too bad it had to be found through runtime testing when it could have been
found by the compiler warning.

       Arnd

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

* Re: [RFC net] net: skbuff: fix stack variable out of bounds access
  2021-03-23 16:29   ` Arnd Bergmann
@ 2021-03-23 17:14     ` Willem de Bruijn
  0 siblings, 0 replies; 4+ messages in thread
From: Willem de Bruijn @ 2021-03-23 17:14 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Willem de Bruijn, David S. Miller, Jakub Kicinski,
	Alexander Lobakin, Jonathan Lemon, Miaohe Lin, Guillaume Nault,
	linux-kernel, Network Development

On Tue, Mar 23, 2021 at 12:30 PM Arnd Bergmann <arnd@kernel.org> wrote:
>
> On Tue, Mar 23, 2021 at 3:42 PM Willem de Bruijn
> <willemdebruijn.kernel@gmail.com> wrote:
> >
> > On Tue, Mar 23, 2021 at 8:52 AM Arnd Bergmann <arnd@kernel.org> wrote:
> > >>
> > A similar fix already landed in 5.12-rc3: commit b228c9b05876 ("net:
> > expand textsearch ts_state to fit skb_seq_state"). That fix landed in
> > 5.12-rc3.
>
> Ah nice, even the same BUILD_BUG_ON() ;-)

Indeed :) Sorry that your work ended up essentially reproducing that.

> Too bad it had to be found through runtime testing when it could have been
> found by the compiler warning.

Definitely useful. Had I enabled it, it would have saved me a lot of debug time.

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

end of thread, other threads:[~2021-03-23 17:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-23 12:52 [RFC net] net: skbuff: fix stack variable out of bounds access Arnd Bergmann
2021-03-23 14:42 ` Willem de Bruijn
2021-03-23 16:29   ` Arnd Bergmann
2021-03-23 17:14     ` Willem de Bruijn

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.