fs.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
  3. * Copyright (C) 2015 Etienne Champetier <champetier.etienne@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #define _GNU_SOURCE
  15. #include <assert.h>
  16. #include <elf.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <linux/limits.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/stat.h>
  23. #include <sys/mman.h>
  24. #include <unistd.h>
  25. #include <libubox/avl.h>
  26. #include <libubox/avl-cmp.h>
  27. #include "elf.h"
  28. #include "fs.h"
  29. #include "jail.h"
  30. #include "log.h"
  31. struct mount {
  32. struct avl_node avl;
  33. const char *path;
  34. int readonly;
  35. int error;
  36. };
  37. struct avl_tree mounts;
  38. int add_mount(const char *path, int readonly, int error)
  39. {
  40. assert(path != NULL);
  41. if (avl_find(&mounts, path))
  42. return 1;
  43. struct mount *m;
  44. m = calloc(1, sizeof(struct mount));
  45. assert(m != NULL);
  46. m->avl.key = m->path = strdup(path);
  47. m->readonly = readonly;
  48. m->error = error;
  49. avl_insert(&mounts, &m->avl);
  50. DEBUG("adding mount %s ro(%d) err(%d)\n", m->path, m->readonly, m->error != 0);
  51. return 0;
  52. }
  53. int mount_all(const char *jailroot) {
  54. struct library *l;
  55. struct mount *m;
  56. avl_for_each_element(&libraries, l, avl)
  57. add_mount(l->path, 1, -1);
  58. avl_for_each_element(&mounts, m, avl)
  59. if (mount_bind(jailroot, m->path, m->readonly, m->error))
  60. return -1;
  61. return 0;
  62. }
  63. void mount_list_init(void) {
  64. avl_init(&mounts, avl_strcmp, false, NULL);
  65. }
  66. static int add_script_interp(const char *path, const char *map, int size)
  67. {
  68. int start = 2;
  69. while (start < size && map[start] != '/') {
  70. start++;
  71. }
  72. if (start >= size) {
  73. ERROR("bad script interp (%s)\n", path);
  74. return -1;
  75. }
  76. int stop = start + 1;
  77. while (stop < size && map[stop] > 0x20 && map[stop] <= 0x7e) {
  78. stop++;
  79. }
  80. if (stop >= size || (stop-start) > PATH_MAX) {
  81. ERROR("bad script interp (%s)\n", path);
  82. return -1;
  83. }
  84. char buf[PATH_MAX];
  85. strncpy(buf, map+start, stop-start);
  86. return add_path_and_deps(buf, 1, -1, 0);
  87. }
  88. int add_path_and_deps(const char *path, int readonly, int error, int lib)
  89. {
  90. assert(path != NULL);
  91. if (lib == 0 && path[0] != '/') {
  92. ERROR("%s is not an absolute path\n", path);
  93. return error;
  94. }
  95. char *map = NULL;
  96. int fd, ret = -1;
  97. if (path[0] == '/') {
  98. if (avl_find(&mounts, path))
  99. return 0;
  100. fd = open(path, O_RDONLY|O_CLOEXEC);
  101. if (fd == -1)
  102. return error;
  103. add_mount(path, readonly, error);
  104. } else {
  105. if (avl_find(&libraries, path))
  106. return 0;
  107. char *fullpath;
  108. fd = lib_open(&fullpath, path);
  109. if (fd == -1)
  110. return error;
  111. if (fullpath) {
  112. alloc_library(fullpath, path);
  113. free(fullpath);
  114. }
  115. }
  116. struct stat s;
  117. if (fstat(fd, &s) == -1) {
  118. ERROR("fstat(%s) failed: %m\n", path);
  119. ret = error;
  120. goto out;
  121. }
  122. if (!S_ISREG(s.st_mode)) {
  123. ret = 0;
  124. goto out;
  125. }
  126. /* too small to be an ELF or a script -> "normal" file */
  127. if (s.st_size < 4) {
  128. ret = 0;
  129. goto out;
  130. }
  131. map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  132. if (map == MAP_FAILED) {
  133. ERROR("failed to mmap %s: %m\n", path);
  134. ret = -1;
  135. goto out;
  136. }
  137. if (map[0] == '#' && map[1] == '!') {
  138. ret = add_script_interp(path, map, s.st_size);
  139. goto out;
  140. }
  141. if (map[0] == ELFMAG0 && map[1] == ELFMAG1 && map[2] == ELFMAG2 && map[3] == ELFMAG3) {
  142. ret = elf_load_deps(path, map);
  143. goto out;
  144. }
  145. ret = 0;
  146. out:
  147. if (fd >= 0)
  148. close(fd);
  149. if (map)
  150. munmap(map, s.st_size);
  151. return ret;
  152. }