Skip to contentSkip to conversation

pkey01: Adding test for PKEY_DISABLE_EXECUTE

PATCH In review Li Wang 2 participants 4 messages Aug 8, 2024 – Aug 9, 2024

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 pkey01: Adding test for PKEY_DISABLE_EXECUTE

The pkey_test function now includes a code snippet to test execute permissions, ensuring proper handling of execution rights in memory protection keys.

---
 include/lapi/pkey.h                      |  4 ++
 testcases/kernel/syscalls/pkeys/pkey01.c | 50 ++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 3 deletions(-)
include/lapi/pkey.h +4 -0
index 398e3be5f..eb9bf7fb4 100644
@@ -17,6 +17,10 @@
 # define PKEY_DISABLE_WRITE  0x2
 #endif
 
+#ifndef PKEY_DISABLE_EXECUTE
+# define PKEY_DISABLE_EXECUTE 0x4
+#endif
+
 #ifndef HAVE_PKEY_MPROTECT
 inline int pkey_mprotect(void *addr, size_t len, int prot, int pkey)
 {
testcases/kernel/syscalls/pkeys/pkey01.c +47 -3

107 lines not on this page · raw message

Signature · 5 lines
2.45.2


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#1 lore raw

Hi Li,

Quoted 3 lines

The pkey_test function now includes a code snippet to test execute permissions, ensuring proper handling of execution rights in memory protection keys.

LGTM, thanks!

Few notes + tiny fixes below (which can be done before merge).

...

testcases/kernel/syscalls/pkeys/pkey01.c static struct tcase { · line 65

Quoted 4 lines
testcases/kernel/syscalls/pkeys/pkey01.c +4 -0
+	pkey = pkey_alloc(0, PKEY_DISABLE_EXECUTE);
+	if (pkey == -1) {
+		if (errno == EINVAL) {
+			tst_res(TINFO, "PKEY_DISABLE_EXECUTE hasn't implement.");

nit: maybe "PKEY_DISABLE_EXECUTE not implemented"

Quoted 6 lines
testcases/kernel/syscalls/pkeys/pkey01.c +6 -0
+			execute_supported = 0;
+		} else {
+			tst_brk(TBROK | TERRNO, "pkey_alloc failed");
+		}
+	}
+	pkey_free(pkey);

...

testcases/kernel/syscalls/pkeys/pkey01.c static char *flag_to_str(int flags) · line 167

Quoted 11 lines

static void pkey_test(struct tcase *tc, struct mmap_param *mpa) {

testcases/kernel/syscalls/pkeys/pkey01.c +5 -0
 	pid_t pid;
 	char *buffer;
 	int pkey, status;
 	int fd = mpa->fd;
+	size_t (*func)();
+	size_t func_size = -1;
+
+	if (!execute_supported && (tc->access_rights == PKEY_DISABLE_EXECUTE))
+		tst_brk(TCONF, "skip PKEY_DISABLE_EXECUTE test");

NOTE due to possible tst_brk() it'd be good to keep PKEY_DISABLE_EXECUTE as a last test. Not sure if it's worth to add a comment (or replace with tst_res() and return).

...

Quoted 9 lines
testcases/kernel/syscalls/pkeys/pkey01.c +5 -0
+	if (mpa->prot == (PROT_READ | PROT_WRITE | PROT_EXEC)) {
+		func_size = function_size((void (*)(void))function_size);
+		memcpy(buffer, (void *)function_size, func_size);
+	}
+
 	pkey = pkey_alloc(tc->flags, tc->access_rights);
 	if (pkey == -1)
 		tst_brk(TBROK | TERRNO, "pkey_alloc failed");

testcases/kernel/syscalls/pkeys/pkey01.c static void pkey_test(struct tcase *tc, struct mmap_param *mpa) · line 207

Quoted 5 lines
testcases/kernel/syscalls/pkeys/pkey01.c +2 -0
@@ -169,6 +203,10 @@ static void pkey_test(struct tcase *tc, struct mmap_param *mpa)
 			tst_res(TFAIL | TERRNO,
 				"Write buffer success, buffer[0] = %d", *buffer);
 		break;
+		case PKEY_DISABLE_EXECUTE:
+			func = (size_t (*)())buffer;;

nit: double ; (please remove the last one).

testcases/kernel/syscalls/pkeys/pkey01.c static void pkey_test(struct tcase *tc, struct mmap_param *mpa) · line 208

Quoted 1 line
testcases/kernel/syscalls/pkeys/pkey01.c +1 -0
+			tst_res(TFAIL | TERRNO, "Excute buffer result = %lu", func(func));

s/%lu/%zi/ (otherwise it introduces warnings on 32bit)
nit: s/Excute/Execute/

Quoted 6 lines
testcases/kernel/syscalls/pkeys/pkey01.c +1 -0
+		break;
 		}
 		exit(0);
 	}
@@ -193,10 +231,16 @@ static void pkey_test(struct tcase *tc, struct mmap_param *mpa)
 		tst_res(TPASS, "Write buffer success, buffer[0] = %d", *buffer);

testcases/kernel/syscalls/pkeys/pkey01.c static void pkey_test(struct tcase *tc, struct mmap_param *mpa) · line 240

Quoted 10 lines
testcases/kernel/syscalls/pkeys/pkey01.c +4 -1
 	break;
 	case PROT_READ | PROT_WRITE:
-	case PROT_READ | PROT_WRITE | PROT_EXEC:
 		*buffer = 'a';
 		tst_res(TPASS, "Read & Write buffer success, buffer[0] = %d", *buffer);
 	break;
+	case PROT_READ | PROT_WRITE | PROT_EXEC:
+		func = (size_t (*)())buffer;;
+		if (func_size == func(func))
+			tst_res(TPASS, "Execute buffer success, result = %lu", func_size);

s/%lu/%zi/

testcases/kernel/syscalls/pkeys/pkey01.c static void pkey_test(struct tcase *tc, struct mmap_param *mpa) · line 242

Quoted 2 lines
testcases/kernel/syscalls/pkeys/pkey01.c +2 -0
+		else
+			tst_res(TFAIL, "Execute buffer with unexpected result: %lu", func(func));

s/%lu/%zi/

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#2 lore raw

Hi Petr,

Quoted 9 lines · Petr Vorel
Quoted 1 line

+ if (!execute_supported && (tc->access_rights ==

PKEY_DISABLE_EXECUTE))

Quoted 1 line

+ tst_brk(TCONF, "skip PKEY_DISABLE_EXECUTE test");

NOTE due to possible tst_brk() it'd be good to keep PKEY_DISABLE_EXECUTE as a
last test. Not sure if it's worth to add a comment (or replace with tst_res()
and return).

Or, we can use 'tst_res + return' which is similar to tst_hugepages missing way.

The rest comments all look good.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#3 lore raw

Pushed with minor changes as you suggested. Thanks!

P.s. The new test passed on my POWER9+kernel-6.10 and Skylake-SP+ kernel-5.14.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp