Skip to contentSkip to conversation

net: check the length of the data before dereferencing it

PATCH In review Changli Gao 3 participants 16 messages Apr 2, 2012

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: check the length of the data before dereferencing it

We should check the length of the data before dereferencing it when parsing the TCP options.

---
 net/ipv4/tcp_input.c |    2 ++
 1 file changed, 2 insertions(+)
net/ipv4/tcp_input.c +2 -0
index e886e2f..5099f08 100644
@@ -3845,6 +3845,8 @@ void tcp_parse_options(const struct sk_buff *skb, struct tcp_options_received *o
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize = *ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;
Acked-by on v1 1/1
#1 lore raw
Quoted 12 lines · Changli Gao

We should check the length of the data before dereferencing it when parsing the TCP options.

---
 net/ipv4/tcp_input.c |    2 ++
 1 file changed, 2 insertions(+)
net/ipv4/tcp_input.c +0 -0
index e886e2f..5099f08 100644

net/ipv4/tcp_input.c void tcp_parse_options(const struct sk_buff *skb, struct tcp_options_received *o · line 3852

Quoted 8 lines
net/ipv4/tcp_input.c +2 -0
@@ -3845,6 +3845,8 @@ void tcp_parse_options(const struct sk_buff *skb, struct tcp_options_received *o
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize = *ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;
maintainer
#2 lore raw

From: Eric Dumazet <[email protected]>
Date: Mon, 02 Apr 2012 05:19:33 +0200

Quoted 11 lines

net/ipv4/tcp_input.c void tcp_parse_options(const struct sk_buff *skb, struct tcp_options_received *o · line 3852

Quoted 9 lines
net/ipv4/tcp_input.c +2 -0
@@ -3845,6 +3845,8 @@ void tcp_parse_options(const struct sk_buff *skb, struct tcp_options_received *o
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize = *ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;

Tag Eric, you're it.

You ACK'd this patch, so you get to show how this is actually able to cause some kind of problem.

I assert that this is adding a useless test, that doesn't fix any kind of possible crash or misbehavior. If length == 1 at the default:, the code will absolutely do the right thing.

Prove me wrong.

#3 lore raw
Quoted 11 lines · David Miller

Tag Eric, you're it.

You ACK'd this patch, so you get to show how this is actually able to cause some kind of problem.

I assert that this is adding a useless test, that doesn't fix any kind of possible crash or misbehavior.  If length == 1 at the default:, the code will absolutely do the right thing.

Prove me wrong.

Thinking about a malformed tcp segment, which has no data but silly options, and whose last byte is neither TCPOPT_EOL or TCPOPT_NOP, we will try to dereference one byte over the boundary when parsing the options. I know we have skb_shared_info at the end and it won't cause any crash, but should we rely on this fact?

Signature · 6 lines
Regards,
Changli Gao([email protected])
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw
Quoted 6 lines · Changli Gao

Thinking about a malformed tcp segment, which has no data but silly options, and whose last byte is neither TCPOPT_EOL or TCPOPT_NOP, we will try to dereference one byte over the boundary when parsing the options. I know we have skb_shared_info at the end and it won't cause any crash, but should we rely on this fact?

No we cant rely on this, kmemcheck might barf on us.

Your patch (and the netfilter one) is fine.

maintainer
#5 lore raw

From: Eric Dumazet <[email protected]>
Date: Mon, 02 Apr 2012 05:53:17 +0200

Quoted 10 lines · Changli Gao
Quoted 7 lines

Thinking about a malformed tcp segment, which has no data but silly options, and whose last byte is neither TCPOPT_EOL or TCPOPT_NOP, we will try to dereference one byte over the boundary when parsing the options. I know we have skb_shared_info at the end and it won't cause any crash, but should we rely on this fact?

No we cant rely on this, kmemcheck might barf on us.

Give me a break.

The code does the right thing, in every possible case, and in every possible valid state of an SKB.

If we can't make kmemcheck handle that, tough, I'm not adding useless tests to a function, specifically tests which are always there and that don't fix anything.

#6 lore raw
Quoted 12 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Mon, 02 Apr 2012 05:53:17 +0200

Quoted 9 lines · Changli Gao
Quoted 7 lines

Thinking about a malformed tcp segment, which has no data but silly options, and whose last byte is neither TCPOPT_EOL or TCPOPT_NOP, we will try to dereference one byte over the boundary when parsing the options. I know we have skb_shared_info at the end and it won't cause any crash, but should we rely on this fact?

Quoted 3 lines
Quoted 1 line

No we cant rely on this, kmemcheck might barf on us.

Give me a break.

Sure. End of discussion.

#7 lore raw
In reply to Eric Dumazet
Quoted 6 lines · Eric Dumazet

No we cant rely on this, kmemcheck might barf on us.

Your patch (and the netfilter one) is fine.

Got it. Thanks. FYI, the tcp options are copied to the stack before being parsed.

Signature · 2 lines
Regards,
Changli Gao([email protected])
#8 lore raw
Quoted 3 lines · Changli Gao

Got it. Thanks. FYI, the tcp options are copied to the stack before being parsed.

What do you mean ?

code looks like :

const struct tcphdr *th = tcp_hdr(skb);
int length = (th->doff * 4) - sizeof(struct tcphdr);

ptr = (const unsigned char *)(th + 1);

Therefore ptr points somewhere in skb->head ...

#9 lore raw
Quoted 10 lines · Eric Dumazet
Quoted 3 lines · Changli Gao

Got it. Thanks. FYI, the tcp options are copied to the stack before being parsed.

What do you mean ?

code looks like :

Quoted 11 lines

const struct tcphdr *th = tcp_hdr(skb);
int length = (th->doff * 4) - sizeof(struct tcphdr);

ptr = (const unsigned char *)(th + 1);

Therefore ptr points somewhere in skb->head ...

Oh, sorry. I forgot to add the condition when I wrote it down. I mean the code in netfilter.

        unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
        const unsigned char *ptr;
        int length = (tcph->doff*4) - sizeof(struct tcphdr);

        if (!length)
                return;

        ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
                                 length, buff);
        BUG_ON(ptr == NULL);
Signature · 2 lines
Regards,
Changli Gao([email protected])
#10 lore raw
Quoted 26 lines · Changli Gao
Quoted 10 lines · Eric Dumazet
Quoted 3 lines · Changli Gao

Got it. Thanks. FYI, the tcp options are copied to the stack before being parsed.

What do you mean ?

code looks like :

Quoted 11 lines

const struct tcphdr *th = tcp_hdr(skb);
int length = (th->doff * 4) - sizeof(struct tcphdr);

ptr = (const unsigned char *)(th + 1);

Therefore ptr points somewhere in skb->head ...

Oh, sorry. I forgot to add the condition when I wrote it down. I mean the code in netfilter.

Quoted 11 lines
        unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
        const unsigned char *ptr;
        int length = (tcph->doff*4) - sizeof(struct tcphdr);

        if (!length)
                return;

        ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
                                 length, buff);
        BUG_ON(ptr == NULL);

Doesnt really save us, skb_header_pointer() copies only if block not directly and fully accessible in skb->head

So if skb->head contains exactly the tcp options, we still can read one uninit byte.

So potential problem in netfilter too.

#11 lore raw
In reply to David Miller
Quoted 17 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Mon, 02 Apr 2012 05:19:33 +0200

Quoted 11 lines

net/ipv4/tcp_input.c void tcp_parse_options(const struct sk_buff *skb, struct tcp_options_received *o · line 3852

Quoted 9 lines
net/ipv4/tcp_input.c +2 -0
@@ -3845,6 +3845,8 @@ void tcp_parse_options(const struct sk_buff *skb, struct tcp_options_received *o
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize = *ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;

Tag Eric, you're it.

Quoted 8 lines

You ACK'd this patch, so you get to show how this is actually able to cause some kind of problem.

I assert that this is adding a useless test, that doesn't fix any kind of possible crash or misbehavior. If length == 1 at the default:, the code will absolutely do the right thing.

Prove me wrong.

No problem.

You can have NOP,NOP,NOP,EVIL-OPTION

initial length=4 (multiple of 4)

We can read 5 bytes, and access 'out of bound' memory.

Usually not a problem since we have many bytes after our head.

maintainer
#12 lore raw

From: Eric Dumazet <[email protected]>
Date: Mon, 02 Apr 2012 05:45:45 +0200

Quoted 1 line

Usually not a problem since we have many bytes after our head.

We always have bytes after the head, it's guarenteed, and whether it's garbage bytes or skb_shared_info() it simply doesn't matter.

#13 lore raw
Quoted 7 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Mon, 02 Apr 2012 05:45:45 +0200

Quoted 1 line

Usually not a problem since we have many bytes after our head.

We always have bytes after the head, it's guarenteed, and whether it's garbage bytes or skb_shared_info() it simply doesn't matter.

Then you have to add a kmemcheck_something() to make this clear and avoid possible warnings.

maintainer
#14 lore raw

From: Eric Dumazet <[email protected]>
Date: Mon, 02 Apr 2012 05:58:25 +0200

Quoted 11 lines · David Miller
Quoted 7 lines

From: Eric Dumazet <[email protected]>
Date: Mon, 02 Apr 2012 05:45:45 +0200

Quoted 1 line

Usually not a problem since we have many bytes after our head.

We always have bytes after the head, it's guarenteed, and whether it's garbage bytes or skb_shared_info() it simply doesn't matter.

Then you have to add a kmemcheck_something() to make this clear and avoid possible warnings.

That's perfectly fine and would document the situation. And we can add a similar annotation to the two other nearly identical pieces of code in net/netfilter/nf_conntrack_proto_tcp.c

maintainer
#15 lore raw
In reply to Changli Gao

From: Changli Gao <[email protected]>
Date: Mon, 2 Apr 2012 11:10:50 +0800

Quoted 4 lines

We should check the length of the data before dereferencing it when parsing the TCP options.

Proper Subject prefix here would be "tcp: ", not "net: " and maybe adjust the subject line to also mention the specific function being fixed, which in this case would be tcp_parse_options(). So:

tcp: Validate length of data before dereference in tcp_parse_options().

and then you can make the commit message just be your signoff.