From patchwork Tue Dec 12 08:57:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 10106517 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3FF186032B for ; Tue, 12 Dec 2017 08:58:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3263C29B2F for ; Tue, 12 Dec 2017 08:58:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2770C29B39; Tue, 12 Dec 2017 08:58:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C009329B2F for ; Tue, 12 Dec 2017 08:58:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752273AbdLLI6K (ORCPT ); Tue, 12 Dec 2017 03:58:10 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:46446 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752323AbdLLI6J (ORCPT ); Tue, 12 Dec 2017 03:58:09 -0500 Received: by mail-pf0-f195.google.com with SMTP id c204so13716077pfc.13; Tue, 12 Dec 2017 00:58:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=fXuI9xZ6eCrkVvzXeScwk+YMWIPCRMLOfeUx8LJ8hgg=; b=Fx+2A8cs6am699yAw2ItmE6xJ6e3sWkY2u6u6KXweMFJKhvdC8SFN32o0VI3fQYiFW tZxY6GLrOefRpjXNalb+Gx7klYxTmaEp9/PdjDOXPzn1rd/k1Sotathu37OE291tpS7c jKproqalqKqqKwoxkHJ0+Clo0UeE0GCuDexmWXTIRtKdmCxN45j547XbQCy/XWxfegTV aaukrv8tOCPD4CmQPs4MXk6oEFqyEz2Fn5ZCz98FkKLxQq04O6wdF5x+n2afRfJr+v7X Mfyc5Sr4S3zlTY39SPAaQzDw1m+Q9qOdNWHHQAnB9CI3JNVyekDooGoUGiIMB5v9mLcw 48cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=fXuI9xZ6eCrkVvzXeScwk+YMWIPCRMLOfeUx8LJ8hgg=; b=kD3IiaSxNbFDQ8Xo68E0ee0p5ckN31gakzPlreRGw5YpCYraJgfFycBibQYlYBjuQv qHBcavbhQd1rZlIWaef+du9fkNplnUJ2kktUibg4gozBHC77AO8vh82clMTM2kpjXa/F w0qAyuezHjiBSemJtsy8E72IFbN2byO8mxJMoZvpVaA70HBE1PZTUfVYO4nKu/s8Mdvk AQGGJdcenbXyJQmlLD5LCaryveLdu4/b6m2ka0OEsafxukYRkon3qyzGw7Sw5YyGLTDh diG7YbVIVjiRY8z9MCs+SSB7GG3h1RywzvsJtpc0uxzGy+419K5E1ZEdxf4NuvnDzTvN WaRQ== X-Gm-Message-State: AKGB3mLnd6OnWEIogR+JXJov8fvMiYBvw97TWHfqfwFIXEP2A3tV2g+f 0dnQNsH2uMVuSO4z0NXvZHB6EQ== X-Google-Smtp-Source: ACJfBouti5D9pfkw5RYlI96B2nZt0tzQDC1iHDnkV5IRbrTp1z8nYxfOQ3KYlcXzLx+1QxEBwlu/iA== X-Received: by 10.159.230.16 with SMTP id u16mr1563908plq.41.1513069088399; Tue, 12 Dec 2017 00:58:08 -0800 (PST) Received: from localhost ([45.248.77.205]) by smtp.gmail.com with ESMTPSA id k8sm27779030pgs.28.2017.12.12.00.58.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Dec 2017 00:58:07 -0800 (PST) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Wanpeng Li Subject: [PATCH RESEND] KVM: X86: Fix stack-out-of-bounds read in write_mmio Date: Tue, 12 Dec 2017 00:57:58 -0800 Message-Id: <1513069078-63339-1-git-send-email-wanpeng.li@hotmail.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wanpeng Li Reported by syzkaller: BUG: KASAN: stack-out-of-bounds in write_mmio+0x11e/0x270 [kvm] Read of size 8 at addr ffff8803259df7f8 by task syz-executor/32298 CPU: 6 PID: 32298 Comm: syz-executor Tainted: G OE 4.15.0-rc2+ #18 Hardware name: LENOVO ThinkCentre M8500t-N000/SHARKBAY, BIOS FBKTC1AUS 02/16/2016 Call Trace: dump_stack+0xab/0xe1 print_address_description+0x6b/0x290 kasan_report+0x28a/0x370 write_mmio+0x11e/0x270 [kvm] emulator_read_write_onepage+0x311/0x600 [kvm] emulator_read_write+0xef/0x240 [kvm] emulator_fix_hypercall+0x105/0x150 [kvm] em_hypercall+0x2b/0x80 [kvm] x86_emulate_insn+0x2b1/0x1640 [kvm] x86_emulate_instruction+0x39a/0xb90 [kvm] handle_exception+0x1b4/0x4d0 [kvm_intel] vcpu_enter_guest+0x15a0/0x2640 [kvm] kvm_arch_vcpu_ioctl_run+0x549/0x7d0 [kvm] kvm_vcpu_ioctl+0x479/0x880 [kvm] do_vfs_ioctl+0x142/0x9a0 SyS_ioctl+0x74/0x80 entry_SYSCALL_64_fastpath+0x23/0x9a The path of patched vmmcall will patch 3 bytes opcode 0F 01 C1(vmcall) to the guest memory, however, write_mmio tracepoint always prints 8 bytes through *(u64 *)val since kvm splits the mmio access into 8 bytes. This can result in stack-out-of-bounds read due to access the extra 5 bytes. This patch fixes it by just accessing the bytes which we operates on. Before patch: syz-executor-5567 [007] .... 51370.561696: kvm_mmio: mmio write len 3 gpa 0x10 val 0x1ffff10077c1010f After patch: syz-executor-13416 [002] .... 51302.299573: kvm_mmio: mmio write len 3 gpa 0x10 val 0xc1010f Reported-by: Dmitry Vyukov Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li --- arch/x86/kvm/x86.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bc5d853..51e7932 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4690,7 +4690,10 @@ static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val) { - trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val); + u64 data = 0; + + memcpy(&data, val, min(8, bytes)); + trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, data); return vcpu_mmio_write(vcpu, gpa, bytes, val); }