Skip to content

Commit f886497

Browse files
edumazetdavem330
authored andcommitted
ipv4: fix dst race in sk_dst_get()
When IP route cache had been removed in linux-3.6, we broke assumption that dst entries were all freed after rcu grace period. DST_NOCACHE dst were supposed to be freed from dst_release(). But it appears we want to keep such dst around, either in UDP sockets or tunnels. In sk_dst_get() we need to make sure dst refcount is not 0 before incrementing it, or else we might end up freeing a dst twice. DST_NOCACHE set on a dst does not mean this dst can not be attached to a socket or a tunnel. Then, before actual freeing, we need to observe a rcu grace period to make sure all other cpus can catch the fact the dst is no longer usable. Signed-off-by: Eric Dumazet <edumazet@google.com> Reported-by: Dormando <dormando@rydia.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 99e72a0 commit f886497

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

include/net/sock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,8 +1730,8 @@ sk_dst_get(struct sock *sk)
17301730

17311731
rcu_read_lock();
17321732
dst = rcu_dereference(sk->sk_dst_cache);
1733-
if (dst)
1734-
dst_hold(dst);
1733+
if (dst && !atomic_inc_not_zero(&dst->__refcnt))
1734+
dst = NULL;
17351735
rcu_read_unlock();
17361736
return dst;
17371737
}

net/core/dst.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,18 +269,24 @@ struct dst_entry *dst_destroy(struct dst_entry * dst)
269269
}
270270
EXPORT_SYMBOL(dst_destroy);
271271

272+
static void dst_destroy_rcu(struct rcu_head *head)
273+
{
274+
struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
275+
276+
dst = dst_destroy(dst);
277+
if (dst)
278+
__dst_free(dst);
279+
}
280+
272281
void dst_release(struct dst_entry *dst)
273282
{
274283
if (dst) {
275284
int newrefcnt;
276285

277286
newrefcnt = atomic_dec_return(&dst->__refcnt);
278287
WARN_ON(newrefcnt < 0);
279-
if (unlikely(dst->flags & DST_NOCACHE) && !newrefcnt) {
280-
dst = dst_destroy(dst);
281-
if (dst)
282-
__dst_free(dst);
283-
}
288+
if (unlikely(dst->flags & DST_NOCACHE) && !newrefcnt)
289+
call_rcu(&dst->rcu_head, dst_destroy_rcu);
284290
}
285291
}
286292
EXPORT_SYMBOL(dst_release);

net/ipv4/ip_tunnel.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ static void __tunnel_dst_set(struct ip_tunnel_dst *idst,
7373
{
7474
struct dst_entry *old_dst;
7575

76-
if (dst) {
77-
if (dst->flags & DST_NOCACHE)
78-
dst = NULL;
79-
else
80-
dst_clone(dst);
81-
}
76+
dst_clone(dst);
8277
old_dst = xchg((__force struct dst_entry **)&idst->dst, dst);
8378
dst_release(old_dst);
8479
}
@@ -108,13 +103,14 @@ static struct rtable *tunnel_rtable_get(struct ip_tunnel *t, u32 cookie)
108103

109104
rcu_read_lock();
110105
dst = rcu_dereference(this_cpu_ptr(t->dst_cache)->dst);
106+
if (dst && !atomic_inc_not_zero(&dst->__refcnt))
107+
dst = NULL;
111108
if (dst) {
112109
if (dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
113-
rcu_read_unlock();
114110
tunnel_dst_reset(t);
115-
return NULL;
111+
dst_release(dst);
112+
dst = NULL;
116113
}
117-
dst_hold(dst);
118114
}
119115
rcu_read_unlock();
120116
return (struct rtable *)dst;

0 commit comments

Comments
 (0)