Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3080882ybt; Mon, 29 Jun 2020 14:50:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQYLVhTvvq7P4AxIZIYTDWrqUuwm0zUYFNgW97HYeGytGE/yiFR9XDN8F1PZ1TfjNPt+Xx X-Received: by 2002:a17:906:87c8:: with SMTP id zb8mr15417928ejb.35.1593467440437; Mon, 29 Jun 2020 14:50:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593467440; cv=none; d=google.com; s=arc-20160816; b=igKrcpHgi9fxDG0Ag/75lo7IpZ1WOOSlAP+EXljmhfnFu6gQfwiEH5pi42l/hyHffV Dv6tVzuwd0bV6KJsqdNidLkq77AUcxaM0vXjX7eLjmVP5EogkwVhAPvqU3buqvzl6hkz gXC0FYpy5L6R/6mvoF0t63HKEWbjA6Q+ahbMeP8aRbK2qPnZmNoxkDdHaITmhJx5vN2L h/uEySDZBgaF2ZxRa8beLGI5K1Y6IKyBiOmFDZM88VnOS6e4avmoZ85rH7CCn5cmnCXE 5+Cl2GvyGZBZ4LVBiZtWItdpXwaQZ+EhVpLBnoX6nDozVDRZB5dd9OtLsz+LSjXRd2/4 XNQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GsTjfsJvOeryzZQe8ExcKwoj9CguK8GsgyPkfVARt2g=; b=VW4zRClS7GnwKd9rOy5XHIJey58yc3cdo/AktX34IXERG3G2TBAbM7FD7JbH4s/MbJ xtHlCCMx4Tk+JLlVys8IXSEDXJw6h73IDIi+C96jjvSKdjUepoMr4v2pouVgJiP4TQAZ M2sIIZ65lIBIxAzKCxDlZ7i4tngGaFwyjy0Cm3raO1d+NQUS1ep7ZKSNWYfK3RuIwnMl ziL2gmuIq6ZfkAWsGznXeXbYgDBbWK7XFtsdVRWcZFOqYUowHUONWNJYYFuASx6jDcdc OPhVG6N1eeQwzSGjSL3YBQft9WyLzaRkC5Ydzawf6qbajrg4GrLYL964votcX3IbaZyN 2kQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EfLCgj9g; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jo13si570905ejb.198.2020.06.29.14.50.17; Mon, 29 Jun 2020 14:50:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EfLCgj9g; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2403910AbgF2VtJ (ORCPT + 99 others); Mon, 29 Jun 2020 17:49:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:56886 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726689AbgF2Sfl (ORCPT ); Mon, 29 Jun 2020 14:35:41 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 890122479F; Mon, 29 Jun 2020 15:21:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1593444108; bh=Top0jrTBsS+WN/rfoZdoiU42xWmYKRZ9EXLESTgsQtw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EfLCgj9gBY0FcqFBtn7BTh9djoSzJ8jknpACV1ZnQ+vc/I5cffiHZjr3SNGayTXQQ YervzJ5H1xkcFBu2kLF3O0a6yE2Ii8eTc/ItORY+u0Ie4VACjiBaXSJRc2c+j/4cE2 iX1DSRijr5sB8MMTO9Tv6NRoJYhnurDPUm/xu8gk= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Kees Cook , Thomas Gleixner , Greg Kroah-Hartman Subject: [PATCH 5.7 219/265] x86/cpu: Use pinning mask for CR4 bits needing to be 0 Date: Mon, 29 Jun 2020 11:17:32 -0400 Message-Id: <20200629151818.2493727-220-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200629151818.2493727-1-sashal@kernel.org> References: <20200629151818.2493727-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.7.7-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.7.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.7.7-rc1 X-KernelTest-Deadline: 2020-07-01T15:14+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kees Cook commit a13b9d0b97211579ea63b96c606de79b963c0f47 upstream. The X86_CR4_FSGSBASE bit of CR4 should not change after boot[1]. Older kernels should enforce this bit to zero, and newer kernels need to enforce it depending on boot-time configuration (e.g. "nofsgsbase"). To support a pinned bit being either 1 or 0, use an explicit mask in combination with the expected pinned bit values. [1] https://lore.kernel.org/lkml/20200527103147.GI325280@hirez.programming.kicks-ass.net Signed-off-by: Kees Cook Signed-off-by: Thomas Gleixner Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/202006082013.71E29A42@keescook Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/cpu/common.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index e2e4af685f9b4..c669a5756bdf6 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -348,6 +348,9 @@ out: cr4_clear_bits(X86_CR4_UMIP); } +/* These bits should not change their value after CPU init is finished. */ +static const unsigned long cr4_pinned_mask = + X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP | X86_CR4_FSGSBASE; static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning); static unsigned long cr4_pinned_bits __ro_after_init; @@ -372,20 +375,20 @@ EXPORT_SYMBOL(native_write_cr0); void native_write_cr4(unsigned long val) { - unsigned long bits_missing = 0; + unsigned long bits_changed = 0; set_register: asm volatile("mov %0,%%cr4": "+r" (val), "+m" (cr4_pinned_bits)); if (static_branch_likely(&cr_pinning)) { - if (unlikely((val & cr4_pinned_bits) != cr4_pinned_bits)) { - bits_missing = ~val & cr4_pinned_bits; - val |= bits_missing; + if (unlikely((val & cr4_pinned_mask) != cr4_pinned_bits)) { + bits_changed = (val & cr4_pinned_mask) ^ cr4_pinned_bits; + val = (val & ~cr4_pinned_mask) | cr4_pinned_bits; goto set_register; } - /* Warn after we've set the missing bits. */ - WARN_ONCE(bits_missing, "CR4 bits went missing: %lx!?\n", - bits_missing); + /* Warn after we've corrected the changed bits. */ + WARN_ONCE(bits_changed, "pinned CR4 bits changed: 0x%lx!?\n", + bits_changed); } } EXPORT_SYMBOL(native_write_cr4); @@ -397,7 +400,7 @@ void cr4_init(void) if (boot_cpu_has(X86_FEATURE_PCID)) cr4 |= X86_CR4_PCIDE; if (static_branch_likely(&cr_pinning)) - cr4 |= cr4_pinned_bits; + cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits; __write_cr4(cr4); @@ -412,10 +415,7 @@ void cr4_init(void) */ static void __init setup_cr_pinning(void) { - unsigned long mask; - - mask = (X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP); - cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & mask; + cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask; static_key_enable(&cr_pinning.key); } -- 2.25.1