Skip to content

Commit d198b27

Browse files
Eric Dumazetdavem330
Eric Dumazet
authored andcommitted
Revert "Revert "ipv4: fix memory leaks in ip_cmsg_send() callers""
This reverts commit d7807a9. As mentioned in https://lkml.org/lkml/2021/9/13/1819 5 years old commit 9194830 ("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: d7807a9 ("Revert "ipv4: fix memory leaks in ip_cmsg_send() callers"") Signed-off-by: Eric Dumazet <[email protected]> Cc: Yajun Deng <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 4f884f3 commit d198b27

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

net/ipv4/ip_sockglue.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
279279
case IP_RETOPTS:
280280
err = cmsg->cmsg_len - sizeof(struct cmsghdr);
281281

282-
/* Our caller is responsible for freeing ipc->opt when err = 0 */
282+
/* Our caller is responsible for freeing ipc->opt */
283283
err = ip_options_get(net, &ipc->opt,
284284
KERNEL_SOCKPTR(CMSG_DATA(cmsg)),
285285
err < 40 ? err : 40);

net/ipv4/ping.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,10 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
727727

728728
if (msg->msg_controllen) {
729729
err = ip_cmsg_send(sk, msg, &ipc, false);
730-
if (unlikely(err))
730+
if (unlikely(err)) {
731+
kfree(ipc.opt);
731732
return err;
732-
733+
}
733734
if (ipc.opt)
734735
free = 1;
735736
}

net/ipv4/raw.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,10 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
562562

563563
if (msg->msg_controllen) {
564564
err = ip_cmsg_send(sk, msg, &ipc, false);
565-
if (unlikely(err))
565+
if (unlikely(err)) {
566+
kfree(ipc.opt);
566567
goto out;
567-
568+
}
568569
if (ipc.opt)
569570
free = 1;
570571
}

net/ipv4/udp.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1122,9 +1122,10 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
11221122
if (err > 0)
11231123
err = ip_cmsg_send(sk, msg, &ipc,
11241124
sk->sk_family == AF_INET6);
1125-
if (unlikely(err < 0))
1125+
if (unlikely(err < 0)) {
1126+
kfree(ipc.opt);
11261127
return err;
1127-
1128+
}
11281129
if (ipc.opt)
11291130
free = 1;
11301131
connected = 0;

0 commit comments

Comments
 (0)