From patchwork Mon Mar 25 22:09:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Garrett X-Patchwork-Id: 10870225 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EA4A414DE for ; Mon, 25 Mar 2019 22:12:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D526928C1D for ; Mon, 25 Mar 2019 22:12:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C9A832905C; Mon, 25 Mar 2019 22:12:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_HI,USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 711E028C1D for ; Mon, 25 Mar 2019 22:12:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730930AbfCYWKb (ORCPT ); Mon, 25 Mar 2019 18:10:31 -0400 Received: from mail-qt1-f202.google.com ([209.85.160.202]:39711 "EHLO mail-qt1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730979AbfCYWKa (ORCPT ); Mon, 25 Mar 2019 18:10:30 -0400 Received: by mail-qt1-f202.google.com with SMTP id n13so11726642qtn.6 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=KFB16Epvf72t790HUSYSiZb2VQbSJeLhG7K1V+4h/mnaow9a1jotRB03vJIZ4mwdAV A8Xx1XCAqRI8IdWW+g1VrjDa/FURMQtQ2KymkgGF1OcvBZXB1Xqbt4DoCourIEvVnswS og6vO0v/DfMHUUXIEiO6OJ5xWJrEmhi5ire8Xuf46DktQVjuBjCIWjG21XsoVQC2IXHr EY25yTmSGhAiDUnBoilkMciD3H6PwxJtg5mJ+4YsqkfpU6/p1zL0DxEKyiVYBkdqXOD1 QFpJLhGkASPPQK7Wnx5rQ3vdVc70idGgAyBVv+KwRyPXYntyRtNMMRL9Ztn1YhFOBoag lzxw== X-Gm-Message-State: APjAAAU5yJGDqI2Gzo7fs/igWCcbdaohDGtXSDyJ4KBM6kJN9y/1AIq5 d5URma0Y27Ei9cGZHKY7ILJkoyUGEQwCFXI8dXElBQ== X-Google-Smtp-Source: APXvYqzdUtz5Gx36/UdEYYKDuhLU2x74e9CV9FTm/rJEs7XSKh22438Jm/2dKgLJLnTdtiHv8PVnyJ2VYXp4UDYxAlqcqw== 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 Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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;