2020-10-14 02:55:06 +03:00
// SPDX-License-Identifier: GPL-2.0-only
/*
*
* Copyright ( c ) 2014 Samsung Electronics Co . , Ltd .
* Author : Andrey Ryabinin < a . ryabinin @ samsung . com >
*/
# define pr_fmt(fmt) "kasan test: %s " fmt, __func__
# include <linux/mman.h>
# include <linux/module.h>
# include <linux/printk.h>
# include <linux/slab.h>
# include <linux/uaccess.h>
2022-09-06 01:18:36 +03:00
# include "kasan.h"
2020-10-14 02:55:06 +03:00
static noinline void __init copy_user_test ( void )
{
char * kmem ;
char __user * usermem ;
2021-09-03 00:57:50 +03:00
size_t size = 128 - KASAN_GRANULE_SIZE ;
2021-04-09 23:27:41 +03:00
int __maybe_unused unused ;
2020-10-14 02:55:06 +03:00
kmem = kmalloc ( size , GFP_KERNEL ) ;
if ( ! kmem )
return ;
usermem = ( char __user * ) vm_mmap ( NULL , 0 , PAGE_SIZE ,
PROT_READ | PROT_WRITE | PROT_EXEC ,
MAP_ANONYMOUS | MAP_PRIVATE , 0 ) ;
if ( IS_ERR ( usermem ) ) {
pr_err ( " Failed to allocate user memory \n " ) ;
kfree ( kmem ) ;
return ;
}
2021-11-05 23:36:12 +03:00
OPTIMIZER_HIDE_VAR ( size ) ;
2020-10-14 02:55:06 +03:00
pr_info ( " out-of-bounds in copy_from_user() \n " ) ;
2021-09-03 00:57:50 +03:00
unused = copy_from_user ( kmem , usermem , size + 1 ) ;
2020-10-14 02:55:06 +03:00
pr_info ( " out-of-bounds in copy_to_user() \n " ) ;
2021-09-03 00:57:50 +03:00
unused = copy_to_user ( usermem , kmem , size + 1 ) ;
2020-10-14 02:55:06 +03:00
pr_info ( " out-of-bounds in __copy_from_user() \n " ) ;
2021-09-03 00:57:50 +03:00
unused = __copy_from_user ( kmem , usermem , size + 1 ) ;
2020-10-14 02:55:06 +03:00
pr_info ( " out-of-bounds in __copy_to_user() \n " ) ;
2021-09-03 00:57:50 +03:00
unused = __copy_to_user ( usermem , kmem , size + 1 ) ;
2020-10-14 02:55:06 +03:00
pr_info ( " out-of-bounds in __copy_from_user_inatomic() \n " ) ;
2021-09-03 00:57:50 +03:00
unused = __copy_from_user_inatomic ( kmem , usermem , size + 1 ) ;
2020-10-14 02:55:06 +03:00
pr_info ( " out-of-bounds in __copy_to_user_inatomic() \n " ) ;
2021-09-03 00:57:50 +03:00
unused = __copy_to_user_inatomic ( usermem , kmem , size + 1 ) ;
2020-10-14 02:55:06 +03:00
pr_info ( " out-of-bounds in strncpy_from_user() \n " ) ;
2021-09-03 00:57:50 +03:00
unused = strncpy_from_user ( kmem , usermem , size + 1 ) ;
2020-10-14 02:55:06 +03:00
vm_munmap ( ( unsigned long ) usermem , PAGE_SIZE ) ;
kfree ( kmem ) ;
}
static int __init test_kasan_module_init ( void )
{
/*
2021-02-24 23:05:13 +03:00
* Temporarily enable multi - shot mode . Otherwise , KASAN would only
* report the first detected bug and panic the kernel if panic_on_warn
* is enabled .
2020-10-14 02:55:06 +03:00
*/
bool multishot = kasan_save_enable_multi_shot ( ) ;
copy_user_test ( ) ;
kasan_restore_multi_shot ( multishot ) ;
return - EAGAIN ;
}
module_init ( test_kasan_module_init ) ;
MODULE_LICENSE ( " GPL " ) ;