Skip to contentSkip to conversation

Revert "pkey01: Adding test for PKEY_DISABLE_EXECUTE"

PATCH In review Martin Doucha 4 participants 12 messages Nov 15, 2024 – Nov 26, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Revert "pkey01: Adding test for PKEY_DISABLE_EXECUTE"

This reverts commit d2b8a476c29d52c484b387454082bbc906b0b4f8.

Remove the PKEY_DISABLE_EXECUTE subtest. The function_size() code is broken in a way that I cannot easily fix. The function tries to calculate the size of a function by finding the first RET instruction. However, in 32bit LTP builds, the code gets compiled to this:

0804b690 <function_size>:

 804b690:       8b 4c 24 04             mov    0x4(%esp),%ecx
 804b694:       0f b6 01                movzbl (%ecx),%eax
 804b697:       83 c0 3e                add    $0x3e,%eax
 804b69a:       3c 01                   cmp    $0x1,%al
 804b69c:       76 1a                   jbe    804b6b8 <function_size+0x28>
 804b69e:       89 c8                   mov    %ecx,%eax
 804b6a0:       83 c0 01                add    $0x1,%eax
 804b6a3:       0f b6 10                movzbl (%eax),%edx
 804b6a6:       83 c2 3e                add    $0x3e,%edx
 804b6a9:       80 fa 01                cmp    $0x1,%dl
 804b6ac:       77 f2                   ja     804b6a0 <function_size+0x10>
 804b6ae:       29 c8                   sub    %ecx,%eax
 804b6b0:       83 c0 10                add    $0x10,%eax
 804b6b3:       c3                      ret
 804b6b4:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
 804b6b8:       b8 10 00 00 00          mov    $0x10,%eax
 804b6bd:       c3                      ret
 804b6be:       66 90                   xchg   %ax,%ax

If you look closely enough, you'll notice a C2 byte in add $0x3e,%edx instruction on address 804b6a6. The function will assume this byte is a RET instruction, return a size that's 22 bytes too short and then the code execution inside the executable buffer will run past the end of buffer, resulting in a segfault.

---
 testcases/kernel/syscalls/pkeys/pkey01.c | 52 ++----------------------
 1 file changed, 3 insertions(+), 49 deletions(-)
