Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp3354039ybl; Mon, 19 Aug 2019 17:20:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqyatnuD59yUlOFIGns/EjAiPbYomKvMp2CwuF44DwiHgJDmvR0KrPeZojQ42nrE08UuIh9c X-Received: by 2002:a63:2157:: with SMTP id s23mr22935663pgm.167.1566260436300; Mon, 19 Aug 2019 17:20:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566260436; cv=none; d=google.com; s=arc-20160816; b=1FtlAAyKaY29AQAgybcQmbJ3pB+nt4auvd1dUQE6BiTsp2xDe6OayS7Yv5pO4E1j9l fBL/OiLtXUiQKjnCqsU52L79Lpdso+S+AmvZx2sib6tCONrclLMJea5voBdcAQU55SNp YxsXtsmhNOAeCrFUxPAM18mM9nZdTi1CmXZ9ztFf4NyEXw9bzhNOnE59ipeUJ+od/MC6 Qo+73s00grtj/nfobnSginaY4xd/HAiLCD3k1BWvw/QZzy+xyzEEtgK0GjiMJqFJTsyF ddsK9lDFVL8+fBelzcLUKy96cyxMmFby7/AAQTB5RC7gAb8IbxZBaVsVRZWCKW3eeDKO ogbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=28L+ehB4vT7SQwhr1GVEAssWYqpFu3ff7CisXtvvxdY=; b=kzomDshF/9wfrckRWXGJIsxvRBQ25Hlad4XX6eNzS58E1s0gpBSQKj2k2t4rGg2fcc wbRkzWfnj5mhp1cBnYUIKh46LP9zQc4AvY2XHStZli/6l5IBFIvBwqlsJOfhX4X1vqpW VUuGrugUDeMFTbImCPJMJhkRGkG2DczsWGgNCZ5/WiJOhoVm4bV0B4rygL4V/Sui5IfI 5FcvFhEb1G71uZUAhxZg+o8X22aL+lLHkKIR+HzaaH58S5IiRwEGPyPUxFJBiHFG7yrX O8m8CbEVimhSA9UvYqaYKEjK4oRq28tDR8EBgVn/hMyCBJGxxMGzrWhdy27GofYyAmnH WtyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=SvGMP+S6; 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 i2si10907481pgk.534.2019.08.19.17.20.21; Mon, 19 Aug 2019 17:20:36 -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=SvGMP+S6; 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 S1729054AbfHTASs (ORCPT + 99 others); Mon, 19 Aug 2019 20:18:48 -0400 Received: from mail-pl1-f201.google.com ([209.85.214.201]:43921 "EHLO mail-pl1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729031AbfHTASp (ORCPT ); Mon, 19 Aug 2019 20:18:45 -0400 Received: by mail-pl1-f201.google.com with SMTP id go13so2937919plb.10 for ; Mon, 19 Aug 2019 17:18:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=28L+ehB4vT7SQwhr1GVEAssWYqpFu3ff7CisXtvvxdY=; b=SvGMP+S6tMmZzB+fw4HPKsKm+2MzyO3OimsS0WVncAijxpLHKoBqaTGUEm9cRs2F3g JFUqTBIEbmfxGZMI708DrwMp7SC9bYp0+w98WIkUwTk7xD2TQ/plYib5XDtlmzInzPK4 Bl15YFCF7X65kaqgC0Jq7joAWDh7/aaHOFj97YCrlK/WTgaq58IRXzDQei3jcJd9JEf1 8zt90+b0w3jm7dsVXxx3U8USdMIbQTQegNG5MEYO04d4lN/NdioBvpCVsa65qQjwjwpJ bww+t43WooMK364Z+SmADURskaIOmMQDM/zOyPpKkUTMMVVLy5hhJzfDuMzR0cb4GqD2 /bQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=28L+ehB4vT7SQwhr1GVEAssWYqpFu3ff7CisXtvvxdY=; b=jvYY1nzfA9MTiq2nTxAWBrRYo+RdownRS3H0NlQhE/Yh1cwbBl7OwoWBF87Gj4NXqf UJEmwsQruRApFnEqbXDGZWAIn4gQc8zNsxyXCB9KQk8AUIPQ+2pNKj/IRC8uLt/MN44s ek7Gj/tNTbO6aufLLWCE9tOIOcCF+f7nTKFJYpTWl+Oq4cKC9sxuCpULZDFSCJtlH/Kj DpuKewq6c7NtM7q9XskvLvkr5xvKw/60GjMASCs2LfvI0WA7F61cCnvLWtJ/mh06zRAh kN7GcTlR6U3/u9kx5+aRyRCDq23DPRZ56Gwogo1AGl/SbKrhPu+4xNXUmm5u0U4Hn1i6 OfUw== X-Gm-Message-State: APjAAAV52V1c3UQygHLa/hUexMVQWJYhXILg65yZCCpV/nXTxU+/4i/6 QgDZceL6kz7EpYjS9NEwBQztZgA69p+UQhsyrIIj3g== X-Received: by 2002:a65:4304:: with SMTP id j4mr22959013pgq.419.1566260323884; Mon, 19 Aug 2019 17:18:43 -0700 (PDT) Date: Mon, 19 Aug 2019 17:17:49 -0700 In-Reply-To: <20190820001805.241928-1-matthewgarrett@google.com> Message-Id: <20190820001805.241928-14-matthewgarrett@google.com> Mime-Version: 1.0 References: <20190820001805.241928-1-matthewgarrett@google.com> X-Mailer: git-send-email 2.23.0.rc1.153.gdeed80330f-goog Subject: [PATCH V40 13/29] x86/msr: Restrict MSR access when the kernel is locked down From: Matthew Garrett To: jmorris@namei.org Cc: linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Matthew Garrett , Matthew Garrett , David Howells , Kees Cook , Thomas Gleixner , x86@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 From: Matthew Garrett Writing to MSRs should not be allowed if the kernel is locked down, since it could lead to execution of arbitrary code in kernel mode. Based on a patch by Kees Cook. Signed-off-by: Matthew Garrett Signed-off-by: David Howells Acked-by: Kees Cook Reviewed-by: Thomas Gleixner cc: x86@kernel.org Signed-off-by: James Morris --- arch/x86/kernel/msr.c | 8 ++++++++ include/linux/security.h | 1 + security/lockdown/lockdown.c | 1 + 3 files changed, 10 insertions(+) diff --git a/arch/x86/kernel/msr.c b/arch/x86/kernel/msr.c index 3db2252b958d..1547be359d7f 100644 --- a/arch/x86/kernel/msr.c +++ b/arch/x86/kernel/msr.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -79,6 +80,10 @@ static ssize_t msr_write(struct file *file, const char __user *buf, int err = 0; ssize_t bytes = 0; + err = security_locked_down(LOCKDOWN_MSR); + if (err) + return err; + if (count % 8) return -EINVAL; /* Invalid chunk size */ @@ -130,6 +135,9 @@ static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg) err = -EFAULT; break; } + err = security_locked_down(LOCKDOWN_MSR); + if (err) + break; err = wrmsr_safe_regs_on_cpu(cpu, regs); if (err) break; diff --git a/include/linux/security.h b/include/linux/security.h index cd93fa5d3c6d..010637a79eac 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -109,6 +109,7 @@ enum lockdown_reason { LOCKDOWN_HIBERNATION, LOCKDOWN_PCI_ACCESS, LOCKDOWN_IOPORT, + LOCKDOWN_MSR, LOCKDOWN_INTEGRITY_MAX, LOCKDOWN_CONFIDENTIALITY_MAX, }; diff --git a/security/lockdown/lockdown.c b/security/lockdown/lockdown.c index 8b7d65dbb086..b1c1c72440d5 100644 --- a/security/lockdown/lockdown.c +++ b/security/lockdown/lockdown.c @@ -24,6 +24,7 @@ static char *lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = { [LOCKDOWN_HIBERNATION] = "hibernation", [LOCKDOWN_PCI_ACCESS] = "direct PCI access", [LOCKDOWN_IOPORT] = "raw io port access", + [LOCKDOWN_MSR] = "raw MSR access", [LOCKDOWN_INTEGRITY_MAX] = "integrity", [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality", }; -- 2.23.0.rc1.153.gdeed80330f-goog