Skip to contentSkip to conversation

xfrm: xfrm_input: fix a possible memory leak in xfrm_input()

PATCH In review Hangyu Hua 2 participants 5 messages May 30, 2022 – Jun 1, 2022

Subsystems (3)
lore
Share & tools
lore

Hangyu Hua

author
#0 lore raw

PATCH 1/1 xfrm: xfrm_input: fix a possible memory leak in xfrm_input()

xfrm_input needs to handle skb internally. But skb is not freed When xo->flags & XFRM_GRO == 0 and decaps == 0.

Fixes 7785bba299a8 ("esp: Add a software GRO codepath")
---
 net/xfrm/xfrm_input.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/xfrm/xfrm_input.c +1 -1
index 144238a50f3d..6f9576352f30 100644
@@ -742,7 +742,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
 			gro_cells_receive(&gro_cells, skb);
 			return err;
 		}
-
+		kfree_skb(skb);
 		return err;
 	}
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 14 lines · Hangyu Hua

xfrm_input needs to handle skb internally. But skb is not freed When xo->flags & XFRM_GRO == 0 and decaps == 0.

Fixes 7785bba299a8 ("esp: Add a software GRO codepath")
---
 net/xfrm/xfrm_input.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/xfrm/xfrm_input.c +0 -0
index 144238a50f3d..6f9576352f30 100644

net/xfrm/xfrm_input.c int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) · line 747

Quoted 7 lines
net/xfrm/xfrm_input.c +1 -1
@@ -742,7 +742,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
 			gro_cells_receive(&gro_cells, skb);
 			return err;
 		}
-
+		kfree_skb(skb);
 		return err;
 	}

Did you test this? The function behind the 'afinfo->the transport_finish()' pointer handles this skb and frees it in that case.

author
#2 lore raw
Quoted 10 lines · Steffen Klassert
Quoted 9 lines · Hangyu Hua

xfrm_input needs to handle skb internally. But skb is not freed When xo->flags & XFRM_GRO == 0 and decaps == 0.

Fixes 7785bba299a8 ("esp: Add a software GRO codepath")
---
  net/xfrm/xfrm_input.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

net/xfrm/xfrm_input.c int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) · line 747

Quoted 12 lines
net/xfrm/xfrm_input.c +1 -1
index 144238a50f3d..6f9576352f30 100644
@@ -742,7 +742,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
  			gro_cells_receive(&gro_cells, skb);
  			return err;
  		}
-
+		kfree_skb(skb);
  		return err;
  	}

Did you test this? The function behind the 'afinfo->the transport_finish()' pointer handles this skb and frees it in that case.

int xfrm4_transport_finish(struct sk_buff *skb, int async)

{
	struct xfrm_offload *xo = xfrm_offload(skb);
	struct iphdr *iph = ip_hdr(skb);

	iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
#ifndef CONFIG_NETFILTER
	if (!async)
		return -iph->protocol;		<--- [1]
#endif
...
	NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
		dev_net(skb->dev), NULL, skb, skb->dev, NULL,
		xfrm4_rcv_encap_finish);	<--- [2]
	return 0;
}

int xfrm6_transport_finish(struct sk_buff *skb, int async)

{
	struct xfrm_offload *xo = xfrm_offload(skb);
	int nhlen = skb->data - skb_network_header(skb);

	skb_network_header(skb)[IP6CB(skb)->nhoff] =
		XFRM_MODE_SKB_CB(skb)->protocol;
#ifndef CONFIG_NETFILTER
	if (!async)
		return 1;			<--- [3]
#endif
...
	NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
		dev_net(skb->dev), NULL, skb, skb->dev, NULL,
		xfrm6_transport_finish2);
	return 0;				<--- [4]
}

If transport_finish() return in [1] or [3], there will be a memory leak. If it return return in [2] and [4], there will not be a memory leak. It look like my patch is incorrect.

How do you think i modify the patch as follows?

gro_cells_receive(&gro_cells, skb); return err;
}
-
+ if (err != 0)
+ kfree_skb(skb);
return err;
}

maintainer
#3 lore raw
Quoted 64 lines · Hangyu Hua
Quoted 10 lines · Steffen Klassert
Quoted 9 lines · Hangyu Hua

xfrm_input needs to handle skb internally. But skb is not freed When xo->flags & XFRM_GRO == 0 and decaps == 0.

Fixes 7785bba299a8 ("esp: Add a software GRO codepath")
---
  net/xfrm/xfrm_input.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

net/xfrm/xfrm_input.c int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) · line 747

Quoted 12 lines
net/xfrm/xfrm_input.c +1 -1
index 144238a50f3d..6f9576352f30 100644
@@ -742,7 +742,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
  			gro_cells_receive(&gro_cells, skb);
  			return err;
  		}
-
+		kfree_skb(skb);
  		return err;
  	}

Did you test this? The function behind the 'afinfo->the transport_finish()' pointer handles this skb and frees it in that case.

int xfrm4_transport_finish(struct sk_buff *skb, int async)

{
	struct xfrm_offload *xo = xfrm_offload(skb);
	struct iphdr *iph = ip_hdr(skb);

	iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
#ifndef CONFIG_NETFILTER
	if (!async)
		return -iph->protocol;		<--- [1]
#endif
...
	NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
		dev_net(skb->dev), NULL, skb, skb->dev, NULL,
		xfrm4_rcv_encap_finish);	<--- [2]
	return 0;
}

int xfrm6_transport_finish(struct sk_buff *skb, int async)

{
	struct xfrm_offload *xo = xfrm_offload(skb);
	int nhlen = skb->data - skb_network_header(skb);

	skb_network_header(skb)[IP6CB(skb)->nhoff] =
		XFRM_MODE_SKB_CB(skb)->protocol;
#ifndef CONFIG_NETFILTER
	if (!async)
		return 1;			<--- [3]
#endif
...
	NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
		dev_net(skb->dev), NULL, skb, skb->dev, NULL,
		xfrm6_transport_finish2);
	return 0;				<--- [4]
}
Quoted 1 line

If transport_finish() return in [1] or [3], there will be a memory leak.

No, even in that case there is no memleak. Look for instance at the IPv4 case, we return -iph->protocol here.
Then look at ip_protocol_deliver_rcu(). If the ipprot->handler (xfrm) returns a negative value, this is interpreted as the protocol number and the packet is resubmitted to the next protocol handler.

Please test your patches before you submit them in the future.

author
#4 lore raw
Quoted 67 lines · Steffen Klassert
Quoted 64 lines · Hangyu Hua
Quoted 10 lines · Steffen Klassert
Quoted 9 lines · Hangyu Hua

xfrm_input needs to handle skb internally. But skb is not freed When xo->flags & XFRM_GRO == 0 and decaps == 0.

Fixes 7785bba299a8 ("esp: Add a software GRO codepath")
---
   net/xfrm/xfrm_input.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

net/xfrm/xfrm_input.c int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) · line 747

Quoted 12 lines
net/xfrm/xfrm_input.c +1 -1
index 144238a50f3d..6f9576352f30 100644
@@ -742,7 +742,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
   			gro_cells_receive(&gro_cells, skb);
   			return err;
   		}
-
+		kfree_skb(skb);
   		return err;
   	}

Did you test this? The function behind the 'afinfo->the transport_finish()' pointer handles this skb and frees it in that case.

int xfrm4_transport_finish(struct sk_buff *skb, int async)

{
	struct xfrm_offload *xo = xfrm_offload(skb);
	struct iphdr *iph = ip_hdr(skb);

	iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
#ifndef CONFIG_NETFILTER
	if (!async)
		return -iph->protocol;		<--- [1]
#endif
...
	NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
		dev_net(skb->dev), NULL, skb, skb->dev, NULL,
		xfrm4_rcv_encap_finish);	<--- [2]
	return 0;
}

int xfrm6_transport_finish(struct sk_buff *skb, int async)

{
	struct xfrm_offload *xo = xfrm_offload(skb);
	int nhlen = skb->data - skb_network_header(skb);

	skb_network_header(skb)[IP6CB(skb)->nhoff] =
		XFRM_MODE_SKB_CB(skb)->protocol;
#ifndef CONFIG_NETFILTER
	if (!async)
		return 1;			<--- [3]
#endif
...
	NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
		dev_net(skb->dev), NULL, skb, skb->dev, NULL,
		xfrm6_transport_finish2);
	return 0;				<--- [4]
}
Quoted 1 line

If transport_finish() return in [1] or [3], there will be a memory leak.

Quoted 7 lines

No, even in that case there is no memleak. Look for instance at the IPv4 case, we return -iph->protocol here.
Then look at ip_protocol_deliver_rcu(). If the ipprot->handler (xfrm) returns a negative value, this is interpreted as the protocol number and the packet is resubmitted to the next protocol handler.

Please test your patches before you submit them in the future.

Thanks for your explanation. I will be more careful in the future.