Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp3662559img; Mon, 25 Mar 2019 15:11:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqyXG9MyWBr629xpJSto1g96HkwjasNRqsO4l0CdZG7taMYBvCxWzsgZZwL74MnEh5WejJF7 X-Received: by 2002:a17:902:2c01:: with SMTP id m1mr27612446plb.186.1553551908708; Mon, 25 Mar 2019 15:11:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553551908; cv=none; d=google.com; s=arc-20160816; b=AhaqjPfKha/NdkEQqHPcEJu4Sr9R/snAGDLyyPegLpqGh62YaGg4nce0ljmR22pH7x 9Yi41X5fpbalpOlj6zA2MxnsOXNa5cW3jae/H0BTNaNAE7IY1UuCXUBkKRqNW2dQYLhe d5NZmwDLPtKtklV8ToF+vdbKF5ljXtcofGhOUdUwTz3FLXIVFtULFrk0+P9D6a8EdiKw W1C51ftLovIX/3PTstgMx+HdbLgDmnHSYWM6D+LFB1eWjzmthx8vXyCB50kiPYBSX3DD QGgBGNUk6CtTF8BAN+E60TC2+f5OEo3DCLCsoTo/mwAG/mLrt4KcnXE/4jKgJi9RlkzB 4lxg== 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=X6Y9rMN4d7Ni/+gDAGhZZery08YVnWjdvZABIqrFXus=; b=vqPekzPOTYTjPkPFTEMgMHdh/Z5x6E48InWFZXyMkfmf+DgwZ54re4lzCwYxC1T+7Z 3fdPz/dAPd4zmOVEVWy50y68tzA4ZTvZ72uLZ5LS2QYpvnRUrxTJ6AnaVwSQ67D+e8nF 7fvwldXyFXxgARmB/k7fKoJO5xfSgmjmWEHLN8YK2gEcOvxb59d4XzXZOC/8sy4reE4y 9+7qoTbRUykUz0pmGkzxjMRi20N/+z/lTG2aLE7VFqnBPNQVNC24YpIoOdyHcUoVjNgO jCB4s6HDMi/NmR5ucFPtWfPhHj7G5qiOFuNXjoc87VdJdCrC+vSbPosoG5cJsiARlcd5 SJyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LKDtQH3s; 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 q207si14476380pgq.321.2019.03.25.15.11.34; Mon, 25 Mar 2019 15:11:48 -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=LKDtQH3s; 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 S1730997AbfCYWKb (ORCPT + 99 others); Mon, 25 Mar 2019 18:10:31 -0400 Received: from mail-qk1-f201.google.com ([209.85.222.201]:37319 "EHLO mail-qk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730971AbfCYWKa (ORCPT ); Mon, 25 Mar 2019 18:10:30 -0400 Received: by mail-qk1-f201.google.com with SMTP id f196so9966186qke.4 for ; Mon, 25 Mar 2019 15:10:29 -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=X6Y9rMN4d7Ni/+gDAGhZZery08YVnWjdvZABIqrFXus=; b=LKDtQH3sTINmIHbUCB0iCeSNdNVfCPCiR/gdwGOJ9VGHpyESnabxsI6T5JdopVkytc rta7c6THcAqSYuieUe6En0Sal75bQ8Pe1TjoXjuV7CgLtMFTNuOKo82zGN/IuTjyVUsH S+O505e/+xrAB6M8XuHPCZVm9U7g7w1Ti/ngyH8w5BrjZ2cBPtEAYdhJdz7SuvGQAT3O yxJWocXTenVNZpPLhpPCDpt6TuGkQbE6e0BGEWVEMw3OF2fPtwh60ajUqxkRnQW7OgEO NZbrtnfEmiaV0IcoLOvZoPfk2lXw/0cKkRIXVkBYXD4etYbRGCgWoL8Z52K0LC27xPch JUoA== 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=X6Y9rMN4d7Ni/+gDAGhZZery08YVnWjdvZABIqrFXus=; b=HX+Oe5x3DTXvw3uM5473pR6IlMS5kAzmXrZGiIBwxng+/hznwm5zyerL/gP58Cip2G vWLq8V57KshYbduYCzs0P+A9an5zu3p0LVXlnQa8Ih75Cqi9hbs+UgJEBCWBk/Azh4nQ /gtLk8aI/mi6UtBeJaVjVVlrPImXGDeK+a1oiOekhkSUn/wLs8W4h3a/dabEZReRL64Q ar47RbxNuBBo/npbzacncciFb1vGTBjvntYDbBHLcUtsJS7KfvZlDPcTP9q3Syg92uMZ nuf54o26u9oprvQR5UmUUniCDprQoJ3TuXSM5vDaDQxAJvw2n4aFnAr3JLvyDkQTjMj9 FksA== X-Gm-Message-State: APjAAAXrgYMLsdDiRUof/DA4CARpYI2atUObaX55FBIHgatfF06D1ryt pcD0v9lznuuRbrD44sdGDP2dcJNFTQfGTe/wOQ6Rqw== X-Received: by 2002:a0c:8a54:: with SMTP id 20mr22892221qvu.167.1553551829340; Mon, 25 Mar 2019 15:10:29 -0700 (PDT) Date: Mon, 25 Mar 2019 15:09:39 -0700 In-Reply-To: <20190325220954.29054-1-matthewgarrett@google.com> Message-Id: <20190325220954.29054-13-matthewgarrett@google.com> Mime-Version: 1.0 References: <20190325220954.29054-1-matthewgarrett@google.com> X-Mailer: git-send-email 2.21.0.392.gf8f6787159e-goog Subject: [PATCH 12/27] 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, dhowells@redhat.com, Matthew Garrett , Kees Cook , Thomas Gleixner , x86@kernel.org, Matthew Garrett 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. MSR accesses are logged for the purposes of building up a whitelist as per Alan Cox's suggestion. 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: Matthew Garrett --- arch/x86/kernel/msr.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/x86/kernel/msr.c b/arch/x86/kernel/msr.c index 4588414e2561..f5a2cf07972f 100644 --- a/arch/x86/kernel/msr.c +++ b/arch/x86/kernel/msr.c @@ -84,6 +84,11 @@ static ssize_t msr_write(struct file *file, const char __user *buf, int err = 0; ssize_t bytes = 0; + if (kernel_is_locked_down("Direct MSR access")) { + pr_info("Direct access to MSR %x\n", reg); + return -EPERM; + } + if (count % 8) return -EINVAL; /* Invalid chunk size */ @@ -135,6 +140,11 @@ static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg) err = -EFAULT; break; } + if (kernel_is_locked_down("Direct MSR access")) { + pr_info("Direct access to MSR %x\n", regs[1]); /* Display %ecx */ + err = -EPERM; + break; + } err = wrmsr_safe_regs_on_cpu(cpu, regs); if (err) break; -- 2.21.0.392.gf8f6787159e-goog