public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: tip-bot for Dave Hansen <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: dave.hansen@linux.intel.com, hpa@zytor.com, mingo@kernel.org,
	tglx@linutronix.de, linux-kernel@vger.kernel.org
Subject: [tip:x86/urgent] x86/pkeys: Check against max pkey to avoid overflows
Date: Wed, 1 Mar 2017 01:57:36 -0800	[thread overview]
Message-ID: <tip-58ab9a088ddac4efe823471275859d64f735577e@git.kernel.org> (raw)
In-Reply-To: <20170223222603.A022ED65@viggo.jf.intel.com>

Commit-ID:  58ab9a088ddac4efe823471275859d64f735577e
Gitweb:     http://git.kernel.org/tip/58ab9a088ddac4efe823471275859d64f735577e
Author:     Dave Hansen <dave.hansen@linux.intel.com>
AuthorDate: Thu, 23 Feb 2017 14:26:03 -0800
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Wed, 1 Mar 2017 10:51:50 +0100

x86/pkeys: Check against max pkey to avoid overflows

Kirill reported a warning from UBSAN about undefined behavior when using
protection keys.  He is running on hardware that actually has support for
it, which is not widely available.

The warning triggers because of very large shifts of integers when doing a
pkey_free() of a large, invalid value. This happens because we never check
that the pkey "fits" into the mm_pkey_allocation_map().

I do not believe there is any danger here of anything bad happening
other than some aliasing issues where somebody could do:

	pkey_free(35);

and the kernel would effectively execute:

	pkey_free(8);

While this might be confusing to an app that was doing something stupid, it
has to do something stupid and the effects are limited to the app shooting
itself in the foot.

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: stable@vger.kernel.org
Cc: linux-kselftest@vger.kernel.org
Cc: shuah@kernel.org
Cc: kirill.shutemov@linux.intel.com
Link: http://lkml.kernel.org/r/20170223222603.A022ED65@viggo.jf.intel.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 arch/x86/include/asm/pkeys.h | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/arch/x86/include/asm/pkeys.h b/arch/x86/include/asm/pkeys.h
index 34684ad..b3b09b9 100644
--- a/arch/x86/include/asm/pkeys.h
+++ b/arch/x86/include/asm/pkeys.h
@@ -46,6 +46,15 @@ extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 static inline
 bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
 {
+	/*
+	 * "Allocated" pkeys are those that have been returned
+	 * from pkey_alloc().  pkey 0 is special, and never
+	 * returned from pkey_alloc().
+	 */
+	if (pkey <= 0)
+		return false;
+	if (pkey >= arch_max_pkey())
+		return false;
 	return mm_pkey_allocation_map(mm) & (1U << pkey);
 }
 
@@ -82,12 +91,6 @@ int mm_pkey_alloc(struct mm_struct *mm)
 static inline
 int mm_pkey_free(struct mm_struct *mm, int pkey)
 {
-	/*
-	 * pkey 0 is special, always allocated and can never
-	 * be freed.
-	 */
-	if (!pkey)
-		return -EINVAL;
 	if (!mm_pkey_is_allocated(mm, pkey))
 		return -EINVAL;
 

      parent reply	other threads:[~2017-03-01 10:44 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-23 22:26 [PATCH 1/2] x86, pkeys: check against max pkey to avoid overflows Dave Hansen
2017-02-23 22:26 ` [PATCH 2/2] selftests, x86, pkeys: test with random, unallocated protection keys Dave Hansen
2017-02-23 22:36   ` Shuah Khan
2017-02-24  7:45     ` Ingo Molnar
2017-02-24 14:41       ` Shuah Khan
2017-02-25  9:10         ` Ingo Molnar
2017-02-24  0:10   ` Kirill A. Shutemov
2017-02-24  0:07 ` [PATCH 1/2] x86, pkeys: check against max pkey to avoid overflows Kirill A. Shutemov
2017-03-01  9:57 ` tip-bot for Dave Hansen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tip-58ab9a088ddac4efe823471275859d64f735577e@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox