Skip to contentSkip to conversation

selftests: powerpc: Add test for execute-disabled pkeys

PATCH Superseded Sandipan Das 2 participants 4 messages May 8, 2020 – May 27, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: powerpc: Add test for execute-disabled pkeys

Apart from read and write access, memory protection keys can also be used for restricting execute permission of pages on powerpc. This adds a test to verify if the feature works as expected.

---
 tools/testing/selftests/powerpc/mm/Makefile   |   3 +-
 .../selftests/powerpc/mm/pkey_exec_prot.c     | 326 ++++++++++++++++++
 2 files changed, 328 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/powerpc/mm/pkey_exec_prot.c
tools/testing/selftests/powerpc/mm/Makefile +2 -1
index b9103c4bb414..2816229f648b 100644
@@ -3,7 +3,7 @@ noarg:
 	$(MAKE) -C ../
 
 TEST_GEN_PROGS := hugetlb_vs_thp_test subpage_prot prot_sao segv_errors wild_bctr \
-		  large_vm_fork_separation bad_accesses
+		  large_vm_fork_separation bad_accesses pkey_exec_prot
 TEST_GEN_PROGS_EXTENDED := tlbie_test
 TEST_GEN_FILES := tempfile
 
@@ -17,6 +17,7 @@ $(OUTPUT)/prot_sao: ../utils.c
 $(OUTPUT)/wild_bctr: CFLAGS += -m64
 $(OUTPUT)/large_vm_fork_separation: CFLAGS += -m64
 $(OUTPUT)/bad_accesses: CFLAGS += -m64
+$(OUTPUT)/pkey_exec_prot: CFLAGS += -m64
 
 $(OUTPUT)/tempfile:
 	dd if=/dev/zero of=$@ bs=64k count=1
tools/testing/selftests/powerpc/mm/pkey_exec_prot.c new file +326 -0

326 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw

Hi Sandipan,

Quoted 20 lines · Sandipan Das
tools/testing/selftests/powerpc/mm/pkey_exec_prot.c new file +14 -0
new file mode 100644
index 000000000000..b346ad205e68
@@ -0,0 +1,326 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+/*
+ * Copyright 2020, Sandipan Das, IBM Corp.
+ *
+ * Test if applying execute protection on pages using memory
+ * protection keys works as expected.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>

tools/testing/selftests/powerpc/mm/pkey_exec_prot.c line 24

Quoted 10 lines
tools/testing/selftests/powerpc/mm/pkey_exec_prot.c new file +10 -0
+
+#include <time.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
+#include "utils.h"
+
+/* Override definitions as they might be inconsistent */
+#undef PKEY_DISABLE_ACCESS
+#define PKEY_DISABLE_ACCESS	0x3

Why would they be inconsistent?

Quoted 4 lines
tools/testing/selftests/powerpc/mm/pkey_exec_prot.c new file +4 -0
+/* Older distros might not define this */
+#ifndef SEGV_PKUERR
+#define SEGV_PKUERR	4
+#endif

...

tools/testing/selftests/powerpc/mm/pkey_exec_prot.c

Quoted 12 lines
tools/testing/selftests/powerpc/mm/pkey_exec_prot.c new file +12 -0
+
+	/* Restore permissions in order to continue */
+	switch (fcode) {
+	case SEGV_ACCERR:
+		if (mprotect(insns, pgsize, PROT_READ | PROT_WRITE)) {
+			perror("mprotect");
+			goto fail;
+		}
+		break;
+	case SEGV_PKUERR:
+		if (sinfo->si_pkey != fpkey)
+			goto fail;

This doesn't compile on older distros, eg Ubuntu 16.04:

pkey_exec_prot.c: In function 'segv_handler':
pkey_exec_prot.c:121:12: error: 'siginfo_t {aka struct <anonymous>}' has no member named 'si_pkey'

     if (sinfo->si_pkey != fpkey)
              ^
  pkey_exec_prot.c:151:24: error: 'siginfo_t {aka struct <anonymous>}' has no member named 'si_pkey'
     pkey_set_rights(sinfo->si_pkey, 0);
                          ^

../../lib.mk:142: recipe for target '/output/kselftest/powerpc/mm/pkey_exec_prot' failed

I think a reasonable solution is to use the absence of SEGV_PKUERR to basically turn the whole test into a nop at build time, eg:

tools/testing/selftests/powerpc/mm/pkey_exec_prot.c +8 -3
index b346ad205e68..218257b89fbb 100644
@@ -30,9 +30,7 @@
 #define PKEY_DISABLE_EXECUTE   0x4
 
 /* Older distros might not define this */
-#ifndef SEGV_PKUERR
-#define SEGV_PKUERR    4
-#endif
+#ifdef SEGV_PKUERR
 
 #define SYS_pkey_mprotect      386
 #define SYS_pkey_alloc         384
@@ -319,6 +317,13 @@ static int test(void)
 
        return 0;
 }