testcases/kernel/syscalls/pkeys/pkey01.c +3 -49
index c041cbcfd..e49e48846 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
- * Copyright (c) 2019-2024 Red Hat, Inc.
+ * Copyright (c) 2019 Red Hat, Inc.
  */
 
 /*\
@@ -41,7 +41,6 @@
 #define PATH_VM_NRHPS "/proc/sys/vm/nr_hugepages"
 
 static int size;
-static int execute_supported = 1;
 
 #define PERM_NAME(x) .access_rights = x, .name = #x
 static struct tcase {
@@ -51,26 +50,14 @@ static struct tcase {
 } tcases[] = {
 	{PERM_NAME(PKEY_DISABLE_ACCESS)},
 	{PERM_NAME(PKEY_DISABLE_WRITE)},
-	{PERM_NAME(PKEY_DISABLE_EXECUTE)} /* keep it the last */
 };
 
 static void setup(void)
 {
-	int i, fd, pkey;
+	int i, fd;
 
 	check_pkey_support();
 
-	pkey = pkey_alloc(0, PKEY_DISABLE_EXECUTE);
-	if (pkey == -1) {
-		if (errno == EINVAL) {
-			tst_res(TINFO, "PKEY_DISABLE_EXECUTE not implemented");
-			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
@@ -144,17 +131,6 @@ 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);
-}
-
 /*
  * return: 1 if it's safe to quit testing on failure (all following would be
  * TCONF, O otherwise.
@@ -165,13 +141,6 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
 	char *buffer;
 	int pkey, status;
 	int fd = mpa->fd;
-	size_t (*func)();
-	size_t func_size = 0;
-
-	if (!execute_supported && (tc->access_rights == PKEY_DISABLE_EXECUTE)) {
-		tst_res(TCONF, "skip PKEY_DISABLE_EXECUTE test");
-		return 1;
-	}
 
 	if (!tst_hugepages && (mpa->flags & MAP_HUGETLB)) {
 		tst_res(TCONF, "Skip test on (%s) buffer", flag_to_str(mpa->flags));
@@ -183,11 +152,6 @@ static int 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");
@@ -210,10 +174,6 @@ static int 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, "Execute buffer result = %zi", func(func));
-		break;
 		}
 		exit(0);
 	}
@@ -238,16 +198,10 @@ static int 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 = %zi", func_size);
-		else
-			tst_res(TFAIL, "Execute buffer with unexpected result: %zi", func(func));
-	break;
 	}
 
 	if (fd >= 0)
Signature · 5 lines
2.46.0


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

Sorry, I forgot to CC Li in the patch.

Quoted 160 lines · Martin Doucha

This reverts commit d2b8a476c29d52c484b387454082bbc906b0b4f8.

Remove the PKEY_DISABLE_EXECUTE subtest. The function_size() code is broken in a way that I cannot easily fix. The function tries to calculate the size of a function by finding the first RET instruction. However, in 32bit LTP builds, the code gets compiled to this:

0804b690 <function_size>:

  804b690:       8b 4c 24 04             mov    0x4(%esp),%ecx
  804b694:       0f b6 01                movzbl (%ecx),%eax
  804b697:       83 c0 3e                add    $0x3e,%eax
  804b69a:       3c 01                   cmp    $0x1,%al
  804b69c:       76 1a                   jbe    804b6b8 <function_size+0x28>
  804b69e:       89 c8                   mov    %ecx,%eax
  804b6a0:       83 c0 01                add    $0x1,%eax
  804b6a3:       0f b6 10                movzbl (%eax),%edx
  804b6a6:       83 c2 3e                add    $0x3e,%edx
  804b6a9:       80 fa 01                cmp    $0x1,%dl
  804b6ac:       77 f2                   ja     804b6a0 <function_size+0x10>
  804b6ae:       29 c8                   sub    %ecx,%eax
  804b6b0:       83 c0 10                add    $0x10,%eax
  804b6b3:       c3                      ret
  804b6b4:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
  804b6b8:       b8 10 00 00 00          mov    $0x10,%eax
  804b6bd:       c3                      ret
  804b6be:       66 90                   xchg   %ax,%ax

If you look closely enough, you'll notice a C2 byte in add $0x3e,%edx instruction on address 804b6a6. The function will assume this byte is a RET instruction, return a size that's 22 bytes too short and then the code execution inside the executable buffer will run past the end of buffer, resulting in a segfault.

---
  testcases/kernel/syscalls/pkeys/pkey01.c | 52 ++----------------------
  1 file changed, 3 insertions(+), 49 deletions(-)
testcases/kernel/syscalls/pkeys/pkey01.c +3 -49
index c041cbcfd..e49e48846 100644
@@ -1,6 +1,6 @@
  // SPDX-License-Identifier: GPL-2.0-or-later
  /*
- * Copyright (c) 2019-2024 Red Hat, Inc.
+ * Copyright (c) 2019 Red Hat, Inc.
   */
 
  /*\
@@ -41,7 +41,6 @@
  #define PATH_VM_NRHPS "/proc/sys/vm/nr_hugepages"
 
  static int size;
-static int execute_supported = 1;
 
  #define PERM_NAME(x) .access_rights = x, .name = #x
  static struct tcase {
@@ -51,26 +50,14 @@ static struct tcase {
  } tcases[] = {
  	{PERM_NAME(PKEY_DISABLE_ACCESS)},
  	{PERM_NAME(PKEY_DISABLE_WRITE)},
-	{PERM_NAME(PKEY_DISABLE_EXECUTE)} /* keep it the last */
  };
 
  static void setup(void)
  {
-	int i, fd, pkey;
+	int i, fd;
 
  	check_pkey_support();
 
-	pkey = pkey_alloc(0, PKEY_DISABLE_EXECUTE);
-	if (pkey == -1) {
-		if (errno == EINVAL) {
-			tst_res(TINFO, "PKEY_DISABLE_EXECUTE not implemented");
-			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
@@ -144,17 +131,6 @@ 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);
-}
-
  /*
   * return: 1 if it's safe to quit testing on failure (all following would be
   * TCONF, O otherwise.
@@ -165,13 +141,6 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
  	char *buffer;
  	int pkey, status;
  	int fd = mpa->fd;
-	size_t (*func)();
-	size_t func_size = 0;
-
-	if (!execute_supported && (tc->access_rights == PKEY_DISABLE_EXECUTE)) {
-		tst_res(TCONF, "skip PKEY_DISABLE_EXECUTE test");
-		return 1;
-	}
 
  	if (!tst_hugepages && (mpa->flags & MAP_HUGETLB)) {
  		tst_res(TCONF, "Skip test on (%s) buffer", flag_to_str(mpa->flags));
@@ -183,11 +152,6 @@ static int 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");
@@ -210,10 +174,6 @@ static int 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, "Execute buffer result = %zi", func(func));
-		break;
  		}
  		exit(0);
  	}
@@ -238,16 +198,10 @@ static int 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 = %zi", func_size);
-		else
-			tst_res(TFAIL, "Execute buffer with unexpected result: %zi", func(func));
-	break;
  	}
 
  	if (fd >= 0)
Signature · 11 lines
Martin Doucha   [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by
#2 lore raw

Hi Li, Martin,

Quoted 36 lines

Sorry, I forgot to CC Li in the patch.

Quoted 33 lines · Martin Doucha

This reverts commit d2b8a476c29d52c484b387454082bbc906b0b4f8.

Remove the PKEY_DISABLE_EXECUTE subtest. The function_size() code is broken in a way that I cannot easily fix. The function tries to calculate the size of a function by finding the first RET instruction. However, in 32bit LTP builds, the code gets compiled to this:

0804b690 <function_size>:

  804b690:       8b 4c 24 04             mov    0x4(%esp),%ecx
  804b694:       0f b6 01                movzbl (%ecx),%eax
  804b697:       83 c0 3e                add    $0x3e,%eax
  804b69a:       3c 01                   cmp    $0x1,%al
  804b69c:       76 1a                   jbe    804b6b8 <function_size+0x28>
  804b69e:       89 c8                   mov    %ecx,%eax
  804b6a0:       83 c0 01                add    $0x1,%eax
  804b6a3:       0f b6 10                movzbl (%eax),%edx
  804b6a6:       83 c2 3e                add    $0x3e,%edx
  804b6a9:       80 fa 01                cmp    $0x1,%dl
  804b6ac:       77 f2                   ja     804b6a0 <function_size+0x10>
  804b6ae:       29 c8                   sub    %ecx,%eax
  804b6b0:       83 c0 10                add    $0x10,%eax
  804b6b3:       c3                      ret
  804b6b4:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
  804b6b8:       b8 10 00 00 00          mov    $0x10,%eax
  804b6bd:       c3                      ret
  804b6be:       66 90                   xchg   %ax,%ax

If you look closely enough, you'll notice a C2 byte in add $0x3e,%edx instruction on address 804b6a6. The function will assume this byte is a RET instruction, return a size that's 22 bytes too short and then the code execution inside the executable buffer will run past the end of buffer, resulting in a segfault.

Martin, thanks a lot for debugging!

Other option would be to disable this test only for 32bit (keep it for 64bit). Li, any change you could have look into it?

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 40 lines · Petr Vorel

Hi Li, Martin,

Quoted 36 lines

Sorry, I forgot to CC Li in the patch.

Quoted 33 lines · Martin Doucha

This reverts commit d2b8a476c29d52c484b387454082bbc906b0b4f8.

Remove the PKEY_DISABLE_EXECUTE subtest. The function_size() code is broken in a way that I cannot easily fix. The function tries to calculate the size of a function by finding the first RET instruction. However, in 32bit LTP builds, the code gets compiled to this:

0804b690 <function_size>:

  804b690:       8b 4c 24 04             mov    0x4(%esp),%ecx
  804b694:       0f b6 01                movzbl (%ecx),%eax
  804b697:       83 c0 3e                add    $0x3e,%eax
  804b69a:       3c 01                   cmp    $0x1,%al
  804b69c:       76 1a                   jbe    804b6b8 <function_size+0x28>
  804b69e:       89 c8                   mov    %ecx,%eax
  804b6a0:       83 c0 01                add    $0x1,%eax
  804b6a3:       0f b6 10                movzbl (%eax),%edx
  804b6a6:       83 c2 3e                add    $0x3e,%edx
  804b6a9:       80 fa 01                cmp    $0x1,%dl
  804b6ac:       77 f2                   ja     804b6a0 <function_size+0x10>
  804b6ae:       29 c8                   sub    %ecx,%eax
  804b6b0:       83 c0 10                add    $0x10,%eax
  804b6b3:       c3                      ret
  804b6b4:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
  804b6b8:       b8 10 00 00 00          mov    $0x10,%eax
  804b6bd:       c3                      ret
  804b6be:       66 90                   xchg   %ax,%ax

If you look closely enough, you'll notice a C2 byte in add $0x3e,%edx instruction on address 804b6a6. The function will assume this byte is a RET instruction, return a size that's 22 bytes too short and then the code execution inside the executable buffer will run past the end of buffer, resulting in a segfault.

Quoted 6 lines

Martin, thanks a lot for debugging!

Other option would be to disable this test only for 32bit (keep it for 64bit). Li, any change you could have look into it?

I vaguely recall we already dealt with something similar for a different test, which
copied function, then changed protection to EXEC and finally ran it. I'll see if I can find it.

Quoted 7 lines

Kind regards,
Petr

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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
Quoted 46 lines · Jan Stancek
Quoted 44 lines · Petr Vorel

Hi Li, Martin,

Quoted 36 lines

Sorry, I forgot to CC Li in the patch.

Quoted 33 lines · Martin Doucha

This reverts commit d2b8a476c29d52c484b387454082bbc906b0b4f8.

Remove the PKEY_DISABLE_EXECUTE subtest. The function_size() code is broken in a way that I cannot easily fix. The function tries to calculate the size of a function by finding the first RET instruction. However, in 32bit LTP builds, the code gets compiled to this:

0804b690 <function_size>:

  804b690:       8b 4c 24 04             mov    0x4(%esp),%ecx
  804b694:       0f b6 01                movzbl (%ecx),%eax
  804b697:       83 c0 3e                add    $0x3e,%eax
  804b69a:       3c 01                   cmp    $0x1,%al
  804b69c:       76 1a                   jbe    804b6b8 <function_size+0x28>
  804b69e:       89 c8                   mov    %ecx,%eax
  804b6a0:       83 c0 01                add    $0x1,%eax
  804b6a3:       0f b6 10                movzbl (%eax),%edx
  804b6a6:       83 c2 3e                add    $0x3e,%edx
  804b6a9:       80 fa 01                cmp    $0x1,%dl
  804b6ac:       77 f2                   ja     804b6a0 <function_size+0x10>
  804b6ae:       29 c8                   sub    %ecx,%eax
  804b6b0:       83 c0 10                add    $0x10,%eax
  804b6b3:       c3                      ret
  804b6b4:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
  804b6b8:       b8 10 00 00 00          mov    $0x10,%eax
  804b6bd:       c3                      ret
  804b6be:       66 90                   xchg   %ax,%ax

If you look closely enough, you'll notice a C2 byte in add $0x3e,%edx instruction on address 804b6a6. The function will assume this byte is a RET instruction, return a size that's 22 bytes too short and then the code execution inside the executable buffer will run past the end of buffer, resulting in a segfault.

Martin, thanks a lot for debugging!

Quoted 7 lines
Quoted 2 lines

Other option would be to disable this test only for 32bit (keep it for 64bit). Li, any change you could have look into it?

I vaguely recall we already dealt with something similar for a different test, which
copied function, then changed protection to EXEC and finally ran it. I'll see if I can find it.

Do we need function_size() to return accurate number? In mprotect04 we ended up using CFLAGS += -falign-functions=64 and copying everything until end of page. Function was dummy, so it always fit and it's not an issue if we copied more.

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

PATCH 1/1 syscalls/pkeys01: use a dummy function instead of function_size

As Martin found:
The function_size() code
is broken in a way that I cannot easily fix. The function tries to calculate the size of a function by finding the first RET instruction. However, in 32bit LTP builds, the code gets compiled to this:

0804b690 <function_size>:

   804b690:       8b 4c 24 04             mov    0x4(%esp),%ecx
   804b694:       0f b6 01                movzbl (%ecx),%eax
   804b697:       83 c0 3e                add    $0x3e,%eax
   804b69a:       3c 01                   cmp    $0x1,%al
   804b69c:       76 1a                   jbe    804b6b8 <function_size+0x28>
   804b69e:       89 c8                   mov    %ecx,%eax
   804b6a0:       83 c0 01                add    $0x1,%eax
   804b6a3:       0f b6 10                movzbl (%eax),%edx
   804b6a6:       83 c2 3e                add    $0x3e,%edx
   804b6a9:       80 fa 01                cmp    $0x1,%dl
   804b6ac:       77 f2                   ja     804b6a0 <function_size+0x10>
   804b6ae:       29 c8                   sub    %ecx,%eax
   804b6b0:       83 c0 10                add    $0x10,%eax
   804b6b3:       c3                      ret
   804b6b4:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
   804b6b8:       b8 10 00 00 00          mov    $0x10,%eax
   804b6bd:       c3                      ret
   804b6be:       66 90                   xchg   %ax,%ax

If you look closely enough, you'll notice a C2 byte in add $0x3e,%edx instruction on address 804b6a6. The function will assume this byte is a RET instruction, return a size that's 22 bytes too short and then the code execution inside the executable buffer will run past the end of buffer, resulting in a segfault.

Use a dummy function and copy entire page, similar to what we do in mprotect04.

---
 testcases/kernel/syscalls/pkeys/Makefile |  2 ++
 testcases/kernel/syscalls/pkeys/pkey01.c | 31 ++++++++++--------------
 2 files changed, 15 insertions(+), 18 deletions(-)

Notes:

This could be an alternative to reverting PKEY_DISABLE_EXECUTE test. I haven't tested it yet on 32bit, but since it doesn't rely on any instruction codes I don't expect it to break. An important test would also be ppc64le.

testcases/kernel/syscalls/pkeys/Makefile +2 -0
index 9ee2c2ea57b0..814593f3c720 100644
@@ -5,4 +5,6 @@ top_srcdir		?= ../../../..
 
 include $(top_srcdir)/include/mk/testcases.mk
 
+pkey01: CFLAGS += -falign-functions=64
+
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/pkeys/pkey01.c +13 -18
index c041cbcfd969..b10760b5bd2f 100644
@@ -144,15 +144,9 @@ static char *flag_to_str(int flags)
 	}
 }
 
-static size_t function_size(void (*func)(void))
+static long __attribute__ ((noinline)) dummy_func(void)
 {
-	unsigned char *start = (unsigned char *)func;
-	unsigned char *end = start;
-
-	while (*end != 0xC3 && *end != 0xC2)
-		end++;
-
-	return (size_t)(end - start + 1);
+	return 0xdead;
 }
 
 /*
@@ -165,8 +159,11 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
 	char *buffer;
 	int pkey, status;
 	int fd = mpa->fd;
-	size_t (*func)();
-	size_t func_size = 0;
+	long (*func)(void) = 0;
+	uintptr_t page_mask = ~(getpagesize() - 1);
+	uintptr_t offset_mask = (getpagesize() - 1);
+	uintptr_t func_page_offset = (uintptr_t)&dummy_func & offset_mask;
+	void *page_to_copy = (void *)((uintptr_t)&dummy_func & page_mask);
 
 	if (!execute_supported && (tc->access_rights == PKEY_DISABLE_EXECUTE)) {
 		tst_res(TCONF, "skip PKEY_DISABLE_EXECUTE test");
@@ -184,8 +181,8 @@ static int 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);
+		memcpy(buffer, page_to_copy, getpagesize());
+		func = (long (*)(void))(buffer + func_page_offset);
 	}
 
 	pkey = pkey_alloc(tc->flags, tc->access_rights);
@@ -211,8 +208,7 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
 				"Write buffer success, buffer[0] = %d", *buffer);
 		break;
 		case PKEY_DISABLE_EXECUTE:
-			func = (size_t (*)())buffer;
-			tst_res(TFAIL | TERRNO, "Execute buffer result = %zi", func(func));
+			tst_res(TFAIL | TERRNO, "Execute buffer result = %ld", func());
 		break;
 		}
 		exit(0);
@@ -242,11 +238,10 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
 		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 = %zi", func_size);
+		if (dummy_func() == func())
+			tst_res(TPASS, "Execute buffer success, result = %ld", dummy_func());
 		else
-			tst_res(TFAIL, "Execute buffer with unexpected result: %zi", func(func));
+			tst_res(TFAIL, "Execute buffer with unexpected result: %zi", func());
 	break;
 	}
 
Signature · 5 lines
2.43.0


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

Re: [LTP] [PATCH] syscalls/pkeys01: use a dummy function instead of function_size

Quoted 43 lines · Jan Stancek

As Martin found:
The function_size() code
is broken in a way that I cannot easily fix. The function tries to calculate the size of a function by finding the first RET instruction. However, in 32bit LTP builds, the code gets compiled to this:

0804b690 <function_size>:

   804b690:       8b 4c 24 04             mov    0x4(%esp),%ecx
   804b694:       0f b6 01                movzbl (%ecx),%eax
   804b697:       83 c0 3e                add    $0x3e,%eax
   804b69a:       3c 01                   cmp    $0x1,%al
   804b69c:       76 1a                   jbe    804b6b8 <function_size+0x28>
   804b69e:       89 c8                   mov    %ecx,%eax
   804b6a0:       83 c0 01                add    $0x1,%eax
   804b6a3:       0f b6 10                movzbl (%eax),%edx
   804b6a6:       83 c2 3e                add    $0x3e,%edx
   804b6a9:       80 fa 01                cmp    $0x1,%dl
   804b6ac:       77 f2                   ja     804b6a0 <function_size+0x10>
   804b6ae:       29 c8                   sub    %ecx,%eax
   804b6b0:       83 c0 10                add    $0x10,%eax
   804b6b3:       c3                      ret
   804b6b4:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
   804b6b8:       b8 10 00 00 00          mov    $0x10,%eax
   804b6bd:       c3                      ret
   804b6be:       66 90                   xchg   %ax,%ax

If you look closely enough, you'll notice a C2 byte in add $0x3e,%edx instruction on address 804b6a6. The function will assume this byte is a RET instruction, return a size that's 22 bytes too short and then the code execution inside the executable buffer will run past the end of buffer, resulting in a segfault.

Use a dummy function and copy entire page, similar to what we do in mprotect04.

---
 testcases/kernel/syscalls/pkeys/Makefile |  2 ++
 testcases/kernel/syscalls/pkeys/pkey01.c | 31 ++++++++++--------------
 2 files changed, 15 insertions(+), 18 deletions(-)
Quoted 6 lines

Notes:

This could be an alternative to reverting PKEY_DISABLE_EXECUTE test. I haven't tested it yet on 32bit, but since it doesn't rely on any instruction codes I don't expect it to break. An important test would also be ppc64le.

I tested 32bit and ppc64le, both worked fine, so I propose we give this a consideration as alternative to Martin's revert.

Quoted 91 lines
testcases/kernel/syscalls/pkeys/Makefile +2 -0
index 9ee2c2ea57b0..814593f3c720 100644
@@ -5,4 +5,6 @@ top_srcdir              ?= ../../../..
 
 include $(top_srcdir)/include/mk/testcases.mk
 
+pkey01: CFLAGS += -falign-functions=64
+
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/pkeys/pkey01.c +13 -19
index c041cbcfd969..b10760b5bd2f 100644
@@ -144,15 +144,9 @@ static char *flag_to_str(int flags)
        }
 }
 
-static size_t function_size(void (*func)(void))
+static long __attribute__ ((noinline)) dummy_func(void)
 {
-       unsigned char *start = (unsigned char *)func;
-       unsigned char *end = start;
-
-       while (*end != 0xC3 && *end != 0xC2)
-               end++;
-
-       return (size_t)(end - start + 1);
+       return 0xdead;
 }
 
 /*
@@ -165,8 +159,11 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
        char *buffer;
        int pkey, status;
        int fd = mpa->fd;
-       size_t (*func)();
-       size_t func_size = 0;
+       long (*func)(void) = 0;
+       uintptr_t page_mask = ~(getpagesize() - 1);
+       uintptr_t offset_mask = (getpagesize() - 1);
+       uintptr_t func_page_offset = (uintptr_t)&dummy_func & offset_mask;
+       void *page_to_copy = (void *)((uintptr_t)&dummy_func & page_mask);
 
        if (!execute_supported && (tc->access_rights == PKEY_DISABLE_EXECUTE)) {
                tst_res(TCONF, "skip PKEY_DISABLE_EXECUTE test");
@@ -184,8 +181,8 @@ static int 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);
+               memcpy(buffer, page_to_copy, getpagesize());
+               func = (long (*)(void))(buffer + func_page_offset);
        }
 
        pkey = pkey_alloc(tc->flags, tc->access_rights);
@@ -211,8 +208,7 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
                                "Write buffer success, buffer[0] = %d", *buffer);
                break;
                case PKEY_DISABLE_EXECUTE:
-                       func = (size_t (*)())buffer;
-                       tst_res(TFAIL | TERRNO, "Execute buffer result = %zi", func(func));
+                       tst_res(TFAIL | TERRNO, "Execute buffer result = %ld", func());
                break;
                }
                exit(0);
@@ -242,11 +238,10 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
                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 = %zi", func_size);
+               if (dummy_func() == func())
+                       tst_res(TPASS, "Execute buffer success, result = %ld", dummy_func());
                else
-                       tst_res(TFAIL, "Execute buffer with unexpected result: %zi", func(func));
+                       tst_res(TFAIL, "Execute buffer with unexpected result: %zi", func());
        break;
        }
 
--

2.43.0

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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by
#7 lore raw

Re: [LTP] [PATCH] syscalls/pkeys01: use a dummy function instead of function_size

Hi Jan,

...

Quoted 4 lines
Quoted 4 lines

This could be an alternative to reverting PKEY_DISABLE_EXECUTE test. I haven't tested it yet on 32bit, but since it doesn't rely on any instruction codes I don't expect it to break. An important test would also be ppc64le.

LGTM.

NOTE: we have hackweek this week, I guess you get Martin's feedback next week.

Quoted 2 lines

I tested 32bit and ppc64le, both worked fine, so I propose we give this a consideration as alternative to Martin's revert.

Thanks!

Kind regards,
Petr

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

Re: [LTP] [PATCH] syscalls/pkeys01: use a dummy function instead of function_size

In reply to Jan Stancek
Quoted 35 lines · Jan Stancek

As Martin found:
The function_size() code
is broken in a way that I cannot easily fix. The function tries to calculate the size of a function by finding the first RET instruction. However, in 32bit LTP builds, the code gets compiled to this:

0804b690 <function_size>:

   804b690:       8b 4c 24 04             mov    0x4(%esp),%ecx
   804b694:       0f b6 01                movzbl (%ecx),%eax
   804b697:       83 c0 3e                add    $0x3e,%eax
   804b69a:       3c 01                   cmp    $0x1,%al
   804b69c:       76 1a                   jbe    804b6b8
<function_size+0x28>
   804b69e:       89 c8                   mov    %ecx,%eax
   804b6a0:       83 c0 01                add    $0x1,%eax
   804b6a3:       0f b6 10                movzbl (%eax),%edx
   804b6a6:       83 c2 3e                add    $0x3e,%edx
   804b6a9:       80 fa 01                cmp    $0x1,%dl
   804b6ac:       77 f2                   ja     804b6a0
<function_size+0x10>
   804b6ae:       29 c8                   sub    %ecx,%eax
   804b6b0:       83 c0 10                add    $0x10,%eax
   804b6b3:       c3                      ret
   804b6b4:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
   804b6b8:       b8 10 00 00 00          mov    $0x10,%eax
   804b6bd:       c3                      ret
   804b6be:       66 90                   xchg   %ax,%ax

If you look closely enough, you'll notice a C2 byte in add $0x3e,%edx instruction on address 804b6a6. The function will assume this byte is a RET instruction, return a size that's 22 bytes too short and then the code execution inside the executable buffer will run past the end of buffer, resulting in a segfault.

Quoted 5 lines

Use a dummy function and copy entire page, similar to what we do in mprotect04.

This way is better than reverting, thanks!

Signature · 5 lines
Regards,
Li Wang

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

Re: [LTP] [PATCH] syscalls/pkeys01: use a dummy function instead of function_size

In reply to Jan Stancek

Hi,
I've successfully tested your patch on both x86 (both 32 and 64bit) and PPC64. The extra alignment should ensure that the dummy function doesn't get split between two pages so this is a good fix. There's just one minor printf formatting issue at the end that can be fixed during merge.

Quoted 119 lines · Jan Stancek

As Martin found:
The function_size() code
is broken in a way that I cannot easily fix. The function tries to calculate the size of a function by finding the first RET instruction. However, in 32bit LTP builds, the code gets compiled to this:

   0804b690 <function_size>:
    804b690:       8b 4c 24 04             mov    0x4(%esp),%ecx
    804b694:       0f b6 01                movzbl (%ecx),%eax
    804b697:       83 c0 3e                add    $0x3e,%eax
    804b69a:       3c 01                   cmp    $0x1,%al
    804b69c:       76 1a                   jbe    804b6b8 <function_size+0x28>
    804b69e:       89 c8                   mov    %ecx,%eax
    804b6a0:       83 c0 01                add    $0x1,%eax
    804b6a3:       0f b6 10                movzbl (%eax),%edx
    804b6a6:       83 c2 3e                add    $0x3e,%edx
    804b6a9:       80 fa 01                cmp    $0x1,%dl
    804b6ac:       77 f2                   ja     804b6a0 <function_size+0x10>
    804b6ae:       29 c8                   sub    %ecx,%eax
    804b6b0:       83 c0 10                add    $0x10,%eax
    804b6b3:       c3                      ret
    804b6b4:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
    804b6b8:       b8 10 00 00 00          mov    $0x10,%eax
    804b6bd:       c3                      ret
    804b6be:       66 90                   xchg   %ax,%ax

   If you look closely enough, you'll notice a C2 byte in add $0x3e,%edx
   instruction on address 804b6a6. The function will assume this byte is
   a RET instruction, return a size that's 22 bytes too short and then
   the code execution inside the executable buffer will run past the end
   of buffer, resulting in a segfault.

Use a dummy function and copy entire page, similar to what we do in mprotect04.

---
  testcases/kernel/syscalls/pkeys/Makefile |  2 ++
  testcases/kernel/syscalls/pkeys/pkey01.c | 31 ++++++++++--------------
  2 files changed, 15 insertions(+), 18 deletions(-)

Notes:

This could be an alternative to reverting PKEY_DISABLE_EXECUTE test. I haven't tested it yet on 32bit, but since it doesn't rely on any instruction codes I don't expect it to break. An important test would also be ppc64le.

testcases/kernel/syscalls/pkeys/Makefile +2 -0
index 9ee2c2ea57b0..814593f3c720 100644
@@ -5,4 +5,6 @@ top_srcdir		?= ../../../..
 
  include $(top_srcdir)/include/mk/testcases.mk
 
+pkey01: CFLAGS += -falign-functions=64
+
  include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/pkeys/pkey01.c +10 -14
index c041cbcfd969..b10760b5bd2f 100644
@@ -144,15 +144,9 @@ static char *flag_to_str(int flags)
  	}
  }
 
-static size_t function_size(void (*func)(void))
+static long __attribute__ ((noinline)) dummy_func(void)
  {
-	unsigned char *start = (unsigned char *)func;
-	unsigned char *end = start;
-
-	while (*end != 0xC3 && *end != 0xC2)
-		end++;
-
-	return (size_t)(end - start + 1);
+	return 0xdead;
  }
 
  /*
@@ -165,8 +159,11 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
  	char *buffer;
  	int pkey, status;
  	int fd = mpa->fd;
-	size_t (*func)();
-	size_t func_size = 0;
+	long (*func)(void) = 0;
+	uintptr_t page_mask = ~(getpagesize() - 1);
+	uintptr_t offset_mask = (getpagesize() - 1);
+	uintptr_t func_page_offset = (uintptr_t)&dummy_func & offset_mask;
+	void *page_to_copy = (void *)((uintptr_t)&dummy_func & page_mask);
 
  	if (!execute_supported && (tc->access_rights == PKEY_DISABLE_EXECUTE)) {
  		tst_res(TCONF, "skip PKEY_DISABLE_EXECUTE test");
@@ -184,8 +181,8 @@ static int 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);
+		memcpy(buffer, page_to_copy, getpagesize());
+		func = (long (*)(void))(buffer + func_page_offset);
  	}
 
  	pkey = pkey_alloc(tc->flags, tc->access_rights);
@@ -211,8 +208,7 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
  				"Write buffer success, buffer[0] = %d", *buffer);
  		break;
  		case PKEY_DISABLE_EXECUTE:
-			func = (size_t (*)())buffer;
-			tst_res(TFAIL | TERRNO, "Execute buffer result = %zi", func(func));
+			tst_res(TFAIL | TERRNO, "Execute buffer result = %ld", func());
  		break;
  		}
  		exit(0);
@@ -242,11 +238,10 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
  		tst_res(TPASS, "Read & Write buffer success, buffer[0] = %d", *buffer);

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

Quoted 10 lines
testcases/kernel/syscalls/pkeys/pkey01.c +3 -4
  	break;
  	case PROT_READ | PROT_WRITE | PROT_EXEC:
-		func = (size_t (*)())buffer;;
-		if (func_size == func(func))
-			tst_res(TPASS, "Execute buffer success, result = %zi", func_size);
+		if (dummy_func() == func())
+			tst_res(TPASS, "Execute buffer success, result = %ld", dummy_func());
  		else
-			tst_res(TFAIL, "Execute buffer with unexpected result: %zi", func(func));
+			tst_res(TFAIL, "Execute buffer with unexpected result: %zi", func());

The format here should be %ld.

Quoted 3 lines
testcases/kernel/syscalls/pkeys/pkey01.c +0 -0
  	break;
  	}
 
Signature · 11 lines
Martin Doucha   [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


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

Re: [LTP] [PATCH] syscalls/pkeys01: use a dummy function instead of function_size

Quoted 133 lines · Martin Doucha

Hi,
I've successfully tested your patch on both x86 (both 32 and 64bit) and PPC64. The extra alignment should ensure that the dummy function doesn't get split between two pages so this is a good fix. There's just one minor printf formatting issue at the end that can be fixed during merge.

Quoted 123 lines · Jan Stancek

As Martin found:
The function_size() code
is broken in a way that I cannot easily fix. The function tries to calculate the size of a function by finding the first RET instruction. However, in 32bit LTP builds, the code gets compiled to this:

   0804b690 <function_size>:
    804b690:       8b 4c 24 04             mov    0x4(%esp),%ecx
    804b694:       0f b6 01                movzbl (%ecx),%eax
    804b697:       83 c0 3e                add    $0x3e,%eax
    804b69a:       3c 01                   cmp    $0x1,%al
    804b69c:       76 1a                   jbe    804b6b8 <function_size+0x28>
    804b69e:       89 c8                   mov    %ecx,%eax
    804b6a0:       83 c0 01                add    $0x1,%eax
    804b6a3:       0f b6 10                movzbl (%eax),%edx
    804b6a6:       83 c2 3e                add    $0x3e,%edx
    804b6a9:       80 fa 01                cmp    $0x1,%dl
    804b6ac:       77 f2                   ja     804b6a0 <function_size+0x10>
    804b6ae:       29 c8                   sub    %ecx,%eax
    804b6b0:       83 c0 10                add    $0x10,%eax
    804b6b3:       c3                      ret
    804b6b4:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
    804b6b8:       b8 10 00 00 00          mov    $0x10,%eax
    804b6bd:       c3                      ret
    804b6be:       66 90                   xchg   %ax,%ax

   If you look closely enough, you'll notice a C2 byte in add $0x3e,%edx
   instruction on address 804b6a6. The function will assume this byte is
   a RET instruction, return a size that's 22 bytes too short and then
   the code execution inside the executable buffer will run past the end
   of buffer, resulting in a segfault.

Use a dummy function and copy entire page, similar to what we do in mprotect04.

---
  testcases/kernel/syscalls/pkeys/Makefile |  2 ++
  testcases/kernel/syscalls/pkeys/pkey01.c | 31 ++++++++++--------------
  2 files changed, 15 insertions(+), 18 deletions(-)

Notes:

This could be an alternative to reverting PKEY_DISABLE_EXECUTE test. I haven't tested it yet on 32bit, but since it doesn't rely on any instruction codes I don't expect it to break. An important test would also be ppc64le.

testcases/kernel/syscalls/pkeys/Makefile +2 -0
index 9ee2c2ea57b0..814593f3c720 100644
@@ -5,4 +5,6 @@ top_srcdir            ?= ../../../..
 
  include $(top_srcdir)/include/mk/testcases.mk
 
+pkey01: CFLAGS += -falign-functions=64
+
  include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/pkeys/pkey01.c +10 -16
index c041cbcfd969..b10760b5bd2f 100644
@@ -144,15 +144,9 @@ static char *flag_to_str(int flags)
      }
  }
 
-static size_t function_size(void (*func)(void))
+static long __attribute__ ((noinline)) dummy_func(void)
  {
-     unsigned char *start = (unsigned char *)func;
-     unsigned char *end = start;
-
-     while (*end != 0xC3 && *end != 0xC2)
-             end++;
-
-     return (size_t)(end - start + 1);
+     return 0xdead;
  }
 
  /*
@@ -165,8 +159,11 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
      char *buffer;
      int pkey, status;
      int fd = mpa->fd;
-     size_t (*func)();
-     size_t func_size = 0;
+     long (*func)(void) = 0;
+     uintptr_t page_mask = ~(getpagesize() - 1);
+     uintptr_t offset_mask = (getpagesize() - 1);
+     uintptr_t func_page_offset = (uintptr_t)&dummy_func & offset_mask;
+     void *page_to_copy = (void *)((uintptr_t)&dummy_func & page_mask);
 
      if (!execute_supported && (tc->access_rights == PKEY_DISABLE_EXECUTE)) {
              tst_res(TCONF, "skip PKEY_DISABLE_EXECUTE test");
@@ -184,8 +181,8 @@ static int 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);
+             memcpy(buffer, page_to_copy, getpagesize());
+             func = (long (*)(void))(buffer + func_page_offset);
      }
 
      pkey = pkey_alloc(tc->flags, tc->access_rights);
@@ -211,8 +208,7 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
                              "Write buffer success, buffer[0] = %d", *buffer);
              break;
              case PKEY_DISABLE_EXECUTE:
-                     func = (size_t (*)())buffer;
-                     tst_res(TFAIL | TERRNO, "Execute buffer result = %zi", func(func));
+                     tst_res(TFAIL | TERRNO, "Execute buffer result = %ld", func());
              break;
              }
              exit(0);
@@ -242,11 +238,10 @@ static int pkey_test(struct tcase *tc, struct mmap_param *mpa)
              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))
Quoted 8 lines

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

Quoted 6 lines
testcases/kernel/syscalls/pkeys/pkey01.c +3 -2
-                     tst_res(TPASS, "Execute buffer success, result = %zi", func_size);
+             if (dummy_func() == func())
+                     tst_res(TPASS, "Execute buffer success, result = %ld", dummy_func());
              else
-                     tst_res(TFAIL, "Execute buffer with unexpected result: %zi", func(func));
+                     tst_res(TFAIL, "Execute buffer with unexpected result: %zi", func());

The format here should be %ld.

Pushed with formatting fixed.

Thanks,
Jan

Quoted 14 lines

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

Quoted 4 lines
testcases/kernel/syscalls/pkeys/pkey01.c +0 -0
      break;
      }
 

--
Martin Doucha [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw
In reply to Jan Stancek
Quoted 55 lines · Jan Stancek
Quoted 16 lines · Jan Stancek
Quoted 14 lines · Petr Vorel

Hi Li, Martin,

Quoted 10 lines

Sorry, I forgot to CC Li in the patch.

Quoted 7 lines · Martin Doucha

This reverts commit d2b8a476c29d52c484b387454082bbc906b0b4f8.

Remove the PKEY_DISABLE_EXECUTE subtest. The function_size() code is broken in a way that I cannot easily fix. The function tries to calculate the size of a function by finding the first RET instruction. However, in 32bit LTP builds, the code gets compiled to this:

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

0804b690 <function_size>:

  804b690:       8b 4c 24 04             mov    0x4(%esp),%ecx
  804b694:       0f b6 01                movzbl (%ecx),%eax
  804b697:       83 c0 3e                add    $0x3e,%eax
  804b69a:       3c 01                   cmp    $0x1,%al
  804b69c:       76 1a                   jbe    804b6b8

<function_size+0x28>

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
  804b69e:       89 c8                   mov    %ecx,%eax
  804b6a0:       83 c0 01                add    $0x1,%eax
  804b6a3:       0f b6 10                movzbl (%eax),%edx
  804b6a6:       83 c2 3e                add    $0x3e,%edx
  804b6a9:       80 fa 01                cmp    $0x1,%dl
  804b6ac:       77 f2                   ja     804b6a0

<function_size+0x10>

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
  804b6ae:       29 c8                   sub    %ecx,%eax
  804b6b0:       83 c0 10                add    $0x10,%eax
  804b6b3:       c3                      ret
  804b6b4:       8d 74 26 00             lea

0x0(%esi,%eiz,1),%esi

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
  804b6b8:       b8 10 00 00 00          mov    $0x10,%eax
  804b6bd:       c3                      ret
  804b6be:       66 90                   xchg   %ax,%ax

If you look closely enough, you'll notice a C2 byte in add

$0x3e,%edx

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

instruction on address 804b6a6. The function will assume this byte

is

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

a RET instruction, return a size that's 22 bytes too short and then the code execution inside the executable buffer will run past the

end

Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line

of buffer, resulting in a segfault.

Martin, thanks a lot for debugging!

Other option would be to disable this test only for 32bit (keep it for

64bit).

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

Li, any change you could have look into it?

I vaguely recall we already dealt with something similar for a different test, which
copied function, then changed protection to EXEC and finally ran it. I'll see if I can find it.

Do we need function_size() to return accurate number? In mprotect04

That is a simple method to check that the buffer can be executed correctly.

Quoted 4 lines

we ended up using CFLAGS += -falign-functions=64 and copying everything until end of page. Function was dummy, so it always fit and it's not an issue if we copied more.

Yes, this could work as well, thanks for improving it.

P.s I am on traveling this week, so I wouldn't find a machine to test the patch.

Signature · 5 lines
Regards,
Li Wang

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