* [PATCH net] Revert "Revert "ipv4: fix memory leaks in ip_cmsg_send() callers""
@ 2021-09-14 5:18 Eric Dumazet
2021-09-14 13:30 ` patchwork-bot+netdevbpf
0 siblings, 1 reply; 2+ messages in thread
From: Eric Dumazet @ 2021-09-14 5:18 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski
Cc: netdev, Eric Dumazet, Eric Dumazet, Yajun Deng
From: Eric Dumazet <edumazet@google.com>
This reverts commit d7807a9adf4856171f8441f13078c33941df48ab.
As mentioned in https://lkml.org/lkml/2021/9/13/1819
5 years old commit 919483096bfe ("ipv4: fix memory leaks in ip_cmsg_send() callers")
was a correct fix.
ip_cmsg_send() can loop over multiple cmsghdr()
If IP_RETOPTS has been successful, but following cmsghdr generates an error,
we do not free ipc.ok
If IP_RETOPTS is not successful, we have freed the allocated temporary space,
not the one currently in ipc.opt.
Sure, code could be refactored, but let's not bring back old bugs.
Fixes: d7807a9adf48 ("Revert "ipv4: fix memory leaks in ip_cmsg_send() callers"")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Yajun Deng <yajun.deng@linux.dev>
---
net/ipv4/ip_sockglue.c | 2 +-
net/ipv4/ping.c | 5 +++--
net/ipv4/raw.c | 5 +++--
net/ipv4/udp.c | 5 +++--
4 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
index 7cef9987ab4ace4444c4b470a3393ce50219a69a..b297bb28556ec5cf383068f67ee910af38591cc3 100644
--- a/net/ipv4/ip_sockglue.c
+++ b/net/ipv4/ip_sockglue.c
@@ -279,7 +279,7 @@ int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
case IP_RETOPTS:
err = cmsg->cmsg_len - sizeof(struct cmsghdr);
- /* Our caller is responsible for freeing ipc->opt when err = 0 */
+ /* Our caller is responsible for freeing ipc->opt */
err = ip_options_get(net, &ipc->opt,
KERNEL_SOCKPTR(CMSG_DATA(cmsg)),
err < 40 ? err : 40);
diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
index c588f9f2f46c2b91fc9424ecfc0590b2b63a3470..1e44a43acfe2dfe57efdc64479c2d18402881d73 100644
--- a/net/ipv4/ping.c
+++ b/net/ipv4/ping.c
@@ -727,9 +727,10 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
if (msg->msg_controllen) {
err = ip_cmsg_send(sk, msg, &ipc, false);
- if (unlikely(err))
+ if (unlikely(err)) {
+ kfree(ipc.opt);
return err;
-
+ }
if (ipc.opt)
free = 1;
}
diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c
index 1c98063a3ae816ecfd7135b0168fea5a9380676f..bb446e60cf58057b448f094b4d6f48d6e91d113c 100644
--- a/net/ipv4/raw.c
+++ b/net/ipv4/raw.c
@@ -562,9 +562,10 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
if (msg->msg_controllen) {
err = ip_cmsg_send(sk, msg, &ipc, false);
- if (unlikely(err))
+ if (unlikely(err)) {
+ kfree(ipc.opt);
goto out;
-
+ }
if (ipc.opt)
free = 1;
}
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index d5f5981d7a43244cace63653b790a34174364e3e..8851c9463b4b62c9017565f545250c4ffe22927c 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -1122,9 +1122,10 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
if (err > 0)
err = ip_cmsg_send(sk, msg, &ipc,
sk->sk_family == AF_INET6);
- if (unlikely(err < 0))
+ if (unlikely(err < 0)) {
+ kfree(ipc.opt);
return err;
-
+ }
if (ipc.opt)
free = 1;
connected = 0;
--
2.33.0.309.g3052b89438-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH net] Revert "Revert "ipv4: fix memory leaks in ip_cmsg_send() callers""
2021-09-14 5:18 [PATCH net] Revert "Revert "ipv4: fix memory leaks in ip_cmsg_send() callers"" Eric Dumazet
@ 2021-09-14 13:30 ` patchwork-bot+netdevbpf
0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+netdevbpf @ 2021-09-14 13:30 UTC (permalink / raw)
To: Eric Dumazet; +Cc: davem, kuba, netdev, edumazet, yajun.deng
Hello:
This patch was applied to netdev/net.git (refs/heads/master):
On Mon, 13 Sep 2021 22:18:51 -0700 you wrote:
> From: Eric Dumazet <edumazet@google.com>
>
> This reverts commit d7807a9adf4856171f8441f13078c33941df48ab.
>
> As mentioned in https://lkml.org/lkml/2021/9/13/1819
> 5 years old commit 919483096bfe ("ipv4: fix memory leaks in ip_cmsg_send() callers")
> was a correct fix.
>
> [...]
Here is the summary with links:
- [net] Revert "Revert "ipv4: fix memory leaks in ip_cmsg_send() callers""
https://git.kernel.org/netdev/net/c/d198b2776264
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-09-14 13:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-14 5:18 [PATCH net] Revert "Revert "ipv4: fix memory leaks in ip_cmsg_send() callers"" Eric Dumazet
2021-09-14 13:30 ` patchwork-bot+netdevbpf
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.