Skip to contentSkip to conversation

geneve: fix Use-After-Free in RX path

PATCH In review Jamal Hadi Salim 2 participants 7 messages May 21 – May 27

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 geneve: fix Use-After-Free in RX path

Context: This commit fixes a UAF in Geneve. The UAF component was discovered because it is triggered by a buggy 'pedit' action[1]. the buggy pedit uses skb_store_bits() and can write into shared or non-linear data without copying the entire skb head. The Geneve device invoking either pskb_may_pull() or iptunnel_pull_header() is much more likely to reallocate the skb head, freeing the memory pointed to by geneve's stale geneveh pointer.

Note, although the patch looks big it's mostly re-arranging functions to avoid forward declaration.

Sample UAF below:
....

BUG: KASAN: slab-use-after-free in geneve_udp_encap_recv+0x2e8c/0x3190
Read of size 1 at addr ffff88811bab3c08 by task kworker/u32:1/62

CPU: 4 UID: 0 PID: 62 Comm: kworker/u32:1 Not tainted 7.1.0-rc3-00182-g0e3e91779b94 #551 PREEMPT(full)
Workqueue: ipv6_addrconf addrconf_dad_work
Call Trace:
 <IRQ>
 dump_stack_lvl+0x8c/0xd0
 print_report+0xce/0x5e0
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? kasan_complete_mode_report_info+0x80/0x200
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 kasan_report+0xd6/0x110
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 __asan_report_load1_noabort+0x18/0x20
 geneve_udp_encap_recv+0x2e8c/0x3190
 ? get_stack_info+0x38/0xc0
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __pfx_geneve_udp_encap_recv+0x10/0x10
 ? udp4_lib_lookup2+0x31c/0x700
 ? __pfx_udp_ehashfn+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __xfrm_policy_check2.constprop.0+0x40/0x5b0
 udp_queue_rcv_one_skb+0x65e/0xd20
 udp_queue_rcv_skb+0x142/0xb00
 udp_unicast_rcv_skb+0xd3/0x2c0
 udp_rcv+0x198f/0x2570
 ? srso_alias_return_thunk+0x5/0xfbef5
 ip_protocol_deliver_rcu+0x1f8/0x3c0
 ip_local_deliver_finish+0x362/0x5d0

....

[1]https://lore.kernel.org/netdev/[email protected]/

Fixes fd0dd796576e ("geneve: use GRO hint option in the RX path")

---
v1->v2 Address gemini-sashiko.
1. local variable "len" initialization in geneve_post_decap_hint() Although doesnt contribute to the fix, it was introduced in fd0dd796576e
2. resolve out-of-bounds transport header for non-GSO packets

---
 drivers/net/geneve.c   | 174 ++++++++++++++++++++++-------------------
 include/linux/skbuff.h |   5 ++
 2 files changed, 98 insertions(+), 81 deletions(-)
drivers/net/geneve.c +93 -81
index c6563367d382..dc5f5394836a 100644
@@ -256,6 +256,79 @@ static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
 	return NULL;
 }
 
+/*
+ * Look for GRO hint in the genenve options; if not found or does not pass basic
+ * sanitization return 0, otherwise the offset WRT the geneve hdr start.
+ */
+static unsigned int
+geneve_opt_gro_hint_off(const struct genevehdr *gh, __be16 *type,
+			unsigned int *gh_len)
+{
+	struct geneve_opt *opt = (void *)(gh + 1);
+	unsigned int id, opt_len = gh->opt_len;
+	struct geneve_opt_gro_hint *gro_hint;
+
+	while (opt_len >= (GENEVE_OPT_GRO_HINT_SIZE >> 2)) {
+		if (opt->opt_class == htons(GENEVE_OPT_NETDEV_CLASS) &&
+		    opt->type == GENEVE_OPT_GRO_HINT_TYPE &&
+		    opt->length == GENEVE_OPT_GRO_HINT_LEN)
+			goto found;
+
+		/* check for bad opt len */
+		if (opt->length + 1 >= opt_len)
+			return 0;
+
+		/* next opt */
+		opt_len -= opt->length + 1;
+		opt = ((void *)opt) + ((opt->length + 1) << 2);
+	}
+	return 0;
+
+found:
+	gro_hint = (struct geneve_opt_gro_hint *)opt->opt_data;
+
+	/*
+	 * Sanitize the hinted hdrs: the nested transport is UDP and must fit
+	 * the overall hinted hdr size.
+	 */
+	if (gro_hint->nested_tp_offset + sizeof(struct udphdr) >
+	    gro_hint->nested_hdr_len)
+		return 0;
+
+	if (gro_hint->nested_nh_offset +
+	    (gro_hint->nested_is_v6 ? sizeof(struct ipv6hdr) :
+				      sizeof(struct iphdr)) >
+	    gro_hint->nested_tp_offset)
+		return 0;
+
+	/* Allow only supported L2. */
+	id = gro_hint->inner_proto_id;
+	if (id >= ARRAY_SIZE(proto_id_map))
+		return 0;
+
+	*type = proto_id_map[id];
+	*gh_len += gro_hint->nested_hdr_len;
+
+	return (void *)gro_hint - (void *)gh;
+}
+
+static const struct geneve_opt_gro_hint *
+geneve_opt_gro_hint(const struct genevehdr *gh, unsigned int hint_off)
+{
+	return (const struct geneve_opt_gro_hint *)((void *)gh + hint_off);
+}
+
+static unsigned int
+geneve_sk_gro_hint_off(const struct sock *sk, const struct genevehdr *gh,
+		       __be16 *type, unsigned int *gh_len)
+{
+	const struct geneve_sock *gs = rcu_dereference_sk_user_data(sk);
+
+	if (!gs || !gs->gro_hint)
+		return 0;
+	return geneve_opt_gro_hint_off(gh, type, gh_len);
+}
+
 /* geneve receive/decap routine */
 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
 		      struct sk_buff *skb, const struct genevehdr *gnvh)
@@ -299,6 +372,21 @@ static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
 	if (tun_dst)
 		skb_dst_set(skb, &tun_dst->dst);
 
+	if (skb->encapsulation) {
+		unsigned int len = 0, hint_off;
+		__be16 p;
+
+		hint_off = geneve_sk_gro_hint_off(gs->sock->sk, gnvh, &p, &len);
+		if (hint_off && len <= skb_headlen(skb)) {
+			const struct geneve_opt_gro_hint *gro_hint;
+
+			gro_hint = geneve_opt_gro_hint(gnvh, hint_off);
+			skb_set_transport_header(skb, gro_hint->nested_tp_offset);
+		}
+	} else {
+		skb_unset_transport_header(skb);
+	}
+
 	if (gnvh->proto_type == htons(ETH_P_TEB)) {
 		skb_reset_mac_header(skb);
 		skb->protocol = eth_type_trans(skb, geneve->dev);
@@ -407,79 +495,6 @@ static int geneve_hlen(const struct genevehdr *gh)
 	return sizeof(*gh) + gh->opt_len * 4;
 }
 
