Skip to contentSkip to conversation

mm/pkey: Add PKEY_UNRESTRICTED macro

PATCH Superseded Yury Khrustalev 2 participants 6 messages Oct 27, 2024 – Oct 28, 2024

Subsystems (5)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm/pkey: Add PKEY_UNRESTRICTED macro

Add PKEY_UNRESTRICTED macro to mman.h and use it in selftests.

For context, this change will also allow for more consistent update of the Glibc manual [1] which in turn will help with introducing memory protection keys on AArch64 targets [2].

Applies to 42f7652d3eb5 (tag: v6.12-rc4).

Note that I couldn't build ppc tests so I would appreciate if someone could check the 3rd patch. Thank you!

[1] https://inbox.sourceware.org/libc-alpha/[email protected]/
[2] https://inbox.sourceware.org/libc-alpha/[email protected]/

---
Chages in v2:
- Update tools/include/uapi/asm-generic/mman-common.h as well
- Add usages of the new macro to selftests.

Link to v1: https://lore.kernel.org/linux-arch/[email protected]/

---
Yury Khrustalev (3):
mm/pkey: Add PKEY_UNRESTRICTED macro selftests/mm: Use PKEY_UNRESTRICTED macro selftests/powerpc/mm: Use PKEY_UNRESTRICTED macro

 include/uapi/asm-generic/mman-common.h              | 1 +
 tools/include/uapi/asm-generic/mman-common.h        | 1 +
 tools/testing/selftests/mm/mseal_test.c             | 4 ++--
 tools/testing/selftests/mm/pkey-helpers.h           | 3 ++-
 tools/testing/selftests/mm/pkey_sighandler_tests.c  | 4 ++--
 tools/testing/selftests/mm/protection_keys.c        | 2 +-
 tools/testing/selftests/powerpc/mm/pkey_exec_prot.c | 2 +-
 tools/testing/selftests/powerpc/mm/pkey_siginfo.c   | 2 +-
 8 files changed, 11 insertions(+), 8 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH v2 1/3 mm/pkey: Add PKEY_UNRESTRICTED macro

Memory protection keys (pkeys) uapi has two macros for pkeys restrictions:

- PKEY_DISABLE_ACCESS 0x1
- PKEY_DISABLE_WRITE 0x2

with implicit literal value of 0x0 that means "unrestricted". Code that works with pkeys has to use this literal value when implying that a pkey imposes no restrictions. This may reduce readability because 0 can be written in various ways (e.g. 0x0 or 0) and also because 0 in the context of pkeys can be mistaken for "no permissions" (akin PROT_NONE) while it actually means "no restrictions". This is important because pkeys are oftentimes used near mprotect() that uses PROT_ macros.

This patch adds PKEY_UNRESTRICTED macro defined as 0x0.

---
 include/uapi/asm-generic/mman-common.h       | 1 +
 tools/include/uapi/asm-generic/mman-common.h | 1 +
 2 files changed, 2 insertions(+)
include/uapi/asm-generic/mman-common.h +1 -0
index 6ce1f1ceb432..ea40e27e6dea 100644
@@ -82,6 +82,7 @@
 /* compatibility flags */
 #define MAP_FILE	0
 
+#define PKEY_UNRESTRICTED	0x0
 #define PKEY_DISABLE_ACCESS	0x1
 #define PKEY_DISABLE_WRITE	0x2
 #define PKEY_ACCESS_MASK	(PKEY_DISABLE_ACCESS |\
tools/include/uapi/asm-generic/mman-common.h +1 -0
index 6ce1f1ceb432..ea40e27e6dea 100644
@@ -82,6 +82,7 @@
 /* compatibility flags */
 #define MAP_FILE	0
 
+#define PKEY_UNRESTRICTED	0x0
 #define PKEY_DISABLE_ACCESS	0x1
 #define PKEY_DISABLE_WRITE	0x2
 #define PKEY_ACCESS_MASK	(PKEY_DISABLE_ACCESS |\
Signature · 1 line
2.39.5
#2 lore raw

PATCH v2 2/3 selftests/mm: Use PKEY_UNRESTRICTED macro

In reply to Yury Khrustalev

Replace literal 0 with macro PKEY_UNRESTRICTED where pkey_*() functions are used in mm selftests for memory protection keys.

---
 tools/testing/selftests/mm/mseal_test.c            | 4 ++--
 tools/testing/selftests/mm/pkey-helpers.h          | 3 ++-
 tools/testing/selftests/mm/pkey_sighandler_tests.c | 4 ++--
 tools/testing/selftests/mm/protection_keys.c       | 2 +-
 4 files changed, 7 insertions(+), 6 deletions(-)
