This reverts commit
168508dc2278e7f9bfed0f3189d8d41bcb663e96.
As mentioned in https://lkml.org/lkml/2021/9/13/1819
5 years old commit
49587447b55d ("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: 168508dc2278 ("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>
Signed-off-by: David S. Miller <davem@davemloft.net>
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);
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;
}
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;
}
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;