-/*
- * Look for GRO hint in the genenve options; if not found or does not pass basic
- * sanitization return 0, otherwise the offset WRT the geneve hdr start.
- */
-static unsigned int
-geneve_opt_gro_hint_off(const struct genevehdr *gh, __be16 *type,
-			unsigned int *gh_len)
-{
-	struct geneve_opt *opt = (void *)(gh + 1);
-	unsigned int id, opt_len = gh->opt_len;
-	struct geneve_opt_gro_hint *gro_hint;
-
-	while (opt_len >= (GENEVE_OPT_GRO_HINT_SIZE >> 2)) {
-		if (opt->opt_class == htons(GENEVE_OPT_NETDEV_CLASS) &&
-		    opt->type == GENEVE_OPT_GRO_HINT_TYPE &&
-		    opt->length == GENEVE_OPT_GRO_HINT_LEN)
-			goto found;
-
-		/* check for bad opt len */
-		if (opt->length + 1 >= opt_len)
-			return 0;
-
-		/* next opt */
-		opt_len -= opt->length + 1;
-		opt = ((void *)opt) + ((opt->length + 1) << 2);
-	}
-	return 0;
-
-found:
-	gro_hint = (struct geneve_opt_gro_hint *)opt->opt_data;
-
-	/*
-	 * Sanitize the hinted hdrs: the nested transport is UDP and must fit
-	 * the overall hinted hdr size.
-	 */
-	if (gro_hint->nested_tp_offset + sizeof(struct udphdr) >
-	    gro_hint->nested_hdr_len)
-		return 0;
-
-	if (gro_hint->nested_nh_offset +
-	    (gro_hint->nested_is_v6 ? sizeof(struct ipv6hdr) :
-				      sizeof(struct iphdr)) >
-	    gro_hint->nested_tp_offset)
-		return 0;
-
-	/* Allow only supported L2. */
-	id = gro_hint->inner_proto_id;
-	if (id >= ARRAY_SIZE(proto_id_map))
-		return 0;
-
-	*type = proto_id_map[id];
-	*gh_len += gro_hint->nested_hdr_len;
-
-	return (void *)gro_hint - (void *)gh;
-}
-
-static const struct geneve_opt_gro_hint *
-geneve_opt_gro_hint(const struct genevehdr *gh, unsigned int hint_off)
-{
-	return (const struct geneve_opt_gro_hint *)((void *)gh + hint_off);
-}
-
-static unsigned int
-geneve_sk_gro_hint_off(const struct sock *sk, const struct genevehdr *gh,
-		       __be16 *type, unsigned int *gh_len)
-{
-	const struct geneve_sock *gs = rcu_dereference_sk_user_data(sk);
-
-	if (!gs || !gs->gro_hint)
-		return 0;
-	return geneve_opt_gro_hint_off(gh, type, gh_len);
-}
-
 /* Validate the packet headers pointed by data WRT the provided hint */
 static bool
 geneve_opt_gro_hint_validate(void *data,
@@ -571,7 +586,7 @@ static int geneve_post_decap_hint(const struct sock *sk, struct sk_buff *skb,
 				  struct genevehdr **geneveh)
 {
 	const struct geneve_opt_gro_hint *gro_hint;
-	unsigned int len, total_len, hint_off;
+	unsigned int len = 0, total_len, hint_off;
 	struct ipv6hdr *ipv6h;
 	struct iphdr *iph;
 	struct udphdr *uh;
@@ -585,10 +600,10 @@ static int geneve_post_decap_hint(const struct sock *sk, struct sk_buff *skb,
 		return 0;
 
 	gro_hint = geneve_opt_gro_hint(*geneveh, hint_off);
-	if (unlikely(!pskb_may_pull(skb, gro_hint->nested_hdr_len)))
+	if (unlikely(skb_ensure_writable(skb, gro_hint->nested_hdr_len)))
 		return -ENOMEM;
 
-	*geneveh = geneve_hdr(skb);
+	*geneveh = (struct genevehdr *)(skb->data - gh_len);
 	gro_hint = geneve_opt_gro_hint(*geneveh, hint_off);
 
 	/*
@@ -612,9 +627,6 @@ static int geneve_post_decap_hint(const struct sock *sk, struct sk_buff *skb,
 	 */
 	skb->encapsulation = 1;
 
-	/* GSO expect a valid transpor header, move it to the current one. */
-	skb_set_transport_header(skb, gro_hint->nested_tp_offset);
-
 	/* Adjust the nested IP{6} hdr to actual GSO len. */
 	if (gro_hint->nested_is_v6) {
 		ipv6h->payload_len = htons(total_len - sizeof(*ipv6h));
@@ -629,7 +641,7 @@ static int geneve_post_decap_hint(const struct sock *sk, struct sk_buff *skb,
 	}
 
 	/* Adjust the nested UDP header len and checksum. */
-	uh = udp_hdr(skb);
+	uh = (struct udphdr *)(skb->data + gro_hint->nested_tp_offset);
 	uh->len = htons(skb->len - gro_hint->nested_tp_offset);
 	if (uh->check) {
 		len = skb->len - gro_hint->nested_nh_offset;
@@ -691,7 +703,7 @@ static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
 	 * After hint processing, the transport header points to the inner one
 	 * and we can't use anymore on geneve_hdr().
 	 */
-	geneveh = geneve_hdr(skb);
+	geneveh = (struct genevehdr *)(skb->data - (sizeof(struct genevehdr) + opts_len));
 	if (geneve_post_decap_hint(sk, skb, sizeof(struct genevehdr) +
 				   opts_len, &geneveh)) {
 		DEV_STATS_INC(geneve->dev, rx_errors);
include/linux/skbuff.h +5 -0
index 2bcf78a4de7b..f535838cff68 100644
@@ -3125,6 +3125,11 @@ static inline void skb_set_transport_header(struct sk_buff *skb,
 	skb->transport_header += offset;
 }
 
+static inline void skb_unset_transport_header(struct sk_buff *skb)
+{
+	skb->transport_header = (typeof(skb->transport_header))~0U;
+}
+
 static inline unsigned char *skb_network_header(const struct sk_buff *skb)
 {
 	return skb->head + skb->network_header;
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 152 lines · Jamal Hadi Salim

Context: This commit fixes a UAF in Geneve. The UAF component was discovered because it is triggered by a buggy 'pedit' action[1]. the buggy pedit uses skb_store_bits() and can write into shared or non-linear data without copying the entire skb head. The Geneve device invoking either pskb_may_pull() or iptunnel_pull_header() is much more likely to reallocate the skb head, freeing the memory pointed to by geneve's stale geneveh pointer.

Note, although the patch looks big it's mostly re-arranging functions to avoid forward declaration.

Sample UAF below:
....

BUG: KASAN: slab-use-after-free in geneve_udp_encap_recv+0x2e8c/0x3190
Read of size 1 at addr ffff88811bab3c08 by task kworker/u32:1/62

CPU: 4 UID: 0 PID: 62 Comm: kworker/u32:1 Not tainted 7.1.0-rc3-00182-g0e3e91779b94 #551 PREEMPT(full)
Workqueue: ipv6_addrconf addrconf_dad_work
Call Trace:
 <IRQ>
 dump_stack_lvl+0x8c/0xd0
 print_report+0xce/0x5e0
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? kasan_complete_mode_report_info+0x80/0x200
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 kasan_report+0xd6/0x110
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 __asan_report_load1_noabort+0x18/0x20
 geneve_udp_encap_recv+0x2e8c/0x3190
 ? get_stack_info+0x38/0xc0
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __pfx_geneve_udp_encap_recv+0x10/0x10
 ? udp4_lib_lookup2+0x31c/0x700
 ? __pfx_udp_ehashfn+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __xfrm_policy_check2.constprop.0+0x40/0x5b0
 udp_queue_rcv_one_skb+0x65e/0xd20
 udp_queue_rcv_skb+0x142/0xb00
 udp_unicast_rcv_skb+0xd3/0x2c0
 udp_rcv+0x198f/0x2570
 ? srso_alias_return_thunk+0x5/0xfbef5
 ip_protocol_deliver_rcu+0x1f8/0x3c0
 ip_local_deliver_finish+0x362/0x5d0

....

[1]https://lore.kernel.org/netdev/[email protected]/

Fixes fd0dd796576e ("geneve: use GRO hint option in the RX path")

---
v1->v2 Address gemini-sashiko.
1. local variable "len" initialization in geneve_post_decap_hint() Although doesnt contribute to the fix, it was introduced in fd0dd796576e
2. resolve out-of-bounds transport header for non-GSO packets

---
 drivers/net/geneve.c   | 174 ++++++++++++++++++++++-------------------
 include/linux/skbuff.h |   5 ++
 2 files changed, 98 insertions(+), 81 deletions(-)
drivers/net/geneve.c +77 -0
index c6563367d382..dc5f5394836a 100644
@@ -256,6 +256,79 @@ static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
 	return NULL;
 }
 
+/*
+ * Look for GRO hint in the genenve options; if not found or does not pass basic
+ * sanitization return 0, otherwise the offset WRT the geneve hdr start.
+ */
+static unsigned int
+geneve_opt_gro_hint_off(const struct genevehdr *gh, __be16 *type,
+			unsigned int *gh_len)
+{
+	struct geneve_opt *opt = (void *)(gh + 1);
+	unsigned int id, opt_len = gh->opt_len;
+	struct geneve_opt_gro_hint *gro_hint;
+
+	while (opt_len >= (GENEVE_OPT_GRO_HINT_SIZE >> 2)) {
+		if (opt->opt_class == htons(GENEVE_OPT_NETDEV_CLASS) &&
+		    opt->type == GENEVE_OPT_GRO_HINT_TYPE &&
+		    opt->length == GENEVE_OPT_GRO_HINT_LEN)
+			goto found;
+
+		/* check for bad opt len */
+		if (opt->length + 1 >= opt_len)
+			return 0;
+
+		/* next opt */
+		opt_len -= opt->length + 1;
+		opt = ((void *)opt) + ((opt->length + 1) << 2);
+	}
+	return 0;
+
+found:
+	gro_hint = (struct geneve_opt_gro_hint *)opt->opt_data;
+
+	/*
+	 * Sanitize the hinted hdrs: the nested transport is UDP and must fit
+	 * the overall hinted hdr size.
+	 */
+	if (gro_hint->nested_tp_offset + sizeof(struct udphdr) >
+	    gro_hint->nested_hdr_len)
+		return 0;
+
+	if (gro_hint->nested_nh_offset +
+	    (gro_hint->nested_is_v6 ? sizeof(struct ipv6hdr) :
+				      sizeof(struct iphdr)) >
+	    gro_hint->nested_tp_offset)
+		return 0;
+
+	/* Allow only supported L2. */
+	id = gro_hint->inner_proto_id;
+	if (id >= ARRAY_SIZE(proto_id_map))
+		return 0;
+
+	*type = proto_id_map[id];
+	*gh_len += gro_hint->nested_hdr_len;
+
+	return (void *)gro_hint - (void *)gh;
+}
+
+static const struct geneve_opt_gro_hint *
+geneve_opt_gro_hint(const struct genevehdr *gh, unsigned int hint_off)
+{
+	return (const struct geneve_opt_gro_hint *)((void *)gh + hint_off);
+}
+
+static unsigned int
+geneve_sk_gro_hint_off(const struct sock *sk, const struct genevehdr *gh,
+		       __be16 *type, unsigned int *gh_len)
+{
+	const struct geneve_sock *gs = rcu_dereference_sk_user_data(sk);
+
+	if (!gs || !gs->gro_hint)
+		return 0;
+	return geneve_opt_gro_hint_off(gh, type, gh_len);
+}
+
 /* geneve receive/decap routine */
 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
 		      struct sk_buff *skb, const struct genevehdr *gnvh)
@@ -299,6 +372,21 @@ static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
 	if (tun_dst)
 		skb_dst_set(skb, &tun_dst->dst);
 
+	if (skb->encapsulation) {
+		unsigned int len = 0, hint_off;
+		__be16 p;
+

drivers/net/geneve.c static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, · line 388

Quoted 10 lines
drivers/net/geneve.c +10 -0
+		hint_off = geneve_sk_gro_hint_off(gs->sock->sk, gnvh, &p, &len);
+		if (hint_off && len <= skb_headlen(skb)) {
+			const struct geneve_opt_gro_hint *gro_hint;
+
+			gro_hint = geneve_opt_gro_hint(gnvh, hint_off);
+			skb_set_transport_header(skb, gro_hint->nested_tp_offset);
+		}
+	} else {
+		skb_unset_transport_header(skb);
+	}

It's not clear to me why the above chunk is needed. Why can't geneve_post_decap_hint() retain the transport header set in the current place? it would make this patch much shorter.

If the above is really needed, I think it would be better to move the code around in a separate, pre-req, patch.

Thanks,

Paolo

#2 lore raw
Quoted 152 lines · Paolo Abeni
Quoted 148 lines · Jamal Hadi Salim

Context: This commit fixes a UAF in Geneve. The UAF component was discovered because it is triggered by a buggy 'pedit' action[1]. the buggy pedit uses skb_store_bits() and can write into shared or non-linear data without copying the entire skb head. The Geneve device invoking either pskb_may_pull() or iptunnel_pull_header() is much more likely to reallocate the skb head, freeing the memory pointed to by geneve's stale geneveh pointer.

Note, although the patch looks big it's mostly re-arranging functions to avoid forward declaration.

Sample UAF below:
....

BUG: KASAN: slab-use-after-free in geneve_udp_encap_recv+0x2e8c/0x3190
Read of size 1 at addr ffff88811bab3c08 by task kworker/u32:1/62

CPU: 4 UID: 0 PID: 62 Comm: kworker/u32:1 Not tainted 7.1.0-rc3-00182-g0e3e91779b94 #551 PREEMPT(full)
Workqueue: ipv6_addrconf addrconf_dad_work
Call Trace:
 <IRQ>
 dump_stack_lvl+0x8c/0xd0
 print_report+0xce/0x5e0
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? kasan_complete_mode_report_info+0x80/0x200
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 kasan_report+0xd6/0x110
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 __asan_report_load1_noabort+0x18/0x20
 geneve_udp_encap_recv+0x2e8c/0x3190
 ? get_stack_info+0x38/0xc0
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __pfx_geneve_udp_encap_recv+0x10/0x10
 ? udp4_lib_lookup2+0x31c/0x700
 ? __pfx_udp_ehashfn+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __xfrm_policy_check2.constprop.0+0x40/0x5b0
 udp_queue_rcv_one_skb+0x65e/0xd20
 udp_queue_rcv_skb+0x142/0xb00
 udp_unicast_rcv_skb+0xd3/0x2c0
 udp_rcv+0x198f/0x2570
 ? srso_alias_return_thunk+0x5/0xfbef5
 ip_protocol_deliver_rcu+0x1f8/0x3c0
 ip_local_deliver_finish+0x362/0x5d0

....

[1]https://lore.kernel.org/netdev/[email protected]/

Fixes fd0dd796576e ("geneve: use GRO hint option in the RX path")

---
v1->v2 Address gemini-sashiko.
1. local variable "len" initialization in geneve_post_decap_hint() Although doesnt contribute to the fix, it was introduced in fd0dd796576e
2. resolve out-of-bounds transport header for non-GSO packets

---
 drivers/net/geneve.c   | 174 ++++++++++++++++++++++-------------------
 include/linux/skbuff.h |   5 ++
 2 files changed, 98 insertions(+), 81 deletions(-)
drivers/net/geneve.c +73 -0
index c6563367d382..dc5f5394836a 100644
@@ -256,6 +256,79 @@ static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
      return NULL;
 }
 
+/*
+ * Look for GRO hint in the genenve options; if not found or does not pass basic
+ * sanitization return 0, otherwise the offset WRT the geneve hdr start.
+ */
+static unsigned int
+geneve_opt_gro_hint_off(const struct genevehdr *gh, __be16 *type,
+                     unsigned int *gh_len)
+{
+     struct geneve_opt *opt = (void *)(gh + 1);
+     unsigned int id, opt_len = gh->opt_len;
+     struct geneve_opt_gro_hint *gro_hint;
+
+     while (opt_len >= (GENEVE_OPT_GRO_HINT_SIZE >> 2)) {
+             if (opt->opt_class == htons(GENEVE_OPT_NETDEV_CLASS) &&
+                 opt->type == GENEVE_OPT_GRO_HINT_TYPE &&
+                 opt->length == GENEVE_OPT_GRO_HINT_LEN)
+                     goto found;
+
+             /* check for bad opt len */
+             if (opt->length + 1 >= opt_len)
+                     return 0;
+
+             /* next opt */
+             opt_len -= opt->length + 1;
+             opt = ((void *)opt) + ((opt->length + 1) << 2);
+     }
+     return 0;
+
+found:
+     gro_hint = (struct geneve_opt_gro_hint *)opt->opt_data;
+
+     /*
+      * Sanitize the hinted hdrs: the nested transport is UDP and must fit
+      * the overall hinted hdr size.
+      */
+     if (gro_hint->nested_tp_offset + sizeof(struct udphdr) >
+         gro_hint->nested_hdr_len)
+             return 0;
+
+     if (gro_hint->nested_nh_offset +
+         (gro_hint->nested_is_v6 ? sizeof(struct ipv6hdr) :
+                                   sizeof(struct iphdr)) >
+         gro_hint->nested_tp_offset)
+             return 0;
+
+     /* Allow only supported L2. */
+     id = gro_hint->inner_proto_id;
+     if (id >= ARRAY_SIZE(proto_id_map))
+             return 0;
+
+     *type = proto_id_map[id];
+     *gh_len += gro_hint->nested_hdr_len;
+
+     return (void *)gro_hint - (void *)gh;
+}
+
+static const struct geneve_opt_gro_hint *
+geneve_opt_gro_hint(const struct genevehdr *gh, unsigned int hint_off)
+{
+     return (const struct geneve_opt_gro_hint *)((void *)gh + hint_off);
+}
+
+static unsigned int
+geneve_sk_gro_hint_off(const struct sock *sk, const struct genevehdr *gh,
+                    __be16 *type, unsigned int *gh_len)
+{
+     const struct geneve_sock *gs = rcu_dereference_sk_user_data(sk);
+
+     if (!gs || !gs->gro_hint)
+             return 0;
+     return geneve_opt_gro_hint_off(gh, type, gh_len);
+}
+
 /* geneve receive/decap routine */
 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
                    struct sk_buff *skb, const struct genevehdr *gnvh)
@@ -299,6 +372,21 @@ static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
      if (tun_dst)
              skb_dst_set(skb, &tun_dst->dst);
 
Quoted 16 lines
Quoted 4 lines
drivers/net/geneve.c +4 -0
+     if (skb->encapsulation) {
+             unsigned int len = 0, hint_off;
+             __be16 p;
+

drivers/net/geneve.c static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, · line 388

Quoted 10 lines
drivers/net/geneve.c +10 -0
+             hint_off = geneve_sk_gro_hint_off(gs->sock->sk, gnvh, &p, &len);
+             if (hint_off && len <= skb_headlen(skb)) {
+                     const struct geneve_opt_gro_hint *gro_hint;
+
+                     gro_hint = geneve_opt_gro_hint(gnvh, hint_off);
+                     skb_set_transport_header(skb, gro_hint->nested_tp_offset);
+             }
+     } else {
+             skb_unset_transport_header(skb);
+     }

It's not clear to me why the above chunk is needed.

I think you understand this so i may be stating the obvious:
The main motivation was to isolate the pulls from making the transport header stale
IOW, geneve_hdr(skb) pointer could become "stale" if the packet's head was reallocated (because geneve_rx() is called after all the "pull" operations) - so this ensures that the inner transport header is only set after all header pulling is done - otherwise we get a UAF.

Quoted 4 lines

Why can't
geneve_post_decap_hint() retain the transport header set in the current place? it would make this patch much shorter.

It was more to ensure clean state for cases where we dont have GRO hints. The "else {skb_unset_transport_header(skb);} " handles the case where we are missing GRO hints - at that point outer UDP header is stripped but tranport header is still pointing to where this outer header used to be and something down the road may misintepret the header.

Quoted 3 lines

If the above is really needed, I think it would be better to move the code around in a separate, pre-req, patch.

Yes, that would be more readable. So if i understood you correctly - break into:
patch 1: move GRO hint functions to avoid forward declarations patch 2: Fix the UAF

cheers,
jamal

maintainer
#3 lore raw
Quoted 167 lines · Jamal Hadi Salim
Quoted 149 lines · Paolo Abeni
Quoted 148 lines · Jamal Hadi Salim

Context: This commit fixes a UAF in Geneve. The UAF component was discovered because it is triggered by a buggy 'pedit' action[1]. the buggy pedit uses skb_store_bits() and can write into shared or non-linear data without copying the entire skb head. The Geneve device invoking either pskb_may_pull() or iptunnel_pull_header() is much more likely to reallocate the skb head, freeing the memory pointed to by geneve's stale geneveh pointer.

Note, although the patch looks big it's mostly re-arranging functions to avoid forward declaration.

Sample UAF below:
....

BUG: KASAN: slab-use-after-free in geneve_udp_encap_recv+0x2e8c/0x3190
Read of size 1 at addr ffff88811bab3c08 by task kworker/u32:1/62

CPU: 4 UID: 0 PID: 62 Comm: kworker/u32:1 Not tainted 7.1.0-rc3-00182-g0e3e91779b94 #551 PREEMPT(full)
Workqueue: ipv6_addrconf addrconf_dad_work
Call Trace:
 <IRQ>
 dump_stack_lvl+0x8c/0xd0
 print_report+0xce/0x5e0
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? kasan_complete_mode_report_info+0x80/0x200
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 kasan_report+0xd6/0x110
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 __asan_report_load1_noabort+0x18/0x20
 geneve_udp_encap_recv+0x2e8c/0x3190
 ? get_stack_info+0x38/0xc0
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __pfx_geneve_udp_encap_recv+0x10/0x10
 ? udp4_lib_lookup2+0x31c/0x700
 ? __pfx_udp_ehashfn+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __xfrm_policy_check2.constprop.0+0x40/0x5b0
 udp_queue_rcv_one_skb+0x65e/0xd20
 udp_queue_rcv_skb+0x142/0xb00
 udp_unicast_rcv_skb+0xd3/0x2c0
 udp_rcv+0x198f/0x2570
 ? srso_alias_return_thunk+0x5/0xfbef5
 ip_protocol_deliver_rcu+0x1f8/0x3c0
 ip_local_deliver_finish+0x362/0x5d0

....

[1]https://lore.kernel.org/netdev/[email protected]/

Fixes fd0dd796576e ("geneve: use GRO hint option in the RX path")

---
v1->v2 Address gemini-sashiko.
1. local variable "len" initialization in geneve_post_decap_hint() Although doesnt contribute to the fix, it was introduced in fd0dd796576e
2. resolve out-of-bounds transport header for non-GSO packets

---
 drivers/net/geneve.c   | 174 ++++++++++++++++++++++-------------------
 include/linux/skbuff.h |   5 ++
 2 files changed, 98 insertions(+), 81 deletions(-)
drivers/net/geneve.c +73 -0
index c6563367d382..dc5f5394836a 100644
@@ -256,6 +256,79 @@ static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
      return NULL;
 }
 
+/*
+ * Look for GRO hint in the genenve options; if not found or does not pass basic
+ * sanitization return 0, otherwise the offset WRT the geneve hdr start.
+ */
+static unsigned int
+geneve_opt_gro_hint_off(const struct genevehdr *gh, __be16 *type,
+                     unsigned int *gh_len)
+{
+     struct geneve_opt *opt = (void *)(gh + 1);
+     unsigned int id, opt_len = gh->opt_len;
+     struct geneve_opt_gro_hint *gro_hint;
+
+     while (opt_len >= (GENEVE_OPT_GRO_HINT_SIZE >> 2)) {
+             if (opt->opt_class == htons(GENEVE_OPT_NETDEV_CLASS) &&
+                 opt->type == GENEVE_OPT_GRO_HINT_TYPE &&
+                 opt->length == GENEVE_OPT_GRO_HINT_LEN)
+                     goto found;
+
+             /* check for bad opt len */
+             if (opt->length + 1 >= opt_len)
+                     return 0;
+
+             /* next opt */
+             opt_len -= opt->length + 1;
+             opt = ((void *)opt) + ((opt->length + 1) << 2);
+     }
+     return 0;
+
+found:
+     gro_hint = (struct geneve_opt_gro_hint *)opt->opt_data;
+
+     /*
+      * Sanitize the hinted hdrs: the nested transport is UDP and must fit
+      * the overall hinted hdr size.
+      */
+     if (gro_hint->nested_tp_offset + sizeof(struct udphdr) >
+         gro_hint->nested_hdr_len)
+             return 0;
+
+     if (gro_hint->nested_nh_offset +
+         (gro_hint->nested_is_v6 ? sizeof(struct ipv6hdr) :
+                                   sizeof(struct iphdr)) >
+         gro_hint->nested_tp_offset)
+             return 0;
+
+     /* Allow only supported L2. */
+     id = gro_hint->inner_proto_id;
+     if (id >= ARRAY_SIZE(proto_id_map))
+             return 0;
+
+     *type = proto_id_map[id];
+     *gh_len += gro_hint->nested_hdr_len;
+
+     return (void *)gro_hint - (void *)gh;
+}
+
+static const struct geneve_opt_gro_hint *
+geneve_opt_gro_hint(const struct genevehdr *gh, unsigned int hint_off)
+{
+     return (const struct geneve_opt_gro_hint *)((void *)gh + hint_off);
+}
+
+static unsigned int
+geneve_sk_gro_hint_off(const struct sock *sk, const struct genevehdr *gh,
+                    __be16 *type, unsigned int *gh_len)
+{
+     const struct geneve_sock *gs = rcu_dereference_sk_user_data(sk);
+
+     if (!gs || !gs->gro_hint)
+             return 0;
+     return geneve_opt_gro_hint_off(gh, type, gh_len);
+}
+
 /* geneve receive/decap routine */
 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
                    struct sk_buff *skb, const struct genevehdr *gnvh)
@@ -299,6 +372,21 @@ static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
      if (tun_dst)
              skb_dst_set(skb, &tun_dst->dst);
 
Quoted 16 lines
Quoted 4 lines
drivers/net/geneve.c +4 -0
+     if (skb->encapsulation) {
+             unsigned int len = 0, hint_off;
+             __be16 p;
+

drivers/net/geneve.c static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, · line 388

Quoted 10 lines
drivers/net/geneve.c +10 -0
+             hint_off = geneve_sk_gro_hint_off(gs->sock->sk, gnvh, &p, &len);
+             if (hint_off && len <= skb_headlen(skb)) {
+                     const struct geneve_opt_gro_hint *gro_hint;
+
+                     gro_hint = geneve_opt_gro_hint(gnvh, hint_off);
+                     skb_set_transport_header(skb, gro_hint->nested_tp_offset);
+             }
+     } else {
+             skb_unset_transport_header(skb);
+     }

It's not clear to me why the above chunk is needed.

Quoted 7 lines

I think you understand this so i may be stating the obvious:
The main motivation was to isolate the pulls from making the transport header stale
IOW, geneve_hdr(skb) pointer could become "stale" if the packet's head was reallocated (because geneve_rx() is called after all the "pull" operations) - so this ensures that the inner transport header is only set after all header pulling is done - otherwise we get a UAF.

Uhmm... my questions sparked because this patch adds quite a bit of new conditionals in the data-path, which I would like to avoid them.

But I must admit that re-reading the code - I'm likely low on coffee - I don't see the UaF. This sequence:

	pskb_may_pull(skb...)
	geneveh = geneve_hdr(skb);

is correct/does not yield to UaF, and AFAICS geneveh is always reloaded after pskb_may_pull()/iptunnel_pull_header(). Could you please share a decoded stack trace for the UaF you are observing? So it's clear where exactly kasan reports the problem.

Quoted 10 lines
Quoted 4 lines

Why can't
geneve_post_decap_hint() retain the transport header set in the current place? it would make this patch much shorter.

It was more to ensure clean state for cases where we dont have GRO hints. The "else {skb_unset_transport_header(skb);} " handles the case where we are missing GRO hints - at that point outer UDP header is stripped but tranport header is still pointing to where this outer header used to be and something down the road may misintepret the header.

This looks like an unrelated problem?!? And possibly not a problem at all?!? Upper layers (outside GSO) are expected to re-evaluate the transport header as needed.

/P

#4 lore raw
Quoted 183 lines · Paolo Abeni
Quoted 167 lines · Jamal Hadi Salim
Quoted 149 lines · Paolo Abeni
Quoted 148 lines · Jamal Hadi Salim

Context: This commit fixes a UAF in Geneve. The UAF component was discovered because it is triggered by a buggy 'pedit' action[1]. the buggy pedit uses skb_store_bits() and can write into shared or non-linear data without copying the entire skb head. The Geneve device invoking either pskb_may_pull() or iptunnel_pull_header() is much more likely to reallocate the skb head, freeing the memory pointed to by geneve's stale geneveh pointer.

Note, although the patch looks big it's mostly re-arranging functions to avoid forward declaration.

Sample UAF below:
....

BUG: KASAN: slab-use-after-free in geneve_udp_encap_recv+0x2e8c/0x3190
Read of size 1 at addr ffff88811bab3c08 by task kworker/u32:1/62

CPU: 4 UID: 0 PID: 62 Comm: kworker/u32:1 Not tainted 7.1.0-rc3-00182-g0e3e91779b94 #551 PREEMPT(full)
Workqueue: ipv6_addrconf addrconf_dad_work
Call Trace:
 <IRQ>
 dump_stack_lvl+0x8c/0xd0
 print_report+0xce/0x5e0
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? kasan_complete_mode_report_info+0x80/0x200
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 kasan_report+0xd6/0x110
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 __asan_report_load1_noabort+0x18/0x20
 geneve_udp_encap_recv+0x2e8c/0x3190
 ? get_stack_info+0x38/0xc0
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __pfx_geneve_udp_encap_recv+0x10/0x10
 ? udp4_lib_lookup2+0x31c/0x700
 ? __pfx_udp_ehashfn+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __xfrm_policy_check2.constprop.0+0x40/0x5b0
 udp_queue_rcv_one_skb+0x65e/0xd20
 udp_queue_rcv_skb+0x142/0xb00
 udp_unicast_rcv_skb+0xd3/0x2c0
 udp_rcv+0x198f/0x2570
 ? srso_alias_return_thunk+0x5/0xfbef5
 ip_protocol_deliver_rcu+0x1f8/0x3c0
 ip_local_deliver_finish+0x362/0x5d0

....

[1]https://lore.kernel.org/netdev/[email protected]/

Fixes fd0dd796576e ("geneve: use GRO hint option in the RX path")

---
v1->v2 Address gemini-sashiko.
1. local variable "len" initialization in geneve_post_decap_hint() Although doesnt contribute to the fix, it was introduced in fd0dd796576e
2. resolve out-of-bounds transport header for non-GSO packets

---
 drivers/net/geneve.c   | 174 ++++++++++++++++++++++-------------------
 include/linux/skbuff.h |   5 ++
 2 files changed, 98 insertions(+), 81 deletions(-)
drivers/net/geneve.c +73 -0
index c6563367d382..dc5f5394836a 100644
@@ -256,6 +256,79 @@ static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
      return NULL;
 }
 
+/*
+ * Look for GRO hint in the genenve options; if not found or does not pass basic
+ * sanitization return 0, otherwise the offset WRT the geneve hdr start.
+ */
+static unsigned int
+geneve_opt_gro_hint_off(const struct genevehdr *gh, __be16 *type,
+                     unsigned int *gh_len)
+{
+     struct geneve_opt *opt = (void *)(gh + 1);
+     unsigned int id, opt_len = gh->opt_len;
+     struct geneve_opt_gro_hint *gro_hint;
+
+     while (opt_len >= (GENEVE_OPT_GRO_HINT_SIZE >> 2)) {
+             if (opt->opt_class == htons(GENEVE_OPT_NETDEV_CLASS) &&
+                 opt->type == GENEVE_OPT_GRO_HINT_TYPE &&
+                 opt->length == GENEVE_OPT_GRO_HINT_LEN)
+                     goto found;
+
+             /* check for bad opt len */
+             if (opt->length + 1 >= opt_len)
+                     return 0;
+
+             /* next opt */
+             opt_len -= opt->length + 1;
+             opt = ((void *)opt) + ((opt->length + 1) << 2);
+     }
+     return 0;
+
+found:
+     gro_hint = (struct geneve_opt_gro_hint *)opt->opt_data;
+
+     /*
+      * Sanitize the hinted hdrs: the nested transport is UDP and must fit
+      * the overall hinted hdr size.
+      */
+     if (gro_hint->nested_tp_offset + sizeof(struct udphdr) >
+         gro_hint->nested_hdr_len)
+             return 0;
+
+     if (gro_hint->nested_nh_offset +
+         (gro_hint->nested_is_v6 ? sizeof(struct ipv6hdr) :
+                                   sizeof(struct iphdr)) >
+         gro_hint->nested_tp_offset)
+             return 0;
+
+     /* Allow only supported L2. */
+     id = gro_hint->inner_proto_id;
+     if (id >= ARRAY_SIZE(proto_id_map))
+             return 0;
+
+     *type = proto_id_map[id];
+     *gh_len += gro_hint->nested_hdr_len;
+
+     return (void *)gro_hint - (void *)gh;
+}
+
+static const struct geneve_opt_gro_hint *
+geneve_opt_gro_hint(const struct genevehdr *gh, unsigned int hint_off)
+{
+     return (const struct geneve_opt_gro_hint *)((void *)gh + hint_off);
+}
+
+static unsigned int
+geneve_sk_gro_hint_off(const struct sock *sk, const struct genevehdr *gh,
+                    __be16 *type, unsigned int *gh_len)
+{
+     const struct geneve_sock *gs = rcu_dereference_sk_user_data(sk);
+
+     if (!gs || !gs->gro_hint)
+             return 0;
+     return geneve_opt_gro_hint_off(gh, type, gh_len);
+}
+
 /* geneve receive/decap routine */
 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
                    struct sk_buff *skb, const struct genevehdr *gnvh)
