mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexey Dobriyan <adobriyan@gmail.com>
To: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>
Cc: linux-kernel@vger.kernel.org, "H. Peter Anvin" <hpa@zytor.com>
Subject: [PATCH] selftests/x86/mm: fixup nx_stack test stability with SA_ONSTACK
Date: Fri, 15 Dec 2023 12:45:57 +0300	[thread overview]
Message-ID: <8299b17a-a730-46e5-a258-fac78ff0420b@p183> (raw)

I forgot that using sigaltstack(2) requires opt-in with SA_ONSTACK.

If userspace stack is NX, then the test continues to work and
reports PASS.

If there is kernel bug and some pages of userspace stack are executable,
then test can be derailed because signal stack frame contents will pass
as random instruction with unpredictable consequences.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 tools/testing/selftests/x86/nx_stack.c |   34 ++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

--- a/tools/testing/selftests/x86/nx_stack.c
+++ b/tools/testing/selftests/x86/nx_stack.c
@@ -160,10 +160,25 @@ static void sigtrap(int _, siginfo_t *__, void *uc_)
 
 int main(void)
 {
+	{
+		/*
+		 * We don't know now much stack SIGSEGV handler uses.
+		 * Bump this by 1 page every time someone complains,
+		 * or rewrite it in assembly.
+		 */
+		const size_t len = SIGSTKSZ;
+		void *p = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+		assert(p != MAP_FAILED);
+		stack_t ss = {};
+		ss.ss_sp = p;
+		ss.ss_size = len;
+		int rv = sigaltstack(&ss, NULL);
+		assert(rv == 0);
+	}
 	{
 		struct sigaction act = {};
 		sigemptyset(&act.sa_mask);
-		act.sa_flags = SA_SIGINFO;
+		act.sa_flags = SA_SIGINFO|SA_ONSTACK;
 		act.sa_sigaction = &sigsegv;
 		int rv = sigaction(SIGSEGV, &act, NULL);
 		assert(rv == 0);
@@ -171,7 +186,7 @@ int main(void)
 	{
 		struct sigaction act = {};
 		sigemptyset(&act.sa_mask);
-		act.sa_flags = SA_SIGINFO;
+		act.sa_flags = SA_SIGINFO|SA_ONSTACK;
 		act.sa_sigaction = &sigtrap;
 		int rv = sigaction(SIGTRAP, &act, NULL);
 		assert(rv == 0);
@@ -188,21 +203,6 @@ int main(void)
 		rv = setrlimit(RLIMIT_STACK, &rlim);
 		assert(rv == 0);
 	}
-	{
-		/*
-		 * We don't know now much stack SIGSEGV handler uses.
-		 * Bump this by 1 page every time someone complains,
-		 * or rewrite it in assembly.
-		 */
-		const size_t len = SIGSTKSZ;
-		void *p = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
-		assert(p != MAP_FAILED);
-		stack_t ss = {};
-		ss.ss_sp = p;
-		ss.ss_size = len;
-		int rv = sigaltstack(&ss, NULL);
-		assert(rv == 0);
-	}
 	make_stack1();
 	/*
 	 * Unreachable, but if _this_ INT3 is ever reached, it's a bug somewhere.

                 reply	other threads:[~2023-12-15  9:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=8299b17a-a730-46e5-a258-fac78ff0420b@p183 \
    --to=adobriyan@gmail.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    /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

all inboxes | Powered by JetHome®