Skip to content

Commit ac888d5

Browse files
edumazetPaolo Abeni
authored andcommitted
net: do not delay dst_entries_add() in dst_release()
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]>
1 parent a354733 commit ac888d5

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 @@ static void 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);
@@ -159,17 +156,27 @@ void dst_dev_put(struct dst_entry *dst)
159156
}
160157
EXPORT_SYMBOL(dst_dev_put);
161158

159+
static void dst_count_dec(struct dst_entry *dst)
160+
{
161+
if (!(dst->flags & DST_NOCOUNT))
162+
dst_entries_add(dst->ops, -1);
163+
}
164+
162165
void dst_release(struct dst_entry *dst)
163166
{
164-
if (dst && rcuref_put(&dst->__rcuref))
167+
if (dst && rcuref_put(&dst->__rcuref)) {
168+
dst_count_dec(dst);
165169
call_rcu_hurry(&dst->rcu_head, dst_destroy_rcu);
170+
}
166171
}
167172
EXPORT_SYMBOL(dst_release);
168173

169174
void dst_release_immediate(struct dst_entry *dst)
170175
{
171-
if (dst && rcuref_put(&dst->__rcuref))
176+
if (dst && rcuref_put(&dst->__rcuref)) {
177+
dst_count_dec(dst);
172178
dst_destroy(dst);
179+
}
173180
}
174181
EXPORT_SYMBOL(dst_release_immediate);
175182

0 commit comments

Comments
 (0)