Received: by 10.223.176.5 with SMTP id f5csp388583wra; Tue, 30 Jan 2018 13:11:55 -0800 (PST) X-Google-Smtp-Source: AH8x224Bi0vPhtG0wjsrQy7j1HDZM19z/h/UoyOMVVkbO/zyIfmZmqv9SPLoV0OJkhzbcbRXRYkF X-Received: by 2002:a17:902:8c8b:: with SMTP id t11-v6mr26075426plo.286.1517346715084; Tue, 30 Jan 2018 13:11:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517346715; cv=none; d=google.com; s=arc-20160816; b=u5zSdyKhugCYeHpL8H9M+53e8YlaqbFN1nKvbMChXRTAdNQkhW9RscWgdYqETz6vjE rvVXt8pdKj7o+cJB7yamiZ1o9kHS1PJUvr+9XZ7NkyoMYbgCBwfnwLHcH0O1TSvMY9DF vwEObz3OwxAsK5aIPh/e0+lzeCEX9vqy34FXb/egGp7eNkvfM+ruEnax62+BaAtVwLuP 4eQDB1Me1/GouSBPsnwRy8vkSuyHqpf8BdEVfQpJJ/JlDfPGufVHURxerpw+HCMz6Nzy m+mJkyl1jCfQc2mpv9mGDPWGBakGaGE5bRpi1kkPqCoIoVDBpwOd3ovJVAIE7qNOHxPo zzAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=ZvRsfOXmyicHb4tXq0A3NwBjm1A7EKwqoqZCH8T3vIY=; b=1KYc5dJrStY+2e4Tlyzc9SlIBJ5qu3jlYuU8lfs6RXJYmZ3Fh8aow2SN/5HHCR3zrG bpSamjIiob/M+nEkZDJ0ozIsiz6VWStb4/+v9AVn8uqXHoxPsxM7L68xaZeneegQjWWt P58NaqH5teDltN5IyTcmWdU/v8WmPb6gE/4ENB8CoQZcsrblFhLXhuyeTRuk6WMmAACR YRe3Ag7mSHc2IZSI1raUyvb4G7ERW+s1bKJ1UiZ8G7BfHq80F3/9tcdApeSpep7JHZU8 N0uzCWuoxned/S4F/qdb5EBTo3KCiTqIcfTFGZALww0Het7EyWZjuz/7kkVBmK9ctcFM nnzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=AAigAZ0A; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m26si1551549pgc.375.2018.01.30.13.11.40; Tue, 30 Jan 2018 13:11:55 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=AAigAZ0A; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752877AbeA3Utc (ORCPT + 99 others); Tue, 30 Jan 2018 15:49:32 -0500 Received: from mail-it0-f65.google.com ([209.85.214.65]:54813 "EHLO mail-it0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752027AbeA3UtZ (ORCPT ); Tue, 30 Jan 2018 15:49:25 -0500 Received: by mail-it0-f65.google.com with SMTP id k131so2290602ith.4; Tue, 30 Jan 2018 12:49:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=ZvRsfOXmyicHb4tXq0A3NwBjm1A7EKwqoqZCH8T3vIY=; b=AAigAZ0AK73Z7GTnzgTY+R1bEDXszrmtxgHt3QI6sg/DYFg+I/zX+0ajjPAw+6gmQf S9KV73h4IGtyvpBe+FSFv0k7bmjbhnP1dinGVRG+W595967PWY03SYHtx4FsGZriqPKT +5TdNOdOKGPzOXWs516RlzFoKRecEmV2R2HvU9hmu8BVr7WxZ6QNyilKX3KTasGX2waT B51IbNjoeWL6j1V6TYeNNWVbpfaUjcvMTqkUV5mtfpJ9X7oVZ3oSuM+ULctBx3vcDqYr tN+HVfQh8wgNB1jkEe7T9yhK5pwx5koA/jfQGIxGY0lPFeIjQDB81CTE15g9upMYCENV 09gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=ZvRsfOXmyicHb4tXq0A3NwBjm1A7EKwqoqZCH8T3vIY=; b=RfwlSXrIuX+ICDRNJCaYi9kQKsa6xCd6YvUSK8Z3h0VzL+9YBMhyZRKkdtwZriJD48 B8WowDNYHf0QI+kclkf34V0ydlpKtVkyxPv4iPzyY6rWA6s/0QxH7ItxH+tckQ9RydjJ iEj/lvCaz9qmjcXnEauwE9N41Fq0ZdB8KtB8GYt2GykHVLQP8jP+88wyq5FtIcmJ/CIt 02MK6aZWie2gviFQU+8XCwuumXy/+hYlWIbzUqVOZm2z5SUmGofdfj2v6WmiVHPzODfB hcYC+tMurhWMo5ZYlLDpCcJ5YOlEvjxMYudLc6D43ed8G6crR3nQlu00GtVc5i78WEHR NRKw== X-Gm-Message-State: AKwxytcQ6qr26DgHLkd05mJddEuPRd+F1weIrfmfweg0SyKW/u6jWT8j Ya4a2e0GPSIZx4qrJqZ42RA= X-Received: by 10.36.123.137 with SMTP id q131mr31835781itc.121.1517345364328; Tue, 30 Jan 2018 12:49:24 -0800 (PST) Received: from gmail.com ([2620:15c:17:3:dc28:5c82:b905:e8a8]) by smtp.gmail.com with ESMTPSA id d202sm6412319iod.73.2018.01.30.12.49.23 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 30 Jan 2018 12:49:23 -0800 (PST) Date: Tue, 30 Jan 2018 12:49:21 -0800 From: Eric Biggers To: Wanpeng Li Cc: kvm , Paolo Bonzini , Radim =?utf-8?B?S3LEjW3DocWZ?= , linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com, Eric Biggers , "# v3 . 10+" , Alex Williamson Subject: Re: [PATCH] KVM: prevent overlap between user and private memslots Message-ID: <20180130204921.trzixusqdorm4a7w@gmail.com> References: <94eb2c06f65e7ece95055cf1aafd@google.com> <20180119081820.30803-1-ebiggers3@gmail.com> <20180119185716.rmjh7nts6cubkvgw@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180119185716.rmjh7nts6cubkvgw@gmail.com> User-Agent: NeoMutt/20170609 (1.8.3) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 19, 2018 at 10:57:16AM -0800, Eric Biggers wrote: > +Cc alex.williamson@redhat.com > > On Fri, Jan 19, 2018 at 05:03:47PM +0800, Wanpeng Li wrote: > > 2018-01-19 17:01 GMT+08:00 Wanpeng Li : > > > 2018-01-19 16:18 GMT+08:00 Eric Biggers : > > >> From: Eric Biggers > > >> > > >> 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 > > >> #include > > >> #include > > >> > > >> 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 > > >> Fixes: e0d62c7f4860 ("KVM: Add kernel-internal memory slots") > > >> Cc: # v2.6.25+ > > >> Signed-off-by: Eric Biggers > > > > > > Please refer to this one. https://patchwork.kernel.org/patch/9645377/ > > > > https://lkml.org/lkml/2017/3/27/57 > > > > Ah, so this was reported before, and you sent the same fix. Well, it was never > applied, so the bug is still there, and anyone who can use /dev/kvm can trigger > it. So one of these patches needs to be applied, unless there is a better fix. > > I don't agree with the "Fixes:" line in your version of the patch. The bug was > actually there prior to 5419369ed, which might explain why that commit seemed to > preserve the behavior intentionally. (Note that KVM_MEMORY_SLOTS did not > include the private memory slots; it was later renamed to KVM_USER_MEM_SLOTS.) > > Eric Ping. Paolo or Radim, can you please consider applying one of these patches? Eric