Skip to content

Commit ae795fe

Browse files
committed
Merge branch 'x86-asm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 user access changes from Ingo Molnar: "This tree contains two copy_[from/to]_user() build time checking changes/enhancements from Jan Beulich. The desired outcome is to get better compiler warnings with CONFIG_DEBUG_STRICT_USER_COPY_CHECKS=y, to keep people from introducing bugs such as overflows and information leaks" * 'x86-asm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86: Unify copy_to_user() and add size checking to it x86: Unify copy_from_user() size checking
2 parents b7ab6e3 + 7a3d9b0 commit ae795fe

File tree

4 files changed

+101
-62
lines changed

4 files changed

+101
-62
lines changed

arch/x86/include/asm/uaccess.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,5 +542,103 @@ extern struct movsl_mask {
542542
# include <asm/uaccess_64.h>
543543
#endif
544544

545+
unsigned long __must_check _copy_from_user(void *to, const void __user *from,
546+
unsigned n);
547+
unsigned long __must_check _copy_to_user(void __user *to, const void *from,
548+
unsigned n);
549+
550+
#ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
551+
# define copy_user_diag __compiletime_error
552+
#else
553+
# define copy_user_diag __compiletime_warning
554+
#endif
555+
556+
extern void copy_user_diag("copy_from_user() buffer size is too small")
557+
copy_from_user_overflow(void);
558+
extern void copy_user_diag("copy_to_user() buffer size is too small")
559+
copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
560+
561+
#undef copy_user_diag
562+
563+
#ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
564+
565+
extern void
566+
__compiletime_warning("copy_from_user() buffer size is not provably correct")
567+
__copy_from_user_overflow(void) __asm__("copy_from_user_overflow");
568+
#define __copy_from_user_overflow(size, count) __copy_from_user_overflow()
569+
570+
extern void
571+
__compiletime_warning("copy_to_user() buffer size is not provably correct")
572+
__copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
573+
#define __copy_to_user_overflow(size, count) __copy_to_user_overflow()
574+
575+
#else
576+
577+
static inline void
578+
__copy_from_user_overflow(int size, unsigned long count)
579+
{
580+
WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
581+
}
582+
583+
#define __copy_to_user_overflow __copy_from_user_overflow
584+
585+
#endif
586+
587+
static inline unsigned long __must_check
588+
copy_from_user(void *to, const void __user *from, unsigned long n)
589+
{
590+
int sz = __compiletime_object_size(to);
591+
592+
might_fault();
593+
594+
/*
595+
* While we would like to have the compiler do the checking for us
596+
* even in the non-constant size case, any false positives there are
597+
* a problem (especially when DEBUG_STRICT_USER_COPY_CHECKS, but even
598+
* without - the [hopefully] dangerous looking nature of the warning
599+
* would make people go look at the respecitive call sites over and
600+
* over again just to find that there's no problem).
601+
*
602+
* And there are cases where it's just not realistic for the compiler
603+
* to prove the count to be in range. For example when multiple call
604+
* sites of a helper function - perhaps in different source files -
605+
* all doing proper range checking, yet the helper function not doing
606+
* so again.
607+
*
608+
* Therefore limit the compile time checking to the constant size
609+
* case, and do only runtime checking for non-constant sizes.
610+
*/
611+
612+
if (likely(sz < 0 || sz >= n))
613+
n = _copy_from_user(to, from, n);
614+
else if(__builtin_constant_p(n))
615+
copy_from_user_overflow();
616+
else
617+
__copy_from_user_overflow(sz, n);
618+
619+
return n;
620+
}
621+
622+
static inline unsigned long __must_check
623+
copy_to_user(void __user *to, const void *from, unsigned long n)
624+
{
625+
int sz = __compiletime_object_size(from);
626+
627+
might_fault();
628+
629+
/* See the comment in copy_from_user() above. */
630+
if (likely(sz < 0 || sz >= n))
631+
n = _copy_to_user(to, from, n);
632+
else if(__builtin_constant_p(n))
633+
copy_to_user_overflow();
634+
else
635+
__copy_to_user_overflow(sz, n);
636+
637+
return n;
638+
}
639+
640+
#undef __copy_from_user_overflow
641+
#undef __copy_to_user_overflow
642+
545643
#endif /* _ASM_X86_UACCESS_H */
546644

arch/x86/include/asm/uaccess_32.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -184,33 +184,4 @@ __copy_from_user_inatomic_nocache(void *to, const void __user *from,
184184
return __copy_from_user_ll_nocache_nozero(to, from, n);
185185
}
186186

187-
unsigned long __must_check copy_to_user(void __user *to,
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-
216187
#endif /* _ASM_X86_UACCESS_32_H */

arch/x86/include/asm/uaccess_64.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,9 @@ copy_user_generic(void *to, const void *from, unsigned len)
4545
return ret;
4646
}
4747

48-
__must_check unsigned long
49-
_copy_to_user(void __user *to, const void *from, unsigned len);
50-
__must_check unsigned long
51-
_copy_from_user(void *to, const void __user *from, unsigned len);
5248
__must_check unsigned long
5349
copy_in_user(void __user *to, const void __user *from, unsigned len);
5450

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
72-
int copy_to_user(void __user *dst, const void *src, unsigned size)
73-
{
74-
might_fault();
75-
76-
return _copy_to_user(dst, src, size);
77-
}
78-
7951
static __always_inline __must_check
8052
int __copy_from_user(void *dst, const void __user *src, unsigned size)
8153
{

arch/x86/lib/usercopy_32.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -654,14 +654,13 @@ EXPORT_SYMBOL(__copy_from_user_ll_nocache_nozero);
654654
* Returns number of bytes that could not be copied.
655655
* On success, this will be zero.
656656
*/
657-
unsigned long
658-
copy_to_user(void __user *to, const void *from, unsigned long n)
657+
unsigned long _copy_to_user(void __user *to, const void *from, unsigned n)
659658
{
660659
if (access_ok(VERIFY_WRITE, to, n))
661660
n = __copy_to_user(to, from, n);
662661
return n;
663662
}
664-
EXPORT_SYMBOL(copy_to_user);
663+
EXPORT_SYMBOL(_copy_to_user);
665664

666665
/**
667666
* copy_from_user: - Copy a block of data from user space.
@@ -679,8 +678,7 @@ EXPORT_SYMBOL(copy_to_user);
679678
* If some data could not be copied, this function will pad the copied
680679
* data to the requested size using zero bytes.
681680
*/
682-
unsigned long
683-
_copy_from_user(void *to, const void __user *from, unsigned long n)
681+
unsigned long _copy_from_user(void *to, const void __user *from, unsigned n)
684682
{
685683
if (access_ok(VERIFY_READ, from, n))
686684
n = __copy_from_user(to, from, n);

0 commit comments

Comments
 (0)