net/flow: fix fc->percpu NULL pointer dereference
Subsystems (3)
PATCH 1/1 net/flow: fix fc->percpu NULL pointer dereference
Now we will force to do garbage collection if any policy removed in xfrm_policy_flush(). But during xfrm_net_exit(). We call flow_cache_fini() first and set set fc->percpu to NULL. Then after we call xfrm_policy_fini() -> frxm_policy_flush() -> flow_cache_flush(), we will get NULL pointer dereference when check percpu_empty. The code path looks like:
flow_cache_fini()
- fc->percpu = NULLxfrm_policy_fini()
- xfrm_policy_flush()
- xfrm_garbage_collect()
- flow_cache_flush()
- flow_cache_percpu_empty()
- fcp = per_cpu_ptr(fc->percpu, cpu)
To reproduce, just add ipsec in netns and then remove the netns.
--- net/core/flow.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
net/core/flow.c +7 -4
@@ -332,10 +332,13 @@ static int flow_cache_percpu_empty(struct flow_cache *fc, int cpu)struct flow_cache_percpu *fcp;unsigned int i; - fcp =per_cpu_ptr (fc->percpu, cpu); -for (i =0 ; i <flow_cache_hash_size (fc); i++) -if (!hlist_empty (&fcp->hash_table[i])) -return 0 ; +if (fc->percpu) { + fcp =per_cpu_ptr (fc->percpu, cpu); +for (i =0 ; i <flow_cache_hash_size (fc); i++) +if (!hlist_empty (&fcp->hash_table[i])) +return 0 ; + } +return 1 ; }
Signature · 1 line
2.5.5
Hi Steffen,
BTW, If we put the check in xfrm_policy_flush(), we can prevent it earlier. But If we put the check in flow_cache_percpu_empty(), we can prevent other functions set fc->percpu to NULL, although not much possible : )
So I'm not quite sure whether we should put the check in flow_cache_percpu_empty() or in xfrm_policy_flush().
Do you have any suggestion?
Thanks
Hangbin
2017-06-09 16:13 GMT+08:00 Hangbin Liu <[email protected]>:
Quoted 48 lines
Now we will force to do garbage collection if any policy removed in
xfrm_policy_flush(). But duringxfrm_net_exit(). We callflow_cache_fini()first and set setfc->percputo NULL. Then after we callxfrm_policy_fini()->frxm_policy_flush()->flow_cache_flush(), we will get NULL pointer dereference when checkpercpu_empty. The code path looks like:
flow_cache_fini()
-fc->percpu= NULLxfrm_policy_fini()
-xfrm_policy_flush()
-xfrm_garbage_collect()
-flow_cache_flush()
-flow_cache_percpu_empty()
- fcp =per_cpu_ptr(fc->percpu, cpu)To reproduce, just add ipsec in netns and then remove the netns.
Fixes 35db06912189 ("xfrm: do the garbage collection after flushing policy")Signed-off-by Hangbin Liu <[email protected]>--- net/core/flow.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)net/core/flow.c +7
-5@@ -332,10 +332,13 @@ static int flow_cache_percpu_empty(struct flow_cache *fc, int cpu)struct flow_cache_percpu *fcp;unsigned int i; - fcp =per_cpu_ptr (fc->percpu, cpu); -for (i =0 ; i <flow_cache_hash_size (fc); i++) -if (!hlist_empty (&fcp->hash_table[i])) -return 0 ; +if (fc->percpu) { + fcp =per_cpu_ptr (fc->percpu, cpu); +for (i =0 ; i <flow_cache_hash_size (fc); i++) +if (!hlist_empty (&fcp->hash_table[i])) +return 0 ; + } +return 1 ; } --2.5.5
Steffen Klassert
maintainerQuoted 8 lines · Hangbin Liu
Hi Steffen,
BTW, If we put the check in
xfrm_policy_flush(), we can prevent it earlier. But If we put the check inflow_cache_percpu_empty(), we can prevent other functions setfc->percputo NULL, although not much possible : )So I'm not quite sure whether we should put the check in
flow_cache_percpu_empty()or inxfrm_policy_flush().
Can't we just call xfrm_policy_fini() first and then flow_cache_fini()?
Quoted 12 lines · Steffen Klassert
Quoted 8 lines · Hangbin Liu
Hi Steffen,
BTW, If we put the check in
xfrm_policy_flush(), we can prevent it earlier. But If we put the check inflow_cache_percpu_empty(), we can prevent other functions setfc->percputo NULL, although not much possible : )So I'm not quite sure whether we should put the check in
flow_cache_percpu_empty()or inxfrm_policy_flush().Can't we just call
xfrm_policy_fini()first and thenflow_cache_fini()?
That would be a better fix. seems safe as what flow_cache_fini does is only to free fcp->hash_table and stop timer, I didn't see it has any dependence on xfrm_policy stuff.
2017-06-09 16:43 GMT+08:00 Xin Long <[email protected]>:
Quoted 5 lines
Quoted 3 lines · Steffen Klassert
Quoted 2 lines · Hangbin Liu
Hi Steffen,
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
BTW, If we put the check in
xfrm_policy_flush(), we can prevent it earlier. But If we put the check inflow_cache_percpu_empty(), we can prevent other functions setfc->percputo NULL, although not much possible : )So I'm not quite sure whether we should put the check in
flow_cache_percpu_empty()or inxfrm_policy_flush().Can't we just call
xfrm_policy_fini()first and thenflow_cache_fini()?
Yes, that would be easy fix. I have been thinking about that. But if we change
the order in xfrm_net_exit(), do we also need to change the order in xfrm_net_init()? That would change a lot.
If no need, that would be good.
Quoted 4 lines
Quoted 1 line
That would be a better fix. seems safe as what
flow_cache_finidoes is only to freefcp->hash_tableand stop timer, I didn't see it has any dependence onxfrm_policystuff.
I'm not familiar about this part. So not sure about the influence if we free the flow cache after xfrm_policy_fini(). I need do some test first.
I would also be appreciate if you or some one could make sure if it doesn't influence anything.
Thanks
Hangbin
Quoted 26 lines · Hangbin Liu
2017-06-09 16:43 GMT+08:00 Xin Long <[email protected]>:
Quoted 5 lines
Quoted 3 lines · Steffen Klassert
Quoted 2 lines · Hangbin Liu
Hi Steffen,
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
BTW, If we put the check in
xfrm_policy_flush(), we can prevent it earlier. But If we put the check inflow_cache_percpu_empty(), we can prevent other functions setfc->percputo NULL, although not much possible : )So I'm not quite sure whether we should put the check in
flow_cache_percpu_empty()or inxfrm_policy_flush().Can't we just call
xfrm_policy_fini()first and thenflow_cache_fini()?Yes, that would be easy fix. I have been thinking about that. But if we change
the order inxfrm_net_exit(), do we also need to change the order inxfrm_net_init()? That would change a lot.If no need, that would be good.
Quoted 4 lines
Quoted 1 line
That would be a better fix. seems safe as what
flow_cache_finidoes is only to freefcp->hash_tableand stop timer, I didn't see it has any dependence onxfrm_policystuff.
Quoted 6 lines
I'm not familiar about this part. So not sure about the influence if we free the flow cache after
xfrm_policy_fini(). I need do some test first.I would also be appreciate if you or some one could make sure if it doesn't influence anything.
another fix is to move xfrm_garbage_collect out of xfrm_policy_flush. I could only see two places need to call it.
something like:
net/key/af_key.c +0 -0
@@ -2755,6 +2755,8 @@ static int pfkey_spdflush(struct sock *sk,
struct sk_buff *skb, const struct sad
int err, err2;
err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
+ if (!err)
+ xfrm_garbage_collect(net);
err2 = unicast_flush_resp(sk, hdr);
if (err || err2) {
if (err == -ESRCH) /* empty table - old silent behavior */net/xfrm/xfrm_policy.c +0 -0
@@ -1007,9 +1007,6 @@ int xfrm_policy_flush(struct net *net, u8 type,
bool task_valid)
out:
spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
- if (cnt)
- xfrm_garbage_collect(net);
-
return err;
}
EXPORT_SYMBOL(xfrm_policy_flush);
net/xfrm/xfrm_user.c +0 -0
@@ -2027,6 +2027,7 @@ static int xfrm_flush_policy(struct sk_buff
*skb, struct nlmsghdr *nlh,
return 0;
return err;
}+ xfrm_garbage_collect(net);
Quoted 11 lines · Xin Long
another fix is to move
xfrm_garbage_collectout ofxfrm_policy_flush. I could only see two places need to call it.
something like:net/key/af_key.c +0
-0@@ -2755,6 +2755,8 @@ static int pfkey_spdflush(struct sock *sk,struct
sk_buff*skb, const struct sadint err, err2; err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
net/key/af_key.c
Quoted 5 lines
net/key/af_key.c +2
-0+if (!err) +xfrm_garbage_collect (net); err2 =unicast_flush_resp (sk, hdr);if (err || err2) {if (err == -ESRCH )/* empty table - old silent behavior */
Hmm, that would be better, just the xfrm_garbage_collect() need to be after err || err2 check
+ xfrm_garbage_collect(net);
I will send v2 patch.
Thanks
Hangbin
2017-06-09 20:29 GMT+08:00 Hangbin Liu <[email protected]>:
Quoted 21 lines · Xin Long
Quoted 11 lines
another fix is to move
xfrm_garbage_collectout ofxfrm_policy_flush. I could only see two places need to call it.
something like:net/key/af_key.c +0
-0@@ -2755,6 +2755,8 @@ static int pfkey_spdflush(struct sock *sk,struct
sk_buff*skb, const struct sadint err, err2; err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);net/key/af_key.c
Quoted 5 lines
net/key/af_key.c +2
-0+if (!err) +xfrm_garbage_collect (net); err2 =unicast_flush_resp (sk, hdr);if (err || err2) {if (err == -ESRCH )/* empty table - old silent behavior */ Hmm, that would be better, just the
xfrm_garbage_collect()need to be after err || err2 check
Quoted 1 line
+
xfrm_garbage_collect(net);
Ah, my mistake. we should check err first. Sorry.
Quoted 5 lines
I will send v2 patch.
Thanks
Hangbin
PATCH v2 1/1 net/flow: fix fc->percpu NULL pointer dereference
In reply to Hangbin LiuNow we will force to do garbage collection if any policy removed in xfrm_policy_flush(). But during xfrm_net_exit(). We call flow_cache_fini() first and set set fc->percpu to NULL. Then after we call xfrm_policy_fini() -> frxm_policy_flush() -> flow_cache_flush(), we will get NULL pointer dereference when check percpu_empty. The code path looks like:
flow_cache_fini()
- fc->percpu = NULLxfrm_policy_fini()
- xfrm_policy_flush()
- xfrm_garbage_collect()
- flow_cache_flush()
- flow_cache_percpu_empty()
- fcp = per_cpu_ptr(fc->percpu, cpu)
To reproduce, just add ipsec in netns and then remove the netns.
v2:
As Xin Long suggested, since only two other places need to call it. move xfrm_garbage_collect() outside xfrm_policy_flush().
--- net/key/af_key.c | 2 ++ net/xfrm/xfrm_policy.c | 4 ---- net/xfrm/xfrm_user.c | 1 + 3 files changed, 3 insertions(+), 4 deletions(-)
net/key/af_key.c +2 -0
@@ -2755,6 +2755,8 @@ static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadint err, err2; err =xfrm_policy_flush (net,XFRM_POLICY_TYPE_MAIN , true); +if (!err) +xfrm_garbage_collect (net); err2 =unicast_flush_resp (sk, hdr);if (err || err2) {if (err == -ESRCH )/* empty table - old silent behavior */
net/xfrm/xfrm_policy.c +0 -4
@@ -1006,10 +1006,6 @@ int xfrm_policy_flush(struct net *net, u8 type, bool task_valid) err = -ESRCH ; out:spin_unlock_bh (&net->xfrm.xfrm_policy_lock); - -if (cnt) -xfrm_garbage_collect (net); -return err; }EXPORT_SYMBOL (xfrm_policy_flush);
net/xfrm/xfrm_user.c +1 -0
@@ -2027,6 +2027,7 @@ static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,return 0 ;return err; } +xfrm_garbage_collect (net); c.data.type = type; c.event = nlh->nlmsg_type;
Signature · 1 line
2.5.5
Quoted 70 lines · Hangbin Liu
Now we will force to do garbage collection if any policy removed in
xfrm_policy_flush(). But duringxfrm_net_exit(). We callflow_cache_fini()first and set setfc->percputo NULL. Then after we callxfrm_policy_fini()->frxm_policy_flush()->flow_cache_flush(), we will get NULL pointer dereference when checkpercpu_empty. The code path looks like:
flow_cache_fini()
-fc->percpu= NULLxfrm_policy_fini()
-xfrm_policy_flush()
-xfrm_garbage_collect()
-flow_cache_flush()
-flow_cache_percpu_empty()
- fcp =per_cpu_ptr(fc->percpu, cpu)To reproduce, just add ipsec in netns and then remove the netns.
v2:
As Xin Long suggested, since only two other places need to call it. movexfrm_garbage_collect()outsidexfrm_policy_flush().Fixes 35db06912189 ("xfrm: do the garbage collection after flushing policy")Signed-off-by Hangbin Liu <[email protected]>--- net/key/af_key.c | 2 ++ net/xfrm/xfrm_policy.c | 4 ---- net/xfrm/xfrm_user.c | 1 + 3 files changed, 3 insertions(+), 4 deletions(-)net/key/af_key.c +2
-0@@ -2755,6 +2755,8 @@ static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadint err, err2; err =xfrm_policy_flush (net,XFRM_POLICY_TYPE_MAIN , true); +if (!err) +xfrm_garbage_collect (net); err2 =unicast_flush_resp (sk, hdr);if (err || err2) {if (err == -ESRCH )/* empty table - old silent behavior */ net/xfrm/xfrm_policy.c +0
-4@@ -1006,10 +1006,6 @@ int xfrm_policy_flush(struct net *net, u8 type, bool task_valid) err = -ESRCH ; out:spin_unlock_bh (&net->xfrm.xfrm_policy_lock); - -if (cnt) -xfrm_garbage_collect (net); -return err; }EXPORT_SYMBOL (xfrm_policy_flush);net/xfrm/xfrm_user.c +1
-1@@ -2027,6 +2027,7 @@ static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,return 0 ;return err; } +xfrm_garbage_collect (net); c.data.type = type; c.event = nlh->nlmsg_type; --
Quoted 2 lines
2.5.5
It's a xfrm fix, pls also fix the title, like:
xfrm: move xfrm_garbage_collect out of xfrm_policy_flush
or
xfrm: fix ...
Quoted 4 lines · Xin Long
It's a xfrm fix, pls also fix the title, like:
xfrm: movexfrm_garbage_collectout ofxfrm_policy_flush
or
xfrm: fix ...
Opps, sorry forgot that.
PATCH v3 1/1 xfrm: move xfrm_garbage_collect out of xfrm_policy_flush
In reply to Hangbin LiuNow we will force to do garbage collection if any policy removed in xfrm_policy_flush(). But during xfrm_net_exit(). We call flow_cache_fini() first and set set fc->percpu to NULL. Then after we call xfrm_policy_fini() -> frxm_policy_flush() -> flow_cache_flush(), we will get NULL pointer dereference when check percpu_empty. The code path looks like:
flow_cache_fini()
- fc->percpu = NULLxfrm_policy_fini()
- xfrm_policy_flush()
- xfrm_garbage_collect()
- flow_cache_flush()
- flow_cache_percpu_empty()
- fcp = per_cpu_ptr(fc->percpu, cpu)
To reproduce, just add ipsec in netns and then remove the netns.
v2:
As Xin Long suggested, since only two other places need to call it. move xfrm_garbage_collect() outside xfrm_policy_flush().
v3:
Fix subject mismatch after v2 fix.
--- net/key/af_key.c | 2 ++ net/xfrm/xfrm_policy.c | 4 ---- net/xfrm/xfrm_user.c | 1 + 3 files changed, 3 insertions(+), 4 deletions(-)
net/key/af_key.c +2 -0
@@ -2755,6 +2755,8 @@ static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadint err, err2; err =xfrm_policy_flush (net,XFRM_POLICY_TYPE_MAIN , true); +if (!err) +xfrm_garbage_collect (net); err2 =unicast_flush_resp (sk, hdr);if (err || err2) {if (err == -ESRCH )/* empty table - old silent behavior */
net/xfrm/xfrm_policy.c +0 -4
@@ -1006,10 +1006,6 @@ int xfrm_policy_flush(struct net *net, u8 type, bool task_valid) err = -ESRCH ; out:spin_unlock_bh (&net->xfrm.xfrm_policy_lock); - -if (cnt) -xfrm_garbage_collect (net); -return err; }EXPORT_SYMBOL (xfrm_policy_flush);
net/xfrm/xfrm_user.c +1 -0
@@ -2027,6 +2027,7 @@ static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,return 0 ;return err; } +xfrm_garbage_collect (net); c.data.type = type; c.event = nlh->nlmsg_type;
Signature · 1 line
2.5.5
Re: [PATCHv3 net] xfrm: move xfrm_garbage_collect out of xfrm_policy_flush
Quoted 73 lines · Hangbin Liu
Now we will force to do garbage collection if any policy removed in
xfrm_policy_flush(). But duringxfrm_net_exit(). We callflow_cache_fini()first and set setfc->percputo NULL. Then after we callxfrm_policy_fini()->frxm_policy_flush()->flow_cache_flush(), we will get NULL pointer dereference when checkpercpu_empty. The code path looks like:
flow_cache_fini()
-fc->percpu= NULLxfrm_policy_fini()
-xfrm_policy_flush()
-xfrm_garbage_collect()
-flow_cache_flush()
-flow_cache_percpu_empty()
- fcp =per_cpu_ptr(fc->percpu, cpu)To reproduce, just add ipsec in netns and then remove the netns.
v2:
As Xin Long suggested, since only two other places need to call it. movexfrm_garbage_collect()outsidexfrm_policy_flush().v3:
Fix subject mismatch after v2 fix.Fixes 35db06912189 ("xfrm: do the garbage collection after flushing policy")Signed-off-by Hangbin Liu <[email protected]>--- net/key/af_key.c | 2 ++ net/xfrm/xfrm_policy.c | 4 ---- net/xfrm/xfrm_user.c | 1 + 3 files changed, 3 insertions(+), 4 deletions(-)net/key/af_key.c +2
-0@@ -2755,6 +2755,8 @@ static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadint err, err2; err =xfrm_policy_flush (net,XFRM_POLICY_TYPE_MAIN , true); +if (!err) +xfrm_garbage_collect (net); err2 =unicast_flush_resp (sk, hdr);if (err || err2) {if (err == -ESRCH )/* empty table - old silent behavior */ net/xfrm/xfrm_policy.c +0
-4@@ -1006,10 +1006,6 @@ int xfrm_policy_flush(struct net *net, u8 type, bool task_valid) err = -ESRCH ; out:spin_unlock_bh (&net->xfrm.xfrm_policy_lock); - -if (cnt) -xfrm_garbage_collect (net); -return err; }EXPORT_SYMBOL (xfrm_policy_flush);net/xfrm/xfrm_user.c +1
-1@@ -2027,6 +2027,7 @@ static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,return 0 ;return err; } +xfrm_garbage_collect (net); c.data.type = type; c.event = nlh->nlmsg_type; --
Quoted 2 lines
2.5.5
though I could still see some typo err in changelog.
Steffen Klassert
maintainerRe: [PATCHv3 net] xfrm: move xfrm_garbage_collect out of xfrm_policy_flush
In reply to Hangbin LiuQuoted 21 lines · Hangbin Liu
Now we will force to do garbage collection if any policy removed in
xfrm_policy_flush(). But duringxfrm_net_exit(). We callflow_cache_fini()first and set setfc->percputo NULL. Then after we callxfrm_policy_fini()->frxm_policy_flush()->flow_cache_flush(), we will get NULL pointer dereference when checkpercpu_empty. The code path looks like:
flow_cache_fini()
-fc->percpu= NULLxfrm_policy_fini()
-xfrm_policy_flush()
-xfrm_garbage_collect()
-flow_cache_flush()
-flow_cache_percpu_empty()
- fcp =per_cpu_ptr(fc->percpu, cpu)To reproduce, just add ipsec in netns and then remove the netns.
v2:
As Xin Long suggested, since only two other places need to call it. movexfrm_garbage_collect()outsidexfrm_policy_flush().
Quoted 5 lines
v3:
Fix subject mismatch after v2 fix.Fixes 35db06912189 ("xfrm: do the garbage collection after flushing policy")Signed-off-by Hangbin Liu <[email protected]>
Patch applied, thanks eveyone!