diff options
author | Jan Beulich <JBeulich@suse.com> | 2013-10-21 04:43:57 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@kernel.org> | 2013-10-26 06:27:36 -0400 |
commit | 3df7b41aa5e7797f391d0a41f8b0dce1fe366a09 (patch) | |
tree | f79334f0230b5728f1b541e1ef500373c0f764a6 | |
parent | 88829dfe4b6ea0c1c24d415668b3bfa78e5a5b16 (diff) |
x86: Unify copy_from_user() size checking
Commits 4a3127693001c61a21d1ce680db6340623f52e93 ("x86: Turn the
copy_from_user check into an (optional) compile time warning")
and 63312b6a6faae3f2e5577f2b001e3b504f10a2aa ("x86: Add a
Kconfig option to turn the copy_from_user warnings into errors")
touched only the 32-bit variant of copy_from_user(), whereas the
original commit 9f0cf4adb6aa0bfccf675c938124e68f7f06349d ("x86:
Use __builtin_object_size() to validate the buffer size for
copy_from_user()") also added the same code to the 64-bit one.
Further the earlier conversion from an inline WARN() to the call
to copy_from_user_overflow() went a little too far: When the
number of bytes to be copied is not a constant (e.g. [looking at
3.11] in drivers/net/tun.c:__tun_chr_ioctl() or
drivers/pci/pcie/aer/aer_inject.c:aer_inject_write()), the
compiler will always have to keep the funtion call, and hence
there will always be a warning. By using __builtin_constant_p()
we can avoid this.
And then this slightly extends the effect of
CONFIG_DEBUG_STRICT_USER_COPY_CHECKS in that apart from
converting warnings to errors in the constant size case, it
retains the (possibly wrong) warnings in the non-constant size
case, such that if someone is prepared to get a few false
positives, (s)he'll be able to recover the current behavior
(except that these diagnostics now will never be converted to
errors).
Since the 32-bit variant (intentionally) didn't call
might_fault(), the unification results in this being called
twice now. Adding a suitable #ifdef would be the alternative if
that's a problem.
I'd like to point out though that with
__compiletime_object_size() being restricted to gcc before 4.6,
the whole construct is going to become more and more pointless
going forward. I would question however that commit
2fb0815c9ee6b9ac50e15dd8360ec76d9fa46a2 ("gcc4: disable
__compiletime_object_size for GCC 4.6+") was really necessary,
and instead this should have been dealt with as is done here
from the beginning.
Signed-off-by: Jan Beulich <jbeulich@suse.com>
Cc: Arjan van de Ven <arjan@linux.intel.com>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/5265056D02000078000FC4F3@nat28.tlf.novell.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r-- | arch/x86/include/asm/uaccess.h | 68 | ||||
-rw-r--r-- | arch/x86/include/asm/uaccess_32.h | 26 | ||||
-rw-r--r-- | arch/x86/include/asm/uaccess_64.h | 18 | ||||
-rw-r--r-- | arch/x86/lib/usercopy_32.c | 3 |
4 files changed, 69 insertions, 46 deletions
diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h index 5838fa911aa0..c9799ed208a8 100644 --- a/arch/x86/include/asm/uaccess.h +++ b/arch/x86/include/asm/uaccess.h | |||
@@ -542,5 +542,73 @@ extern struct movsl_mask { | |||
542 | # include <asm/uaccess_64.h> | 542 | # include <asm/uaccess_64.h> |
543 | #endif | 543 | #endif |
544 | 544 | ||
545 | unsigned long __must_check _copy_from_user(void *to, const void __user *from, | ||
546 | unsigned n); | ||
547 | |||
548 | #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS | ||
549 | # define copy_user_diag __compiletime_error | ||
550 | #else | ||
551 | # define copy_user_diag __compiletime_warning | ||
552 | #endif | ||
553 | |||
554 | extern void copy_user_diag("copy_from_user() buffer size is too small") | ||
555 | copy_from_user_overflow(void); | ||
556 | |||
557 | #undef copy_user_diag | ||
558 | |||
559 | #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS | ||
560 | |||
561 | extern void | ||
562 | __compiletime_warning("copy_from_user() buffer size is not provably correct") | ||
563 | __copy_from_user_overflow(void) __asm__("copy_from_user_overflow"); | ||
564 | #define __copy_from_user_overflow(size, count) __copy_from_user_overflow() | ||
565 | |||
566 | #else | ||
567 | |||
568 | static inline void | ||
569 | __copy_from_user_overflow(int size, unsigned long count) | ||
570 | { | ||
571 | WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count); | ||
572 | } | ||
573 | |||
574 | #endif | ||
575 | |||
576 | static inline unsigned long __must_check | ||
577 | copy_from_user(void *to, const void __user *from, unsigned long n) | ||
578 | { | ||
579 | int sz = __compiletime_object_size(to); | ||
580 | |||
581 | might_fault(); | ||
582 | |||
583 | /* | ||
584 | * While we would like to have the compiler do the checking for us | ||
585 | * even in the non-constant size case, any false positives there are | ||
586 | * a problem (especially when DEBUG_STRICT_USER_COPY_CHECKS, but even | ||
587 | * without - the [hopefully] dangerous looking nature of the warning | ||
588 | * would make people go look at the respecitive call sites over and | ||
589 | * over again just to find that there's no problem). | ||
590 | * | ||
591 | * And there are cases where it's just not realistic for the compiler | ||
592 | * to prove the count to be in range. For example when multiple call | ||
593 | * sites of a helper function - perhaps in different source files - | ||
594 | * all doing proper range checking, yet the helper function not doing | ||
595 | * so again. | ||
596 | * | ||
597 | * Therefore limit the compile time checking to the constant size | ||
598 | * case, and do only runtime checking for non-constant sizes. | ||
599 | */ | ||
600 | |||
601 | if (likely(sz < 0 || sz >= n)) | ||
602 | n = _copy_from_user(to, from, n); | ||
603 | else if(__builtin_constant_p(n)) | ||
604 | copy_from_user_overflow(); | ||
605 | else | ||
606 | __copy_from_user_overflow(sz, n); | ||
607 | |||
608 | return n; | ||
609 | } | ||
610 | |||
611 | #undef __copy_from_user_overflow | ||
612 | |||
545 | #endif /* _ASM_X86_UACCESS_H */ | 613 | #endif /* _ASM_X86_UACCESS_H */ |
546 | 614 | ||
diff --git a/arch/x86/include/asm/uaccess_32.h b/arch/x86/include/asm/uaccess_32.h index 7f760a9f1f61..c348c87cefc8 100644 --- a/arch/x86/include/asm/uaccess_32.h +++ b/arch/x86/include/asm/uaccess_32.h | |||
@@ -186,31 +186,5 @@ __copy_from_user_inatomic_nocache(void *to, const void __user *from, | |||
186 | 186 | ||
187 | unsigned long __must_check copy_to_user(void __user *to, | 187 | unsigned long __must_check copy_to_user(void __user *to, |
188 | const void *from, unsigned long n); | 188 | const void *from, unsigned long n); |
189 | unsigned long __must_check _copy_from_user(void *to, | ||
190 | const void __user *from, | ||
191 | unsigned long n); | ||
192 | |||
193 | |||
194 | extern void copy_from_user_overflow(void) | ||
195 | #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS | ||
196 | __compiletime_error("copy_from_user() buffer size is not provably correct") | ||
197 | #else | ||
198 | __compiletime_warning("copy_from_user() buffer size is not provably correct") | ||
199 | #endif | ||
200 | ; | ||
201 | |||
202 | static inline unsigned long __must_check copy_from_user(void *to, | ||
203 | const void __user *from, | ||
204 | unsigned long n) | ||
205 | { | ||
206 | int sz = __compiletime_object_size(to); | ||
207 | |||
208 | if (likely(sz == -1 || sz >= n)) | ||
209 | n = _copy_from_user(to, from, n); | ||
210 | else | ||
211 | copy_from_user_overflow(); | ||
212 | |||
213 | return n; | ||
214 | } | ||
215 | 189 | ||
216 | #endif /* _ASM_X86_UACCESS_32_H */ | 190 | #endif /* _ASM_X86_UACCESS_32_H */ |
diff --git a/arch/x86/include/asm/uaccess_64.h b/arch/x86/include/asm/uaccess_64.h index 4f7923dd0007..4df93c46852b 100644 --- a/arch/x86/include/asm/uaccess_64.h +++ b/arch/x86/include/asm/uaccess_64.h | |||
@@ -48,26 +48,8 @@ copy_user_generic(void *to, const void *from, unsigned len) | |||
48 | __must_check unsigned long | 48 | __must_check unsigned long |
49 | _copy_to_user(void __user *to, const void *from, unsigned len); | 49 | _copy_to_user(void __user *to, const void *from, unsigned len); |
50 | __must_check unsigned long | 50 | __must_check unsigned long |
51 | _copy_from_user(void *to, const void __user *from, unsigned len); | ||
52 | __must_check unsigned long | ||
53 | copy_in_user(void __user *to, const void __user *from, unsigned len); | 51 | copy_in_user(void __user *to, const void __user *from, unsigned len); |
54 | 52 | ||
55 | static inline unsigned long __must_check copy_from_user(void *to, | ||
56 | const void __user *from, | ||
57 | unsigned long n) | ||
58 | { | ||
59 | int sz = __compiletime_object_size(to); | ||
60 | |||
61 | might_fault(); | ||
62 | if (likely(sz == -1 || sz >= n)) | ||
63 | n = _copy_from_user(to, from, n); | ||
64 | #ifdef CONFIG_DEBUG_VM | ||
65 | else | ||
66 | WARN(1, "Buffer overflow detected!\n"); | ||
67 | #endif | ||
68 | return n; | ||
69 | } | ||
70 | |||
71 | static __always_inline __must_check | 53 | static __always_inline __must_check |
72 | int copy_to_user(void __user *dst, const void *src, unsigned size) | 54 | int copy_to_user(void __user *dst, const void *src, unsigned size) |
73 | { | 55 | { |
diff --git a/arch/x86/lib/usercopy_32.c b/arch/x86/lib/usercopy_32.c index 3eb18acd0e40..c408d0222917 100644 --- a/arch/x86/lib/usercopy_32.c +++ b/arch/x86/lib/usercopy_32.c | |||
@@ -679,8 +679,7 @@ EXPORT_SYMBOL(copy_to_user); | |||
679 | * If some data could not be copied, this function will pad the copied | 679 | * If some data could not be copied, this function will pad the copied |
680 | * data to the requested size using zero bytes. | 680 | * data to the requested size using zero bytes. |
681 | */ | 681 | */ |
682 | unsigned long | 682 | unsigned long _copy_from_user(void *to, const void __user *from, unsigned n) |
683 | _copy_from_user(void *to, const void __user *from, unsigned long n) | ||
684 | { | 683 | { |
685 | if (access_ok(VERIFY_READ, from, n)) | 684 | if (access_ok(VERIFY_READ, from, n)) |
686 | n = __copy_from_user(to, from, n); | 685 | n = __copy_from_user(to, from, n); |