mem.c 4.8 KB

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