Skip to content

Commit 0224432

Browse files
a-darwishgregkh
authored andcommitted
net: xfrm: Localize sequence counter per network namespace
[ Upstream commit e88add1 ] A sequence counter write section must be serialized or its internal state can get corrupted. The "xfrm_state_hash_generation" seqcount is global, but its write serialization lock (net->xfrm.xfrm_state_lock) is instantiated per network namespace. The write protection is thus insufficient. To provide full protection, localize the sequence counter per network namespace instead. This should be safe as both the seqcount read and write sections access data exclusively within the network namespace. It also lays the foundation for transforming "xfrm_state_hash_generation" data type from seqcount_t to seqcount_LOCKNAME_t in further commits. Fixes: b65e3d7 ("xfrm: state: add sequence count to detect hash resizes") Signed-off-by: Ahmed S. Darwish <a.darwish@linutronix.de> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 1e6a3b4 commit 0224432

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

‎include/net/netns/xfrm.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ struct netns_xfrm {
7272
#if IS_ENABLED(CONFIG_IPV6)
7373
struct dst_ops xfrm6_dst_ops;
7474
#endif
75-
spinlock_t xfrm_state_lock;
75+
spinlock_t xfrm_state_lock;
76+
seqcount_t xfrm_state_hash_generation;
77+
7678
spinlock_t xfrm_policy_lock;
7779
struct mutex xfrm_cfg_mutex;
7880
};

‎net/xfrm/xfrm_state.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ static void xfrm_state_gc_task(struct work_struct *work);
4444
*/
4545

4646
static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
47-
static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation);
4847
static struct kmem_cache *xfrm_state_cache __ro_after_init;
4948

5049
static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
@@ -140,7 +139,7 @@ static void xfrm_hash_resize(struct work_struct *work)
140139
}
141140

142141
spin_lock_bh(&net->xfrm.xfrm_state_lock);
143-
write_seqcount_begin(&xfrm_state_hash_generation);
142+
write_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
144143

145144
nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
146145
odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
@@ -156,7 +155,7 @@ static void xfrm_hash_resize(struct work_struct *work)
156155
rcu_assign_pointer(net->xfrm.state_byspi, nspi);
157156
net->xfrm.state_hmask = nhashmask;
158157

159-
write_seqcount_end(&xfrm_state_hash_generation);
158+
write_seqcount_end(&net->xfrm.xfrm_state_hash_generation);
160159
spin_unlock_bh(&net->xfrm.xfrm_state_lock);
161160

162161
osize = (ohashmask + 1) * sizeof(struct hlist_head);
@@ -1061,7 +1060,7 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
10611060

10621061
to_put = NULL;
10631062

1064-
sequence = read_seqcount_begin(&xfrm_state_hash_generation);
1063+
sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
10651064

10661065
rcu_read_lock();
10671066
h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
@@ -1174,7 +1173,7 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
11741173
if (to_put)
11751174
xfrm_state_put(to_put);
11761175

1177-
if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1176+
if (read_seqcount_retry(&net->xfrm.xfrm_state_hash_generation, sequence)) {
11781177
*err = -EAGAIN;
11791178
if (x) {
11801179
xfrm_state_put(x);
@@ -2664,6 +2663,7 @@ int __net_init xfrm_state_init(struct net *net)
26642663
net->xfrm.state_num = 0;
26652664
INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
26662665
spin_lock_init(&net->xfrm.xfrm_state_lock);
2666+
seqcount_init(&net->xfrm.xfrm_state_hash_generation);
26672667
return 0;
26682668

26692669
out_byspi:

0 commit comments

Comments
 (0)