Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[NET]: Make packet reception network namespace safe

This patch modifies every packet receive function
registered with dev_add_pack() to drop packets if they
are not from the initial network namespace.

This should ensure that the various network stacks do
not receive packets in a anything but the initial network
namespace until the code has been converted and is ready
for them.

Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Eric W. Biederman and committed by
David S. Miller
e730c155 6d34b1c2

+113
+4
drivers/block/aoe/aoenet.c
··· 8 8 #include <linux/blkdev.h> 9 9 #include <linux/netdevice.h> 10 10 #include <linux/moduleparam.h> 11 + #include <net/net_namespace.h> 11 12 #include <asm/unaligned.h> 12 13 #include "aoe.h" 13 14 ··· 114 113 { 115 114 struct aoe_hdr *h; 116 115 u32 n; 116 + 117 + if (ifp->nd_net != &init_net) 118 + goto exit; 117 119 118 120 skb = skb_share_check(skb, GFP_ATOMIC); 119 121 if (skb == NULL)
+4
drivers/net/bonding/bond_3ad.c
··· 29 29 #include <linux/ethtool.h> 30 30 #include <linux/if_bonding.h> 31 31 #include <linux/pkt_sched.h> 32 + #include <net/net_namespace.h> 32 33 #include "bonding.h" 33 34 #include "bond_3ad.h" 34 35 ··· 2448 2447 struct bonding *bond = dev->priv; 2449 2448 struct slave *slave = NULL; 2450 2449 int ret = NET_RX_DROP; 2450 + 2451 + if (dev->nd_net != &init_net) 2452 + goto out; 2451 2453 2452 2454 if (!(dev->flags & IFF_MASTER)) 2453 2455 goto out;
+3
drivers/net/bonding/bond_alb.c
··· 345 345 struct arp_pkt *arp = (struct arp_pkt *)skb->data; 346 346 int res = NET_RX_DROP; 347 347 348 + if (bond_dev->nd_net != &init_net) 349 + goto out; 350 + 348 351 if (!(bond_dev->flags & IFF_MASTER)) 349 352 goto out; 350 353
+3
drivers/net/bonding/bond_main.c
··· 2458 2458 unsigned char *arp_ptr; 2459 2459 u32 sip, tip; 2460 2460 2461 + if (dev->nd_net != &init_net) 2462 + goto out; 2463 + 2461 2464 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER)) 2462 2465 goto out; 2463 2466
+3
drivers/net/hamradio/bpqether.c
··· 173 173 struct ethhdr *eth; 174 174 struct bpqdev *bpq; 175 175 176 + if (dev->nd_net != &init_net) 177 + goto drop; 178 + 176 179 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) 177 180 return NET_RX_DROP; 178 181
+6
drivers/net/pppoe.c
··· 389 389 if (!(skb = skb_share_check(skb, GFP_ATOMIC))) 390 390 goto out; 391 391 392 + if (dev->nd_net != &init_net) 393 + goto drop; 394 + 392 395 if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr))) 393 396 goto drop; 394 397 ··· 420 417 { 421 418 struct pppoe_hdr *ph; 422 419 struct pppox_sock *po; 420 + 421 + if (dev->nd_net != &init_net) 422 + goto abort; 423 423 424 424 if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr))) 425 425 goto abort;
+7
drivers/net/wan/hdlc.c
··· 36 36 #include <linux/rtnetlink.h> 37 37 #include <linux/notifier.h> 38 38 #include <linux/hdlc.h> 39 + #include <net/net_namespace.h> 39 40 40 41 41 42 static const char* version = "HDLC support module revision 1.21"; ··· 67 66 struct packet_type *p, struct net_device *orig_dev) 68 67 { 69 68 struct hdlc_device_desc *desc = dev_to_desc(dev); 69 + 70 + if (dev->nd_net != &init_net) { 71 + kfree_skb(skb); 72 + return 0; 73 + } 74 + 70 75 if (desc->netif_rx) 71 76 return desc->netif_rx(skb); 72 77
+3
drivers/net/wan/lapbether.c
··· 91 91 int len, err; 92 92 struct lapbethdev *lapbeth; 93 93 94 + if (dev->nd_net != &init_net) 95 + goto drop; 96 + 94 97 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) 95 98 return NET_RX_DROP; 96 99
+6
drivers/net/wan/syncppp.c
··· 51 51 #include <linux/spinlock.h> 52 52 #include <linux/rcupdate.h> 53 53 54 + #include <net/net_namespace.h> 54 55 #include <net/syncppp.h> 55 56 56 57 #include <asm/byteorder.h> ··· 1446 1445 1447 1446 static int sppp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *p, struct net_device *orig_dev) 1448 1447 { 1448 + if (dev->nd_net != &init_net) { 1449 + kfree_skb(skb); 1450 + return 0; 1451 + } 1452 + 1449 1453 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) 1450 1454 return NET_RX_DROP; 1451 1455 sppp_input(dev,skb);
+5
net/8021q/vlan_dev.c
··· 122 122 unsigned short vlan_TCI; 123 123 __be16 proto; 124 124 125 + if (dev->nd_net != &init_net) { 126 + kfree_skb(skb); 127 + return -1; 128 + } 129 + 125 130 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) 126 131 return -1; 127 132
+3
net/appletalk/aarp.c
··· 713 713 struct atalk_addr sa, *ma, da; 714 714 struct atalk_iface *ifa; 715 715 716 + if (dev->nd_net != &init_net) 717 + goto out0; 718 + 716 719 /* We only do Ethernet SNAP AARP. */ 717 720 if (dev->type != ARPHRD_ETHER) 718 721 goto out0;
+6
net/appletalk/ddp.c
··· 1403 1403 int origlen; 1404 1404 __u16 len_hops; 1405 1405 1406 + if (dev->nd_net != &init_net) 1407 + goto freeit; 1408 + 1406 1409 /* Don't mangle buffer if shared */ 1407 1410 if (!(skb = skb_share_check(skb, GFP_ATOMIC))) 1408 1411 goto out; ··· 1491 1488 static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev, 1492 1489 struct packet_type *pt, struct net_device *orig_dev) 1493 1490 { 1491 + if (dev->nd_net != &init_net) 1492 + goto freeit; 1493 + 1494 1494 /* Expand any short form frames */ 1495 1495 if (skb_mac_header(skb)[2] == 1) { 1496 1496 struct ddpehdr *ddp;
+5
net/ax25/ax25_in.c
··· 451 451 skb->sk = NULL; /* Initially we don't know who it's for */ 452 452 skb->destructor = NULL; /* Who initializes this, dammit?! */ 453 453 454 + if (dev->nd_net != &init_net) { 455 + kfree_skb(skb); 456 + return 0; 457 + } 458 + 454 459 if ((*skb->data & 0x0F) != 0) { 455 460 kfree_skb(skb); /* Not a KISS data frame */ 456 461 return 0;
+4
net/bridge/br_stp_bpdu.c
··· 17 17 #include <linux/netfilter_bridge.h> 18 18 #include <linux/etherdevice.h> 19 19 #include <linux/llc.h> 20 + #include <net/net_namespace.h> 20 21 #include <net/llc.h> 21 22 #include <net/llc_pdu.h> 22 23 #include <asm/unaligned.h> ··· 141 140 struct net_bridge_port *p = rcu_dereference(dev->br_port); 142 141 struct net_bridge *br; 143 142 const unsigned char *buf; 143 + 144 + if (dev->nd_net != &init_net) 145 + goto err; 144 146 145 147 if (!p) 146 148 goto err;
+3
net/decnet/dn_route.c
··· 584 584 struct dn_dev *dn = (struct dn_dev *)dev->dn_ptr; 585 585 unsigned char padlen = 0; 586 586 587 + if (dev->nd_net != &init_net) 588 + goto dump_it; 589 + 587 590 if (dn == NULL) 588 591 goto dump_it; 589 592
+3
net/econet/af_econet.c
··· 1065 1065 struct sock *sk; 1066 1066 struct ec_device *edev = dev->ec_ptr; 1067 1067 1068 + if (dev->nd_net != &init_net) 1069 + goto drop; 1070 + 1068 1071 if (skb->pkt_type == PACKET_OTHERHOST) 1069 1072 goto drop; 1070 1073
+3
net/ipv4/arp.c
··· 932 932 { 933 933 struct arphdr *arp; 934 934 935 + if (dev->nd_net != &init_net) 936 + goto freeskb; 937 + 935 938 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */ 936 939 if (!pskb_may_pull(skb, (sizeof(struct arphdr) + 937 940 (2 * dev->addr_len) +
+3
net/ipv4/ip_input.c
··· 382 382 struct iphdr *iph; 383 383 u32 len; 384 384 385 + if (dev->nd_net != &init_net) 386 + goto drop; 387 + 385 388 /* When the interface is in promisc. mode, drop all the crap 386 389 * that it receives, do not try to analyse it. 387 390 */
+6
net/ipv4/ipconfig.c
··· 426 426 unsigned char *sha, *tha; /* s for "source", t for "target" */ 427 427 struct ic_device *d; 428 428 429 + if (dev->nd_net != &init_net) 430 + goto drop; 431 + 429 432 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) 430 433 return NET_RX_DROP; 431 434 ··· 837 834 struct iphdr *h; 838 835 struct ic_device *d; 839 836 int len, ext_len; 837 + 838 + if (dev->nd_net != &init_net) 839 + goto drop; 840 840 841 841 /* Perform verifications before taking the lock. */ 842 842 if (skb->pkt_type == PACKET_OTHERHOST)
+5
net/ipv6/ip6_input.c
··· 61 61 u32 pkt_len; 62 62 struct inet6_dev *idev; 63 63 64 + if (dev->nd_net != &init_net) { 65 + kfree_skb(skb); 66 + return 0; 67 + } 68 + 64 69 if (skb->pkt_type == PACKET_OTHERHOST) { 65 70 kfree_skb(skb); 66 71 return 0;
+3
net/ipx/af_ipx.c
··· 1647 1647 u16 ipx_pktsize; 1648 1648 int rc = 0; 1649 1649 1650 + if (dev->nd_net != &init_net) 1651 + goto drop; 1652 + 1650 1653 /* Not ours */ 1651 1654 if (skb->pkt_type == PACKET_OTHERHOST) 1652 1655 goto drop;
+3
net/irda/irlap_frame.c
··· 1326 1326 int command; 1327 1327 __u8 control; 1328 1328 1329 + if (dev->nd_net != &init_net) 1330 + goto out; 1331 + 1329 1332 /* FIXME: should we get our own field? */ 1330 1333 self = (struct irlap_cb *) dev->atalk_ptr; 1331 1334
+4
net/llc/llc_input.c
··· 12 12 * See the GNU General Public License for more details. 13 13 */ 14 14 #include <linux/netdevice.h> 15 + #include <net/net_namespace.h> 15 16 #include <net/llc.h> 16 17 #include <net/llc_pdu.h> 17 18 #include <net/llc_sap.h> ··· 145 144 int dest; 146 145 int (*rcv)(struct sk_buff *, struct net_device *, 147 146 struct packet_type *, struct net_device *); 147 + 148 + if (dev->nd_net != &init_net) 149 + goto drop; 148 150 149 151 /* 150 152 * When the interface is in promisc. mode, drop all the crap that it
+9
net/packet/af_packet.c
··· 252 252 struct sock *sk; 253 253 struct sockaddr_pkt *spkt; 254 254 255 + if (dev->nd_net != &init_net) 256 + goto out; 257 + 255 258 /* 256 259 * When we registered the protocol we saved the socket in the data 257 260 * field for just this event. ··· 455 452 int skb_len = skb->len; 456 453 unsigned int snaplen, res; 457 454 455 + if (dev->nd_net != &init_net) 456 + goto drop; 457 + 458 458 if (skb->pkt_type == PACKET_LOOPBACK) 459 459 goto drop; 460 460 ··· 573 567 unsigned short macoff, netoff; 574 568 struct sk_buff *copy_skb = NULL; 575 569 struct timeval tv; 570 + 571 + if (dev->nd_net != &init_net) 572 + goto drop; 576 573 577 574 if (skb->pkt_type == PACKET_LOOPBACK) 578 575 goto drop;
+6
net/tipc/eth_media.c
··· 38 38 #include <net/tipc/tipc_bearer.h> 39 39 #include <net/tipc/tipc_msg.h> 40 40 #include <linux/netdevice.h> 41 + #include <net/net_namespace.h> 41 42 42 43 #define MAX_ETH_BEARERS 2 43 44 #define ETH_LINK_PRIORITY TIPC_DEF_LINK_PRI ··· 100 99 { 101 100 struct eth_bearer *eb_ptr = (struct eth_bearer *)pt->af_packet_priv; 102 101 u32 size; 102 + 103 + if (dev->nd_net != &init_net) { 104 + kfree_skb(buf); 105 + return 0; 106 + } 103 107 104 108 if (likely(eb_ptr->bearer)) { 105 109 if (likely(buf->pkt_type <= PACKET_BROADCAST)) {
+3
net/x25/x25_dev.c
··· 95 95 struct sk_buff *nskb; 96 96 struct x25_neigh *nb; 97 97 98 + if (dev->nd_net != &init_net) 99 + goto drop; 100 + 98 101 nskb = skb_copy(skb, GFP_ATOMIC); 99 102 if (!nskb) 100 103 goto drop;