Skip to contentSkip to conversation

xfrm: prevent ipcomp scratch buffer race condition

PATCH Superseded Michal Kubecek 5 participants 19 messages Oct 14, 2013 – Oct 21, 2013

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

PATCH 1/1 xfrm: prevent ipcomp scratch buffer race condition

In ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten by ipcomp_decompress() (called on the same CPU in softirq context) between populating the buffer and copying the compressed data to the skb.

Add similar protection into ipcomp_decompress() as it can be called from process context as well (even if such scenario seems a bit artificial).

---
 net/xfrm/xfrm_ipcomp.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/xfrm/xfrm_ipcomp.c +6 -2
index 2906d52..96946fb 100644
@@ -48,9 +48,11 @@ static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
 	const int cpu = get_cpu();
 	u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
 	struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
-	int err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
+	int err;
 	int len;
 
+	local_bh_disable();
+	err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
 	if (err)
 		goto out;
 
@@ -103,6 +105,7 @@ static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
 	err = 0;
 
 out:
+	local_bh_enable();
 	put_cpu();
 	return err;
 }
@@ -148,7 +151,6 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
 
 	local_bh_disable();
 	err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
-	local_bh_enable();
 	if (err)
 		goto out;
 
@@ -158,12 +160,14 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
 	}
 
 	memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
+	local_bh_enable();
 	put_cpu();
 
 	pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
 	return 0;
 
 out:
+	local_bh_enable();
 	put_cpu();
 	return err;
 }
Signature · 1 line
1.8.1.4
maintainer
#1 lore raw
Quoted 19 lines · Michal Kubecek

In ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten by ipcomp_decompress() (called on the same CPU in softirq context) between populating the buffer and copying the compressed data to the skb.

Add similar protection into ipcomp_decompress() as it can be called from process context as well (even if such scenario seems a bit artificial).

---
 net/xfrm/xfrm_ipcomp.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/xfrm/xfrm_ipcomp.c +0 -0
index 2906d52..96946fb 100644

net/xfrm/xfrm_ipcomp.c static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb) · line 54

Quoted 8 lines
net/xfrm/xfrm_ipcomp.c +2 -1
@@ -48,9 +48,11 @@ static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
 	const int cpu = get_cpu();
 	u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
 	struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
-	int err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
+	int err;
 	int len;
 
+	local_bh_disable();

Maybe we could disable the BHs before we fetch the percpu pointers. Then we can use smp_processor_id() to get the cpu. With that we could get rid of a (now useless) preempt_disable()/preempt_enable() pair. Same could be done in ipcomp_compress().

Looks ok otherwise. Thanks!

#2 lore raw
Quoted 27 lines · Steffen Klassert
Quoted 26 lines · Michal Kubecek

In ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten by ipcomp_decompress() (called on the same CPU in softirq context) between populating the buffer and copying the compressed data to the skb.

Add similar protection into ipcomp_decompress() as it can be called from process context as well (even if such scenario seems a bit artificial).

---
  net/xfrm/xfrm_ipcomp.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
net/xfrm/xfrm_ipcomp.c +1 -1
index 2906d52..96946fb 100644
@@ -48,9 +48,11 @@ static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
  	const int cpu = get_cpu();
  	u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
  	struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
-	int err = crypto_comp_decompress(tfm, start, plen, scratch,&dlen);
+	int err;
  	int len;
 
Quoted 6 lines

net/xfrm/xfrm_ipcomp.c static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb) · line 54

Quoted 1 line
net/xfrm/xfrm_ipcomp.c +1 -0
+	local_bh_disable();

Maybe we could disable the BHs before we fetch the percpu pointers. Then we can use smp_processor_id() to get the cpu. With that we could get rid of a (now useless) preempt_disable()/preempt_enable() pair. Same could be done in ipcomp_compress().

Is it possible that two tasks race scratch buffer when both of them trying to compress data without preempt disabled? for example, when task A working on compression, then task B with higher priority preempts task A, and try to touch scratch buffer, which leaves stale data for task A after then.

I think we needs preempt disabled for such case, otherwise I overlook codes in somewhere else.

Quoted 7 lines

Looks ok otherwise. Thanks!

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 3 lines
浮沉随浪只记今朝笑

--fan
maintainer
#3 lore raw
Quoted 9 lines · Fan Du
Quoted 5 lines · Steffen Klassert