tools/testing/selftests/mm/mseal_test.c +2 -2
index 01675c412b2a..eab214997de0 100644
@@ -218,7 +218,7 @@ bool seal_support(void)
 bool pkey_supported(void)
 {
 #if defined(__i386__) || defined(__x86_64__) /* arch */
-	int pkey = sys_pkey_alloc(0, 0);
+	int pkey = sys_pkey_alloc(0, PKEY_UNRESTRICTED);
 
 	if (pkey > 0)
 		return true;
@@ -1671,7 +1671,7 @@ static void test_seal_discard_ro_anon_on_pkey(bool seal)
 	setup_single_address_rw(size, &ptr);
 	FAIL_TEST_IF_FALSE(ptr != (void *)-1);
 
-	pkey = sys_pkey_alloc(0, 0);
+	pkey = sys_pkey_alloc(0, PKEY_UNRESTRICTED);
 	FAIL_TEST_IF_FALSE(pkey > 0);
 
 	ret = sys_mprotect_pkey((void *)ptr, size, PROT_READ | PROT_WRITE, pkey);
tools/testing/selftests/mm/pkey-helpers.h +2 -1
index 9ab6a3ee153b..e7fb0fcfcb05 100644
@@ -12,6 +12,7 @@
 #include <stdlib.h>
 #include <ucontext.h>
 #include <sys/mman.h>
+#include <linux/mman.h>
 
 #include "../kselftest.h"
 
@@ -217,7 +218,7 @@ static inline u32 *siginfo_get_pkey_ptr(siginfo_t *si)
 static inline int kernel_has_pkeys(void)
 {
 	/* try allocating a key and see if it succeeds */
-	int ret = sys_pkey_alloc(0, 0);
+	int ret = sys_pkey_alloc(0, PKEY_UNRESTRICTED);
 	if (ret <= 0) {
 		return 0;
 	}
tools/testing/selftests/mm/pkey_sighandler_tests.c +2 -2
index a8088b645ad6..76e85d2cf698 100644
@@ -260,7 +260,7 @@ static void test_sigsegv_handler_with_different_pkey_for_stack(void)
 	__write_pkey_reg(0x55555550);
 
 	/* Protect the new stack with MPK 1 */
-	pkey = pkey_alloc(0, 0);
+	pkey = pkey_alloc(0, PKEY_UNRESTRICTED);
 	pkey_mprotect(stack, STACK_SIZE, PROT_READ | PROT_WRITE, pkey);
 
 	/* Set up alternate signal stack that will use the default MPK */
@@ -421,7 +421,7 @@ static void test_pkru_sigreturn(void)
 	__write_pkey_reg(0x55555544);
 
 	/* Protect the stack with MPK 2 */
-	pkey = pkey_alloc(0, 0);
+	pkey = pkey_alloc(0, PKEY_UNRESTRICTED);
 	pkey_mprotect(stack, STACK_SIZE, PROT_READ | PROT_WRITE, pkey);
 
 	/* Set up alternate signal stack that will use the default MPK */
tools/testing/selftests/mm/protection_keys.c +1 -1
index 4990f7ab4cb7..cca7435a7bc5 100644
@@ -491,7 +491,7 @@ int sys_pkey_alloc(unsigned long flags, unsigned long init_val)
 int alloc_pkey(void)
 {
 	int ret;
-	unsigned long init_val = 0x0;
+	unsigned long init_val = PKEY_UNRESTRICTED;
 
 	dprintf1("%s()::%d, pkey_reg: 0x%016llx shadow: %016llx\n",
 			__func__, __LINE__, __read_pkey_reg(), shadow_pkey_reg);
Signature · 1 line
2.39.5
Reviewed-by on v2 2/3
#3 lore raw

Re: [PATCH v2 2/3] selftests/mm: Use PKEY_UNRESTRICTED macro

Quoted 5 lines · Yury Khrustalev

Replace literal 0 with macro PKEY_UNRESTRICTED where pkey_*() functions are used in mm selftests for memory protection keys.

Quoted 21 lines
---
 tools/testing/selftests/mm/mseal_test.c            | 4 ++--
 tools/testing/selftests/mm/pkey-helpers.h          | 3 ++-
 tools/testing/selftests/mm/pkey_sighandler_tests.c | 4 ++--
 tools/testing/selftests/mm/protection_keys.c       | 2 +-
 4 files changed, 7 insertions(+), 6 deletions(-)
tools/testing/selftests/mm/mseal_test.c +1 -1
index 01675c412b2a..eab214997de0 100644
@@ -218,7 +218,7 @@ bool seal_support(void)
 bool pkey_supported(void)
 {
 #if defined(__i386__) || defined(__x86_64__) /* arch */
-	int pkey = sys_pkey_alloc(0, 0);
+	int pkey = sys_pkey_alloc(0, PKEY_UNRESTRICTED);
 
 	if (pkey > 0)
 		return true;

tools/testing/selftests/mm/mseal_test.c static void test_seal_discard_ro_anon_on_pkey(bool seal) · line 1677

Quoted 8 lines
tools/testing/selftests/mm/mseal_test.c +1 -1
@@ -1671,7 +1671,7 @@ static void test_seal_discard_ro_anon_on_pkey(bool seal)
 	setup_single_address_rw(size, &ptr);
 	FAIL_TEST_IF_FALSE(ptr != (void *)-1);
 
-	pkey = sys_pkey_alloc(0, 0);
+	pkey = sys_pkey_alloc(0, PKEY_UNRESTRICTED);
 	FAIL_TEST_IF_FALSE(pkey > 0);
 
 	ret = sys_mprotect_pkey((void *)ptr, size, PROT_READ | PROT_WRITE, pkey);

Found one more case just below:

(quoted diff) +0 -0

b/tools/testing/selftests/mm/mseal_test.c
index 01675c412b2a..cb0671de1299 100644

tools/testing/selftests/mm/mseal_test.c +0 -0
@@ -1683,7 +1683,7 @@ static void test_seal_discard_ro_anon_on_pkey(bool

seal)
     }

     /* sealing doesn't take effect if PKRU allow write. */ -    set_pkey(pkey, 0);
+    set_pkey(pkey, PKEY_UNRESTRICTED);
     ret = sys_madvise(ptr, size, MADV_DONTNEED); FAIL_TEST_IF_FALSE(!ret);

- Kevin

Quoted 1 line

[...]

#4 lore raw

PATCH v2 3/3 selftests/powerpc/mm: Use PKEY_UNRESTRICTED macro

In reply to Yury Khrustalev

Replace literal 0 with macro PKEY_UNRESTRICTED where pkey_*() functions are used in mm selftests for memory protection keys for ppc target.

Note that I couldn't build these tests so I would appreciate if someone could check this patch. Thank you!

---
 tools/testing/selftests/powerpc/mm/pkey_exec_prot.c | 2 +-
 tools/testing/selftests/powerpc/mm/pkey_siginfo.c   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/powerpc/mm/pkey_exec_prot.c +1 -1
index 0af4f02669a1..29b91b7456eb 100644
@@ -72,7 +72,7 @@ static void segv_handler(int signum, siginfo_t *sinfo, void *ctx)
 
 		switch (fault_type) {
 		case PKEY_DISABLE_ACCESS:
-			pkey_set_rights(fault_pkey, 0);
+			pkey_set_rights(fault_pkey, PKEY_UNRESTRICTED);
 			break;
 		case PKEY_DISABLE_EXECUTE:
 			/*
tools/testing/selftests/powerpc/mm/pkey_siginfo.c +1 -1
index 2db76e56d4cb..e89a164c686b 100644
@@ -83,7 +83,7 @@ static void segv_handler(int signum, siginfo_t *sinfo, void *ctx)
 	    mprotect(pgstart, pgsize, PROT_EXEC))
 		_exit(1);
 	else
-		pkey_set_rights(pkey, 0);
+		pkey_set_rights(pkey, PKEY_UNRESTRICTED);
 
 	fault_count++;
 }
Signature · 1 line
2.39.5
#5 lore raw

Re: [PATCH v2 3/3] selftests/powerpc/mm: Use PKEY_UNRESTRICTED macro

Quoted 5 lines · Yury Khrustalev

Replace literal 0 with macro PKEY_UNRESTRICTED where pkey_*() functions are used in mm selftests for memory protection keys for ppc target.

Note that I couldn't build these tests so I would appreciate if someone could check this patch. Thank you!

This shouldn't be in the commit message presumably (you could add it below ---). More importantly the ppc folks need to be cc'ed, get_maintainer.pl is telling me that means at least:

[email protected]
Michael Ellerman <[email protected]>

Quoted 25 lines
---
 tools/testing/selftests/powerpc/mm/pkey_exec_prot.c | 2 +-
 tools/testing/selftests/powerpc/mm/pkey_siginfo.c   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/powerpc/mm/pkey_exec_prot.c +1 -1
index 0af4f02669a1..29b91b7456eb 100644
@@ -72,7 +72,7 @@ static void segv_handler(int signum, siginfo_t *sinfo, void *ctx)
 
 		switch (fault_type) {
 		case PKEY_DISABLE_ACCESS:
-			pkey_set_rights(fault_pkey, 0);
+			pkey_set_rights(fault_pkey, PKEY_UNRESTRICTED);
 			break;
 		case PKEY_DISABLE_EXECUTE:
 			/*
tools/testing/selftests/powerpc/mm/pkey_siginfo.c +0 -0
index 2db76e56d4cb..e89a164c686b 100644

tools/testing/selftests/powerpc/mm/pkey_siginfo.c static void segv_handler(int signum, siginfo_t *sinfo, void *ctx) · line 89

Quoted 8 lines
tools/testing/selftests/powerpc/mm/pkey_siginfo.c +1 -1
@@ -83,7 +83,7 @@ static void segv_handler(int signum, siginfo_t *sinfo, void *ctx)
 	    mprotect(pgstart, pgsize, PROT_EXEC))
 		_exit(1);
 	else
-		pkey_set_rights(pkey, 0);
+		pkey_set_rights(pkey, PKEY_UNRESTRICTED);
 
 	fault_count++;
 }

Patch 2 made me realise there are quite a few sys_pkey_alloc(0, 0) calls in the ppc tests, those could be converted as well.

Kevin