Skip to contentSkip to conversation

bpf: fix bpf_skb_adjust_net/bpf_skb_proto_xlat to deal with gso sctp skbs

PATCH Applied Daniel Borkmann 3 participants 4 messages Mar 3, 2018

Subsystems (5)
lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

PATCH 1/1 bpf: fix bpf_skb_adjust_net/bpf_skb_proto_xlat to deal with gso sctp skbs

From: Daniel Axtens <[email protected]>

SCTP GSO skbs have a gso_size of GSO_BY_FRAGS, so any sort of unconditionally mangling of that will result in nonsense value and would corrupt the skb later on.

Therefore, i) add two helpers skb_increase_gso_size() and skb_decrease_gso_size() that would throw a one time warning and bail out for such skbs and ii) refuse and return early with an error in those BPF helpers that are affected. We do need to bail out as early as possible from there before any changes on the skb have been performed.

Fixes 6578171a7ff0 ("bpf: add bpf_skb_change_proto helper")
Cc Marcelo Ricardo Leitner <[email protected]>

---
[ Also squashed the two into one, so that this is better for
backporting and has no other dependency. ]

 Documentation/networking/segmentation-offloads.txt | 11 +++-
 include/linux/skbuff.h                             | 22 ++++++++
 net/core/filter.c                                  | 60 +++++++++++++++-------
 3 files changed, 73 insertions(+), 20 deletions(-)
Documentation/networking/segmentation-offloads.txt +9 -2
index d47480b..23a8dd9 100644
@@ -153,8 +153,15 @@ To signal this, gso_size is set to the special value GSO_BY_FRAGS.
 
 Therefore, any code in the core networking stack must be aware of the
 possibility that gso_size will be GSO_BY_FRAGS and handle that case
-appropriately. (For size checks, the skb_gso_validate_*_len family of
-helpers do this automatically.)
+appropriately.
+
+There are a couple of helpers to make this easier:
+
+ - For size checks, the skb_gso_validate_*_len family of helpers correctly
+   considers GSO_BY_FRAGS.
+
+ - For manipulating packets, skb_increase_gso_size and skb_decrease_gso_size
+   will check for GSO_BY_FRAGS and WARN if asked to manipulate these skbs.
 
 This also affects drivers with the NETIF_F_FRAGLIST & NETIF_F_GSO_SCTP bits
 set. Note also that NETIF_F_GSO_SCTP is included in NETIF_F_GSO_SOFTWARE.
include/linux/skbuff.h +22 -0
index c1e66bd..8c67c33 100644
@@ -4038,6 +4038,12 @@ static inline bool skb_is_gso_v6(const struct sk_buff *skb)
 	return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
 }
 
+/* Note: Should be called only if skb_is_gso(skb) is true */
+static inline bool skb_is_gso_sctp(const struct sk_buff *skb)
+{
+	return skb_shinfo(skb)->gso_type & SKB_GSO_SCTP;
+}
+
 static inline void skb_gso_reset(struct sk_buff *skb)
 {
 	skb_shinfo(skb)->gso_size = 0;
@@ -4045,6 +4051,22 @@ static inline void skb_gso_reset(struct sk_buff *skb)
 	skb_shinfo(skb)->gso_type = 0;
 }
 
+static inline void skb_increase_gso_size(struct skb_shared_info *shinfo,
+					 u16 increment)
+{
+	if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS))
+		return;
+	shinfo->gso_size += increment;
+}
+
+static inline void skb_decrease_gso_size(struct skb_shared_info *shinfo,
+					 u16 decrement)
+{
+	if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS))
+		return;
+	shinfo->gso_size -= decrement;
+}
+
 void __skb_warn_lro_forwarding(const struct sk_buff *skb);
 
 static inline bool skb_warn_if_lro(const struct sk_buff *skb)
net/core/filter.c +42 -18

126 lines not on this page · raw message

Signature · 1 line
2.9.5
#1 lore raw

Hi Daniel,

Quoted 13 lines

From: Daniel Axtens <[email protected]>

SCTP GSO skbs have a gso_size of GSO_BY_FRAGS, so any sort of unconditionally mangling of that will result in nonsense value and would corrupt the skb later on.

