diff options
author | Andrew Morton <akpm@linux-foundation.org> | 2015-11-05 21:46:03 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2015-11-05 22:34:48 -0500 |
commit | 0ab32b6f1b88444524e52429fab334ff96683a3f (patch) | |
tree | 421de8da078f1ca72131097fdee0b9d4d1ef7c59 | |
parent | 86d2adccfbe7d5a1f050fa08db9638c9168736d9 (diff) |
uaccess: reimplement probe_kernel_address() using probe_kernel_read()
probe_kernel_address() is basically the same as the (later added)
probe_kernel_read().
The return value on EFAULT is a bit different: probe_kernel_address()
returns number-of-bytes-not-copied whereas probe_kernel_read() returns
-EFAULT. All callers have been checked, none cared.
probe_kernel_read() can be overridden by the architecture whereas
probe_kernel_address() cannot. parisc, blackfin and um do this, to insert
additional checking. Hence this patch possibly fixes obscure bugs,
although there are only two probe_kernel_address() callsites outside
arch/.
My first attempt involved removing probe_kernel_address() entirely and
converting all callsites to use probe_kernel_read() directly, but that got
tiresome.
This patch shrinks mm/slab_common.o by 218 bytes. For a single
probe_kernel_address() callsite.
Cc: Steven Miao <realmz6@gmail.com>
Cc: Jeff Dike <jdike@addtoit.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: "James E.J. Bottomley" <jejb@parisc-linux.org>
Cc: Helge Deller <deller@gmx.de>
Cc: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | arch/arm/mm/alignment.c | 2 | ||||
-rw-r--r-- | arch/powerpc/sysdev/fsl_pci.c | 2 | ||||
-rw-r--r-- | include/linux/uaccess.h | 40 | ||||
-rw-r--r-- | mm/maccess.c | 5 |
4 files changed, 17 insertions, 32 deletions
diff --git a/arch/arm/mm/alignment.c b/arch/arm/mm/alignment.c index 00b7f7de28a1..7d5f4c736a16 100644 --- a/arch/arm/mm/alignment.c +++ b/arch/arm/mm/alignment.c | |||
@@ -803,7 +803,7 @@ do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | |||
803 | } | 803 | } |
804 | } | 804 | } |
805 | } else { | 805 | } else { |
806 | fault = probe_kernel_address(instrptr, instr); | 806 | fault = probe_kernel_address((void *)instrptr, instr); |
807 | instr = __mem_to_opcode_arm(instr); | 807 | instr = __mem_to_opcode_arm(instr); |
808 | } | 808 | } |
809 | 809 | ||
diff --git a/arch/powerpc/sysdev/fsl_pci.c b/arch/powerpc/sysdev/fsl_pci.c index ebc1f412cf49..13b9bcf5485e 100644 --- a/arch/powerpc/sysdev/fsl_pci.c +++ b/arch/powerpc/sysdev/fsl_pci.c | |||
@@ -999,7 +999,7 @@ int fsl_pci_mcheck_exception(struct pt_regs *regs) | |||
999 | ret = get_user(regs->nip, &inst); | 999 | ret = get_user(regs->nip, &inst); |
1000 | pagefault_enable(); | 1000 | pagefault_enable(); |
1001 | } else { | 1001 | } else { |
1002 | ret = probe_kernel_address(regs->nip, inst); | 1002 | ret = probe_kernel_address((void *)regs->nip, inst); |
1003 | } | 1003 | } |
1004 | 1004 | ||
1005 | if (mcheck_handle_load(regs, inst)) { | 1005 | if (mcheck_handle_load(regs, inst)) { |
diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index d6f2c2c5b043..558129af828a 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h | |||
@@ -75,36 +75,6 @@ static inline unsigned long __copy_from_user_nocache(void *to, | |||
75 | 75 | ||
76 | #endif /* ARCH_HAS_NOCACHE_UACCESS */ | 76 | #endif /* ARCH_HAS_NOCACHE_UACCESS */ |
77 | 77 | ||
78 | /** | ||
79 | * probe_kernel_address(): safely attempt to read from a location | ||
80 | * @addr: address to read from - its type is type typeof(retval)* | ||
81 | * @retval: read into this variable | ||
82 | * | ||
83 | * Safely read from address @addr into variable @revtal. If a kernel fault | ||
84 | * happens, handle that and return -EFAULT. | ||
85 | * We ensure that the __get_user() is executed in atomic context so that | ||
86 | * do_page_fault() doesn't attempt to take mmap_sem. This makes | ||
87 | * probe_kernel_address() suitable for use within regions where the caller | ||
88 | * already holds mmap_sem, or other locks which nest inside mmap_sem. | ||
89 | * This must be a macro because __get_user() needs to know the types of the | ||
90 | * args. | ||
91 | * | ||
92 | * We don't include enough header files to be able to do the set_fs(). We | ||
93 | * require that the probe_kernel_address() caller will do that. | ||
94 | */ | ||
95 | #define probe_kernel_address(addr, retval) \ | ||
96 | ({ \ | ||
97 | long ret; \ | ||
98 | mm_segment_t old_fs = get_fs(); \ | ||
99 | \ | ||
100 | set_fs(KERNEL_DS); \ | ||
101 | pagefault_disable(); \ | ||
102 | ret = __copy_from_user_inatomic(&(retval), (__force typeof(retval) __user *)(addr), sizeof(retval)); \ | ||
103 | pagefault_enable(); \ | ||
104 | set_fs(old_fs); \ | ||
105 | ret; \ | ||
106 | }) | ||
107 | |||
108 | /* | 78 | /* |
109 | * probe_kernel_read(): safely attempt to read from a location | 79 | * probe_kernel_read(): safely attempt to read from a location |
110 | * @dst: pointer to the buffer that shall take the data | 80 | * @dst: pointer to the buffer that shall take the data |
@@ -131,4 +101,14 @@ extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size | |||
131 | 101 | ||
132 | extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count); | 102 | extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count); |
133 | 103 | ||
104 | /** | ||
105 | * probe_kernel_address(): safely attempt to read from a location | ||
106 | * @addr: address to read from | ||
107 | * @retval: read into this variable | ||
108 | * | ||
109 | * Returns 0 on success, or -EFAULT. | ||
110 | */ | ||
111 | #define probe_kernel_address(addr, retval) \ | ||
112 | probe_kernel_read(&retval, addr, sizeof(retval)) | ||
113 | |||
134 | #endif /* __LINUX_UACCESS_H__ */ | 114 | #endif /* __LINUX_UACCESS_H__ */ |
diff --git a/mm/maccess.c b/mm/maccess.c index 34fe24759ed1..1b13638d238d 100644 --- a/mm/maccess.c +++ b/mm/maccess.c | |||
@@ -13,6 +13,11 @@ | |||
13 | * | 13 | * |
14 | * Safely read from address @src to the buffer at @dst. If a kernel fault | 14 | * Safely read from address @src to the buffer at @dst. If a kernel fault |
15 | * happens, handle that and return -EFAULT. | 15 | * happens, handle that and return -EFAULT. |
16 | * | ||
17 | * We ensure that the copy_from_user is executed in atomic context so that | ||
18 | * do_page_fault() doesn't attempt to take mmap_sem. This makes | ||
19 | * probe_kernel_read() suitable for use within regions where the caller | ||
20 | * already holds mmap_sem, or other locks which nest inside mmap_sem. | ||
16 | */ | 21 | */ |
17 | 22 | ||
18 | long __weak probe_kernel_read(void *dst, const void *src, size_t size) | 23 | long __weak probe_kernel_read(void *dst, const void *src, size_t size) |