Skip to contentSkip to conversation

netpoll: linearize skb before accessing its data

PATCH In review Antonio Quartulli 4 participants 24 messages Oct 21, 2013 – Oct 25, 2013

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

PATCH 1/1 netpoll: linearize skb before accessing its data

__netpoll_rx() assumes that the data buffer of the received skb is linear and then passes it to rx_hook(). However this is not true because the skb has not been linearized yet.

This can cause rx_hook() to access non allocated memory while parsing the received data.

Fix __netpoll_rx() by explicitly linearising the skb.

---

I checked linux-3.0 and this bug seems to be already there. Please consider queueing it for stable.

Regards,

 net/core/netpoll.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/netpoll.c +5 -0
index fc75c9e..97cff18 100644
@@ -814,6 +814,9 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 		if (pskb_trim_rcsum(skb, len))
 			goto out;
 
+		if (skb_linearize(skb))
+			goto out;
+
 		iph = (struct iphdr *)skb->data;
 		if (iph->protocol != IPPROTO_UDP)
 			goto out;
@@ -855,6 +858,8 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 			goto out;
 		if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
 			goto out;
+		if (skb_linearize(skb))
+			goto out;
 		ip6h = ipv6_hdr(skb);
 		if (!pskb_may_pull(skb, sizeof(struct udphdr)))
 			goto out;
Signature · 1 line
1.8.4
maintainer
#1 lore raw

From: Antonio Quartulli <[email protected]>
Date: Mon, 21 Oct 2013 23:31:20 +0200

Quoted 11 lines

__netpoll_rx() assumes that the data buffer of the received skb is linear and then passes it to rx_hook(). However this is not true because the skb has not been linearized yet.

This can cause rx_hook() to access non allocated memory while parsing the received data.

Fix __netpoll_rx() by explicitly linearising the skb.

It is rx_hook's obligation to access the SKB properly and not assume that the SKB is linear. It is very expensive to linearize every SKB just for the sake of improperly implemented receive hooks.

In particular the rx hooks must make use of interface such as pskb_may_pull(), just like every other protocol does on packet input processing, to make sure the area they want to access is in the linear area.

#2 lore raw
Quoted 20 lines · David Miller

From: Antonio Quartulli <[email protected]>
Date: Mon, 21 Oct 2013 23:31:20 +0200

Quoted 11 lines

__netpoll_rx() assumes that the data buffer of the received skb is linear and then passes it to rx_hook(). However this is not true because the skb has not been linearized yet.

This can cause rx_hook() to access non allocated memory while parsing the received data.

Fix __netpoll_rx() by explicitly linearising the skb.

It is rx_hook's obligation to access the SKB properly and not assume that the SKB is linear. It is very expensive to linearize every SKB just for the sake of improperly implemented receive hooks.

Quoted 5 lines

In particular the rx hooks must make use of interface such as pskb_may_pull(), just like every other protocol does on packet input processing, to make sure the area they want to access is in the linear area.

But rx_hook() does not receive any skb:

609 np->rx_hook(np, ntohs(uh->source),
610 (char *)(uh+1),
611 ulen - sizeof(struct udphdr));

it just receives a pointer to the data and can't do anything to make it linear. (uh is a pointer to the udp header). Am I missing something?

Regards,

Signature · 1 line
Antonio Quartulli
maintainer
#3 lore raw

From: Antonio Quartulli <[email protected]>
Date: Tue, 22 Oct 2013 08:06:35 +0200

Quoted 8 lines

But rx_hook() does not receive any skb:

609 np->rx_hook(np, ntohs(uh->source),
610 (char *)(uh+1),
611 ulen - sizeof(struct udphdr));

it just receives a pointer to the data and can't do anything to make it linear. (uh is a pointer to the udp header). Am I missing something?

Then this hook's API needs to be fixed, it's completely broken.

Make it pass the SKB and the appropriate offset (from skb->data) in bytes.

#4 lore raw
Quoted 9 lines · David Miller

From: Antonio Quartulli <[email protected]>
Date: Tue, 22 Oct 2013 08:06:35 +0200

Quoted 6 lines

But rx_hook() does not receive any skb:

609 np->rx_hook(np, ntohs(uh->source),
610 (char *)(uh+1),
611 ulen - sizeof(struct udphdr));

Quoted 8 lines
Quoted 2 lines

it just receives a pointer to the data and can't do anything to make it linear. (uh is a pointer to the udp header). Am I missing something?

Then this hook's API needs to be fixed, it's completely broken.

Make it pass the SKB and the appropriate offset (from skb->data) in bytes.

Ok I will do that. But I think this API change must go into net-next, right? Otherwise we would break every existing user.

What about resending this patch to net (after having removed the pskb_may_pull() as pointed out by Eric) and then fixing the API in net-next?

Signature · 1 line
Antonio Quartulli
maintainer
#5 lore raw

From: Antonio Quartulli <[email protected]>
Date: Tue, 22 Oct 2013 08:37:14 +0200

Quoted 2 lines

Ok I will do that. But I think this API change must go into net-next, right? Otherwise we would break every existing user.

Too bad, it's a necessary API change to fix the bug, the users will need to change too.

It's not our problem if they are out-of-tree and can't be fixed in-situ.

#6 lore raw

PATCH 1/1 netpoll: fix rx_hook() interface by passing the skb

Right now skb->data is passed to rx_hook() even if the skb has not been linearised and without giving rx_hook() a way to linearise it.

Change the rx_hook() interface and make it accept the skb as argument. In this way users implementing rx_hook() can perform all the needed operations to properly (and safely) access the skb data.

---
 include/linux/netpoll.h |  2 +-
 net/core/netpoll.c      | 10 ++++------
 2 files changed, 5 insertions(+), 7 deletions(-)
include/linux/netpoll.h +1 -1
index f3c7c24..5352160 100644
@@ -24,7 +24,7 @@ struct netpoll {
 	struct net_device *dev;
 	char dev_name[IFNAMSIZ];
 	const char *name;
-	void (*rx_hook)(struct netpoll *, int, char *, int);
+	void (*rx_hook)(struct netpoll *np, struct sk_buff *skb, int offset);
 
 	union inet_addr local_ip, remote_ip;
 	bool ipv6;
net/core/netpoll.c +4 -6
index fc75c9e..b415437 100644
@@ -834,9 +834,8 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 			if (np->local_port && np->local_port != ntohs(uh->dest))
 				continue;
 
-			np->rx_hook(np, ntohs(uh->source),
-				       (char *)(uh+1),
-				       ulen - sizeof(struct udphdr));
+			np->rx_hook(np, skb,
+				    (unsigned char *)(uh + 1) - skb->data);
 			hits++;
 		}
 	} else {
@@ -872,9 +871,8 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 			if (np->local_port && np->local_port != ntohs(uh->dest))
 				continue;
 
-			np->rx_hook(np, ntohs(uh->source),
-				       (char *)(uh+1),
-				       ulen - sizeof(struct udphdr));
+			np->rx_hook(np, skb,
+				    (unsigned char *)(uh + 1) - skb->data);
 			hits++;
 		}
 #endif
Signature · 1 line
1.8.4
#7 lore raw

RE: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Quoted 10 lines

Subject: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Right now skb->data is passed to rx_hook() even if the skb has not been linearised and without giving rx_hook() a way to linearise it.

Change the rx_hook() interface and make it accept the skb as argument. In this way users implementing rx_hook() can perform all the needed operations to properly (and safely) access the skb data.

...

