From patchwork Thu Jun 8 08:22:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 9774215 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 B5D7F6034B for ; Thu, 8 Jun 2017 08:22:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A213F23B24 for ; Thu, 8 Jun 2017 08:22:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 94E64267EC; Thu, 8 Jun 2017 08:22:58 +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=unavailable 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 47A6428554 for ; Thu, 8 Jun 2017 08:22:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751908AbdFHIWa (ORCPT ); Thu, 8 Jun 2017 04:22:30 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:34773 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751637AbdFHIW1 (ORCPT ); Thu, 8 Jun 2017 04:22:27 -0400 Received: by mail-pf0-f194.google.com with SMTP id u1so4314272pfg.1; Thu, 08 Jun 2017 01:22:17 -0700 (PDT) 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=ENvaphPt3Qh8szpA3AZj4GgAMc6yBdbAiffB/m2N92Y=; b=orkKQtqUHSyI929eVLQF9kOtnY6Z4KW3+geUEr5/NMJhFqeiLXnoFtINESjPiNac+Z OWjjizaoA8tZlw99ZNTesH53e51qXbPZ+ym6IAbnsfeKEmIBQjiGgIvg8Z0B49+h8LBN +FU1CwBFsk53beqi3VkgGvNVf8j6jH5Le4NldSte7JNn2tyeKQwWDNsnyAf+rWFHBW2I nYVW4UiZIaHD4xWsmekqK2C8gdKZkt5F3GEIeVgmyNzC9WdIB5SuYiKos3VY1mOZi115 eoq1LnANHBscSH4IIEkO0z8j7UGq9nSgO4/E9oC5nGT/uv5EkoNDD3Vd0wAJvpSlN2gG IxQg== 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=ENvaphPt3Qh8szpA3AZj4GgAMc6yBdbAiffB/m2N92Y=; b=M+r2XxfGDvTTNEDhocKFEr+zdfJGNNh/VBuvlTd4V3nLPelEwLbmXkrrgLTQqphY2v yJNUHEbZnag06gvtNingRItAkArfnyu3bwUYxULZlWHfRBRYHrz9o1nzKZbVOhvKKUVG ELS13ENk71Nc9Vw4U79CJ3Z5wyNYS9WEywDdLyAu8ZRmxONF7r3gv3UisrN3gUZ4uQc3 mkD4CLDMTJaptg3nQT0+ezi0d+xM/2Jm0T01cfkTt60PVmde8jmBYelcIbGkRoEMoNyu BOZClLPgsXNsiT3VyWAv471U9BmQse2rtd70mHsL19+bk4JCODY+iOqhqIlYDvE5Z6EV +gjw== X-Gm-Message-State: AODbwcAyEqLZQRsRIX/FjoSKQY8mdCgUTOBriymc7k7IHeKPczJJcknt 9GQE9luNZOvYv6L+ X-Received: by 10.84.196.129 with SMTP id l1mr29054419pld.109.1496910131736; Thu, 08 Jun 2017 01:22:11 -0700 (PDT) Received: from localhost ([203.205.141.123]) by smtp.gmail.com with ESMTPSA id y2sm8621886pfk.1.2017.06.08.01.22.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 01:22:11 -0700 (PDT) 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 , Moguofang , stable@vger.kernel.org Subject: [PATCH v2 1/2] KVM: cpuid: Fix read/write out-of-bounds vulnerability in cpuid emulation Date: Thu, 8 Jun 2017 01:22:07 -0700 Message-Id: <1496910128-87057-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 If "i" is the last element in the vcpu->arch.cpuid_entries[] array, it potentially can be exploited the vulnerability. this will out-of-bounds read and write the unused memory in host OS. As Paolo pointed: >> /* when no next entry is found, the current entry[i] is reselected */ >> - for (j = i + 1; ; j = (j + 1) % nent) { >> - struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; >> - if (ej->function == e->function) { > >It reads ej->maxphyaddr, which is user controlled. > >> - ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; > >After cpuid_entries there is > > int maxphyaddr; > struct x86_emulate_ctxt emulate_ctxt; /* 16-byte aligned */ > >So indeed we have: > >- cpuid_entries at offset 1B50 (6992) >- maxphyaddr at offset 27D0 (6992 + 3200 = 10192) >- padding at 27D4...27DF >- emulate_ctxt at 27E0 > >So this indeed writes in the padding. Pfew, writing the ops field of >emulate_ctxt would have been much worse. This patch fixes it by modding the index to avoid the out-of-bounds. At the worst case, i == j and ej->function == e->function, the loop can bail out. Reported-by: Moguofang Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Moguofang Cc: stable@vger.kernel.org Signed-off-by: Wanpeng Li --- v1 -> v2: * update patch description arch/x86/kvm/cpuid.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index a181ae7..b927a42 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -779,19 +779,20 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) { + int j = i, nent = vcpu->arch.cpuid_nent; struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; - int j, nent = vcpu->arch.cpuid_nent; + struct kvm_cpuid_entry2 *ej; e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; /* when no next entry is found, the current entry[i] is reselected */ - for (j = i + 1; ; j = (j + 1) % nent) { - struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; - if (ej->function == e->function) { - ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; - return j; - } - } - return 0; /* silence gcc, even though control never reaches here */ + do { + j = (j + 1) % nent; + ej = &vcpu->arch.cpuid_entries[j]; + } while(ej->function != e->function); + + ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; + + return j; } /* find an entry with matching function, matching index (if needed), and that