mem.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  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/param.h>
  15. #include <init.h>
  16. #include <os.h>
  17. /* Modified by which_tmpdir, which is called during early boot */
  18. static char *default_tmpdir = "/tmp";
  19. /*
  20. * Modified when creating the physical memory file and when checking
  21. * the tmp filesystem for usability, both happening during early boot.
  22. */
  23. static char *tempdir = NULL;
  24. static void __init find_tempdir(void)
  25. {
  26. const char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
  27. int i;
  28. char *dir = NULL;
  29. if (tempdir != NULL)
  30. /* We've already been called */
  31. return;
  32. for (i = 0; dirs[i]; i++) {
  33. dir = getenv(dirs[i]);
  34. if ((dir != NULL) && (*dir != '\0'))
  35. break;
  36. }
  37. if ((dir == NULL) || (*dir == '\0'))
  38. dir = default_tmpdir;
  39. tempdir = malloc(strlen(dir) + 2);
  40. if (tempdir == NULL) {
  41. fprintf(stderr, "Failed to malloc tempdir, "
  42. "errno = %d\n", errno);
  43. return;
  44. }
  45. strcpy(tempdir, dir);
  46. strcat(tempdir, "/");
  47. }
  48. /*
  49. * Remove bytes from the front of the buffer and refill it so that if there's a
  50. * partial string that we care about, it will be completed, and we can recognize
  51. * it.
  52. */
  53. static int pop(int fd, char *buf, size_t size, size_t npop)
  54. {
  55. ssize_t n;
  56. size_t len = strlen(&buf[npop]);
  57. memmove(buf, &buf[npop], len + 1);
  58. n = read(fd, &buf[len], size - len - 1);
  59. if (n < 0)
  60. return -errno;
  61. buf[len + n] = '\0';
  62. return 1;
  63. }
  64. /*
  65. * This will return 1, with the first character in buf being the
  66. * character following the next instance of c in the file. This will
  67. * read the file as needed. If there's an error, -errno is returned;
  68. * if the end of the file is reached, 0 is returned.
  69. */
  70. static int next(int fd, char *buf, size_t size, char c)
  71. {
  72. ssize_t n;
  73. char *ptr;
  74. while ((ptr = strchr(buf, c)) == NULL) {
  75. n = read(fd, buf, size - 1);
  76. if (n == 0)
  77. return 0;
  78. else if (n < 0)
  79. return -errno;
  80. buf[n] = '\0';
  81. }
  82. return pop(fd, buf, size, ptr - buf + 1);
  83. }
  84. /*
  85. * Decode an octal-escaped and space-terminated path of the form used by
  86. * /proc/mounts. May be used to decode a path in-place. "out" must be at least
  87. * as large as the input. The output is always null-terminated. "len" gets the
  88. * length of the output, excluding the trailing null. Returns 0 if a full path
  89. * was successfully decoded, otherwise an error.
  90. */
  91. static int decode_path(const char *in, char *out, size_t *len)
  92. {
  93. char *first = out;
  94. int c;
  95. int i;
  96. int ret = -EINVAL;
  97. while (1) {
  98. switch (*in) {
  99. case '\0':
  100. goto out;
  101. case ' ':
  102. ret = 0;
  103. goto out;
  104. case '\\':
  105. in++;
  106. c = 0;
  107. for (i = 0; i < 3; i++) {
  108. if (*in < '0' || *in > '7')
  109. goto out;
  110. c = (c << 3) | (*in++ - '0');
  111. }
  112. *(unsigned char *)out++ = (unsigned char) c;
  113. break;
  114. default:
  115. *out++ = *in++;
  116. break;
  117. }
  118. }
  119. out:
  120. *out = '\0';
  121. *len = out - first;
  122. return ret;
  123. }
  124. /*
  125. * Computes the length of s when encoded with three-digit octal escape sequences
  126. * for the characters in chars.
  127. */
  128. static size_t octal_encoded_length(const char *s, const char *chars)
  129. {
  130. size_t len = strlen(s);
  131. while ((s = strpbrk(s, chars)) != NULL) {
  132. len += 3;
  133. s++;
  134. }
  135. return len;
  136. }
  137. enum {
  138. OUTCOME_NOTHING_MOUNTED,
  139. OUTCOME_TMPFS_MOUNT,
  140. OUTCOME_NON_TMPFS_MOUNT,
  141. };
  142. /* Read a line of /proc/mounts data looking for a tmpfs mount at "path". */
  143. static int read_mount(int fd, char *buf, size_t bufsize, const char *path,
  144. int *outcome)
  145. {
  146. int found;
  147. int match;
  148. char *space;
  149. size_t len;
  150. enum {
  151. MATCH_NONE,
  152. MATCH_EXACT,
  153. MATCH_PARENT,
  154. };
  155. found = next(fd, buf, bufsize, ' ');
  156. if (found != 1)
  157. return found;
  158. /*
  159. * If there's no following space in the buffer, then this path is
  160. * truncated, so it can't be the one we're looking for.
  161. */
  162. space = strchr(buf, ' ');
  163. if (space) {
  164. match = MATCH_NONE;
  165. if (!decode_path(buf, buf, &len)) {
  166. if (!strcmp(buf, path))
  167. match = MATCH_EXACT;
  168. else if (!strncmp(buf, path, len)
  169. && (path[len] == '/' || !strcmp(buf, "/")))
  170. match = MATCH_PARENT;
  171. }
  172. found = pop(fd, buf, bufsize, space - buf + 1);
  173. if (found != 1)
  174. return found;
  175. switch (match) {
  176. case MATCH_EXACT:
  177. if (!strncmp(buf, "tmpfs", strlen("tmpfs")))
  178. *outcome = OUTCOME_TMPFS_MOUNT;
  179. else
  180. *outcome = OUTCOME_NON_TMPFS_MOUNT;
  181. break;
  182. case MATCH_PARENT:
  183. /* This mount obscures any previous ones. */
  184. *outcome = OUTCOME_NOTHING_MOUNTED;
  185. break;
  186. }
  187. }
  188. return next(fd, buf, bufsize, '\n');
  189. }
  190. /* which_tmpdir is called only during early boot */
  191. static int checked_tmpdir = 0;
  192. /*
  193. * Look for a tmpfs mounted at /dev/shm. I couldn't find a cleaner
  194. * way to do this than to parse /proc/mounts. statfs will return the
  195. * same filesystem magic number and fs id for both /dev and /dev/shm
  196. * when they are both tmpfs, so you can't tell if they are different
  197. * filesystems. Also, there seems to be no other way of finding the
  198. * mount point of a filesystem from within it.
  199. *
  200. * If a /dev/shm tmpfs entry is found, then we switch to using it.
  201. * Otherwise, we stay with the default /tmp.
  202. */
  203. static void which_tmpdir(void)
  204. {
  205. int fd;
  206. int found;
  207. int outcome;
  208. char *path;
  209. char *buf;
  210. size_t bufsize;
  211. if (checked_tmpdir)
  212. return;
  213. checked_tmpdir = 1;
  214. printf("Checking for tmpfs mount on /dev/shm...");
  215. path = realpath("/dev/shm", NULL);
  216. if (!path) {
  217. printf("failed to check real path, errno = %d\n", errno);
  218. return;
  219. }
  220. printf("%s...", path);
  221. /*
  222. * The buffer needs to be able to fit the full octal-escaped path, a
  223. * space, and a trailing null in order to successfully decode it.
  224. */
  225. bufsize = octal_encoded_length(path, " \t\n\\") + 2;
  226. if (bufsize < 128)
  227. bufsize = 128;
  228. buf = malloc(bufsize);
  229. if (!buf) {
  230. printf("malloc failed, errno = %d\n", errno);
  231. goto out;
  232. }
  233. buf[0] = '\0';
  234. fd = open("/proc/mounts", O_RDONLY);
  235. if (fd < 0) {
  236. printf("failed to open /proc/mounts, errno = %d\n", errno);
  237. goto out1;
  238. }
  239. outcome = OUTCOME_NOTHING_MOUNTED;
  240. while (1) {
  241. found = read_mount(fd, buf, bufsize, path, &outcome);
  242. if (found != 1)
  243. break;
  244. }
  245. if (found < 0) {
  246. printf("read returned errno %d\n", -found);
  247. } else {
  248. switch (outcome) {
  249. case OUTCOME_TMPFS_MOUNT:
  250. printf("OK\n");
  251. default_tmpdir = "/dev/shm";
  252. break;
  253. case OUTCOME_NON_TMPFS_MOUNT:
  254. printf("not tmpfs\n");
  255. break;
  256. default:
  257. printf("nothing mounted on /dev/shm\n");
  258. break;
  259. }
  260. }
  261. close(fd);
  262. out1:
  263. free(buf);
  264. out:
  265. free(path);
  266. }
  267. static int __init make_tempfile(const char *template, char **out_tempname,
  268. int do_unlink)
  269. {
  270. char *tempname;
  271. int fd;
  272. which_tmpdir();
  273. tempname = malloc(MAXPATHLEN);
  274. if (tempname == NULL)
  275. return -1;
  276. find_tempdir();
  277. if ((tempdir == NULL) || (strlen(tempdir) >= MAXPATHLEN))
  278. goto out;
  279. if (template[0] != '/')
  280. strcpy(tempname, tempdir);
  281. else
  282. tempname[0] = '\0';
  283. strncat(tempname, template, MAXPATHLEN-1-strlen(tempname));
  284. fd = mkstemp(tempname);
  285. if (fd < 0) {
  286. fprintf(stderr, "open - cannot create %s: %s\n", tempname,
  287. strerror(errno));
  288. goto out;
  289. }
  290. if (do_unlink && (unlink(tempname) < 0)) {
  291. perror("unlink");
  292. goto close;
  293. }
  294. if (out_tempname) {
  295. *out_tempname = tempname;
  296. } else
  297. free(tempname);
  298. return fd;
  299. close:
  300. close(fd);
  301. out:
  302. free(tempname);
  303. return -1;
  304. }
  305. #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
  306. static int __init create_tmp_file(unsigned long long len)
  307. {
  308. int fd, err;
  309. char zero;
  310. fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
  311. if (fd < 0)
  312. exit(1);
  313. err = fchmod(fd, 0777);
  314. if (err < 0) {
  315. perror("fchmod");
  316. exit(1);
  317. }
  318. /*
  319. * Seek to len - 1 because writing a character there will
  320. * increase the file size by one byte, to the desired length.
  321. */
  322. if (lseek64(fd, len - 1, SEEK_SET) < 0) {
  323. perror("lseek64");
  324. exit(1);
  325. }
  326. zero = 0;
  327. err = write(fd, &zero, 1);
  328. if (err != 1) {
  329. perror("write");
  330. exit(1);
  331. }
  332. return fd;
  333. }
  334. int __init create_mem_file(unsigned long long len)
  335. {
  336. int err, fd;
  337. fd = create_tmp_file(len);
  338. err = os_set_exec_close(fd);
  339. if (err < 0) {
  340. errno = -err;
  341. perror("exec_close");
  342. }
  343. return fd;
  344. }
  345. void __init check_tmpexec(void)
  346. {
  347. void *addr;
  348. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  349. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  350. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  351. printf("Checking PROT_EXEC mmap in %s...",tempdir);
  352. fflush(stdout);
  353. if (addr == MAP_FAILED) {
  354. err = errno;
  355. perror("failed");
  356. close(fd);
  357. if (err == EPERM)
  358. printf("%s must be not mounted noexec\n",tempdir);
  359. exit(1);
  360. }
  361. printf("OK\n");
  362. munmap(addr, UM_KERN_PAGE_SIZE);
  363. close(fd);
  364. }