From: Sean Christopherson <sean.j.christopherson@intel.com>
To: Borislav Petkov <bp@alien8.de>
Cc: x86-ml <x86@kernel.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
lkml <linux-kernel@vger.kernel.org>,
Tony Luck <tony.luck@intel.com>
Subject: Re: [PATCH -v2.1] x86/msr: Filter MSR writes
Date: Wed, 24 Jun 2020 22:51:40 -0700 [thread overview]
Message-ID: <20200625055140.GA2141@linux.intel.com> (raw)
In-Reply-To: <20200615063837.GA14668@zn.tnic>
On Mon, Jun 15, 2020 at 08:38:37AM +0200, Borislav Petkov wrote:
> Here's an improved v2 with sparse warnings fixed:
>
> ---
> Disable writing to MSRs from userspace by default. Writes can still be
> allowed by supplying the allow_writes=1 module parameter and the kernel
> will be tainted so that it shows in oopses.
>
> Having unfettered access to all MSRs on a system is and has always been
> a disaster waiting to happen. Think performance counter MSRs, MSRs with
> sticky or locked bits, MSRs making major system changes like loading
> microcode, MTRRs, PAT configuration, TSC counter, security mitigations
> MSRs, you name it.
>
> This also destroys all the kernel's caching of MSR values for
> performance, as the recent case with MSR_AMD64_LS_CFG showed.
>
> Another example is writing MSRs by mistake by simply typing the wrong
> MSR address. System freezes have been experienced that way.
>
> In general, poking at MSRs under the kernel's feet is a bad bad idea.
>
> So disable poking directly at the MSRs by default. If userspace still
> wants to do that, then proper interfaces should be defined which
> are under the kernel's control and accesses to those MSRs can be
> synchronized and sanitized properly.
>
> Changelog:
> - taint before WRMSR, all
> - make param 0600, Sean.
> - do not deny but log writes by default, Linus.
>
> [ Fix sparse warnings ]
> Reported-by: kernel test robot <lkp@intel.com>
> Signed-off-by: Borislav Petkov <bp@suse.de>
A few non-functional nits below.
Tested-by: Sean Christopherson <sean.j.christopherson@intel.com>
> ---
> arch/x86/kernel/msr.c | 69 +++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 69 insertions(+)
>
> diff --git a/arch/x86/kernel/msr.c b/arch/x86/kernel/msr.c
> index 1547be359d7f..576c43e39247 100644
> --- a/arch/x86/kernel/msr.c
> +++ b/arch/x86/kernel/msr.c
> @@ -42,6 +42,14 @@
> static struct class *msr_class;
> static enum cpuhp_state cpuhp_msr_state;
>
> +enum allow_write_msrs {
> + MSR_WRITES_ON,
> + MSR_WRITES_OFF,
> + MSR_WRITES_DEFAULT,
> +};
> +
> +static enum allow_write_msrs allow_writes = MSR_WRITES_DEFAULT;
> +
> static ssize_t msr_read(struct file *file, char __user *buf,
> size_t count, loff_t *ppos)
> {
> @@ -70,6 +78,24 @@ static ssize_t msr_read(struct file *file, char __user *buf,
> return bytes ? bytes : err;
> }
>
> +static int filter_write(u32 reg)
> +{
> + switch (allow_writes) {
> + case MSR_WRITES_ON: return 0; break;
> + case MSR_WRITES_OFF: return -EPERM; break;
The breaks after the returns are unnecessary.
> + default: break;
> + }
> +
> + if (reg == MSR_IA32_ENERGY_PERF_BIAS)
> + return 0;
> +
> + pr_err_ratelimited("Write to unrecognized MSR 0x%x by %s\n"
> + "Please report to x86@kernel.org\n",
> + reg, current->comm);
Maybe s/unrecognized/unauthorized? Unrecognized implies the kernel doesn't
know anything about the MSR being written, which may not hold true.
> + return 0;
> +}
> +
> static ssize_t msr_write(struct file *file, const char __user *buf,
> size_t count, loff_t *ppos)
> {
> @@ -84,6 +110,10 @@ static ssize_t msr_write(struct file *file, const char __user *buf,
> if (err)
> return err;
>
> + err = filter_write(reg);
> + if (err)
> + return err;
> +
> if (count % 8)
> return -EINVAL; /* Invalid chunk size */
>
> @@ -92,9 +122,13 @@ static ssize_t msr_write(struct file *file, const char __user *buf,
> err = -EFAULT;
> break;
> }
> +
> + add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
> +
> err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
> if (err)
> break;
> +
Random leftover whitespace change.
> tmp += 2;
> bytes += 8;
> }
next prev parent reply other threads:[~2020-06-25 5:51 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-12 10:50 [RFC PATCH] x86/msr: Filter MSR writes Borislav Petkov
2020-06-12 16:34 ` Sean Christopherson
2020-06-12 16:46 ` Borislav Petkov
2020-06-12 16:57 ` Sean Christopherson
2020-06-12 17:03 ` Borislav Petkov
2020-06-12 17:43 ` Sean Christopherson
2020-06-12 17:52 ` Borislav Petkov
2020-06-12 17:20 ` Linus Torvalds
2020-06-12 17:48 ` Borislav Petkov
2020-06-12 19:47 ` Borislav Petkov
2020-06-12 20:39 ` Peter Zijlstra
2020-06-13 5:40 ` Tony Luck
2020-06-13 9:39 ` Borislav Petkov
2020-06-13 15:48 ` [PATCH -v2] " Borislav Petkov
2020-06-15 6:38 ` [PATCH -v2.1] " Borislav Petkov
2020-06-25 5:51 ` Sean Christopherson [this message]
2020-06-25 8:37 ` Borislav Petkov
2020-07-14 12:19 ` Chris Down
2020-07-14 15:47 ` Borislav Petkov
2020-07-14 16:04 ` Chris Down
2020-07-14 16:46 ` Luck, Tony
2020-07-14 16:58 ` Borislav Petkov
2020-07-14 17:02 ` Chris Down
2020-07-14 16:56 ` Borislav Petkov
2020-07-14 17:04 ` Chris Down
2020-07-14 18:52 ` Srinivas Pandruvada
2020-07-15 4:26 ` Borislav Petkov
2020-07-14 19:17 ` Matthew Garrett
2020-11-17 21:00 ` Mathieu Chouquet-Stringer
2020-11-17 21:20 ` Borislav Petkov
2020-11-18 8:58 ` Mathieu Chouquet-Stringer
2020-11-18 9:09 ` Mathieu Chouquet-Stringer
2020-11-18 11:50 ` Borislav Petkov
2020-11-18 14:04 ` [PATCH] " Mathieu Chouquet-Stringer
2020-11-18 17:50 ` Borislav Petkov
2020-11-19 10:53 ` Mathieu Chouquet-Stringer
2020-11-25 21:41 ` Mathieu Chouquet-Stringer
2020-11-26 10:03 ` Borislav Petkov
2020-11-17 21:21 ` [PATCH -v2.1] " Matthew Garrett
2020-11-17 21:22 ` Matthew Garrett
2020-11-18 9:02 ` Mathieu Chouquet-Stringer
2020-06-17 15:06 ` [tip: x86/misc] " tip-bot2 for Borislav Petkov
2020-06-25 8:45 ` tip-bot2 for Borislav Petkov
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=20200625055140.GA2141@linux.intel.com \
--to=sean.j.christopherson@intel.com \
--cc=bp@alien8.de \
--cc=linux-kernel@vger.kernel.org \
--cc=tony.luck@intel.com \
--cc=torvalds@linux-foundation.org \
--cc=x86@kernel.org \
/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