Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6480577ybi; Wed, 31 Jul 2019 15:23:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqzBQ6vuLl6V5VAjof9rzDc4jXI/L8RXiRWQKM27dYwQcyLY3aD9SHrGOmnvZUxGXQRqn0gl X-Received: by 2002:aa7:8d98:: with SMTP id i24mr50251763pfr.199.1564611800854; Wed, 31 Jul 2019 15:23:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564611800; cv=none; d=google.com; s=arc-20160816; b=jTX/3Y2blWiRVcADKwYxCOVw3ImHjw2d9eHMccdSxyfytz7DD7qd2w3os0JneYxBh9 t5jpFQ6xqMNzp6LJZwGNHuVHgN0a6cf4jyURoSYH8T1aJFrPcoeyMPfKmLem3gbNqdEc +ERoVWmL8n2mFvjTBWqndJB1/QJYtegyZoqibeuMXoB0fV6mWPhYaIiSNLZ95hyV5pgl tEcuxfFG/q5479wsrJniDGYEJwy268+bU/xiSkf+RQkKAkzcy2TeFriR+AjgEa/NWb81 tJYI1VTCooX4vxVdKYIc8BlNKHXLjHo1GhLC1kwO2sY6xQo9ockQBlPD99YhDjTZ2WnY G7Ew== 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=q3etb4lqcpfoYcLMBUBDWWlC5fl/teVFWCZH3Y1QNdw=; b=RqB61WPuYpDM29AbsRk1i/7E1fTliYm0UwsjGFF9DmASaRarqaDEdM/PL3kAIMElKb kQm1xBi+izJGhXYzdq5LMFuQRJ2FnoIPRFA/3XKWZJLSFNI9pok+NlIusByqO/OW7UF7 xba4bpQGY+S2+n4w6uS1K/KF9WnkDzm/fmoaGt2Xg2ECUr1aIqXPfGsJQcqyQouqDF9h wtmb+xZLAWNwsbQY6adHemchR0UxZeyz/Q2zH7yKuu5LdQuAtP0xu/6Kbyl8VehGQXkl xQUJ2frBAK5N6WGTCBlQO7njaecCPSpgB8C6jJVtSl0ufQS2WNh5p580xYsEPPV38cR1 33LQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=quGzWel8; 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 207si35916987pfu.258.2019.07.31.15.23.06; Wed, 31 Jul 2019 15:23:20 -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=quGzWel8; 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 S1730796AbfGaWTP (ORCPT + 99 others); Wed, 31 Jul 2019 18:19:15 -0400 Received: from mail-pf1-f201.google.com ([209.85.210.201]:34730 "EHLO mail-pf1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731276AbfGaWRA (ORCPT ); Wed, 31 Jul 2019 18:17:00 -0400 Received: by mail-pf1-f201.google.com with SMTP id i2so44194541pfe.1 for ; Wed, 31 Jul 2019 15:16:59 -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=q3etb4lqcpfoYcLMBUBDWWlC5fl/teVFWCZH3Y1QNdw=; b=quGzWel87vKpTW4m4ti6UC5lPNM8poQ5r/5Gcx2k+UB1of3DzW/ygjWMG+qe/Fd4uM cDTpYU+PDwUIfPSRoB6CQIQj14vfKG771ql+UsouBTpmPik0ygjY67XRnFdi9Kxcg7ZS +bQ9M+2CNh/LhLV+HYPRlRvvArey+z6EEfT82xg/+VTjZ6i//Lq5auWIhNehcWDM1krg UtiiHS1J2R7eUNf8pXRVBs907DgLQKa70qAYzpZJfVOFrM+OBJyVIDoMThdLqXErL6Do Um1IJ+0Apei97QMgGp8BKBMTadz8Hvrz8eHjCdW6y1/DwlwDIl4hwCQ7BCxBPWQ+qcdb GZIA== 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=q3etb4lqcpfoYcLMBUBDWWlC5fl/teVFWCZH3Y1QNdw=; b=ibbAxY8FA8p8XCNAwHJFTcKHGYRSFIgyvi6bhyBQcephi2shvAWDf91KBGwdmHzRcS oMZ0o0G+CRnSnPpesygImuvYk8jlmxigkckikb3dmn969z/bG5LzRvpj5cSL5EGOjMk6 ni2LO20rPMfgZxmGY6v95E8i9ap842wbIzXcHrwiahlzPsilnDJI+1cBNO2vULU9daIj Pfr4LQ+h4xXaEVS513ey+yCipkp6S82XXE63TQUUMYG1dWmmQnQcpqz88EweHbgUTgS5 /uBORWDFTx+P056CiHMf7uOlYyfGIkbyT+z2nJa6JjYXLDw+PjO70dxDy/P3G4GBcS0h tziQ== X-Gm-Message-State: APjAAAVwiNqZWWmY+rAJNRyTTwTBcsI39wmqxFL+wvR5Y0leH9wyYbsI B3/EbTalHb+1tzWe9gE2ou0YOSrLhKdwScqQYfE7yQ== X-Received: by 2002:a63:ab08:: with SMTP id p8mr26982787pgf.340.1564611419264; Wed, 31 Jul 2019 15:16:59 -0700 (PDT) Date: Wed, 31 Jul 2019 15:16:01 -0700 In-Reply-To: <20190731221617.234725-1-matthewgarrett@google.com> Message-Id: <20190731221617.234725-14-matthewgarrett@google.com> Mime-Version: 1.0 References: <20190731221617.234725-1-matthewgarrett@google.com> X-Mailer: git-send-email 2.22.0.770.g0f2c4a37fd-goog Subject: [PATCH V37 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 --- 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 79250b2ffb8f..155ff026eca4 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 316f7cf4e996..d99c0bee739d 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.22.0.770.g0f2c4a37fd-goog