Skip to contentSkip to conversation

netfilter: check the length of the data before dereferencing it

PATCH In review Changli Gao 3 participants 5 messages Apr 1, 2012 – Apr 2, 2012

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

PATCH 1/1 netfilter: 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/netfilter/nf_conntrack_proto_tcp.c |    4 ++++
 1 file changed, 4 insertions(+)
net/netfilter/nf_conntrack_proto_tcp.c +4 -0
index 361eade..9e446c5 100644
@@ -404,6 +404,8 @@ static void tcp_options(const struct sk_buff *skb,
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize=*ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;
@@ -464,6 +466,8 @@ static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize = *ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;
#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/netfilter/nf_conntrack_proto_tcp.c |    4 ++++
 1 file changed, 4 insertions(+)
net/netfilter/nf_conntrack_proto_tcp.c +0 -0
index 361eade..9e446c5 100644

net/netfilter/nf_conntrack_proto_tcp.c static void tcp_options(const struct sk_buff *skb, · line 411

Quoted 8 lines
net/netfilter/nf_conntrack_proto_tcp.c +2 -0
@@ -404,6 +404,8 @@ static void tcp_options(const struct sk_buff *skb,
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize=*ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;

length is always multiple of 4:

int length = (tcph->doff*4) - sizeof(struct tcphdr);

#2 lore raw
Quoted 24 lines · Pablo Neira Ayuso
Quoted 12 lines · Changli Gao

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

---
 net/netfilter/nf_conntrack_proto_tcp.c |    4 ++++
 1 file changed, 4 insertions(+)
net/netfilter/nf_conntrack_proto_tcp.c +0 -0
index 361eade..9e446c5 100644

net/netfilter/nf_conntrack_proto_tcp.c static void tcp_options(const struct sk_buff *skb, · line 411

Quoted 8 lines
net/netfilter/nf_conntrack_proto_tcp.c +2 -0
@@ -404,6 +404,8 @@ static void tcp_options(const struct sk_buff *skb,
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize=*ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;

length is always multiple of 4:

Quoted 2 lines

int length = (tcph->doff*4) - sizeof(struct tcphdr); --

initial value yes, but it can change in the loop.

#3 lore raw
Quoted 25 lines · Eric Dumazet
Quoted 24 lines · Pablo Neira Ayuso
Quoted 12 lines · Changli Gao

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

---
 net/netfilter/nf_conntrack_proto_tcp.c |    4 ++++
 1 file changed, 4 insertions(+)
net/netfilter/nf_conntrack_proto_tcp.c +0 -0
index 361eade..9e446c5 100644

net/netfilter/nf_conntrack_proto_tcp.c static void tcp_options(const struct sk_buff *skb, · line 411

Quoted 8 lines
net/netfilter/nf_conntrack_proto_tcp.c +2 -0
@@ -404,6 +404,8 @@ static void tcp_options(const struct sk_buff *skb,
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize=*ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;

length is always multiple of 4:

Quoted 4 lines
Quoted 2 lines

int length = (tcph->doff*4) - sizeof(struct tcphdr); --

initial value yes, but it can change in the loop.

Indeed, then I think we need a similar patch for tcp_parse_options() in net/ipv4/tcp_input.c

#4 lore raw
Quoted 3 lines · Pablo Neira Ayuso

Indeed, then I think we need a similar patch for tcp_parse_options() in net/ipv4/tcp_input.c

I'll send a separated patch for it. Thanks.

Signature · 2 lines
Regards,
Changli Gao([email protected])