Skip to content

Commit 16171bf

Browse files
hansendcsuryasaimadhu
authored andcommitted
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 <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent e70b100 commit 16171bf

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

arch/x86/include/asm/pkeys.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
#define ARCH_DEFAULT_PKEY 0
66

7+
/*
8+
* If more than 16 keys are ever supported, a thorough audit
9+
* will be necessary to ensure that the types that store key
10+
* numbers and masks have sufficient capacity.
11+
*/
712
#define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1)
813

914
extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,

arch/x86/kernel/fpu/xstate.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,6 @@ const void *get_xsave_field_ptr(int xfeature_nr)
895895

896896
#ifdef CONFIG_ARCH_HAS_PKEYS
897897

898-
#define NR_VALID_PKRU_BITS (CONFIG_NR_PROTECTION_KEYS * 2)
899-
#define PKRU_VALID_MASK (NR_VALID_PKRU_BITS - 1)
900898
/*
901899
* This will go out and modify PKRU register to set the access
902900
* rights for @pkey to @init_val.
@@ -915,6 +913,13 @@ int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
915913
if (!boot_cpu_has(X86_FEATURE_OSPKE))
916914
return -EINVAL;
917915

916+
/*
917+
* This code should only be called with valid 'pkey'
918+
* values originating from in-kernel users. Complain
919+
* if a bad value is observed.
920+
*/
921+
WARN_ON_ONCE(pkey >= arch_max_pkey());
922+
918923
/* Set the bits we need in PKRU: */
919924
if (init_val & PKEY_DISABLE_ACCESS)
920925
new_pkru_bits |= PKRU_AD_BIT;

0 commit comments

Comments
 (0)