The callers of the user nofault helpers (copy_{from,to}_user_nofault(),
strncpy_from_user_nofault() and strnlen_user_nofault()) that may run in
any context now check for a NULL current->mm themselves (see the
previous patches). Document that requirement, so that new callers do
too.

The remaining callers don't need to, as a NULL mm would be a bug there.
The next patch makes the uaccess helpers warn in that case.

 - btrfs's tree search ioctl, and vread_iter() through
   copy_page_to_iter_nofault(), run in process context. They use the
   nofault helpers only because they can't take page faults there.
 - KVM's steal time update already returns early when current->mm is
   not the VM's mm.
 - riscv's and powerpc's dumping of user instructions on faults, and the
   Freescale PCI machine check handler, only access user memory when
   the faulting context is in user mode.
 - The architectures' perf_callchain_user(), e.g. powerpc's, which is
   only called by get_perf_callchain(), and only for a user task with
   an mm.

Signed-off-by: James Houghton <[email protected]>
---
 mm/maccess.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/mm/maccess.c b/mm/maccess.c
index 486559d68858..b99a1e465b1c 100644
--- a/mm/maccess.c
+++ b/mm/maccess.c
@@ -116,6 +116,10 @@ long strncpy_from_kernel_nofault(char *dst, const void 
*unsafe_addr, long count)
  *
  * Safely read from user address @src to the buffer at @dst. If a kernel fault
  * happens, handle that and return -EFAULT.
+ *
+ * The current task must have an mm. Callers that may run without one, e.g.
+ * tracing helpers that can run in kernel threads, must check current->mm
+ * themselves first.
  */
 long copy_from_user_nofault(void *dst, const void __user *src, size_t size)
 {
@@ -145,6 +149,10 @@ EXPORT_SYMBOL_GPL(copy_from_user_nofault);
  *
  * Safely write to address @dst from the buffer at @src.  If a kernel fault
  * happens, handle that and return -EFAULT.
+ *
+ * The current task must have an mm. Callers that may run without one, e.g.
+ * tracing helpers that can run in kernel threads, must check current->mm
+ * themselves first.
  */
 long copy_to_user_nofault(void __user *dst, const void *src, size_t size)
 {
@@ -179,6 +187,10 @@ EXPORT_SYMBOL_GPL(copy_to_user_nofault);
  *
  * 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.
+ *
+ * The current task must have an mm. Callers that may run without one, e.g.
+ * tracing helpers that can run in kernel threads, must check current->mm
+ * themselves first.
  */
 long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
                              long count)
@@ -217,6 +229,10 @@ long strncpy_from_user_nofault(char *dst, const void 
__user *unsafe_addr,
  *
  * Unlike strnlen_user, this can be used from IRQ handler etc. because
  * it disables pagefaults.
+ *
+ * The current task must have an mm. Callers that may run without one, e.g.
+ * tracing helpers that can run in kernel threads, must check current->mm
+ * themselves first.
  */
 long strnlen_user_nofault(const void __user *unsafe_addr, long count)
 {
-- 
2.56.1.911.g883d8bcb20-goog


Reply via email to