1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
|
// SPDX-License-Identifier: GPL-2.0
#include <assert.h>
#include <fcntl.h>
#include <inttypes.h>
#include <libgen.h>
#include <limits.h>
#include <pthread.h>
#include <string.h>
#include <sys/mount.h>
#include <poll.h>
#include <sys/epoll.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <linux/fs.h>
#include <linux/limits.h>
#include <linux/nsfs.h>
#include "../kselftest_harness.h"
TEST(nsid_mntns_basic)
{
__u64 mnt_ns_id = 0;
int fd_mntns;
int ret;
/* Open the current mount namespace */
fd_mntns = open("/proc/self/ns/mnt", O_RDONLY);
ASSERT_GE(fd_mntns, 0);
/* Get the mount namespace ID */
ret = ioctl(fd_mntns, NS_GET_MNTNS_ID, &mnt_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(mnt_ns_id, 0);
/* Verify we can get the same ID again */
__u64 mnt_ns_id2 = 0;
ret = ioctl(fd_mntns, NS_GET_ID, &mnt_ns_id2);
ASSERT_EQ(ret, 0);
ASSERT_EQ(mnt_ns_id, mnt_ns_id2);
close(fd_mntns);
}
TEST(nsid_mntns_separate)
{
__u64 parent_mnt_ns_id = 0;
__u64 child_mnt_ns_id = 0;
int fd_parent_mntns, fd_child_mntns;
int ret;
pid_t pid;
int pipefd[2];
/* Get parent's mount namespace ID */
fd_parent_mntns = open("/proc/self/ns/mnt", O_RDONLY);
ASSERT_GE(fd_parent_mntns, 0);
ret = ioctl(fd_parent_mntns, NS_GET_ID, &parent_mnt_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(parent_mnt_ns_id, 0);
/* Create a pipe for synchronization */
ASSERT_EQ(pipe(pipefd), 0);
pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* Child process */
close(pipefd[0]);
/* Create new mount namespace */
ret = unshare(CLONE_NEWNS);
if (ret != 0) {
/* Skip test if we don't have permission */
if (errno == EPERM || errno == EACCES) {
write(pipefd[1], "S", 1); /* Signal skip */
_exit(0);
}
_exit(1);
}
/* Signal success */
write(pipefd[1], "Y", 1);
close(pipefd[1]);
/* Keep namespace alive */
pause();
_exit(0);
}
/* Parent process */
close(pipefd[1]);
char buf;
ASSERT_EQ(read(pipefd[0], &buf, 1), 1);
close(pipefd[0]);
if (buf == 'S') {
/* Child couldn't create namespace, skip test */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
close(fd_parent_mntns);
SKIP(return, "No permission to create mount namespace");
}
ASSERT_EQ(buf, 'Y');
/* Open child's mount namespace */
char path[256];
snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
fd_child_mntns = open(path, O_RDONLY);
ASSERT_GE(fd_child_mntns, 0);
/* Get child's mount namespace ID */
ret = ioctl(fd_child_mntns, NS_GET_ID, &child_mnt_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(child_mnt_ns_id, 0);
/* Parent and child should have different mount namespace IDs */
ASSERT_NE(parent_mnt_ns_id, child_mnt_ns_id);
close(fd_parent_mntns);
close(fd_child_mntns);
/* Clean up child process */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
}
TEST(nsid_cgroupns_basic)
{
__u64 cgroup_ns_id = 0;
int fd_cgroupns;
int ret;
/* Open the current cgroup namespace */
fd_cgroupns = open("/proc/self/ns/cgroup", O_RDONLY);
ASSERT_GE(fd_cgroupns, 0);
/* Get the cgroup namespace ID */
ret = ioctl(fd_cgroupns, NS_GET_ID, &cgroup_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(cgroup_ns_id, 0);
/* Verify we can get the same ID again */
__u64 cgroup_ns_id2 = 0;
ret = ioctl(fd_cgroupns, NS_GET_ID, &cgroup_ns_id2);
ASSERT_EQ(ret, 0);
ASSERT_EQ(cgroup_ns_id, cgroup_ns_id2);
close(fd_cgroupns);
}
TEST(nsid_cgroupns_separate)
{
__u64 parent_cgroup_ns_id = 0;
__u64 child_cgroup_ns_id = 0;
int fd_parent_cgroupns, fd_child_cgroupns;
int ret;
pid_t pid;
int pipefd[2];
/* Get parent's cgroup namespace ID */
fd_parent_cgroupns = open("/proc/self/ns/cgroup", O_RDONLY);
ASSERT_GE(fd_parent_cgroupns, 0);
ret = ioctl(fd_parent_cgroupns, NS_GET_ID, &parent_cgroup_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(parent_cgroup_ns_id, 0);
/* Create a pipe for synchronization */
ASSERT_EQ(pipe(pipefd), 0);
pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* Child process */
close(pipefd[0]);
/* Create new cgroup namespace */
ret = unshare(CLONE_NEWCGROUP);
if (ret != 0) {
/* Skip test if we don't have permission */
if (errno == EPERM || errno == EACCES) {
write(pipefd[1], "S", 1); /* Signal skip */
_exit(0);
}
_exit(1);
}
/* Signal success */
write(pipefd[1], "Y", 1);
close(pipefd[1]);
/* Keep namespace alive */
pause();
_exit(0);
}
/* Parent process */
close(pipefd[1]);
char buf;
ASSERT_EQ(read(pipefd[0], &buf, 1), 1);
close(pipefd[0]);
if (buf == 'S') {
/* Child couldn't create namespace, skip test */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
close(fd_parent_cgroupns);
SKIP(return, "No permission to create cgroup namespace");
}
ASSERT_EQ(buf, 'Y');
/* Open child's cgroup namespace */
char path[256];
snprintf(path, sizeof(path), "/proc/%d/ns/cgroup", pid);
fd_child_cgroupns = open(path, O_RDONLY);
ASSERT_GE(fd_child_cgroupns, 0);
/* Get child's cgroup namespace ID */
ret = ioctl(fd_child_cgroupns, NS_GET_ID, &child_cgroup_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(child_cgroup_ns_id, 0);
/* Parent and child should have different cgroup namespace IDs */
ASSERT_NE(parent_cgroup_ns_id, child_cgroup_ns_id);
close(fd_parent_cgroupns);
close(fd_child_cgroupns);
/* Clean up child process */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
}
TEST(nsid_ipcns_basic)
{
__u64 ipc_ns_id = 0;
int fd_ipcns;
int ret;
/* Open the current IPC namespace */
fd_ipcns = open("/proc/self/ns/ipc", O_RDONLY);
ASSERT_GE(fd_ipcns, 0);
/* Get the IPC namespace ID */
ret = ioctl(fd_ipcns, NS_GET_ID, &ipc_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(ipc_ns_id, 0);
/* Verify we can get the same ID again */
__u64 ipc_ns_id2 = 0;
ret = ioctl(fd_ipcns, NS_GET_ID, &ipc_ns_id2);
ASSERT_EQ(ret, 0);
ASSERT_EQ(ipc_ns_id, ipc_ns_id2);
close(fd_ipcns);
}
TEST(nsid_ipcns_separate)
{
__u64 parent_ipc_ns_id = 0;
__u64 child_ipc_ns_id = 0;
int fd_parent_ipcns, fd_child_ipcns;
int ret;
pid_t pid;
int pipefd[2];
/* Get parent's IPC namespace ID */
fd_parent_ipcns = open("/proc/self/ns/ipc", O_RDONLY);
ASSERT_GE(fd_parent_ipcns, 0);
ret = ioctl(fd_parent_ipcns, NS_GET_ID, &parent_ipc_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(parent_ipc_ns_id, 0);
/* Create a pipe for synchronization */
ASSERT_EQ(pipe(pipefd), 0);
pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* Child process */
close(pipefd[0]);
/* Create new IPC namespace */
ret = unshare(CLONE_NEWIPC);
if (ret != 0) {
/* Skip test if we don't have permission */
if (errno == EPERM || errno == EACCES) {
write(pipefd[1], "S", 1); /* Signal skip */
_exit(0);
}
_exit(1);
}
/* Signal success */
write(pipefd[1], "Y", 1);
close(pipefd[1]);
/* Keep namespace alive */
pause();
_exit(0);
}
/* Parent process */
close(pipefd[1]);
char buf;
ASSERT_EQ(read(pipefd[0], &buf, 1), 1);
close(pipefd[0]);
if (buf == 'S') {
/* Child couldn't create namespace, skip test */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
close(fd_parent_ipcns);
SKIP(return, "No permission to create IPC namespace");
}
ASSERT_EQ(buf, 'Y');
/* Open child's IPC namespace */
char path[256];
snprintf(path, sizeof(path), "/proc/%d/ns/ipc", pid);
fd_child_ipcns = open(path, O_RDONLY);
ASSERT_GE(fd_child_ipcns, 0);
/* Get child's IPC namespace ID */
ret = ioctl(fd_child_ipcns, NS_GET_ID, &child_ipc_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(child_ipc_ns_id, 0);
/* Parent and child should have different IPC namespace IDs */
ASSERT_NE(parent_ipc_ns_id, child_ipc_ns_id);
close(fd_parent_ipcns);
close(fd_child_ipcns);
/* Clean up child process */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
}
TEST(nsid_utsns_basic)
{
__u64 uts_ns_id = 0;
int fd_utsns;
int ret;
/* Open the current UTS namespace */
fd_utsns = open("/proc/self/ns/uts", O_RDONLY);
ASSERT_GE(fd_utsns, 0);
/* Get the UTS namespace ID */
ret = ioctl(fd_utsns, NS_GET_ID, &uts_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(uts_ns_id, 0);
/* Verify we can get the same ID again */
__u64 uts_ns_id2 = 0;
ret = ioctl(fd_utsns, NS_GET_ID, &uts_ns_id2);
ASSERT_EQ(ret, 0);
ASSERT_EQ(uts_ns_id, uts_ns_id2);
close(fd_utsns);
}
TEST(nsid_utsns_separate)
{
__u64 parent_uts_ns_id = 0;
__u64 child_uts_ns_id = 0;
int fd_parent_utsns, fd_child_utsns;
int ret;
pid_t pid;
int pipefd[2];
/* Get parent's UTS namespace ID */
fd_parent_utsns = open("/proc/self/ns/uts", O_RDONLY);
ASSERT_GE(fd_parent_utsns, 0);
ret = ioctl(fd_parent_utsns, NS_GET_ID, &parent_uts_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(parent_uts_ns_id, 0);
/* Create a pipe for synchronization */
ASSERT_EQ(pipe(pipefd), 0);
pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* Child process */
close(pipefd[0]);
/* Create new UTS namespace */
ret = unshare(CLONE_NEWUTS);
if (ret != 0) {
/* Skip test if we don't have permission */
if (errno == EPERM || errno == EACCES) {
write(pipefd[1], "S", 1); /* Signal skip */
_exit(0);
}
_exit(1);
}
/* Signal success */
write(pipefd[1], "Y", 1);
close(pipefd[1]);
/* Keep namespace alive */
pause();
_exit(0);
}
/* Parent process */
close(pipefd[1]);
char buf;
ASSERT_EQ(read(pipefd[0], &buf, 1), 1);
close(pipefd[0]);
if (buf == 'S') {
/* Child couldn't create namespace, skip test */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
close(fd_parent_utsns);
SKIP(return, "No permission to create UTS namespace");
}
ASSERT_EQ(buf, 'Y');
/* Open child's UTS namespace */
char path[256];
snprintf(path, sizeof(path), "/proc/%d/ns/uts", pid);
fd_child_utsns = open(path, O_RDONLY);
ASSERT_GE(fd_child_utsns, 0);
/* Get child's UTS namespace ID */
ret = ioctl(fd_child_utsns, NS_GET_ID, &child_uts_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(child_uts_ns_id, 0);
/* Parent and child should have different UTS namespace IDs */
ASSERT_NE(parent_uts_ns_id, child_uts_ns_id);
close(fd_parent_utsns);
close(fd_child_utsns);
/* Clean up child process */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
}
TEST(nsid_userns_basic)
{
__u64 user_ns_id = 0;
int fd_userns;
int ret;
/* Open the current user namespace */
fd_userns = open("/proc/self/ns/user", O_RDONLY);
ASSERT_GE(fd_userns, 0);
/* Get the user namespace ID */
ret = ioctl(fd_userns, NS_GET_ID, &user_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(user_ns_id, 0);
/* Verify we can get the same ID again */
__u64 user_ns_id2 = 0;
ret = ioctl(fd_userns, NS_GET_ID, &user_ns_id2);
ASSERT_EQ(ret, 0);
ASSERT_EQ(user_ns_id, user_ns_id2);
close(fd_userns);
}
TEST(nsid_userns_separate)
{
__u64 parent_user_ns_id = 0;
__u64 child_user_ns_id = 0;
int fd_parent_userns, fd_child_userns;
int ret;
pid_t pid;
int pipefd[2];
/* Get parent's user namespace ID */
fd_parent_userns = open("/proc/self/ns/user", O_RDONLY);
ASSERT_GE(fd_parent_userns, 0);
ret = ioctl(fd_parent_userns, NS_GET_ID, &parent_user_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(parent_user_ns_id, 0);
/* Create a pipe for synchronization */
ASSERT_EQ(pipe(pipefd), 0);
pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* Child process */
close(pipefd[0]);
/* Create new user namespace */
ret = unshare(CLONE_NEWUSER);
if (ret != 0) {
/* Skip test if we don't have permission */
if (errno == EPERM || errno == EACCES) {
write(pipefd[1], "S", 1); /* Signal skip */
_exit(0);
}
_exit(1);
}
/* Signal success */
write(pipefd[1], "Y", 1);
close(pipefd[1]);
/* Keep namespace alive */
pause();
_exit(0);
}
/* Parent process */
close(pipefd[1]);
char buf;
ASSERT_EQ(read(pipefd[0], &buf, 1), 1);
close(pipefd[0]);
if (buf == 'S') {
/* Child couldn't create namespace, skip test */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
close(fd_parent_userns);
SKIP(return, "No permission to create user namespace");
}
ASSERT_EQ(buf, 'Y');
/* Open child's user namespace */
char path[256];
snprintf(path, sizeof(path), "/proc/%d/ns/user", pid);
fd_child_userns = open(path, O_RDONLY);
ASSERT_GE(fd_child_userns, 0);
/* Get child's user namespace ID */
ret = ioctl(fd_child_userns, NS_GET_ID, &child_user_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(child_user_ns_id, 0);
/* Parent and child should have different user namespace IDs */
ASSERT_NE(parent_user_ns_id, child_user_ns_id);
close(fd_parent_userns);
close(fd_child_userns);
/* Clean up child process */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
}
TEST(nsid_timens_basic)
{
__u64 time_ns_id = 0;
int fd_timens;
int ret;
/* Open the current time namespace */
fd_timens = open("/proc/self/ns/time", O_RDONLY);
if (fd_timens < 0) {
SKIP(return, "Time namespaces not supported");
}
/* Get the time namespace ID */
ret = ioctl(fd_timens, NS_GET_ID, &time_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(time_ns_id, 0);
/* Verify we can get the same ID again */
__u64 time_ns_id2 = 0;
ret = ioctl(fd_timens, NS_GET_ID, &time_ns_id2);
ASSERT_EQ(ret, 0);
ASSERT_EQ(time_ns_id, time_ns_id2);
close(fd_timens);
}
TEST(nsid_timens_separate)
{
__u64 parent_time_ns_id = 0;
__u64 child_time_ns_id = 0;
int fd_parent_timens, fd_child_timens;
int ret;
pid_t pid;
int pipefd[2];
/* Open the current time namespace */
fd_parent_timens = open("/proc/self/ns/time", O_RDONLY);
if (fd_parent_timens < 0) {
SKIP(return, "Time namespaces not supported");
}
/* Get parent's time namespace ID */
ret = ioctl(fd_parent_timens, NS_GET_ID, &parent_time_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(parent_time_ns_id, 0);
/* Create a pipe for synchronization */
ASSERT_EQ(pipe(pipefd), 0);
pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* Child process */
close(pipefd[0]);
/* Create new time namespace */
ret = unshare(CLONE_NEWTIME);
if (ret != 0) {
/* Skip test if we don't have permission */
if (errno == EPERM || errno == EACCES || errno == EINVAL) {
write(pipefd[1], "S", 1); /* Signal skip */
_exit(0);
}
_exit(1);
}
/* Fork a grandchild to actually enter the new namespace */
pid_t grandchild = fork();
if (grandchild == 0) {
/* Grandchild is in the new namespace */
write(pipefd[1], "Y", 1);
close(pipefd[1]);
pause();
_exit(0);
} else if (grandchild > 0) {
/* Child writes grandchild PID and waits */
write(pipefd[1], "Y", 1);
write(pipefd[1], &grandchild, sizeof(grandchild));
close(pipefd[1]);
pause(); /* Keep the parent alive to maintain the grandchild */
_exit(0);
} else {
_exit(1);
}
}
/* Parent process */
close(pipefd[1]);
char buf;
ASSERT_EQ(read(pipefd[0], &buf, 1), 1);
if (buf == 'S') {
/* Child couldn't create namespace, skip test */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
close(fd_parent_timens);
close(pipefd[0]);
SKIP(return, "Cannot create time namespace");
}
ASSERT_EQ(buf, 'Y');
pid_t grandchild_pid;
ASSERT_EQ(read(pipefd[0], &grandchild_pid, sizeof(grandchild_pid)), sizeof(grandchild_pid));
close(pipefd[0]);
/* Open grandchild's time namespace */
char path[256];
snprintf(path, sizeof(path), "/proc/%d/ns/time", grandchild_pid);
fd_child_timens = open(path, O_RDONLY);
ASSERT_GE(fd_child_timens, 0);
/* Get child's time namespace ID */
ret = ioctl(fd_child_timens, NS_GET_ID, &child_time_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(child_time_ns_id, 0);
/* Parent and child should have different time namespace IDs */
ASSERT_NE(parent_time_ns_id, child_time_ns_id);
close(fd_parent_timens);
close(fd_child_timens);
/* Clean up child process */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
}
TEST(nsid_pidns_basic)
{
__u64 pid_ns_id = 0;
int fd_pidns;
int ret;
/* Open the current PID namespace */
fd_pidns = open("/proc/self/ns/pid", O_RDONLY);
ASSERT_GE(fd_pidns, 0);
/* Get the PID namespace ID */
ret = ioctl(fd_pidns, NS_GET_ID, &pid_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(pid_ns_id, 0);
/* Verify we can get the same ID again */
__u64 pid_ns_id2 = 0;
ret = ioctl(fd_pidns, NS_GET_ID, &pid_ns_id2);
ASSERT_EQ(ret, 0);
ASSERT_EQ(pid_ns_id, pid_ns_id2);
close(fd_pidns);
}
TEST(nsid_pidns_separate)
{
__u64 parent_pid_ns_id = 0;
__u64 child_pid_ns_id = 0;
int fd_parent_pidns, fd_child_pidns;
int ret;
pid_t pid;
int pipefd[2];
/* Get parent's PID namespace ID */
fd_parent_pidns = open("/proc/self/ns/pid", O_RDONLY);
ASSERT_GE(fd_parent_pidns, 0);
ret = ioctl(fd_parent_pidns, NS_GET_ID, &parent_pid_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(parent_pid_ns_id, 0);
/* Create a pipe for synchronization */
ASSERT_EQ(pipe(pipefd), 0);
pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* Child process */
close(pipefd[0]);
/* Create new PID namespace */
ret = unshare(CLONE_NEWPID);
if (ret != 0) {
/* Skip test if we don't have permission */
if (errno == EPERM || errno == EACCES) {
write(pipefd[1], "S", 1); /* Signal skip */
_exit(0);
}
_exit(1);
}
/* Fork a grandchild to actually enter the new namespace */
pid_t grandchild = fork();
if (grandchild == 0) {
/* Grandchild is in the new namespace */
write(pipefd[1], "Y", 1);
close(pipefd[1]);
pause();
_exit(0);
} else if (grandchild > 0) {
/* Child writes grandchild PID and waits */
write(pipefd[1], "Y", 1);
write(pipefd[1], &grandchild, sizeof(grandchild));
close(pipefd[1]);
pause(); /* Keep the parent alive to maintain the grandchild */
_exit(0);
} else {
_exit(1);
}
}
/* Parent process */
close(pipefd[1]);
char buf;
ASSERT_EQ(read(pipefd[0], &buf, 1), 1);
if (buf == 'S') {
/* Child couldn't create namespace, skip test */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
close(fd_parent_pidns);
close(pipefd[0]);
SKIP(return, "No permission to create PID namespace");
}
ASSERT_EQ(buf, 'Y');
pid_t grandchild_pid;
ASSERT_EQ(read(pipefd[0], &grandchild_pid, sizeof(grandchild_pid)), sizeof(grandchild_pid));
close(pipefd[0]);
/* Open grandchild's PID namespace */
char path[256];
snprintf(path, sizeof(path), "/proc/%d/ns/pid", grandchild_pid);
fd_child_pidns = open(path, O_RDONLY);
ASSERT_GE(fd_child_pidns, 0);
/* Get child's PID namespace ID */
ret = ioctl(fd_child_pidns, NS_GET_ID, &child_pid_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(child_pid_ns_id, 0);
/* Parent and child should have different PID namespace IDs */
ASSERT_NE(parent_pid_ns_id, child_pid_ns_id);
close(fd_parent_pidns);
close(fd_child_pidns);
/* Clean up child process */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
}
TEST(nsid_netns_basic)
{
__u64 net_ns_id = 0;
__u64 netns_cookie = 0;
int fd_netns;
int sock;
socklen_t optlen;
int ret;
/* Open the current network namespace */
fd_netns = open("/proc/self/ns/net", O_RDONLY);
ASSERT_GE(fd_netns, 0);
/* Get the network namespace ID via ioctl */
ret = ioctl(fd_netns, NS_GET_ID, &net_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(net_ns_id, 0);
/* Create a socket to get the SO_NETNS_COOKIE */
sock = socket(AF_UNIX, SOCK_STREAM, 0);
ASSERT_GE(sock, 0);
/* Get the network namespace cookie via socket option */
optlen = sizeof(netns_cookie);
ret = getsockopt(sock, SOL_SOCKET, SO_NETNS_COOKIE, &netns_cookie, &optlen);
ASSERT_EQ(ret, 0);
ASSERT_EQ(optlen, sizeof(netns_cookie));
/* The namespace ID and cookie should be identical */
ASSERT_EQ(net_ns_id, netns_cookie);
/* Verify we can get the same ID again */
__u64 net_ns_id2 = 0;
ret = ioctl(fd_netns, NS_GET_ID, &net_ns_id2);
ASSERT_EQ(ret, 0);
ASSERT_EQ(net_ns_id, net_ns_id2);
close(sock);
close(fd_netns);
}
TEST(nsid_netns_separate)
{
__u64 parent_net_ns_id = 0;
__u64 parent_netns_cookie = 0;
__u64 child_net_ns_id = 0;
__u64 child_netns_cookie = 0;
int fd_parent_netns, fd_child_netns;
int parent_sock, child_sock;
socklen_t optlen;
int ret;
pid_t pid;
int pipefd[2];
/* Get parent's network namespace ID */
fd_parent_netns = open("/proc/self/ns/net", O_RDONLY);
ASSERT_GE(fd_parent_netns, 0);
ret = ioctl(fd_parent_netns, NS_GET_ID, &parent_net_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(parent_net_ns_id, 0);
/* Get parent's network namespace cookie */
parent_sock = socket(AF_UNIX, SOCK_STREAM, 0);
ASSERT_GE(parent_sock, 0);
optlen = sizeof(parent_netns_cookie);
ret = getsockopt(parent_sock, SOL_SOCKET, SO_NETNS_COOKIE, &parent_netns_cookie, &optlen);
ASSERT_EQ(ret, 0);
/* Verify parent's ID and cookie match */
ASSERT_EQ(parent_net_ns_id, parent_netns_cookie);
/* Create a pipe for synchronization */
ASSERT_EQ(pipe(pipefd), 0);
pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* Child process */
close(pipefd[0]);
/* Create new network namespace */
ret = unshare(CLONE_NEWNET);
if (ret != 0) {
/* Skip test if we don't have permission */
if (errno == EPERM || errno == EACCES) {
write(pipefd[1], "S", 1); /* Signal skip */
_exit(0);
}
_exit(1);
}
/* Signal success */
write(pipefd[1], "Y", 1);
close(pipefd[1]);
/* Keep namespace alive */
pause();
_exit(0);
}
/* Parent process */
close(pipefd[1]);
char buf;
ASSERT_EQ(read(pipefd[0], &buf, 1), 1);
close(pipefd[0]);
if (buf == 'S') {
/* Child couldn't create namespace, skip test */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
close(fd_parent_netns);
close(parent_sock);
SKIP(return, "No permission to create network namespace");
}
ASSERT_EQ(buf, 'Y');
/* Open child's network namespace */
char path[256];
snprintf(path, sizeof(path), "/proc/%d/ns/net", pid);
fd_child_netns = open(path, O_RDONLY);
ASSERT_GE(fd_child_netns, 0);
/* Get child's network namespace ID */
ret = ioctl(fd_child_netns, NS_GET_ID, &child_net_ns_id);
ASSERT_EQ(ret, 0);
ASSERT_NE(child_net_ns_id, 0);
/* Create socket in child's namespace to get cookie */
ret = setns(fd_child_netns, CLONE_NEWNET);
if (ret == 0) {
child_sock = socket(AF_UNIX, SOCK_STREAM, 0);
ASSERT_GE(child_sock, 0);
optlen = sizeof(child_netns_cookie);
ret = getsockopt(child_sock, SOL_SOCKET, SO_NETNS_COOKIE, &child_netns_cookie, &optlen);
ASSERT_EQ(ret, 0);
/* Verify child's ID and cookie match */
ASSERT_EQ(child_net_ns_id, child_netns_cookie);
close(child_sock);
/* Return to parent namespace */
setns(fd_parent_netns, CLONE_NEWNET);
}
/* Parent and child should have different network namespace IDs */
ASSERT_NE(parent_net_ns_id, child_net_ns_id);
if (child_netns_cookie != 0) {
ASSERT_NE(parent_netns_cookie, child_netns_cookie);
}
close(fd_parent_netns);
close(fd_child_netns);
close(parent_sock);
/* Clean up child process */
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
}
TEST_HARNESS_MAIN
|