Maybe we could disable the BHs before we fetch the percpu pointers. Then we can use smp_processor_id() to get the cpu. With that we could get rid of a (now useless) preempt_disable()/preempt_enable() pair. Same could be done in ipcomp_compress().

Quoted 7 lines

Is it possible that two tasks race scratch buffer when both of them trying to compress data without preempt disabled? for example, when task A working on compression, then task B with higher priority preempts task A, and try to touch scratch buffer, which leaves stale data for task A after then.

I think we needs preempt disabled for such case, otherwise I overlook codes in somewhere else.

You overlook that preemption is disabled if the BHs are disabled.

#4 lore raw
In reply to Steffen Klassert
Quoted 12 lines · Steffen Klassert
Quoted 12 lines
net/xfrm/xfrm_ipcomp.c +1 -1
index 2906d52..96946fb 100644
@@ -48,9 +48,11 @@ static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
 	const int cpu = get_cpu();
 	u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
 	struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
-	int err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
+	int err;
 	int len;
 
Quoted 6 lines

net/xfrm/xfrm_ipcomp.c static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb) · line 54

Quoted 1 line
net/xfrm/xfrm_ipcomp.c +1 -0
+	local_bh_disable();

Maybe we could disable the BHs before we fetch the percpu pointers. Then we can use smp_processor_id() to get the cpu. With that we could get rid of a (now useless) preempt_disable()/preempt_enable() pair. Same could be done in ipcomp_compress().

Sounds like a good idea. I'll send v2 after some basic testing.

Michal Kubecek

#5 lore raw

PATCH v2 1/1 xfrm: prevent ipcomp scratch buffer race condition

In reply to Steffen Klassert

In ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten by ipcomp_decompress() (called on the same CPU in softirq context) between populating the buffer and copying the compressed data to the skb.

Add similar protection into ipcomp_decompress() as it can be called from process context as well (even if such scenario seems a bit artificial).

v2: as pointed out by Steffen Klassert, if we also move the local_bh_disable() before reading the per-cpu pointers, we can get rid of get_cpu()/put_cpu().

---
 net/xfrm/xfrm_ipcomp.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
net/xfrm/xfrm_ipcomp.c +18 -11
index 2906d52..9fe4f42 100644
@@ -45,12 +45,17 @@ static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
 	const int plen = skb->len;
 	int dlen = IPCOMP_SCRATCH_SIZE;
 	const u8 *start = skb->data;
-	const int cpu = get_cpu();
-	u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
-	struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
-	int err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
+	struct crypto_comp *tfm;
+	u8 *scratch;
+	int cpu;
+	int err;
 	int len;
 
+	local_bh_disable();
+	cpu = smp_processor_id();
+	scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
+	tfm = *per_cpu_ptr(ipcd->tfms, cpu);
+	err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
 	if (err)
 		goto out;
 
@@ -103,7 +108,7 @@ static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
 	err = 0;
 
 out:
-	put_cpu();
+	local_bh_enable();
 	return err;
 }
 
@@ -141,14 +146,16 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
 	const int plen = skb->len;
 	int dlen = IPCOMP_SCRATCH_SIZE;
 	u8 *start = skb->data;
-	const int cpu = get_cpu();
-	u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
-	struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
+	struct crypto_comp *tfm;
+	u8 *scratch;
+	int cpu;
 	int err;
 
 	local_bh_disable();
+	cpu = smp_processor_id();
+	scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
+	tfm = *per_cpu_ptr(ipcd->tfms, cpu);
 	err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
-	local_bh_enable();
 	if (err)
 		goto out;
 
@@ -158,13 +165,13 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
 	}
 
 	memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
-	put_cpu();
+	local_bh_enable();
 
 	pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
 	return 0;
 
 out:
-	put_cpu();
+	local_bh_enable();
 	return err;
 }
 
Signature · 1 line
1.8.1.4
#6 lore raw
Quoted 30 lines · Michal Kubecek

In ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten by ipcomp_decompress() (called on the same CPU in softirq context) between populating the buffer and copying the compressed data to the skb.

Add similar protection into ipcomp_decompress() as it can be called from process context as well (even if such scenario seems a bit artificial).

v2: as pointed out by Steffen Klassert, if we also move the local_bh_disable() before reading the per-cpu pointers, we can get rid of get_cpu()/put_cpu().

