Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752626AbdLAHHy (ORCPT ); Fri, 1 Dec 2017 02:07:54 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47428 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751946AbdLAHHw (ORCPT ); Fri, 1 Dec 2017 02:07:52 -0500 Subject: Re: [PATCH 2/3] tun: free skb in early errors To: wexu@redhat.com, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: mst@redhat.com, mjrosato@linux.vnet.ibm.com References: <1512107669-27572-1-git-send-email-wexu@redhat.com> <1512107669-27572-3-git-send-email-wexu@redhat.com> From: Jason Wang Message-ID: Date: Fri, 1 Dec 2017 15:07:44 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <1512107669-27572-3-git-send-email-wexu@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 01 Dec 2017 07:07:52 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1787 Lines: 63 On 2017年12月01日 13:54, wexu@redhat.com wrote: > From: Wei Xu > > tun_recvmsg() supports accepting skb by msg_control after > commit ac77cfd4258f ("tun: support receiving skb through msg_control"), > the skb if presented should be freed within the function, otherwise it > would be leaked. > > Signed-off-by: Wei Xu > Reported-by: Matthew Rosato > --- > drivers/net/tun.c | 14 +++++++++++--- > 1 file changed, 11 insertions(+), 3 deletions(-) > > diff --git a/drivers/net/tun.c b/drivers/net/tun.c > index 6a7bde9..5563430 100644 > --- a/drivers/net/tun.c > +++ b/drivers/net/tun.c > @@ -2067,14 +2067,17 @@ 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_struct *tun = tun_get(tfile); > + struct sk_buff *skb = m->msg_control; > int ret; > > - if (!tun) > - return -EBADFD; > + if (!tun) { > + ret = -EBADFD; > + goto out_free_skb; Unfortunately, you can't to there since tun is NULL. > + } > > if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { > ret = -EINVAL; > - goto out; > + goto out_free_skb; > } > if (flags & MSG_ERRQUEUE) { > ret = sock_recv_errqueue(sock->sk, m, total_len, > @@ -2087,6 +2090,11 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, > m->msg_flags |= MSG_TRUNC; > ret = flags & MSG_TRUNC ? ret : total_len; > } > + goto out; We usually don't use goto in the case of success, and you need deal with the case skb != NULL but iov_iter_count(to) == 0 in tun_do_read(). Thanks > + > +out_free_skb: > + if (skb) > + kfree_skb(skb); > out: > tun_put(tun); > return ret;