mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Like Xu <like.xu.linux@gmail.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: jmattson@google.com, wanpengli@tencent.com,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	kvm@vger.kernel.org
Subject: Re: [PATCH] KVM: x86: avoid out of bounds indices for fixed performance counters
Date: Fri, 10 Dec 2021 16:59:29 +0800	[thread overview]
Message-ID: <7a2fc1d7-6ef4-cf4c-5ba0-c0eaefd2c66b@gmail.com> (raw)
In-Reply-To: <20211209191101.288041-1-pbonzini@redhat.com>

On 10/12/2021 3:11 am, Paolo Bonzini wrote:
> Because IceLake has 4 fixed performance counters but KVM only supports 3,
> it is possible for reprogram_fixed_counters to pass to

Emm, it's possible for a unwise user space or smart syzkaller.

> reprogram_fixed_counter an index that is out of bounds for
> the fixed_pmc_events array.
> 
> Ultimately intel_find_fixed_event, which is the only place that uses
> fixed_pmc_events, handles this correctly because it checks against the
> size of fixed_pmc_events anyway.  Every other place
> operates on the fixed_counters[] array which is sized
> according to INTEL_PMC_MAX_FIXED.  However, it is cleaner if
> the unsupported performance counters are culled early on
> in reprogram_fixed_counters.

How about introducing a static "struct x86_pmu_capability" variable [1] so that 
we can

(1) setup num_counters_fixed just once in the kvm_init_pmu_capability(), and
(2) avoid repeated calls to perf_get_x86_pmu_capability() ;

?

[1] https://lore.kernel.org/kvm/20210806133802.3528-17-lingshan.zhu@intel.com/
By the way, do you need a re-based version of the guest PBES feature ?

Thanks,
Like Xu

> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>   arch/x86/kvm/vmx/pmu_intel.c | 7 ++++---
>   1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
> index 1b7456b2177b..d33e9799276e 100644
> --- a/arch/x86/kvm/vmx/pmu_intel.c
> +++ b/arch/x86/kvm/vmx/pmu_intel.c
> @@ -91,7 +91,7 @@ static unsigned intel_find_fixed_event(int idx)
>   	u32 event;
>   	size_t size = ARRAY_SIZE(fixed_pmc_events);
>   
> -	if (idx >= size)
> +	if (WARN_ON_ONCE(idx >= size))
>   		return PERF_COUNT_HW_MAX;
>   
>   	event = fixed_pmc_events[array_index_nospec(idx, size)];
> @@ -500,8 +500,9 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
>   		pmu->nr_arch_fixed_counters = 0;
>   	} else {
>   		pmu->nr_arch_fixed_counters =
> -			min_t(int, edx.split.num_counters_fixed,
> -			      x86_pmu.num_counters_fixed);
> +			min3(ARRAY_SIZE(fixed_pmc_events),
> +			     (size_t) edx.split.num_counters_fixed,
> +			     (size_t) x86_pmu.num_counters_fixed);
>   		edx.split.bit_width_fixed = min_t(int,
>   			edx.split.bit_width_fixed, x86_pmu.bit_width_fixed);
>   		pmu->counter_bitmask[KVM_PMC_FIXED] =

  reply	other threads:[~2021-12-10  8:59 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-09 19:11 Paolo Bonzini
2021-12-10  8:59 ` Like Xu [this message]
2021-12-10  9:03   ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7a2fc1d7-6ef4-cf4c-5ba0-c0eaefd2c66b@gmail.com \
    --to=like.xu.linux@gmail.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=wanpengli@tencent.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®