All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: Eric Blake <eblake@redhat.com>, qemu-devel@nongnu.org
Cc: kwolf@redhat.com, zhang.zhanghailiang@huawei.com,
	qemu-block@nongnu.org, quintela@redhat.com, armbru@redhat.com,
	dgilbert@redhat.com, marcandre.lureau@redhat.com, den@openvz.org,
	mreitz@redhat.com, jsnow@redhat.com, mdroth@linux.vnet.ibm.com
Subject: Re: [PATCH 6/6] qga/commands-posix: fix use after free of local_err
Date: Wed, 25 Mar 2020 07:28:02 +0300	[thread overview]
Message-ID: <3e28eab6-a7c8-a9b3-84bf-b98e241139cc@virtuozzo.com> (raw)
In-Reply-To: <012d4cf0-e168-a9ea-273a-a683e50ef7a0@redhat.com>

24.03.2020 23:03, Eric Blake wrote:
> On 3/24/20 10:36 AM, Vladimir Sementsov-Ogievskiy wrote:
>> local_err is used several times in guest_suspend(). Setting non-NULL
>> local_err will crash, so let's zero it after freeing. Also fix possible
>> leak of local_err in final if().
>>
>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
>> ---
>>   qga/commands-posix.c | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
>> index 93474ff770..cc69b82704 100644
>> --- a/qga/commands-posix.c
>> +++ b/qga/commands-posix.c
>> @@ -1773,6 +1773,7 @@ static void guest_suspend(SuspendMode mode, Error **errp)
>>       }
>>       error_free(local_err);
>> +    local_err = NULL;
> 
> Let's show this with more context.
> 
>> static void guest_suspend(SuspendMode mode, Error **errp)
>> {
>>     Error *local_err = NULL;
>>     bool mode_supported = false;
>>
>>     if (systemd_supports_mode(mode, &local_err)) {
> 
> Hmm - we have an even earlier bug that needs fixing.  Note that systemd_supports_mode() returns a bool AND conditionally sets errp.  But it is inconsistent: it has the following table of actions based on the results of run_process_child() on "systemctl status" coupled with the man page on "systemctl status" return values:
> -1 (unable to run systemctl) -> errp set, return false
> 0 (unit is active) -> errp left unchanged, return false
> 1 (unit not failed) -> errp left unchanged, return true
> 2 (unused) -> errp left unchanged, return true
> 3 (unit not active) -> errp left unchanged, return true
> 4 (no such unit) -> errp left unchanged, return false
> 5+ (unexpected from systemctl) -> errp left unchanged, return false
> 
> But the comments in systemd_supports_mode() claim that ANY status < 4 (other than -1, which means we did not run systemctl) should count as the service existing, even though the most common status is 3.  If our comment is to be believed, then we should return true, not false, for status 0.
> 
> Now, back to _this_ function:
> 
>>         mode_supported = true;
>>         systemd_suspend(mode, &local_err);
> 
> Okay - if we get here (whether from status 1-3, or with systemd_supports_mode fixed to support status 0-3), local_err is still unset prior to calling systemd_suspend(), and we are guaranteed that after the call, either we suspended successfully or local_err is now set.
> 
>>     }
>>
>>     if (!local_err) {
>>         return;
>>     }
> 
> So if returned, we succeeded at systemd_suspend, and there is nothing further to do; but if we get past that point, we don't know if it was systemd_supports_mode that failed or systemd_suspend that failed, and we don't know if local_err is set.

No, we know that is set, as we check exactly this and return if not set.

> 
>>
>>     error_free(local_err);
>> +    local_err = NULL;
> 
> Yet, we blindly throw away local_err, without trying to report it.  If that's the case, then WHY are we passing in local_err?  Wouldn't it be better to pass in NULL (we really don't care about the error message), and/or fix systemd_suspend() to return a bool just like systemd_supports_mode, and/or fix systemd_supports_mode to guarantee that it sets errp when returning false?
> 

I agree that this is a strange function and its logic is weird. But I don't know what the logic should be. My patch is still valid to just fix obvious use-after-free and possible leak. It doesn't fix the logic.


-- 
Best regards,
Vladimir


  reply	other threads:[~2020-03-25  4:29 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-24 15:36 [PATCH for-5.0 0/6] Several error use-after-free Vladimir Sementsov-Ogievskiy
2020-03-24 15:36 ` [PATCH 1/6] scripts/coccinelle: add error-use-after-free.cocci Vladimir Sementsov-Ogievskiy
2020-03-31  9:00   ` Markus Armbruster
2020-03-31  9:12     ` Vladimir Sementsov-Ogievskiy
2020-03-31 13:14       ` Markus Armbruster
2020-03-31 13:49         ` Vladimir Sementsov-Ogievskiy
2020-03-31 18:38           ` Markus Armbruster
2020-03-31 18:56     ` Peter Maydell
2020-04-01  5:07       ` Markus Armbruster
2020-04-01 11:04         ` Peter Maydell
2020-04-01 14:44           ` Markus Armbruster
2020-04-01 16:12             ` Peter Maydell
2020-04-02  6:55               ` Markus Armbruster
2020-04-02  8:19                 ` Peter Maydell
2020-04-02  8:36                   ` Markus Armbruster
2020-03-24 15:36 ` [PATCH 2/6] block/mirror: fix use after free of local_err Vladimir Sementsov-Ogievskiy
2020-03-24 15:57   ` Eric Blake
2020-03-24 17:10   ` John Snow
2020-03-25 11:11   ` Max Reitz
2020-03-25 11:29     ` Max Reitz
2020-03-25 11:47     ` Vladimir Sementsov-Ogievskiy
2020-03-25 12:00       ` Max Reitz
2020-03-31  9:12         ` Markus Armbruster
2020-03-25 13:01     ` Eric Blake
2020-03-25 12:01   ` Max Reitz
2020-03-24 15:36 ` [PATCH 3/6] dump/win_dump: fix use after free of err Vladimir Sementsov-Ogievskiy
2020-03-30 15:54   ` Markus Armbruster
2020-03-24 15:36 ` [PATCH 4/6] migration/colo: fix use after free of local_err Vladimir Sementsov-Ogievskiy
2020-03-24 19:40   ` Dr. David Alan Gilbert
2020-03-24 15:36 ` [PATCH 5/6] migration/ram: " Vladimir Sementsov-Ogievskiy
2020-03-24 19:41   ` Dr. David Alan Gilbert
2020-03-24 15:36 ` [PATCH 6/6] qga/commands-posix: " Vladimir Sementsov-Ogievskiy
2020-03-24 20:03   ` Eric Blake
2020-03-25  4:28     ` Vladimir Sementsov-Ogievskiy [this message]
2020-03-31 11:46       ` Markus Armbruster
2020-03-31 12:04         ` Vladimir Sementsov-Ogievskiy
2020-03-31  8:13     ` Markus Armbruster
2020-03-24 15:50 ` [PATCH for-5.0 0/6] Several error use-after-free Richard Henderson
2020-04-04 12:18 ` Markus Armbruster

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=3e28eab6-a7c8-a9b3-84bf-b98e241139cc@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=armbru@redhat.com \
    --cc=den@openvz.org \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=zhang.zhanghailiang@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.