From patchwork Sun Jan 20 23:39:38 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: 10772547 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 2BB9917E9 for ; Sun, 20 Jan 2019 23:42:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1A4AA29CB1 for ; Sun, 20 Jan 2019 23:42:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E04429CD1; Sun, 20 Jan 2019 23:42:50 +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 E1FCF29CB1 for ; Sun, 20 Jan 2019 23:42:48 +0000 (UTC) Received: (qmail 30516 invoked by uid 550); 20 Jan 2019 23:41:46 -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 30452 invoked from network); 20 Jan 2019 23:41:45 -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=1c9y8M8u1GlScRssrmufW1/Y7fhxLH4q1jt/CA5XKaXFsNAdtbu0w9xJ2Qx9oc50kG jNFY65vUmtflsLNaOdQyDTK3ARCroDpnifxvPaAW79sXXeb4IJqUXimuiBGpaUlYHwq7 cZu2ZqpZUh9Yg10KJKG9SEzo7YGdtIOzH8maEzfl3X5BYCRRcQpqurbS2ftuZ5VuFZYG UacqMhRQougz3i0vLt1pouYosIW92Ax47uLaYXP0teBu7W5AHJ36K+5RXmYlxJBPFWs7 k9pEtFLWsg+tSppbCbX1eqBgb8Kw/rb1Rb1MaEkCfsEQM56SMpRCHdZIg7MV3aRHZoW5 FT5g== 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=ZFeWp5Ij7nv5X0zU7BEzFcBYk4SvkiXPghw02PcCKDb8GlZpEGFnjhw7hycxmYmSnW zDZvU/CPSyTnXx2+GmJD81gVjlnG8xX8kUfwVb86iwMbP92q7u+mQGFV6VY4hycNC1g3 dHL6gCUCGfpvOdbxnroFTv3sK+grpHSBxtTlxCLlMPgHXV6iunsNndG7rwKT4/DD33Sb ZqaW93/XQ11MV/yyF6oPY559TRPvSmWfhGZXtzQfUQ0G43QygVrd8k7uUm8XXpFM2ojg fr8xNTymxN6qF2ZnoQMk7e7sr+nK6SgoLWxyqIvPMx/1E1M/syB/HsCc+cnvd7xSIpp2 3pYg== X-Gm-Message-State: AJcUukfcChHDPqQZSr1c5vjP9y2oLz0QmUvLlrixCA6BuDTQmx+Ilyix UbSb8sULqsylkXCV/7Yk9+V/CQ== X-Google-Smtp-Source: ALg8bN4DTv7SHsMAYfU64me5hoPQjfKcJBRkYkXoG2pmxBtf2ZQEC5d/5sRI+8BCLbZJXWgDqNZGlQ== X-Received: by 2002:a5d:470b:: with SMTP id y11mr25418979wrq.16.1548027694264; Sun, 20 Jan 2019 15:41:34 -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: [RESEND PATCH V8 09/11] KVM: Add new exit reason For ROE violations Date: Mon, 21 Jan 2019 01:39:38 +0200 Message-Id: <20190120233940.15282-10-ahmedsoliman@mena.vt.edu> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190120233940.15282-1-ahmedsoliman@mena.vt.edu> References: <20190120233940.15282-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