Commit 15ca2bf6 authored by Eric Dumazet's avatar Eric Dumazet Committed by Kamal Mostafa

ipv4: fix memory leaks in ip_cmsg_send() callers

[ Upstream commit 91948309 ]

Dmitry reported memory leaks of IP options allocated in
ip_cmsg_send() when/if this function returns an error.

Callers are responsible for the freeing.

Many thanks to Dmitry for the report and diagnostic.
Reported-by: default avatarDmitry Vyukov <dvyukov@google.com>
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarKamal Mostafa <kamal@canonical.com>
parent 25199b04
...@@ -216,6 +216,8 @@ int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc, ...@@ -216,6 +216,8 @@ int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc,
switch (cmsg->cmsg_type) { switch (cmsg->cmsg_type) {
case IP_RETOPTS: case IP_RETOPTS:
err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)); err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
/* Our caller is responsible for freeing ipc->opt */
err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg), err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
err < 40 ? err : 40); err < 40 ? err : 40);
if (err) if (err)
......
...@@ -746,8 +746,10 @@ static int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *m ...@@ -746,8 +746,10 @@ static int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *m
if (msg->msg_controllen) { if (msg->msg_controllen) {
err = ip_cmsg_send(sock_net(sk), msg, &ipc, false); err = ip_cmsg_send(sock_net(sk), msg, &ipc, false);
if (err) if (unlikely(err)) {
kfree(ipc.opt);
return err; return err;
}
if (ipc.opt) if (ipc.opt)
free = 1; free = 1;
} }
......
...@@ -545,8 +545,10 @@ static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, ...@@ -545,8 +545,10 @@ static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
if (msg->msg_controllen) { if (msg->msg_controllen) {
err = ip_cmsg_send(sock_net(sk), msg, &ipc, false); err = ip_cmsg_send(sock_net(sk), msg, &ipc, false);
if (err) if (unlikely(err)) {
kfree(ipc.opt);
goto out; goto out;
}
if (ipc.opt) if (ipc.opt)
free = 1; free = 1;
} }
......
...@@ -965,8 +965,10 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, ...@@ -965,8 +965,10 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
if (msg->msg_controllen) { if (msg->msg_controllen) {
err = ip_cmsg_send(sock_net(sk), msg, &ipc, err = ip_cmsg_send(sock_net(sk), msg, &ipc,
sk->sk_family == AF_INET6); sk->sk_family == AF_INET6);
if (err) if (unlikely(err)) {
kfree(ipc.opt);
return err; return err;
}
if (ipc.opt) if (ipc.opt)
free = 1; free = 1;
connected = 0; connected = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment