xfrm: prevent ipcomp scratch buffer race condition
Subsystems (3)
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
@@ -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
Steffen Klassert
maintainerQuoted 19 lines · Michal Kubecek
In
ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten byipcomp_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).Signed-off-by Michal Kubecek <[email protected]>--- net/xfrm/xfrm_ipcomp.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)net/xfrm/xfrm_ipcomp.c +0
-0
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!
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 byipcomp_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).Signed-off-by Michal Kubecek<[email protected]>--- net/xfrm/xfrm_ipcomp.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)net/xfrm/xfrm_ipcomp.c +1
-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;
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 inipcomp_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
Steffen Klassert
maintainerQuoted 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 inipcomp_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.
Quoted 12 lines · Steffen Klassert
Quoted 12 lines
net/xfrm/xfrm_ipcomp.c +1
-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;
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 inipcomp_compress().
Sounds like a good idea. I'll send v2 after some basic testing.
Michal Kubecek
PATCH v2 1/1 xfrm: prevent ipcomp scratch buffer race condition
In reply to Steffen KlassertIn 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
@@ -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
Quoted 30 lines · Michal Kubecek
In
ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten byipcomp_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().Signed-off-by Michal Kubecek <[email protected]>--- net/xfrm/xfrm_ipcomp.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-)net/xfrm/xfrm_ipcomp.c +0
-4@@ -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 ?
Herbert Xu
maintainerQuoted 4 lines · Michal Kubecek
In
ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten byipcomp_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
Steffen Klassert
maintainerQuoted 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 byipcomp_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_inputwill dead-lock.
Michal, please incorporate the feedback from Herbert and Eric, I'll take it into the ipsec tree then. Thanks!
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
@@ -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
Quoted 9 lines · Michal Kubecek
In
ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten byipcomp_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_decompresspart (as explained by Herbert Xu, it cannot be called from process context), get rid of cpu variable (thanks to Eric Dumazet)Signed-off-by Michal Kubecek <[email protected]>---
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 byipcomp_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_decompresspart (as explained by Herbert Xu, it cannot be called from process context), get rid of cpu variable (thanks to Eric Dumazet)Signed-off-by Michal Kubecek <[email protected]>---
Reviewed-by Eric Dumazet <[email protected]>
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
Quoted 9 lines · Michal Kubecek
In
ipcomp_compress(), sortirq is enabled too early, allowing the per-cpu scratch buffer to be rewritten byipcomp_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_decompresspart (as explained by Herbert Xu, it cannot be called from process context), get rid of cpu variable (thanks to Eric Dumazet)Signed-off-by Michal Kubecek <[email protected]>
Applied to the ipsec tree, thanks everyone!
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
@@ -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;
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.Signed-off-by Eric Dumazet <[email protected]>
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
Re: [PATCH ipsec-next] xfrm: use vmalloc_node() for percpu scratches
In reply to Eric DumazetQuoted 6 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
scratches are per cpu, we can use
vmalloc_node()for proper NUMA affinity.Signed-off-by Eric Dumazet <[email protected]>
Applied to ipsec-next, thanks a lot Eric!
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_inputwill 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
Herbert Xu
maintainerQuoted 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
Quoted 8 lines · Michal Kubecek
Quoted 2 lines
Am I wrong?
I hope so :) Because otherwise
xfrm_inputwill surely dead-lock since it usesspin_lock.The cases you mentioned should all go through
netif_rxwhich will then do the processing in BH context.
You are right. Thank you for the correction.
Michal Kubecek