This improves the usercopy tests:

- check zeroing on failed copy_from_user()/get_user() (caught bug on ARM)
 - adjust tests for SMAP/PAN (can't zero userspace memory on failure)
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1
 Comment: Kees Cook <kees@outflux.net>
 
 iQIcBAABCgAGBQJYrJzrAAoJEIly9N/cbcAmS+UP/iAQ+2HTzT4RA+JpV+MctGlk
 4mB6Qq8FZZTI2Xa1KrZY/k7yqq3slSfGOlmJrA+hfF915TpOVQVYwMh7kRWz3Vk5
 3Nu+x6h5sgN1L0mi6KhSZR0vk/Z6uUqKceiIFfmClrKYRrEn60/6RhyWqQWtNmXs
 +boOxZl44ZAuBPs7UHW12FyDusjFQsSjVNSBe2MXji8AmOJbUGGcWE5gAqNx6g/k
 FcXhUd+1zL5DamCN8fCxYckl2Q7hsLSwv00Ab+SP+8vScqXuznLkEelFcICB14BE
 sSr2fwd1NkuSNvS6oa89DbxSdBrs734nsHjhxymT7S5ZJnqi3aukF91yq0K5vbfe
 NLH7ja6EQj43ABd5Jzou/kJZkm4AidpQRrjtFsF7R8Kiskx622jHT2Pvmz07W34o
 RA+C+1Mrg6KL1xFBn38kzQCtPjX0SFHw+T2CgADuEc8EWf5zG/T9K870dZyJMm9p
 4GbTzbR+D1C9bgP24qxCxi+Fmd0gFXTrmzaiG/d348d8BS1iaUnRoXPVH1+9T2bY
 0kpROj/Cu4+Y24SmFbRDAGazos7oKPICQ8apCh8UN/F7Lj3jIT2ENdtxRYgPZ3DB
 Exi2AwSd4AvnSxfxkih6+oRlqGXYRMc1CJCHYIRAYphwgP/LbyMCA3YpevmwZCb9
 /BHZruZJUoBzQ5PKqpkM
 =F2H1
 -----END PGP SIGNATURE-----

Merge tag 'usercopy-v4.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux

Pull usercopy test updates from Kees Cook:
 "This improves the usercopy tests:

   - check zeroing on failed copy_from_user()/get_user() (caught bug on
     ARM)

   - adjust tests for SMAP/PAN (can't zero userspace memory on failure)"

* tag 'usercopy-v4.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux:
  usercopy: Add tests for all get_user() sizes
  usercopy: Adjust tests to deal with SMAP/PAN
  usercopy: add testcases to check zeroing on failure
This commit is contained in:
Linus Torvalds 2017-02-21 17:53:23 -08:00
commit 4a0853bf88
1 changed files with 102 additions and 13 deletions

View File

@ -25,6 +25,23 @@
#include <linux/uaccess.h>
#include <linux/vmalloc.h>
/*
* Several 32-bit architectures support 64-bit {get,put}_user() calls.
* As there doesn't appear to be anything that can safely determine
* their capability at compile-time, we just have to opt-out certain archs.
*/
#if BITS_PER_LONG == 64 || (!defined(CONFIG_AVR32) && \
!defined(CONFIG_BLACKFIN) && \
!defined(CONFIG_M32R) && \
!defined(CONFIG_M68K) && \
!defined(CONFIG_MICROBLAZE) && \
!defined(CONFIG_MN10300) && \
!defined(CONFIG_NIOS2) && \
!defined(CONFIG_PPC32) && \
!defined(CONFIG_SUPERH))
# define TEST_U64
#endif
#define test(condition, msg) \
({ \
int cond = (condition); \
@ -40,7 +57,12 @@ static int __init test_user_copy_init(void)
char __user *usermem;
char *bad_usermem;
unsigned long user_addr;
unsigned long value = 0x5A;
u8 val_u8;
u16 val_u16;
u32 val_u32;
#ifdef TEST_U64
u64 val_u64;
#endif
kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
if (!kmem)
@ -58,33 +80,100 @@ static int __init test_user_copy_init(void)
usermem = (char __user *)user_addr;
bad_usermem = (char *)user_addr;
/* Legitimate usage: none of these should fail. */
ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE),
"legitimate copy_from_user failed");
/*
* Legitimate usage: none of these copies should fail.
*/
memset(kmem, 0x3a, PAGE_SIZE * 2);
ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE),
"legitimate copy_to_user failed");
ret |= test(get_user(value, (unsigned long __user *)usermem),
"legitimate get_user failed");
ret |= test(put_user(value, (unsigned long __user *)usermem),
"legitimate put_user failed");
memset(kmem, 0x0, PAGE_SIZE);
ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE),
"legitimate copy_from_user failed");
ret |= test(memcmp(kmem, kmem + PAGE_SIZE, PAGE_SIZE),
"legitimate usercopy failed to copy data");
/* Invalid usage: none of these should succeed. */
#define test_legit(size, check) \
do { \
val_##size = check; \
ret |= test(put_user(val_##size, (size __user *)usermem), \
"legitimate put_user (" #size ") failed"); \
val_##size = 0; \
ret |= test(get_user(val_##size, (size __user *)usermem), \
"legitimate get_user (" #size ") failed"); \
ret |= test(val_##size != check, \
"legitimate get_user (" #size ") failed to do copy"); \
if (val_##size != check) { \
pr_info("0x%llx != 0x%llx\n", \
(unsigned long long)val_##size, \
(unsigned long long)check); \
} \
} while (0)
test_legit(u8, 0x5a);
test_legit(u16, 0x5a5b);
test_legit(u32, 0x5a5b5c5d);
#ifdef TEST_U64
test_legit(u64, 0x5a5b5c5d6a6b6c6d);
#endif
#undef test_legit
/*
* Invalid usage: none of these copies should succeed.
*/
/* Prepare kernel memory with check values. */
memset(kmem, 0x5a, PAGE_SIZE);
memset(kmem + PAGE_SIZE, 0, PAGE_SIZE);
/* Reject kernel-to-kernel copies through copy_from_user(). */
ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE),
PAGE_SIZE),
"illegal all-kernel copy_from_user passed");
/* Destination half of buffer should have been zeroed. */
ret |= test(memcmp(kmem + PAGE_SIZE, kmem, PAGE_SIZE),
"zeroing failure for illegal all-kernel copy_from_user");
#if 0
/*
* When running with SMAP/PAN/etc, this will Oops the kernel
* due to the zeroing of userspace memory on failure. This needs
* to be tested in LKDTM instead, since this test module does not
* expect to explode.
*/
ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem,
PAGE_SIZE),
"illegal reversed copy_from_user passed");
#endif
ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE,
PAGE_SIZE),
"illegal all-kernel copy_to_user passed");
ret |= test(!copy_to_user((char __user *)kmem, bad_usermem,
PAGE_SIZE),
"illegal reversed copy_to_user passed");
ret |= test(!get_user(value, (unsigned long __user *)kmem),
"illegal get_user passed");
ret |= test(!put_user(value, (unsigned long __user *)kmem),
"illegal put_user passed");
#define test_illegal(size, check) \
do { \
val_##size = (check); \
ret |= test(!get_user(val_##size, (size __user *)kmem), \
"illegal get_user (" #size ") passed"); \
ret |= test(val_##size != (size)0, \
"zeroing failure for illegal get_user (" #size ")"); \
if (val_##size != (size)0) { \
pr_info("0x%llx != 0\n", \
(unsigned long long)val_##size); \
} \
ret |= test(!put_user(val_##size, (size __user *)kmem), \
"illegal put_user (" #size ") passed"); \
} while (0)
test_illegal(u8, 0x5a);
test_illegal(u16, 0x5a5b);
test_illegal(u32, 0x5a5b5c5d);
#ifdef TEST_U64
test_illegal(u64, 0x5a5b5c5d6a6b6c6d);
#endif
#undef test_illegal
vm_munmap(user_addr, PAGE_SIZE * 2);
kfree(kmem);