Skip to content

Commit 00adedc

Browse files
committed
net: fix __dst_negative_advice() race
jira VULN-8973 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 ff7b829 commit 00adedc

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
@@ -24,7 +24,8 @@ 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+
RH_KABI_REPLACE(struct dst_entry * (*negative_advice)(struct dst_entry *),
28+
void (*negative_advice)(struct sock *sk, struct dst_entry *))
2829
void (*link_failure)(struct sk_buff *);
2930
void (*update_pmtu)(struct dst_entry *dst, struct sock *sk,
3031
struct sk_buff *skb, u32 mtu,

include/net/sock.h

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

21222122
static inline void __dst_negative_advice(struct sock *sk)
21232123
{
2124-
struct dst_entry *ndst, *dst = __sk_dst_get(sk);
2124+
struct dst_entry *dst = __sk_dst_get(sk);
21252125

2126-
if (dst && dst->ops->negative_advice) {
2127-
ndst = dst->ops->negative_advice(dst);
2128-
2129-
if (ndst != dst) {
2130-
rcu_assign_pointer(sk->sk_dst_cache, ndst);
2131-
sk_tx_queue_clear(sk);
2132-
sk->sk_dst_pending_confirm = 0;
2133-
}
2134-
}
2126+
if (dst && dst->ops->negative_advice)
2127+
dst->ops->negative_advice(sk, dst);
21352128
}
21362129

21372130
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 @@ struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
140140
static unsigned int ipv4_default_advmss(const struct dst_entry *dst);
141141
INDIRECT_CALLABLE_SCOPE
142142
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,
@@ -844,22 +845,15 @@ static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buf
844845
__ip_do_redirect(rt, skb, &fl4, true);
845846
}
846847

847-
static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
848+
static void ipv4_negative_advice(struct sock *sk,
849+
struct dst_entry *dst)
848850
{
849851
struct rtable *rt = (struct rtable *)dst;
850-
struct dst_entry *ret = dst;
851852

852-
if (rt) {
853-
if (dst->obsolete > 0) {
854-
ip_rt_put(rt);
855-
ret = NULL;
856-
} else if ((rt->rt_flags & RTCF_REDIRECTED) ||
857-
rt->dst.expires) {
858-
ip_rt_put(rt);
859-
ret = NULL;
860-
}
861-
}
862-
return ret;
853+
if ((dst->obsolete > 0) ||
854+
(rt->rt_flags & RTCF_REDIRECTED) ||
855+
rt->dst.expires)
856+
sk_dst_reset(sk);
863857
}
864858

865859
/*

net/ipv6/route.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
8787
static unsigned int ip6_default_advmss(const struct dst_entry *dst);
8888
INDIRECT_CALLABLE_SCOPE
8989
unsigned int ip6_mtu(const struct dst_entry *dst);
90-
static struct dst_entry *ip6_negative_advice(struct dst_entry *);
90+
static void ip6_negative_advice(struct sock *sk,
91+
struct dst_entry *dst);
9192
static void ip6_dst_destroy(struct dst_entry *);
9293
static void ip6_dst_ifdown(struct dst_entry *,
9394
struct net_device *dev, int how);
@@ -2757,24 +2758,24 @@ INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
27572758
}
27582759
EXPORT_INDIRECT_CALLABLE(ip6_dst_check);
27592760

2760-
static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2761+
static void ip6_negative_advice(struct sock *sk,
2762+
struct dst_entry *dst)
27612763
{
27622764
struct rt6_info *rt = (struct rt6_info *) dst;
27632765

2764-
if (rt) {
2765-
if (rt->rt6i_flags & RTF_CACHE) {
2766-
rcu_read_lock();
2767-
if (rt6_check_expired(rt)) {
2768-
rt6_remove_exception_rt(rt);
2769-
dst = NULL;
2770-
}
2771-
rcu_read_unlock();
2772-
} else {
2773-
dst_release(dst);
2774-
dst = NULL;
2766+
if (rt->rt6i_flags & RTF_CACHE) {
2767+
rcu_read_lock();
2768+
if (rt6_check_expired(rt)) {
2769+
/* counteract the dst_release() in sk_dst_reset() */
2770+
dst_hold(dst);
2771+
sk_dst_reset(sk);
2772+
2773+
rt6_remove_exception_rt(rt);
27752774
}
2775+
rcu_read_unlock();
2776+
return;
27762777
}
2777-
return dst;
2778+
sk_dst_reset(sk);
27782779
}
27792780

27802781
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
@@ -3744,15 +3744,10 @@ static void xfrm_link_failure(struct sk_buff *skb)
37443744
/* Impossible. Such dst must be popped before reaches point of failure. */
37453745
}
37463746

3747-
static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
3747+
static void xfrm_negative_advice(struct sock *sk, struct dst_entry *dst)
37483748
{
3749-
if (dst) {
3750-
if (dst->obsolete) {
3751-
dst_release(dst);
3752-
dst = NULL;
3753-
}
3754-
}
3755-
return dst;
3749+
if (dst->obsolete)
3750+
sk_dst_reset(sk);
37563751
}
37573752

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

0 commit comments

Comments
 (0)