diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/test_user_copy.c | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/lib/test_user_copy.c b/lib/test_user_copy.c index 0f86c67d87db..73ff7a628e3a 100644 --- a/lib/test_user_copy.c +++ b/lib/test_user_copy.c | |||
| @@ -58,7 +58,9 @@ static int __init test_user_copy_init(void) | |||
| 58 | usermem = (char __user *)user_addr; | 58 | usermem = (char __user *)user_addr; |
| 59 | bad_usermem = (char *)user_addr; | 59 | bad_usermem = (char *)user_addr; |
| 60 | 60 | ||
| 61 | /* Legitimate usage: none of these should fail. */ | 61 | /* |
| 62 | * Legitimate usage: none of these copies should fail. | ||
| 63 | */ | ||
| 62 | ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE), | 64 | ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE), |
| 63 | "legitimate copy_from_user failed"); | 65 | "legitimate copy_from_user failed"); |
| 64 | ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE), | 66 | ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE), |
| @@ -68,31 +70,45 @@ static int __init test_user_copy_init(void) | |||
| 68 | ret |= test(put_user(value, (unsigned long __user *)usermem), | 70 | ret |= test(put_user(value, (unsigned long __user *)usermem), |
| 69 | "legitimate put_user failed"); | 71 | "legitimate put_user failed"); |
| 70 | 72 | ||
| 71 | /* Invalid usage: none of these should succeed. */ | 73 | /* |
| 74 | * Invalid usage: none of these copies should succeed. | ||
| 75 | */ | ||
| 76 | |||
| 77 | /* Prepare kernel memory with check values. */ | ||
| 72 | memset(kmem, 0x5a, PAGE_SIZE); | 78 | memset(kmem, 0x5a, PAGE_SIZE); |
| 73 | memset(kmem + PAGE_SIZE, 0, PAGE_SIZE); | 79 | memset(kmem + PAGE_SIZE, 0, PAGE_SIZE); |
| 80 | |||
| 81 | /* Reject kernel-to-kernel copies through copy_from_user(). */ | ||
| 74 | ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE), | 82 | ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE), |
| 75 | PAGE_SIZE), | 83 | PAGE_SIZE), |
| 76 | "illegal all-kernel copy_from_user passed"); | 84 | "illegal all-kernel copy_from_user passed"); |
| 85 | |||
| 86 | /* Destination half of buffer should have been zeroed. */ | ||
| 77 | ret |= test(memcmp(kmem + PAGE_SIZE, kmem, PAGE_SIZE), | 87 | ret |= test(memcmp(kmem + PAGE_SIZE, kmem, PAGE_SIZE), |
| 78 | "zeroing failure for illegal all-kernel copy_from_user"); | 88 | "zeroing failure for illegal all-kernel copy_from_user"); |
| 79 | memset(bad_usermem, 0x5A, PAGE_SIZE); | 89 | |
| 90 | #if 0 | ||
| 91 | /* | ||
| 92 | * When running with SMAP/PAN/etc, this will Oops the kernel | ||
| 93 | * due to the zeroing of userspace memory on failure. This needs | ||
| 94 | * to be tested in LKDTM instead, since this test module does not | ||
| 95 | * expect to explode. | ||
| 96 | */ | ||
| 80 | ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem, | 97 | ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem, |
| 81 | PAGE_SIZE), | 98 | PAGE_SIZE), |
| 82 | "illegal reversed copy_from_user passed"); | 99 | "illegal reversed copy_from_user passed"); |
| 83 | ret |= test(memcmp(kmem + PAGE_SIZE, bad_usermem, PAGE_SIZE), | 100 | #endif |
| 84 | "zeroing failure for illegal reversed copy_from_user"); | ||
| 85 | ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE, | 101 | ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE, |
| 86 | PAGE_SIZE), | 102 | PAGE_SIZE), |
| 87 | "illegal all-kernel copy_to_user passed"); | 103 | "illegal all-kernel copy_to_user passed"); |
| 88 | ret |= test(!copy_to_user((char __user *)kmem, bad_usermem, | 104 | ret |= test(!copy_to_user((char __user *)kmem, bad_usermem, |
| 89 | PAGE_SIZE), | 105 | PAGE_SIZE), |
| 90 | "illegal reversed copy_to_user passed"); | 106 | "illegal reversed copy_to_user passed"); |
| 91 | memset(kmem, 0x5a, PAGE_SIZE); | 107 | |
| 108 | value = 0x5a; | ||
| 92 | ret |= test(!get_user(value, (unsigned long __user *)kmem), | 109 | ret |= test(!get_user(value, (unsigned long __user *)kmem), |
| 93 | "illegal get_user passed"); | 110 | "illegal get_user passed"); |
| 94 | ret |= test(memcmp(kmem + PAGE_SIZE, kmem, sizeof(value)), | 111 | ret |= test(value != 0, "zeroing failure for illegal get_user"); |
| 95 | "zeroing failure for illegal get_user"); | ||
| 96 | ret |= test(!put_user(value, (unsigned long __user *)kmem), | 112 | ret |= test(!put_user(value, (unsigned long __user *)kmem), |
| 97 | "illegal put_user passed"); | 113 | "illegal put_user passed"); |
| 98 | 114 | ||
