Skip to content

Commit

Permalink
ipv4: fix memory leaks in udp_sendmsg, ping_v4_sendmsg
Browse files Browse the repository at this point in the history
[ Upstream commit 1b97013 ]

Fix more memory leaks in ip_cmsg_send() callers. Part of them were fixed
earlier in 9194830.

* udp_sendmsg one was there since the beginning when linux sources were
  first added to git;
* ping_v4_sendmsg one was copy/pasted in c319b4d.

Whenever return happens in udp_sendmsg() or ping_v4_sendmsg() IP options
have to be freed if they were allocated previously.

Add label so that future callers (if any) can use it instead of kfree()
before return that is easy to forget.

Fixes: c319b4d (net: ipv4: add IPPROTO_ICMP socket kind)
Signed-off-by: Andrey Ignatov <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
rdna authored and gregkh committed May 25, 2018
1 parent 7eaa949 commit fb10481
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions net/ipv4/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,10 @@ static int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *m
ipc.addr = faddr = daddr;

if (ipc.opt && ipc.opt->opt.srr) {
if (!daddr)
return -EINVAL;
if (!daddr) {
err = -EINVAL;
goto out_free;
}
faddr = ipc.opt->opt.faddr;
}
tos = get_rttos(&ipc, inet);
Expand Down Expand Up @@ -837,6 +839,7 @@ static int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *m

out:
ip_rt_put(rt);
out_free:
if (free)
kfree(ipc.opt);
if (!err) {
Expand Down
7 changes: 5 additions & 2 deletions net/ipv4/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,10 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
ipc.addr = faddr = daddr;

if (ipc.opt && ipc.opt->opt.srr) {
if (!daddr)
return -EINVAL;
if (!daddr) {
err = -EINVAL;
goto out_free;
}
faddr = ipc.opt->opt.faddr;
connected = 0;
}
Expand Down Expand Up @@ -1081,6 +1083,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,

out:
ip_rt_put(rt);
out_free:
if (free)
kfree(ipc.opt);
if (!err)
Expand Down

0 comments on commit fb10481

Please sign in to comment.