Commit 3b8377dc authored by David S. Miller's avatar David S. Miller

Merge branch 'variable-length-ll-headers'

Willem de Bruijn says:

====================
net: validate variable length ll headers

Allow device-specific validation of link layer headers. Existing
checks drop all packets shorter than hard_header_len. For variable
length protocols, such packets can be valid.

patch 1 adds header_ops.validate and dev_validate_header
patch 2 implements the protocol specific callback for AX25
patch 3 replaces ll_header_truncated with dev_validate_header
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 9531ab65 9ed988cd
...@@ -268,6 +268,7 @@ struct header_ops { ...@@ -268,6 +268,7 @@ struct header_ops {
void (*cache_update)(struct hh_cache *hh, void (*cache_update)(struct hh_cache *hh,
const struct net_device *dev, const struct net_device *dev,
const unsigned char *haddr); const unsigned char *haddr);
bool (*validate)(const char *ll_header, unsigned int len);
}; };
/* These flag bits are private to the generic network queueing /* These flag bits are private to the generic network queueing
...@@ -1459,8 +1460,7 @@ enum netdev_priv_flags { ...@@ -1459,8 +1460,7 @@ enum netdev_priv_flags {
* @dma: DMA channel * @dma: DMA channel
* @mtu: Interface MTU value * @mtu: Interface MTU value
* @type: Interface hardware type * @type: Interface hardware type
* @hard_header_len: Hardware header length, which means that this is the * @hard_header_len: Maximum hardware header length.
* minimum size of a packet.
* *
* @needed_headroom: Extra headroom the hardware may need, but not in all * @needed_headroom: Extra headroom the hardware may need, but not in all
* cases can this be guaranteed * cases can this be guaranteed
...@@ -2687,6 +2687,24 @@ static inline int dev_parse_header(const struct sk_buff *skb, ...@@ -2687,6 +2687,24 @@ static inline int dev_parse_header(const struct sk_buff *skb,
return dev->header_ops->parse(skb, haddr); return dev->header_ops->parse(skb, haddr);
} }
/* ll_header must have at least hard_header_len allocated */
static inline bool dev_validate_header(const struct net_device *dev,
char *ll_header, int len)
{
if (likely(len >= dev->hard_header_len))
return true;
if (capable(CAP_SYS_RAWIO)) {
memset(ll_header + len, 0, dev->hard_header_len - len);
return true;
}
if (dev->header_ops && dev->header_ops->validate)
return dev->header_ops->validate(ll_header, len);
return false;
}
typedef int gifconf_func_t(struct net_device * dev, char __user * bufptr, int len); typedef int gifconf_func_t(struct net_device * dev, char __user * bufptr, int len);
int register_gifconf(unsigned int family, gifconf_func_t *gifconf); int register_gifconf(unsigned int family, gifconf_func_t *gifconf);
static inline int unregister_gifconf(unsigned int family) static inline int unregister_gifconf(unsigned int family)
......
...@@ -228,8 +228,23 @@ netdev_tx_t ax25_ip_xmit(struct sk_buff *skb) ...@@ -228,8 +228,23 @@ netdev_tx_t ax25_ip_xmit(struct sk_buff *skb)
} }
#endif #endif
static bool ax25_validate_header(const char *header, unsigned int len)
{
ax25_digi digi;
if (!len)
return false;
if (header[0])
return true;
return ax25_addr_parse(header + 1, len - 1, NULL, NULL, &digi, NULL,
NULL);
}
const struct header_ops ax25_header_ops = { const struct header_ops ax25_header_ops = {
.create = ax25_hard_header, .create = ax25_hard_header,
.validate = ax25_validate_header,
}; };
EXPORT_SYMBOL(ax25_header_ops); EXPORT_SYMBOL(ax25_header_ops);
......
...@@ -1915,6 +1915,10 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg, ...@@ -1915,6 +1915,10 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
goto retry; goto retry;
} }
if (!dev_validate_header(dev, skb->data, len)) {
err = -EINVAL;
goto out_unlock;
}
if (len > (dev->mtu + dev->hard_header_len + extra_len) && if (len > (dev->mtu + dev->hard_header_len + extra_len) &&
!packet_extra_vlan_len_allowed(dev, skb)) { !packet_extra_vlan_len_allowed(dev, skb)) {
err = -EMSGSIZE; err = -EMSGSIZE;
...@@ -2393,18 +2397,6 @@ static void tpacket_destruct_skb(struct sk_buff *skb) ...@@ -2393,18 +2397,6 @@ static void tpacket_destruct_skb(struct sk_buff *skb)
sock_wfree(skb); sock_wfree(skb);
} }
static bool ll_header_truncated(const struct net_device *dev, int len)
{
/* net device doesn't like empty head */
if (unlikely(len < dev->hard_header_len)) {
net_warn_ratelimited("%s: packet size is too short (%d < %d)\n",
current->comm, len, dev->hard_header_len);
return true;
}
return false;
}
static void tpacket_set_protocol(const struct net_device *dev, static void tpacket_set_protocol(const struct net_device *dev,
struct sk_buff *skb) struct sk_buff *skb)
{ {
...@@ -2522,16 +2514,20 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb, ...@@ -2522,16 +2514,20 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
if (unlikely(err < 0)) if (unlikely(err < 0))
return -EINVAL; return -EINVAL;
} else if (copylen) { } else if (copylen) {
int hdrlen = min_t(int, copylen, tp_len);
skb_push(skb, dev->hard_header_len); skb_push(skb, dev->hard_header_len);
skb_put(skb, copylen - dev->hard_header_len); skb_put(skb, copylen - dev->hard_header_len);
err = skb_store_bits(skb, 0, data, copylen); err = skb_store_bits(skb, 0, data, hdrlen);
if (unlikely(err)) if (unlikely(err))
return err; return err;
if (!dev_validate_header(dev, skb->data, hdrlen))
return -EINVAL;
if (!skb->protocol) if (!skb->protocol)
tpacket_set_protocol(dev, skb); tpacket_set_protocol(dev, skb);
data += copylen; data += hdrlen;
to_write -= copylen; to_write -= hdrlen;
} }
offset = offset_in_page(data); offset = offset_in_page(data);
...@@ -2703,13 +2699,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) ...@@ -2703,13 +2699,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
copylen = __virtio16_to_cpu(vio_le(), copylen = __virtio16_to_cpu(vio_le(),
vnet_hdr->hdr_len); vnet_hdr->hdr_len);
} }
if (dev->hard_header_len) { copylen = max_t(int, copylen, dev->hard_header_len);
if (ll_header_truncated(dev, tp_len)) {
tp_len = -EINVAL;
goto tpacket_error;
}
copylen = max_t(int, copylen, dev->hard_header_len);
}
skb = sock_alloc_send_skb(&po->sk, skb = sock_alloc_send_skb(&po->sk,
hlen + tlen + sizeof(struct sockaddr_ll) + hlen + tlen + sizeof(struct sockaddr_ll) +
(copylen - dev->hard_header_len), (copylen - dev->hard_header_len),
...@@ -2905,9 +2895,6 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len) ...@@ -2905,9 +2895,6 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len); offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
if (unlikely(offset < 0)) if (unlikely(offset < 0))
goto out_free; goto out_free;
} else {
if (ll_header_truncated(dev, len))
goto out_free;
} }
/* Returns -EFAULT on error */ /* Returns -EFAULT on error */
...@@ -2915,6 +2902,12 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len) ...@@ -2915,6 +2902,12 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
if (err) if (err)
goto out_free; goto out_free;
if (sock->type == SOCK_RAW &&
!dev_validate_header(dev, skb->data, len)) {
err = -EINVAL;
goto out_free;
}
sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags); sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) && if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment