mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] net: ipv4: fix a memleak in ip_setup_cork
@ 2024-01-26  7:51 Zhipeng Lu
  2024-01-26 10:13 ` Eric Dumazet
  0 siblings, 1 reply; 4+ messages in thread
From: Zhipeng Lu @ 2024-01-26  7:51 UTC (permalink / raw)
  To: alexious
  Cc: David S. Miller, David Ahern, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, netdev, linux-kernel

When inetdev_valid_mtu fails, cork->opt should be freed if it is
allocated in ip_setup_cork. Otherwise there could be a memleak.

Fixes: 501a90c94510 ("inet: protect against too small mtu values.")
Signed-off-by: Zhipeng Lu <alexious@zju.edu.cn>
---
 net/ipv4/ip_output.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c
index b06f678b03a1..3215ea07d398 100644
--- a/net/ipv4/ip_output.c
+++ b/net/ipv4/ip_output.c
@@ -1282,6 +1282,7 @@ static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
 {
 	struct ip_options_rcu *opt;
 	struct rtable *rt;
+	int free_opt = 0;
 
 	rt = *rtp;
 	if (unlikely(!rt))
@@ -1297,6 +1298,7 @@ static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
 					    sk->sk_allocation);
 			if (unlikely(!cork->opt))
 				return -ENOBUFS;
+			free_opt = 1;
 		}
 		memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
 		cork->flags |= IPCORK_OPT;
@@ -1306,8 +1308,13 @@ static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
 	cork->fragsize = ip_sk_use_pmtu(sk) ?
 			 dst_mtu(&rt->dst) : READ_ONCE(rt->dst.dev->mtu);
 
-	if (!inetdev_valid_mtu(cork->fragsize))
+	if (!inetdev_valid_mtu(cork->fragsize)) {
+		if (opt && free_opt) {
+			kfree(cork->opt);
+			cork->opt = NULL;
+		}
 		return -ENETUNREACH;
+	}
 
 	cork->gso_size = ipc->gso_size;
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-29  9:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-26  7:51 [PATCH] net: ipv4: fix a memleak in ip_setup_cork Zhipeng Lu
2024-01-26 10:13 ` Eric Dumazet
2024-01-27  8:34   ` Eric Dumazet
2024-01-29  9:02     ` alexious

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®