public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Daniel Mentz <danielmentz@google.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2] mprotect04: Support execute-only page access permissions
Date: Mon, 11 Feb 2019 16:00:08 -0800	[thread overview]
Message-ID: <20190212000008.57085-1-danielmentz@google.com> (raw)

Linux version 4.9 introduced support for execute-only page access permissions on
arm64. As a result, user space processes, by default, cannot read from
their own .text sections. This change adds an extra call to mprotect()
to explicitly change access protections to allow relevant parts of the
.text section to be read.

Without this change, mprotect04 generates false TBROK results. We
previously saw this test output:

mprotect04    1  TPASS  :  test PROT_NONE for mprotect success
mprotect04    0  TINFO  :  exec_func: 0x5ac82d3588, page_to_copy: 0x5ac82d3000
mprotect04    2  TBROK  :  ltp/testcases/kernel/syscalls/mprotect/mprotect04.c:236: page_to_copy not present
mprotect04    3  TBROK  :  ltp/testcases/kernel/syscalls/mprotect/mprotect04.c:236: Remaining cases broken

Signed-off-by: Daniel Mentz <danielmentz@google.com>
---

v2: Re-based on Jan's commit ("syscalls/mprotect: align exec_func to 64
bytes")

 .../kernel/syscalls/mprotect/mprotect04.c     | 27 ++++++++++++++++---
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/testcases/kernel/syscalls/mprotect/mprotect04.c b/testcases/kernel/syscalls/mprotect/mprotect04.c
index 0f7890dca..192a0184d 100644
--- a/testcases/kernel/syscalls/mprotect/mprotect04.c
+++ b/testcases/kernel/syscalls/mprotect/mprotect04.c
@@ -227,10 +227,25 @@ static void *get_func(void *mem, uintptr_t *func_page_offset)
 	tst_resm(TINFO, "exec_func: %p, page_to_copy: %p",
 		&exec_func, page_to_copy);
 
-	/* copy 1st page, if it's not present something is wrong */
-	if (!page_present(page_to_copy))
-		tst_brkm(TBROK, cleanup, "page_to_copy not present\n");
-
+	/* Copy 1st page. If it's not accessible, we might be running on a
+	 * platform that supports execute-only page access permissions, in which
+	 * case we have to explicitly change access protections to allow the
+	 * memory to be read. */
+	if (!page_present(page_to_copy)) {
+		TEST(mprotect(page_to_copy, page_sz, PROT_READ | PROT_EXEC));
+		if (TEST_RETURN == -1) {
+			tst_resm(TFAIL | TTERRNO,
+				 "mprotect(PROT_READ|PROT_EXEC) failed");
+			return NULL;
+		}
+		/* If the memory is still not accessible, then something must be
+		 * wrong. */
+		if (!page_present(page_to_copy)) {
+			tst_resm(TINFO, "exec_func: %p, page_to_copy: %p\n",
+				&exec_func, page_to_copy);
+			tst_brkm(TBROK, cleanup, "page_to_copy not present\n");
+		}
+	}
 	memcpy(mem, page_to_copy, page_sz);
 
 	clear_cache(mem_start, page_sz);
@@ -260,6 +275,9 @@ static void testfunc_protexec(void)
 	func = get_func(p, &func_page_offset);
 #endif
 
+	if (!func)
+		goto out;
+
 	if (func_page_offset + 64 >= page_sz) {
 		SAFE_MUNMAP(cleanup, p, page_sz);
 		tst_brkm(TCONF, cleanup, "func too close to page boundary, "
@@ -289,6 +307,7 @@ static void testfunc_protexec(void)
 		}
 	}
 
+out:
 	SAFE_MUNMAP(cleanup, p, page_sz);
 }
 
-- 
2.20.1.791.gb4d0f1c61a-goog


             reply	other threads:[~2019-02-12  0:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-12  0:00 Daniel Mentz [this message]
2019-02-19 13:34 ` [LTP] [PATCH v2] mprotect04: Support execute-only page access permissions Li Wang
2019-02-20  0:21   ` Daniel Mentz
2019-02-20  7:59     ` Li Wang
2019-02-21 15:01       ` Will Deacon
2019-02-21 20:43         ` Daniel Mentz
2019-02-22  3:13           ` Li Wang
2019-02-22 11:16             ` Will Deacon
2019-02-25  7:39 ` Jan Stancek

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=20190212000008.57085-1-danielmentz@google.com \
    --to=danielmentz@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox