Commit c33ee15b authored by Wei Xu's avatar Wei Xu Committed by David S. Miller

tun: free skb in early errors

tun_recvmsg() supports accepting skb by msg_control after
commit ac77cfd4 ("tun: support receiving skb through msg_control"),
the skb if presented should be freed no matter how far it can go
along, otherwise it would be leaked.

This patch fixes several missed cases.
Signed-off-by: default avatarWei Xu <wexu@redhat.com>
Reported-by: default avatarMatthew Rosato <mjrosato@linux.vnet.ibm.com>
Acked-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 6e474083
...@@ -1952,8 +1952,11 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, ...@@ -1952,8 +1952,11 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
tun_debug(KERN_INFO, tun, "tun_do_read\n"); tun_debug(KERN_INFO, tun, "tun_do_read\n");
if (!iov_iter_count(to)) if (!iov_iter_count(to)) {
if (skb)
kfree_skb(skb);
return 0; return 0;
}
if (!skb) { if (!skb) {
/* Read frames from ring */ /* Read frames from ring */
...@@ -2069,22 +2072,24 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, ...@@ -2069,22 +2072,24 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
{ {
struct tun_file *tfile = container_of(sock, struct tun_file, socket); struct tun_file *tfile = container_of(sock, struct tun_file, socket);
struct tun_struct *tun = tun_get(tfile); struct tun_struct *tun = tun_get(tfile);
struct sk_buff *skb = m->msg_control;
int ret; int ret;
if (!tun) if (!tun) {
return -EBADFD; ret = -EBADFD;
goto out_free_skb;
}
if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
ret = -EINVAL; ret = -EINVAL;
goto out; goto out_put_tun;
} }
if (flags & MSG_ERRQUEUE) { if (flags & MSG_ERRQUEUE) {
ret = sock_recv_errqueue(sock->sk, m, total_len, ret = sock_recv_errqueue(sock->sk, m, total_len,
SOL_PACKET, TUN_TX_TIMESTAMP); SOL_PACKET, TUN_TX_TIMESTAMP);
goto out; goto out;
} }
ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, skb);
m->msg_control);
if (ret > (ssize_t)total_len) { if (ret > (ssize_t)total_len) {
m->msg_flags |= MSG_TRUNC; m->msg_flags |= MSG_TRUNC;
ret = flags & MSG_TRUNC ? ret : total_len; ret = flags & MSG_TRUNC ? ret : total_len;
...@@ -2092,6 +2097,13 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, ...@@ -2092,6 +2097,13 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
out: out:
tun_put(tun); tun_put(tun);
return ret; return ret;
out_put_tun:
tun_put(tun);
out_free_skb:
if (skb)
kfree_skb(skb);
return ret;
} }
static int tun_peek_len(struct socket *sock) static int tun_peek_len(struct socket *sock)
......
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