History

vlan: consolidate VLAN parsing code and limit max parsing depth

Toshiaki pointed out that we now have two very similar functions to extract the L3 protocol number in the presence of VLAN tags. And Daniel pointed out that the unbounded parsing loop makes it possible for maliciously crafted packets to loop through potentially hundreds of tags. Fix both of these issues by consolidating the two parsing functions and limiting the VLAN tag parsing to a max depth of 8 tags. As part of this, switch over __vlan_get_protocol() to use skb_header_pointer() instead of pskb_may_pull(), to avoid the possible side effects of the latter and keep the skb pointer 'const' through all the parsing functions. v2: - Use limit of 8 tags instead of 32 (matching XMIT_RECURSION_LIMIT)

Toke Høiland-Jørgensen committed 6 years ago

Commit 469aced1 parent da328711 file · +22 −35Browse files

Showing the changes to include/linux (1 of 1 file). Show all

Unified · Split

include/linux/if_vlan.h+22−35
@@ -25,6 +25,8 @@ · 24 unmodified lines
25#define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */25#define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */
26#define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */26#define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */
27 27
28#define VLAN_MAX_DEPTH 8 /* Max. number of nested VLAN tags parsed */
29
28/*30/*
29 * struct vlan_hdr - vlan header31 * struct vlan_hdr - vlan header
30 * @h_vlan_TCI: priority and VLAN ID32 * @h_vlan_TCI: priority and VLAN ID
@@ -308,34 +310,6 @@ static inline bool eth_type_vlan(__be16 ethertype) · 277 unmodified lines
308 }310 }
309}311}
310 312
311/* A getter for the SKB protocol field which will handle VLAN tags consistently
312 * whether VLAN acceleration is enabled or not.
313 */
314static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
315{
316 unsigned int offset = skb_mac_offset(skb) + sizeof(struct ethhdr);
317 __be16 proto = skb->protocol;
318
319 if (!skip_vlan)
320 /* VLAN acceleration strips the VLAN header from the skb and
321 * moves it to skb->vlan_proto
322 */
323 return skb_vlan_tag_present(skb) ? skb->vlan_proto : proto;
324
325 while (eth_type_vlan(proto)) {
326 struct vlan_hdr vhdr, *vh;
327
328 vh = skb_header_pointer(skb, offset, sizeof(vhdr), &vhdr);
329 if (!vh)
330 break;
331
332 proto = vh->h_vlan_encapsulated_proto;
333 offset += sizeof(vhdr);
334 }
335
336 return proto;
337}
338
339static inline bool vlan_hw_offload_capable(netdev_features_t features,313static inline bool vlan_hw_offload_capable(netdev_features_t features,
340 __be16 proto)314 __be16 proto)
341{315{
@@ -605,10 +579,10 @@ static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) · 263 unmodified lines
605 * Returns the EtherType of the packet, regardless of whether it is579 * Returns the EtherType of the packet, regardless of whether it is
606 * vlan encapsulated (normal or hardware accelerated) or not.580 * vlan encapsulated (normal or hardware accelerated) or not.
607 */581 */
608static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type,582static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
609 int *depth)583 int *depth)
610{584{
611 unsigned int vlan_depth = skb->mac_len;585 unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH;
612 586
613 /* if type is 802.1Q/AD then the header should already be587 /* if type is 802.1Q/AD then the header should already be
614 * present at mac_len - VLAN_HLEN (if mac_len > 0), or at588 * present at mac_len - VLAN_HLEN (if mac_len > 0), or at
@@ -623,13 +597,12 @@ static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type, · 8 unmodified lines
623 vlan_depth = ETH_HLEN;597 vlan_depth = ETH_HLEN;
624 }598 }
625 do {599 do {
626 struct vlan_hdr *vh;600 struct vlan_hdr vhdr, *vh;
627 601
628 if (unlikely(!pskb_may_pull(skb,602 vh = skb_header_pointer(skb, vlan_depth, sizeof(vhdr), &vhdr);
629 vlan_depth + VLAN_HLEN)))603 if (unlikely(!vh || !--parse_depth))
630 return 0;604 return 0;
631 605
632 vh = (struct vlan_hdr *)(skb->data + vlan_depth);
633 type = vh->h_vlan_encapsulated_proto;606 type = vh->h_vlan_encapsulated_proto;
634 vlan_depth += VLAN_HLEN;607 vlan_depth += VLAN_HLEN;
635 } while (eth_type_vlan(type));608 } while (eth_type_vlan(type));
@@ -648,11 +621,25 @@ static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type, · 12 unmodified lines
648 * Returns the EtherType of the packet, regardless of whether it is621 * Returns the EtherType of the packet, regardless of whether it is
649 * vlan encapsulated (normal or hardware accelerated) or not.622 * vlan encapsulated (normal or hardware accelerated) or not.
650 */623 */
651static inline __be16 vlan_get_protocol(struct sk_buff *skb)624static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
652{625{
653 return __vlan_get_protocol(skb, skb->protocol, NULL);626 return __vlan_get_protocol(skb, skb->protocol, NULL);
654}627}
655 628
629/* A getter for the SKB protocol field which will handle VLAN tags consistently
630 * whether VLAN acceleration is enabled or not.
631 */
632static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
633{
634 if (!skip_vlan)
635 /* VLAN acceleration strips the VLAN header from the skb and
636 * moves it to skb->vlan_proto
637 */
638 return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
639
640 return vlan_get_protocol(skb);
641}
642
656static inline void vlan_set_encap_proto(struct sk_buff *skb,643static inline void vlan_set_encap_proto(struct sk_buff *skb,
657 struct vlan_hdr *vhdr)644 struct vlan_hdr *vhdr)
658{645{

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t