From patchwork Tue Jul 19 23:49:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 12923184 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 68C22C43334 for ; Tue, 19 Jul 2022 23:50:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232455AbiGSXuV (ORCPT ); Tue, 19 Jul 2022 19:50:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229565AbiGSXuT (ORCPT ); Tue, 19 Jul 2022 19:50:19 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7976222B5 for ; Tue, 19 Jul 2022 16:50:18 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id j9-20020aa78009000000b0052b5ccdf6b8so2116936pfi.6 for ; Tue, 19 Jul 2022 16:50:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=lxeM6vO2F4cE69KMm+OUfNaRAkZ+4gn+w9GC88OjZ4s=; b=o+bh8roK+w9ayZyoe9MY9troUTKam3j0RLQPLnbDvPqeFGpBUL3Oex4vcaljiPDLUu iHaMjgqmmWKn5DKlZTaKG/sJk+4PCSAwt8fRD/9tlfr0XQBSyD1DMp+aJuoNKHMAAXtQ 5FlCJsZtP1WQL+Of3aVaYWd90fKCBPaxo/RhsIEmRPbO+HoxGKT5VMQS0zWhnNfHYZHh b4LRJLSD3xuQNaY80JFL3rcAq4xBwu1jol6B8piD3Cs8EQ1pQBm/gh6N3xW9G88cOrY+ VUvkdVfg+EYuQZs2K6UR0vwEPOMRkhrEfPPFV27ZVR0Fjvnx4QtdWe3jYMC8JUbzpsM6 u1/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=lxeM6vO2F4cE69KMm+OUfNaRAkZ+4gn+w9GC88OjZ4s=; b=DmRzlaKAtBj2jlQh7xlT+yZ6I92eWcQejHT0s9pF6OESWx7Hd1PhpMiGW5Qrl28ds+ FiRzZ2k/ck68He0FiSXhg9qcI+xsK2S40lWKmOBNqBI1mDHawFiQS5pnh9PFG3GKOWGA jrCYCJJZZvpPrAM6ZnNgopWZULNp76L4qiwC1Cr1AdWwmWlXM/BUAhxIJSjT7I2QQbyA tK26Q7IrxrUrjlRBn002Pxg1f0IUaBmsh7oIcX+zUaZ7CYVZyIRati2uX51SpNxRBdYn fnrXV5Mlc7GvBsKCq8qsOOZiUP7pWxQWyQfRlK4HezPGg9lhe57gY0r6rHYV5Rt5+S5H v7NQ== X-Gm-Message-State: AJIora/W5d4l37KzhRjrV0czzljH7tDP7Svel8OKNWWcVhcCaEezd3Km cXZA+EuP37FEY9gsqEJkxJSc0eQQ+MeMJu+88tulgRmFrm6dNQ7gU62sW/uEG3Va+6wZWFzR0f2 Fk64JIp2ycV+gZpapjD7KjZWOui9XvrMPpKg+n9NhP3zBA2WzY90uPyxP8aV+aH02kOnJ X-Google-Smtp-Source: AGRyM1uDng+1UOzTDGGyaScdzThcOgLNA0gzlUYQteWO0uWccQZqHR9dTN39YWjQmZBQ15JRaiXh6YF3kIrY8f3/ X-Received: from aaronlewis.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2675]) (user=aaronlewis job=sendgmr) by 2002:a05:6a00:22d6:b0:52b:343b:6dcf with SMTP id f22-20020a056a0022d600b0052b343b6dcfmr26897453pfj.54.1658274617822; Tue, 19 Jul 2022 16:50:17 -0700 (PDT) Date: Tue, 19 Jul 2022 23:49:49 +0000 In-Reply-To: <20220719234950.3612318-1-aaronlewis@google.com> Message-Id: <20220719234950.3612318-2-aaronlewis@google.com> Mime-Version: 1.0 References: <20220719234950.3612318-1-aaronlewis@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [RFC PATCH v2 1/3] KVM: x86: Protect the unused bits in the MSR filtering / exiting flags From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The flags used in KVM_CAP_X86_USER_SPACE_MSR and KVM_X86_SET_MSR_FILTER have no protection for their unused bits. Without protection, future development for these features will be difficult. Add the protection needed to make it possible to extend these features in the future. Signed-off-by: Aaron Lewis --- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/x86.c | 6 ++++++ include/uapi/linux/kvm.h | 3 +++ 3 files changed, 10 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index ee3896416c68..63691a4c62d0 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -224,6 +224,7 @@ struct kvm_msr_filter_range { struct kvm_msr_filter { #define KVM_MSR_FILTER_DEFAULT_ALLOW (0 << 0) #define KVM_MSR_FILTER_DEFAULT_DENY (1 << 0) +#define KVM_MSR_FILTER_VALID_MASK (KVM_MSR_FILTER_DEFAULT_DENY) __u32 flags; struct kvm_msr_filter_range ranges[KVM_MSR_FILTER_MAX_RANGES]; }; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 031678eff28e..adaec8d07a25 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6171,6 +6171,9 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r = 0; break; case KVM_CAP_X86_USER_SPACE_MSR: + r = -EINVAL; + if (cap->args[0] & ~KVM_MSR_EXIT_REASON_VALID_MASK) + break; kvm->arch.user_space_msr_mask = cap->args[0]; r = 0; break; @@ -6384,6 +6387,9 @@ static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp) if (copy_from_user(&filter, user_msr_filter, sizeof(filter))) return -EFAULT; + if (filter.flags & ~KVM_MSR_FILTER_VALID_MASK) + return -EINVAL; + for (i = 0; i < ARRAY_SIZE(filter.ranges); i++) empty &= !filter.ranges[i].nmsrs; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index a36e78710382..236b8e09eef1 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -484,6 +484,9 @@ struct kvm_run { #define KVM_MSR_EXIT_REASON_INVAL (1 << 0) #define KVM_MSR_EXIT_REASON_UNKNOWN (1 << 1) #define KVM_MSR_EXIT_REASON_FILTER (1 << 2) +#define KVM_MSR_EXIT_REASON_VALID_MASK (KVM_MSR_EXIT_REASON_INVAL | \ + KVM_MSR_EXIT_REASON_UNKNOWN | \ + KVM_MSR_EXIT_REASON_FILTER) __u32 reason; /* kernel -> user */ __u32 index; /* kernel -> user */ __u64 data; /* kernel <-> user */ From patchwork Tue Jul 19 23:49:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 12923185 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3595C433EF for ; Tue, 19 Jul 2022 23:50:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234190AbiGSXuX (ORCPT ); Tue, 19 Jul 2022 19:50:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229565AbiGSXuW (ORCPT ); Tue, 19 Jul 2022 19:50:22 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 220F625C57 for ; Tue, 19 Jul 2022 16:50:21 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id j11-20020a05690212cb00b006454988d225so12169750ybu.10 for ; Tue, 19 Jul 2022 16:50:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=6tLAZ3S0KEa31uqY5dk3zGsG2eH/h5VqNqxXlgbbTfw=; b=HoA5DvFCF5wz3SjlHg1Eys6QJr0RCHOtPB0k9lmEB/Y/51FCC+MalsbSTFOZgJpuJw sQBdlRMlTyjYcBaPD9BdJcFTLLrKKIZRVO1E66rFb2Tbpbw1v2UKvsYmCqgidHHKHyNL ZDgwsOBxuzk3rB7k1Bz8KXoT6dkB2RUN8HWDG7jQZ6brB8fKvpZm7pssTNZqdC31rLZV Mv1Lba8X22ylyQYIqpuWBEPCFgEX8U6Ubw3Nd7YUmVhPcr/Nqdw7ErHCG0m33hWi2Eml 65DydR60qfnxzwGoVgL30PJlwl2yDbAk0WWPPoLYIOSPX8DIzb4iY4ibzPDibHnoMo40 1fug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=6tLAZ3S0KEa31uqY5dk3zGsG2eH/h5VqNqxXlgbbTfw=; b=1nDtvJdTVXRs/rB67gAsR4IDDB3hrzKNXan6LsrmveE7hNpwHUcHVQoU2IG/U9rSJT bDIXR7A5vLGUR4M4bddaA3l+NBkvoREqtzqyYq75gE0CYn9r7wPXDq0l/aKwBbIeFCYE SGhXMJEHUxOQhC8JuKYsAI7RaPub1NzqNmbAH42IGIHbDCKKO4io/PIue+jcTRNLSFW2 1AfVFIVURE24i3jqC5aTyowpgPUmUyBgM03yujK9w5yIfx2Ht80YP/1jiZis05sEmNft sNJARFBbIGtjYfTmY2+SrnwmzpqZDkqhD4SlBc6mRCaEFAtuZSpHYYCJygQQgBN5qHJm svGQ== X-Gm-Message-State: AJIora/m7ggKvR5qoTN2ztQk8PZ7tv8mzoMGR9iNw4hxRwSLaX1yxg5S judt2/RRdRzpSCRbozMERXYbIpZTJtUDfZBDwWrZxoen0dw6CN6xCxUwRv3zJKVwQswpqS35iNF +6lSD6/xJQSU0i1yPz2tSEwrMC0dEZDAVjW852YtJQecysTwapyKe5lfUjQv1ws0kQ4VV X-Google-Smtp-Source: AGRyM1vm2ZV9/5drYhN5ogPd3zThT+X6MZBguPMGLqyFkVV0JJaDM7pj7p4tEp3dBZi+N7mrxiKTLjW0CD0yxFrq X-Received: from aaronlewis.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2675]) (user=aaronlewis job=sendgmr) by 2002:a81:2405:0:b0:31c:82eb:eef9 with SMTP id k5-20020a812405000000b0031c82ebeef9mr38272752ywk.171.1658274620384; Tue, 19 Jul 2022 16:50:20 -0700 (PDT) Date: Tue, 19 Jul 2022 23:49:50 +0000 In-Reply-To: <20220719234950.3612318-1-aaronlewis@google.com> Message-Id: <20220719234950.3612318-3-aaronlewis@google.com> Mime-Version: 1.0 References: <20220719234950.3612318-1-aaronlewis@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v2 2/3] KVM: x86: Add a VALID_MASK for the flags in kvm_msr_filter_range From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add the mask KVM_MSR_FILTER_RANGE_VALID_MASK for the flags in the struct kvm_msr_filter_range. This simplifies checks that validate these flags, and makes it easier to introduce new flags in the future. No functional change intended. Signed-off-by: Aaron Lewis --- arch/x86/include/uapi/asm/kvm.h | 2 ++ arch/x86/kvm/x86.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 63691a4c62d0..a9fecd3eab61 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -214,6 +214,8 @@ struct kvm_msr_list { struct kvm_msr_filter_range { #define KVM_MSR_FILTER_READ (1 << 0) #define KVM_MSR_FILTER_WRITE (1 << 1) +#define KVM_MSR_FILTER_RANGE_VALID_MASK (KVM_MSR_FILTER_READ | \ + KVM_MSR_FILTER_WRITE) __u32 flags; __u32 nmsrs; /* number of msrs in bitmap */ __u32 base; /* MSR index the bitmap starts at */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index adaec8d07a25..6c1a531e3b88 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6349,7 +6349,7 @@ static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter, if (!user_range->nmsrs) return 0; - if (user_range->flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE)) + if (user_range->flags & ~KVM_MSR_FILTER_RANGE_VALID_MASK) return -EINVAL; if (!user_range->flags) From patchwork Tue Jul 19 23:49:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 12923186 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5048BC433EF for ; Tue, 19 Jul 2022 23:50:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230190AbiGSXu0 (ORCPT ); Tue, 19 Jul 2022 19:50:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235219AbiGSXuY (ORCPT ); Tue, 19 Jul 2022 19:50:24 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4746932BA5 for ; Tue, 19 Jul 2022 16:50:23 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id o3-20020a17090aac0300b001f210a5e47aso260290pjq.7 for ; Tue, 19 Jul 2022 16:50:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=BeLUBPWGvrc1z4ayLhHoeORRjwnbyy4mCi2eqGdwMcQ=; b=ZcO7RFWgYV6BKEYKTi4CwajtwHcMIW/kQDa14t+Z5p/2btKv7Lsd65ZwXJxvg23uHn IsWYQR2aVRYbsehw0mh2InIlozJI4xkuqBRaymR6xoJusYvZLN0EJLlF/sFtt7lUedIt hOyUNAO0ONJgm2Yr4e9ARSUyIgWVm+W3bS398SuVbGgN4D9NZ8F+dC4O0tY8ZPseEanA sqt8wYS/b5eMwzp06EjdTJdfDMFc4PKq9o9BE1wbxj1SRoQsl0WZEQ3Nkj7mF8I4ItOz t7jZMiXwgLfKLzq2++Ghf2m9bxj7UmEUyYAGQcLW162PspODoIh8zTdzxv1mGiSDntPb Px6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=BeLUBPWGvrc1z4ayLhHoeORRjwnbyy4mCi2eqGdwMcQ=; b=7FVUYFFbT5keJiTbuNkBqgJZNHC79NTzFzchrnENvr+6Ugr29S0wFceJYeshH7Eeu8 Z4Mi81PclSbjhw4QWZB64x6IATNxMU/2gdRsq/IbgepmSXQwdfW70j8kq2UIJcOQS+c1 wcB4TKSQogF3ZdqfdpAsI5rkJVsBun3Ea5RRvyuIbsF3pUriGQNPJdjHesBH9M131rrU X5+PSV7Tdg8XIU/XeFns8Qvxzy/suSrTLOs6VlDTXST+Zzp7VCuhukpT3JJrd8vaGqCC e5buxqJNuVFvO7LfD3O98CrNDvpRinn3sg9OUowuxBeTB8LnpIs+5lrlqg8tVzfXrQgl S22g== X-Gm-Message-State: AJIora9eAXPb8SyGZ5rGLhnvolcxqfPeQrCm6pfYLjT9avyQJoRUqj7Q 0ccxCeCzuP8pCAJa/2hchWWBycgJKSZzbEkGNYu/NgAtmMuD3ww6dYaho60q9hTs9vD7JmDmn52 6n1U13bl5AjrAFLF96ImMoTYAIS4rU9C9OJqz0OXlcr+LaPebhMtT3L5VmzOkqs07NZwi X-Google-Smtp-Source: AGRyM1st7uUbJNGtpgQrCAA/vwf6kPQlthY9EgcKC5332JcCDPmtWwB+4UYGgdOo4BhmYBUFLz21u+l4emMjwYkk X-Received: from aaronlewis.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2675]) (user=aaronlewis job=sendgmr) by 2002:a17:90a:1190:b0:1f2:69d:40c9 with SMTP id e16-20020a17090a119000b001f2069d40c9mr2065680pja.188.1658274622705; Tue, 19 Jul 2022 16:50:22 -0700 (PDT) Date: Tue, 19 Jul 2022 23:49:51 +0000 In-Reply-To: <20220719234950.3612318-1-aaronlewis@google.com> Message-Id: <20220719234950.3612318-4-aaronlewis@google.com> Mime-Version: 1.0 References: <20220719234950.3612318-1-aaronlewis@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [RFC PATCH v2 3/3] selftests: kvm/x86: Test the flags in MSR filtering / exiting From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org When using the flags in KVM_X86_SET_MSR_FILTER and KVM_CAP_X86_USER_SPACE_MSR it is expected that an attempt to write to any of the unused bits will fail. Add testing to walk over every bit in each of the flag fields in MSR filtering / exiting to verify that happens. Signed-off-by: Aaron Lewis --- .../kvm/x86_64/userspace_msr_exit_test.c | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c b/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c index f84dc37426f5..3b4ad16cc982 100644 --- a/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c +++ b/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c @@ -734,6 +734,99 @@ static void test_msr_permission_bitmap(void) kvm_vm_free(vm); } +static void test_results(int rc, const char *scmd, bool expected_success) +{ + int expected_rc; + + expected_rc = expected_success ? 0 : -1; + TEST_ASSERT(rc == expected_rc, + "Unexpected result from '%s', rc: %d, expected rc: %d.", + scmd, rc, expected_rc); + TEST_ASSERT(!rc || (rc == -1 && errno == EINVAL), + "Failures are expected to have rc == -1 && errno == EINVAL(%d),\n" + " got rc: %d, errno: %d", + EINVAL, rc, errno); +} + +#define test_ioctl(vm, cmd, arg, expected_success) \ +({ \ + int rc = __vm_ioctl(vm, cmd, arg); \ + \ + test_results(rc, #cmd, expected_success); \ +}) +#define FLAG (1ul << i) + +static void run_user_space_msr_flag_test(struct kvm_vm *vm) +{ + struct kvm_enable_cap cap = { .cap = KVM_CAP_X86_USER_SPACE_MSR }; + int nflags = sizeof(cap.args[0]) * BITS_PER_BYTE; + int rc; + int i; + + rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR); + TEST_ASSERT(rc, "KVM_CAP_X86_USER_SPACE_MSR is available"); + + for (i = 0; i < nflags; i++) { + cap.args[0] = FLAG; + test_ioctl(vm, KVM_ENABLE_CAP, &cap, + FLAG & KVM_MSR_EXIT_REASON_VALID_MASK); + } +} + +static void run_msr_filter_flag_test(struct kvm_vm *vm) +{ + u64 deny_bits = 0; + struct kvm_msr_filter filter = { + .flags = KVM_MSR_FILTER_DEFAULT_ALLOW, + .ranges = { + { + .flags = KVM_MSR_FILTER_READ, + .nmsrs = 1, + .base = 0, + .bitmap = (uint8_t *)&deny_bits, + }, + }, + }; + int nflags; + int rc; + int i; + + rc = kvm_check_cap(KVM_CAP_X86_MSR_FILTER); + TEST_ASSERT(rc, "KVM_CAP_X86_MSR_FILTER is available"); + + nflags = sizeof(filter.flags) * BITS_PER_BYTE; + for (i = 0; i < nflags; i++) { + filter.flags = FLAG; + test_ioctl(vm, KVM_X86_SET_MSR_FILTER, &filter, + FLAG & KVM_MSR_FILTER_VALID_MASK); + } + + filter.flags = KVM_MSR_FILTER_DEFAULT_ALLOW; + nflags = sizeof(filter.ranges[0].flags) * BITS_PER_BYTE; + for (i = 0; i < nflags; i++) { + filter.ranges[0].flags = FLAG; + test_ioctl(vm, KVM_X86_SET_MSR_FILTER, &filter, + FLAG & KVM_MSR_FILTER_RANGE_VALID_MASK); + } +} + +/* Test that attempts to write to the unused bits in a flag fails. */ +static void test_flags(void) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + + vm = vm_create_with_one_vcpu(&vcpu, NULL); + + /* Test flags for KVM_CAP_X86_USER_SPACE_MSR. */ + run_user_space_msr_flag_test(vm); + + /* Test flags and range flags for KVM_X86_SET_MSR_FILTER. */ + run_msr_filter_flag_test(vm); + + kvm_vm_free(vm); +} + int main(int argc, char *argv[]) { /* Tell stdout not to buffer its content */ @@ -745,5 +838,7 @@ int main(int argc, char *argv[]) test_msr_permission_bitmap(); + test_flags(); + return 0; }