Skip to contentSkip to conversation

[2.6] ethertap and af_inet.c assertion failures

DISCUSSION Simon Roscic 4 participants 14 messages Dec 23, 2004 – Jan 19, 2005

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

(i forgot to cc this mail to this list, sorry)

---------- Forwarded Message ----------

Subject: [2.6] ethertap and af_inet.c assertion failures
Date: Wednesday 22 December 2004 19:53
From: Simon Roscic <[email protected]>
To: [email protected]

(please cc me, as i'm not subscribed to lkml - thanks)

hi,

today i upgraded my kernel from 2.6.9-rc2 to 2.6.10-rc3-bk12, now i get the following assertion failures while using the (closed source) phion vpn client, the vpn client uses ethertap, there are no closed source kernel modules or the like:

KERNEL: assertion (!atomic_read(&sk->sk_wmem_alloc)) failed at net/ipv4/af_inet.c (150)

when the kernel prints out the above message the connection for the program using the vpn gets stuck - it happens very often if i use rdesktop, but it also happens when i just use ssh, so the bug may be triggered more often when there is more traffic over the vpn tunnel.

i tried with some other 2.6 kernel releases:

up to and including 2.6.9-rc2: no problem
2.6.9-rc3 does not boot on my machine
2.6.9-rc4 assertion failed as explained above
2.6.9 assertion failed as explained above

so it seems ethertap got broken somewhere post 2.6.9-rc2. any ideas what got changed post 2.6.9-rc2 wich might cause this?

thanks for looking at this problem, if i can provide more information, just contact me.

bye,
simon.

-------------------------------------------------------

#1 lore raw
Quoted 9 lines · Simon Roscic

hi,

today i upgraded my kernel from 2.6.9-rc2 to 2.6.10-rc3-bk12, now i get the following assertion failures while using the (closed source) phion vpn client, the vpn client uses ethertap, there are no closed source kernel modules or the like:

KERNEL: assertion (!atomic_read(&sk->sk_wmem_alloc)) failed at net/ipv4/af_inet.c (150)

netlink has messed up the send buffer accounting, when trimming a skb.

Patch below should fix this for ethertap. Could you give it a try?

-Tommy

drivers/net/ethertap.c +2 -2
@@ -207,8 +207,8 @@
 			return 0;
 		}
 	  	dev_kfree_skb(skb2);
-	}
-	/* ... but do not orphan it here, netlink does it in any case. */
+	} else
+		skb_orphan(skb);
 
 	lp->stats.tx_bytes+=skb->len;
 	lp->stats.tx_packets++;
#2 lore raw
Quoted 5 lines · Tommy Christensen

netlink has messed up the send buffer accounting, when trimming a skb.

Patch below should fix this for ethertap. Could you give it a try?

-Tommy

Thanks a lot! Your patch fixed the bug.

bye,
simon.

#3 lore raw
In reply to Tommy Christensen
Quoted 2 lines · Tommy Christensen

netlink has messed up the send buffer accounting, when trimming a skb.

You are spot on. Dave, you were right too about checking for charged skb's in netlink_trim.

Since the skb will be orphaned on its way to the destination anyway, let's simply do that before the trimming. This shouldn't lead to skb leakage since the skb will either be charged to the destination socket or freed.

Simon, please check if this fixes your problem or not.

Thanks,

Signature · 49 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
===== net/netlink/af_netlink.c 1.66 vs edited =====
--- 1.66/net/netlink/af_netlink.c	2005-01-14 15:41:06 +11:00
+++ edited/net/netlink/af_netlink.c	2005-01-16 00:24:20 +11:00
@@ -629,7 +629,6 @@
 		}
 		return 1;
 	}
-	skb_orphan(skb);
 	skb_set_owner_r(skb, sk);
 	return 0;
 }
