Skip to contentSkip to conversation

powerpc/mm/book3s54/pkeys: make pkey access check work on execute_only_key

PATCH In review Aneesh Kumar K.V 3 participants 4 messages Jun 27, 2020 – Jun 30, 2020 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 powerpc/mm/book3s54/pkeys: make pkey access check work on execute_only_key

pkey_access_permitted() should not check for pkey is available in UAMOR or not. The kernel needs to do that check only while allocating keys. This also makes sure execute_only_key which is marked as non-manageable via UAMOR gives the right access check return w.r.t pkey_access_permitted().

This fix the page fault loop when using PROT_EXEC as below

addr = mmap(0, page_sz, PROT_EXEC, MAP_FILE | MAP_PRIVATE, fildes, 0); x = *addr);

Fixes c46241a370a6 ("powerpc/pkeys: Check vma before returning key fault error to the user")
---
 arch/powerpc/mm/book3s64/pkeys.c | 3 ---
 1 file changed, 3 deletions(-)
arch/powerpc/mm/book3s64/pkeys.c +0 -3
index 1199fc2bfaec..ca5fcb4bff32 100644
@@ -353,9 +353,6 @@ static bool pkey_access_permitted(int pkey, bool write, bool execute)
 	int pkey_shift;
 	u64 amr;
 
-	if (!is_pkey_enabled(pkey))
-		return true;
-
 	pkey_shift = pkeyshift(pkey);
 	if (execute && !(read_iamr() & (IAMR_EX_BIT << pkey_shift)))
 		return true;
Signature · 1 line
2.26.2
#1 lore raw

PATCH 2/2 powerpc/mm/books64/pkeys: Rename is_pkey_enabled()

Rename is_pkey_enabled() to is_pkey_masked() to better indicates that this check is to make sure the key is available for userspace usage. For it to be made available both the bits in UAMOR should be set to 1 (0b11).

---
 arch/powerpc/mm/book3s64/pkeys.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
arch/powerpc/mm/book3s64/pkeys.c +4 -6
index ca5fcb4bff32..70d760ade922 100644
@@ -206,18 +206,16 @@ static inline void write_uamor(u64 value)
 	mtspr(SPRN_UAMOR, value);
 }
 
-static bool is_pkey_enabled(int pkey)
+static bool is_pkey_masked(int pkey)
 {
 	u64 uamor = read_uamor();
 	u64 pkey_bits = 0x3ul << pkeyshift(pkey);
 	u64 uamor_pkey_bits = (uamor & pkey_bits);
 
 	/*
-	 * Both the bits in UAMOR corresponding to the key should be set or
-	 * reset.
+	 * Both the bits in UAMOR corresponding to the key should be set
 	 */
-	WARN_ON(uamor_pkey_bits && (uamor_pkey_bits != pkey_bits));
-	return !!(uamor_pkey_bits);
+	return (uamor_pkey_bits != pkey_bits);
 }
 
 static inline void init_amr(int pkey, u8 init_bits)
@@ -246,7 +244,7 @@ int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 	u64 new_amr_bits = 0x0ul;
 	u64 new_iamr_bits = 0x0ul;
 
-	if (!is_pkey_enabled(pkey))
+	if (is_pkey_masked(pkey))
 		return -EINVAL;
 
 	if (init_val & PKEY_DISABLE_EXECUTE) {
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH 2/2] powerpc/mm/books64/pkeys: Rename is_pkey_enabled()

Quoted 2 lines · Aneesh Kumar K.V

Rename is_pkey_enabled() to is_pkey_masked() to better indicates that this check is to make sure the key is available for userspace usage.

I don't think the new name makes that any clearer. Unless you know that "masked" means not "available for userspace".

It's also not clear if masked means 00 or 11.

Now that there's only one caller why not just fold it in, that way it doesn't need a name at all.

Quoted 27 lines
arch/powerpc/mm/book3s64/pkeys.c +3 -5
index ca5fcb4bff32..70d760ade922 100644
@@ -206,18 +206,16 @@ static inline void write_uamor(u64 value)
 	mtspr(SPRN_UAMOR, value);
 }
 
-static bool is_pkey_enabled(int pkey)
+static bool is_pkey_masked(int pkey)
 {
 	u64 uamor = read_uamor();
 	u64 pkey_bits = 0x3ul << pkeyshift(pkey);
 	u64 uamor_pkey_bits = (uamor & pkey_bits);
 
 	/*
-	 * Both the bits in UAMOR corresponding to the key should be set or
-	 * reset.
+	 * Both the bits in UAMOR corresponding to the key should be set
 	 */
-	WARN_ON(uamor_pkey_bits && (uamor_pkey_bits != pkey_bits));
-	return !!(uamor_pkey_bits);
+	return (uamor_pkey_bits != pkey_bits);
 }
 
 static inline void init_amr(int pkey, u8 init_bits)

arch/powerpc/mm/book3s64/pkeys.c int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey, · line 248

Quoted 6 lines
arch/powerpc/mm/book3s64/pkeys.c +1 -1
@@ -246,7 +244,7 @@ int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 	u64 new_amr_bits = 0x0ul;
 	u64 new_iamr_bits = 0x0ul;
 
-	if (!is_pkey_enabled(pkey))
+	if (is_pkey_masked(pkey))
 		return -EINVAL;

eg:

	u64 pkey_bits = 0x3ul << pkeyshift(pkey);

	if ((read_uamor() & pkey_bits) != pkey_bits)
        	return -EINVAL;

arch/powerpc/mm/book3s64/pkeys.c int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey, · line 250

Quoted 2 lines
arch/powerpc/mm/book3s64/pkeys.c +0 -0
 	if (init_val & PKEY_DISABLE_EXECUTE) {

cheers

#3 lore raw
In reply to Aneesh Kumar K.V
Quoted 8 lines · Aneesh Kumar K.V

pkey_access_permitted() should not check for pkey is available in UAMOR or not. The kernel needs to do that check only while allocating keys. This also makes sure execute_only_key which is marked as non-manageable via UAMOR gives the right access check return w.r.t pkey_access_permitted().

This fix the page fault loop when using PROT_EXEC as below

[...]

Patch 1 applied to powerpc/fixes.

[1/2] powerpc/mm/pkeys: Make pkey access check work on execute_only_key

      https://git.kernel.org/powerpc/c/19ab500edb5d6020010caba48ce3b4ce4182ab63

cheers