include/linux/netpoll.h struct netpoll { · line 27

Quoted 2 lines
include/linux/netpoll.h +1 -1
-	void (*rx_hook)(struct netpoll *, int, char *, int);
+	void (*rx_hook)(struct netpoll *np, struct sk_buff *skb, int offset);

You can't do that change without changing the way that hooks are registered so that any existing modules will fail to register their hooks.

David

#8 lore raw

Re: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Quoted 10 lines · David Laight
Quoted 10 lines

Subject: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Right now skb->data is passed to rx_hook() even if the skb has not been linearised and without giving rx_hook() a way to linearise it.

Change the rx_hook() interface and make it accept the skb as argument. In this way users implementing rx_hook() can perform all the needed operations to properly (and safely) access the skb data.

Quoted 6 lines

...

include/linux/netpoll.h struct netpoll { · line 27

Quoted 2 lines
include/linux/netpoll.h +1 -1
-	void (*rx_hook)(struct netpoll *, int, char *, int);
+	void (*rx_hook)(struct netpoll *np, struct sk_buff *skb, int offset);

You can't do that change without changing the way that hooks are registered so that any existing modules will fail to register their hooks.

There is no hook registration in the kernel tree. All the users are outside.

Signature · 1 line
Antonio Quartulli
#9 lore raw

RE: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Quoted 13 lines

Subject: Re: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Quoted 10 lines · David Laight
Quoted 10 lines

Subject: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Right now skb->data is passed to rx_hook() even if the skb has not been linearised and without giving rx_hook() a way to linearise it.

Change the rx_hook() interface and make it accept the skb as argument. In this way users implementing rx_hook() can perform all the needed operations to properly (and safely) access the skb data.

Quoted 8 lines
Quoted 6 lines

...

include/linux/netpoll.h struct netpoll { · line 27

Quoted 2 lines
include/linux/netpoll.h +1 -1
-	void (*rx_hook)(struct netpoll *, int, char *, int);
+	void (*rx_hook)(struct netpoll *np, struct sk_buff *skb, int offset);

You can't do that change without changing the way that hooks are registered so that any existing modules will fail to register their hooks.

There is no hook registration in the kernel tree. All the users are outside.

Looking at __netpoll_rx() I notice that there isn't an skb_pull for the udp header.

Actually, I think the alignment rules effectively imply that iph->ihl (the second byte) will always be in the first skb fragment so the code could sensible do a single skb_pull() that includes the udp header.

I can't remember which value you passed as 'offset' (and my mailer makes it hard to find), but to ease the code changes the offset of the udp data would make sense.
In that case you still need to pass the source port.
If you do rx_hook(np, source_port, skb, offset) then if anyone manages to load an old module (or code that casts the assignement to rx_poll) at least it won't go 'bang'.
Renaming the structure member will guarantee to generate compile errors.

David

#10 lore raw

Re: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Quoted 25 lines · David Laight
Quoted 13 lines

Subject: Re: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Quoted 10 lines · David Laight
Quoted 10 lines

Subject: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Right now skb->data is passed to rx_hook() even if the skb has not been linearised and without giving rx_hook() a way to linearise it.

Change the rx_hook() interface and make it accept the skb as argument. In this way users implementing rx_hook() can perform all the needed operations to properly (and safely) access the skb data.

Quoted 8 lines
Quoted 6 lines

...

include/linux/netpoll.h struct netpoll { · line 27

Quoted 2 lines
include/linux/netpoll.h +1 -1
-	void (*rx_hook)(struct netpoll *, int, char *, int);
+	void (*rx_hook)(struct netpoll *np, struct sk_buff *skb, int offset);

You can't do that change without changing the way that hooks are registered so that any existing modules will fail to register their hooks.

There is no hook registration in the kernel tree. All the users are outside.

Looking at __netpoll_rx() I notice that there isn't an skb_pull for the udp header.

Quoted 8 lines

Actually, I think the alignment rules effectively imply that iph->ihl (the second byte) will always be in the first skb fragment so the code could sensible do a single skb_pull() that includes the udp header.

I can't remember which value you passed as 'offset' (and my mailer makes it hard to find), but to ease the code changes the offset of the udp data would make sense.
In that case you still need to pass the source port.

I decided not to pass the source port because if the user is really interested in it, it is still possible to get the udp_hdr from the skb and read its value.

Quoted 5 lines

If you do rx_hook(np, source_port, skb, offset) then if anyone manages to load an old module (or code that casts the assignement to rx_poll) at least it won't go 'bang'.
Renaming the structure member will guarantee to generate compile errors.

so you suggest to rename rx_hook to something else to warn people about the change?

If we go for the "no udp port" approach they will get an error any way because of the mismatching arguments.

Regards,

Signature · 1 line
Antonio Quartulli
maintainer
#11 lore raw

Re: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

From: Antonio Quartulli <[email protected]>
Date: Tue, 22 Oct 2013 19:13:14 +0200

Quoted 2 lines

If we go for the "no udp port" approach they will get an error any way because of the mismatching arguments.

Antonio, I think it is fair enough to keep passing the port argument as well as the length.

These precomputed values might as well be provided the to receiver.

Thanks.

#12 lore raw

RE: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

In reply to Antonio Quartulli

...

Quoted 7 lines
Quoted 4 lines

I can't remember which value you passed as 'offset' (and my mailer makes it hard to find), but to ease the code changes the offset of the udp data would make sense.
In that case you still need to pass the source port.

I decided not to pass the source port because if the user is really interested in it, it is still possible to get the udp_hdr from the skb and read its value.

It just seemed that there was no need to require that the hook re-parse the ip header just to find the source port.
(ok it could assume that the udp header is just before the data)

Quoted 7 lines
Quoted 4 lines

If you do rx_hook(np, source_port, skb, offset) then if anyone manages to load an old module (or code that casts the assignement to rx_poll) at least it won't go 'bang'.
Renaming the structure member will guarantee to generate compile errors.

so you suggest to rename rx_hook to something else to warn people about the change?

Yes.

Quoted 2 lines

If we go for the "no udp port" approach they will get an error any way because of the mismatching arguments.

No - you only get a warning when you assign a function pointer of the wrong type. And that is true even if you just change the type of the pointer. However code might already have a cast on the function pointer (eg because the hook has 'unsigned char *') - so you won't even get a warning.
You then get an OOPS when the hook tries to read the buffer.

It is a really bad interface...
There isn't even a flags/options (etc) word that can be used to detect enhancements.

David

#13 lore raw

Re: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Quoted 6 lines · David Laight

...

Quoted 5 lines
Quoted 4 lines

I can't remember which value you passed as 'offset' (and my mailer makes it hard to find), but to ease the code changes the offset of the udp data would make sense.
In that case you still need to pass the source port.

Quoted 6 lines
Quoted 2 lines

I decided not to pass the source port because if the user is really interested in it, it is still possible to get the udp_hdr from the skb and read its value.

It just seemed that there was no need to require that the hook re-parse the ip header just to find the source port.
(ok it could assume that the udp header is just before the data)

Also David (M.) pointed out the same. I will keep the port as argument for rx_hook.

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines

If you do rx_hook(np, source_port, skb, offset) then if anyone manages to load an old module (or code that casts the assignement to rx_poll) at least it won't go 'bang'.
Renaming the structure member will guarantee to generate compile errors.

so you suggest to rename rx_hook to something else to warn people about the change?

Yes.

mh..what about rx_skb_hook ? this way we also make it easy to notice the difference (both in arguments and behaviour).

Quoted 9 lines
Quoted 2 lines

If we go for the "no udp port" approach they will get an error any way because of the mismatching arguments.

No - you only get a warning when you assign a function pointer of the wrong type. And that is true even if you just change the type of the pointer. However code might already have a cast on the function pointer (eg because the hook has 'unsigned char *') - so you won't even get a warning.
You then get an OOPS when the hook tries to read the buffer.

Quoted 4 lines

It is a really bad interface...
There isn't even a flags/options (etc) word that can be used to detect enhancements.

agreed. But I am not sure about what I could do to fix that.

My idea is to use the following API:

rx_skb_hook(struct netpoll *np, int source, struct sk_buff *skb, int len);

Any suggestion or objection?

Regards,

Signature · 1 line
Antonio Quartulli
#14 lore raw

RE: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Quoted 5 lines

My idea is to use the following API:

rx_skb_hook(struct netpoll *np, int source, struct sk_buff *skb, int len);

Any suggestion or objection?

Don't you need to pass the offset of the udp data?

David

#15 lore raw

Re: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

Quoted 7 lines · David Laight
Quoted 5 lines

My idea is to use the following API:

rx_skb_hook(struct netpoll *np, int source, struct sk_buff *skb, int len);

Any suggestion or objection?

Don't you need to pass the offset of the udp data?

Yes, you are right. I just forgot it. Therefore we have:

rx_skb_hook(struct netpoll *np, int source, struct sk_buff *skb, int offset,

	    int len);

where offset is going to be = (udp_hdr + 1) - skb->data and len = skb->len - offset

Regards,

Signature · 1 line
Antonio Quartulli
maintainer
#16 lore raw

Re: [PATCH net] netpoll: fix rx_hook() interface by passing the skb

From: Antonio Quartulli <[email protected]>
Date: Wed, 23 Oct 2013 14:44:01 +0200

Quoted 9 lines · David Laight
Quoted 7 lines
Quoted 5 lines

My idea is to use the following API:

rx_skb_hook(struct netpoll *np, int source, struct sk_buff *skb, int len);

Any suggestion or objection?

Don't you need to pass the offset of the udp data?

Quoted 7 lines

Yes, you are right. I just forgot it. Therefore we have:

rx_skb_hook(struct netpoll *np, int source, struct sk_buff *skb, int offset,

	    int len);

where offset is going to be = (udp_hdr + 1) - skb->data and len = skb->len - offset

This looks good to me.

#17 lore raw

PATCH v2 1/1 netpoll: fix rx_hook() interface by passing the skb

Right now skb->data is passed to rx_hook() even if the skb has not been linearised and without giving rx_hook() a way to linearise it.

Change the rx_hook() interface and make it accept the skb and the offset to the UDP payload as arguments. rx_hook() is also renamed to rx_skb_hook() to ensure that out of the tree users notice the API change.

In this way any rx_skb_hook() implementation can perform all the needed operations to properly (and safely) access the skb data.

---
 include/linux/netpoll.h |  5 +++--
 net/core/netpoll.c      | 31 ++++++++++++++++++-------------
 2 files changed, 21 insertions(+), 15 deletions(-)
include/linux/netpoll.h +3 -2
index f3c7c24..fbfdb9d 100644
@@ -24,7 +24,8 @@ struct netpoll {
 	struct net_device *dev;
 	char dev_name[IFNAMSIZ];
 	const char *name;
-	void (*rx_hook)(struct netpoll *, int, char *, int);
+	void (*rx_skb_hook)(struct netpoll *np, int source, struct sk_buff *skb,
+			    int offset, int len);
 
 	union inet_addr local_ip, remote_ip;
 	bool ipv6;
@@ -41,7 +42,7 @@ struct netpoll_info {
 	unsigned long rx_flags;
 	spinlock_t rx_lock;
 	struct semaphore dev_lock;
-	struct list_head rx_np; /* netpolls that registered an rx_hook */
+	struct list_head rx_np; /* netpolls that registered an rx_skb_hook */
 
 	struct sk_buff_head neigh_tx; /* list of neigh requests to reply to */
 	struct sk_buff_head txq;
net/core/netpoll.c +18 -13
index fc75c9e..8f97199 100644
@@ -636,8 +636,9 @@ static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo
 
 			netpoll_send_skb(np, send_skb);
 
-			/* If there are several rx_hooks for the same address,
-			   we're fine by sending a single reply */
+			/* If there are several rx_skb_hooks for the same
+			 * address we're fine by sending a single reply
+			 */
 			break;
 		}
 		spin_unlock_irqrestore(&npinfo->rx_lock, flags);
@@ -719,8 +720,9 @@ static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo
 
 			netpoll_send_skb(np, send_skb);
 
-			/* If there are several rx_hooks for the same address,
-			   we're fine by sending a single reply */
+			/* If there are several rx_skb_hooks for the same
+			 * address, we're fine by sending a single reply
+			 */
 			break;
 		}
 		spin_unlock_irqrestore(&npinfo->rx_lock, flags);
@@ -756,11 +758,12 @@ static bool pkt_is_ns(struct sk_buff *skb)
 
 int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 {
-	int proto, len, ulen;
-	int hits = 0;
+	int proto, len, ulen, data_len;
+	int hits = 0, offset;
 	const struct iphdr *iph;
 	struct udphdr *uh;
 	struct netpoll *np, *tmp;
+	uint16_t source;
 
 	if (list_empty(&npinfo->rx_np))
 		goto out;
@@ -820,7 +823,10 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 
 		len -= iph->ihl*4;
 		uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
+		offset = (unsigned char *)(uh + 1) - skb->data;
 		ulen = ntohs(uh->len);
+		data_len = skb->len - offset;
+		source = ntohs(uh->source);
 
 		if (ulen != len)
 			goto out;
@@ -834,9 +840,7 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 			if (np->local_port && np->local_port != ntohs(uh->dest))
 				continue;
 
-			np->rx_hook(np, ntohs(uh->source),
-				       (char *)(uh+1),
-				       ulen - sizeof(struct udphdr));
+			np->rx_skb_hook(np, source, skb, offset, data_len);
 			hits++;
 		}
 	} else {
@@ -859,7 +863,10 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 		if (!pskb_may_pull(skb, sizeof(struct udphdr)))
 			goto out;
 		uh = udp_hdr(skb);
+		offset = (unsigned char *)(uh + 1) - skb->data;
 		ulen = ntohs(uh->len);
+		data_len = skb->len - offset;
+		source = ntohs(uh->source);
 		if (ulen != skb->len)
 			goto out;
 		if (udp6_csum_init(skb, uh, IPPROTO_UDP))
@@ -872,9 +879,7 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 			if (np->local_port && np->local_port != ntohs(uh->dest))
 				continue;
 
-			np->rx_hook(np, ntohs(uh->source),
-				       (char *)(uh+1),
-				       ulen - sizeof(struct udphdr));
+			np->rx_skb_hook(np, source, skb, offset, data_len);
 			hits++;
 		}
 #endif
@@ -1062,7 +1067,7 @@ int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
 
 	npinfo->netpoll = np;
 
-	if (np->rx_hook) {
+	if (np->rx_skb_hook) {
 		spin_lock_irqsave(&npinfo->rx_lock, flags);
 		npinfo->rx_flags |= NETPOLL_RX_ENABLED;
 		list_add_tail(&np->rx, &npinfo->rx_np);
Signature · 1 line
1.8.4
#18 lore raw

RE: [PATCHv2 net] netpoll: fix rx_hook() interface by passing the skb

Quoted 14 lines

Subject: [PATCHv2 net] netpoll: fix rx_hook() interface by passing the skb

Right now skb->data is passed to rx_hook() even if the skb has not been linearised and without giving rx_hook() a way to linearise it.

Change the rx_hook() interface and make it accept the skb and the offset to the UDP payload as arguments. rx_hook() is also renamed to rx_skb_hook() to ensure that out of the tree users notice the API change.

In this way any rx_skb_hook() implementation can perform all the needed operations to properly (and safely) access the skb data.

...

Quoted 23 lines
net/core/netpoll.c +4 -3
@@ -820,7 +823,10 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 
 		len -= iph->ihl*4;
 		uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
+		offset = (unsigned char *)(uh + 1) - skb->data;
 		ulen = ntohs(uh->len);
+		data_len = skb->len - offset;
+		source = ntohs(uh->source);
 
 		if (ulen != len)
 			goto out;
@@ -834,9 +840,7 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 			if (np->local_port && np->local_port != ntohs(uh->dest))
 				continue;
 
-			np->rx_hook(np, ntohs(uh->source),
-				       (char *)(uh+1),
-				       ulen - sizeof(struct udphdr));
+			np->rx_skb_hook(np, source, skb, offset, data_len);
 			hits++;
 		}
 	} else {
Quoted 1 line

From a code optimisation point of view you probably don't want to be

calculating the source, offset and length early.
It is quite likely that the local variables will have to be written to the stack (because of the function calls) - so it is almost certainly more efficient to calculate them just before the call.

David

#19 lore raw

Re: [PATCHv2 net] netpoll: fix rx_hook() interface by passing the skb

Quoted 16 lines · David Laight
Quoted 16 lines

Subject: [PATCHv2 net] netpoll: fix rx_hook() interface by passing the skb

net/core/netpoll.c +3 -0
@@ -820,7 +823,10 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 
 		len -= iph->ihl*4;
 		uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
+		offset = (unsigned char *)(uh + 1) - skb->data;
 		ulen = ntohs(uh->len);
+		data_len = skb->len - offset;
+		source = ntohs(uh->source);
 
 		if (ulen != len)
 			goto out;
@@ -834,9 +840,7 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 			if (np->local_port && np->local_port != ntohs(uh->dest))
 				continue;
 
Quoted 13 lines

net/core/netpoll.c int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo) · line 846

Quoted 7 lines
net/core/netpoll.c +1 -3
-			np->rx_hook(np, ntohs(uh->source),
-				       (char *)(uh+1),
-				       ulen - sizeof(struct udphdr));
+			np->rx_skb_hook(np, source, skb, offset, data_len);
 			hits++;
 		}
 	} else {

From a code optimisation point of view you probably don't want to be calculating the source, offset and length early.
It is quite likely that the local variables will have to be written to the stack (because of the function calls) - so it is almost certainly more efficient to calculate them just before the call.

I thought that computing them once outside the loop was better than re-computing them during each iteration.

Having them outside makes it also clear that they always have the same value.

Regards,

Signature · 1 line
Antonio Quartulli
maintainer
#20 lore raw

Re: [PATCHv2 net] netpoll: fix rx_hook() interface by passing the skb

In reply to David Laight

From: "David Laight" <[email protected]>
Date: Thu, 24 Oct 2013 09:43:38 +0100

Quoted 5 lines

From a code optimisation point of view you probably don't want to be calculating the source, offset and length early.
It is quite likely that the local variables will have to be written to the stack (because of the function calls) - so it is almost certainly more efficient to calculate them just before the call.

But this change is being made from a bug prevention point of view, we already we're already getting this code path wrong as-is.

maintainer Applied
#21 lore raw

Re: [PATCHv2 net] netpoll: fix rx_hook() interface by passing the skb

In reply to Antonio Quartulli

From: Antonio Quartulli <[email protected]>
Date: Wed, 23 Oct 2013 23:36:30 +0200

Quoted 9 lines

Right now skb->data is passed to rx_hook() even if the skb has not been linearised and without giving rx_hook() a way to linearise it.

Change the rx_hook() interface and make it accept the skb and the offset to the UDP payload as arguments. rx_hook() is also renamed to rx_skb_hook() to ensure that out of the tree users notice the API change.

Quoted 5 lines

In this way any rx_skb_hook() implementation can perform all the needed operations to properly (and safely) access the skb data.

Applied.

#22 lore raw
In reply to Antonio Quartulli
Quoted 39 lines · Antonio Quartulli

__netpoll_rx() assumes that the data buffer of the received skb is linear and then passes it to rx_hook(). However this is not true because the skb has not been linearized yet.

This can cause rx_hook() to access non allocated memory while parsing the received data.

Fix __netpoll_rx() by explicitly linearising the skb.

---

I checked linux-3.0 and this bug seems to be already there. Please consider queueing it for stable.

Regards,

 net/core/netpoll.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/netpoll.c +3 -0
index fc75c9e..97cff18 100644
@@ -814,6 +814,9 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 		if (pskb_trim_rcsum(skb, len))
 			goto out;
 
+		if (skb_linearize(skb))
+			goto out;
+
 		iph = (struct iphdr *)skb->data;
 		if (iph->protocol != IPPROTO_UDP)
 			goto out;

net/core/netpoll.c int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo) · line 865

Quoted 8 lines
net/core/netpoll.c +2 -0
@@ -855,6 +858,8 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
 			goto out;
 		if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
 			goto out;
+		if (skb_linearize(skb))
+			goto out;
 		ip6h = ipv6_hdr(skb);
 		if (!pskb_may_pull(skb, sizeof(struct udphdr)))
 			goto out;

Well, if you linearize the skb, no need for pskb_may_pull(), and it would be better to do it once at the beginning...

Anyway, how I see nothing sets rx_hook, what am I missing ?

# git grep -n rx_hook
include/linux/netpoll.h:27:     void (*rx_hook)(struct netpoll *, int, char *, int);
include/linux/netpoll.h:44:     struct list_head rx_np; /* netpolls that registered an rx_hook */
net/core/netpoll.c:639:                 /* If there are several rx_hooks for the same address,
net/core/netpoll.c:722:                 /* If there are several rx_hooks for the same address,
net/core/netpoll.c:837:                 np->rx_hook(np, ntohs(uh->source),
net/core/netpoll.c:875:                 np->rx_hook(np, ntohs(uh->source),
net/core/netpoll.c:1065:        if (np->rx_hook) {
maintainer
#23 lore raw

From: Eric Dumazet <[email protected]>
Date: Mon, 21 Oct 2013 15:25:36 -0700

Quoted 1 line

Anyway, how I see nothing sets rx_hook, what am I missing ?

Only out of tree code makes use of this facility, and it's been this way since the facility was introduced.

Yes, I'm disappointed and unhappy about this too.