Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp4527853imb; Wed, 6 Mar 2019 16:02:20 -0800 (PST) X-Google-Smtp-Source: APXvYqy6S5V0pyn1VvWAE+UQ41hKnLFInqFR21bunLGe6V3eR1oZZkfOZTYDqDcleT5zjvczBmUu X-Received: by 2002:aa7:8c48:: with SMTP id e8mr9883176pfd.166.1551916940527; Wed, 06 Mar 2019 16:02:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551916940; cv=none; d=google.com; s=arc-20160816; b=vRNk19ICiekOFwvIkoCGErXCEmL0pyOxK8nRmLHjSkiscN75tewP+J/61DtOTmY9yy NZG8vsAe3qVwsAgk22CzcEbf60LGQ+xNDDlcj0/js0IW+cs8AcHMdj1YekDccCg9Berd N6SLRBLAnI4E23Brd5eIurW+KzSmoae4Lf9yzoAHiWLMSUy4EFDFlFIrnqcpG74NRjV8 cehmRmbja1r1xc4Zg8poXXFWZ3Crwf9SEHSp0flftCMt1g7/6t+uWhiVgB1jCoXVDo8g LL5S0DhsOxcxPwTAEzyi2o+9B8eoca5BsY+MiaMCA8tPWaA1qGLUTU99lJ9AyKQaMHAL orbg== 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=EgD7M29mELKbRq1NWt6DNZZmbYMYEW26ecUsjbEvlVs=; b=r0XsSrrYIAkLm4Ao5qPlviTKJPKnU5t5Qdb3JZC1J2rtkenmz0bf5DYOJfS9kVnD0n CyZdm4zLtkAntxKmEwXwA/aqtdmzqaQwOSuufu4/CnI08dxzMId54d6QK4nSHjx55Ipe 8WaofQPeyoLDXdIOU882seLuNTyfyomU7gAI/wpsxk+uvpY/aknSs8Zlj46Rkz599SXk Kjs6vOfKU26ZjD0DCB9gF+xV9qV14g7BRCVVUgqzjntJrQnGETr0uwqSzcYfgQiSsfOF R3fvo55LKcldOIgv6QIend4kcnMLTIemV4DTFY5YMTch1Upj/H0IC//4+iTgMVBIsJFq em4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=gYTOWof+; 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 c10si2554230pgd.18.2019.03.06.16.02.05; Wed, 06 Mar 2019 16:02:20 -0800 (PST) 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=gYTOWof+; 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 S1726545AbfCGAAD (ORCPT + 99 others); Wed, 6 Mar 2019 19:00:03 -0500 Received: from mail-qk1-f201.google.com ([209.85.222.201]:53005 "EHLO mail-qk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726127AbfCGAAB (ORCPT ); Wed, 6 Mar 2019 19:00:01 -0500 Received: by mail-qk1-f201.google.com with SMTP id k21so11618578qkg.19 for ; Wed, 06 Mar 2019 16:00:00 -0800 (PST) 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=EgD7M29mELKbRq1NWt6DNZZmbYMYEW26ecUsjbEvlVs=; b=gYTOWof+SnJgMS2LMqTCUBSp6zvC0gCXrCSqXNr50gmxewWj8i1zMYIleOGZoVxMPQ LF37ClnmwpqbzLw2xE8kWW8XNteLVIsCet67X232y7QE+kb57re8nGck768NviwV1t5l fJecS05fRNQ9WKgzKmIbkiqL82Z1S8X8Npca0z9r7YkeAgAo8zFrxNeTjgbZqXxrCkcm d5+Jr2N6cwpx+0SPxEEmQS6+b1WW3yrKkoGtM7svV8BvQzcAOUIWyFSYyYDHqfdpPkB7 D/Mk96oFbG+YOC/1Yyf5ws/IFrQ2VPf7EEq49q2+f/jbjKa3lmOtkfTb8PWvjD0Pp2pF U5mg== 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=EgD7M29mELKbRq1NWt6DNZZmbYMYEW26ecUsjbEvlVs=; b=jrt5bIJIuudbyCS+ArlW0aaJjTij4yzaaVrG9tZ7vknopxGPVKt6gXBD11L2wDi2Vl GqHk6ns6Jp1gh62QyBleHy6BW9I4DvNjo/ntDkZgeH00uIbmGIos1jvgeT/i6aqSzOMu YWORNs5elFKPloRd/bi9zCiAuMsV64a6aNPbUtnFruFviTkoKN0S+Gg0Y5DW44sPr0hM vpz7ePGlP6iuQFykGBEmTWL55+GFAMZeehPOFfbLspFwxvncP9++RFotdgq5iaXq9f9S iLBJSDVYiCjttvo90qnfIGXcLQhTVwzaZAXrL6UpBG9kF3E8s5spujvVrTkK27bLzhcN +FvA== X-Gm-Message-State: APjAAAV7xMR0BVWjFZ0FXCRRaL+KWYmh9Fpe8N4pYKMzh6a4yRJKjRXZ K3L942Pf7nm2hEBktVf9ulh33UOUATMNkC2AuYNjFQ== X-Received: by 2002:aed:37c4:: with SMTP id j62mr5900910qtb.19.1551916799883; Wed, 06 Mar 2019 15:59:59 -0800 (PST) Date: Wed, 6 Mar 2019 15:58:59 -0800 In-Reply-To: <20190306235913.6631-1-matthewgarrett@google.com> Message-Id: <20190306235913.6631-14-matthewgarrett@google.com> Mime-Version: 1.0 References: <20190306235913.6631-1-matthewgarrett@google.com> X-Mailer: git-send-email 2.21.0.352.gf09ad66450-goog Subject: [PATCH 13/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 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 Reviewed-by: "Lee, Chun-Yi" 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.352.gf09ad66450-goog