Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp220731imm; Wed, 29 Aug 2018 19:04:55 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZ79lhD9XvWDg6SIM5DPzRm2FSs2dnnbgf7nfnMNi8ckq+SCBPC/kp1GZHVXqvIKLzXLhrk X-Received: by 2002:a17:902:26c:: with SMTP id 99-v6mr8220258plc.341.1535594695735; Wed, 29 Aug 2018 19:04:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535594695; cv=none; d=google.com; s=arc-20160816; b=wc4zkU0ew8dgVwB6Dn/uSTN4rXbVOE6h7Rc5U0nWbUmsB9lUaVSxXbVuaqSx6nyqwa gn1HZO1YfC3JeJDNdcK06y14/Y9QELr+rnpVfxqcuziAxLgMcxC7Z1d1c0t0hrLrW8hs lkyqyQ7P9NPRT03GUHHZiWAlZzzTxNou8yUbxvKMPPj/SxAEVC9ck4587X2ofOu9NCfl sWApYkPEIGpr2HJCxj/c4/wAR7nNiLawkn2yB/u7tAgJTEsYxdcAi7Nw/ob5r4uzgHGp 7kRcm77LwXHLjDefUmS5L/R1wjHF04oz1LmHaqtZEygmbzwEOSBFVy7jQ+S+UvuJz94g rl5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=ZriRi6qAj14t1p4MRdPKPXTSGBiamfNqVcP9ENPbS/M=; b=AoqYhmdBX43pHoGstMkiFQ96Bp+AfRC6Uz25mj/lywodp6ZL+gMGoO1GbCQQslcCwD MMncZkVCyd50h45rHG0vpyVlfzoiW9wsPqeOACKmX1nKxLUDfCY9D9EP4Pfr0w5q2ptm CJFwqcSwxqxt2gq3wHmg+Kgyf9CLvEinJqDmdnKjg2FzgbFWUs0dNdLz+nRNjgb+r7IA Kcu0yM8fCkeiXnwLj2sRqkbgLHSOYDT56S23OCh8yNLTkr/9jgYqD5821N94ZNcaNw6B Ab4FlBYv7pD5TX0LObmAagaCzuCoOsT2Fk+xl+54yJrZdMzZncXutrhBBZefAVBg6xHI 5Ecw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="PyZ9K/mi"; 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=QUARANTINE 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 o18-v6si5406285pgn.190.2018.08.29.19.04.40; Wed, 29 Aug 2018 19:04:55 -0700 (PDT) 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="PyZ9K/mi"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727245AbeH3GDU (ORCPT + 99 others); Thu, 30 Aug 2018 02:03:20 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:42225 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725996AbeH3GDT (ORCPT ); Thu, 30 Aug 2018 02:03:19 -0400 Received: by mail-pf1-f194.google.com with SMTP id l9-v6so3125006pff.9; Wed, 29 Aug 2018 19:03:35 -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=ZriRi6qAj14t1p4MRdPKPXTSGBiamfNqVcP9ENPbS/M=; b=PyZ9K/miH87VO1ITKdhZ17dZswYPMdQEyVt5OZM6l5MF2f3Hkld46fO5Nw/74K9xiy qicJnBpvsrmIt/iV1PlUjACyHnItPckY7oza6faGVa+FzdRCCX/PjIY6lv9gY/LwAYbd CVywRw6Rq5DHnkk6Kzq2ja8HbN17prafKeUMeW2MQ+KKG16dwHG4O449ujC1siahYOe+ ZZpe4qA52HbU/2EnNFUkyqa41A+z3gF+QlE53iRfue6Br9Noz6OnQCKZpmlLbpEVNrAc iAi2Y1dlo7u8axgngP+emXh3HiKBTTvGJ2ka+MDKWWlTZY3+rtWFscmLrkCJXH+Sw4F4 u+7Q== 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=ZriRi6qAj14t1p4MRdPKPXTSGBiamfNqVcP9ENPbS/M=; b=S3NWuldwRwDT8OlULek/1sJv1Hde0m9j5TgQsn3CLFrnWMMRExLJwZYDqepkIzdwFE h1TZqN6fs5+YmVXtBh2mVO3id9yQ4qrB6Q+IF8pKzs0yHumFxkyIasYj1kiP4VTCSfrI bn/jraRxuFRzPDCq2btOdPd3s7d+yzJcuaN19O7uSd6H++6K3wL8Vjuj1x1BgrIE0dxa 3HoOiTiKQ/7xksz7nuuuvdolk4EvD3/F7YU77OheRf9iH7bhDDK2LJyHyaWPGYi1r0Qy PY2znKG+7Zui9+NOrAePz9AT7UW8dTZOHFIfQgDcH512rDWCM/uGxyty0abkiK6DT4Bd 0QDg== X-Gm-Message-State: APzg51BFwt/R2rUlsT1e3oldV8li3nn5xbdxFrXJ8eLUypivdPOJ8Pug waSOy+Vum5iYraVm2nIeteg6HAIj X-Received: by 2002:a63:225f:: with SMTP id t31-v6mr7770713pgm.275.1535594615054; Wed, 29 Aug 2018 19:03:35 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id b18-v6sm11433523pgk.15.2018.08.29.19.03.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 29 Aug 2018 19:03:34 -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?= , Liran Alon , Dan Carpenter Subject: [PATCH v2] KVM: LAPIC: Fix pv ipis out-of-bounds access Date: Thu, 30 Aug 2018 10:03:30 +0800 Message-Id: <1535594610-9095-1-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wanpeng Li Dan Carpenter reported that the untrusted data returns from kvm_register_read() results in the following static checker warning: arch/x86/kvm/lapic.c:576 kvm_pv_send_ipi() error: buffer underflow 'map->phys_map' 's32min-s32max' KVM guest can easily trigger this by executing the following assembly sequence in Ring0: mov $10, %rax mov $0xFFFFFFFF, %rbx mov $0xFFFFFFFF, %rdx mov $0, %rsi vmcall As this will cause KVM to execute the following code-path: vmx_handle_exit() -> handle_vmcall() -> kvm_emulate_hypercall() -> kvm_pv_send_ipi() which will reach out-of-bounds access. This patch fixes it by adding a check to kvm_pv_send_ipi() against map->max_apic_id, ignoring destinations that are not present and delivering the rest. We also check whether or not map->phys_map[min + i] is NULL since the max_apic_id is set to the max apic id, some phys_map maybe NULL when apic id is sparse, especially kvm unconditionally set max_apic_id to 255 to reserve enough space for any xAPIC ID. Reported-by: Dan Carpenter Reviewed-by: Liran Alon Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Liran Alon Cc: Dan Carpenter Signed-off-by: Wanpeng Li --- v1 -> v2: * add min > map->max_apic_id check * change min to u32 * add min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1)) arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/lapic.c | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 00ddb0c..e6534b3 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1463,7 +1463,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event); void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu); int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low, - unsigned long ipi_bitmap_high, int min, + unsigned long ipi_bitmap_high, u32 min, unsigned long icr, int op_64_bit); u64 kvm_get_arch_capabilities(void); diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 0cefba2..7de605c 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -548,7 +548,7 @@ int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq, } int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low, - unsigned long ipi_bitmap_high, int min, + unsigned long ipi_bitmap_high, u32 min, unsigned long icr, int op_64_bit) { int i; @@ -571,18 +571,27 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low, rcu_read_lock(); map = rcu_dereference(kvm->arch.apic_map); + if (min > map->max_apic_id) + goto out; /* Bits above cluster_size are masked in the caller. */ - for_each_set_bit(i, &ipi_bitmap_low, BITS_PER_LONG) { - vcpu = map->phys_map[min + i]->vcpu; - count += kvm_apic_set_irq(vcpu, &irq, NULL); + for_each_set_bit(i, &ipi_bitmap_low, + min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) { + if (map->phys_map[min + i]) { + vcpu = map->phys_map[min + i]->vcpu; + count += kvm_apic_set_irq(vcpu, &irq, NULL); + } } min += cluster_size; - for_each_set_bit(i, &ipi_bitmap_high, BITS_PER_LONG) { - vcpu = map->phys_map[min + i]->vcpu; - count += kvm_apic_set_irq(vcpu, &irq, NULL); + for_each_set_bit(i, &ipi_bitmap_high, + min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) { + if (map->phys_map[min + i]) { + vcpu = map->phys_map[min + i]->vcpu; + count += kvm_apic_set_irq(vcpu, &irq, NULL); + } } +out: rcu_read_unlock(); return count; } -- 2.7.4