From patchwork Sun Oct 22 07:48:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 10021611 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 6B5A560224 for ; Sun, 22 Oct 2017 07:48:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5574528613 for ; Sun, 22 Oct 2017 07:48:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 49F8F2866B; Sun, 22 Oct 2017 07:48: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=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=ham 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 B3D9928613 for ; Sun, 22 Oct 2017 07:48:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751259AbdJVHsm (ORCPT ); Sun, 22 Oct 2017 03:48:42 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:55799 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751240AbdJVHsl (ORCPT ); Sun, 22 Oct 2017 03:48:41 -0400 Received: by mail-wm0-f65.google.com with SMTP id u138so4456122wmu.4 for ; Sun, 22 Oct 2017 00:48:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=fZJxvLW7RbrngsSL+SaSWxMB1h+oMfLghP/oqCgSe/4=; b=NIOqIpHY1yYWPJiSoQkilzoPxO8Ocaz1h5VhEKhetV39VnRvlqO64WW8JyRrXUE9sy l/kg5+ZFRjdJmY3F5Fba7SvgJIxFiN/AoOxB36COMunYPw7T8iSkd3h7iJWxV6TouCDN LzE8PjO7WFglYQ8/XWjlqI+dGRb6ptnxeHZGg= 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; bh=fZJxvLW7RbrngsSL+SaSWxMB1h+oMfLghP/oqCgSe/4=; b=EfukpX4FtZ1cijqveAkpcfmhZzt2UjHSx4sJ9IX36eYxaufEZrmrhaxb75MVP2EXl4 lRaES3pfp7wjeWrCgxjVQf4U1AyCTBC0VqDpIMI4PRYRJIqPYM3b2SbQ+LyBMZR7het0 WSo1rbN9UseswVNzYfW2XqsJQ4aHARBRkdeAajnF+7ZlOU87H57sTLmSRngA5OXmFL+H rxAioIVgc+gGbRqbUBYpyArxZaG/BBlmThD8Bfifkt2NKVTnbxPttrCfiGrgDf3OiZmj YyCHXfjcmvXU7L+ekCnkkWfQM//fj9YWQr6srSsMD65nojtWv/rzcNHwipSj8Y6xknA1 6k8w== X-Gm-Message-State: AMCzsaWhABEzoM9MYLa2qtBEwATeIaJQMu5GWN66BRMyTQCayIwzU+mQ ui3olg42gqqDZExmproeOSDXdg== X-Google-Smtp-Source: ABhQp+Sz/f4sV8FpwZvjlmVoilbLNyxClC4KF/6oVC0dNW2kxv4A08YSywEg2ZtBWE/nEl8gULMdZw== X-Received: by 10.80.162.103 with SMTP id 94mr12175066edl.159.1508658520525; Sun, 22 Oct 2017 00:48:40 -0700 (PDT) Received: from localhost.localdomain (xd93dd96b.cust.hiper.dk. [217.61.217.107]) by smtp.gmail.com with ESMTPSA id g45sm4128103eda.19.2017.10.22.00.48.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 22 Oct 2017 00:48:39 -0700 (PDT) From: Christoffer Dall To: kvmarm@lists.cs.columbia.edu Cc: linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, Christoffer Dall Subject: [PATCH v2] KVM: arm/arm64: Allow usercopy to vcpu->arch.ctxt and arm64 debug Date: Sun, 22 Oct 2017 09:48:36 +0200 Message-Id: <20171022074836.17728-1-christoffer.dall@linaro.org> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171020232525.7387-1-pbonzini@redhat.com> References: <20171020232525.7387-1-pbonzini@redhat.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP We do direct useraccess copying to the kvm_cpu_context structure embedded in the kvm_vcpu_arch structure, and to the vcpu debug register state. Everything else (timer, PMU, vgic) goes through a temporary indirection. Fixing all accesses to kvm_cpu_context is massively invasive, and we'd like to avoid that, so we tell kvm_init_usercopy to whitelist accesses to out context structure. The debug system register accesses on arm64 are modified to work through an indirection instead. Signed-off-by: Christoffer Dall --- Changes since v1: - Use get_user() and put_user() instead of the implicit understanding that these will always be 64-bit values. arch/arm64/kvm/sys_regs.c | 44 ++++++++++++++++++++++++++++---------------- virt/kvm/arm/arm.c | 5 ++++- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 2e070d3baf9f..34b9e1734a3f 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -293,19 +293,22 @@ static bool trap_bvr(struct kvm_vcpu *vcpu, static int set_bvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, const struct kvm_one_reg *reg, void __user *uaddr) { - __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg]; + __u64 __user *uval = uaddr; + __u64 r; - if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0) + if (get_user(r, uval)) return -EFAULT; + vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg] = r; return 0; } static int get_bvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, const struct kvm_one_reg *reg, void __user *uaddr) { - __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg]; + __u64 r = vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg]; + __u64 __user *uval = uaddr; - if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0) + if (put_user(r, uval)) return -EFAULT; return 0; } @@ -335,10 +338,12 @@ static bool trap_bcr(struct kvm_vcpu *vcpu, static int set_bcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, const struct kvm_one_reg *reg, void __user *uaddr) { - __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bcr[rd->reg]; + __u64 __user *uval = uaddr; + __u64 r; - if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0) + if (get_user(r, uval)) return -EFAULT; + vcpu->arch.vcpu_debug_state.dbg_bcr[rd->reg] = r; return 0; } @@ -346,9 +351,10 @@ static int set_bcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, static int get_bcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, const struct kvm_one_reg *reg, void __user *uaddr) { - __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bcr[rd->reg]; + __u64 r = vcpu->arch.vcpu_debug_state.dbg_bcr[rd->reg]; + __u64 __user *uval = uaddr; - if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0) + if (put_user(r, uval)) return -EFAULT; return 0; } @@ -379,19 +385,22 @@ static bool trap_wvr(struct kvm_vcpu *vcpu, static int set_wvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, const struct kvm_one_reg *reg, void __user *uaddr) { - __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg]; + __u64 __user *uval = uaddr; + __u64 r; - if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0) + if (get_user(r, uval)) return -EFAULT; + vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg] = r; return 0; } static int get_wvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, const struct kvm_one_reg *reg, void __user *uaddr) { - __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg]; + __u64 r = vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg]; + __u64 __user *uval = uaddr; - if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0) + if (put_user(r, uval)) return -EFAULT; return 0; } @@ -421,19 +430,22 @@ static bool trap_wcr(struct kvm_vcpu *vcpu, static int set_wcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, const struct kvm_one_reg *reg, void __user *uaddr) { - __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg]; + __u64 __user *uval = uaddr; + __u64 r; - if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0) + if (get_user(r, uval)) return -EFAULT; + vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg] = r; return 0; } static int get_wcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, const struct kvm_one_reg *reg, void __user *uaddr) { - __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg]; + __u64 r = vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg]; + __u64 __user *uval = uaddr; - if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0) + if (put_user(r, uval)) return -EFAULT; return 0; } diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c index b9f68e4add71..639e388678ff 100644 --- a/virt/kvm/arm/arm.c +++ b/virt/kvm/arm/arm.c @@ -1502,7 +1502,10 @@ void kvm_arch_exit(void) static int arm_init(void) { - int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); + int rc = kvm_init_usercopy(NULL, sizeof(struct kvm_vcpu), 0, + offsetof(struct kvm_vcpu_arch, ctxt), + sizeof_field(struct kvm_vcpu_arch, ctxt), + THIS_MODULE); return rc; }