From patchwork Sun Jan 6 19:23:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ahmed Abd El Mawgood X-Patchwork-Id: 10749645 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A89DC14E5 for ; Sun, 6 Jan 2019 19:26:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C73D288ED for ; Sun, 6 Jan 2019 19:26:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8FAD32891B; Sun, 6 Jan 2019 19:26:39 +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=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 68268288ED for ; Sun, 6 Jan 2019 19:26:38 +0000 (UTC) Received: (qmail 26124 invoked by uid 550); 6 Jan 2019 19:25:55 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 26068 invoked from network); 6 Jan 2019 19:25:54 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mena-vt-edu.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FcIt9r2n/mEgqCNb5UT29WUXJF2aCPaNHWPRCZT6MPQ=; b=FBu/N6FjXEtoTZSAT8qt14ryvmjQ1cG1/usTceUA7DDlcEoiCTon9xeIXq9+JfiQ1k lisFy9MTDRQfESRMYVlKXdrw1Pn9DAeyMAD3Bd1jlTFhXKuP+CkYbapUuAga1dHyAs80 EiWHcoiG3JwCuPWmsai+WV8I8blhaHKZ4SnxsvxoYIS8HtPKJWPkf96QW1QVlmxtZhzF lExDwC617+RSgR+DRM3RvoonZtZKG/vocEzWeVob5SSjr+UKVRHXSQFniNdoBpkE8c8j ZLQW6n7ZF8CBr3SjV4YFtimMN6XG6aYX7Gqdl5UKS7xNqtKYWUqarZOyetkekXXbc2pq jI8A== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=FcIt9r2n/mEgqCNb5UT29WUXJF2aCPaNHWPRCZT6MPQ=; b=VLzH6O/esrLtQWVJ6C4HqeIjwkntN2Fu+w5pWo64EXkRidt1zCxSaDGABjhohR8TWV OdRjXeBMZOujXp1QfpO2X9Q1PisLNDwJExB4UT8ZZqfzX2TpxOFdQv+jEjkIzsnn/nMN iWb434CEwHrxTNtw5UqpqBkIGVd9U/xFVHaGMIRJ6jLnWeSMIbs93KKMGDYVjcUqo/ez xBB4pWdfEsK4FdziAJiCVIw9JZeC9UkORyBLZltwUyxSyl3fuWGZOpMOQhobEJ4q/pPu HpYBExLCwuRaAi0fIuN1UOyN7ZP8IX9rWGwKEXulEfE3djz3M6fTc56OjJgBI7nhsBtO 38hw== X-Gm-Message-State: AA+aEWb+d2tD9pFv6lafG8u5+whL8cLnNTvAqoIu/sr8o/yv8FmIH2Ld 8WB9U4Um8KeBItXaECL70dYvSw== X-Google-Smtp-Source: AFSGD/V8SOJNT1UutbLRRp80MgbQavLTT1b+HqnaQeuKyMCxrGRrO8Vi6T5jJ1+TdOAW7IChT2tHYw== X-Received: by 2002:a50:b559:: with SMTP id z25mr52523066edd.239.1546802742952; Sun, 06 Jan 2019 11:25:42 -0800 (PST) From: Ahmed Abd El Mawgood To: Paolo Bonzini , rkrcmar@redhat.com, Jonathan Corbet , Thomas Gleixner , Ingo Molnar , Borislav Petkov , hpa@zytor.com, x86@kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, ahmedsoliman0x666@gmail.com, ovich00@gmail.com, kernel-hardening@lists.openwall.com, nigel.edwards@hpe.com, Boris Lukashev , Igor Stoppa Cc: Ahmed Abd El Mawgood Subject: [PATCH V8 09/11] KVM: Add new exit reason For ROE violations Date: Sun, 6 Jan 2019 21:23:43 +0200 Message-Id: <20190106192345.13578-10-ahmedsoliman@mena.vt.edu> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190106192345.13578-1-ahmedsoliman@mena.vt.edu> References: <20190106192345.13578-1-ahmedsoliman@mena.vt.edu> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP The problem is that qemu will not be able to detect ROE violations, so one option would be create host API to tell if a given page is ROE protected, or create ROE violation exit reason. Signed-off-by: Ahmed Abd El Mawgood --- arch/x86/kvm/x86.c | 10 +++++++++- include/kvm/roe.h | 12 ++++++++++++ include/uapi/linux/kvm.h | 2 +- virt/kvm/kvm_main.c | 1 + virt/kvm/roe.c | 2 +- virt/kvm/roe_generic.h | 9 +-------- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 19b0f2307e..368e3d99fd 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5409,6 +5409,7 @@ static int emulator_read_write(struct x86_emulate_ctxt *ctxt, const struct read_write_emulator_ops *ops) { struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); + struct kvm_memory_slot *slot; gpa_t gpa; int rc; @@ -5450,7 +5451,14 @@ static int emulator_read_write(struct x86_emulate_ctxt *ctxt, vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; - vcpu->run->exit_reason = KVM_EXIT_MMIO; + slot = kvm_vcpu_gfn_to_memslot(vcpu, gpa >> PAGE_SHIFT); + if (slot && ops->write && (kvm_roe_check_range(slot, gpa>>PAGE_SHIFT, + gpa - (gpa & PAGE_MASK), bytes) || + gfn_is_full_roe(slot, gpa>>PAGE_SHIFT))) + vcpu->run->exit_reason = KVM_EXIT_ROE; + else + vcpu->run->exit_reason = KVM_EXIT_MMIO; + vcpu->run->mmio.phys_addr = gpa; return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); diff --git a/include/kvm/roe.h b/include/kvm/roe.h index 6a86866623..3121a67753 100644 --- a/include/kvm/roe.h +++ b/include/kvm/roe.h @@ -13,4 +13,16 @@ void kvm_roe_arch_commit_protection(struct kvm *kvm, struct kvm_memory_slot *slot); int kvm_roe(struct kvm_vcpu *vcpu, u64 a0, u64 a1, u64 a2, u64 a3); bool kvm_roe_arch_is_userspace(struct kvm_vcpu *vcpu); +bool kvm_roe_check_range(struct kvm_memory_slot *slot, gfn_t gfn, int offset, + int len); +static inline bool gfn_is_full_roe(struct kvm_memory_slot *slot, gfn_t gfn) +{ + return test_bit(gfn - slot->base_gfn, slot->roe_bitmap); + +} +static inline bool gfn_is_partial_roe(struct kvm_memory_slot *slot, gfn_t gfn) +{ + return test_bit(gfn - slot->base_gfn, slot->partial_roe_bitmap); +} + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 6d4ea4b6c9..0a386bb5f2 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -235,7 +235,7 @@ struct kvm_hyperv_exit { #define KVM_EXIT_S390_STSI 25 #define KVM_EXIT_IOAPIC_EOI 26 #define KVM_EXIT_HYPERV 27 - +#define KVM_EXIT_ROE 28 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ #define KVM_INTERNAL_ERROR_EMULATION 1 diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 819033f475..d92d300539 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -62,6 +62,7 @@ #include "async_pf.h" #include "vfio.h" #include "roe_generic.h" +#include #define CREATE_TRACE_POINTS #include diff --git a/virt/kvm/roe.c b/virt/kvm/roe.c index 4393a6a6a2..9540473f89 100644 --- a/virt/kvm/roe.c +++ b/virt/kvm/roe.c @@ -60,7 +60,7 @@ bool kvm_roe_check_range(struct kvm_memory_slot *slot, gfn_t gfn, int offset, return false; return kvm_roe_protected_range(slot, gpa, len); } - +EXPORT_SYMBOL_GPL(kvm_roe_check_range); void kvm_roe_free(struct kvm_memory_slot *slot) { diff --git a/virt/kvm/roe_generic.h b/virt/kvm/roe_generic.h index ad121372f2..f1ce4a8aec 100644 --- a/virt/kvm/roe_generic.h +++ b/virt/kvm/roe_generic.h @@ -14,12 +14,5 @@ void kvm_roe_free(struct kvm_memory_slot *slot); int kvm_roe_init(struct kvm_memory_slot *slot); bool kvm_roe_check_range(struct kvm_memory_slot *slot, gfn_t gfn, int offset, int len); -static inline bool gfn_is_full_roe(struct kvm_memory_slot *slot, gfn_t gfn) -{ - return test_bit(gfn - slot->base_gfn, slot->roe_bitmap); -} -static inline bool gfn_is_partial_roe(struct kvm_memory_slot *slot, gfn_t gfn) -{ - return test_bit(gfn - slot->base_gfn, slot->partial_roe_bitmap); -} + #endif