@@ -299,6 +372,21 @@ static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
      if (tun_dst)
              skb_dst_set(skb, &tun_dst->dst);
 
Quoted 16 lines
Quoted 4 lines
drivers/net/geneve.c +4 -0
+     if (skb->encapsulation) {
+             unsigned int len = 0, hint_off;
+             __be16 p;
+

drivers/net/geneve.c static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, · line 388

Quoted 10 lines
drivers/net/geneve.c +10 -0
+             hint_off = geneve_sk_gro_hint_off(gs->sock->sk, gnvh, &p, &len);
+             if (hint_off && len <= skb_headlen(skb)) {
+                     const struct geneve_opt_gro_hint *gro_hint;
+
+                     gro_hint = geneve_opt_gro_hint(gnvh, hint_off);
+                     skb_set_transport_header(skb, gro_hint->nested_tp_offset);
+             }
+     } else {
+             skb_unset_transport_header(skb);
+     }

It's not clear to me why the above chunk is needed.

Quoted 7 lines

I think you understand this so i may be stating the obvious:
The main motivation was to isolate the pulls from making the transport header stale
IOW, geneve_hdr(skb) pointer could become "stale" if the packet's head was reallocated (because geneve_rx() is called after all the "pull" operations) - so this ensures that the inner transport header is only set after all header pulling is done - otherwise we get a UAF.

