LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] ipv4: tcp_input: fix stack out of bounds when parsing TCP options.
@ 2019-05-29  8:10 Young Xiao
  2019-05-29 14:20 ` Eric Dumazet
  2019-05-30 19:33 ` David Miller
  0 siblings, 2 replies; 5+ messages in thread
From: Young Xiao @ 2019-05-29  8:10 UTC (permalink / raw)
  To: edumazet, davem, kuznet, yoshfuji, netdev, linux-kernel; +Cc: Young Xiao

The TCP option parsing routines in tcp_parse_options function could
read one byte out of the buffer of the TCP options.

1         while (length > 0) {
2                 int opcode = *ptr++;
3                 int opsize;
4
5                 switch (opcode) {
6                 case TCPOPT_EOL:
7                         return;
8                 case TCPOPT_NOP:        /* Ref: RFC 793 section 3.1 */
9                         length--;
10                        continue;
11                default:
12                        opsize = *ptr++; //out of bound access

If length = 1, then there is an access in line2.
And another access is occurred in line 12.
This would lead to out-of-bound access.

Therefore, in the patch we check that the available data length is
larger enough to pase both TCP option code and size.

Signed-off-by: Young Xiao <92siuyang@gmail.com>
---
 net/ipv4/tcp_input.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 20f6fac..9775825 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -3791,6 +3791,8 @@ void tcp_parse_options(const struct net *net,
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize = *ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;
-- 
2.7.4


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

* Re: [PATCH] ipv4: tcp_input: fix stack out of bounds when parsing TCP options.
  2019-05-29  8:10 [PATCH] ipv4: tcp_input: fix stack out of bounds when parsing TCP options Young Xiao
@ 2019-05-29 14:20 ` Eric Dumazet
  2019-05-29 15:11   ` Yang Xiao
  2019-05-30 19:33 ` David Miller
  1 sibling, 1 reply; 5+ messages in thread
From: Eric Dumazet @ 2019-05-29 14:20 UTC (permalink / raw)
  To: Young Xiao
  Cc: David Miller, Alexey Kuznetsov, Hideaki YOSHIFUJI, netdev, LKML

On Wed, May 29, 2019 at 1:10 AM Young Xiao <92siuyang@gmail.com> wrote:
>
> The TCP option parsing routines in tcp_parse_options function could
> read one byte out of the buffer of the TCP options.
>
> 1         while (length > 0) {
> 2                 int opcode = *ptr++;
> 3                 int opsize;
> 4
> 5                 switch (opcode) {
> 6                 case TCPOPT_EOL:
> 7                         return;
> 8                 case TCPOPT_NOP:        /* Ref: RFC 793 section 3.1 */
> 9                         length--;
> 10                        continue;
> 11                default:
> 12                        opsize = *ptr++; //out of bound access
>
> If length = 1, then there is an access in line2.
> And another access is occurred in line 12.
> This would lead to out-of-bound access.
>
> Therefore, in the patch we check that the available data length is
> larger enough to pase both TCP option code and size.
>
> Signed-off-by: Young Xiao <92siuyang@gmail.com>
> ---
>  net/ipv4/tcp_input.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index 20f6fac..9775825 100644
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c
> @@ -3791,6 +3791,8 @@ void tcp_parse_options(const struct net *net,
>                         length--;
>                         continue;
>                 default:
> +                       if (length < 2)
> +                               return;
>                         opsize = *ptr++;
>                         if (opsize < 2) /* "silly options" */
>                                 return;

In practice we are good, since we have at least 320 bytes of room there,
and the test done later catches silly options.

if (opsize < 2) /* "silly options" */
    return;
if (opsize > length)   /* remember, opsize >= 2 here */
     return; /* don't parse partial options */

I guess adding yet another conditional will make this code obviously
correct for all eyes
and various tools.

Thanks.

Signed-off-by: Eric Dumazet <edumazet@google.com>

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

* Re: [PATCH] ipv4: tcp_input: fix stack out of bounds when parsing TCP options.
  2019-05-29 14:20 ` Eric Dumazet
@ 2019-05-29 15:11   ` Yang Xiao
  2019-05-29 15:44     ` Eric Dumazet
  0 siblings, 1 reply; 5+ messages in thread
From: Yang Xiao @ 2019-05-29 15:11 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: David Miller, Alexey Kuznetsov, Hideaki YOSHIFUJI, netdev, LKML

Indeed, condition opsize < 2 and opsize > length can deduce that length >= 2.
However, before the condition (if opsize < 2), there may be one-byte
out-of-bound access in line 12.
I'm not sure whether I have put it very clearly.

On Wed, May 29, 2019 at 10:20 PM Eric Dumazet <edumazet@google.com> wrote:
>
> On Wed, May 29, 2019 at 1:10 AM Young Xiao <92siuyang@gmail.com> wrote:
> >
> > The TCP option parsing routines in tcp_parse_options function could
> > read one byte out of the buffer of the TCP options.
> >
> > 1         while (length > 0) {
> > 2                 int opcode = *ptr++;
> > 3                 int opsize;
> > 4
> > 5                 switch (opcode) {
> > 6                 case TCPOPT_EOL:
> > 7                         return;
> > 8                 case TCPOPT_NOP:        /* Ref: RFC 793 section 3.1 */
> > 9                         length--;
> > 10                        continue;
> > 11                default:
> > 12                        opsize = *ptr++; //out of bound access
> >
> > If length = 1, then there is an access in line2.
> > And another access is occurred in line 12.
> > This would lead to out-of-bound access.
> >
> > Therefore, in the patch we check that the available data length is
> > larger enough to pase both TCP option code and size.
> >
> > Signed-off-by: Young Xiao <92siuyang@gmail.com>
> > ---
> >  net/ipv4/tcp_input.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> > index 20f6fac..9775825 100644
> > --- a/net/ipv4/tcp_input.c
> > +++ b/net/ipv4/tcp_input.c
> > @@ -3791,6 +3791,8 @@ void tcp_parse_options(const struct net *net,
> >                         length--;
> >                         continue;
> >                 default:
> > +                       if (length < 2)
> > +                               return;
> >                         opsize = *ptr++;
> >                         if (opsize < 2) /* "silly options" */
> >                                 return;
>
> In practice we are good, since we have at least 320 bytes of room there,
> and the test done later catches silly options.
>
> if (opsize < 2) /* "silly options" */
>     return;
> if (opsize > length)   /* remember, opsize >= 2 here */
>      return; /* don't parse partial options */
>
> I guess adding yet another conditional will make this code obviously
> correct for all eyes
> and various tools.
>
> Thanks.
>
> Signed-off-by: Eric Dumazet <edumazet@google.com>



-- 
Best regards!

Young
-----------------------------------------------------------

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

* Re: [PATCH] ipv4: tcp_input: fix stack out of bounds when parsing TCP options.
  2019-05-29 15:11   ` Yang Xiao
@ 2019-05-29 15:44     ` Eric Dumazet
  0 siblings, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2019-05-29 15:44 UTC (permalink / raw)
  To: Yang Xiao; +Cc: David Miller, Alexey Kuznetsov, Hideaki YOSHIFUJI, netdev, LKML

On Wed, May 29, 2019 at 8:11 AM Yang Xiao <92siuyang@gmail.com> wrote:
>
> Indeed, condition opsize < 2 and opsize > length can deduce that length >= 2.
> However, before the condition (if opsize < 2), there may be one-byte
> out-of-bound access in line 12.
> I'm not sure whether I have put it very clearly.

Maybe I should have been clear about the 320 bytes we have at the end
of skb->head

This is the struct skb_shared_info

So reading one byte, 'out-of-bound' here is harmless.

Whatever value is read, we will return early without ever looking at a
following byte.


>
> On Wed, May 29, 2019 at 10:20 PM Eric Dumazet <edumazet@google.com> wrote:
> >
> > On Wed, May 29, 2019 at 1:10 AM Young Xiao <92siuyang@gmail.com> wrote:
> > >
> > > The TCP option parsing routines in tcp_parse_options function could
> > > read one byte out of the buffer of the TCP options.
> > >
> > > 1         while (length > 0) {
> > > 2                 int opcode = *ptr++;
> > > 3                 int opsize;
> > > 4
> > > 5                 switch (opcode) {
> > > 6                 case TCPOPT_EOL:
> > > 7                         return;
> > > 8                 case TCPOPT_NOP:        /* Ref: RFC 793 section 3.1 */
> > > 9                         length--;
> > > 10                        continue;
> > > 11                default:
> > > 12                        opsize = *ptr++; //out of bound access
> > >
> > > If length = 1, then there is an access in line2.
> > > And another access is occurred in line 12.
> > > This would lead to out-of-bound access.
> > >
> > > Therefore, in the patch we check that the available data length is
> > > larger enough to pase both TCP option code and size.
> > >
> > > Signed-off-by: Young Xiao <92siuyang@gmail.com>
> > > ---
> > >  net/ipv4/tcp_input.c | 2 ++
> > >  1 file changed, 2 insertions(+)
> > >
> > > diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> > > index 20f6fac..9775825 100644
> > > --- a/net/ipv4/tcp_input.c
> > > +++ b/net/ipv4/tcp_input.c
> > > @@ -3791,6 +3791,8 @@ void tcp_parse_options(const struct net *net,
> > >                         length--;
> > >                         continue;
> > >                 default:
> > > +                       if (length < 2)
> > > +                               return;
> > >                         opsize = *ptr++;
> > >                         if (opsize < 2) /* "silly options" */
> > >                                 return;
> >
> > In practice we are good, since we have at least 320 bytes of room there,
> > and the test done later catches silly options.
> >
> > if (opsize < 2) /* "silly options" */
> >     return;
> > if (opsize > length)   /* remember, opsize >= 2 here */
> >      return; /* don't parse partial options */
> >
> > I guess adding yet another conditional will make this code obviously
> > correct for all eyes
> > and various tools.
> >
> > Thanks.
> >
> > Signed-off-by: Eric Dumazet <edumazet@google.com>
>
>
>
> --
> Best regards!
>
> Young
> -----------------------------------------------------------

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

* Re: [PATCH] ipv4: tcp_input: fix stack out of bounds when parsing TCP options.
  2019-05-29  8:10 [PATCH] ipv4: tcp_input: fix stack out of bounds when parsing TCP options Young Xiao
  2019-05-29 14:20 ` Eric Dumazet
@ 2019-05-30 19:33 ` David Miller
  1 sibling, 0 replies; 5+ messages in thread
From: David Miller @ 2019-05-30 19:33 UTC (permalink / raw)
  To: 92siuyang; +Cc: edumazet, kuznet, yoshfuji, netdev, linux-kernel

From: Young Xiao <92siuyang@gmail.com>
Date: Wed, 29 May 2019 16:10:59 +0800

> The TCP option parsing routines in tcp_parse_options function could
> read one byte out of the buffer of the TCP options.
> 
> 1         while (length > 0) {
> 2                 int opcode = *ptr++;
> 3                 int opsize;
> 4
> 5                 switch (opcode) {
> 6                 case TCPOPT_EOL:
> 7                         return;
> 8                 case TCPOPT_NOP:        /* Ref: RFC 793 section 3.1 */
> 9                         length--;
> 10                        continue;
> 11                default:
> 12                        opsize = *ptr++; //out of bound access
> 
> If length = 1, then there is an access in line2.
> And another access is occurred in line 12.
> This would lead to out-of-bound access.
> 
> Therefore, in the patch we check that the available data length is
> larger enough to pase both TCP option code and size.
> 
> Signed-off-by: Young Xiao <92siuyang@gmail.com>

Applied.

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

end of thread, other threads:[~2019-05-30 19:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-29  8:10 [PATCH] ipv4: tcp_input: fix stack out of bounds when parsing TCP options Young Xiao
2019-05-29 14:20 ` Eric Dumazet
2019-05-29 15:11   ` Yang Xiao
2019-05-29 15:44     ` Eric Dumazet
2019-05-30 19:33 ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).