Skip to content

Commit 7b34ca2

Browse files
committed
inet: Avoid potential NULL peer dereference.
We handle NULL in rt{,6}_set_peer but then our caller will try to pass that NULL pointer into inet_putpeer() which isn't ready for it. Fix this by moving the NULL check one level up, and then remove the now unnecessary NULL check from inetpeer_ptr_set_peer(). Reported-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8b96d22 commit 7b34ca2

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

‎include/net/inetpeer.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static inline bool inetpeer_ptr_set_peer(unsigned long *ptr, struct inet_peer *p
104104
unsigned long val = (unsigned long) peer;
105105
unsigned long orig = *ptr;
106106

107-
if (!(orig & INETPEER_BASE_BIT) || !val ||
107+
if (!(orig & INETPEER_BASE_BIT) ||
108108
cmpxchg(ptr, orig, val) != orig)
109109
return false;
110110
return true;

‎net/ipv4/route.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,11 +1333,12 @@ void rt_bind_peer(struct rtable *rt, __be32 daddr, int create)
13331333
return;
13341334

13351335
peer = inet_getpeer_v4(base, daddr, create);
1336-
1337-
if (!rt_set_peer(rt, peer))
1338-
inet_putpeer(peer);
1339-
else
1340-
rt->rt_peer_genid = rt_peer_genid();
1336+
if (peer) {
1337+
if (!rt_set_peer(rt, peer))
1338+
inet_putpeer(peer);
1339+
else
1340+
rt->rt_peer_genid = rt_peer_genid();
1341+
}
13411342
}
13421343

13431344
/*

‎net/ipv6/route.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,12 @@ void rt6_bind_peer(struct rt6_info *rt, int create)
313313
return;
314314

315315
peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
316-
if (!rt6_set_peer(rt, peer))
317-
inet_putpeer(peer);
318-
else
319-
rt->rt6i_peer_genid = rt6_peer_genid();
316+
if (peer) {
317+
if (!rt6_set_peer(rt, peer))
318+
inet_putpeer(peer);
319+
else
320+
rt->rt6i_peer_genid = rt6_peer_genid();
321+
}
320322
}
321323

322324
static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,

0 commit comments

Comments
 (0)