197870c34SAlex Dewar // SPDX-License-Identifier: GPL-2.0 25134d8feSJeff Dike /* 35134d8feSJeff Dike * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 45134d8feSJeff Dike */ 55134d8feSJeff Dike 60f80bc85SJeff Dike #include <stdio.h> 70f80bc85SJeff Dike #include <stddef.h> 85134d8feSJeff Dike #include <stdlib.h> 90f80bc85SJeff Dike #include <unistd.h> 100f80bc85SJeff Dike #include <errno.h> 110f80bc85SJeff Dike #include <fcntl.h> 125134d8feSJeff Dike #include <string.h> 13fb967eccSLiu Aleaxander #include <sys/stat.h> 140f80bc85SJeff Dike #include <sys/mman.h> 150d71832eSTristan Schmelcher #include <sys/vfs.h> 160d71832eSTristan Schmelcher #include <linux/magic.h> 1737185b33SAl Viro #include <init.h> 1837185b33SAl Viro #include <os.h> 190f80bc85SJeff Dike 20*5b301409SPatricia Alfonso /* 21*5b301409SPatricia Alfonso * kasan_map_memory - maps memory from @start with a size of @len. 22*5b301409SPatricia Alfonso * The allocated memory is filled with zeroes upon success. 23*5b301409SPatricia Alfonso * @start: the start address of the memory to be mapped 24*5b301409SPatricia Alfonso * @len: the length of the memory to be mapped 25*5b301409SPatricia Alfonso * 26*5b301409SPatricia Alfonso * This function is used to map shadow memory for KASAN in uml 27*5b301409SPatricia Alfonso */ 28*5b301409SPatricia Alfonso void kasan_map_memory(void *start, size_t len) 29*5b301409SPatricia Alfonso { 30*5b301409SPatricia Alfonso if (mmap(start, 31*5b301409SPatricia Alfonso len, 32*5b301409SPatricia Alfonso PROT_READ|PROT_WRITE, 33*5b301409SPatricia Alfonso MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, 34*5b301409SPatricia Alfonso -1, 35*5b301409SPatricia Alfonso 0) == MAP_FAILED) { 36*5b301409SPatricia Alfonso os_info("Couldn't allocate shadow memory: %s\n.", 37*5b301409SPatricia Alfonso strerror(errno)); 38*5b301409SPatricia Alfonso exit(1); 39*5b301409SPatricia Alfonso } 40*5b301409SPatricia Alfonso } 41*5b301409SPatricia Alfonso 420d71832eSTristan Schmelcher /* Set by make_tempfile() during early boot. */ 430f80bc85SJeff Dike static char *tempdir = NULL; 440f80bc85SJeff Dike 450d71832eSTristan Schmelcher /* Check if dir is on tmpfs. Return 0 if yes, -1 if no or error. */ 460d71832eSTristan Schmelcher static int __init check_tmpfs(const char *dir) 470f80bc85SJeff Dike { 480d71832eSTristan Schmelcher struct statfs st; 490f80bc85SJeff Dike 50d3878bb8SMasami Hiramatsu os_info("Checking if %s is on tmpfs...", dir); 510d71832eSTristan Schmelcher if (statfs(dir, &st) < 0) { 52d3878bb8SMasami Hiramatsu os_info("%s\n", strerror(errno)); 530d71832eSTristan Schmelcher } else if (st.f_type != TMPFS_MAGIC) { 54d3878bb8SMasami Hiramatsu os_info("no\n"); 5574735341STristan Schmelcher } else { 56d3878bb8SMasami Hiramatsu os_info("OK\n"); 570d71832eSTristan Schmelcher return 0; 5874735341STristan Schmelcher } 590d71832eSTristan Schmelcher return -1; 6074735341STristan Schmelcher } 6174735341STristan Schmelcher 620d71832eSTristan Schmelcher /* 630d71832eSTristan Schmelcher * Choose the tempdir to use. We want something on tmpfs so that our memory is 640d71832eSTristan Schmelcher * not subject to the host's vm.dirty_ratio. If a tempdir is specified in the 650d71832eSTristan Schmelcher * environment, we use that even if it's not on tmpfs, but we warn the user. 660d71832eSTristan Schmelcher * Otherwise, we try common tmpfs locations, and if no tmpfs directory is found 670d71832eSTristan Schmelcher * then we fall back to /tmp. 680d71832eSTristan Schmelcher */ 690d71832eSTristan Schmelcher static char * __init choose_tempdir(void) 700d71832eSTristan Schmelcher { 710d71832eSTristan Schmelcher static const char * const vars[] = { 720d71832eSTristan Schmelcher "TMPDIR", 730d71832eSTristan Schmelcher "TMP", 740d71832eSTristan Schmelcher "TEMP", 750d71832eSTristan Schmelcher NULL 760d71832eSTristan Schmelcher }; 770d71832eSTristan Schmelcher static const char fallback_dir[] = "/tmp"; 780d71832eSTristan Schmelcher static const char * const tmpfs_dirs[] = { 790d71832eSTristan Schmelcher "/dev/shm", 800d71832eSTristan Schmelcher fallback_dir, 810d71832eSTristan Schmelcher NULL 820d71832eSTristan Schmelcher }; 830d71832eSTristan Schmelcher int i; 840d71832eSTristan Schmelcher const char *dir; 850d71832eSTristan Schmelcher 86d3878bb8SMasami Hiramatsu os_info("Checking environment variables for a tempdir..."); 870d71832eSTristan Schmelcher for (i = 0; vars[i]; i++) { 880d71832eSTristan Schmelcher dir = getenv(vars[i]); 890d71832eSTristan Schmelcher if ((dir != NULL) && (*dir != '\0')) { 90d3878bb8SMasami Hiramatsu os_info("%s\n", dir); 910d71832eSTristan Schmelcher if (check_tmpfs(dir) >= 0) 920d71832eSTristan Schmelcher goto done; 930d71832eSTristan Schmelcher else 940d71832eSTristan Schmelcher goto warn; 950d71832eSTristan Schmelcher } 960d71832eSTristan Schmelcher } 97d3878bb8SMasami Hiramatsu os_info("none found\n"); 980d71832eSTristan Schmelcher 990d71832eSTristan Schmelcher for (i = 0; tmpfs_dirs[i]; i++) { 1000d71832eSTristan Schmelcher dir = tmpfs_dirs[i]; 1010d71832eSTristan Schmelcher if (check_tmpfs(dir) >= 0) 1020d71832eSTristan Schmelcher goto done; 103966a082fSRob Landley } 104966a082fSRob Landley 1050d71832eSTristan Schmelcher dir = fallback_dir; 1060d71832eSTristan Schmelcher warn: 1070936d4f3SMasami Hiramatsu os_warn("Warning: tempdir %s is not on tmpfs\n", dir); 1080d71832eSTristan Schmelcher done: 1090d71832eSTristan Schmelcher /* Make a copy since getenv results may not remain valid forever. */ 1100d71832eSTristan Schmelcher return strdup(dir); 1110d71832eSTristan Schmelcher } 1120d71832eSTristan Schmelcher 1130d71832eSTristan Schmelcher /* 1140d71832eSTristan Schmelcher * Create an unlinked tempfile in a suitable tempdir. template must be the 1150d71832eSTristan Schmelcher * basename part of the template with a leading '/'. 1160d71832eSTristan Schmelcher */ 1170d71832eSTristan Schmelcher static int __init make_tempfile(const char *template) 1180f80bc85SJeff Dike { 11987276f72SPaolo 'Blaisorblade' Giarrusso char *tempname; 1200f80bc85SJeff Dike int fd; 1210f80bc85SJeff Dike 1220d71832eSTristan Schmelcher if (tempdir == NULL) { 1230d71832eSTristan Schmelcher tempdir = choose_tempdir(); 1240d71832eSTristan Schmelcher if (tempdir == NULL) { 1250936d4f3SMasami Hiramatsu os_warn("Failed to choose tempdir: %s\n", 1260d71832eSTristan Schmelcher strerror(errno)); 1270d71832eSTristan Schmelcher return -1; 1280d71832eSTristan Schmelcher } 1290d71832eSTristan Schmelcher } 1300d71832eSTristan Schmelcher 1313e46b253SMickaël Salaün #ifdef O_TMPFILE 1323e46b253SMickaël Salaün fd = open(tempdir, O_CLOEXEC | O_RDWR | O_EXCL | O_TMPFILE, 0700); 1333e46b253SMickaël Salaün /* 1343e46b253SMickaël Salaün * If the running system does not support O_TMPFILE flag then retry 1353e46b253SMickaël Salaün * without it. 1363e46b253SMickaël Salaün */ 1373e46b253SMickaël Salaün if (fd != -1 || (errno != EINVAL && errno != EISDIR && 1383e46b253SMickaël Salaün errno != EOPNOTSUPP)) 1393e46b253SMickaël Salaün return fd; 1403e46b253SMickaël Salaün #endif 1413e46b253SMickaël Salaün 1420d71832eSTristan Schmelcher tempname = malloc(strlen(tempdir) + strlen(template) + 1); 14311a7ac23SJim Meyering if (tempname == NULL) 14411a7ac23SJim Meyering return -1; 14587276f72SPaolo 'Blaisorblade' Giarrusso 1460f80bc85SJeff Dike strcpy(tempname, tempdir); 1470d71832eSTristan Schmelcher strcat(tempname, template); 1480f80bc85SJeff Dike fd = mkstemp(tempname); 1490f80bc85SJeff Dike if (fd < 0) { 1500936d4f3SMasami Hiramatsu os_warn("open - cannot create %s: %s\n", tempname, 1510f80bc85SJeff Dike strerror(errno)); 15287276f72SPaolo 'Blaisorblade' Giarrusso goto out; 1530f80bc85SJeff Dike } 1540d71832eSTristan Schmelcher if (unlink(tempname) < 0) { 1550f80bc85SJeff Dike perror("unlink"); 1562a6d0ac1SDavidlohr Bueso goto close; 1570f80bc85SJeff Dike } 15887276f72SPaolo 'Blaisorblade' Giarrusso free(tempname); 15981999a01SJeff Dike return fd; 1602a6d0ac1SDavidlohr Bueso close: 1612a6d0ac1SDavidlohr Bueso close(fd); 16287276f72SPaolo 'Blaisorblade' Giarrusso out: 16387276f72SPaolo 'Blaisorblade' Giarrusso free(tempname); 16487276f72SPaolo 'Blaisorblade' Giarrusso return -1; 1650f80bc85SJeff Dike } 1660f80bc85SJeff Dike 1670d71832eSTristan Schmelcher #define TEMPNAME_TEMPLATE "/vm_file-XXXXXX" 1680f80bc85SJeff Dike 1695134d8feSJeff Dike static int __init create_tmp_file(unsigned long long len) 1700f80bc85SJeff Dike { 1710f80bc85SJeff Dike int fd, err; 1720f80bc85SJeff Dike char zero; 1730f80bc85SJeff Dike 1740d71832eSTristan Schmelcher fd = make_tempfile(TEMPNAME_TEMPLATE); 1755134d8feSJeff Dike if (fd < 0) 1760f80bc85SJeff Dike exit(1); 1770f80bc85SJeff Dike 1785134d8feSJeff Dike /* 1795134d8feSJeff Dike * Seek to len - 1 because writing a character there will 180190f4939SJeff Dike * increase the file size by one byte, to the desired length. 181190f4939SJeff Dike */ 182190f4939SJeff Dike if (lseek64(fd, len - 1, SEEK_SET) < 0) { 183512b6fb1SJeff Dike perror("lseek64"); 1840f80bc85SJeff Dike exit(1); 1850f80bc85SJeff Dike } 1860f80bc85SJeff Dike 1870f80bc85SJeff Dike zero = 0; 1880f80bc85SJeff Dike 189a61f334fSJeff Dike err = write(fd, &zero, 1); 1900f80bc85SJeff Dike if (err != 1) { 191a61f334fSJeff Dike perror("write"); 1920f80bc85SJeff Dike exit(1); 1930f80bc85SJeff Dike } 1940f80bc85SJeff Dike 19581999a01SJeff Dike return fd; 1960f80bc85SJeff Dike } 1970f80bc85SJeff Dike 19836e45463SJeff Dike int __init create_mem_file(unsigned long long len) 1990f80bc85SJeff Dike { 2000f80bc85SJeff Dike int err, fd; 2010f80bc85SJeff Dike 20202dea087SJeff Dike fd = create_tmp_file(len); 2030f80bc85SJeff Dike 204512b6fb1SJeff Dike err = os_set_exec_close(fd); 2050f80bc85SJeff Dike if (err < 0) { 2060f80bc85SJeff Dike errno = -err; 2070f80bc85SJeff Dike perror("exec_close"); 2080f80bc85SJeff Dike } 20981999a01SJeff Dike return fd; 2100f80bc85SJeff Dike } 211966a082fSRob Landley 21236e45463SJeff Dike void __init check_tmpexec(void) 213966a082fSRob Landley { 214966a082fSRob Landley void *addr; 215966a082fSRob Landley int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE); 216966a082fSRob Landley 217966a082fSRob Landley addr = mmap(NULL, UM_KERN_PAGE_SIZE, 218966a082fSRob Landley PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0); 219d3878bb8SMasami Hiramatsu os_info("Checking PROT_EXEC mmap in %s...", tempdir); 220966a082fSRob Landley if (addr == MAP_FAILED) { 221966a082fSRob Landley err = errno; 2220936d4f3SMasami Hiramatsu os_warn("%s\n", strerror(err)); 223c9a3072dSWANG Cong close(fd); 224966a082fSRob Landley if (err == EPERM) 2250936d4f3SMasami Hiramatsu os_warn("%s must be not mounted noexec\n", tempdir); 226966a082fSRob Landley exit(1); 227966a082fSRob Landley } 228d3878bb8SMasami Hiramatsu os_info("OK\n"); 229966a082fSRob Landley munmap(addr, UM_KERN_PAGE_SIZE); 230966a082fSRob Landley 231966a082fSRob Landley close(fd); 232966a082fSRob Landley } 233