Uhmm... my questions sparked because this patch adds quite a bit of new conditionals in the data-path, which I would like to avoid them.

But I must admit that re-reading the code - I'm likely low on coffee - I don't see the UaF. This sequence:

Quoted 8 lines
        pskb_may_pull(skb...)
        geneveh = geneve_hdr(skb);

is correct/does not yield to UaF, and AFAICS geneveh is always reloaded after pskb_may_pull()/iptunnel_pull_header(). Could you please share a decoded stack trace for the UaF you are observing? So it's clear where exactly kasan reports the problem.

The only one i have is what is in the commit. Comments below..

Quoted 11 lines
Quoted 10 lines
Quoted 4 lines

Why can't
geneve_post_decap_hint() retain the transport header set in the current place? it would make this patch much shorter.

It was more to ensure clean state for cases where we dont have GRO hints. The "else {skb_unset_transport_header(skb);} " handles the case where we are missing GRO hints - at that point outer UDP header is stripped but tranport header is still pointing to where this outer header used to be and something down the road may misintepret the header.

Quoted 3 lines

This looks like an unrelated problem?!? And possibly not a problem at all?!? Upper layers (outside GSO) are expected to re-evaluate the transport header as needed.

Let me provide context, maybe that will help explain or provide a way to fix this better.

