pkeys: Make pkey unsigned in arch_set_user_pkey_access()
PATCH 1/1 pkeys: Make pkey unsigned in arch_set_user_pkey_access()
From: Ira Weiny <[email protected]>
The WARN_ON check in arch_set_user_pkey_access() in the x86 architecture fails to check for an invalid negative value.
A simple check for less than 0 would fix this issue however, in the call stack below arch_set_user_pkey_access() the pkey should never be negative on any architecture. It is always best to use correct types when possible. x86 only supports 16 keys while ppc supports 32, u8 is therefore large enough for all current architectures and likely those in the future.
Change the type of the pkey passed to arch_set_user_pkey_access() to u8.
To: Dave Hansen <[email protected]>
To: Michael Ellerman <[email protected]>
--- arch/powerpc/include/asm/pkeys.h | 4 ++-- arch/powerpc/mm/book3s64/pkeys.c | 2 +- arch/x86/include/asm/pkeys.h | 4 ++-- arch/x86/kernel/fpu/xstate.c | 2 +- include/linux/pkeys.h | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-)
arch/powerpc/include/asm/pkeys.h +2 -2
@@ -143,9 +143,9 @@ static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,return __arch_override_mprotect_pkey (vma, prot, pkey); } -extern int __arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +extern int __arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val); -static inline int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +static inline int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val) {if (!mmu_has_feature (MMU_FTR_PKEY ))
arch/powerpc/mm/book3s64/pkeys.c +1 -1
@@ -333,7 +333,7 @@ static inline void init_iamr(int pkey, u8 init_bits)* Set the access rights in AMR IAMR and UAMOR registers for @pkey to that * specified in @init_val. */ -int __arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +int __arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val) {u64 new_amr_bits =0x0ul ;
arch/x86/include/asm/pkeys.h +2 -2
@@ -9,7 +9,7 @@*/ #define arch_max_pkey () (cpu_feature_enabled(X86_FEATURE_OSPKE) ? 16 : 1) -extern int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +extern int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val);static inline bool arch_pkeys_enabled (void )
@@ -115,7 +115,7 @@ int mm_pkey_free(struct mm_struct *mm, int pkey)return 0 ; } -extern int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +extern int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val);static inline int vma_pkey (struct vm_area_struct *vma)
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -1068,7 +1068,7 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)* This will go out and modify PKRU register to set the access * rights for @pkey to @init_val. */ -int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val) {u32 old_pkru, new_pkru_bits =0 ;
include/linux/pkeys.h +1 -1
@@ -35,7 +35,7 @@ static inline int mm_pkey_free(struct mm_struct *mm, int pkey)return -EINVAL ; } -static inline int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +static inline int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val) {return 0 ;
Signature · 1 line
2.35.1
Quoted 11 lines · [email protected] writes:
From: Ira Weiny <[email protected]>
The
WARN_ONcheck inarch_set_user_pkey_access()in the x86 architecture fails to check for an invalid negative value.A simple check for less than 0 would fix this issue however, in the call stack below
arch_set_user_pkey_access()the pkey should never be negative on any architecture. It is always best to use correct types when possible. x86 only supports 16 keys while ppc supports 32, u8 is therefore large enough for all current architectures and likely those in the future.
Should we do that as a separate patch? ie, now convert the variable to unsigned int and later switch all the variables to u8? because what we now have is confusing.
static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
unsigned long pkey)
static inline u64 pkey_to_vmflag_bits(u16 pkey)
Quoted 96 lines
Change the type of the pkey passed to
arch_set_user_pkey_access()to u8.To: Dave Hansen <[email protected]>
To: Michael Ellerman <[email protected]>Cc Aneesh Kumar K.V <[email protected]>Signed-off-by Ira Weiny <[email protected]>--- arch/powerpc/include/asm/pkeys.h | 4 ++-- arch/powerpc/mm/book3s64/pkeys.c | 2 +- arch/x86/include/asm/pkeys.h | 4 ++-- arch/x86/kernel/fpu/xstate.c | 2 +- include/linux/pkeys.h | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-)arch/powerpc/include/asm/pkeys.h +2
-2@@ -143,9 +143,9 @@ static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,return __arch_override_mprotect_pkey (vma, prot, pkey); } -extern int __arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +extern int __arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val); -static inline int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +static inline int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val){
arch/powerpc/include/asm/pkeys.h +0
-0if (!mmu_has_feature (MMU_FTR_PKEY ))arch/powerpc/mm/book3s64/pkeys.c +1
-1@@ -333,7 +333,7 @@ static inline void init_iamr(int pkey, u8 init_bits)* Set the access rights in AMR IAMR and UAMOR registers for @pkey to that * specified in @init_val. */ -int __arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +int __arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val) {u64 new_amr_bits =0x0ul ;arch/x86/include/asm/pkeys.h +2
-2@@ -9,7 +9,7 @@*/ #define arch_max_pkey () (cpu_feature_enabled(X86_FEATURE_OSPKE) ? 16 : 1) -extern int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +extern int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val);static inline bool arch_pkeys_enabled (void )@@ -115,7 +115,7 @@ int mm_pkey_free(struct mm_struct *mm, int pkey)return 0 ; } -extern int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +extern int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val);static inline int vma_pkey (struct vm_area_struct *vma)arch/x86/kernel/fpu/xstate.c +1
-1@@ -1068,7 +1068,7 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)* This will go out and modify PKRU register to set the access * rights for @pkey to @init_val. */ -int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val) {u32 old_pkru, new_pkru_bits =0 ;include/linux/pkeys.h +1
-1@@ -35,7 +35,7 @@ static inline int mm_pkey_free(struct mm_struct *mm, int pkey)return -EINVAL ; } -static inline int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +static inline int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val) {return 0 ;Signature · 1 line
2.35.1
Quoted 14 lines · Aneesh Kumar K.V
Quoted 11 lines · [email protected] writes:
From: Ira Weiny <[email protected]>
The
WARN_ONcheck inarch_set_user_pkey_access()in the x86 architecture fails to check for an invalid negative value.A simple check for less than 0 would fix this issue however, in the call stack below
arch_set_user_pkey_access()the pkey should never be negative on any architecture. It is always best to use correct types when possible. x86 only supports 16 keys while ppc supports 32, u8 is therefore large enough for all current architectures and likely those in the future.
Quoted 2 lines
Should we do that as a separate patch? ie, now convert the variable to unsigned int and later switch all the variables to u8?
Maybe.
Quoted 7 lines
because what we
now have is confusing.static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
unsigned long pkey)
static inline u64pkey_to_vmflag_bits(u16 pkey)
This looks like a good cleanup as well. Why not convert arch_calc_vm_prot_bits() and pkey_to_vmflag_bits() to u8? (In another patch.)
This is all a result of this PKS conversation:
https://lore.kernel.org/lkml/Yg8C6UkgfBmQlPSq@iweiny-desk3/
That started me down the path of trying to figure out why 'int' was used for PKRU and I realized that negative values had meaning there which did not apply to me with PKS. So at some point a conversion needs to be made between a 'conceptual pkey' (int) and a real pkey (unsigned) IHMO.
It's no bit deal to split this patch into one which converts to unsigned and then another to u8 (or u16 if there is some arch which may need it that big).
However, digging more:
Is there a reason u16 was used in pkey_to_vmflag_bits()? How about in __pkru_allows_read() in the x86 code? If possible I think u8 should be standardized but I'm ok with u16 if that is preferred.
Also, am I missing something in init_amr() and init_iamr()? I think I could have gone farther and changed init_amr() and init_iamr() right?
From what I can see the argument to use unsigned long vs u8 (or u16) is some expectation that pkeys will grow beyond 256 in number. From what I can see I don't think that is going to happen.
So do we need to do this in two steps?
Ira
Quoted 97 lines
Quoted 97 lines
Change the type of the pkey passed to
arch_set_user_pkey_access()to u8.To: Dave Hansen <[email protected]>
To: Michael Ellerman <[email protected]>Cc Aneesh Kumar K.V <[email protected]>Signed-off-by Ira Weiny <[email protected]>--- arch/powerpc/include/asm/pkeys.h | 4 ++-- arch/powerpc/mm/book3s64/pkeys.c | 2 +- arch/x86/include/asm/pkeys.h | 4 ++-- arch/x86/kernel/fpu/xstate.c | 2 +- include/linux/pkeys.h | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-)arch/powerpc/include/asm/pkeys.h +2
-2@@ -143,9 +143,9 @@ static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,return __arch_override_mprotect_pkey (vma, prot, pkey); } -extern int __arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +extern int __arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val); -static inline int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +static inline int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val){
arch/powerpc/include/asm/pkeys.h +0
-0if (!mmu_has_feature (MMU_FTR_PKEY ))arch/powerpc/mm/book3s64/pkeys.c +1
-1@@ -333,7 +333,7 @@ static inline void init_iamr(int pkey, u8 init_bits)* Set the access rights in AMR IAMR and UAMOR registers for @pkey to that * specified in @init_val. */ -int __arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +int __arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val) {u64 new_amr_bits =0x0ul ;arch/x86/include/asm/pkeys.h +2
-2@@ -9,7 +9,7 @@*/ #define arch_max_pkey () (cpu_feature_enabled(X86_FEATURE_OSPKE) ? 16 : 1) -extern int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +extern int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val);static inline bool arch_pkeys_enabled (void )@@ -115,7 +115,7 @@ int mm_pkey_free(struct mm_struct *mm, int pkey)return 0 ; } -extern int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +extern int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val);static inline int vma_pkey (struct vm_area_struct *vma)arch/x86/kernel/fpu/xstate.c +1
-1@@ -1068,7 +1068,7 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)* This will go out and modify PKRU register to set the access * rights for @pkey to @init_val. */ -int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val) {u32 old_pkru, new_pkru_bits =0 ;include/linux/pkeys.h +1
-1@@ -35,7 +35,7 @@ static inline int mm_pkey_free(struct mm_struct *mm, int pkey)return -EINVAL ; } -static inline int arch_set_user_pkey_access (struct task_struct *tsk,int pkey, +static inline int arch_set_user_pkey_access (struct task_struct *tsk,u8 pkey,unsigned long init_val) {return 0 ;Signature · 1 line
2.35.1