linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Shakeel Butt <shakeelb@google.com>
To: Thomas Gleixner <tglx@linutronix.de>,
	Greg Thelen <gthelen@google.com>, Ingo Molnar <mingo@redhat.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	John Sperbeck <jsperbeck@google.com>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, x86@kernel.org,
	Shakeel Butt <shakeelb@google.com>
Subject: SIGSEGV on OSPKE machine
Date: Fri, 16 Mar 2018 14:36:04 -0700	[thread overview]
Message-ID: <20180316213604.167305-1-shakeelb@google.com> (raw)

Hi all,

The following simple program is producing SIGSEGV on machines which have
X86_FEATURE_OSPKE feature on 4.15 kernel.

#include <sys/mman.h>

int main(int argc, char *argv[])
{
	void *p = mmap(0, 4096, PROT_EXEC, MAP_ANONYMOUS|MAP_PRIVATE,
		       -1, 0);
	mprotect(p, 4096, PROT_NONE);
	mprotect(p, 4096, PROT_READ);
	(void)*(volatile unsigned char *)p;
}

On further inspection it seems like transition from PROT_EXEC to
PROT_NONE leaves the exec-only pkey lingering in the vma flags.  That
is, new_vma_pkey is non-zero in do_mprotect_pkey().  Later, then
enabling PROT_READ, the pkey remains and overrides the normal page
protections.

This change seems to help but is this the right way to solve it?

---
 arch/x86/mm/pkeys.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/x86/mm/pkeys.c b/arch/x86/mm/pkeys.c
index d7bc0eea20a5..4a837a220516 100644
--- a/arch/x86/mm/pkeys.c
+++ b/arch/x86/mm/pkeys.c
@@ -94,6 +94,10 @@ int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, int pkey
 	 */
 	if (pkey != -1)
 		return pkey;
+
+	if ((prot & (PROT_READ|PROT_WRITE|PROT_EXEC)) == 0)
+		return 0;
+
 	/*
 	 * Look for a protection-key-drive execute-only mapping
 	 * which is now being given permissions that are not
-- 
2.16.2.804.g6dcf76e118-goog

                 reply	other threads:[~2018-03-16 21:36 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=20180316213604.167305-1-shakeelb@google.com \
    --to=shakeelb@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=gthelen@google.com \
    --cc=hpa@zytor.com \
    --cc=jsperbeck@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).