uaccess: add generic __{get,put}_kernel_nofault
authorArnd Bergmann <arnd@arndb.de>
Fri, 11 Feb 2022 07:50:00 +0000 (08:50 +0100)
committerArnd Bergmann <arnd@arndb.de>
Fri, 25 Feb 2022 08:36:05 +0000 (09:36 +0100)
Nine architectures are still missing __{get,put}_kernel_nofault:
alpha, ia64, microblaze, nds32, nios2, openrisc, sh, sparc32, xtensa.

Add a generic version that lets everything use the normal
copy_{from,to}_kernel_nofault() code based on these, removing the last
use of get_fs()/set_fs() from architecture-independent code.

Reviewed-by: Christoph Hellwig <hch@lst.de>
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
14 files changed:
arch/arm/include/asm/uaccess.h
arch/arm64/include/asm/uaccess.h
arch/m68k/include/asm/uaccess.h
arch/mips/include/asm/uaccess.h
arch/parisc/include/asm/uaccess.h
arch/powerpc/include/asm/uaccess.h
arch/riscv/include/asm/uaccess.h
arch/s390/include/asm/uaccess.h
arch/sparc/include/asm/uaccess_64.h
arch/um/include/asm/uaccess.h
arch/x86/include/asm/uaccess.h
include/asm-generic/uaccess.h
include/linux/uaccess.h
mm/maccess.c

index 32dbfd81f42a48a27c2927634d46a4d30609d7e1..d20d78c34b94581b5c359a2fa85251f6f101c84c 100644 (file)
@@ -476,8 +476,6 @@ do {                                                                        \
        : "r" (x), "i" (-EFAULT)                                \
        : "cc")
 
