2019-08-25 12:49:18 +03:00
// SPDX-License-Identifier: GPL-2.0
2008-02-08 15:22:08 +03:00
/*
* Copyright ( C ) 2007 Jeff Dike ( jdike @ { addtoit , linux . intel } . com )
*/
2005-09-17 06:27:50 +04:00
# include <stdio.h>
# include <stddef.h>
2008-02-08 15:22:08 +03:00
# include <stdlib.h>
2005-09-17 06:27:50 +04:00
# include <unistd.h>
# include <errno.h>
# include <fcntl.h>
2008-02-08 15:22:08 +03:00
# include <string.h>
2010-06-30 02:05:40 +04:00
# include <sys/stat.h>
2005-09-17 06:27:50 +04:00
# include <sys/mman.h>
2013-11-11 22:03:06 +04:00
# include <sys/vfs.h>
# include <linux/magic.h>
2012-10-08 06:27:32 +04:00
# include <init.h>
# include <os.h>
2005-09-17 06:27:50 +04:00
UML: add support for KASAN under x86_64
Make KASAN run on User Mode Linux on x86_64.
The UML-specific KASAN initializer uses mmap to map the ~16TB of shadow
memory to the location defined by KASAN_SHADOW_OFFSET. kasan_init()
utilizes constructors to initialize KASAN before main().
The location of the KASAN shadow memory, starting at
KASAN_SHADOW_OFFSET, can be configured using the KASAN_SHADOW_OFFSET
option. The default location of this offset is 0x100000000000, which
keeps it out-of-the-way even on UML setups with more "physical" memory.
For low-memory setups, 0x7fff8000 can be used instead, which fits in an
immediate and is therefore faster, as suggested by Dmitry Vyukov. There
is usually enough free space at this location; however, it is a config
option so that it can be easily changed if needed.
Note that, unlike KASAN on other architectures, vmalloc allocations
still use the shadow memory allocated upfront, rather than allocating
and free-ing it per-vmalloc allocation.
If another architecture chooses to go down the same path, we should
replace the checks for CONFIG_UML with something more generic, such
as:
- A CONFIG_KASAN_NO_SHADOW_ALLOC option, which architectures could set
- or, a way of having architecture-specific versions of these vmalloc
and module shadow memory allocation options.
Also note that, while UML supports both KASAN in inline mode
(CONFIG_KASAN_INLINE) and static linking (CONFIG_STATIC_LINK), it does
not support both at the same time.
Signed-off-by: Patricia Alfonso <trishalfonso@google.com>
Co-developed-by: Vincent Whitchurch <vincent.whitchurch@axis.com>
Signed-off-by: Vincent Whitchurch <vincent.whitchurch@axis.com>
Signed-off-by: David Gow <davidgow@google.com>
Reviewed-by: Johannes Berg <johannes@sipsolutions.net>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
Signed-off-by: Richard Weinberger <richard@nod.at>
2022-07-01 12:16:20 +03:00
/*
* kasan_map_memory - maps memory from @ start with a size of @ len .
* The allocated memory is filled with zeroes upon success .
* @ start : the start address of the memory to be mapped
* @ len : the length of the memory to be mapped
*
* This function is used to map shadow memory for KASAN in uml
*/
void kasan_map_memory ( void * start , size_t len )
{
if ( mmap ( start ,
len ,
PROT_READ | PROT_WRITE ,
MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE ,
- 1 ,
0 ) = = MAP_FAILED ) {
os_info ( " Couldn't allocate shadow memory: %s \n . " ,
strerror ( errno ) ) ;
exit ( 1 ) ;
}
}
2013-11-11 22:03:06 +04:00
/* Set by make_tempfile() during early boot. */
2005-09-17 06:27:50 +04:00
static char * tempdir = NULL ;
2013-11-11 22:03:06 +04:00
/* Check if dir is on tmpfs. Return 0 if yes, -1 if no or error. */
static int __init check_tmpfs ( const char * dir )
2005-09-17 06:27:50 +04:00
{
2013-11-11 22:03:06 +04:00
struct statfs st ;
2005-09-17 06:27:50 +04:00
2017-05-17 20:17:14 +03:00
os_info ( " Checking if %s is on tmpfs... " , dir ) ;
2013-11-11 22:03:06 +04:00
if ( statfs ( dir , & st ) < 0 ) {
2017-05-17 20:17:14 +03:00
os_info ( " %s \n " , strerror ( errno ) ) ;
2013-11-11 22:03:06 +04:00
} else if ( st . f_type ! = TMPFS_MAGIC ) {
2017-05-17 20:17:14 +03:00
os_info ( " no \n " ) ;
2013-11-11 22:03:06 +04:00
} else {
2017-05-17 20:17:14 +03:00
os_info ( " OK \n " ) ;
2013-11-11 22:03:06 +04:00
return 0 ;
2006-04-19 09:21:43 +04:00
}
2013-11-11 22:03:06 +04:00
return - 1 ;
2013-07-09 00:19:49 +04:00
}
/*
2013-11-11 22:03:06 +04:00
* Choose the tempdir to use . We want something on tmpfs so that our memory is
* not subject to the host ' s vm . dirty_ratio . If a tempdir is specified in the
* environment , we use that even if it ' s not on tmpfs , but we warn the user .
* Otherwise , we try common tmpfs locations , and if no tmpfs directory is found
* then we fall back to / tmp .
2013-07-09 00:19:49 +04:00
*/
2013-11-11 22:03:06 +04:00
static char * __init choose_tempdir ( void )
2013-07-09 00:19:49 +04:00
{
2013-11-11 22:03:06 +04:00
static const char * const vars [ ] = {
" TMPDIR " ,
" TMP " ,
" TEMP " ,
NULL
} ;
static const char fallback_dir [ ] = " /tmp " ;
static const char * const tmpfs_dirs [ ] = {
" /dev/shm " ,
fallback_dir ,
NULL
} ;
2013-07-09 00:19:49 +04:00
int i ;
2013-11-11 22:03:06 +04:00
const char * dir ;
2017-05-17 20:17:14 +03:00
os_info ( " Checking environment variables for a tempdir... " ) ;
2013-11-11 22:03:06 +04:00
for ( i = 0 ; vars [ i ] ; i + + ) {
dir = getenv ( vars [ i ] ) ;
if ( ( dir ! = NULL ) & & ( * dir ! = ' \0 ' ) ) {
2017-05-17 20:17:14 +03:00
os_info ( " %s \n " , dir ) ;
2013-11-11 22:03:06 +04:00
if ( check_tmpfs ( dir ) > = 0 )
goto done ;
else
goto warn ;
2013-07-09 00:19:49 +04:00
}
}
2017-05-17 20:17:14 +03:00
os_info ( " none found \n " ) ;
2013-07-09 00:19:49 +04:00
2013-11-11 22:03:06 +04:00
for ( i = 0 ; tmpfs_dirs [ i ] ; i + + ) {
dir = tmpfs_dirs [ i ] ;
if ( check_tmpfs ( dir ) > = 0 )
goto done ;
2013-07-09 00:19:49 +04:00
}
2013-11-11 22:03:06 +04:00
dir = fallback_dir ;
warn :
2017-05-17 20:19:31 +03:00
os_warn ( " Warning: tempdir %s is not on tmpfs \n " , dir ) ;
2013-11-11 22:03:06 +04:00
done :
/* Make a copy since getenv results may not remain valid forever. */
return strdup ( dir ) ;
2006-04-19 09:21:43 +04:00
}
2008-02-08 15:22:08 +03:00
/*
2013-11-11 22:03:06 +04:00
* Create an unlinked tempfile in a suitable tempdir . template must be the
* basename part of the template with a leading ' / ' .
2006-04-19 09:21:43 +04:00
*/
2013-11-11 22:03:06 +04:00
static int __init make_tempfile ( const char * template )
2006-04-19 09:21:43 +04:00
{
2013-11-11 22:03:06 +04:00
char * tempname ;
2013-07-09 00:19:49 +04:00
int fd ;
2006-04-19 09:21:43 +04:00
2013-11-11 22:03:06 +04:00
if ( tempdir = = NULL ) {
tempdir = choose_tempdir ( ) ;
if ( tempdir = = NULL ) {
2017-05-17 20:19:31 +03:00
os_warn ( " Failed to choose tempdir: %s \n " ,
2013-11-11 22:03:06 +04:00
strerror ( errno ) ) ;
return - 1 ;
2013-07-09 00:19:49 +04:00
}
2006-04-19 09:21:43 +04:00
}
2015-12-23 00:15:10 +03:00
# ifdef O_TMPFILE
fd = open ( tempdir , O_CLOEXEC | O_RDWR | O_EXCL | O_TMPFILE , 0700 ) ;
/*
* If the running system does not support O_TMPFILE flag then retry
* without it .
*/
if ( fd ! = - 1 | | ( errno ! = EINVAL & & errno ! = EISDIR & &
errno ! = EOPNOTSUPP ) )
return fd ;
# endif
2013-11-11 22:03:06 +04:00
tempname = malloc ( strlen ( tempdir ) + strlen ( template ) + 1 ) ;
2008-02-08 15:22:09 +03:00
if ( tempname = = NULL )
return - 1 ;
2006-04-11 09:53:39 +04:00
2013-11-11 22:03:06 +04:00
strcpy ( tempname , tempdir ) ;
strcat ( tempname , template ) ;
2005-09-17 06:27:50 +04:00
fd = mkstemp ( tempname ) ;
2008-02-08 15:22:08 +03:00
if ( fd < 0 ) {
2017-05-17 20:19:31 +03:00
os_warn ( " open - cannot create %s: %s \n " , tempname ,
2005-09-17 06:27:50 +04:00
strerror ( errno ) ) ;
2006-04-11 09:53:39 +04:00
goto out ;
2005-09-17 06:27:50 +04:00
}
2013-11-11 22:03:06 +04:00
if ( unlink ( tempname ) < 0 ) {
2005-09-17 06:27:50 +04:00
perror ( " unlink " ) ;
2011-07-26 04:12:52 +04:00
goto close ;
2005-09-17 06:27:50 +04:00
}
2013-11-11 22:03:06 +04:00
free ( tempname ) ;
2007-02-10 12:44:21 +03:00
return fd ;
2011-07-26 04:12:52 +04:00
close :
close ( fd ) ;
2006-04-11 09:53:39 +04:00
out :
free ( tempname ) ;
return - 1 ;
2005-09-17 06:27:50 +04:00
}
2013-11-11 22:03:06 +04:00
# define TEMPNAME_TEMPLATE " / vm_file-XXXXXX"
2005-09-17 06:27:50 +04:00
2008-02-08 15:22:08 +03:00
static int __init create_tmp_file ( unsigned long long len )
2005-09-17 06:27:50 +04:00
{
int fd , err ;
char zero ;
2013-11-11 22:03:06 +04:00
fd = make_tempfile ( TEMPNAME_TEMPLATE ) ;
2008-02-08 15:22:08 +03:00
if ( fd < 0 )
2005-09-17 06:27:50 +04:00
exit ( 1 ) ;
2008-02-08 15:22:08 +03:00
/*
* Seek to len - 1 because writing a character there will
2006-06-30 12:55:55 +04:00
* increase the file size by one byte , to the desired length .
*/
if ( lseek64 ( fd , len - 1 , SEEK_SET ) < 0 ) {
2007-10-16 12:27:11 +04:00
perror ( " lseek64 " ) ;
2005-09-17 06:27:50 +04:00
exit ( 1 ) ;
}
zero = 0 ;
2007-05-07 01:51:35 +04:00
err = write ( fd , & zero , 1 ) ;
2008-02-08 15:22:08 +03:00
if ( err ! = 1 ) {
2007-05-07 01:51:35 +04:00
perror ( " write " ) ;
2005-09-17 06:27:50 +04:00
exit ( 1 ) ;
}
2007-02-10 12:44:21 +03:00
return fd ;
2005-09-17 06:27:50 +04:00
}
2007-05-07 01:51:11 +04:00
int __init create_mem_file ( unsigned long long len )
2005-09-17 06:27:50 +04:00
{
int err , fd ;
2006-03-31 14:30:08 +04:00
fd = create_tmp_file ( len ) ;
2005-09-17 06:27:50 +04:00
2007-10-16 12:27:11 +04:00
err = os_set_exec_close ( fd ) ;
2008-02-08 15:22:08 +03:00
if ( err < 0 ) {
2005-09-17 06:27:50 +04:00
errno = - err ;
perror ( " exec_close " ) ;
}
2007-02-10 12:44:21 +03:00
return fd ;
2005-09-17 06:27:50 +04:00
}
2006-04-19 09:21:43 +04:00
2007-05-07 01:51:11 +04:00
void __init check_tmpexec ( void )
2006-04-19 09:21:43 +04:00
{
void * addr ;
int err , fd = create_tmp_file ( UM_KERN_PAGE_SIZE ) ;
addr = mmap ( NULL , UM_KERN_PAGE_SIZE ,
PROT_READ | PROT_WRITE | PROT_EXEC , MAP_PRIVATE , fd , 0 ) ;
2017-05-17 20:17:14 +03:00
os_info ( " Checking PROT_EXEC mmap in %s... " , tempdir ) ;
2008-02-08 15:22:08 +03:00
if ( addr = = MAP_FAILED ) {
2006-04-19 09:21:43 +04:00
err = errno ;
2017-05-17 20:19:31 +03:00
os_warn ( " %s \n " , strerror ( err ) ) ;
2008-02-05 09:30:35 +03:00
close ( fd ) ;
2008-02-08 15:22:08 +03:00
if ( err = = EPERM )
2017-05-17 20:19:31 +03:00
os_warn ( " %s must be not mounted noexec \n " , tempdir ) ;
2006-04-19 09:21:43 +04:00
exit ( 1 ) ;
}
2017-05-17 20:17:14 +03:00
os_info ( " OK \n " ) ;
2006-04-19 09:21:43 +04:00
munmap ( addr , UM_KERN_PAGE_SIZE ) ;
close ( fd ) ;
}