aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAndrey Ryabinin <aryabinin@virtuozzo.com>2016-05-20 19:59:34 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-05-20 20:58:30 -0400
commiteae08dcab80c695c16c9f1f7dcd5b8ed52bfc88b (patch)
treef3900e677e70d10fd46140e811baa7e748148e9b /lib
parent1771c6e1a567ea0ba2cccc0a4ffe68a1419fd8ef (diff)
kasan/tests: add tests for user memory access functions
Add some tests for the newly-added user memory access API. Link: http://lkml.kernel.org/r/1462538722-1574-1-git-send-email-aryabinin@virtuozzo.com Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Alexander Potapenko <glider@google.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/test_kasan.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/test_kasan.c b/lib/test_kasan.c
index 48e5a0be655c..5e51872b3fc1 100644
--- a/lib/test_kasan.c
+++ b/lib/test_kasan.c
@@ -12,9 +12,12 @@
12#define pr_fmt(fmt) "kasan test: %s " fmt, __func__ 12#define pr_fmt(fmt) "kasan test: %s " fmt, __func__
13 13
14#include <linux/kernel.h> 14#include <linux/kernel.h>
15#include <linux/mman.h>
16#include <linux/mm.h>
15#include <linux/printk.h> 17#include <linux/printk.h>
16#include <linux/slab.h> 18#include <linux/slab.h>
17#include <linux/string.h> 19#include <linux/string.h>
20#include <linux/uaccess.h>
18#include <linux/module.h> 21#include <linux/module.h>
19 22
20static noinline void __init kmalloc_oob_right(void) 23static noinline void __init kmalloc_oob_right(void)
@@ -363,6 +366,51 @@ static noinline void __init ksize_unpoisons_memory(void)
363 kfree(ptr); 366 kfree(ptr);
364} 367}
365 368
369static noinline void __init copy_user_test(void)
370{
371 char *kmem;
372 char __user *usermem;
373 size_t size = 10;
374 int unused;
375
376 kmem = kmalloc(size, GFP_KERNEL);
377 if (!kmem)
378 return;
379
380 usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
381 PROT_READ | PROT_WRITE | PROT_EXEC,
382 MAP_ANONYMOUS | MAP_PRIVATE, 0);
383 if (IS_ERR(usermem)) {
384 pr_err("Failed to allocate user memory\n");
385 kfree(kmem);
386 return;
387 }
388
389 pr_info("out-of-bounds in copy_from_user()\n");
390 unused = copy_from_user(kmem, usermem, size + 1);
391
392 pr_info("out-of-bounds in copy_to_user()\n");
393 unused = copy_to_user(usermem, kmem, size + 1);
394
395 pr_info("out-of-bounds in __copy_from_user()\n");
396 unused = __copy_from_user(kmem, usermem, size + 1);
397
398 pr_info("out-of-bounds in __copy_to_user()\n");
399 unused = __copy_to_user(usermem, kmem, size + 1);
400
401 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
402 unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
403
404 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
405 unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
406
407 pr_info("out-of-bounds in strncpy_from_user()\n");
408 unused = strncpy_from_user(kmem, usermem, size + 1);
409
410 vm_munmap((unsigned long)usermem, PAGE_SIZE);
411 kfree(kmem);
412}
413
366static int __init kmalloc_tests_init(void) 414static int __init kmalloc_tests_init(void)
367{ 415{
368 kmalloc_oob_right(); 416 kmalloc_oob_right();
@@ -387,6 +435,7 @@ static int __init kmalloc_tests_init(void)
387 kasan_stack_oob(); 435 kasan_stack_oob();
388 kasan_global_oob(); 436 kasan_global_oob();
389 ksize_unpoisons_memory(); 437 ksize_unpoisons_memory();
438 copy_user_test();
390 return -EAGAIN; 439 return -EAGAIN;
391} 440}
392 441