linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
To: Qian Cai <cai@lca.pw>
Cc: dave.hansen@intel.com, bp@suse.de, tglx@linutronix.de,
	x86@kernel.org, "linux-mm@kvack.org" <linux-mm@kvack.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	luto@amacapital.net, hpa@zytor.com, mingo@kernel.org,
	Vlastimil Babka <vbabka@suse.cz>
Subject: [RFC PATCH] x86/fpu: Use get_user_pages_unlocked() to fault-in pages
Date: Wed, 1 May 2019 11:00:48 +0200	[thread overview]
Message-ID: <20190501090048.emqugoplr4sajnqc@linutronix.de> (raw)
In-Reply-To: <1556657902.6132.13.camel@lca.pw>

Using get_user_pages() seems to be problematic: KASAN reports
use-after-free in LTP's signal06 testcase.
The test invokes the signal handler with a provided stack and changes
the RW/WO page flags of the stack while the signal is invoked.  A crash
due to a NULL pointer has also been observed.

get_user_pages() may be invoked (or so I assumed) without holding the
mmap_sem for pre-faulting. KASAN probably slows down processing that we
can observe the user-after-free while page-flags are changed. It does
not happen without KASAN.

Use get_user_pages_unlocked() which holds the mm_sem around while
paging-in user memory.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---

While this fixes the problem and the crash later on, I would like to
hear from MM folks if it is intended to invoke get_user_pages() without
holding the mmap_sem. Without passing lockde & pages we only do:
  __get_user_pages_locked()
    - __get_user_pages()
    - if (!pages)
	/* If it's a prefault don't insist harder
	 */
	return ret;
Which was my intention. 
The comment above faultin_page() says "mmap_sem must be held on entry"
so this makes me thing that one must hold it…

 arch/x86/kernel/fpu/signal.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c
index eaddb185cac95..3a94e3d2e3bdf 100644
--- a/arch/x86/kernel/fpu/signal.c
+++ b/arch/x86/kernel/fpu/signal.c
@@ -172,8 +172,8 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
 		aligned_size = offset_in_page(buf_fx) + fpu_user_xstate_size;
 		nr_pages = DIV_ROUND_UP(aligned_size, PAGE_SIZE);
 
-		ret = get_user_pages((unsigned long)buf_fx, nr_pages,
-				     FOLL_WRITE, NULL, NULL);
+		ret = get_user_pages_unlocked((unsigned long)buf_fx, nr_pages,
+					      NULL, FOLL_WRITE);
 		if (ret == nr_pages)
 			goto retry;
 		return -EFAULT;
-- 
2.20.1


      parent reply	other threads:[~2019-05-01  9:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-30 20:58 copy_fpstate_to_sigframe() use-after-free Qian Cai
2019-05-01  8:23 ` Borislav Petkov
2019-05-02 17:11   ` [PATCH v2] x86/fpu: Fault-in user stack if copy_fpstate_to_sigframe() fails Sebastian Andrzej Siewior
2019-05-03  6:04     ` Borislav Petkov
2019-05-01  9:00 ` Sebastian Andrzej Siewior [this message]

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=20190501090048.emqugoplr4sajnqc@linutronix.de \
    --to=bigeasy@linutronix.de \
    --cc=bp@suse.de \
    --cc=cai@lca.pw \
    --cc=dave.hansen@intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@amacapital.net \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=vbabka@suse.cz \
    --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).