Skip to content

Commit f60e599

Browse files
strssndktndavem330
authored andcommitted
ipv6: protect skb->sk accesses from recursive dereference inside the stack
We should not consult skb->sk for output decisions in xmit recursion levels > 0 in the stack. Otherwise local socket settings could influence the result of e.g. tunnel encapsulation process. ipv6 does not conform with this in three places: 1) ip6_fragment: we do consult ipv6_npinfo for frag_size 2) sk_mc_loop in ipv6 uses skb->sk and checks if we should loop the packet back to the local socket 3) ip6_skb_dst_mtu could query the settings from the user socket and force a wrong MTU Furthermore: In sk_mc_loop we could potentially land in WARN_ON(1) if we use a PF_PACKET socket ontop of an IPv6-backed vxlan device. Reuse xmit_recursion as we are currently only interested in protecting tunnel devices. Cc: Jiri Pirko <[email protected]> Signed-off-by: Hannes Frederic Sowa <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 576b7cd commit f60e599

File tree

7 files changed

+34
-19
lines changed

7 files changed

+34
-19
lines changed

include/linux/netdevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,12 @@ void netdev_freemem(struct net_device *dev);
21852185
void synchronize_net(void);
21862186
int init_dummy_netdev(struct net_device *dev);
21872187

2188+
DECLARE_PER_CPU(int, xmit_recursion);
2189+
static inline int dev_recursion_level(void)
2190+
{
2191+
return this_cpu_read(xmit_recursion);
2192+
}
2193+
21882194
struct net_device *dev_get_by_index(struct net *net, int ifindex);
21892195
struct net_device *__dev_get_by_index(struct net *net, int ifindex);
21902196
struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex);

include/net/ip.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -453,22 +453,6 @@ static __inline__ void inet_reset_saddr(struct sock *sk)
453453

454454
#endif
455455

456-
static inline int sk_mc_loop(struct sock *sk)
457-
{
458-
if (!sk)
459-
return 1;
460-
switch (sk->sk_family) {
461-
case AF_INET:
462-
return inet_sk(sk)->mc_loop;
463-
#if IS_ENABLED(CONFIG_IPV6)
464-
case AF_INET6:
465-
return inet6_sk(sk)->mc_loop;
466-
#endif
467-
}
468-
WARN_ON(1);
469-
return 1;
470-
}
471-
472456
bool ip_call_ra_chain(struct sk_buff *skb);
473457

474458
/*

include/net/ip6_route.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *));
174174

175175
static inline int ip6_skb_dst_mtu(struct sk_buff *skb)
176176
{
177-
struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
177+
struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
178+
inet6_sk(skb->sk) : NULL;
178179

179180
return (np && np->pmtudisc >= IPV6_PMTUDISC_PROBE) ?
180181
skb_dst(skb)->dev->mtu : dst_mtu(skb_dst(skb));

include/net/sock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,8 @@ struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie);
17621762

17631763
struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie);
17641764

1765+
bool sk_mc_loop(struct sock *sk);
1766+
17651767
static inline bool sk_can_gso(const struct sock *sk)
17661768
{
17671769
return net_gso_ok(sk->sk_route_caps, sk->sk_gso_type);

net/core/dev.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2848,7 +2848,9 @@ static void skb_update_prio(struct sk_buff *skb)
28482848
#define skb_update_prio(skb)
28492849
#endif
28502850

2851-
static DEFINE_PER_CPU(int, xmit_recursion);
2851+
DEFINE_PER_CPU(int, xmit_recursion);
2852+
EXPORT_SYMBOL(xmit_recursion);
2853+
28522854
#define RECURSION_LIMIT 10
28532855

28542856
/**

net/core/sock.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,25 @@ static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
653653
sock_reset_flag(sk, bit);
654654
}
655655

656+
bool sk_mc_loop(struct sock *sk)
657+
{
658+
if (dev_recursion_level())
659+
return false;
660+
if (!sk)
661+
return true;
662+
switch (sk->sk_family) {
663+
case AF_INET:
664+
return inet_sk(sk)->mc_loop;
665+
#if IS_ENABLED(CONFIG_IPV6)
666+
case AF_INET6:
667+
return inet6_sk(sk)->mc_loop;
668+
#endif
669+
}
670+
WARN_ON(1);
671+
return true;
672+
}
673+
EXPORT_SYMBOL(sk_mc_loop);
674+
656675
/*
657676
* This is meant for all protocols to use and covers goings on
658677
* at the socket level. Everything here is generic.

net/ipv6/ip6_output.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
542542
{
543543
struct sk_buff *frag;
544544
struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
545-
struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
545+
struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
546+
inet6_sk(skb->sk) : NULL;
546547
struct ipv6hdr *tmp_hdr;
547548
struct frag_hdr *fh;
548549
unsigned int mtu, hlen, left, len;

0 commit comments

Comments
 (0)