@@ -663,14 +662,11 @@
 
 static inline void netlink_trim(struct sk_buff *skb, int allocation)
 {
-	int delta = skb->end - skb->tail;
+	int delta;
 
-	/* If the packet is charged to a socket, the modification
-	 * of truesize below is illegal and will corrupt socket
-	 * buffer accounting state.
-	 */
-	BUG_ON(skb->list != NULL);
+	skb_orphan(skb);
 
+	delta = skb->end - skb->tail;
 	if (delta * 2 < skb->truesize)
 		return;
 	if (pskb_expand_head(skb, 0, -delta, allocation))
@@ -707,14 +703,12 @@
 	struct netlink_opt *nlk = nlk_sk(sk);
 #ifdef NL_EMULATE_DEV
 	if (nlk->handler) {
-		skb_orphan(skb);
 		nlk->handler(sk->sk_protocol, skb);
 		return 0;
 	} else
 #endif
 	if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
 	    !test_bit(0, &nlk->state)) {
-                skb_orphan(skb);
 		skb_set_owner_r(skb, sk);
 		skb_queue_tail(&sk->sk_receive_queue, skb);
 		sk->sk_data_ready(sk, skb->len);
#4 lore raw
Quoted 4 lines · Herbert Xu

Since the skb will be orphaned on its way to the destination anyway, let's simply do that before the trimming. This shouldn't lead to skb leakage since the skb will either be charged to the destination socket or freed.

Looks fine. This will solve the problem for other callers as well.

Shouldn't there be a check for skb_shared as well? Or are the callers of netlink_unicast/broadcast supposed to avoid this.

-Tommy

#5 lore raw
In reply to Tommy Christensen
Quoted 3 lines · Tommy Christensen

Shouldn't there be a check for skb_shared as well? Or are the callers of netlink_unicast/broadcast supposed to avoid this.

Had anyone been using shared skb's here before they would've got into trouble a long time ago with calls such as skb_orphan in the path.

Even if they managed to do that and not notice then the pskb_expand_head call in netlink_trim would've likely caught it as well.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#6 lore raw
Quoted 11 lines · Herbert Xu
Quoted 2 lines · Tommy Christensen

Shouldn't there be a check for skb_shared as well? Or are the callers of netlink_unicast/broadcast supposed to avoid this.

Had anyone been using shared skb's here before they would've got into trouble a long time ago with calls such as skb_orphan in the path.

Even if they managed to do that and not notice then the pskb_expand_head call in netlink_trim would've likely caught it as well.

Well, pskb_expand_head was added recently and isn't even always called.

Have a look at audit_log_drain() in kernel/audit.c. This code does:
skb_get
netlink_unicast
...
kfree_skb

This is working just fine - until one day an over-sized skb comes along and hits the BUG in pskb_expand_head ...

That particular example can easily be solved by re-arranging the code a bit (I'll send a patch to someone).

Still, regarding netlink, I think the safe approach is to either handle or disallow shared skb's. Anything in-between could appear to be working, and then suddenly blow up on your production system. </paranoia>

-Tommy

#7 lore raw

[NETLINK] Do not netlink_unicast shared packet in kernel/audit.c

Quoted 11 lines · Tommy Christensen

Well, pskb_expand_head was added recently and isn't even always called.

Have a look at audit_log_drain() in kernel/audit.c. This code does:
skb_get
netlink_unicast
...
kfree_skb

This is working just fine - until one day an over-sized skb comes along and hits the BUG in pskb_expand_head ...

You're always right :)

Quoted 3 lines

Still, regarding netlink, I think the safe approach is to either handle or disallow shared skb's. Anything in-between could appear to be working, and then suddenly blow up on your production system. </paranoia>

kernel/audit.c seems to be the only caller that does this. Strictly speaking shared skb's are always illegal in netlink_unicast/netlink_broadcast since we call functions such as skb_orphan which modifies the sk_buff structure. However, for callers such as kernel/audit.c that don't care about those fields, it actually turns out to be harmless.

I'm hesitant to put an outright ban on it since it will force callers such as kernel/audit.c to always do an skb_clone.

So here is a patch to handle shared skb's by cloning them if we're going to do a pskb_expand_head. This patch depends on the previous one that moves skb_orphan into netlink_trim.

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
net/netlink/af_netlink.c renamed from net/netlink/af_netlink.c.orig +17 -7
@@ -660,7 +660,7 @@
 	sock_put(sk);
 }
 
-static inline void netlink_trim(struct sk_buff *skb, int allocation)
+static inline struct sk_buff *netlink_trim(struct sk_buff *skb, int allocation)
 {
 	int delta;
 
@@ -668,10 +668,20 @@
 
 	delta = skb->end - skb->tail;
 	if (delta * 2 < skb->truesize)
-		return;
-	if (pskb_expand_head(skb, 0, -delta, allocation))
-		return;
-	skb->truesize -= delta;
+		return skb;
+
+	if (skb_shared(skb)) {
+		struct sk_buff *nskb = skb_clone(skb, allocation);
+		if (!nskb)
+			return skb;
+		kfree_skb(skb);
+		skb = nskb;
+	}
+
+	if (!pskb_expand_head(skb, 0, -delta, allocation))
+		skb->truesize -= delta;
+
+	return skb;
 }
 
 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
@@ -680,7 +690,7 @@
 	int err;
 	long timeo;
 
-	netlink_trim(skb, gfp_any());
+	skb = netlink_trim(skb, gfp_any());
 
 	timeo = sock_sndtimeo(ssk, nonblock);
 retry:
@@ -788,7 +798,7 @@
 	info.skb = skb;
 	info.skb2 = NULL;
 
-	netlink_trim(skb, allocation);
+	skb = netlink_trim(skb, allocation);
 
 	/* While we sleep in clone, do not allow to change socket list */
 
#8 lore raw

Re: [NETLINK] Do not netlink_unicast shared packet in kernel/audit.c

Quoted 6 lines · Herbert Xu

I'm hesitant to put an outright ban on it since it will force callers such as kernel/audit.c to always do an skb_clone.

So here is a patch to handle shared skb's by cloning them if we're going to do a pskb_expand_head. This patch depends on the previous one that moves skb_orphan into netlink_trim.

Very nice, Herbert. This should be fine for everybody.

-Tommy

maintainer
#9 lore raw

Re: [NETLINK] Do not netlink_unicast shared packet in kernel/audit.c

In reply to Herbert Xu
Quoted 3 lines · On Sun, 16 Jan 2005 19:02:54 +1100 Herbert Xu <[email protected]>…

So here is a patch to handle shared skb's by cloning them if we're going to do a pskb_expand_head. This patch depends on the previous one that moves skb_orphan into netlink_trim.

I think this patch is buggy. At least in the final hunk, we do this:

	info.skb = skb;
	...
	skb = netlink_trim( ... );

Now info.skb can be pointing to a freed up skb.

I'll apply the first patch though.

maintainer
#13 lore raw
In reply to Herbert Xu
Quoted 6 lines · On Sun, 16 Jan 2005 00:31:46 +1100 Herbert Xu <[email protected]>…
Quoted 2 lines · Tommy Christensen

netlink has messed up the send buffer accounting, when trimming a skb.

You are spot on. Dave, you were right too about checking for charged skb's in netlink_trim.

It's good to know that I'm legitimately paranoid from time to time :-)

