Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1051074ybi; Fri, 31 May 2019 13:00:57 -0700 (PDT) X-Google-Smtp-Source: APXvYqzpWmnpZxX2NcbtEDbpF5xraxH4/re98M6suyT0GKVE0wLYHlfQme1qamNL6ELQsNnZ+Ptq X-Received: by 2002:a63:6111:: with SMTP id v17mr1508932pgb.206.1559332856961; Fri, 31 May 2019 13:00:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559332856; cv=none; d=google.com; s=arc-20160816; b=UkHxFMZPuVPJkbTIORovo4SiGm3tenoz6XqkQp5y+Zwn+wu3fpT6J676mPvzpRf4wv JQZfJZLLlWu6iirUNPE6RtEDoohRCAwOBNTq9bqf0Ty+C3+es5GscCsgQ0rFs6i6uSTD AEeFqakZzDgrKPRZqunX+mPGMTIHNs1c3mUxWoLq9sAin3+pwHcQOmNL6CaMEXDyz1e2 Qi8jb0AjsoR5xVQDccVeaBTKyzDHs55iEUn5jwsKpFX3izzsySYUbHrUanK3rizNRjoT XFLals9nVprE5rhH28gUKZS8rvyiQ0kpObkAgLg2+N344XWuJGxbYSbNPyc6kL5tqOy6 /pKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=3SkRaYCGnq1C8VvlqaJ8i8zblsitHZj5v7WamNGN6H8=; b=En8N4hnnZVblOYUmj6MIXT9TH4LRb4fVxs+0UDXL0fLw1GogS8IOtBRZ4cQx07wwaw u73RAK+hYr2BuLTdrqkc3ZGMs0MZoWTij4Gacu91vITH/WUPWedWa6/zMudO7GDvNcLG fAKzW5OxVYgq9NBeDj5T8uJzWkpv7iFTmahi9cWsTGQ0nyo9rcDF/n/Gz6qjT+zvUWEG j93v5s6UVwcZh+ZnQWpCdZmBysrKiWsrncZczZV/sQkvUidPLmtCyhIDhwevKykSNYj3 E7ICctj7V8kf/5xGHIMI1cotKk9ZPhadM1sgS9Hec3ZJD4+Gau/NWTM0nHNHxOkTkjPm u0Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=u+WzyLtE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g98si7265133plb.38.2019.05.31.13.00.40; Fri, 31 May 2019 13:00:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=u+WzyLtE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727413AbfEaT7i (ORCPT + 99 others); Fri, 31 May 2019 15:59:38 -0400 Received: from mail-ed1-f65.google.com ([209.85.208.65]:41531 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727282AbfEaT7i (ORCPT ); Fri, 31 May 2019 15:59:38 -0400 Received: by mail-ed1-f65.google.com with SMTP id x25so4018783eds.8 for ; Fri, 31 May 2019 12:59:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3SkRaYCGnq1C8VvlqaJ8i8zblsitHZj5v7WamNGN6H8=; b=u+WzyLtECMcbvjVI27nPUY/f1sNnnGQ4v95OEd2FRD+2RIN3aj6jzGAF/2PTtZkWbl IsTT3rCeorqDEpWXIuiNbgTjlGHWnHUr/D/sOZSUHeY0xjsMN9HKVI7kH6G0gclx6jkl B82MHaosoGQikbSAqSxEEk2TDrSJO/hPcw3taZ9jfyFUJ4ANImCDvahXr9qfh8f2T5BE FHCu+ECupVFYxp5GFBf5w0hdhwEUn3OgrWM+Py/pRkyzEODGAZgWcZ81VdDKzxCOPVfN 6nLxbxuIKeaZTg+ce2C7SPKFLQaUE7po1agK96jvrVl4UCS91D4hCsJWwk8pQ0eJ7d9+ 0WLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=3SkRaYCGnq1C8VvlqaJ8i8zblsitHZj5v7WamNGN6H8=; b=JBiQX/fKL0ClC49lS+LC3VteK6fc9+adXDKk5+M3zQU0hDe/O3qYDBziq8Yosx7oR5 bL9nc8iRgjmSQPAg9e3cJYc7NBNvGKZcTZx3hxQ1iRdPAc6TVa96H1B9syvwQCc42Wrs G+lh3FseWZt7KckS9GDERMBzsFTJdteTNftLDhIpwz2qleAzRi7UiCUXf1hTNB/cARY+ tEIL0LOHEEh+EvIv4klKf6fyTuENyEsu50V723gVs/YRohUNOTm9OteZJ51wA5TRR7lV lLz0WiEai7e5Gz2unpnsKZaWElLncp+XrQ3hS8ITP9g2neeDUu17ma6awMMrVl0XyJ7L OJ2g== X-Gm-Message-State: APjAAAWY+JcrMXslh394iUPxzej1MpWQsST4+awyRaFvPaJIbVZ7ONw2 Ab36ra7G1VmNnOkeUqoBQz1DyiRS/tsoqhRJzFYb6Q== X-Received: by 2002:a17:906:2922:: with SMTP id v2mr10871610ejd.115.1559332775551; Fri, 31 May 2019 12:59:35 -0700 (PDT) MIME-Version: 1.0 References: <5CEC9667.30100@intel.com> <5CEE3AC4.3020904@intel.com> <5CF07D37.9090805@intel.com> In-Reply-To: <5CF07D37.9090805@intel.com> From: Eric Hankland Date: Fri, 31 May 2019 12:59:24 -0700 Message-ID: Subject: Re: [PATCH v1] KVM: x86: PMU Whitelist To: Wei Wang Cc: pbonzini@redhat.com, rkrcmar@redhat.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 30, 2019 at 5:57 PM Wei Wang wrote: > > On 05/30/2019 01:11 AM, Eric Hankland wrote: > > On Wed, May 29, 2019 at 12:49 AM Wei Wang wrote: > >> On 05/29/2019 02:14 AM, Eric Hankland wrote: > >>> On Mon, May 27, 2019 at 6:56 PM Wei Wang wrote: > >>>> On 05/23/2019 06:23 AM, Eric Hankland wrote: > >>>>> - Add a VCPU ioctl that can control which events the guest can monitor. > >>>>> > >>>>> Signed-off-by: ehankland > >>>>> --- > >>>>> Some events can provide a guest with information about other guests or the > >>>>> host (e.g. L3 cache stats); providing the capability to restrict access > >>>>> to a "safe" set of events would limit the potential for the PMU to be used > >>>>> in any side channel attacks. This change introduces a new vcpu ioctl that > >>>>> sets an event whitelist. If the guest attempts to program a counter for > >>>>> any unwhitelisted event, the kernel counter won't be created, so any > >>>>> RDPMC/RDMSR will show 0 instances of that event. > >>>> The general idea sounds good to me :) > >>>> > >>>> For the implementation, I would have the following suggestions: > >>>> > >>>> 1) Instead of using a whitelist, it would be better to use a blacklist to > >>>> forbid the guest from counting any core level information. So by default, > >>>> kvm maintains a list of those core level events, which are not supported to > >>>> the guest. > >>>> > >>>> The userspace ioctl removes the related events from the blacklist to > >>>> make them usable by the guest. > >>>> > >>>> 2) Use vm ioctl, instead of vcpu ioctl. The blacklist-ed events can be > >>>> VM wide > >>>> (unnecessary to make each CPU to maintain the same copy). > >>>> Accordingly, put the pmu event blacklist into kvm->arch. > >>>> > >>>> 3) Returning 1 when the guest tries to set the evetlsel msr to count an > >>>> event which is on the blacklist. > >>>> > >>>> Best, > >>>> Wei > >>> Thanks for the feedback. I have a couple concerns with a KVM > >>> maintained blacklist. First, I'm worried it will be difficult to keep > >>> such a list up to date and accurate (both coming up with the initial > >>> list since there are so many events, and updating it whenever any new > >>> events are published or vulnerabilities are discovered). > >> Not sure about "so many" above. I think there should be much > >> fewer events that may need to be blacklisted. > >> > >> For example the event table 19-3 from SDM 19.2 shows hundreds of > >> events, how many of them would you think that need to be blacklisted? > >> > >>> Second, users > >>> may want to differentiate between whole-socket and sub-socket VMs > >>> (some events may be fine for the whole-socket case) - keeping a single > >>> blacklist wouldn't allow for this. > >> Why wouldn't? > >> In any case (e.g. the whole socket dedicated to the single VM) we > >> want to unlock the blacklisted events, we can have the userspace > >> (e.g. qemu command line options "+event1, +event2") do ioctl to > >> have KVM do that. > >> > >> Btw, for the L3 cache stats event example, I'm not sure if that could > >> be an issue if we have "AnyThread=0". I'll double confirm with > >> someone. > >> > >> Best, > >> Wei > >> Not sure about "so many" above. I think there should be much > >> fewer events that may need to be blacklisted. > > I think you're right that there are not as many events that seem like > > they could leak info as events that seem like they won't, but I think > > the work to validate that they definitely don't could be expensive; > > with a whitelist it's easy to start with a smaller set and > > incrementally add to it without having to evaluate all the events > > right away. > > Before going that whitelist/blacklist direction, do you have an event > example that couldn't be solved by setting "AnyThread=0"? > > If no, I think we could simply gate guest's setting of "AnyThread=0". > > Best, > Wei With anythread=0, I'm not aware of any events that directly give info about other VMs, but monitoring events related to shared resources (e.g. LLC References and LLC Misses) could indirectly give you info about how heavily other users are using that resource. I tried returning 1 when the guest tries to write the eventsel msr for a disallowed event - the behavior on modern guest kernels looks reasonable (warns once about an unchecked MSR access error), but it looks like guests using older kernels (older than 2016) might panic due to the gpfault (not to mention I'm not sure about the behavior on non-linux kernels). So I'm hesitant to return 1 - what do you think? I also looked into moving from a vcpu ioctl to a vm ioctl - I can send out a version of the patch with this change once we settle on the other issues. It will involve some extra locking every time the counters are programmed to ensure the whitelist or blacklist isn't removed during access. Eric