summaryrefslogtreecommitdiffstats
path: root/lib/test_user_copy.c
diff options
context:
space:
mode:
authorHoeun Ryu <hoeun.ryu@gmail.com>2017-02-12 15:13:33 +0900
committerKees Cook <keescook@chromium.org>2017-02-16 16:34:59 -0800
commit4fbfeb8bd684d564bddeff1e3723d3d9f99aa5de (patch)
treeacd71c212dec8ad0f3117b33c5c37ff9e68b1183 /lib/test_user_copy.c
parent0c744ea4f77d72b3dcebb7a8f2684633ec79be88 (diff)
downloadlinux-4fbfeb8bd684d564bddeff1e3723d3d9f99aa5de.tar.gz
linux-4fbfeb8bd684d564bddeff1e3723d3d9f99aa5de.tar.bz2
linux-4fbfeb8bd684d564bddeff1e3723d3d9f99aa5de.zip
usercopy: add testcases to check zeroing on failure
During usercopy the destination buffer will be zeroed if copy_from_user() or get_user() fails. This patch adds testcases for it. The destination buffer is set with non-zero value before illegal copy_from_user() or get_user() is executed and the buffer is compared to zero after usercopy is done. Signed-off-by: Hoeun Ryu <hoeun.ryu@gmail.com> [kees: clarified commit log, dropped second kmalloc] Signed-off-by: Kees Cook <keescook@chromium.org>
Diffstat (limited to 'lib/test_user_copy.c')
-rw-r--r--lib/test_user_copy.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/test_user_copy.c b/lib/test_user_copy.c
index 0ecef3e4690e..0f86c67d87db 100644
--- a/lib/test_user_copy.c
+++ b/lib/test_user_copy.c
@@ -69,20 +69,30 @@ static int __init test_user_copy_init(void)
"legitimate put_user failed");
/* Invalid usage: none of these should succeed. */
+ memset(kmem, 0x5a, PAGE_SIZE);
+ memset(kmem + PAGE_SIZE, 0, PAGE_SIZE);
ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE),
PAGE_SIZE),
"illegal all-kernel copy_from_user passed");
+ ret |= test(memcmp(kmem + PAGE_SIZE, kmem, PAGE_SIZE),
+ "zeroing failure for illegal all-kernel copy_from_user");
+ memset(bad_usermem, 0x5A, PAGE_SIZE);
ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem,
PAGE_SIZE),
"illegal reversed copy_from_user passed");
+ ret |= test(memcmp(kmem + PAGE_SIZE, bad_usermem, PAGE_SIZE),
+ "zeroing failure for illegal reversed copy_from_user");
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");
+ memset(kmem, 0x5a, PAGE_SIZE);
ret |= test(!get_user(value, (unsigned long __user *)kmem),
"illegal get_user passed");
+ ret |= test(memcmp(kmem + PAGE_SIZE, kmem, sizeof(value)),
+ "zeroing failure for illegal get_user");
ret |= test(!put_user(value, (unsigned long __user *)kmem),
"illegal put_user passed");