public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Masami Hiramatsu <mhiramat@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>,
	Linus Torvalds <torvalds@linux-foundation.org>
Cc: mhiramat@kernel.org, linux-kernel@vger.kernel.org,
	Andy Lutomirski <luto@amacapital.net>,
	Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Changbin Du <changbin.du@gmail.com>, Jann Horn <jannh@google.com>,
	Kees Cook <keescook@chromium.org>,
	Andy Lutomirski <luto@kernel.org>,
	Alexei Starovoitov <alexei.starovoitov@gmail.com>,
	Nadav Amit <namit@vmware.com>,
	Peter Zijlstra <peterz@infradead.org>
Subject: [RFC PATCH 2/4] uaccess: Add non-pagefault user-space read functions
Date: Mon, 25 Feb 2019 23:05:41 +0900	[thread overview]
Message-ID: <155110354092.21156.13871336589042178985.stgit@devbox> (raw)
In-Reply-To: <155110348217.21156.3874419272673328527.stgit@devbox>

Add probe_user_read() and strncpy_from_unsafe_user() which
will not involves mm_sem so we can use it for accessing
user-space in irq-handler.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
 include/linux/uaccess.h |   13 +++++
 mm/maccess.c            |  134 +++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 131 insertions(+), 16 deletions(-)

diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 37b226e8df13..906573b8f02c 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -240,6 +240,17 @@ extern long probe_kernel_read(void *dst, const void *src, size_t size);
 extern long __probe_kernel_read(void *dst, const void *src, size_t size);
 
 /*
+ * probe_user_read(): safely attempt to read from a location in user space
+ * @dst: pointer to the buffer that shall take the data
+ * @src: address to read from
+ * @size: size of the data chunk
+ *
+ * Safely read from address @src to the buffer at @dst.  If a kernel fault
+ * happens, handle that and return -EFAULT.
+ */
+extern long probe_user_read(void *dst, const void __user *src, size_t size);
+
+/*
  * probe_kernel_write(): safely attempt to write to a location
  * @dst: address to write to
  * @src: pointer to the data that shall be written
@@ -252,6 +263,8 @@ extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
 extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
 
 extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
+extern long strncpy_from_unsafe_user(char *dst, const void __user *unsafe_addr,
+				     long count);
 
 /**
  * probe_kernel_address(): safely attempt to read from a location
diff --git a/mm/maccess.c b/mm/maccess.c
index 7a9752d59587..7d38d783e0b1 100644
--- a/mm/maccess.c
+++ b/mm/maccess.c
@@ -5,8 +5,20 @@
 #include <linux/mm.h>
 #include <linux/uaccess.h>
 
+static __always_inline long
+probe_read_common(void *dst, const void __user *src, size_t size)
+{
+	long ret;
+
+	pagefault_disable();
+	ret = __copy_from_user_inatomic(dst, src, size);
+	pagefault_enable();
+
+	return ret ? -EFAULT : 0;
+}
+
 /**
- * probe_kernel_read(): safely attempt to read from a location
+ * probe_kernel_read(): safely attempt to read from a kernel-space location
  * @dst: pointer to the buffer that shall take the data
  * @src: address to read from
  * @size: size of the data chunk
@@ -30,18 +42,53 @@ long __probe_kernel_read(void *dst, const void *src, size_t size)
 
 	set_fs(KERNEL_DS);
 	current->kernel_uaccess_faults_ok++;
-	pagefault_disable();
-	ret = __copy_from_user_inatomic(dst,
-			(__force const void __user *)src, size);
-	pagefault_enable();
+	ret = probe_read_common(dst, (__force const void __user *)src, size);
 	current->kernel_uaccess_faults_ok--;
 	set_fs(old_fs);
 
-	return ret ? -EFAULT : 0;
+	return ret;
 }
 EXPORT_SYMBOL_GPL(probe_kernel_read);
 
 /**
+ * probe_user_read(): safely attempt to read from a user-space location
+ * @dst: pointer to the buffer that shall take the data
+ * @src: address to read from. This must be a user address.
+ * @size: size of the data chunk
+ *
+ * Safely read from user address @src to the buffer at @dst. If a kernel fault
+ * happens, handle that and return -EFAULT.
+ */
+
+long __weak probe_user_read(void *dst, const void __user *src, size_t size)
+    __attribute__((alias("__probe_user_read")));
+
+static long __normal_probe_user_read(void *dst, const void __user *src,
+				     size_t size)
+{
+	if (!access_ok(src, size))
+		return -EFAULT;
+
+	return probe_read_common(dst, src, size);
+}
+
+long __probe_user_read(void *dst, const void __user *src, size_t size)
+{
+	long ret;
+	mm_segment_t old_fs = get_fs();
+
+	if (segment_eq(old_fs, USER_DS)) {
+		ret = __normal_probe_user_read(dst, src, size);
+	} else {
+		set_fs(USER_DS);
+		ret = __normal_probe_user_read(dst, src, size);
+		set_fs(old_fs);
+	}
+	return ret ? -EFAULT : 0;
+}
+EXPORT_SYMBOL_GPL(probe_user_read);
+
+/**
  * probe_kernel_write(): safely attempt to write to a location
  * @dst: address to write to
  * @src: pointer to the data that shall be written
@@ -70,6 +117,22 @@ long __probe_kernel_write(void *dst, const void *src, size_t size)
 }
 EXPORT_SYMBOL_GPL(probe_kernel_write);
 
+
+static __always_inline long strncpy_from_unsafe_common(char *dst,
+				const char __user *unsafe_addr, long count)
+{
+	const char __user *src = unsafe_addr;
+	int ret;
+
+	pagefault_disable();
+	do {
+		ret = __get_user(*dst++, src++);
+	} while (dst[-1] && ret == 0 && src - unsafe_addr < count);
+	dst[-1] = '\0';
+	pagefault_enable();
+
+	return ret ? -EFAULT : src - unsafe_addr;
+}
 /**
  * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
  * @dst:   Destination address, in kernel space.  This buffer must be at
@@ -90,7 +153,6 @@ EXPORT_SYMBOL_GPL(probe_kernel_write);
 long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
 {
 	mm_segment_t old_fs = get_fs();
-	const void *src = unsafe_addr;
 	long ret;
 
 	if (unlikely(count <= 0))
@@ -98,16 +160,56 @@ long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
 
 	set_fs(KERNEL_DS);
 	current->kernel_uaccess_faults_ok++;
-	pagefault_disable();
-
-	do {
-		ret = __get_user(*dst++, (const char __user __force *)src++);
-	} while (dst[-1] && ret == 0 && src - unsafe_addr < count);
-
-	dst[-1] = '\0';
-	pagefault_enable();
+	ret = strncpy_from_unsafe_common(dst,
+			(const char __user __force *)unsafe_addr, count);
 	current->kernel_uaccess_faults_ok--;
 	set_fs(old_fs);
 
-	return ret ? -EFAULT : src - unsafe_addr;
+	return ret;
+}
+
+static __always_inline long __strncpy_from_unsafe_user(char *dst,
+			 const char __user *unsafe_addr, long count)
+{
+	if (!access_ok(unsafe_addr, count))
+		return -EFAULT;
+
+	return strncpy_from_unsafe_common(dst, unsafe_addr, count);
+}
+
+/**
+ * strncpy_from_unsafe_user: - Copy a NUL terminated string from unsafe user
+ *				address.
+ * @dst:   Destination address, in kernel space.  This buffer must be at
+ *         least @count bytes long.
+ * @unsafe_addr: Unsafe user address.
+ * @count: Maximum number of bytes to copy, including the trailing NUL.
+ *
+ * Copies a NUL-terminated string from unsafe user address to kernel buffer.
+ *
+ * On success, returns the length of the string INCLUDING the trailing NUL.
+ *
+ * If access fails, returns -EFAULT (some data may have been copied
+ * and the trailing NUL added).
+ *
+ * If @count is smaller than the length of the string, copies @count-1 bytes,
+ * sets the last byte of @dst buffer to NUL and returns @count.
+ */
+long strncpy_from_unsafe_user(char *dst, const void __user *unsafe_addr,
+			      long count)
+{
+	mm_segment_t old_fs = get_fs();
+	long ret;
+
+	if (unlikely(count <= 0))
+		return 0;
+
+	if (segment_eq(old_fs, USER_DS)) {
+		ret = __strncpy_from_unsafe_user(dst, unsafe_addr, count);
+	} else {
+		set_fs(USER_DS);
+		ret = __strncpy_from_unsafe_user(dst, unsafe_addr, count);
+		set_fs(old_fs);
+	}
+	return ret;
 }


  parent reply	other threads:[~2019-02-25 14:06 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-25 14:04 [RFC PATCH 0/4] tracing/probes: uaccess: Add support user-space access Masami Hiramatsu
2019-02-25 14:05 ` [RFC PATCH 1/4] uaccess: Make sure kernel_uaccess_faults_ok is updated before pagefault Masami Hiramatsu
2019-02-25 14:05 ` Masami Hiramatsu [this message]
2019-02-25 15:06   ` [RFC PATCH 2/4] uaccess: Add non-pagefault user-space read functions Peter Zijlstra
2019-02-25 17:00     ` Linus Torvalds
2019-02-25 18:16       ` Andy Lutomirski
2019-02-26  4:16       ` Masami Hiramatsu
2019-02-26 12:24         ` Masami Hiramatsu
2019-02-26 15:14           ` [RFC PATCH v2] " Masami Hiramatsu
2019-02-26  3:01     ` [RFC PATCH 2/4] " Masami Hiramatsu
2019-02-25 17:06   ` Kees Cook
2019-02-26  4:07     ` Masami Hiramatsu
2019-02-25 14:06 ` [RFC PATCH 3/4] tracing/probe: Add ustring type for user-space string Masami Hiramatsu
2019-02-25 14:06 ` [RFC PATCH 4/4] tracing/probe: Support user-space dereference Masami Hiramatsu
2019-02-26 21:38 ` [RFC PATCH 0/4] tracing/probes: uaccess: Add support user-space access Joel Fernandes
2019-02-27  7:41   ` Masami Hiramatsu
2019-02-27  8:00     ` Peter Zijlstra
2019-02-27 11:39       ` Masami Hiramatsu
2019-02-27 21:33     ` Joel Fernandes

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=155110354092.21156.13871336589042178985.stgit@devbox \
    --to=mhiramat@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alexei.starovoitov@gmail.com \
    --cc=changbin.du@gmail.com \
    --cc=jannh@google.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=luto@kernel.org \
    --cc=mingo@kernel.org \
    --cc=namit@vmware.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=torvalds@linux-foundation.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