I received multiple patches (via security) to resolve the pedit issue; Rajat's was the first one.
Rajat's repro tricks geneve into having _negative transport offsets_. Does that trigger anything for you? geneve just trusts those headers are valid.
It was not Rajat's patch that caused this splat but one of the multiple other reported alternative patches. I would have to dig in to find which patch caused the splat.

Does this help? The key i believe is looking at the effect of negative transport headers.
I can send you the repro maybe you will be able to have a better approach.

The most interesting part:
This patch "fixes the pedit issue". IOW, if you apply this patch to geneve, _none_ of the repros can trigger the pedit issue. Of course that is not fixing the root cause which is pedit - but i figured it was worth posting because it does resolve the negative offset. TBH, I was hoping this fix would go in first as a plug to the pedit bug and also fix the negative transport offset. After adding yet another fix for the cloned skb frags and seeing that Han Guidong found a way to trigger using a different repro that creates it from a kernel module i just shrugged.

cheers,
jamal

maintainer
#5 lore raw
Quoted 221 lines · Jamal Hadi Salim
Quoted 182 lines · Paolo Abeni
Quoted 167 lines · Jamal Hadi Salim
Quoted 149 lines · Paolo Abeni
Quoted 148 lines · Jamal Hadi Salim

Context: This commit fixes a UAF in Geneve. The UAF component was discovered because it is triggered by a buggy 'pedit' action[1]. the buggy pedit uses skb_store_bits() and can write into shared or non-linear data without copying the entire skb head. The Geneve device invoking either pskb_may_pull() or iptunnel_pull_header() is much more likely to reallocate the skb head, freeing the memory pointed to by geneve's stale geneveh pointer.

Note, although the patch looks big it's mostly re-arranging functions to avoid forward declaration.

Sample UAF below:
....

BUG: KASAN: slab-use-after-free in geneve_udp_encap_recv+0x2e8c/0x3190
Read of size 1 at addr ffff88811bab3c08 by task kworker/u32:1/62

CPU: 4 UID: 0 PID: 62 Comm: kworker/u32:1 Not tainted 7.1.0-rc3-00182-g0e3e91779b94 #551 PREEMPT(full)
Workqueue: ipv6_addrconf addrconf_dad_work
Call Trace:
 <IRQ>
 dump_stack_lvl+0x8c/0xd0
 print_report+0xce/0x5e0
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? kasan_complete_mode_report_info+0x80/0x200
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 kasan_report+0xd6/0x110
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 __asan_report_load1_noabort+0x18/0x20
 geneve_udp_encap_recv+0x2e8c/0x3190
 ? get_stack_info+0x38/0xc0
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __pfx_geneve_udp_encap_recv+0x10/0x10
 ? udp4_lib_lookup2+0x31c/0x700
 ? __pfx_udp_ehashfn+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __xfrm_policy_check2.constprop.0+0x40/0x5b0
 udp_queue_rcv_one_skb+0x65e/0xd20
 udp_queue_rcv_skb+0x142/0xb00
 udp_unicast_rcv_skb+0xd3/0x2c0
 udp_rcv+0x198f/0x2570
 ? srso_alias_return_thunk+0x5/0xfbef5
 ip_protocol_deliver_rcu+0x1f8/0x3c0
 ip_local_deliver_finish+0x362/0x5d0

....

[1]https://lore.kernel.org/netdev/[email protected]/

Fixes fd0dd796576e ("geneve: use GRO hint option in the RX path")

---
v1->v2 Address gemini-sashiko.
1. local variable "len" initialization in geneve_post_decap_hint() Although doesnt contribute to the fix, it was introduced in fd0dd796576e
2. resolve out-of-bounds transport header for non-GSO packets

---
 drivers/net/geneve.c   | 174 ++++++++++++++++++++++-------------------
 include/linux/skbuff.h |   5 ++
 2 files changed, 98 insertions(+), 81 deletions(-)
drivers/net/geneve.c +73 -0
index c6563367d382..dc5f5394836a 100644
@@ -256,6 +256,79 @@ static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
      return NULL;
 }
 
