Skip to content

Commit a779d91

Browse files
zhuoliang zhangklassert
authored andcommitted
net: xfrm: fix a race condition during allocing spi
we found that the following race condition exists in xfrm_alloc_userspi flow: user thread state_hash_work thread ---- ---- xfrm_alloc_userspi() __find_acq_core() /*alloc new xfrm_state:x*/ xfrm_state_alloc() /*schedule state_hash_work thread*/ xfrm_hash_grow_check() xfrm_hash_resize() xfrm_alloc_spi /*hold lock*/ x->id.spi = htonl(spi) spin_lock_bh(&net->xfrm.xfrm_state_lock) /*waiting lock release*/ xfrm_hash_transfer() spin_lock_bh(&net->xfrm.xfrm_state_lock) /*add x into hlist:net->xfrm.state_byspi*/ hlist_add_head_rcu(&x->byspi) spin_unlock_bh(&net->xfrm.xfrm_state_lock) /*add x into hlist:net->xfrm.state_byspi 2 times*/ hlist_add_head_rcu(&x->byspi) 1. a new state x is alloced in xfrm_state_alloc() and added into the bydst hlist in __find_acq_core() on the LHS; 2. on the RHS, state_hash_work thread travels the old bydst and tranfers every xfrm_state (include x) into the new bydst hlist and new byspi hlist; 3. user thread on the LHS gets the lock and adds x into the new byspi hlist again. So the same xfrm_state (x) is added into the same list_hash (net->xfrm.state_byspi) 2 times that makes the list_hash become an inifite loop. To fix the race, x->id.spi = htonl(spi) in the xfrm_alloc_spi() is moved to the back of spin_lock_bh, sothat state_hash_work thread no longer add x which id.spi is zero into the hash_list. Fixes: f034b5d ("[XFRM]: Dynamic xfrm_state hash table sizing.") Signed-off-by: zhuoliang zhang <zhuoliang.zhang@mediatek.com> Acked-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
1 parent 7fe9461 commit a779d91

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

net/xfrm/xfrm_state.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,7 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
20042004
int err = -ENOENT;
20052005
__be32 minspi = htonl(low);
20062006
__be32 maxspi = htonl(high);
2007+
__be32 newspi = 0;
20072008
u32 mark = x->mark.v & x->mark.m;
20082009

20092010
spin_lock_bh(&x->lock);
@@ -2022,21 +2023,22 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
20222023
xfrm_state_put(x0);
20232024
goto unlock;
20242025
}
2025-
x->id.spi = minspi;
2026+
newspi = minspi;
20262027
} else {
20272028
u32 spi = 0;
20282029
for (h = 0; h < high-low+1; h++) {
20292030
spi = low + prandom_u32()%(high-low+1);
20302031
x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
20312032
if (x0 == NULL) {
2032-
x->id.spi = htonl(spi);
2033+
newspi = htonl(spi);
20332034
break;
20342035
}
20352036
xfrm_state_put(x0);
20362037
}
20372038
}
2038-
if (x->id.spi) {
2039+
if (newspi) {
20392040
spin_lock_bh(&net->xfrm.xfrm_state_lock);
2041+
x->id.spi = newspi;
20402042
h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
20412043
hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
20422044
spin_unlock_bh(&net->xfrm.xfrm_state_lock);

0 commit comments

Comments
 (0)