Skip to content

Commit 079d4f3

Browse files
edumazetTreehugger Robot
authored andcommitted
BACKPORT: net: fix __dst_negative_advice() race
__dst_negative_advice() does not enforce proper RCU rules when sk->dst_cache must be cleared, leading to possible UAF. RCU rules are that we must first clear sk->sk_dst_cache, then call dst_release(old_dst). Note that sk_dst_reset(sk) is implementing this protocol correctly, while __dst_negative_advice() uses the wrong order. Given that ip6_negative_advice() has special logic against RTF_CACHE, this means each of the three ->negative_advice() existing methods must perform the sk_dst_reset() themselves. Note the check against NULL dst is centralized in __dst_negative_advice(), there is no need to duplicate it in various callbacks. Many thanks to Clement Lecigne for tracking this issue. This old bug became visible after the blamed commit, using UDP sockets. Bug: 343727534 Fixes: a87cb3e ("net: Facility to report route quality of connected sockets") Reported-by: Clement Lecigne <[email protected]> Diagnosed-by: Clement Lecigne <[email protected]> Signed-off-by: Eric Dumazet <[email protected]> Cc: Tom Herbert <[email protected]> Reviewed-by: David Ahern <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> (cherry picked from commit 92f1655aa2b2294d0b49925f3b875a634bd3b59e) [Lee: Trivial/unrelated conflict - no change to the patch] Signed-off-by: Lee Jones <[email protected]> Change-Id: I293734dca1b81fcb712e1de294f51e96a405f7e4
1 parent 8ccbe20 commit 079d4f3

File tree

5 files changed

+30
-47
lines changed

5 files changed

+30
-47
lines changed

include/net/dst_ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct dst_ops {
2424
void (*destroy)(struct dst_entry *);
2525
void (*ifdown)(struct dst_entry *,
2626
struct net_device *dev, int how);
27-
struct dst_entry * (*negative_advice)(struct dst_entry *);
27+
void (*negative_advice)(struct sock *sk, struct dst_entry *);
2828
void (*link_failure)(struct sk_buff *);
2929
void (*update_pmtu)(struct dst_entry *dst, struct sock *sk,
3030
struct sk_buff *skb, u32 mtu,

include/net/sock.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,19 +1919,12 @@ sk_dst_get(struct sock *sk)
19191919

19201920
static inline void dst_negative_advice(struct sock *sk)
19211921
{
1922-
struct dst_entry *ndst, *dst = __sk_dst_get(sk);
1922+
struct dst_entry *dst = __sk_dst_get(sk);
19231923

19241924
sk_rethink_txhash(sk);
19251925

1926-
if (dst && dst->ops->negative_advice) {
1927-
ndst = dst->ops->negative_advice(dst);
1928-
1929-
if (ndst != dst) {
1930-
rcu_assign_pointer(sk->sk_dst_cache, ndst);
1931-
sk_tx_queue_clear(sk);
1932-
WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
1933-
}
1934-
}
1926+
if (dst && dst->ops->negative_advice)
1927+
dst->ops->negative_advice(sk, dst);
19351928
}
19361929

19371930
static inline void

net/ipv4/route.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ static int ip_rt_gc_timeout __read_mostly = RT_GC_TIMEOUT;
140140
static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
141141
static unsigned int ipv4_default_advmss(const struct dst_entry *dst);
142142
static unsigned int ipv4_mtu(const struct dst_entry *dst);
143-
static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
143+
static void ipv4_negative_advice(struct sock *sk,
144+
struct dst_entry *dst);
144145
static void ipv4_link_failure(struct sk_buff *skb);
145146
static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
146147
struct sk_buff *skb, u32 mtu,
@@ -848,22 +849,15 @@ static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buf
848849
__ip_do_redirect(rt, skb, &fl4, true);
849850
}
850851

851-
static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
852+
static void ipv4_negative_advice(struct sock *sk,
853+
struct dst_entry *dst)
852854
{
853855
struct rtable *rt = (struct rtable *)dst;
854-
struct dst_entry *ret = dst;
855856

856-
if (rt) {
857-
if (dst->obsolete > 0) {
858-
ip_rt_put(rt);
859-
ret = NULL;
860-
} else if ((rt->rt_flags & RTCF_REDIRECTED) ||
861-
rt->dst.expires) {
862-
ip_rt_put(rt);
863-
ret = NULL;
864-
}
865-
}
866-
return ret;
857+
if ((dst->obsolete > 0) ||
858+
(rt->rt_flags & RTCF_REDIRECTED) ||
859+
rt->dst.expires)
860+
sk_dst_reset(sk);
867861
}
868862

869863
/*

net/ipv6/route.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ enum rt6_nud_state {
8888
static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
8989
static unsigned int ip6_default_advmss(const struct dst_entry *dst);
9090
static unsigned int ip6_mtu(const struct dst_entry *dst);
91-
static struct dst_entry *ip6_negative_advice(struct dst_entry *);
91+
static void ip6_negative_advice(struct sock *sk,
92+
struct dst_entry *dst);
9293
static void ip6_dst_destroy(struct dst_entry *);
9394
static void ip6_dst_ifdown(struct dst_entry *,
9495
struct net_device *dev, int how);
@@ -2281,24 +2282,24 @@ static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
22812282
return dst_ret;
22822283
}
22832284

2284-
static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2285+
static void ip6_negative_advice(struct sock *sk,
2286+
struct dst_entry *dst)
22852287
{
22862288
struct rt6_info *rt = (struct rt6_info *) dst;
22872289

2288-
if (rt) {
2289-
if (rt->rt6i_flags & RTF_CACHE) {
2290-
rcu_read_lock();
2291-
if (rt6_check_expired(rt)) {
2292-
rt6_remove_exception_rt(rt);
2293-
dst = NULL;
2294-
}
2295-
rcu_read_unlock();
2296-
} else {
2297-
dst_release(dst);
2298-
dst = NULL;
2290+
if (rt->rt6i_flags & RTF_CACHE) {
2291+
rcu_read_lock();
2292+
if (rt6_check_expired(rt)) {
2293+
/* counteract the dst_release() in sk_dst_reset() */
2294+
dst_hold(dst);
2295+
sk_dst_reset(sk);
2296+
2297+
rt6_remove_exception_rt(rt);
22992298
}
2299+
rcu_read_unlock();
2300+
return;
23002301
}
2301-
return dst;
2302+
sk_dst_reset(sk);
23022303
}
23032304

23042305
static void ip6_link_failure(struct sk_buff *skb)

net/xfrm/xfrm_policy.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,15 +2558,10 @@ static void xfrm_link_failure(struct sk_buff *skb)
25582558
/* Impossible. Such dst must be popped before reaches point of failure. */
25592559
}
25602560

2561-
static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2561+
static void xfrm_negative_advice(struct sock *sk, struct dst_entry *dst)
25622562
{
2563-
if (dst) {
2564-
if (dst->obsolete) {
2565-
dst_release(dst);
2566-
dst = NULL;
2567-
}
2568-
}
2569-
return dst;
2563+
if (dst->obsolete)
2564+
sk_dst_reset(sk);
25702565
}
25712566

25722567
static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)

0 commit comments

Comments
 (0)