Therefore, i) add two helpers skb_increase_gso_size() and skb_decrease_gso_size() that would throw a one time warning and bail out for such skbs and ii) refuse and return early with an error in those BPF helpers that are affected. We do need to bail out as early as possible from there before any changes on the skb have been performed.

Quoted 5 lines
Fixes 6578171a7ff0 ("bpf: add bpf_skb_change_proto helper")
Cc Marcelo Ricardo Leitner <[email protected]>

I've looked over your changes and they all look good to me.

include/linux/skbuff.h static inline bool skb_is_gso_v6(const struct sk_buff *skb) · line 4046

Quoted 6 lines
include/linux/skbuff.h +6 -0
+/* Note: Should be called only if skb_is_gso(skb) is true */
+static inline bool skb_is_gso_sctp(const struct sk_buff *skb)
+{
+	return skb_shinfo(skb)->gso_type & SKB_GSO_SCTP;
+}
+

This helper is a fantastic idea and I will send a docs update to highlight it.

Regards,
Daniel

author maintainer
#2 lore raw
Quoted 21 lines · Daniel Axtens
Quoted 13 lines

From: Daniel Axtens <[email protected]>

SCTP GSO skbs have a gso_size of GSO_BY_FRAGS, so any sort of unconditionally mangling of that will result in nonsense value and would corrupt the skb later on.

Therefore, i) add two helpers skb_increase_gso_size() and skb_decrease_gso_size() that would throw a one time warning and bail out for such skbs and ii) refuse and return early with an error in those BPF helpers that are affected. We do need to bail out as early as possible from there before any changes on the skb have been performed.

Quoted 5 lines
Fixes 6578171a7ff0 ("bpf: add bpf_skb_change_proto helper")
Cc Marcelo Ricardo Leitner <[email protected]>

I've looked over your changes and they all look good to me.

Quoted 9 lines

include/linux/skbuff.h static inline bool skb_is_gso_v6(const struct sk_buff *skb) · line 4046

Quoted 6 lines
include/linux/skbuff.h +6 -0
+/* Note: Should be called only if skb_is_gso(skb) is true */
+static inline bool skb_is_gso_sctp(const struct sk_buff *skb)
+{
+	return skb_shinfo(skb)->gso_type & SKB_GSO_SCTP;
+}
+

This helper is a fantastic idea and I will send a docs update to highlight it.

Sounds good. There are infact several places in the code that could make use of this right away. If you have a chance, this could be done in net-next along with the doc update or so.

Thanks,
Daniel

#3 lore raw
Quoted 32 lines · Daniel Borkmann
Quoted 21 lines · Daniel Axtens
Quoted 13 lines

From: Daniel Axtens <[email protected]>

SCTP GSO skbs have a gso_size of GSO_BY_FRAGS, so any sort of unconditionally mangling of that will result in nonsense value and would corrupt the skb later on.

Therefore, i) add two helpers skb_increase_gso_size() and skb_decrease_gso_size() that would throw a one time warning and bail out for such skbs and ii) refuse and return early with an error in those BPF helpers that are affected. We do need to bail out as early as possible from there before any changes on the skb have been performed.

Quoted 5 lines
Fixes 6578171a7ff0 ("bpf: add bpf_skb_change_proto helper")
Cc Marcelo Ricardo Leitner <[email protected]>

I've looked over your changes and they all look good to me.

Quoted 9 lines

include/linux/skbuff.h static inline bool skb_is_gso_v6(const struct sk_buff *skb) · line 4046

Quoted 6 lines
include/linux/skbuff.h +6 -0
+/* Note: Should be called only if skb_is_gso(skb) is true */
+static inline bool skb_is_gso_sctp(const struct sk_buff *skb)
+{
+	return skb_shinfo(skb)->gso_type & SKB_GSO_SCTP;
+}
+

This helper is a fantastic idea and I will send a docs update to highlight it.

Quoted 3 lines

Sounds good. There are infact several places in the code that could make use of this right away. If you have a chance, this could be done in net-next along with the doc update or so.

Applied to bpf tree, Thanks everyone.