From patchwork Fri Dec 7 12:48:02 2018 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: 10718107 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 9F4731759 for ; Fri, 7 Dec 2018 12:58:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8F40F2E006 for ; Fri, 7 Dec 2018 12:58:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 828D42E01B; Fri, 7 Dec 2018 12:58:23 +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 6B4EC2E006 for ; Fri, 7 Dec 2018 12:58:22 +0000 (UTC) Received: (qmail 15453 invoked by uid 550); 7 Dec 2018 12:56:22 -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 Delivered-To: moderator for kernel-hardening@lists.openwall.com Received: (qmail 3214 invoked from network); 7 Dec 2018 12:49:42 -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=6aBx4Uf0AXPUXYaYC8L7IOOvH1hBcTiqrQSEjZvFMvY=; b=ZRLrCoeXD9HW8NIerF03oCdBCfs49Kp1L+4nSKW3GXD2Oq7tL/6qr6EIac2uT/L0el DPWzzOG+eEgD/0CL1mm/lzRuqDFU+j7TXH05SlfQOurMEQpG0S4fTGcgQveCjfkHmJaI pCTNXx5TrQmu8OJZeNA3vWJ+SgSft7oEwlepT2AFDZXeGXqvfsIkEUVwyGrtWJfN/Uti QrqdD+0d+avOGwZHD+JXLAr9++3byKWfskeJv9HgdL1DfhB2Ki4prBhvK9q3w5vvDJ12 zMoR4r0b2LmF2kZvzdwd8smerBUM+M6obbMfBIoyeFSHT+JT5lOx6MlH8cJvvyCk6oSP 6jrQ== 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=6aBx4Uf0AXPUXYaYC8L7IOOvH1hBcTiqrQSEjZvFMvY=; b=Xf+DFsJh5yyTgxMn1fFDdDc/rAvjF2GaiX3wDVfjM1HvYy7rPGRIP8H25RH4QTpQfl drqzflw4yUOtZiWF9qzKaRX5A3fwuxltvBhYvSndO1PcvLiPwMwIWQ+pE2PN9q0AAnKp 0OLY0gjSEt+7/PqSZRZxNEGKHLXNSDv9YkMCvAV442lxPQmdbrQP+hpx9iwI3xgEN4PJ DYk1tSDiUCvQtkn5qgautFV8aknWxsE/vFjfo6FnGTNdXzyI/YKzH+ggnrNjF9tZ8SrI 08aNh0iMBtC7Ll7tDNZUtZt8dlf/E+ID/e9AOEtvLBNwt3hez5Mg5TDq7P2dvsWS+xaT 23Tg== X-Gm-Message-State: AA+aEWauYxV4IUK17ud87o54ZfmJ7uY81L/u4vkTUqzvmcpSRxmE1XN2 fn5Yspmi4cUcmOxRv5+LNQEcMA== X-Google-Smtp-Source: AFSGD/UgbVwxu0T4GOFk5WoSSvUhKOVLjgnI86o17K0DWzUsnEBQw6xC+04yacjI7Psv/elfu6Qi9A== X-Received: by 2002:a1c:2b45:: with SMTP id r66mr2063076wmr.7.1544186971172; Fri, 07 Dec 2018 04:49:31 -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 09/10] KVM: Add new exit reason For ROE violations Date: Fri, 7 Dec 2018 14:48:02 +0200 Message-Id: <20181207124803.10828-10-ahmedsoliman@mena.vt.edu> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181207124803.10828-1-ahmedsoliman@mena.vt.edu> References: <20181207124803.10828-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 28475c83f9..ddd15bb1a7 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5334,6 +5334,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; @@ -5375,7 +5376,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 2b7a652c9f..185767e512 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 0d129b05d5..c3a21d3bc8 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 dfb1de314c..6555838f0c 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