Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4684730ybi; Mon, 15 Jul 2019 13:01:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqw4DsPGOcdeo9lg2mOLOEFO7w4s0KIqgDI/s9x60/2bnClqrj46ayofpjPG4P2MEsa/3MnD X-Received: by 2002:a63:9c5:: with SMTP id 188mr3672353pgj.2.1563220898226; Mon, 15 Jul 2019 13:01:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563220898; cv=none; d=google.com; s=arc-20160816; b=LUgSSiWfAV8ySdVkP+LU2JYh+fyjw6ZY06NeJzSQvA0V6njQM8Mcw3Xp58Xq2T4jvD ap0H5yKuY+lq0UMDmzLfnRODGeUiGnWc1lo3f3ZsX1JE28MpESMxQhRzp9zIK6CzEsms cuas3bQjHyL87KDFVEZWpfGcVYWehy/iMRvTXRa0+MxwmAY8KIKH9cuvamp0Nqxw15lK +x2Kr/AItrcj53vn3JcCAGn5y+N9PhjA164KOdX0eFqlT1W5m3iYOdWRpdAqKIGnfoJY wxerMXxyctnCB74FULxAaDFshmlCchXQh0VLhPKUkyxSzuEFqZqfS4ZItGXXcewtDRuk jPwg== 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=minBZuRPisw7H2/W4JdDr67L/DoNl62iO6bi7/9H078=; b=tCoCWlW+dLeE0vVsZ2ujSdqMtlPctB0+R/cG+3E47iVFGFgSWR0+ep+8J4m2D5UGmb 814wjT8gzUoT2E5pAH+BbKZ1ZkMgcGw6vBNKynDYHCKP/PV6bhwxNKqTbiXnfiCH62JL jnv69Hi99LpElFCHAyugsRfuNu6J0PQkvt4I2yP3W7DSEu4MlaUCx9o+swRX+LrYoZZF mD7eDt2KVtVy6qQtiH2EOhoM2nDx136GaiNuktiMzizfSnUkA57f44Q/1G8LOjNFbj8Y 09q7waqZRr0T4DehSkYERdmiZBpW2xq2ZIfNCBtO23GSMXQ6aeOPQJIYv2Cj0DspA0BM 71hQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=d74tXKiZ; 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 k193si16453390pge.330.2019.07.15.13.01.17; Mon, 15 Jul 2019 13:01:38 -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=d74tXKiZ; 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 S1732461AbfGOUAc (ORCPT + 99 others); Mon, 15 Jul 2019 16:00:32 -0400 Received: from mail-pf1-f202.google.com ([209.85.210.202]:45100 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730764AbfGOUA3 (ORCPT ); Mon, 15 Jul 2019 16:00:29 -0400 Received: by mail-pf1-f202.google.com with SMTP id i27so10858440pfk.12 for ; Mon, 15 Jul 2019 13:00:28 -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=minBZuRPisw7H2/W4JdDr67L/DoNl62iO6bi7/9H078=; b=d74tXKiZoNQe7CxJS6ufF6sO/j8wOIqa3x36LTPJYLKZ9UdNFn6dgCyjw3/Z1aZ7Jc F3DtrAN/vtTDtkmrDzpkxG4Ygv6pu2RnHQgx+UpdbfbCvP9LKb/XEd+WoKaB0truEiZV G30B5f9wQ+I4+G219HkbVJ3pushqH/sfs+uV8CxSJcwLYdl6CKlnuujbj3YPZOuefjN4 IMG0qqIg37wHdHnGXCuiScEUx/4uD4x1aRjE4UtBtO4xlVP3+rg6eU9zIk7E8mbY1+SB 2LR4iXq7zh3pK+SLZHXn08NYLKIY1FDi3EqHL5Eu48yvD8bL4AC0PpEIL8meyHk6F15Z DJMA== 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=minBZuRPisw7H2/W4JdDr67L/DoNl62iO6bi7/9H078=; b=ItuX6KS4ET9ujNhoxvfV0XnFXiVIz+tlJj8nxceosFbATUdyDepOl0qonZXluFyEED XfGrqT3fx8H3LLnDSf/d8uF9a4pEX4n5pHpu/zMWoPRqMJ9gfMbqcPOjnTe4k/ODrCRI Pp8L6js7cNAYcZLIY7Y1K8BzZvYfNgqqrddVV36pSv4x5lndQobcgwak/z1bMA36tKRR a62dlgaut6ObxD6Y9ue0c/XaCO8IYMFOwdaCqIUSdY2oN6lEnLRFw8fGMCjQwvT/fNcU N5Hnt5BQDbksxrE/xT9CNh/dPJblmVL+VUbzcyPXsRvgkorkKZwMtu7x9Aq/bC062zT2 hb7A== X-Gm-Message-State: APjAAAW+s828ImfmhFjs09KpgKXAhUQWW+1TxSr8hpfjMiEdSsekfu++ mbUUaIvCbkfm0jPSiCmlSt4S44HIibd8ws14f4bFEw== X-Received: by 2002:a63:e14d:: with SMTP id h13mr28815497pgk.431.1563220828029; Mon, 15 Jul 2019 13:00:28 -0700 (PDT) Date: Mon, 15 Jul 2019 12:59:30 -0700 In-Reply-To: <20190715195946.223443-1-matthewgarrett@google.com> Message-Id: <20190715195946.223443-14-matthewgarrett@google.com> Mime-Version: 1.0 References: <20190715195946.223443-1-matthewgarrett@google.com> X-Mailer: git-send-email 2.22.0.510.g264f2c817a-goog Subject: [PATCH V35 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.510.g264f2c817a-goog