+/*
+ * Look for GRO hint in the genenve options; if not found or does not pass basic
+ * sanitization return 0, otherwise the offset WRT the geneve hdr start.
+ */
+static unsigned int
+geneve_opt_gro_hint_off(const struct genevehdr *gh, __be16 *type,
+                     unsigned int *gh_len)
+{
+     struct geneve_opt *opt = (void *)(gh + 1);
+     unsigned int id, opt_len = gh->opt_len;
+     struct geneve_opt_gro_hint *gro_hint;
+
+     while (opt_len >= (GENEVE_OPT_GRO_HINT_SIZE >> 2)) {
+             if (opt->opt_class == htons(GENEVE_OPT_NETDEV_CLASS) &&
+                 opt->type == GENEVE_OPT_GRO_HINT_TYPE &&
+                 opt->length == GENEVE_OPT_GRO_HINT_LEN)
+                     goto found;
+
+             /* check for bad opt len */
+             if (opt->length + 1 >= opt_len)
+                     return 0;
+
+             /* next opt */
+             opt_len -= opt->length + 1;
+             opt = ((void *)opt) + ((opt->length + 1) << 2);
+     }
+     return 0;
+
+found:
+     gro_hint = (struct geneve_opt_gro_hint *)opt->opt_data;
+
+     /*
+      * Sanitize the hinted hdrs: the nested transport is UDP and must fit
+      * the overall hinted hdr size.
+      */
+     if (gro_hint->nested_tp_offset + sizeof(struct udphdr) >
+         gro_hint->nested_hdr_len)
+             return 0;
+
+     if (gro_hint->nested_nh_offset +
+         (gro_hint->nested_is_v6 ? sizeof(struct ipv6hdr) :
+                                   sizeof(struct iphdr)) >
+         gro_hint->nested_tp_offset)
+             return 0;
+
+     /* Allow only supported L2. */
+     id = gro_hint->inner_proto_id;
+     if (id >= ARRAY_SIZE(proto_id_map))
+             return 0;
+
+     *type = proto_id_map[id];
+     *gh_len += gro_hint->nested_hdr_len;
+
+     return (void *)gro_hint - (void *)gh;
+}
+
+static const struct geneve_opt_gro_hint *
+geneve_opt_gro_hint(const struct genevehdr *gh, unsigned int hint_off)
+{
+     return (const struct geneve_opt_gro_hint *)((void *)gh + hint_off);
+}
+
+static unsigned int
+geneve_sk_gro_hint_off(const struct sock *sk, const struct genevehdr *gh,
+                    __be16 *type, unsigned int *gh_len)
+{
+     const struct geneve_sock *gs = rcu_dereference_sk_user_data(sk);
+
+     if (!gs || !gs->gro_hint)
+             return 0;
+     return geneve_opt_gro_hint_off(gh, type, gh_len);
+}
+
 /* geneve receive/decap routine */
 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
                    struct sk_buff *skb, const struct genevehdr *gnvh)
@@ -299,6 +372,21 @@ static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
      if (tun_dst)
              skb_dst_set(skb, &tun_dst->dst);
 
Quoted 16 lines
Quoted 4 lines
drivers/net/geneve.c +4 -0
+     if (skb->encapsulation) {
+             unsigned int len = 0, hint_off;
+             __be16 p;
+

drivers/net/geneve.c static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, · line 388

Quoted 10 lines
drivers/net/geneve.c +10 -0
+             hint_off = geneve_sk_gro_hint_off(gs->sock->sk, gnvh, &p, &len);
+             if (hint_off && len <= skb_headlen(skb)) {
+                     const struct geneve_opt_gro_hint *gro_hint;
+
+                     gro_hint = geneve_opt_gro_hint(gnvh, hint_off);
+                     skb_set_transport_header(skb, gro_hint->nested_tp_offset);
+             }
+     } else {
+             skb_unset_transport_header(skb);
+     }

It's not clear to me why the above chunk is needed.

Quoted 7 lines

I think you understand this so i may be stating the obvious:
The main motivation was to isolate the pulls from making the transport header stale
IOW, geneve_hdr(skb) pointer could become "stale" if the packet's head was reallocated (because geneve_rx() is called after all the "pull" operations) - so this ensures that the inner transport header is only set after all header pulling is done - otherwise we get a UAF.

Uhmm... my questions sparked because this patch adds quite a bit of new conditionals in the data-path, which I would like to avoid them.

But I must admit that re-reading the code - I'm likely low on coffee - I don't see the UaF. This sequence:

Quoted 8 lines
        pskb_may_pull(skb...)
        geneveh = geneve_hdr(skb);

is correct/does not yield to UaF, and AFAICS geneveh is always reloaded after pskb_may_pull()/iptunnel_pull_header(). Could you please share a decoded stack trace for the UaF you are observing? So it's clear where exactly kasan reports the problem.

The only one i have is what is in the commit. Comments below..

Quoted 11 lines
Quoted 10 lines
Quoted 4 lines

Why can't
geneve_post_decap_hint() retain the transport header set in the current place? it would make this patch much shorter.

It was more to ensure clean state for cases where we dont have GRO hints. The "else {skb_unset_transport_header(skb);} " handles the case where we are missing GRO hints - at that point outer UDP header is stripped but tranport header is still pointing to where this outer header used to be and something down the road may misintepret the header.

Quoted 3 lines

This looks like an unrelated problem?!? And possibly not a problem at all?!? Upper layers (outside GSO) are expected to re-evaluate the transport header as needed.

Let me provide context, maybe that will help explain or provide a way to fix this better.

I received multiple patches (via security) to resolve the pedit issue; Rajat's was the first one.
Rajat's repro tricks geneve into having _negative transport offsets_. Does that trigger anything for you? geneve just trusts those headers are valid.
It was not Rajat's patch that caused this splat but one of the multiple other reported alternative patches. I would have to dig in to find which patch caused the splat.

Quoted 2 lines

Does this help? The key i believe is looking at the effect of negative transport headers.

Indeed, thank you for the context! My (very) wild guess/first suspect is that what really fixes the issue is the transport header reset.

Quoted 1 line

I can send you the repro maybe you will be able to have a better approach.

I think I already have that one. Let me try to use it and do some experiments. Unfortunatelly it will require a little calendar time, due to obvious EOUTOFTIME :(

/P

#6 lore raw
Quoted 223 lines · Paolo Abeni
Quoted 221 lines · Jamal Hadi Salim
Quoted 182 lines · Paolo Abeni
Quoted 167 lines · Jamal Hadi Salim
Quoted 149 lines · Paolo Abeni
Quoted 148 lines · Jamal Hadi Salim

Context: This commit fixes a UAF in Geneve. The UAF component was discovered because it is triggered by a buggy 'pedit' action[1]. the buggy pedit uses skb_store_bits() and can write into shared or non-linear data without copying the entire skb head. The Geneve device invoking either pskb_may_pull() or iptunnel_pull_header() is much more likely to reallocate the skb head, freeing the memory pointed to by geneve's stale geneveh pointer.

Note, although the patch looks big it's mostly re-arranging functions to avoid forward declaration.

Sample UAF below:
....

BUG: KASAN: slab-use-after-free in geneve_udp_encap_recv+0x2e8c/0x3190
Read of size 1 at addr ffff88811bab3c08 by task kworker/u32:1/62

CPU: 4 UID: 0 PID: 62 Comm: kworker/u32:1 Not tainted 7.1.0-rc3-00182-g0e3e91779b94 #551 PREEMPT(full)
Workqueue: ipv6_addrconf addrconf_dad_work
Call Trace:
 <IRQ>
 dump_stack_lvl+0x8c/0xd0
 print_report+0xce/0x5e0
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? kasan_complete_mode_report_info+0x80/0x200
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 kasan_report+0xd6/0x110
 ? geneve_udp_encap_recv+0x2e8c/0x3190
 __asan_report_load1_noabort+0x18/0x20
 geneve_udp_encap_recv+0x2e8c/0x3190
 ? get_stack_info+0x38/0xc0
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __pfx_geneve_udp_encap_recv+0x10/0x10
 ? udp4_lib_lookup2+0x31c/0x700
 ? __pfx_udp_ehashfn+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __xfrm_policy_check2.constprop.0+0x40/0x5b0
 udp_queue_rcv_one_skb+0x65e/0xd20
 udp_queue_rcv_skb+0x142/0xb00
 udp_unicast_rcv_skb+0xd3/0x2c0
 udp_rcv+0x198f/0x2570
 ? srso_alias_return_thunk+0x5/0xfbef5
 ip_protocol_deliver_rcu+0x1f8/0x3c0
 ip_local_deliver_finish+0x362/0x5d0

....

[1]https://lore.kernel.org/netdev/[email protected]/

Fixes fd0dd796576e ("geneve: use GRO hint option in the RX path")

---
v1->v2 Address gemini-sashiko.
1. local variable "len" initialization in geneve_post_decap_hint() Although doesnt contribute to the fix, it was introduced in fd0dd796576e
2. resolve out-of-bounds transport header for non-GSO packets

---
 drivers/net/geneve.c   | 174 ++++++++++++++++++++++-------------------
 include/linux/skbuff.h |   5 ++
 2 files changed, 98 insertions(+), 81 deletions(-)
drivers/net/geneve.c +73 -0
index c6563367d382..dc5f5394836a 100644
@@ -256,6 +256,79 @@ static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
      return NULL;
 }
 
