qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: zhanghailiang <zhang.zhanghailiang@huawei.com>
To: Paolo Bonzini <pbonzini@redhat.com>, qemu-devel@nongnu.org
Cc: mdroth@linux.vnet.ibm.com, armbru@redhat.com,
	qemu-stable@nongnu.org, luonengjun@huawei.com,
	peter.huangpeng@huawei.com, lcapitulino@redhat.com
Subject: Re: [Qemu-devel] [PATCH v2] qga: Rewrite code where using readdir_r
Date: Sun, 28 Sep 2014 14:06:24 +0800	[thread overview]
Message-ID: <5427A560.5040302@huawei.com> (raw)
In-Reply-To: <542588D8.30701@redhat.com>

On 2014/9/26 23:40, Paolo Bonzini wrote:
> Il 19/09/2014 05:09, zhanghailiang ha scritto:
>> If readdir_r fails, error_setg_errno will reference the freed
>> pointer *dirpath*.
>>
>> Moreover, readdir_r may cause a buffer overflow, using readdir instead.
>>
>> Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
>> ---
>>   v2:
>> - Switch readdir_r to readdir (Comment of Eric Blake)
>> ---
>>   qga/commands-posix.c | 27 +++++++++++++++------------
>>   1 file changed, 15 insertions(+), 12 deletions(-)
>>
>> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
>> index 7eed7f4..f6f3e3c 100644
>> --- a/qga/commands-posix.c
>> +++ b/qga/commands-posix.c
>> @@ -956,7 +956,7 @@ static void build_guest_fsinfo_for_virtual_device(char const *syspath,
>>   {
>>       DIR *dir;
>>       char *dirpath;
>> -    struct dirent entry, *result;
>> +    struct dirent *entry;
>>
>>       dirpath = g_strdup_printf("%s/slaves", syspath);
>>       dir = opendir(dirpath);
>> @@ -965,22 +965,24 @@ static void build_guest_fsinfo_for_virtual_device(char const *syspath,
>>           g_free(dirpath);
>>           return;
>>       }
>> -    g_free(dirpath);
>>
>>       for (;;) {
>> -        if (readdir_r(dir, &entry, &result) != 0) {
>> -            error_setg_errno(errp, errno, "readdir_r(\"%s\")", dirpath);
>> -            break;
>> -        }
>> -        if (!result) {
>> +        errno = 0;
>> +        entry = readdir(dir);
>> +        if (entry == NULL) {
>> +            if (errno) {
>> +                error_setg_errno(errp, errno, "readdir(\"%s\")", dirpath);
>> +            }
>>               break;
>>           }
>>
>> -        if (entry.d_type == DT_LNK) {
>> -            g_debug(" slave device '%s'", entry.d_name);
>> -            dirpath = g_strdup_printf("%s/slaves/%s", syspath, entry.d_name);
>> -            build_guest_fsinfo_for_device(dirpath, fs, errp);
>> -            g_free(dirpath);
>> +        if (entry->d_type == DT_LNK) {
>> +            char *path;
>> +
>> +            g_debug(" slave device '%s'", entry->d_name);
>> +            path = g_strdup_printf("%s/slaves/%s", syspath, entry->d_name);
>> +            build_guest_fsinfo_for_device(path, fs, errp);
>> +            g_free(path);
>>
>>               if (*errp) {
>>                   break;
>> @@ -988,6 +990,7 @@ static void build_guest_fsinfo_for_virtual_device(char const *syspath,
>>           }
>>       }
>>
>> +    g_free(dirpath);
>>       closedir(dir);
>>   }
>>
>>
>
> Thanks,
>
> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
>
> Michael Roth will pick this up.
>

OK, Thanks!

> Paolo
>
> .
>

  reply	other threads:[~2014-09-28  6:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-19  3:09 [Qemu-devel] [PATCH v2] qga: Rewrite code where using readdir_r zhanghailiang
2014-09-26  8:32 ` zhanghailiang
2014-09-26 15:40 ` Paolo Bonzini
2014-09-28  6:06   ` zhanghailiang [this message]
2014-09-26 15:45 ` Eric Blake
2014-10-01 17:47 ` Michael Roth

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=5427A560.5040302@huawei.com \
    --to=zhang.zhanghailiang@huawei.com \
    --cc=armbru@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=luonengjun@huawei.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.huangpeng@huawei.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).