listns_test.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <sched.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <linux/nsfs.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/socket.h>
  13. #include <sys/stat.h>
  14. #include <sys/syscall.h>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. #include <unistd.h>
  18. #include "../kselftest_harness.h"
  19. #include "../filesystems/utils.h"
  20. #include "wrappers.h"
  21. /*
  22. * Test basic listns() functionality with the unified namespace tree.
  23. * List all active namespaces globally.
  24. */
  25. TEST(listns_basic_unified)
  26. {
  27. struct ns_id_req req = {
  28. .size = sizeof(req),
  29. .spare = 0,
  30. .ns_id = 0,
  31. .ns_type = 0, /* All types */
  32. .spare2 = 0,
  33. .user_ns_id = 0, /* Global listing */
  34. };
  35. __u64 ns_ids[100];
  36. ssize_t ret;
  37. ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
  38. if (ret < 0) {
  39. if (errno == ENOSYS)
  40. SKIP(return, "listns() not supported");
  41. TH_LOG("listns failed: %s (errno=%d)", strerror(errno), errno);
  42. ASSERT_TRUE(false);
  43. }
  44. /* Should find at least the initial namespaces */
  45. ASSERT_GT(ret, 0);
  46. TH_LOG("Found %zd active namespaces", ret);
  47. /* Verify all returned IDs are non-zero */
  48. for (ssize_t i = 0; i < ret; i++) {
  49. ASSERT_NE(ns_ids[i], 0);
  50. TH_LOG(" [%zd] ns_id: %llu", i, (unsigned long long)ns_ids[i]);
  51. }
  52. }
  53. /*
  54. * Test listns() with type filtering.
  55. * List only network namespaces.
  56. */
  57. TEST(listns_filter_by_type)
  58. {
  59. struct ns_id_req req = {
  60. .size = sizeof(req),
  61. .spare = 0,
  62. .ns_id = 0,
  63. .ns_type = CLONE_NEWNET, /* Only network namespaces */
  64. .spare2 = 0,
  65. .user_ns_id = 0,
  66. };
  67. __u64 ns_ids[100];
  68. ssize_t ret;
  69. ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
  70. if (ret < 0) {
  71. if (errno == ENOSYS)
  72. SKIP(return, "listns() not supported");
  73. TH_LOG("listns failed: %s (errno=%d)", strerror(errno), errno);
  74. ASSERT_TRUE(false);
  75. }
  76. ASSERT_GE(ret, 0);
  77. /* Should find at least init_net */
  78. ASSERT_GT(ret, 0);
  79. TH_LOG("Found %zd active network namespaces", ret);
  80. /* Verify we can open each namespace and it's actually a network namespace */
  81. for (ssize_t i = 0; i < ret && i < 5; i++) {
  82. struct nsfs_file_handle nsfh = {
  83. .ns_id = ns_ids[i],
  84. .ns_type = CLONE_NEWNET,
  85. .ns_inum = 0,
  86. };
  87. struct file_handle *fh;
  88. int fd;
  89. fh = (struct file_handle *)malloc(sizeof(*fh) + sizeof(nsfh));
  90. ASSERT_NE(fh, NULL);
  91. fh->handle_bytes = sizeof(nsfh);
  92. fh->handle_type = 0;
  93. memcpy(fh->f_handle, &nsfh, sizeof(nsfh));
  94. fd = open_by_handle_at(-10003, fh, O_RDONLY);
  95. free(fh);
  96. if (fd >= 0) {
  97. int ns_type;
  98. /* Verify it's a network namespace via ioctl */
  99. ns_type = ioctl(fd, NS_GET_NSTYPE);
  100. if (ns_type >= 0) {
  101. ASSERT_EQ(ns_type, CLONE_NEWNET);
  102. }
  103. close(fd);
  104. }
  105. }
  106. }
  107. /*
  108. * Test listns() pagination.
  109. * List namespaces in batches.
  110. */
  111. TEST(listns_pagination)
  112. {
  113. struct ns_id_req req = {
  114. .size = sizeof(req),
  115. .spare = 0,
  116. .ns_id = 0,
  117. .ns_type = 0,
  118. .spare2 = 0,
  119. .user_ns_id = 0,
  120. };
  121. __u64 batch1[2], batch2[2];
  122. ssize_t ret1, ret2;
  123. /* Get first batch */
  124. ret1 = sys_listns(&req, batch1, ARRAY_SIZE(batch1), 0);
  125. if (ret1 < 0) {
  126. if (errno == ENOSYS)
  127. SKIP(return, "listns() not supported");
  128. TH_LOG("listns failed: %s (errno=%d)", strerror(errno), errno);
  129. ASSERT_TRUE(false);
  130. }
  131. ASSERT_GE(ret1, 0);
  132. if (ret1 == 0)
  133. SKIP(return, "No namespaces found");
  134. TH_LOG("First batch: %zd namespaces", ret1);
  135. /* Get second batch using last ID from first batch */
  136. if (ret1 == ARRAY_SIZE(batch1)) {
  137. req.ns_id = batch1[ret1 - 1];
  138. ret2 = sys_listns(&req, batch2, ARRAY_SIZE(batch2), 0);
  139. ASSERT_GE(ret2, 0);
  140. TH_LOG("Second batch: %zd namespaces (after ns_id=%llu)",
  141. ret2, (unsigned long long)req.ns_id);
  142. /* If we got more results, verify IDs are monotonically increasing */
  143. if (ret2 > 0) {
  144. ASSERT_GT(batch2[0], batch1[ret1 - 1]);
  145. TH_LOG("Pagination working: %llu > %llu",
  146. (unsigned long long)batch2[0],
  147. (unsigned long long)batch1[ret1 - 1]);
  148. }
  149. } else {
  150. TH_LOG("All namespaces fit in first batch");
  151. }
  152. }
  153. /*
  154. * Test listns() with LISTNS_CURRENT_USER.
  155. * List namespaces owned by current user namespace.
  156. */
  157. TEST(listns_current_user)
  158. {
  159. struct ns_id_req req = {
  160. .size = sizeof(req),
  161. .spare = 0,
  162. .ns_id = 0,
  163. .ns_type = 0,
  164. .spare2 = 0,
  165. .user_ns_id = LISTNS_CURRENT_USER,
  166. };
  167. __u64 ns_ids[100];
  168. ssize_t ret;
  169. ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
  170. if (ret < 0) {
  171. if (errno == ENOSYS)
  172. SKIP(return, "listns() not supported");
  173. TH_LOG("listns failed: %s (errno=%d)", strerror(errno), errno);
  174. ASSERT_TRUE(false);
  175. }
  176. ASSERT_GE(ret, 0);
  177. /* Should find at least the initial namespaces if we're in init_user_ns */
  178. TH_LOG("Found %zd namespaces owned by current user namespace", ret);
  179. for (ssize_t i = 0; i < ret; i++)
  180. TH_LOG(" [%zd] ns_id: %llu", i, (unsigned long long)ns_ids[i]);
  181. }
  182. /*
  183. * Test that listns() only returns active namespaces.
  184. * Create a namespace, let it become inactive, verify it's not listed.
  185. */
  186. TEST(listns_only_active)
  187. {
  188. struct ns_id_req req = {
  189. .size = sizeof(req),
  190. .spare = 0,
  191. .ns_id = 0,
  192. .ns_type = CLONE_NEWNET,
  193. .spare2 = 0,
  194. .user_ns_id = 0,
  195. };
  196. __u64 ns_ids_before[100], ns_ids_after[100];
  197. ssize_t ret_before, ret_after;
  198. int pipefd[2];
  199. pid_t pid;
  200. __u64 new_ns_id = 0;
  201. int status;
  202. /* Get initial list */
  203. ret_before = sys_listns(&req, ns_ids_before, ARRAY_SIZE(ns_ids_before), 0);
  204. if (ret_before < 0) {
  205. if (errno == ENOSYS)
  206. SKIP(return, "listns() not supported");
  207. TH_LOG("listns failed: %s (errno=%d)", strerror(errno), errno);
  208. ASSERT_TRUE(false);
  209. }
  210. ASSERT_GE(ret_before, 0);
  211. TH_LOG("Before: %zd active network namespaces", ret_before);
  212. /* Create a new namespace in a child process and get its ID */
  213. ASSERT_EQ(pipe(pipefd), 0);
  214. pid = fork();
  215. ASSERT_GE(pid, 0);
  216. if (pid == 0) {
  217. int fd;
  218. __u64 ns_id;
  219. close(pipefd[0]);
  220. /* Create new network namespace */
  221. if (unshare(CLONE_NEWNET) < 0) {
  222. close(pipefd[1]);
  223. exit(1);
  224. }
  225. /* Get its ID */
  226. fd = open("/proc/self/ns/net", O_RDONLY);
  227. if (fd < 0) {
  228. close(pipefd[1]);
  229. exit(1);
  230. }
  231. if (ioctl(fd, NS_GET_ID, &ns_id) < 0) {
  232. close(fd);
  233. close(pipefd[1]);
  234. exit(1);
  235. }
  236. close(fd);
  237. /* Send ID to parent */
  238. write(pipefd[1], &ns_id, sizeof(ns_id));
  239. close(pipefd[1]);
  240. /* Keep namespace active briefly */
  241. usleep(100000);
  242. exit(0);
  243. }
  244. /* Parent reads the new namespace ID */
  245. {
  246. int bytes;
  247. close(pipefd[1]);
  248. bytes = read(pipefd[0], &new_ns_id, sizeof(new_ns_id));
  249. close(pipefd[0]);
  250. if (bytes == sizeof(new_ns_id)) {
  251. __u64 ns_ids_during[100];
  252. int ret_during;
  253. TH_LOG("Child created namespace with ID %llu", (unsigned long long)new_ns_id);
  254. /* List namespaces while child is still alive - should see new one */
  255. ret_during = sys_listns(&req, ns_ids_during, ARRAY_SIZE(ns_ids_during), 0);
  256. ASSERT_GE(ret_during, 0);
  257. TH_LOG("During: %d active network namespaces", ret_during);
  258. /* Should have more namespaces than before */
  259. ASSERT_GE(ret_during, ret_before);
  260. }
  261. }
  262. /* Wait for child to exit */
  263. waitpid(pid, &status, 0);
  264. /* Give time for namespace to become inactive */
  265. usleep(100000);
  266. /* List namespaces after child exits - should not see new one */
  267. ret_after = sys_listns(&req, ns_ids_after, ARRAY_SIZE(ns_ids_after), 0);
  268. ASSERT_GE(ret_after, 0);
  269. TH_LOG("After: %zd active network namespaces", ret_after);
  270. /* Verify the new namespace ID is not in the after list */
  271. if (new_ns_id != 0) {
  272. bool found = false;
  273. for (ssize_t i = 0; i < ret_after; i++) {
  274. if (ns_ids_after[i] == new_ns_id) {
  275. found = true;
  276. break;
  277. }
  278. }
  279. ASSERT_FALSE(found);
  280. }
  281. }
  282. /*
  283. * Test listns() with specific user namespace ID.
  284. * Create a user namespace and list namespaces it owns.
  285. */
  286. TEST(listns_specific_userns)
  287. {
  288. struct ns_id_req req = {
  289. .size = sizeof(req),
  290. .spare = 0,
  291. .ns_id = 0,
  292. .ns_type = 0,
  293. .spare2 = 0,
  294. .user_ns_id = 0, /* Will be filled with created userns ID */
  295. };
  296. __u64 ns_ids[100];
  297. int sv[2];
  298. pid_t pid;
  299. int status;
  300. __u64 user_ns_id = 0;
  301. int bytes;
  302. ssize_t ret;
  303. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
  304. pid = fork();
  305. ASSERT_GE(pid, 0);
  306. if (pid == 0) {
  307. int fd;
  308. __u64 ns_id;
  309. char buf;
  310. close(sv[0]);
  311. /* Create new user namespace */
  312. if (setup_userns() < 0) {
  313. close(sv[1]);
  314. exit(1);
  315. }
  316. /* Get user namespace ID */
  317. fd = open("/proc/self/ns/user", O_RDONLY);
  318. if (fd < 0) {
  319. close(sv[1]);
  320. exit(1);
  321. }
  322. if (ioctl(fd, NS_GET_ID, &ns_id) < 0) {
  323. close(fd);
  324. close(sv[1]);
  325. exit(1);
  326. }
  327. close(fd);
  328. /* Send ID to parent */
  329. if (write(sv[1], &ns_id, sizeof(ns_id)) != sizeof(ns_id)) {
  330. close(sv[1]);
  331. exit(1);
  332. }
  333. /* Create some namespaces owned by this user namespace */
  334. unshare(CLONE_NEWNET);
  335. unshare(CLONE_NEWUTS);
  336. /* Wait for parent signal */
  337. if (read(sv[1], &buf, 1) != 1) {
  338. close(sv[1]);
  339. exit(1);
  340. }
  341. close(sv[1]);
  342. exit(0);
  343. }
  344. /* Parent */
  345. close(sv[1]);
  346. bytes = read(sv[0], &user_ns_id, sizeof(user_ns_id));
  347. if (bytes != sizeof(user_ns_id)) {
  348. close(sv[0]);
  349. kill(pid, SIGKILL);
  350. waitpid(pid, NULL, 0);
  351. SKIP(return, "Failed to get user namespace ID from child");
  352. }
  353. TH_LOG("Child created user namespace with ID %llu", (unsigned long long)user_ns_id);
  354. /* List namespaces owned by this user namespace */
  355. req.user_ns_id = user_ns_id;
  356. ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
  357. if (ret < 0) {
  358. TH_LOG("listns failed: %s (errno=%d)", strerror(errno), errno);
  359. close(sv[0]);
  360. kill(pid, SIGKILL);
  361. waitpid(pid, NULL, 0);
  362. if (errno == ENOSYS) {
  363. SKIP(return, "listns() not supported");
  364. }
  365. ASSERT_GE(ret, 0);
  366. }
  367. TH_LOG("Found %zd namespaces owned by user namespace %llu", ret,
  368. (unsigned long long)user_ns_id);
  369. /* Should find at least the network and UTS namespaces we created */
  370. if (ret > 0) {
  371. for (ssize_t i = 0; i < ret && i < 10; i++)
  372. TH_LOG(" [%zd] ns_id: %llu", i, (unsigned long long)ns_ids[i]);
  373. }
  374. /* Signal child to exit */
  375. if (write(sv[0], "X", 1) != 1) {
  376. close(sv[0]);
  377. kill(pid, SIGKILL);
  378. waitpid(pid, NULL, 0);
  379. ASSERT_TRUE(false);
  380. }
  381. close(sv[0]);
  382. waitpid(pid, &status, 0);
  383. }
  384. /*
  385. * Test listns() with multiple namespace types filter.
  386. */
  387. TEST(listns_multiple_types)
  388. {
  389. struct ns_id_req req = {
  390. .size = sizeof(req),
  391. .spare = 0,
  392. .ns_id = 0,
  393. .ns_type = CLONE_NEWNET | CLONE_NEWUTS, /* Network and UTS */
  394. .spare2 = 0,
  395. .user_ns_id = 0,
  396. };
  397. __u64 ns_ids[100];
  398. ssize_t ret;
  399. ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
  400. if (ret < 0) {
  401. if (errno == ENOSYS)
  402. SKIP(return, "listns() not supported");
  403. TH_LOG("listns failed: %s (errno=%d)", strerror(errno), errno);
  404. ASSERT_TRUE(false);
  405. }
  406. ASSERT_GE(ret, 0);
  407. TH_LOG("Found %zd active network/UTS namespaces", ret);
  408. for (ssize_t i = 0; i < ret; i++)
  409. TH_LOG(" [%zd] ns_id: %llu", i, (unsigned long long)ns_ids[i]);
  410. }
  411. /*
  412. * Test that hierarchical active reference propagation keeps parent
  413. * user namespaces visible in listns().
  414. */
  415. TEST(listns_hierarchical_visibility)
  416. {
  417. struct ns_id_req req = {
  418. .size = sizeof(req),
  419. .spare = 0,
  420. .ns_id = 0,
  421. .ns_type = CLONE_NEWUSER,
  422. .spare2 = 0,
  423. .user_ns_id = 0,
  424. };
  425. __u64 parent_ns_id = 0, child_ns_id = 0;
  426. int sv[2];
  427. pid_t pid;
  428. int status;
  429. int bytes;
  430. __u64 ns_ids[100];
  431. ssize_t ret;
  432. bool found_parent, found_child;
  433. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
  434. pid = fork();
  435. ASSERT_GE(pid, 0);
  436. if (pid == 0) {
  437. int fd;
  438. char buf;
  439. close(sv[0]);
  440. /* Create parent user namespace */
  441. if (setup_userns() < 0) {
  442. close(sv[1]);
  443. exit(1);
  444. }
  445. fd = open("/proc/self/ns/user", O_RDONLY);
  446. if (fd < 0) {
  447. close(sv[1]);
  448. exit(1);
  449. }
  450. if (ioctl(fd, NS_GET_ID, &parent_ns_id) < 0) {
  451. close(fd);
  452. close(sv[1]);
  453. exit(1);
  454. }
  455. close(fd);
  456. /* Create child user namespace */
  457. if (setup_userns() < 0) {
  458. close(sv[1]);
  459. exit(1);
  460. }
  461. fd = open("/proc/self/ns/user", O_RDONLY);
  462. if (fd < 0) {
  463. close(sv[1]);
  464. exit(1);
  465. }
  466. if (ioctl(fd, NS_GET_ID, &child_ns_id) < 0) {
  467. close(fd);
  468. close(sv[1]);
  469. exit(1);
  470. }
  471. close(fd);
  472. /* Send both IDs to parent */
  473. if (write(sv[1], &parent_ns_id, sizeof(parent_ns_id)) != sizeof(parent_ns_id)) {
  474. close(sv[1]);
  475. exit(1);
  476. }
  477. if (write(sv[1], &child_ns_id, sizeof(child_ns_id)) != sizeof(child_ns_id)) {
  478. close(sv[1]);
  479. exit(1);
  480. }
  481. /* Wait for parent signal */
  482. if (read(sv[1], &buf, 1) != 1) {
  483. close(sv[1]);
  484. exit(1);
  485. }
  486. close(sv[1]);
  487. exit(0);
  488. }
  489. /* Parent */
  490. close(sv[1]);
  491. /* Read both namespace IDs */
  492. bytes = read(sv[0], &parent_ns_id, sizeof(parent_ns_id));
  493. bytes += read(sv[0], &child_ns_id, sizeof(child_ns_id));
  494. if (bytes != (int)(2 * sizeof(__u64))) {
  495. close(sv[0]);
  496. kill(pid, SIGKILL);
  497. waitpid(pid, NULL, 0);
  498. SKIP(return, "Failed to get namespace IDs from child");
  499. }
  500. TH_LOG("Parent user namespace ID: %llu", (unsigned long long)parent_ns_id);
  501. TH_LOG("Child user namespace ID: %llu", (unsigned long long)child_ns_id);
  502. /* List all user namespaces */
  503. ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
  504. if (ret < 0 && errno == ENOSYS) {
  505. close(sv[0]);
  506. kill(pid, SIGKILL);
  507. waitpid(pid, NULL, 0);
  508. SKIP(return, "listns() not supported");
  509. }
  510. ASSERT_GE(ret, 0);
  511. TH_LOG("Found %zd active user namespaces", ret);
  512. /* Both parent and child should be visible (active due to child process) */
  513. found_parent = false;
  514. found_child = false;
  515. for (ssize_t i = 0; i < ret; i++) {
  516. if (ns_ids[i] == parent_ns_id)
  517. found_parent = true;
  518. if (ns_ids[i] == child_ns_id)
  519. found_child = true;
  520. }
  521. TH_LOG("Parent namespace %s, child namespace %s",
  522. found_parent ? "found" : "NOT FOUND",
  523. found_child ? "found" : "NOT FOUND");
  524. ASSERT_TRUE(found_child);
  525. /* With hierarchical propagation, parent should also be active */
  526. ASSERT_TRUE(found_parent);
  527. /* Signal child to exit */
  528. if (write(sv[0], "X", 1) != 1) {
  529. close(sv[0]);
  530. kill(pid, SIGKILL);
  531. waitpid(pid, NULL, 0);
  532. ASSERT_TRUE(false);
  533. }
  534. close(sv[0]);
  535. waitpid(pid, &status, 0);
  536. }
  537. /*
  538. * Test error cases for listns().
  539. */
  540. TEST(listns_error_cases)
  541. {
  542. struct ns_id_req req = {
  543. .size = sizeof(req),
  544. .spare = 0,
  545. .ns_id = 0,
  546. .ns_type = 0,
  547. .spare2 = 0,
  548. .user_ns_id = 0,
  549. };
  550. __u64 ns_ids[10];
  551. int ret;
  552. /* Test with invalid flags */
  553. ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0xFFFF);
  554. if (errno == ENOSYS) {
  555. /* listns() not supported, skip this check */
  556. } else {
  557. ASSERT_LT(ret, 0);
  558. ASSERT_EQ(errno, EINVAL);
  559. }
  560. /* Test with NULL ns_ids array */
  561. ret = sys_listns(&req, NULL, 10, 0);
  562. ASSERT_LT(ret, 0);
  563. /* Test with invalid spare field */
  564. req.spare = 1;
  565. ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
  566. if (errno == ENOSYS) {
  567. /* listns() not supported, skip this check */
  568. } else {
  569. ASSERT_LT(ret, 0);
  570. ASSERT_EQ(errno, EINVAL);
  571. }
  572. req.spare = 0;
  573. /* Test with huge nr_ns_ids */
  574. ret = sys_listns(&req, ns_ids, 2000000, 0);
  575. if (errno == ENOSYS) {
  576. /* listns() not supported, skip this check */
  577. } else {
  578. ASSERT_LT(ret, 0);
  579. }
  580. }
  581. TEST_HARNESS_MAIN