Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit eae7435

Browse files
edumazetgregkh
authored andcommitted
net: do not delay dst_entries_add() in dst_release()
[ Upstream commit ac888d5 ] dst_entries_add() uses per-cpu data that might be freed at netns dismantle from ip6_route_net_exit() calling dst_entries_destroy() Before ip6_route_net_exit() can be called, we release all the dsts associated with this netns, via calls to dst_release(), which waits an rcu grace period before calling dst_destroy() dst_entries_add() use in dst_destroy() is racy, because dst_entries_destroy() could have been called already. Decrementing the number of dsts must happen sooner. Notes: 1) in CONFIG_XFRM case, dst_destroy() can call dst_release_immediate(child), this might also cause UAF if the child does not have DST_NOCOUNT set. IPSEC maintainers might take a look and see how to address this. 2) There is also discussion about removing this count of dst, which might happen in future kernels. Fixes: f886497 ("ipv4: fix dst race in sk_dst_get()") Closes: https://lore.kernel.org/lkml/CANn89iLCCGsP7SFn9HKpvnKu96Td4KD08xf7aGtiYgZnkjaL=w@mail.gmail.com/T/ Reported-by: Naresh Kamboju <[email protected]> Tested-by: Linux Kernel Functional Testing <[email protected]> Tested-by: Naresh Kamboju <[email protected]> Signed-off-by: Eric Dumazet <[email protected]> Cc: Xin Long <[email protected]> Cc: Steffen Klassert <[email protected]> Reviewed-by: Xin Long <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 64121e3 commit eae7435

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

net/core/dst.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ struct dst_entry *dst_destroy(struct dst_entry * dst)
109109
child = xdst->child;
110110
}
111111
#endif
112-
if (!(dst->flags & DST_NOCOUNT))
113-
dst_entries_add(dst->ops, -1);
114-
115112
if (dst->ops->destroy)
116113
dst->ops->destroy(dst);
117114
netdev_put(dst->dev, &dst->dev_tracker);
@@ -161,17 +158,27 @@ void dst_dev_put(struct dst_entry *dst)
161158
}
162159
EXPORT_SYMBOL(dst_dev_put);
163160

161+
static void dst_count_dec(struct dst_entry *dst)
162+
{
163+
if (!(dst->flags & DST_NOCOUNT))
164+
dst_entries_add(dst->ops, -1);
165+
}
166+
164167
void dst_release(struct dst_entry *dst)
165168
{
166-
if (dst && rcuref_put(&dst->__rcuref))
169+
if (dst && rcuref_put(&dst->__rcuref)) {
170+
dst_count_dec(dst);
167171
call_rcu_hurry(&dst->rcu_head, dst_destroy_rcu);
172+
}
168173
}
169174
EXPORT_SYMBOL(dst_release);
170175

171176
void dst_release_immediate(struct dst_entry *dst)
172177
{
173-
if (dst && rcuref_put(&dst->__rcuref))
178+
if (dst && rcuref_put(&dst->__rcuref)) {
179+
dst_count_dec(dst);
174180
dst_destroy(dst);
181+
}
175182
}
176183
EXPORT_SYMBOL(dst_release_immediate);
177184

0 commit comments

Comments
 (0)