Skip to contentSkip to conversation

TCP: Replace __kfree_skb() with kfree_skb()

PATCH In review Masayuki Nakagawa 5 participants 14 messages Jan 26, 2007 – Jan 29, 2007

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

PATCH 1/1 TCP: Replace __kfree_skb() with kfree_skb()

This patch simply replaces __kfree_skb() in exit path with kfree_skb(). In tcp_rcv_state_process(), generally skbs should be destroyed only when the ref count is zero.
That is the way things are supposed to be done in the kernel.

This change might reveals a memory leak of skb.
If it happens, it would be because someone doesn't deal with the skb properly.

net/ipv4/tcp_input.c renamed from net/ipv4/tcp_input.c.orig +1 -3
@@ -4423,8 +4423,6 @@ int tcp_rcv_state_process(struct sock *s
 			 * in the interest of security over speed unless
 			 * it's still in use.
 			 */
-			kfree_skb(skb);
-			return 0;
 		}
 		goto discard;
 
@@ -4634,7 +4632,7 @@ int tcp_rcv_state_process(struct sock *s
 
 	if (!queued) {
 discard:
-		__kfree_skb(skb);
+		kfree_skb(skb);
 	}
 	return 0;
 }
#1 lore raw
Quoted 7 lines · Masayuki Nakagawa

This patch simply replaces __kfree_skb() in exit path with kfree_skb(). In tcp_rcv_state_process(), generally skbs should be destroyed only when the ref count is zero.
That is the way things are supposed to be done in the kernel.

This change might reveals a memory leak of skb.
If it happens, it would be because someone doesn't deal with the skb properly.

So maybe for some time there should be added skb->users test with a warning to reveal this?

Regards,
Jarek P.

#2 lore raw
Quoted 3 lines · Jarek Poplawski

So maybe for some time there should be added skb->users test with a warning to reveal this?

Or just replace __kfree_skb by kfree_skb. The atomic operation (the expensive part of kfree_skb vs. __kfree_skb) is only taken when it is needed.

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
#3 lore raw
Quoted 8 lines · Herbert Xu
Quoted 3 lines · Jarek Poplawski

So maybe for some time there should be added skb->users test with a warning to reveal this?

Or just replace __kfree_skb by kfree_skb. The atomic operation (the expensive part of kfree_skb vs. __kfree_skb) is only taken when it is needed.

But:

Quoted 2 lines

This change might reveals a memory leak of skb.
If it happens, it would be because someone doesn't deal with the skb properly.

How do we know about those improper deals? I understand there should be no other users here if it's __kfree_skb now. So I mean to test and warn before kfree_skb for some debugging time.

Jarek P.

#4 lore raw
Quoted 5 lines · Jarek Poplawski

How do we know about those improper deals? I understand there should be no other users here if it's __kfree_skb now. So I mean to test and warn before kfree_skb for some debugging time.

We only need to do that if there is a legitimate reason to use __kfree_skb. Which there was when this code was first written since kfree_skb had an unconditional atomic op back then.

Now that it's a conditinoal atomic op __kfree_skb is no longer necessary.

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
#5 lore raw
Quoted 7 lines · Herbert Xu
Quoted 5 lines · Jarek Poplawski

How do we know about those improper deals? I understand there should be no other users here if it's __kfree_skb now. So I mean to test and warn before kfree_skb for some debugging time.

Quoted 6 lines

We only need to do that if there is a legitimate reason to use __kfree_skb. Which there was when this code was first written since kfree_skb had an unconditional atomic op back then.

Now that it's a conditinoal atomic op __kfree_skb is no longer necessary.

I don't mean it's necessary. I mean now skb is freed unconditionally and after this patch, if there is some error in counting, skb will stay. I thought Masayuki wrote about such possibility, but if I missed his point, then the rest is really O.K.

Cheers,
Jarek P.

#6 lore raw
Quoted 6 lines · Jarek Poplawski

I don't mean it's necessary. I mean now skb is freed unconditionally and after this patch, if there is some error in counting, skb will stay. I thought Masayuki wrote about such possibility, but if I missed his point, then the rest is really O.K.

OK, I see what you mean.

I'm not aware of anybody who has coded in this way. Alexey & Dave, do you know of any place where __kfree_skb is used to free an skb whose ref count is greater than 1?

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
maintainer
#7 lore raw

