Commit a779d91
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2004 | 2004 | | |
2005 | 2005 | | |
2006 | 2006 | | |
| 2007 | + | |
2007 | 2008 | | |
2008 | 2009 | | |
2009 | 2010 | | |
| |||
2022 | 2023 | | |
2023 | 2024 | | |
2024 | 2025 | | |
2025 | | - | |
| 2026 | + | |
2026 | 2027 | | |
2027 | 2028 | | |
2028 | 2029 | | |
2029 | 2030 | | |
2030 | 2031 | | |
2031 | 2032 | | |
2032 | | - | |
| 2033 | + | |
2033 | 2034 | | |
2034 | 2035 | | |
2035 | 2036 | | |
2036 | 2037 | | |
2037 | 2038 | | |
2038 | | - | |
| 2039 | + | |
2039 | 2040 | | |
| 2041 | + | |
2040 | 2042 | | |
2041 | 2043 | | |
2042 | 2044 | | |
| |||
0 commit comments