mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xiao Guangrong <guangrong.xiao@linux.intel.com>
To: pbonzini@redhat.com
Cc: gleb@kernel.org, mtosatti@redhat.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	root <root@lyu2-mobl2.ccr.corp.intel.com>,
	Xiao Guangrong <guangrong.xiao@linux.intel.com>
Subject: [PATCH] check smap and !cr0.wp
Date: Thu,  7 May 2015 16:30:56 +0800	[thread overview]
Message-ID: <1430987456-6790-1-git-send-email-guangrong.xiao@linux.intel.com> (raw)

From: root <root@lyu2-mobl2.ccr.corp.intel.com>

This test case is used to produce the bug that:

KVM may turn a user page to a kernel page when kernel writes a readonly
user page if CR0.WP = 1. This shadow page entry will be reused after
SMAP is enabled so that kernel is allowed to access this user page

Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
---
 x86/smap.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/x86/smap.c b/x86/smap.c
index 042c5aa..66f97b8 100644
--- a/x86/smap.c
+++ b/x86/smap.c
@@ -48,6 +48,7 @@ asm ("pf_tss:\n"
 
 #define USER_BASE	(1 << 24)
 #define USER_VAR(v)	(*((__typeof__(&(v))) (((unsigned long)&v) + USER_BASE)))
+#define USER_ADDR(v)   ((void *)((unsigned long)(&v) + USER_BASE))
 
 static void init_test(int i)
 {
@@ -58,6 +59,29 @@ static void init_test(int i)
 	}
 }
 
+static void check_smap_nowp(void)
+{
+	test = 0x99;
+
+	*get_pte(phys_to_virt(read_cr3()), USER_ADDR(test)) &= ~PTE_WRITE;
+
+	write_cr4(read_cr4() & ~X86_CR4_SMAP);
+	write_cr0(read_cr0() & ~X86_CR0_WP);
+	clac();
+	write_cr3(read_cr3());
+
+	init_test(0);
+	USER_VAR(test) = 0x99;
+	report("write from user page with SMAP=0, AC=0, WP=0, PTE.U=1 && PTE.W=0", pf_count == 0);
+
+	write_cr4(read_cr4() | X86_CR4_SMAP);
+	write_cr3(read_cr3());
+
+	init_test(0);
+	(void)USER_VAR(test);
+	report("read from user page with SMAP=1, AC=0, WP=0, PTE.U=1 && PTE.W=0", pf_count == 1 && save == 0x99);
+}
+
 int main(int ac, char **av)
 {
 	unsigned long i;
@@ -150,6 +174,8 @@ int main(int ac, char **av)
 		report("executing on user page with AC=0", pf_count == 0);
 	}
 
+	check_smap_nowp();
+
 	// TODO: implicit kernel access from ring 3 (e.g. int)
 
 	return report_summary();
-- 
1.8.3.1


             reply	other threads:[~2015-05-07  8:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-07  8:30 Xiao Guangrong [this message]
2015-05-07  8:39 ` Xiao Guangrong
2015-05-07  8:43   ` Paolo Bonzini
2015-05-07  8:44 Xiao Guangrong
2015-05-12  3:13 ` Xiao Guangrong
2015-05-12  9:19   ` Paolo Bonzini

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=1430987456-6790-1-git-send-email-guangrong.xiao@linux.intel.com \
    --to=guangrong.xiao@linux.intel.com \
    --cc=gleb@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=root@lyu2-mobl2.ccr.corp.intel.com \
    /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®