From: mark.rutland@arm.com (Mark Rutland)
To: linux-arm-kernel@lists.infradead.org
Subject: kvm/arm64: use-after-free in kvm_unmap_hva_handler/unmap_stage2_pmds
Date: Tue, 18 Apr 2017 10:08:46 +0100 [thread overview]
Message-ID: <20170418090845.GC17866@leverpostej> (raw)
In-Reply-To: <20170418083230.GA17866@leverpostej>
On Tue, Apr 18, 2017 at 09:32:31AM +0100, Mark Rutland wrote:
> Hi Suzuki,
>
> On Thu, Apr 13, 2017 at 04:50:46PM +0100, Suzuki K. Poulose wrote:
> > kvm: Hold reference to the user address space
> >
> > The core KVM code, uses mmgrab/mmdrop to pin the mm struct of the user
> > application. mmgrab only guarantees that the mm struct is available,
> > while the "real address space" (see Documentation/vm/active_mm.txt) may
> > be destroyed. Since the KVM depends on the user space page tables for
> > the Guest pages, we should instead do an mmget/mmput. Even though
> > mmget/mmput is not encouraged for uses with unbounded time, the KVM
> > is fine to do so, as we are doing it from the context of the same process.
> >
> > This also prevents the race condition where mmu_notifier_release() could
> > be called in parallel and one instance could end up using a free'd kvm
> > instance.
> >
> > Cc: Mark Rutland <mark.rutland@arm.com>
> > Cc: Paolo Bonzin <pbonzini@redhat.com>
> > Cc: Radim Kr?m?? <rkrcmar@redhat.com>
> > Cc: Marc Zyngier <marc.zyngier@arm.com>
> > Cc: Christoffer Dall <christoffer.dall@linaro.org>
> > Cc: andreyknvl at google.com
> > Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
> > ---
> > virt/kvm/kvm_main.c | 6 +++---
> > 1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> > index 88257b3..555712e 100644
> > --- a/virt/kvm/kvm_main.c
> > +++ b/virt/kvm/kvm_main.c
> > @@ -613,7 +613,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
> > return ERR_PTR(-ENOMEM);
> >
> > spin_lock_init(&kvm->mmu_lock);
> > - mmgrab(current->mm);
> > + mmget(current->mm);
> > kvm->mm = current->mm;
> > kvm_eventfd_init(kvm);
> > mutex_init(&kvm->lock);
> > @@ -685,7 +685,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
> > for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
> > kvm_free_memslots(kvm, kvm->memslots[i]);
> > kvm_arch_free_vm(kvm);
> > - mmdrop(current->mm);
> > + mmput(current->mm);
> > return ERR_PTR(r);
> > }
> >
> > @@ -747,7 +747,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
> > kvm_arch_free_vm(kvm);
> > preempt_notifier_dec();
> > hardware_disable_all();
> > - mmdrop(mm);
> > + mmput(mm);
> > }
>
>
> As a heads-up, I'm seeing what looks to be a KVM memory leak with this
> patch applied atop of next-20170411.
>
> I don't yet know if this is a problem with next-20170411 or this patch
> in particular -- I will try to track that down. In the mean time, info
> dump below.
>
> I left syzkaller running over the weekend using this kernel on the host,
> and OOM kicked in after it had been running for a short while. Almost
> all of my memory is in use, but judging by top, almost none of this is
> associated with processes.
FWIW, it seems easy enough to trigger this leak with kvmtool. Start a
guest that'll allocate a tonne of memory:
$ lkvm run --console virtio -m 4096 --kernel Image \
-p "memtest=1 console=hvc0,38400 earlycon=uart,mmio,0x3f8"
... then kill this with a SIGKILL from your favourite process management
tool.
Also, if the guest exits normally, everything appears to be cleaned up
correctly. So it looks like this is in some way related to our fatal
signal handling.
Thanks,
Mark.
next prev parent reply other threads:[~2017-04-18 9:08 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-10 13:34 kvm/arm64: use-after-free in kvm_unmap_hva_handler/unmap_stage2_pmds Andrey Konovalov
2017-03-14 11:07 ` Suzuki K Poulose
2017-03-14 16:57 ` Paolo Bonzini
2017-04-12 16:19 ` Andrey Konovalov
2017-04-12 18:43 ` Marc Zyngier
2017-04-12 18:51 ` Andrey Konovalov
2017-04-13 9:34 ` Mark Rutland
2017-04-13 11:53 ` Andrey Konovalov
2017-04-13 13:45 ` Mark Rutland
2017-04-13 9:17 ` Suzuki K Poulose
2017-04-13 15:50 ` Suzuki K. Poulose
2017-04-13 15:53 ` Suzuki K Poulose
2017-04-13 17:06 ` Andrey Konovalov
2017-04-18 8:32 ` Mark Rutland
2017-04-18 9:08 ` Mark Rutland [this message]
2017-04-18 10:30 ` Suzuki K Poulose
2017-04-20 16:40 ` Suzuki K Poulose
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=20170418090845.GC17866@leverpostej \
--to=mark.rutland@arm.com \
--cc=linux-arm-kernel@lists.infradead.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