All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gonglei <arei.gonglei@huawei.com>
To: Michael Tokarev <mjt@tls.msk.ru>
Cc: "qemu-trivial@nongnu.org" <qemu-trivial@nongnu.org>,
	"mst@redhat.com" <mst@redhat.com>,
	"Huangweidong \(C\)" <weidong.huang@huawei.com>,
	"qemu-devel@nongnu.org" <qemu-devel@nongnu.org>,
	"aneesh.kumar@linux.vnet.ibm.com"
	<aneesh.kumar@linux.vnet.ibm.com>
Subject: Re: [Qemu-trivial] [PATCH] virtio-9p-proxy: Fix sockfd leak and modify the check logic
Date: Thu, 30 Oct 2014 18:51:34 +0800	[thread overview]
Message-ID: <54521836.3070701@huawei.com> (raw)
In-Reply-To: <5451F0CC.3030100@msgid.tls.msk.ru>

On 2014/10/30 16:03, Michael Tokarev wrote:

> 29.10.2014 13:52, arei.gonglei@huawei.com wrote:
>> From: Gonglei <arei.gonglei@huawei.com>
>>
>> If connect() return false, the sockfd will leak,
>> meanwhile proxy_init() can't check the return value
>> of connect_namedsocket(), maybe cause unpredictable
>> results.
>>
>> Let's move the sock_id check logic out, which can
>> check both if and else statements.
>>
>> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
>> ---
>>  hw/9pfs/virtio-9p-proxy.c | 12 +++++++-----
>>  1 file changed, 7 insertions(+), 5 deletions(-)
>>
>> diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c
>> index b57966d..1c3aa5f 100644
>> --- a/hw/9pfs/virtio-9p-proxy.c
>> +++ b/hw/9pfs/virtio-9p-proxy.c
>> @@ -1112,6 +1112,7 @@ static int connect_namedsocket(const char *path)
>>      size = strlen(helper.sun_path) + sizeof(helper.sun_family);
>>      if (connect(sockfd, (struct sockaddr *)&helper, size) < 0) {
>>          fprintf(stderr, "socket error\n");
>> +        close(sockfd);
>>          return -1;
>>      }
>>  
>> @@ -1152,11 +1153,12 @@ static int proxy_init(FsContext *ctx)
>>          sock_id = connect_namedsocket(ctx->fs_root);
>>      } else {
>>          sock_id = atoi(ctx->fs_root);
>> -        if (sock_id < 0) {
>> -            fprintf(stderr, "socket descriptor not initialized\n");
>> -            g_free(proxy);
>> -            return -1;
>> -        }
>> +    }
>> +
>> +    if (sock_id < 0) {
>> +        fprintf(stderr, "socket descriptor not initialized\n");
>> +        g_free(proxy);
>> +        return -1;
>>      }
>>      g_free(ctx->fs_root);
>>      ctx->fs_root = NULL;
> 
> 
> Um. I'm applying 2 patches instead of this one.  First:
> 
> 

Ok, Thanks for your work.

Best regards,
-Gonglei

> 
> virtio-9p-proxy: Fix sockfd leak
> 
> If connect() in connect_namedsocket() return false, the sockfd will leak.
> Plug it.
> 
> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
> 
> diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c
> index b57966d..e6bbb06 100644
> --- a/hw/9pfs/virtio-9p-proxy.c
> +++ b/hw/9pfs/virtio-9p-proxy.c
> @@ -1112,6 +1112,7 @@ static int connect_namedsocket(const char *path)
>      size = strlen(helper.sun_path) + sizeof(helper.sun_family);
>      if (connect(sockfd, (struct sockaddr *)&helper, size) < 0) {
>          fprintf(stderr, "socket error\n");
> +        close(sockfd);
>          return -1;
>      }
> 
> 
> 
> And second.  Note the slight change in logic and error messages
> compared with your version - there's no need to print error
> message twice if connect_namedsocket() returned -1 (it already
> printed error message).
> 
> virtio-9p-proxy: fix error return in proxy_init()
> 
> proxy_init() does not check the return value of connect_namedsocket(),
> fix this by rearranging code a little bit.
> 
> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
> 
> diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c
> index e6bbb06..2ec211b 100644
> --- a/hw/9pfs/virtio-9p-proxy.c
> +++ b/hw/9pfs/virtio-9p-proxy.c
> @@ -1155,10 +1155,12 @@ static int proxy_init(FsContext *ctx)
>          sock_id = atoi(ctx->fs_root);
>          if (sock_id < 0) {
>              fprintf(stderr, "socket descriptor not initialized\n");
> -            g_free(proxy);
> -            return -1;
>          }
>      }
> +    if (sock_id < 0) {
> +        g_free(proxy);
> +        return -1;
> +    }
>      g_free(ctx->fs_root);
>      ctx->fs_root = NULL;
> 
> 
> And I'll immediately send another followup patch to improve
> error messages in connect_namedsocket(), -- these are awful.
> 
> /mjt





WARNING: multiple messages have this Message-ID (diff)
From: Gonglei <arei.gonglei@huawei.com>
To: Michael Tokarev <mjt@tls.msk.ru>
Cc: "qemu-trivial@nongnu.org" <qemu-trivial@nongnu.org>,
	"mst@redhat.com" <mst@redhat.com>,
	"Huangweidong (C)" <weidong.huang@huawei.com>,
	"qemu-devel@nongnu.org" <qemu-devel@nongnu.org>,
	"aneesh.kumar@linux.vnet.ibm.com"
	<aneesh.kumar@linux.vnet.ibm.com>
Subject: Re: [Qemu-devel] [Qemu-trivial] [PATCH] virtio-9p-proxy: Fix sockfd leak and modify the check logic
Date: Thu, 30 Oct 2014 18:51:34 +0800	[thread overview]
Message-ID: <54521836.3070701@huawei.com> (raw)
In-Reply-To: <5451F0CC.3030100@msgid.tls.msk.ru>

On 2014/10/30 16:03, Michael Tokarev wrote:

> 29.10.2014 13:52, arei.gonglei@huawei.com wrote:
>> From: Gonglei <arei.gonglei@huawei.com>
>>
>> If connect() return false, the sockfd will leak,
>> meanwhile proxy_init() can't check the return value
>> of connect_namedsocket(), maybe cause unpredictable
>> results.
>>
>> Let's move the sock_id check logic out, which can
>> check both if and else statements.
>>
>> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
>> ---
>>  hw/9pfs/virtio-9p-proxy.c | 12 +++++++-----
>>  1 file changed, 7 insertions(+), 5 deletions(-)
>>
>> diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c
>> index b57966d..1c3aa5f 100644
>> --- a/hw/9pfs/virtio-9p-proxy.c
>> +++ b/hw/9pfs/virtio-9p-proxy.c
>> @@ -1112,6 +1112,7 @@ static int connect_namedsocket(const char *path)
>>      size = strlen(helper.sun_path) + sizeof(helper.sun_family);
>>      if (connect(sockfd, (struct sockaddr *)&helper, size) < 0) {
>>          fprintf(stderr, "socket error\n");
>> +        close(sockfd);
>>          return -1;
>>      }
>>  
>> @@ -1152,11 +1153,12 @@ static int proxy_init(FsContext *ctx)
>>          sock_id = connect_namedsocket(ctx->fs_root);
>>      } else {
>>          sock_id = atoi(ctx->fs_root);
>> -        if (sock_id < 0) {
>> -            fprintf(stderr, "socket descriptor not initialized\n");
>> -            g_free(proxy);
>> -            return -1;
>> -        }
>> +    }
>> +
>> +    if (sock_id < 0) {
>> +        fprintf(stderr, "socket descriptor not initialized\n");
>> +        g_free(proxy);
>> +        return -1;
>>      }
>>      g_free(ctx->fs_root);
>>      ctx->fs_root = NULL;
> 
> 
> Um. I'm applying 2 patches instead of this one.  First:
> 
> 

Ok, Thanks for your work.

Best regards,
-Gonglei

> 
> virtio-9p-proxy: Fix sockfd leak
> 
> If connect() in connect_namedsocket() return false, the sockfd will leak.
> Plug it.
> 
> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
> 
> diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c
> index b57966d..e6bbb06 100644
> --- a/hw/9pfs/virtio-9p-proxy.c
> +++ b/hw/9pfs/virtio-9p-proxy.c
> @@ -1112,6 +1112,7 @@ static int connect_namedsocket(const char *path)
>      size = strlen(helper.sun_path) + sizeof(helper.sun_family);
>      if (connect(sockfd, (struct sockaddr *)&helper, size) < 0) {
>          fprintf(stderr, "socket error\n");
> +        close(sockfd);
>          return -1;
>      }
> 
> 
> 
> And second.  Note the slight change in logic and error messages
> compared with your version - there's no need to print error
> message twice if connect_namedsocket() returned -1 (it already
> printed error message).
> 
> virtio-9p-proxy: fix error return in proxy_init()
> 
> proxy_init() does not check the return value of connect_namedsocket(),
> fix this by rearranging code a little bit.
> 
> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
> 
> diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c
> index e6bbb06..2ec211b 100644
> --- a/hw/9pfs/virtio-9p-proxy.c
> +++ b/hw/9pfs/virtio-9p-proxy.c
> @@ -1155,10 +1155,12 @@ static int proxy_init(FsContext *ctx)
>          sock_id = atoi(ctx->fs_root);
>          if (sock_id < 0) {
>              fprintf(stderr, "socket descriptor not initialized\n");
> -            g_free(proxy);
> -            return -1;
>          }
>      }
> +    if (sock_id < 0) {
> +        g_free(proxy);
> +        return -1;
> +    }
>      g_free(ctx->fs_root);
>      ctx->fs_root = NULL;
> 
> 
> And I'll immediately send another followup patch to improve
> error messages in connect_namedsocket(), -- these are awful.
> 
> /mjt

  reply	other threads:[~2014-10-30 10:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-29 10:52 [Qemu-trivial] [PATCH] virtio-9p-proxy: Fix sockfd leak and modify the check logic arei.gonglei
2014-10-29 10:52 ` [Qemu-devel] " arei.gonglei
2014-10-30  8:03 ` [Qemu-trivial] " Michael Tokarev
2014-10-30  8:03   ` [Qemu-devel] " Michael Tokarev
2014-10-30 10:51   ` Gonglei [this message]
2014-10-30 10:51     ` Gonglei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=54521836.3070701@huawei.com \
    --to=arei.gonglei@huawei.com \
    --cc=aneesh.kumar@linux.vnet.ibm.com \
    --cc=mjt@tls.msk.ru \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@nongnu.org \
    --cc=weidong.huang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.