Skip to content

Commit 6a8fa30

Browse files
committed
net: fix __dst_negative_advice() race
jira VULN-5441 cve CVE-2024-36971 commit-author Eric Dumazet <[email protected]> commit 92f1655 upstream-diff This change breaks the kabi. Use the RH_KABI_REPLACE macro to define the negative_advice function such that check-kabi will still pass. From rh_kabi.h: "The RH_KABI_REPLACE* macros attempt to add the ability to use the '_new' element while preserving size alignment and kabi agreement with the '_orig' element." __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. 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 92f1655) Signed-off-by: Brett Mastbergen <[email protected]>
1 parent fa2ce2c commit 6a8fa30

File tree

5 files changed

+31
-47
lines changed

5 files changed

+31
-47
lines changed

include/net/dst_ops.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ struct dst_ops {
2626
void (*destroy)(struct dst_entry *);
2727
void (*ifdown)(struct dst_entry *,
2828
struct net_device *dev, int how);
29-
struct dst_entry * (*negative_advice)(struct dst_entry *);
29+
RH_KABI_REPLACE(struct dst_entry * (*negative_advice)(struct dst_entry *),
30+
void (*negative_advice)(struct sock *sk, struct dst_entry *))
3031
void (*link_failure)(struct sk_buff *);
3132
RH_KABI_REPLACE(void (*update_pmtu)(struct dst_entry *dst, struct sock *sk,
3233
struct sk_buff *skb, u32 mtu),

include/net/sock.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,17 +2008,10 @@ sk_dst_get(struct sock *sk)
20082008

20092009
static inline void __dst_negative_advice(struct sock *sk)
20102010
{
2011-
struct dst_entry *ndst, *dst = __sk_dst_get(sk);
2011+
struct dst_entry *dst = __sk_dst_get(sk);
20122012

2013-
if (dst && dst->ops->negative_advice) {
2014-
ndst = dst->ops->negative_advice(dst);
2015-
2016-
if (ndst != dst) {
2017-
rcu_assign_pointer(sk->sk_dst_cache, ndst);
2018-
sk_tx_queue_clear(sk);
2019-
sk->sk_dst_pending_confirm = 0;
2020-
}
2021-
}
2013+
if (dst && dst->ops->negative_advice)
2014+
dst->ops->negative_advice(sk, dst);
20222015
}
20232016

20242017
static inline void dst_negative_advice(struct sock *sk)

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,
@@ -857,22 +858,15 @@ static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buf
857858
__ip_do_redirect(rt, skb, &fl4, true);
858859
}
859860

860-
static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
861+
static void ipv4_negative_advice(struct sock *sk,
862+
struct dst_entry *dst)
861863
{
862864
struct rtable *rt = (struct rtable *)dst;
863-
struct dst_entry *ret = dst;
864865

865-
if (rt) {
866-
if (dst->obsolete > 0) {
867-
ip_rt_put(rt);
868-
ret = NULL;
869-
} else if ((rt->rt_flags & RTCF_REDIRECTED) ||
870-
rt->dst.expires) {
871-
ip_rt_put(rt);
872-
ret = NULL;
873-
}
874-
}
875-
return ret;
866+
if ((dst->obsolete > 0) ||
867+
(rt->rt_flags & RTCF_REDIRECTED) ||
868+
rt->dst.expires)
869+
sk_dst_reset(sk);
876870
}
877871

878872
/*

net/ipv6/route.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ enum rt6_nud_state {
8989
static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
9090
static unsigned int ip6_default_advmss(const struct dst_entry *dst);
9191
static unsigned int ip6_mtu(const struct dst_entry *dst);
92-
static struct dst_entry *ip6_negative_advice(struct dst_entry *);
92+
static void ip6_negative_advice(struct sock *sk,
93+
struct dst_entry *dst);
9394
static void ip6_dst_destroy(struct dst_entry *);
9495
static void ip6_dst_ifdown(struct dst_entry *,
9596
struct net_device *dev, int how);
@@ -2399,24 +2400,24 @@ static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
23992400
return dst_ret;
24002401
}
24012402

2402-
static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2403+
static void ip6_negative_advice(struct sock *sk,
2404+
struct dst_entry *dst)
24032405
{
24042406
struct rt6_info *rt = (struct rt6_info *) dst;
24052407

2406-
if (rt) {
2407-
if (rt->rt6i_flags & RTF_CACHE) {
2408-
rcu_read_lock();
2409-
if (rt6_check_expired(rt)) {
2410-
rt6_remove_exception_rt(rt);
2411-
dst = NULL;
2412-
}
2413-
rcu_read_unlock();
2414-
} else {
2415-
dst_release(dst);
2416-
dst = NULL;
2408+
if (rt->rt6i_flags & RTF_CACHE) {
2409+
rcu_read_lock();
2410+
if (rt6_check_expired(rt)) {
2411+
/* counteract the dst_release() in sk_dst_reset() */
2412+
dst_hold(dst);
2413+
sk_dst_reset(sk);
2414+
2415+
rt6_remove_exception_rt(rt);
24172416
}
2417+
rcu_read_unlock();
2418+
return;
24182419
}
2419-
return dst;
2420+
sk_dst_reset(sk);
24202421
}
24212422

24222423
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
@@ -3561,15 +3561,10 @@ static void xfrm_link_failure(struct sk_buff *skb)
35613561
/* Impossible. Such dst must be popped before reaches point of failure. */
35623562
}
35633563

3564-
static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
3564+
static void xfrm_negative_advice(struct sock *sk, struct dst_entry *dst)
35653565
{
3566-
if (dst) {
3567-
if (dst->obsolete) {
3568-
dst_release(dst);
3569-
dst = NULL;
3570-
}
3571-
}
3572-
return dst;
3566+
if (dst->obsolete)
3567+
sk_dst_reset(sk);
35733568
}
35743569

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

0 commit comments

Comments
 (0)