Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2791798ybv; Mon, 24 Feb 2020 11:41:46 -0800 (PST) X-Google-Smtp-Source: APXvYqy9/6pCh5K3wbIMB5miMJdLezGMJWTbZ8OZXTmpgUnbDjrOuY1gMEJMhBCSL0pFAi+MojJx X-Received: by 2002:a05:6830:607:: with SMTP id w7mr43802488oti.155.1582573306241; Mon, 24 Feb 2020 11:41:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582573306; cv=none; d=google.com; s=arc-20160816; b=CXXMdR9/uligcxOX3xQ5v+IKEZnCwgXSBchgT75Cf4vXj8V8D9z0Chcse7+D43deGi bwFGfSAx5hlrxEkT1GxMQdTb7mZMDQslZx3RhGH3sjf5f3FmlRudDwkml4WqfC77cvvO GUQMD3vg6fVmBNWPlzmmfSQaMfJZoPpkgD0T0SJc5UvSgMyQfRY9ozY7XlJQ3R01owk/ 7EjgbKHTPlk3ai3tsHvd7a+5zaZ07jgtbi85adh+3ITds/mpwGLE0glVjQqYt5bxWfIP vUHQca9thnO0ruOsBcMIyF6E8JZv0GNcPDSCMqKxCsHrCmzsw3B6VRXAumhMNvRj4DqE NZyQ== 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 :robot-unsubscribe:robot-id:message-id:mime-version:references :in-reply-to:cc:subject:to:reply-to:from:date; bh=wPqgcDUTWEb044Go6IypIdCo4AHOVJvAM25bFnc1ItE=; b=dPG1VrvpuYYFOUZn0yRCpg3P55D3lT0XZQ6htJLfDBZDBZRmGqV0ujhvgC7+tkc8QE AnnfTeeQ15DXGZG7AlQ/wCbEFMLSQioEJFi7b5gVbiZR7F+LRdf1TxF/MGjcOxLRwEO+ CN0wtKJh9lM25sGJsnH4io7ovW+zVI5A1SHJ4KgxNPrqSkUQp6JKI1CV2J7x4Xlop7BK /IowxCjRsBv88MR3YBzjCV5NB04gXKH61BWnZrwOpb0MdAXGIL1xzKYGAYi3KGRDuKxQ cSgU9puPr6zkbhjh+rQbGgfYBoFAIQPN/VXp+v4cWmaZT+VUMLr+s1hlUncgI3YsKp82 LeaA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p22si6588254ota.43.2020.02.24.11.41.33; Mon, 24 Feb 2020 11:41:46 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727278AbgBXTlT (ORCPT + 99 others); Mon, 24 Feb 2020 14:41:19 -0500 Received: from Galois.linutronix.de ([193.142.43.55]:51010 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726452AbgBXTlT (ORCPT ); Mon, 24 Feb 2020 14:41:19 -0500 Received: from [5.158.153.53] (helo=tip-bot2.lab.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1j6JZE-00042F-00; Mon, 24 Feb 2020 20:39:01 +0100 Received: from [127.0.1.1] (localhost [IPv6:::1]) by tip-bot2.lab.linutronix.de (Postfix) with ESMTP id 49DC51C2141; Mon, 24 Feb 2020 20:38:53 +0100 (CET) Date: Mon, 24 Feb 2020 19:38:52 -0000 From: "tip-bot2 for Dave Hansen" Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/fpu] x86/pkeys: Add check for pkey "overflow" Cc: Alex Shi , Dave Hansen , Borislav Petkov , x86 , LKML In-Reply-To: <20200122165346.AD4DA150@viggo.jf.intel.com> References: <20200122165346.AD4DA150@viggo.jf.intel.com> MIME-Version: 1.0 Message-ID: <158257313295.28353.13260588823755271323.tip-bot2@tip-bot2> X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/fpu branch of tip: Commit-ID: 16171bffc829272d5e6014bad48f680cb50943d9 Gitweb: https://git.kernel.org/tip/16171bffc829272d5e6014bad48f680cb50943d9 Author: Dave Hansen AuthorDate: Wed, 22 Jan 2020 08:53:46 -08:00 Committer: Borislav Petkov CommitterDate: Mon, 24 Feb 2020 20:25:21 +01:00 x86/pkeys: Add check for pkey "overflow" Alex Shi reported the pkey macros above arch_set_user_pkey_access() to be unused. They are unused, and even refer to a nonexistent CONFIG option. But, they might have served a good use, which was to ensure that the code does not try to set values that would not fit in the PKRU register. As it stands, a too-large 'pkey' value would be likely to silently overflow the u32 new_pkru_bits. Add a check to look for overflows. Also add a comment to remind any future developer to closely examine the types used to store pkey values if arch_max_pkey() ever changes. This boots and passes the x86 pkey selftests. Reported-by: Alex Shi Signed-off-by: Dave Hansen Signed-off-by: Borislav Petkov Link: https://lkml.kernel.org/r/20200122165346.AD4DA150@viggo.jf.intel.com --- arch/x86/include/asm/pkeys.h | 5 +++++ arch/x86/kernel/fpu/xstate.c | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/pkeys.h b/arch/x86/include/asm/pkeys.h index 19b137f..2ff9b98 100644 --- a/arch/x86/include/asm/pkeys.h +++ b/arch/x86/include/asm/pkeys.h @@ -4,6 +4,11 @@ #define ARCH_DEFAULT_PKEY 0 +/* + * If more than 16 keys are ever supported, a thorough audit + * will be necessary to ensure that the types that store key + * numbers and masks have sufficient capacity. + */ #define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1) extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 73fe597..32b153d 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -895,8 +895,6 @@ const void *get_xsave_field_ptr(int xfeature_nr) #ifdef CONFIG_ARCH_HAS_PKEYS -#define NR_VALID_PKRU_BITS (CONFIG_NR_PROTECTION_KEYS * 2) -#define PKRU_VALID_MASK (NR_VALID_PKRU_BITS - 1) /* * This will go out and modify PKRU register to set the access * rights for @pkey to @init_val. @@ -915,6 +913,13 @@ int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, if (!boot_cpu_has(X86_FEATURE_OSPKE)) return -EINVAL; + /* + * This code should only be called with valid 'pkey' + * values originating from in-kernel users. Complain + * if a bad value is observed. + */ + WARN_ON_ONCE(pkey >= arch_max_pkey()); + /* Set the bits we need in PKRU: */ if (init_val & PKEY_DISABLE_ACCESS) new_pkru_bits |= PKRU_AD_BIT;