---
 net/xfrm/xfrm_ipcomp.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
net/xfrm/xfrm_ipcomp.c +0 -4
index 2906d52..9fe4f42 100644
@@ -45,12 +45,17 @@ static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
 	const int plen = skb->len;
 	int dlen = IPCOMP_SCRATCH_SIZE;
 	const u8 *start = skb->data;
-	const int cpu = get_cpu();
-	u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
-	struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
-	int err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);

net/xfrm/xfrm_ipcomp.c static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb) · line 57

Quoted 10 lines
net/xfrm/xfrm_ipcomp.c +8 -0
+	struct crypto_comp *tfm;
+	u8 *scratch;
+	int cpu;
+	int err;
 	int len;
 
+	local_bh_disable();
+	cpu = smp_processor_id();
+	scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
+	tfm = *per_cpu_ptr(ipcd->tfms, cpu);

Have you tried this_cpu_ptr() instead ?

maintainer
#7 lore raw
In reply to Michal Kubecek
Quoted 4 lines · Michal Kubecek

In ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten by ipcomp_decompress() (called on the same CPU in softirq context) between populating the buffer and copying the compressed data to the skb.

Good catch.

Quoted 3 lines

Add similar protection into ipcomp_decompress() as it can be called from process context as well (even if such scenario seems a bit artificial).

I don't think this is possible or otherwise xfrm_input will dead-lock.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#8 lore raw
Quoted 6 lines · Herbert Xu
Quoted 4 lines · Michal Kubecek

In ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten by ipcomp_decompress() (called on the same CPU in softirq context) between populating the buffer and copying the compressed data to the skb.

Quoted 9 lines

Good catch.

Quoted 3 lines

Add similar protection into ipcomp_decompress() as it can be called from process context as well (even if such scenario seems a bit artificial).

I don't think this is possible or otherwise xfrm_input will dead-lock.

Michal, please incorporate the feedback from Herbert and Eric, I'll take it into the ipsec tree then. Thanks!

#9 lore raw

PATCH v3 1/1 xfrm: prevent ipcomp scratch buffer race condition

In ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten by ipcomp_decompress() (called on the same CPU in softirq context) between populating the buffer and copying the compressed data to the skb.

v2: as pointed out by Steffen Klassert, if we also move the local_bh_disable() before reading the per-cpu pointers, we can get rid of get_cpu()/put_cpu().

v3: removed ipcomp_decompress part (as explained by Herbert Xu, it cannot be called from process context), get rid of cpu variable (thanks to Eric Dumazet)

---
 net/xfrm/xfrm_ipcomp.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
net/xfrm/xfrm_ipcomp.c +6 -6
index 2906d52..3be02b6 100644
@@ -141,14 +141,14 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
 	const int plen = skb->len;
 	int dlen = IPCOMP_SCRATCH_SIZE;
 	u8 *start = skb->data;
-	const int cpu = get_cpu();
-	u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
-	struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
+	struct crypto_comp *tfm;
+	u8 *scratch;
 	int err;
 
 	local_bh_disable();
+	scratch = *this_cpu_ptr(ipcomp_scratches);
+	tfm = *this_cpu_ptr(ipcd->tfms);
 	err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
-	local_bh_enable();
 	if (err)
 		goto out;
 
@@ -158,13 +158,13 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
 	}
 
 	memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
-	put_cpu();
+	local_bh_enable();
 
 	pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
 	return 0;
 
 out:
-	put_cpu();
+	local_bh_enable();
 	return err;
 }
 
Signature · 1 line
1.8.1.4
Reviewed-by on v3 1/1
#10 lore raw
Quoted 9 lines · Michal Kubecek

In ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten by ipcomp_decompress() (called on the same CPU in softirq context) between populating the buffer and copying the compressed data to the skb.

v2: as pointed out by Steffen Klassert, if we also move the local_bh_disable() before reading the per-cpu pointers, we can get rid of get_cpu()/put_cpu().

Quoted 6 lines

v3: removed ipcomp_decompress part (as explained by Herbert Xu, it cannot be called from process context), get rid of cpu variable (thanks to Eric Dumazet)

---

maintainer Acked-by
#11 lore raw
Quoted 10 lines · Eric Dumazet
Quoted 9 lines · Michal Kubecek

In ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten by ipcomp_decompress() (called on the same CPU in softirq context) between populating the buffer and copying the compressed data to the skb.

v2: as pointed out by Steffen Klassert, if we also move the local_bh_disable() before reading the per-cpu pointers, we can get rid of get_cpu()/put_cpu().

Quoted 8 lines
Quoted 6 lines

v3: removed ipcomp_decompress part (as explained by Herbert Xu, it cannot be called from process context), get rid of cpu variable (thanks to Eric Dumazet)

---

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer Applied
#12 lore raw
In reply to Michal Kubecek
Quoted 9 lines · Michal Kubecek

In ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten by ipcomp_decompress() (called on the same CPU in softirq context) between populating the buffer and copying the compressed data to the skb.

v2: as pointed out by Steffen Klassert, if we also move the local_bh_disable() before reading the per-cpu pointers, we can get rid of get_cpu()/put_cpu().

Quoted 5 lines

v3: removed ipcomp_decompress part (as explained by Herbert Xu, it cannot be called from process context), get rid of cpu variable (thanks to Eric Dumazet)

Applied to the ipsec tree, thanks everyone!

#13 lore raw

PATCH 1/1 xfrm: use vmalloc_node() for percpu scratches

From: Eric Dumazet <[email protected]>

scratches are per cpu, we can use vmalloc_node() for proper NUMA affinity.

---
 net/xfrm/xfrm_ipcomp.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/xfrm/xfrm_ipcomp.c +4 -2
index 2906d52..b943c7f 100644
@@ -220,8 +220,8 @@ static void ipcomp_free_scratches(void)
 
 static void * __percpu *ipcomp_alloc_scratches(void)
 {
-	int i;
 	void * __percpu *scratches;
+	int i;
 
 	if (ipcomp_scratch_users++)
 		return ipcomp_scratches;
@@ -233,7 +233,9 @@ static void * __percpu *ipcomp_alloc_scratches(void)
 	ipcomp_scratches = scratches;
 
 	for_each_possible_cpu(i) {
-		void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
+		void *scratch;
+
+		scratch = vmalloc_node(IPCOMP_SCRATCH_SIZE, cpu_to_node(i));
 		if (!scratch)
 			return NULL;
 		*per_cpu_ptr(scratches, i) = scratch;
maintainer Acked-by on v1 1/1
#14 lore raw

Re: [PATCH ipsec-next] xfrm: use vmalloc_node() for percpu scratches

Quoted 6 lines · Eric Dumazet

From: Eric Dumazet <[email protected]>

scratches are per cpu, we can use vmalloc_node() for proper NUMA affinity.

I agree completely.

Thanks!

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#16 lore raw
In reply to Herbert Xu
Quoted 8 lines · Herbert Xu
Quoted 3 lines · Michal Kubecek

Add similar protection into ipcomp_decompress() as it can be called from process context as well (even if such scenario seems a bit artificial).

I don't think this is possible or otherwise xfrm_input will dead-lock.

When I was thinking about this, I came with two situations when this might IMHO happen:

1. We are sending a packet to ourselves; this could mean not only a local loop but also e.g. a packets between an LXC container and the host or between two LXC containers. While it doesn't make much sense to use compression in such case, it might happen while testing a solution which is going to be used in a normal setup later.

2. The packet was passed to tun/tap device by a userspace application.

Am I wrong?

Michal Kubecek

maintainer
#17 lore raw
Quoted 10 lines · Michal Kubecek

When I was thinking about this, I came with two situations when this might IMHO happen:

1. We are sending a packet to ourselves; this could mean not only a local loop but also e.g. a packets between an LXC container and the host or between two LXC containers. While it doesn't make much sense to use compression in such case, it might happen while testing a solution which is going to be used in a normal setup later.

Quoted 3 lines

2. The packet was passed to tun/tap device by a userspace application.

Am I wrong?

I hope so :) Because otherwise xfrm_input will surely dead-lock since it uses spin_lock.

The cases you mentioned should all go through netif_rx which will then do the processing in BH context.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#18 lore raw
Quoted 8 lines · Michal Kubecek
Quoted 2 lines

Am I wrong?

I hope so :) Because otherwise xfrm_input will surely dead-lock since it uses spin_lock.

The cases you mentioned should all go through netif_rx which will then do the processing in BH context.

You are right. Thank you for the correction.

Michal Kubecek