x86/pkeys: Check against max pkey to avoid overflows
authorDave Hansen <dave.hansen@linux.intel.com>
Thu, 23 Feb 2017 22:26:03 +0000 (14:26 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 12 Mar 2017 05:41:47 +0000 (06:41 +0100)
commitbfb55d4087cfc4346b1ec5a04cbac04e9df10ab4
treef3f0f4774e866ce01cff7b5f427604ffe0dc22db
parent1e6be9c19c1286cdc3e87a27b06038361a0165ab
x86/pkeys: Check against max pkey to avoid overflows

commit 58ab9a088ddac4efe823471275859d64f735577e upstream.

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: 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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/x86/include/asm/pkeys.h