mem.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. */
  5. #include <stdio.h>
  6. #include <stddef.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <string.h>
  12. #include <sys/stat.h>
  13. #include <sys/mman.h>
  14. #include <sys/vfs.h>
  15. #include <linux/magic.h>
  16. #include <init.h>
  17. #include <kern_util.h>
  18. #include <os.h>
  19. #include "internal.h"
  20. /*
  21. * kasan_map_memory - maps memory from @start with a size of @len.
  22. * The allocated memory is filled with zeroes upon success.
  23. * @start: the start address of the memory to be mapped
  24. * @len: the length of the memory to be mapped
  25. *
  26. * This function is used to map shadow memory for KASAN in uml
  27. */
  28. void kasan_map_memory(void *start, size_t len)
  29. {
  30. if (mmap(start,
  31. len,
  32. PROT_READ|PROT_WRITE,
  33. MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE,
  34. -1,
  35. 0) == MAP_FAILED) {
  36. os_info("Couldn't allocate shadow memory: %s\n.",
  37. strerror(errno));
  38. exit(1);
  39. }
  40. }
  41. /* Set by make_tempfile() during early boot. */
  42. static char *tempdir = NULL;
  43. /* Check if dir is on tmpfs. Return 0 if yes, -1 if no or error. */
  44. static int __init check_tmpfs(const char *dir)
  45. {
  46. struct statfs st;
  47. os_info("Checking if %s is on tmpfs...", dir);
  48. if (statfs(dir, &st) < 0) {
  49. os_info("%s\n", strerror(errno));
  50. } else if (st.f_type != TMPFS_MAGIC) {
  51. os_info("no\n");
  52. } else {
  53. os_info("OK\n");
  54. return 0;
  55. }
  56. return -1;
  57. }
  58. /*
  59. * Choose the tempdir to use. We want something on tmpfs so that our memory is
  60. * not subject to the host's vm.dirty_ratio. If a tempdir is specified in the
  61. * environment, we use that even if it's not on tmpfs, but we warn the user.
  62. * Otherwise, we try common tmpfs locations, and if no tmpfs directory is found
  63. * then we fall back to /tmp.
  64. */
  65. static char * __init choose_tempdir(void)
  66. {
  67. static const char * const vars[] = {
  68. "TMPDIR",
  69. "TMP",
  70. "TEMP",
  71. NULL
  72. };
  73. static const char fallback_dir[] = "/tmp";
  74. static const char * const tmpfs_dirs[] = {
  75. "/dev/shm",
  76. fallback_dir,
  77. NULL
  78. };
  79. int i;
  80. const char *dir;
  81. os_info("Checking environment variables for a tempdir...");
  82. for (i = 0; vars[i]; i++) {
  83. dir = getenv(vars[i]);
  84. if ((dir != NULL) && (*dir != '\0')) {
  85. os_info("%s\n", dir);
  86. if (check_tmpfs(dir) >= 0)
  87. goto done;
  88. else
  89. goto warn;
  90. }
  91. }
  92. os_info("none found\n");
  93. for (i = 0; tmpfs_dirs[i]; i++) {
  94. dir = tmpfs_dirs[i];
  95. if (check_tmpfs(dir) >= 0)
  96. goto done;
  97. }
  98. dir = fallback_dir;
  99. warn:
  100. os_warn("Warning: tempdir %s is not on tmpfs\n", dir);
  101. done:
  102. /* Make a copy since getenv results may not remain valid forever. */
  103. return strdup(dir);
  104. }
  105. /*
  106. * Create an unlinked tempfile in a suitable tempdir. template must be the
  107. * basename part of the template with a leading '/'.
  108. */
  109. static int __init make_tempfile(const char *template)
  110. {
  111. char *tempname;
  112. int fd;
  113. if (tempdir == NULL) {
  114. tempdir = choose_tempdir();
  115. if (tempdir == NULL) {
  116. os_warn("Failed to choose tempdir: %s\n",
  117. strerror(errno));
  118. return -1;
  119. }
  120. }
  121. #ifdef O_TMPFILE
  122. fd = open(tempdir, O_CLOEXEC | O_RDWR | O_EXCL | O_TMPFILE, 0700);
  123. /*
  124. * If the running system does not support O_TMPFILE flag then retry
  125. * without it.
  126. */
  127. if (fd != -1 || (errno != EINVAL && errno != EISDIR &&
  128. errno != EOPNOTSUPP))
  129. return fd;
  130. #endif
  131. tempname = malloc(strlen(tempdir) + strlen(template) + 1);
  132. if (tempname == NULL)
  133. return -1;
  134. strcpy(tempname, tempdir);
  135. strcat(tempname, template);
  136. fd = mkstemp(tempname);
  137. if (fd < 0) {
  138. os_warn("open - cannot create %s: %s\n", tempname,
  139. strerror(errno));
  140. goto out;
  141. }
  142. if (unlink(tempname) < 0) {
  143. perror("unlink");
  144. goto close;
  145. }
  146. free(tempname);
  147. return fd;
  148. close:
  149. close(fd);
  150. out:
  151. free(tempname);
  152. return -1;
  153. }
  154. #define TEMPNAME_TEMPLATE "/vm_file-XXXXXX"
  155. static int __init create_tmp_file(unsigned long long len)
  156. {
  157. int fd, err;
  158. char zero;
  159. fd = make_tempfile(TEMPNAME_TEMPLATE);
  160. if (fd < 0)
  161. exit(1);
  162. /*
  163. * Seek to len - 1 because writing a character there will
  164. * increase the file size by one byte, to the desired length.
  165. */
  166. if (lseek64(fd, len - 1, SEEK_SET) < 0) {
  167. perror("lseek64");
  168. exit(1);
  169. }
  170. zero = 0;
  171. err = write(fd, &zero, 1);
  172. if (err != 1) {
  173. perror("write");
  174. exit(1);
  175. }
  176. return fd;
  177. }
  178. int __init create_mem_file(unsigned long long len)
  179. {
  180. int err, fd;
  181. fd = create_tmp_file(len);
  182. err = os_set_exec_close(fd);
  183. if (err < 0) {
  184. errno = -err;
  185. perror("exec_close");
  186. }
  187. return fd;
  188. }
  189. void __init check_tmpexec(void)
  190. {
  191. void *addr;
  192. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  193. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  194. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  195. os_info("Checking PROT_EXEC mmap in %s...", tempdir);
  196. if (addr == MAP_FAILED) {
  197. err = errno;
  198. os_warn("%s\n", strerror(err));
  199. close(fd);
  200. if (err == EPERM)
  201. os_warn("%s must be not mounted noexec\n", tempdir);
  202. exit(1);
  203. }
  204. os_info("OK\n");
  205. munmap(addr, UM_KERN_PAGE_SIZE);
  206. close(fd);
  207. }