Skip to contentSkip to conversation

net/flow: fix fc->percpu NULL pointer dereference

PATCH Superseded Hangbin Liu 3 participants 14 messages Jun 9, 2017 – Jun 12, 2017

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

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 = NULL
xfrm_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")
---
 net/core/flow.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/core/flow.c +7 -4
index f7f5d19..321fc53 100644
@@ -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
#1 lore raw

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 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 = NULL
xfrm_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")
---
 net/core/flow.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/core/flow.c +7 -5
index f7f5d19..321fc53 100644
@@ -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

maintainer
#2 lore raw
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 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().

Can't we just call xfrm_policy_fini() first and then flow_cache_fini()?

#3 lore raw
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 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().

Can't we just call xfrm_policy_fini() first and then flow_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.

#4 lore raw

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 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().

Can't we just call xfrm_policy_fini() first and then flow_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_fini does is only to free fcp->hash_table and stop timer, I didn't see it has any dependence on xfrm_policy stuff.

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

#5 lore raw
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 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().

Can't we just call xfrm_policy_fini() first and then flow_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_fini does is only to free fcp->hash_table and stop timer, I didn't see it has any dependence on xfrm_policy stuff.

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
index ed4e52d..89343a3 100644
@@ -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
index 38614df..86116e9 100644
@@ -2027,6 +2027,7 @@ static int xfrm_flush_policy(struct sk_buff

*skb, struct nlmsghdr *nlh,

                        return 0;
                return err;
        }

+ xfrm_garbage_collect(net);

#6 lore raw
Quoted 11 lines · Xin Long

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);

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

#7 lore raw

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_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);

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

#8 lore raw

PATCH v2 1/1 net/flow: fix fc->percpu NULL pointer dereference

In reply to Hangbin Liu

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 = NULL
xfrm_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().

Fixes 35db06912189 ("xfrm: do the garbage collection after flushing policy")
---
 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
index 512dc43..5103f92 100644
@@ -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 -4
index ed4e52d..643a18f 100644
@@ -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
index 38614df..86116e9 100644
@@ -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
#9 lore raw
Quoted 70 lines · Hangbin Liu

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 = NULL
xfrm_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().

Fixes 35db06912189 ("xfrm: do the garbage collection after flushing policy")
---
 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
index 512dc43..5103f92 100644
@@ -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 -4
index ed4e52d..643a18f 100644
@@ -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
index 38614df..86116e9 100644
@@ -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 ...

#10 lore raw
Quoted 4 lines · Xin Long

It's a xfrm fix, pls also fix the title, like:
xfrm: move xfrm_garbage_collect out of xfrm_policy_flush
or
xfrm: fix ...

Opps, sorry forgot that.

#11 lore raw

PATCH v3 1/1 xfrm: move xfrm_garbage_collect out of xfrm_policy_flush

In reply to Hangbin Liu

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 = NULL
xfrm_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.

Fixes 35db06912189 ("xfrm: do the garbage collection after flushing policy")
---
 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
index 512dc43..5103f92 100644
@@ -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 -4
index ed4e52d..643a18f 100644
@@ -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
index 38614df..86116e9 100644
@@ -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
Reviewed-by on v3 1/1
#12 lore raw

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 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 = NULL
xfrm_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.

Fixes 35db06912189 ("xfrm: do the garbage collection after flushing policy")
---
 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
index 512dc43..5103f92 100644
@@ -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 -4
index ed4e52d..643a18f 100644
@@ -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
index 38614df..86116e9 100644
@@ -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.

maintainer
#13 lore raw

Re: [PATCHv3 net] xfrm: move xfrm_garbage_collect out of xfrm_policy_flush

In reply to Hangbin Liu
Quoted 21 lines · Hangbin Liu

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 = NULL
xfrm_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().

Quoted 5 lines

v3:
Fix subject mismatch after v2 fix.

Fixes 35db06912189 ("xfrm: do the garbage collection after flushing policy")

Patch applied, thanks eveyone!