+#else
+static int test(void)
+{
+       printf("Test built with old libc lacking pkey support.\n");
+       SKIP_IF(true);
+}
+#endif /* SEGV_PKUERR */
 
 int main(void)
 {

cheers

#2 lore raw

Hi Michael,

Quoted 8 lines · Michael Ellerman

[...]

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+/* Override definitions as they might be inconsistent */
+#undef PKEY_DISABLE_ACCESS
+#define PKEY_DISABLE_ACCESS	0x3

Why would they be inconsistent?

The definition in sys/mman.h still uses the value specific to Intel's implementation i.e. 1, when this should have been 3 for powerpc. I have seen this on Ubuntu 18.04 and 20.04.

Quoted 21 lines
Quoted 5 lines
(quoted diff) +4 -0
+/* Older distros might not define this */
+#ifndef SEGV_PKUERR
+#define SEGV_PKUERR	4
+#endif

...

Review

Quoted 12 lines
(quoted diff) +12 -0
+
+	/* Restore permissions in order to continue */
+	switch (fcode) {
+	case SEGV_ACCERR:
+		if (mprotect(insns, pgsize, PROT_READ | PROT_WRITE)) {
+			perror("mprotect");
+			goto fail;
+		}
+		break;
+	case SEGV_PKUERR:
+		if (sinfo->si_pkey != fpkey)
+			goto fail;

This doesn't compile on older distros, eg Ubuntu 16.04:

Quoted 9 lines

pkey_exec_prot.c: In function 'segv_handler':
pkey_exec_prot.c:121:12: error: 'siginfo_t {aka struct <anonymous>}' has no member named 'si_pkey'

     if (sinfo->si_pkey != fpkey)
              ^
  pkey_exec_prot.c:151:24: error: 'siginfo_t {aka struct <anonymous>}' has no member named 'si_pkey'
     pkey_set_rights(sinfo->si_pkey, 0);
                          ^

../../lib.mk:142: recipe for target '/output/kselftest/powerpc/mm/pkey_exec_prot' failed

Thanks for reporting this.

Quoted 25 lines

I think a reasonable solution is to use the absence of SEGV_PKUERR to basically turn the whole test into a nop at build time, eg:

tools/testing/selftests/powerpc/mm/pkey_exec_prot.c +1 -3
index b346ad205e68..218257b89fbb 100644
@@ -30,9 +30,7 @@
 #define PKEY_DISABLE_EXECUTE   0x4
 
 /* Older distros might not define this */
-#ifndef SEGV_PKUERR
-#define SEGV_PKUERR    4
-#endif
+#ifdef SEGV_PKUERR
 
 #define SYS_pkey_mprotect      386
 #define SYS_pkey_alloc         384
@@ -319,6 +317,13 @@ static int test(void)
 
        return 0;
 }

tools/testing/selftests/powerpc/mm/pkey_exec_prot.c static int test(void) · line 329

Quoted 10 lines
tools/testing/selftests/powerpc/mm/pkey_exec_prot.c +7 -0
+#else
+static int test(void)
+{
+       printf("Test built with old libc lacking pkey support.\n");
+       SKIP_IF(true);
+}
+#endif /* SEGV_PKUERR */
 
 int main(void)
 {

Or can I use this from the pkey tests under selftests/vm?

static inline u32 *siginfo_get_pkey_ptr(siginfo_t *si) {

#ifdef si_pkey
	return &si->si_pkey;
#else
	return (u32 *)(((u8 *)si) + si_pkey_offset);

#endif
}

Where si_pkey_offset is 0x20 for powerpc.

- Sandipan

#3 lore raw
Quoted 12 lines · Sandipan Das

Hi Michael,

Quoted 8 lines · Michael Ellerman

[...]

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+/* Override definitions as they might be inconsistent */
+#undef PKEY_DISABLE_ACCESS
+#define PKEY_DISABLE_ACCESS	0x3

Why would they be inconsistent?

Quoted 3 lines

The definition in sys/mman.h still uses the value specific to Intel's implementation i.e. 1, when this should have been 3 for powerpc. I have seen this on Ubuntu 18.04 and 20.04.

Hmm OK, that's a bug but oh well nothing we can do about it.

Quoted 2 lines
Quoted 2 lines

I think a reasonable solution is to use the absence of SEGV_PKUERR to basically turn the whole test into a nop at build time, eg:

...

Quoted 5 lines

Or can I use this from the pkey tests under selftests/vm?

static inline u32 *siginfo_get_pkey_ptr(siginfo_t *si) {

Quoted 8 lines
#ifdef si_pkey
	return &si->si_pkey;
#else
	return (u32 *)(((u8 *)si) + si_pkey_offset);

#endif
}

Where si_pkey_offset is 0x20 for powerpc.

Yeah that's fine if it works. Please send a v2 with that change.

cheers