Skip to contentSkip to conversation

xfrm: fix reinsert in xfrm_hash_rebuild()

PATCH In review Cong Wang 2 participants 3 messages Jan 4, 2019 – Jan 5, 2019

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 xfrm: fix reinsert in xfrm_hash_rebuild()

xfrm_hash_rebuild() re-inserts existing policies into the hashtables, so it should not insert a same policy in the same place twice. This means we have to pass excl==1 to xfrm_policy_inexact_insert() and ignore the -EEXIST error. Otherwise we end up having an entry in the hashtable points to itself, which leads to a use-after-free as reported by syzbot.

Inside xfrm_policy_inexact_insert(), xfrm_policy_insert_list() could only return either a NULL pointer, a valid non-NULL pointer, or an error pointer (-EEXIST) when excl==1. Testing delpol && excl for -EEXIST is incorrect as it could return a valid pointer for excl case too, testing IS_ERR(delpol) is correct.

Reported-and-tested-by [email protected]
Fixes 24969facd704 ("xfrm: policy: store inexact policies in an rhashtable")
Cc Florian Westphal <[email protected]>
---
 net/xfrm/xfrm_policy.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
net/xfrm/xfrm_policy.c +5 -4
index 934492bad8e0..c77c44b975ca 100644
@@ -1193,9 +1193,9 @@ xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
 	}
 
 	delpol = xfrm_policy_insert_list(chain, policy, excl);
-	if (delpol && excl) {
+	if (IS_ERR(delpol)) {
 		__xfrm_policy_inexact_prune_bin(bin, false);
-		return ERR_PTR(-EEXIST);
+		return delpol;
 	}
 
 	chain = &net->xfrm.policy_inexact[dir];
@@ -1314,9 +1314,10 @@ static void xfrm_hash_rebuild(struct work_struct *work)
 		chain = policy_hash_bysel(net, &policy->selector,
 					  policy->family, dir);
 		if (!chain) {
-			void *p = xfrm_policy_inexact_insert(policy, dir, 0);
+			void *p = xfrm_policy_inexact_insert(policy, dir, 1);
 
-			WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p));
+			if (IS_ERR(p) && PTR_ERR(p) != -EEXIST)
+				WARN_ONCE(1, "reinsert fails: %ld\n", PTR_ERR(p));
 			continue;
 		}
 
Signature · 1 line
2.19.2
#1 lore raw
Quoted 5 lines · Cong Wang

xfrm_hash_rebuild() re-inserts existing policies into the hashtables, so it should not insert a same policy in the same place twice. This means we have to pass excl==1 to xfrm_policy_inexact_insert() and ignore the -EEXIST error. Otherwise we end up having an entry in the hashtable points to itself, which leads to a use-after-free as reported by syzbot.

Yes, double-insert is the reason for this syzbot report.

Quoted 3 lines

Inside xfrm_policy_inexact_insert(), xfrm_policy_insert_list() could only return either a NULL pointer, a valid non-NULL pointer, or an error pointer (-EEXIST) when excl==1.

Right.

Quoted 3 lines

Testing delpol && excl for -EEXIST
is incorrect as it could return a valid pointer for excl case too, testing IS_ERR(delpol) is correct.

Agree.

net/xfrm/xfrm_policy.c static void xfrm_hash_rebuild(struct work_struct *work) · line 1317

Quoted 3 lines
net/xfrm/xfrm_policy.c +1 -1
 		if (!chain) {
-			void *p = xfrm_policy_inexact_insert(policy, dir, 0);
+			void *p = xfrm_policy_inexact_insert(policy, dir, 1);

I am not so sure about this change.

Excl == 1 doesn't check for "this entry", it checks for any policy that has identical properties, so I do not think its correct to pass 1 here; only userspace can when adding entry.

Steffen, could you please hold off on this patch for a few hours?

I will finish test script updates today and will pass a series that includes a different fix for this bug.

I will CC Cong on the patches to make sure I did not miss anything.

(Other aspect of this patch appears correct to me though).

author
#2 lore raw
Quoted 26 lines · Florian Westphal
Quoted 5 lines · Cong Wang

xfrm_hash_rebuild() re-inserts existing policies into the hashtables, so it should not insert a same policy in the same place twice. This means we have to pass excl==1 to xfrm_policy_inexact_insert() and ignore the -EEXIST error. Otherwise we end up having an entry in the hashtable points to itself, which leads to a use-after-free as reported by syzbot.

Yes, double-insert is the reason for this syzbot report.

Quoted 3 lines

Inside xfrm_policy_inexact_insert(), xfrm_policy_insert_list() could only return either a NULL pointer, a valid non-NULL pointer, or an error pointer (-EEXIST) when excl==1.

Right.

Quoted 3 lines

Testing delpol && excl for -EEXIST
is incorrect as it could return a valid pointer for excl case too, testing IS_ERR(delpol) is correct.

Agree.

net/xfrm/xfrm_policy.c static void xfrm_hash_rebuild(struct work_struct *work) · line 1317

Quoted 3 lines
              if (!chain) {
net/xfrm/xfrm_policy.c +1 -1
-                     void *p = xfrm_policy_inexact_insert(policy, dir, 0);
+                     void *p = xfrm_policy_inexact_insert(policy, dir, 1);
Quoted 5 lines

I am not so sure about this change.

Excl == 1 doesn't check for "this entry", it checks for any policy that has identical properties, so I do not think its correct to pass 1 here; only userspace can when adding entry.

Yeah, I realized the hashtable could contain two identical polices after sending out this patch.

Quoted 9 lines

Steffen, could you please hold off on this patch for a few hours?

I will finish test script updates today and will pass a series that includes a different fix for this bug.

I will CC Cong on the patches to make sure I did not miss anything.

(Other aspect of this patch appears correct to me though).

I will take a look at your fix.

Thanks.