stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] x86, pkeys: check against max pkey to avoid overflows
@ 2017-02-23 22:26 Dave Hansen
  2017-02-24  0:07 ` Kirill A. Shutemov
  0 siblings, 1 reply; 2+ messages in thread
From: Dave Hansen @ 2017-02-23 22:26 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, Dave Hansen, kirill.shutemov, shuah, linux-kselftest, stable


From: Dave Hansen <dave.hansen@linux.intel.com>

Kirill got 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 was because we did some very large shifts of integers when
doing a pkey_free() of a large, invalid value 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: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: linux-kselftest@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: x86@kernel.org
Cc: stable@vger.kernel.org
---

 b/arch/x86/include/asm/pkeys.h |   15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff -puN arch/x86/include/asm/pkeys.h~do-max-pkey-check arch/x86/include/asm/pkeys.h
--- a/arch/x86/include/asm/pkeys.h~do-max-pkey-check	2017-02-23 14:17:33.291953218 -0800
+++ b/arch/x86/include/asm/pkeys.h	2017-02-23 14:24:32.573666333 -0800
@@ -46,6 +46,15 @@ extern int __arch_set_user_pkey_access(s
 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;
 
_

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/2] x86, pkeys: check against max pkey to avoid overflows
  2017-02-23 22:26 [PATCH 1/2] x86, pkeys: check against max pkey to avoid overflows Dave Hansen
@ 2017-02-24  0:07 ` Kirill A. Shutemov
  0 siblings, 0 replies; 2+ messages in thread
From: Kirill A. Shutemov @ 2017-02-24  0:07 UTC (permalink / raw)
  To: Dave Hansen
  Cc: linux-kernel, x86, kirill.shutemov, shuah, linux-kselftest,
	stable

On Thu, Feb 23, 2017 at 02:26:03PM -0800, Dave Hansen wrote:
> 
> From: Dave Hansen <dave.hansen@linux.intel.com>
> 
> Kirill got 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 was because we did some very large shifts of integers when
> doing a pkey_free() of a large, invalid value 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>

Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>

-- 
 Kirill A. Shutemov

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-02-24  0:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-02-23 22:26 [PATCH 1/2] x86, pkeys: check against max pkey to avoid overflows Dave Hansen
2017-02-24  0:07 ` Kirill A. Shutemov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).