statmount_test_ns.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #define _GNU_SOURCE
  3. #include <assert.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <sched.h>
  7. #include <stdlib.h>
  8. #include <sys/mount.h>
  9. #include <sys/stat.h>
  10. #include <sys/wait.h>
  11. #include <linux/nsfs.h>
  12. #include <linux/stat.h>
  13. #include "statmount.h"
  14. #include "../../kselftest.h"
  15. #define NSID_PASS 0
  16. #define NSID_FAIL 1
  17. #define NSID_SKIP 2
  18. #define NSID_ERROR 3
  19. static void handle_result(int ret, const char *testname)
  20. {
  21. if (ret == NSID_PASS)
  22. ksft_test_result_pass("%s\n", testname);
  23. else if (ret == NSID_FAIL)
  24. ksft_test_result_fail("%s\n", testname);
  25. else if (ret == NSID_ERROR)
  26. ksft_exit_fail_msg("%s\n", testname);
  27. else
  28. ksft_test_result_skip("%s\n", testname);
  29. }
  30. static inline int wait_for_pid(pid_t pid)
  31. {
  32. int status, ret;
  33. again:
  34. ret = waitpid(pid, &status, 0);
  35. if (ret == -1) {
  36. if (errno == EINTR)
  37. goto again;
  38. ksft_print_msg("waitpid returned -1, errno=%d\n", errno);
  39. return -1;
  40. }
  41. if (!WIFEXITED(status)) {
  42. ksft_print_msg(
  43. "waitpid !WIFEXITED, WIFSIGNALED=%d, WTERMSIG=%d\n",
  44. WIFSIGNALED(status), WTERMSIG(status));
  45. return -1;
  46. }
  47. ret = WEXITSTATUS(status);
  48. return ret;
  49. }
  50. static int get_mnt_ns_id(const char *mnt_ns, uint64_t *mnt_ns_id)
  51. {
  52. int fd = open(mnt_ns, O_RDONLY);
  53. if (fd < 0) {
  54. ksft_print_msg("failed to open for ns %s: %s\n",
  55. mnt_ns, strerror(errno));
  56. sleep(60);
  57. return NSID_ERROR;
  58. }
  59. if (ioctl(fd, NS_GET_MNTNS_ID, mnt_ns_id) < 0) {
  60. ksft_print_msg("failed to get the nsid for ns %s: %s\n",
  61. mnt_ns, strerror(errno));
  62. return NSID_ERROR;
  63. }
  64. close(fd);
  65. return NSID_PASS;
  66. }
  67. static int get_mnt_id(const char *path, uint64_t *mnt_id)
  68. {
  69. struct statx sx;
  70. int ret;
  71. ret = statx(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx);
  72. if (ret == -1) {
  73. ksft_print_msg("retrieving unique mount ID for %s: %s\n", path,
  74. strerror(errno));
  75. return NSID_ERROR;
  76. }
  77. if (!(sx.stx_mask & STATX_MNT_ID_UNIQUE)) {
  78. ksft_print_msg("no unique mount ID available for %s\n", path);
  79. return NSID_ERROR;
  80. }
  81. *mnt_id = sx.stx_mnt_id;
  82. return NSID_PASS;
  83. }
  84. static int write_file(const char *path, const char *val)
  85. {
  86. int fd = open(path, O_WRONLY);
  87. size_t len = strlen(val);
  88. int ret;
  89. if (fd == -1) {
  90. ksft_print_msg("opening %s for write: %s\n", path, strerror(errno));
  91. return NSID_ERROR;
  92. }
  93. ret = write(fd, val, len);
  94. if (ret == -1) {
  95. ksft_print_msg("writing to %s: %s\n", path, strerror(errno));
  96. return NSID_ERROR;
  97. }
  98. if (ret != len) {
  99. ksft_print_msg("short write to %s\n", path);
  100. return NSID_ERROR;
  101. }
  102. ret = close(fd);
  103. if (ret == -1) {
  104. ksft_print_msg("closing %s\n", path);
  105. return NSID_ERROR;
  106. }
  107. return NSID_PASS;
  108. }
  109. static int setup_namespace(void)
  110. {
  111. int ret;
  112. char buf[32];
  113. uid_t uid = getuid();
  114. gid_t gid = getgid();
  115. ret = unshare(CLONE_NEWNS|CLONE_NEWUSER|CLONE_NEWPID);
  116. if (ret == -1)
  117. ksft_exit_fail_msg("unsharing mountns and userns: %s\n",
  118. strerror(errno));
  119. sprintf(buf, "0 %d 1", uid);
  120. ret = write_file("/proc/self/uid_map", buf);
  121. if (ret != NSID_PASS)
  122. return ret;
  123. ret = write_file("/proc/self/setgroups", "deny");
  124. if (ret != NSID_PASS)
  125. return ret;
  126. sprintf(buf, "0 %d 1", gid);
  127. ret = write_file("/proc/self/gid_map", buf);
  128. if (ret != NSID_PASS)
  129. return ret;
  130. ret = mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
  131. if (ret == -1) {
  132. ksft_print_msg("making mount tree private: %s\n",
  133. strerror(errno));
  134. return NSID_ERROR;
  135. }
  136. return NSID_PASS;
  137. }
  138. static int _test_statmount_mnt_ns_id(void)
  139. {
  140. struct statmount sm;
  141. uint64_t mnt_ns_id;
  142. uint64_t root_id;
  143. int ret;
  144. ret = get_mnt_ns_id("/proc/self/ns/mnt", &mnt_ns_id);
  145. if (ret != NSID_PASS)
  146. return ret;
  147. ret = get_mnt_id("/", &root_id);
  148. if (ret != NSID_PASS)
  149. return ret;
  150. ret = statmount(root_id, 0, STATMOUNT_MNT_NS_ID, &sm, sizeof(sm), 0);
  151. if (ret == -1) {
  152. ksft_print_msg("statmount mnt ns id: %s\n", strerror(errno));
  153. return NSID_ERROR;
  154. }
  155. if (sm.size != sizeof(sm)) {
  156. ksft_print_msg("unexpected size: %u != %u\n", sm.size,
  157. (uint32_t)sizeof(sm));
  158. return NSID_FAIL;
  159. }
  160. if (sm.mask != STATMOUNT_MNT_NS_ID) {
  161. ksft_print_msg("statmount mnt ns id unavailable\n");
  162. return NSID_SKIP;
  163. }
  164. if (sm.mnt_ns_id != mnt_ns_id) {
  165. ksft_print_msg("unexpected mnt ns ID: 0x%llx != 0x%llx\n",
  166. (unsigned long long)sm.mnt_ns_id,
  167. (unsigned long long)mnt_ns_id);
  168. return NSID_FAIL;
  169. }
  170. return NSID_PASS;
  171. }
  172. static void test_statmount_mnt_ns_id(void)
  173. {
  174. pid_t pid;
  175. int ret;
  176. pid = fork();
  177. if (pid < 0)
  178. ksft_exit_fail_msg("failed to fork: %s\n", strerror(errno));
  179. /* We're the original pid, wait for the result. */
  180. if (pid != 0) {
  181. ret = wait_for_pid(pid);
  182. handle_result(ret, "test statmount ns id");
  183. return;
  184. }
  185. ret = setup_namespace();
  186. if (ret != NSID_PASS)
  187. exit(ret);
  188. ret = _test_statmount_mnt_ns_id();
  189. exit(ret);
  190. }
  191. static int validate_external_listmount(pid_t pid, uint64_t child_nr_mounts)
  192. {
  193. uint64_t list[256];
  194. uint64_t mnt_ns_id;
  195. uint64_t nr_mounts;
  196. char buf[256];
  197. int ret;
  198. /* Get the mount ns id for our child. */
  199. snprintf(buf, sizeof(buf), "/proc/%lu/ns/mnt", (unsigned long)pid);
  200. ret = get_mnt_ns_id(buf, &mnt_ns_id);
  201. nr_mounts = listmount(LSMT_ROOT, mnt_ns_id, 0, list, 256, 0);
  202. if (nr_mounts == (uint64_t)-1) {
  203. ksft_print_msg("listmount: %s\n", strerror(errno));
  204. return NSID_ERROR;
  205. }
  206. if (nr_mounts != child_nr_mounts) {
  207. ksft_print_msg("listmount results is %zi != %zi\n", nr_mounts,
  208. child_nr_mounts);
  209. return NSID_FAIL;
  210. }
  211. /* Validate that all of our entries match our mnt_ns_id. */
  212. for (int i = 0; i < nr_mounts; i++) {
  213. struct statmount sm;
  214. ret = statmount(list[i], mnt_ns_id, STATMOUNT_MNT_NS_ID, &sm,
  215. sizeof(sm), 0);
  216. if (ret < 0) {
  217. ksft_print_msg("statmount mnt ns id: %s\n", strerror(errno));
  218. return NSID_ERROR;
  219. }
  220. if (sm.mask != STATMOUNT_MNT_NS_ID) {
  221. ksft_print_msg("statmount mnt ns id unavailable\n");
  222. return NSID_SKIP;
  223. }
  224. if (sm.mnt_ns_id != mnt_ns_id) {
  225. ksft_print_msg("listmount gave us the wrong ns id: 0x%llx != 0x%llx\n",
  226. (unsigned long long)sm.mnt_ns_id,
  227. (unsigned long long)mnt_ns_id);
  228. return NSID_FAIL;
  229. }
  230. }
  231. return NSID_PASS;
  232. }
  233. static void test_listmount_ns(void)
  234. {
  235. uint64_t nr_mounts;
  236. char pval;
  237. int child_ready_pipe[2];
  238. int parent_ready_pipe[2];
  239. pid_t pid;
  240. int ret, child_ret;
  241. if (pipe(child_ready_pipe) < 0)
  242. ksft_exit_fail_msg("failed to create the child pipe: %s\n",
  243. strerror(errno));
  244. if (pipe(parent_ready_pipe) < 0)
  245. ksft_exit_fail_msg("failed to create the parent pipe: %s\n",
  246. strerror(errno));
  247. pid = fork();
  248. if (pid < 0)
  249. ksft_exit_fail_msg("failed to fork: %s\n", strerror(errno));
  250. if (pid == 0) {
  251. char cval;
  252. uint64_t list[256];
  253. close(child_ready_pipe[0]);
  254. close(parent_ready_pipe[1]);
  255. ret = setup_namespace();
  256. if (ret != NSID_PASS)
  257. exit(ret);
  258. nr_mounts = listmount(LSMT_ROOT, 0, 0, list, 256, 0);
  259. if (nr_mounts == (uint64_t)-1) {
  260. ksft_print_msg("listmount: %s\n", strerror(errno));
  261. exit(NSID_FAIL);
  262. }
  263. /*
  264. * Tell our parent how many mounts we have, and then wait for it
  265. * to tell us we're done.
  266. */
  267. if (write(child_ready_pipe[1], &nr_mounts, sizeof(nr_mounts)) !=
  268. sizeof(nr_mounts))
  269. ret = NSID_ERROR;
  270. if (read(parent_ready_pipe[0], &cval, sizeof(cval)) != sizeof(cval))
  271. ret = NSID_ERROR;
  272. exit(NSID_PASS);
  273. }
  274. close(child_ready_pipe[1]);
  275. close(parent_ready_pipe[0]);
  276. /* Wait until the child has created everything. */
  277. if (read(child_ready_pipe[0], &nr_mounts, sizeof(nr_mounts)) !=
  278. sizeof(nr_mounts))
  279. ret = NSID_ERROR;
  280. ret = validate_external_listmount(pid, nr_mounts);
  281. if (write(parent_ready_pipe[1], &pval, sizeof(pval)) != sizeof(pval))
  282. ret = NSID_ERROR;
  283. child_ret = wait_for_pid(pid);
  284. if (child_ret != NSID_PASS)
  285. ret = child_ret;
  286. handle_result(ret, "test listmount ns id");
  287. }
  288. int main(void)
  289. {
  290. int ret;
  291. ksft_print_header();
  292. ret = statmount(0, 0, 0, NULL, 0, 0);
  293. assert(ret == -1);
  294. if (errno == ENOSYS)
  295. ksft_exit_skip("statmount() syscall not supported\n");
  296. ksft_set_plan(2);
  297. test_statmount_mnt_ns_id();
  298. test_listmount_ns();
  299. if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)
  300. ksft_exit_fail();
  301. else
  302. ksft_exit_pass();
  303. }