Quoted 6 lines

Since the skb will be orphaned on its way to the destination anyway, let's simply do that before the trimming. This shouldn't lead to skb leakage since the skb will either be charged to the destination socket or freed.

Looks good, I'll apply this.

Thanks Herbert.

#10 lore raw

Re: [NETLINK] Do not netlink_unicast shared packet in kernel/audit.c

In reply to David S. Miller
Quoted 9 lines · David S. Miller

I think this patch is buggy. At least in the final hunk, we do this:

	info.skb = skb;
	...
	skb = netlink_trim( ... );

Now info.skb can be pointing to a freed up skb.

Doh! Here is the corrected version.

Thanks Dave.

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
home/gondolin/herbert/src/bk/linux-2.6/net/netlink/af_netlink.c renamed from home/gondolin/herbert/src/bk/linux-2.6/net/netlink/af_netlink.c.orig +18 -8
@@ -660,7 +660,7 @@
 	sock_put(sk);
 }
 
-static inline void netlink_trim(struct sk_buff *skb, int allocation)
+static inline struct sk_buff *netlink_trim(struct sk_buff *skb, int allocation)
 {
 	int delta;
 
@@ -668,10 +668,20 @@
 
 	delta = skb->end - skb->tail;
 	if (delta * 2 < skb->truesize)
-		return;
-	if (pskb_expand_head(skb, 0, -delta, allocation))
-		return;
-	skb->truesize -= delta;
+		return skb;
+
+	if (skb_shared(skb)) {
+		struct sk_buff *nskb = skb_clone(skb, allocation);
+		if (!nskb)
+			return skb;
+		kfree_skb(skb);
+		skb = nskb;
+	}
+
+	if (!pskb_expand_head(skb, 0, -delta, allocation))
+		skb->truesize -= delta;
+
+	return skb;
 }
 
 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
@@ -680,7 +690,7 @@
 	int err;
 	long timeo;
 
-	netlink_trim(skb, gfp_any());
+	skb = netlink_trim(skb, gfp_any());
 
 	timeo = sock_sndtimeo(ssk, nonblock);
 retry:
@@ -778,6 +788,8 @@
 	struct hlist_node *node;
 	struct sock *sk;
 
+	skb = netlink_trim(skb, allocation);
+
 	info.exclude_sk = ssk;
 	info.pid = pid;
 	info.group = group;
@@ -788,8 +800,6 @@
 	info.skb = skb;
 	info.skb2 = NULL;
 
-	netlink_trim(skb, allocation);
-
 	/* While we sleep in clone, do not allow to change socket list */
 
 	netlink_lock_table();