From: Herbert Xu <[email protected]>
Date: Fri, 26 Jan 2007 21:45:18 +1100

Quoted 3 lines

I'm not aware of anybody who has coded in this way. Alexey & Dave, do you know of any place where __kfree_skb is used to free an skb whose ref count is greater than 1?

Not at all.

#8 lore raw
In reply to Herbert Xu
Quoted 8 lines · Herbert Xu
Quoted 6 lines · Jarek Poplawski

I don't mean it's necessary. I mean now skb is freed unconditionally and after this patch, if there is some error in counting, skb will stay. I thought Masayuki wrote about such possibility, but if I missed his point, then the rest is really O.K.

Quoted 5 lines

OK, I see what you mean.

I'm not aware of anybody who has coded in this way. Alexey & Dave, do you know of any place where __kfree_skb is used to free an skb whose ref count is greater than 1?

I'm sure it wasn't done on purpose! But it could hide some errors anyway.

Jarek P.

#9 lore raw
Quoted 9 lines · Jarek Poplawski
Quoted 8 lines · Herbert Xu
Quoted 6 lines · Jarek Poplawski

I don't mean it's necessary. I mean now skb is freed unconditionally and after this patch, if there is some error in counting, skb will stay. I thought Masayuki wrote about such possibility, but if I missed his point, then the rest is really O.K.

Quoted 8 lines
Quoted 5 lines

OK, I see what you mean.

I'm not aware of anybody who has coded in this way. Alexey & Dave, do you know of any place where __kfree_skb is used to free an skb whose ref count is greater than 1?

I'm sure it wasn't done on purpose! But it could hide some errors anyway.

But not sure anymore...

I've only now read the original thread of this problem and this long note about security. I need more time to understand this, but now I'm not sure Masayuki's server isn't doing something against the policy of this note (at the first sight it looks like this kind of skb isn't welcommed there).

So maybe I miss something, but there could be also possibility this __kfree_skb prevents some errors in a hard way - I'm not sure: on purpose or accidentally.

Have a lot of fun in the weekend,
Jarek P.

#10 lore raw

On Fri, Jan 26, 2007 at 02:18:08PM +0100, Jarek Poplawski wrote:
...

Quoted 4 lines

I've only now read the original thread of this problem and this long note about security. I need more time to understand this, but now I'm not sure Masayuki's server isn't doing something against the policy of this note

This would be socket case then (not server), sorry.

Jarek P.

#11 lore raw
In reply to Herbert Xu

Hello!

Quoted 2 lines

do you know of any place where __kfree_skb is used to free an skb whose ref count is greater than 1?

No.

Actually, since kfree_skb is not inline, __kfree_skb could be made static and remaining places still using it switched to kfree_skb.

#12 lore raw
In reply to Herbert Xu
Quoted 9 lines · Herbert Xu
Quoted 6 lines · Jarek Poplawski

I don't mean it's necessary. I mean now skb is freed unconditionally and after this patch, if there is some error in counting, skb will stay. I thought Masayuki wrote about such possibility, but if I missed his point, then the rest is really O.K.

OK, I see what you mean.

Now I see what I mean too... And it's very bad!

This patch was done to allow more skb->users here, so this debugging would warn mainly about something we've just enabled.

I was simply fooled by this change - how it was possible to work all these days? I see, the main idea
is: the data from skb is copied at this level only and no cloning (tcp_ipv4). Except tcp_ipv6.

So this patch is changing it to ipv6 way. It seems to be quite brave. I think there was some reason to do this "old way" and I hope consequences were analyzed.

There are also other doubts: what decisions are made? According to tcp_rcv_state_process skb should be dropped. tcp_v6_do_rcv seems to have other opinion. And after this change there could be, probably, more such doubtful places.

Maybe it would be sufficient to enable this by some additional "if" and return code from conn_request in the "if (th->syn)" block?

Regards,
Jarek P.

#13 lore raw

On Mon, Jan 29, 2007 at 09:26:16AM +0100, Jarek Poplawski wrote:
...

Quoted 3 lines

So this patch is changing it to ipv6 way. It seems to be quite brave. I think there was some reason to do this "old way" and I hope consequences were analyzed.

...

But since Alexey Kuznetsov accepted this change too, they surely were. Let's forget about my doubts.

Sorry for disturbing,
Jarek P.