-#define HAVE_GET_KERNEL_NOFAULT
-
 #define __get_kernel_nofault(dst, src, type, err_label)                        \
 do {                                                                   \
        const type *__pk_ptr = (src);                                   \
index 3a5ff5e2058630e0c4fb1429bd2959a47b917380..2e20879fe3cf6904ce6e761bfb57c85cfbd5f2e9 100644 (file)
@@ -26,8 +26,6 @@
 #include <asm/memory.h>
 #include <asm/extable.h>
 
-#define HAVE_GET_KERNEL_NOFAULT
-
 /*
  * Test whether a block of memory is a valid user space address.
  * Returns 1 if the range is valid, 0 otherwise.
index ba670523885c89ff72352f0147da5534b3773a1b..79617c0b2f91694684340f04a232c9be9af1683a 100644 (file)
@@ -390,8 +390,6 @@ raw_copy_to_user(void __user *to, const void *from, unsigned long n)
 #define INLINE_COPY_FROM_USER
 #define INLINE_COPY_TO_USER
 
-#define HAVE_GET_KERNEL_NOFAULT
-
 #define __get_kernel_nofault(dst, src, type, err_label)                        \
 do {                                                                   \
        type *__gk_dst = (type *)(dst);                                 \
index f8f74f9f58836c8b09cbb1444daac5a56f0bf9ec..db9a8e002b6295b79b50a70661fb13af1fd4fd11 100644 (file)
@@ -296,8 +296,6 @@ struct __large_struct { unsigned long buf[100]; };
        (val) = __gu_tmp.t;                                             \
 }
 
-#define HAVE_GET_KERNEL_NOFAULT
-
 #define __get_kernel_nofault(dst, src, type, err_label)                        \
 do {                                                                   \
        int __gu_err;                                                   \
index ebf8a845b01742f1fe75c8cace45b6e2ef4373ce..0925bbd6db6788c1ea7915b78a238456ceb94997 100644 (file)
@@ -95,7 +95,6 @@ struct exception_table_entry {
        (val) = (__force __typeof__(*(ptr))) __gu_val;  \
 }
 
-#define HAVE_GET_KERNEL_NOFAULT
 #define __get_kernel_nofault(dst, src, type, err_label)        \
 {                                                      \
        type __z;                                       \
index 63316100080c1e312bd7cd762e0522312209c10f..a0032c2e7550eea28a9efdc6840e7d527cb876d9 100644 (file)
@@ -467,8 +467,6 @@ do {                                                                        \
                unsafe_put_user(*(u8*)(_src + _i), (u8 __user *)(_dst + _i), e); \
 } while (0)
 
-#define HAVE_GET_KERNEL_NOFAULT
-
 #define __get_kernel_nofault(dst, src, type, err_label)                        \
        __get_user_size_goto(*((type *)(dst)),                          \
                (__force type __user *)(src), sizeof(type), err_label)
index c701a5e57a2b44b822290ef4f4bfb001120fa3f3..4407b9e48d2c485b7bba873c1461c598d5ade4c0 100644 (file)
@@ -346,8 +346,6 @@ unsigned long __must_check clear_user(void __user *to, unsigned long n)
                __clear_user(to, n) : n;
 }
 
-#define HAVE_GET_KERNEL_NOFAULT
-
 #define __get_kernel_nofault(dst, src, type, err_label)                        \
 do {                                                                   \
        long __kr_err;                                                  \
index d74e26b48604d720b5b776057320b3390b10ee3b..29332edf46f0e6e8a0adadb36837f922c2e4e064 100644 (file)
@@ -282,8 +282,6 @@ static inline unsigned long __must_check clear_user(void __user *to, unsigned lo
 int copy_to_user_real(void __user *dest, void *src, unsigned long count);
 void *s390_kernel_write(void *dst, const void *src, size_t size);
 
-#define HAVE_GET_KERNEL_NOFAULT
-
 int __noreturn __put_kernel_bad(void);
 
 #define __put_kernel_asm(val, to, insn)                                        \
index b283798315b1b0366c2bf08f97d94a7481ccac90..5c12fb46bc61940b25a0caa33af6664dcdb2c46f 100644 (file)
@@ -210,8 +210,6 @@ __asm__ __volatile__(                                                       \
               : "=r" (ret), "=r" (x) : "r" (__m(addr)),                \
                 "i" (-EFAULT))
 
-#define HAVE_GET_KERNEL_NOFAULT
-
 #define __get_user_nocheck(data, addr, size, type) ({                       \
        register int __gu_ret;                                               \
        register unsigned long __gu_val;                                     \
index 17d18cfd82a51fb46b7a6864c244d077a4ccfe21..1ecfc96bcc50684d834859a4b74718663075250d 100644 (file)
@@ -44,8 +44,6 @@ static inline int __access_ok(unsigned long addr, unsigned long size)
 }
 
 /* no pagefaults for kernel addresses in um */
-#define HAVE_GET_KERNEL_NOFAULT 1
-
 #define __get_kernel_nofault(dst, src, type, err_label)                        \
 do {                                                                   \
        *((type *)dst) = get_unaligned((type *)(src));                  \
index a59ba2578e6483e10fe2ba9dd080b913040fcdb6..201efcec66b7639707631529499034ea9992479e 100644 (file)
@@ -507,8 +507,6 @@ do {                                                                        \
        unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u8, label);   \
 } while (0)
 
-#define HAVE_GET_KERNEL_NOFAULT
-
 #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
 #define __get_kernel_nofault(dst, src, type, err_label)                        \
        __get_user_size(*((type *)(dst)), (__force type __user *)(src), \
index 10ffa8b5c1175fcfe4f049ac0ebc572149147dbe..0870fa11a7c5772830a4c1c20931f48b24791525 100644 (file)
@@ -77,8 +77,6 @@ do {                                                                  \
                goto err_label;                                         \
 } while (0)
 
-#define HAVE_GET_KERNEL_NOFAULT 1
-
 static inline __must_check unsigned long
 raw_copy_from_user(void *to, const void __user * from, unsigned long n)
 {
index ac0394087f7d4d2ad569eeca9dba0e8c50893d23..67e9bc94dc40e359f656526ab9dacdcbab4f907d 100644 (file)
@@ -368,6 +368,25 @@ long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
                long count);
 long strnlen_user_nofault(const void __user *unsafe_addr, long count);
 
+#ifndef __get_kernel_nofault
+#define __get_kernel_nofault(dst, src, type, label)    \
+do {                                                   \
+       type __user *p = (type __force __user *)(src);  \
+       type data;                                      \
+       if (__get_user(data, p))                        \
+               goto label;                             \
+       *(type *)dst = data;                            \
+} while (0)
+
+#define __put_kernel_nofault(dst, src, type, label)    \
+do {                                                   \
+       type __user *p = (type __force __user *)(dst);  \
+       type data = *(type *)src;                       \
+       if (__put_user(data, p))                        \
+               goto label;                             \
+} while (0)
+#endif
+
 /**
  * get_kernel_nofault(): safely attempt to read from a location
  * @val: read into this variable
index d3f1a1f0b1c1acca131d05b82e9c87c45467f4f2..cbd1b3959af245519b6aa125eb24accf9613b372 100644 (file)
@@ -12,8 +12,6 @@ bool __weak copy_from_kernel_nofault_allowed(const void *unsafe_src,
        return true;
 }
 
-#ifdef HAVE_GET_KERNEL_NOFAULT
-
 #define copy_from_kernel_nofault_loop(dst, src, len, type, err_label)  \
        while (len >= sizeof(type)) {                                   \
                __get_kernel_nofault(dst, src, type, err_label);                \
@@ -102,112 +100,6 @@ Efault:
        dst[-1] = '\0';
        return -EFAULT;
 }
-#else /* HAVE_GET_KERNEL_NOFAULT */
-/**
- * copy_from_kernel_nofault(): safely attempt to read from kernel-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 kernel address @src to the buffer at @dst.  If a kernel
- * fault happens, handle that and return -EFAULT.  If @src is not a valid kernel
- * address, return -ERANGE.
- *
- * We ensure that the copy_from_user is executed in atomic context so that
- * do_page_fault() doesn't attempt to take mmap_lock.  This makes
- * copy_from_kernel_nofault() suitable for use within regions where the caller
- * already holds mmap_lock, or other locks which nest inside mmap_lock.
- */
-long copy_from_kernel_nofault(void *dst, const void *src, size_t size)
-{
-       long ret;
-       mm_segment_t old_fs = get_fs();
-
-       if (!copy_from_kernel_nofault_allowed(src, size))
-               return -ERANGE;
-
-       set_fs(KERNEL_DS);
-       pagefault_disable();
-       ret = __copy_from_user_inatomic(dst, (__force const void __user *)src,
-                       size);
-       pagefault_enable();
-       set_fs(old_fs);
-
-       if (ret)
-               return -EFAULT;
-       return 0;
-}
-EXPORT_SYMBOL_GPL(copy_from_kernel_nofault);
-
-/**
- * copy_to_kernel_nofault(): safely attempt to write to a location
- * @dst: address to write to
- * @src: pointer to the data that shall be written
- * @size: size of the data chunk
- *
- * Safely write to address @dst from the buffer at @src.  If a kernel fault
- * happens, handle that and return -EFAULT.
- */
-long copy_to_kernel_nofault(void *dst, const void *src, size_t size)
-{
-       long ret;
-       mm_segment_t old_fs = get_fs();
-
-       set_fs(KERNEL_DS);
-       pagefault_disable();
-       ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
-       pagefault_enable();
-       set_fs(old_fs);
-
-       if (ret)
-               return -EFAULT;
-       return 0;
-}
-
-/**
- * strncpy_from_kernel_nofault: - Copy a NUL terminated string from unsafe
- *                              address.
- * @dst:   Destination address, in kernel space.  This buffer must be at
- *         least @count bytes long.
- * @unsafe_addr: Unsafe address.
- * @count: Maximum number of bytes to copy, including the trailing NUL.
- *
- * Copies a NUL-terminated string from unsafe 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 @unsafe_addr is not a valid kernel address, return
- * -ERANGE.
- *
- * 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_kernel_nofault(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))
-               return 0;
-       if (!copy_from_kernel_nofault_allowed(unsafe_addr, count))
-               return -ERANGE;
-
-       set_fs(KERNEL_DS);
-       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();
-       set_fs(old_fs);
-
-       return ret ? -EFAULT : src - unsafe_addr;
-}
-#endif /* HAVE_GET_KERNEL_NOFAULT */
 
 /**
  * copy_from_user_nofault(): safely attempt to read from a user-space location