+/*
+ * Look for GRO hint in the genenve options; if not found or does not pass basic
+ * sanitization return 0, otherwise the offset WRT the geneve hdr start.
+ */
+static unsigned int
+geneve_opt_gro_hint_off(const struct genevehdr *gh, __be16 *type,
+                     unsigned int *gh_len)
+{
+     struct geneve_opt *opt = (void *)(gh + 1);
+     unsigned int id, opt_len = gh->opt_len;
+     struct geneve_opt_gro_hint *gro_hint;
+
+     while (opt_len >= (GENEVE_OPT_GRO_HINT_SIZE >> 2)) {
+             if (opt->opt_class == htons(GENEVE_OPT_NETDEV_CLASS) &&
+                 opt->type == GENEVE_OPT_GRO_HINT_TYPE &&
+                 opt->length == GENEVE_OPT_GRO_HINT_LEN)
+                     goto found;
+
+             /* check for bad opt len */
+             if (opt->length + 1 >= opt_len)
+                     return 0;
+
+             /* next opt */
+             opt_len -= opt->length + 1;
+             opt = ((void *)opt) + ((opt->length + 1) << 2);
+     }
+     return 0;
+
+found:
+     gro_hint = (struct geneve_opt_gro_hint *)opt->opt_data;
+
+     /*
+      * Sanitize the hinted hdrs: the nested transport is UDP and must fit
+      * the overall hinted hdr size.
+      */
+     if (gro_hint->nested_tp_offset + sizeof(struct udphdr) >
+         gro_hint->nested_hdr_len)
+             return 0;
+
+     if (gro_hint->nested_nh_offset +
+         (gro_hint->nested_is_v6 ? sizeof(struct ipv6hdr) :
+                                   sizeof(struct iphdr)) >
+         gro_hint->nested_tp_offset)
+             return 0;
+
+     /* Allow only supported L2. */
+     id = gro_hint->inner_proto_id;
+     if (id >= ARRAY_SIZE(proto_id_map))
+             return 0;
+
+     *type = proto_id_map[id];
+     *gh_len += gro_hint->nested_hdr_len;
+
+     return (void *)gro_hint - (void *)gh;
+}
+
+static const struct geneve_opt_gro_hint *
+geneve_opt_gro_hint(const struct genevehdr *gh, unsigned int hint_off)
+{
+     return (const struct geneve_opt_gro_hint *)((void *)gh + hint_off);
+}
+
+static unsigned int
+geneve_sk_gro_hint_off(const struct sock *sk, const struct genevehdr *gh,
+                    __be16 *type, unsigned int *gh_len)
+{
+     const struct geneve_sock *gs = rcu_dereference_sk_user_data(sk);
+
+     if (!gs || !gs->gro_hint)
+             return 0;
+     return geneve_opt_gro_hint_off(gh, type, gh_len);
+}
+
 /* geneve receive/decap routine */
 static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
                    struct sk_buff *skb, const struct genevehdr *gnvh)
@@ -299,6 +372,21 @@ static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
      if (tun_dst)
              skb_dst_set(skb, &tun_dst->dst);
 
Quoted 16 lines
Quoted 4 lines
drivers/net/geneve.c +4 -0
+     if (skb->encapsulation) {
+             unsigned int len = 0, hint_off;
+             __be16 p;
+

drivers/net/geneve.c static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs, · line 388

Quoted 10 lines
drivers/net/geneve.c +10 -0
+             hint_off = geneve_sk_gro_hint_off(gs->sock->sk, gnvh, &p, &len);
+             if (hint_off && len <= skb_headlen(skb)) {
+                     const struct geneve_opt_gro_hint *gro_hint;
+
+                     gro_hint = geneve_opt_gro_hint(gnvh, hint_off);
+                     skb_set_transport_header(skb, gro_hint->nested_tp_offset);
+             }
+     } else {
+             skb_unset_transport_header(skb);
+     }

It's not clear to me why the above chunk is needed.

Quoted 7 lines

I think you understand this so i may be stating the obvious:
The main motivation was to isolate the pulls from making the transport header stale
IOW, geneve_hdr(skb) pointer could become "stale" if the packet's head was reallocated (because geneve_rx() is called after all the "pull" operations) - so this ensures that the inner transport header is only set after all header pulling is done - otherwise we get a UAF.

Uhmm... my questions sparked because this patch adds quite a bit of new conditionals in the data-path, which I would like to avoid them.

But I must admit that re-reading the code - I'm likely low on coffee - I don't see the UaF. This sequence:

Quoted 8 lines
        pskb_may_pull(skb...)
        geneveh = geneve_hdr(skb);

is correct/does not yield to UaF, and AFAICS geneveh is always reloaded after pskb_may_pull()/iptunnel_pull_header(). Could you please share a decoded stack trace for the UaF you are observing? So it's clear where exactly kasan reports the problem.

The only one i have is what is in the commit. Comments below..

Quoted 11 lines
Quoted 10 lines
Quoted 4 lines

Why can't
geneve_post_decap_hint() retain the transport header set in the current place? it would make this patch much shorter.

It was more to ensure clean state for cases where we dont have GRO hints. The "else {skb_unset_transport_header(skb);} " handles the case where we are missing GRO hints - at that point outer UDP header is stripped but tranport header is still pointing to where this outer header used to be and something down the road may misintepret the header.

Quoted 3 lines

This looks like an unrelated problem?!? And possibly not a problem at all?!? Upper layers (outside GSO) are expected to re-evaluate the transport header as needed.

Let me provide context, maybe that will help explain or provide a way to fix this better.

I received multiple patches (via security) to resolve the pedit issue; Rajat's was the first one.
Rajat's repro tricks geneve into having _negative transport offsets_. Does that trigger anything for you? geneve just trusts those headers are valid.
It was not Rajat's patch that caused this splat but one of the multiple other reported alternative patches. I would have to dig in to find which patch caused the splat.

Quoted 6 lines
Quoted 2 lines

Does this help? The key i believe is looking at the effect of negative transport headers.

Indeed, thank you for the context! My (very) wild guess/first suspect is that what really fixes the issue is the transport header reset.

I believe so. Initial patch was to reset it in the core code in my adventure to ensure all tunnels are handled equivalently but gave up after thinking it's too risky.

Quoted 5 lines
Quoted 1 line

I can send you the repro maybe you will be able to have a better approach.

I think I already have that one. Let me try to use it and do some experiments. Unfortunatelly it will require a little calendar time, due to obvious EOUTOFTIME :(

I will leave it in your able hands to whatever is appropriate. The sense of urgency to get a fix is not there anymore since it seems we are making good progress on the pedit one.

cheers,
jamal

Quoted 2 lines

/P