All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers3@gmail.com>
To: kvm@vger.kernel.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
	"Radim Krčmář" <rkrcmar@redhat.com>,
	linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com,
	"Eric Biggers" <ebiggers@google.com>,
	stable@vger.kernel.org
Subject: [PATCH] KVM: prevent overlap between user and private memslots
Date: Fri, 19 Jan 2018 00:18:20 -0800	[thread overview]
Message-ID: <20180119081820.30803-1-ebiggers3@gmail.com> (raw)
In-Reply-To: <94eb2c06f65e7ece95055cf1aafd@google.com>

From: Eric Biggers <ebiggers@google.com>

Memslots must not overlap in guest physical memory, since otherwise some
guest physical addresses will not uniquely map to a memslot.  Yet, the
overlap check in __kvm_set_memory_region() allows a memslot that
overlaps one of the "private" memslots, e.g. the memslot reserved for
the TSS on x86.

This seems to be a very old bug that was introduced years ago when
private memory slots were first added.  It seems that later refactoring
incorrectly assumed this bug was intentional and preserved it.

Fix it by removing the loophole for private memslots, so we just check
for overlap against all memslots.

This bug was found by syzkaller, which used a memslot overlap to make
pte_list_remove() be called for the wrong memslot, hitting a BUG():

    pte_list_remove: 000000007185ed42 0->BUG
    kernel BUG at arch/x86/kvm/mmu.c:1209!
    [...]
    RIP: 0010:pte_list_remove+0x107/0x110 arch/x86/kvm/mmu.c:1208
    [...]
    Call Trace:
     mmu_page_zap_pte+0x7e/0xd0 arch/x86/kvm/mmu.c:2499
     kvm_mmu_page_unlink_children arch/x86/kvm/mmu.c:2521 [inline]
     kvm_mmu_prepare_zap_page+0x4f/0x340 arch/x86/kvm/mmu.c:2565
     kvm_zap_obsolete_pages arch/x86/kvm/mmu.c:5348 [inline]
     kvm_mmu_invalidate_zap_all_pages+0xa6/0x100 arch/x86/kvm/mmu.c:5389
     kvm_mmu_notifier_release+0x4f/0x80 arch/x86/kvm/../../../virt/kvm/kvm_main.c:468
     __mmu_notifier_release+0x63/0x100 mm/mmu_notifier.c:75
     mmu_notifier_release include/linux/mmu_notifier.h:244 [inline]
     exit_mmap+0x160/0x170 mm/mmap.c:3009
     __mmput kernel/fork.c:966 [inline]
     mmput+0x44/0xd0 kernel/fork.c:987
     exit_mm kernel/exit.c:544 [inline]
     do_exit+0x24a/0xb50 kernel/exit.c:856
     do_group_exit+0x34/0xb0 kernel/exit.c:972
     SYSC_exit_group kernel/exit.c:983 [inline]
     SyS_exit_group+0xb/0x10 kernel/exit.c:981
     entry_SYSCALL_64_fastpath+0x1e/0x8b

Reproducer:

    #include <fcntl.h>
    #include <linux/kvm.h>
    #include <sys/ioctl.h>

    int main()
    {
            static char buf[4096*3] __attribute__((aligned(4096)));
            int kvm, vm, cpu;
            struct kvm_mp_state mp_state = { KVM_MP_STATE_SIPI_RECEIVED };
            struct kvm_userspace_memory_region memreg = {
                    .memory_size = sizeof(buf),
                    .userspace_addr = (__u64)buf,
            };

            kvm = open("/dev/kvm", O_RDWR);
            vm = ioctl(kvm, KVM_CREATE_VM, 0);
            ioctl(vm, KVM_CREATE_IRQCHIP);
            cpu = ioctl(vm, KVM_CREATE_VCPU, 0);
            ioctl(cpu, KVM_SET_MP_STATE, &mp_state);
            ioctl(vm, KVM_SET_TSS_ADDR, 0);
            ioctl(cpu, KVM_RUN, 0);
            ioctl(vm, KVM_SET_USER_MEMORY_REGION, &memreg);
    }

Reported-by: syzbot <syzkaller@googlegroups.com>
Fixes: e0d62c7f4860 ("KVM: Add kernel-internal memory slots")
Cc: <stable@vger.kernel.org> # v2.6.25+
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 virt/kvm/kvm_main.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 210bf820385a..e536977e7b6d 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -974,8 +974,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
 		/* Check for overlaps */
 		r = -EEXIST;
 		kvm_for_each_memslot(slot, __kvm_memslots(kvm, as_id)) {
-			if ((slot->id >= KVM_USER_MEM_SLOTS) ||
-			    (slot->id == id))
+			if (slot->id == id)
 				continue;
 			if (!((base_gfn + npages <= slot->base_gfn) ||
 			      (base_gfn >= slot->base_gfn + slot->npages)))
-- 
2.16.0

  reply	other threads:[~2018-01-19  8:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-01 20:25 kernel BUG at arch/x86/kvm/mmu.c:LINE! syzbot
2017-11-01 20:25 ` syzbot
2018-01-19  8:18 ` Eric Biggers [this message]
2018-01-19  9:01   ` [PATCH] KVM: prevent overlap between user and private memslots Wanpeng Li
2018-01-19  9:03     ` Wanpeng Li
2018-01-19 18:57       ` Eric Biggers
2018-01-30 20:49         ` Eric Biggers
2018-02-13  3:38           ` Eric Biggers
2018-02-13 14:38             ` Paolo Bonzini

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=20180119081820.30803-1-ebiggers3@gmail.com \
    --to=ebiggers3@gmail.com \
    --cc=ebiggers@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=rkrcmar@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=syzkaller-bugs@googlegroups.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.