xref: /linux/arch/um/os-Linux/mem.c (revision 6a85e34c4d07d2ec0c153067baff338ac0db55ca)
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>
18*6a85e34cSTiwei Bie #include <kern_util.h>
1937185b33SAl Viro #include <os.h>
200f80bc85SJeff Dike 
215b301409SPatricia Alfonso /*
225b301409SPatricia Alfonso  * kasan_map_memory - maps memory from @start with a size of @len.
235b301409SPatricia Alfonso  * The allocated memory is filled with zeroes upon success.
245b301409SPatricia Alfonso  * @start: the start address of the memory to be mapped
255b301409SPatricia Alfonso  * @len: the length of the memory to be mapped
265b301409SPatricia Alfonso  *
275b301409SPatricia Alfonso  * This function is used to map shadow memory for KASAN in uml
285b301409SPatricia Alfonso  */
295b301409SPatricia Alfonso void kasan_map_memory(void *start, size_t len)
305b301409SPatricia Alfonso {
315b301409SPatricia Alfonso 	if (mmap(start,
325b301409SPatricia Alfonso 		 len,
335b301409SPatricia Alfonso 		 PROT_READ|PROT_WRITE,
345b301409SPatricia Alfonso 		 MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE,
355b301409SPatricia Alfonso 		 -1,
365b301409SPatricia Alfonso 		 0) == MAP_FAILED) {
375b301409SPatricia Alfonso 		os_info("Couldn't allocate shadow memory: %s\n.",
385b301409SPatricia Alfonso 			strerror(errno));
395b301409SPatricia Alfonso 		exit(1);
405b301409SPatricia Alfonso 	}
415b301409SPatricia Alfonso }
425b301409SPatricia Alfonso 
430d71832eSTristan Schmelcher /* Set by make_tempfile() during early boot. */
440f80bc85SJeff Dike static char *tempdir = NULL;
450f80bc85SJeff Dike 
460d71832eSTristan Schmelcher /* Check if dir is on tmpfs. Return 0 if yes, -1 if no or error. */
470d71832eSTristan Schmelcher static int __init check_tmpfs(const char *dir)
480f80bc85SJeff Dike {
490d71832eSTristan Schmelcher 	struct statfs st;
500f80bc85SJeff Dike 
51d3878bb8SMasami Hiramatsu 	os_info("Checking if %s is on tmpfs...", dir);
520d71832eSTristan Schmelcher 	if (statfs(dir, &st) < 0) {
53d3878bb8SMasami Hiramatsu 		os_info("%s\n", strerror(errno));
540d71832eSTristan Schmelcher 	} else if (st.f_type != TMPFS_MAGIC) {
55d3878bb8SMasami Hiramatsu 		os_info("no\n");
5674735341STristan Schmelcher 	} else {
57d3878bb8SMasami Hiramatsu 		os_info("OK\n");
580d71832eSTristan Schmelcher 		return 0;
5974735341STristan Schmelcher 	}
600d71832eSTristan Schmelcher 	return -1;
6174735341STristan Schmelcher }
6274735341STristan Schmelcher 
630d71832eSTristan Schmelcher /*
640d71832eSTristan Schmelcher  * Choose the tempdir to use. We want something on tmpfs so that our memory is
650d71832eSTristan Schmelcher  * not subject to the host's vm.dirty_ratio. If a tempdir is specified in the
660d71832eSTristan Schmelcher  * environment, we use that even if it's not on tmpfs, but we warn the user.
670d71832eSTristan Schmelcher  * Otherwise, we try common tmpfs locations, and if no tmpfs directory is found
680d71832eSTristan Schmelcher  * then we fall back to /tmp.
690d71832eSTristan Schmelcher  */
700d71832eSTristan Schmelcher static char * __init choose_tempdir(void)
710d71832eSTristan Schmelcher {
720d71832eSTristan Schmelcher 	static const char * const vars[] = {
730d71832eSTristan Schmelcher 		"TMPDIR",
740d71832eSTristan Schmelcher 		"TMP",
750d71832eSTristan Schmelcher 		"TEMP",
760d71832eSTristan Schmelcher 		NULL
770d71832eSTristan Schmelcher 	};
780d71832eSTristan Schmelcher 	static const char fallback_dir[] = "/tmp";
790d71832eSTristan Schmelcher 	static const char * const tmpfs_dirs[] = {
800d71832eSTristan Schmelcher 		"/dev/shm",
810d71832eSTristan Schmelcher 		fallback_dir,
820d71832eSTristan Schmelcher 		NULL
830d71832eSTristan Schmelcher 	};
840d71832eSTristan Schmelcher 	int i;
850d71832eSTristan Schmelcher 	const char *dir;
860d71832eSTristan Schmelcher 
87d3878bb8SMasami Hiramatsu 	os_info("Checking environment variables for a tempdir...");
880d71832eSTristan Schmelcher 	for (i = 0; vars[i]; i++) {
890d71832eSTristan Schmelcher 		dir = getenv(vars[i]);
900d71832eSTristan Schmelcher 		if ((dir != NULL) && (*dir != '\0')) {
91d3878bb8SMasami Hiramatsu 			os_info("%s\n", dir);
920d71832eSTristan Schmelcher 			if (check_tmpfs(dir) >= 0)
930d71832eSTristan Schmelcher 				goto done;
940d71832eSTristan Schmelcher 			else
950d71832eSTristan Schmelcher 				goto warn;
960d71832eSTristan Schmelcher 		}
970d71832eSTristan Schmelcher 	}
98d3878bb8SMasami Hiramatsu 	os_info("none found\n");
990d71832eSTristan Schmelcher 
1000d71832eSTristan Schmelcher 	for (i = 0; tmpfs_dirs[i]; i++) {
1010d71832eSTristan Schmelcher 		dir = tmpfs_dirs[i];
1020d71832eSTristan Schmelcher 		if (check_tmpfs(dir) >= 0)
1030d71832eSTristan Schmelcher 			goto done;
104966a082fSRob Landley 	}
105966a082fSRob Landley 
1060d71832eSTristan Schmelcher 	dir = fallback_dir;
1070d71832eSTristan Schmelcher warn:
1080936d4f3SMasami Hiramatsu 	os_warn("Warning: tempdir %s is not on tmpfs\n", dir);
1090d71832eSTristan Schmelcher done:
1100d71832eSTristan Schmelcher 	/* Make a copy since getenv results may not remain valid forever. */
1110d71832eSTristan Schmelcher 	return strdup(dir);
1120d71832eSTristan Schmelcher }
1130d71832eSTristan Schmelcher 
1140d71832eSTristan Schmelcher /*
1150d71832eSTristan Schmelcher  * Create an unlinked tempfile in a suitable tempdir. template must be the
1160d71832eSTristan Schmelcher  * basename part of the template with a leading '/'.
1170d71832eSTristan Schmelcher  */
1180d71832eSTristan Schmelcher static int __init make_tempfile(const char *template)
1190f80bc85SJeff Dike {
12087276f72SPaolo 'Blaisorblade' Giarrusso 	char *tempname;
1210f80bc85SJeff Dike 	int fd;
1220f80bc85SJeff Dike 
1230d71832eSTristan Schmelcher 	if (tempdir == NULL) {
1240d71832eSTristan Schmelcher 		tempdir = choose_tempdir();
1250d71832eSTristan Schmelcher 		if (tempdir == NULL) {
1260936d4f3SMasami Hiramatsu 			os_warn("Failed to choose tempdir: %s\n",
1270d71832eSTristan Schmelcher 				strerror(errno));
1280d71832eSTristan Schmelcher 			return -1;
1290d71832eSTristan Schmelcher 		}
1300d71832eSTristan Schmelcher 	}
1310d71832eSTristan Schmelcher 
1323e46b253SMickaël Salaün #ifdef O_TMPFILE
1333e46b253SMickaël Salaün 	fd = open(tempdir, O_CLOEXEC | O_RDWR | O_EXCL | O_TMPFILE, 0700);
1343e46b253SMickaël Salaün 	/*
1353e46b253SMickaël Salaün 	 * If the running system does not support O_TMPFILE flag then retry
1363e46b253SMickaël Salaün 	 * without it.
1373e46b253SMickaël Salaün 	 */
1383e46b253SMickaël Salaün 	if (fd != -1 || (errno != EINVAL && errno != EISDIR &&
1393e46b253SMickaël Salaün 			errno != EOPNOTSUPP))
1403e46b253SMickaël Salaün 		return fd;
1413e46b253SMickaël Salaün #endif
1423e46b253SMickaël Salaün 
1430d71832eSTristan Schmelcher 	tempname = malloc(strlen(tempdir) + strlen(template) + 1);
14411a7ac23SJim Meyering 	if (tempname == NULL)
14511a7ac23SJim Meyering 		return -1;
14687276f72SPaolo 'Blaisorblade' Giarrusso 
1470f80bc85SJeff Dike 	strcpy(tempname, tempdir);
1480d71832eSTristan Schmelcher 	strcat(tempname, template);
1490f80bc85SJeff Dike 	fd = mkstemp(tempname);
1500f80bc85SJeff Dike 	if (fd < 0) {
1510936d4f3SMasami Hiramatsu 		os_warn("open - cannot create %s: %s\n", tempname,
1520f80bc85SJeff Dike 			strerror(errno));
15387276f72SPaolo 'Blaisorblade' Giarrusso 		goto out;
1540f80bc85SJeff Dike 	}
1550d71832eSTristan Schmelcher 	if (unlink(tempname) < 0) {
1560f80bc85SJeff Dike 		perror("unlink");
1572a6d0ac1SDavidlohr Bueso 		goto close;
1580f80bc85SJeff Dike 	}
15987276f72SPaolo 'Blaisorblade' Giarrusso 	free(tempname);
16081999a01SJeff Dike 	return fd;
1612a6d0ac1SDavidlohr Bueso close:
1622a6d0ac1SDavidlohr Bueso 	close(fd);
16387276f72SPaolo 'Blaisorblade' Giarrusso out:
16487276f72SPaolo 'Blaisorblade' Giarrusso 	free(tempname);
16587276f72SPaolo 'Blaisorblade' Giarrusso 	return -1;
1660f80bc85SJeff Dike }
1670f80bc85SJeff Dike 
1680d71832eSTristan Schmelcher #define TEMPNAME_TEMPLATE "/vm_file-XXXXXX"
1690f80bc85SJeff Dike 
1705134d8feSJeff Dike static int __init create_tmp_file(unsigned long long len)
1710f80bc85SJeff Dike {
1720f80bc85SJeff Dike 	int fd, err;
1730f80bc85SJeff Dike 	char zero;
1740f80bc85SJeff Dike 
1750d71832eSTristan Schmelcher 	fd = make_tempfile(TEMPNAME_TEMPLATE);
1765134d8feSJeff Dike 	if (fd < 0)
1770f80bc85SJeff Dike 		exit(1);
1780f80bc85SJeff Dike 
1795134d8feSJeff Dike 	/*
1805134d8feSJeff Dike 	 * Seek to len - 1 because writing a character there will
181190f4939SJeff Dike 	 * increase the file size by one byte, to the desired length.
182190f4939SJeff Dike 	 */
183190f4939SJeff Dike 	if (lseek64(fd, len - 1, SEEK_SET) < 0) {
184512b6fb1SJeff Dike 		perror("lseek64");
1850f80bc85SJeff Dike 		exit(1);
1860f80bc85SJeff Dike 	}
1870f80bc85SJeff Dike 
1880f80bc85SJeff Dike 	zero = 0;
1890f80bc85SJeff Dike 
190a61f334fSJeff Dike 	err = write(fd, &zero, 1);
1910f80bc85SJeff Dike 	if (err != 1) {
192a61f334fSJeff Dike 		perror("write");
1930f80bc85SJeff Dike 		exit(1);
1940f80bc85SJeff Dike 	}
1950f80bc85SJeff Dike 
19681999a01SJeff Dike 	return fd;
1970f80bc85SJeff Dike }
1980f80bc85SJeff Dike 
19936e45463SJeff Dike int __init create_mem_file(unsigned long long len)
2000f80bc85SJeff Dike {
2010f80bc85SJeff Dike 	int err, fd;
2020f80bc85SJeff Dike 
20302dea087SJeff Dike 	fd = create_tmp_file(len);
2040f80bc85SJeff Dike 
205512b6fb1SJeff Dike 	err = os_set_exec_close(fd);
2060f80bc85SJeff Dike 	if (err < 0) {
2070f80bc85SJeff Dike 		errno = -err;
2080f80bc85SJeff Dike 		perror("exec_close");
2090f80bc85SJeff Dike 	}
21081999a01SJeff Dike 	return fd;
2110f80bc85SJeff Dike }
212966a082fSRob Landley 
21336e45463SJeff Dike void __init check_tmpexec(void)
214966a082fSRob Landley {
215966a082fSRob Landley 	void *addr;
216966a082fSRob Landley 	int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
217966a082fSRob Landley 
218966a082fSRob Landley 	addr = mmap(NULL, UM_KERN_PAGE_SIZE,
219966a082fSRob Landley 		    PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
220d3878bb8SMasami Hiramatsu 	os_info("Checking PROT_EXEC mmap in %s...", tempdir);
221966a082fSRob Landley 	if (addr == MAP_FAILED) {
222966a082fSRob Landley 		err = errno;
2230936d4f3SMasami Hiramatsu 		os_warn("%s\n", strerror(err));
224c9a3072dSWANG Cong 		close(fd);
225966a082fSRob Landley 		if (err == EPERM)
2260936d4f3SMasami Hiramatsu 			os_warn("%s must be not mounted noexec\n", tempdir);
227966a082fSRob Landley 		exit(1);
228966a082fSRob Landley 	}
229d3878bb8SMasami Hiramatsu 	os_info("OK\n");
230966a082fSRob Landley 	munmap(addr, UM_KERN_PAGE_SIZE);
231966a082fSRob Landley 
232966a082fSRob Landley 	close(fd);
233966a082fSRob Landley }
234