All of lore.kernel.org
 help / color / mirror / Atom feed
From: Li Wang <liwang@redhat.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] pkey01: Adding test for PKEY_DISABLE_EXECUTE
Date: Thu,  8 Aug 2024 14:57:32 +0800	[thread overview]
Message-ID: <20240808065732.64328-1-liwang@redhat.com> (raw)

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

Signed-off-by: Li Wang <liwang@redhat.com>
---
 include/lapi/pkey.h                      |  4 ++
 testcases/kernel/syscalls/pkeys/pkey01.c | 50 ++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/include/lapi/pkey.h b/include/lapi/pkey.h
index 398e3be5f..eb9bf7fb4 100644
--- a/include/lapi/pkey.h
+++ b/include/lapi/pkey.h
@@ -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)
 {
diff --git a/testcases/kernel/syscalls/pkeys/pkey01.c b/testcases/kernel/syscalls/pkeys/pkey01.c
index f1ecfec0b..70fdffe48 100644
--- a/testcases/kernel/syscalls/pkeys/pkey01.c
+++ b/testcases/kernel/syscalls/pkeys/pkey01.c
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
- * Copyright (c) 2019 Red Hat, Inc.
+ * Copyright (c) 2019-2024 Red Hat, Inc.
  */
 
 /*\
@@ -41,6 +41,7 @@
 #define PATH_VM_NRHPS "/proc/sys/vm/nr_hugepages"
 
 static int size;
+static int execute_supported = 1;
 
 static struct tcase {
 	unsigned long flags;
@@ -49,14 +50,26 @@ static struct tcase {
 } tcases[] = {
 	{0, PKEY_DISABLE_ACCESS, "PKEY_DISABLE_ACCESS"},
 	{0, PKEY_DISABLE_WRITE, "PKEY_DISABLE_WRITE"},
+	{0, PKEY_DISABLE_EXECUTE, "PKEY_DISABLE_EXECUTE"},
 };
 
 static void setup(void)
 {
-	int i, fd;
+	int i, fd, pkey;
 
 	check_pkey_support();
 
+	pkey = pkey_alloc(0, PKEY_DISABLE_EXECUTE);
+	if (pkey == -1) {
+		if (errno == EINVAL) {
+			tst_res(TINFO, "PKEY_DISABLE_EXECUTE hasn't implement.");
+			execute_supported = 0;
+		} else {
+			tst_brk(TBROK | TERRNO, "pkey_alloc failed");
+		}
+	}
+	pkey_free(pkey);
+
 	if (tst_hugepages == test.hugepages.number)
 		size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 	else
@@ -130,12 +143,28 @@ static char *flag_to_str(int flags)
 	}
 }
 
+static size_t function_size(void (*func)(void))
+{
+	unsigned char *start = (unsigned char *)func;
+	unsigned char *end = start;
+
+	while (*end != 0xC3 && *end != 0xC2)
+		end++;
+
+	return (size_t)(end - start + 1);
+}
+
 static void pkey_test(struct tcase *tc, struct mmap_param *mpa)
 {
 	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");
 
 	if (!tst_hugepages && (mpa->flags & MAP_HUGETLB)) {
 		tst_res(TINFO, "Skip test on (%s) buffer", flag_to_str(mpa->flags));
@@ -147,6 +176,11 @@ static void pkey_test(struct tcase *tc, struct mmap_param *mpa)
 
 	buffer = SAFE_MMAP(NULL, size, mpa->prot, mpa->flags, fd, 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");
@@ -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;;
+			tst_res(TFAIL | TERRNO, "Excute buffer result = %lu", func(func));
+		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);
 	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);
+		else
+			tst_res(TFAIL, "Execute buffer with unexpected result: %lu", func(func));
+	break;
 	}
 
 	if (fd >= 0)
-- 
2.45.2


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

             reply	other threads:[~2024-08-08  6:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-08  6:57 Li Wang [this message]
2024-08-08  7:44 ` [LTP] [PATCH] pkey01: Adding test for PKEY_DISABLE_EXECUTE Petr Vorel
2024-08-08 10:20   ` Li Wang
2024-08-09  0:59     ` Li Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240808065732.64328-1-liwang@redhat.com \
    --to=liwang@redhat.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.