setns-sysvipc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright © 2019 Alexey Dobriyan <adobriyan@gmail.com>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /*
  17. * Test that setns(CLONE_NEWIPC) points to new /proc/sysvipc content even
  18. * if old one is in dcache.
  19. */
  20. #undef NDEBUG
  21. #include <assert.h>
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <sched.h>
  25. #include <signal.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #include <sys/ipc.h>
  33. #include <sys/shm.h>
  34. static pid_t pid = -1;
  35. static void f(void)
  36. {
  37. if (pid > 0) {
  38. kill(pid, SIGTERM);
  39. }
  40. }
  41. int main(void)
  42. {
  43. int fd[2];
  44. char _ = 0;
  45. int nsfd;
  46. atexit(f);
  47. /* Check for priviledges and syscall availability straight away. */
  48. if (unshare(CLONE_NEWIPC) == -1) {
  49. if (errno == ENOSYS || errno == EPERM) {
  50. return 4;
  51. }
  52. return 1;
  53. }
  54. /* Distinguisher between two otherwise empty IPC namespaces. */
  55. if (shmget(IPC_PRIVATE, 1, IPC_CREAT) == -1) {
  56. return 1;
  57. }
  58. if (pipe(fd) == -1) {
  59. return 1;
  60. }
  61. pid = fork();
  62. if (pid == -1) {
  63. return 1;
  64. }
  65. if (pid == 0) {
  66. if (unshare(CLONE_NEWIPC) == -1) {
  67. return 1;
  68. }
  69. if (write(fd[1], &_, 1) != 1) {
  70. return 1;
  71. }
  72. pause();
  73. return 0;
  74. }
  75. if (read(fd[0], &_, 1) != 1) {
  76. return 1;
  77. }
  78. {
  79. char buf[64];
  80. snprintf(buf, sizeof(buf), "/proc/%u/ns/ipc", pid);
  81. nsfd = open(buf, O_RDONLY);
  82. if (nsfd == -1) {
  83. return 1;
  84. }
  85. }
  86. /* Reliably pin dentry into dcache. */
  87. (void)open("/proc/sysvipc/shm", O_RDONLY);
  88. if (setns(nsfd, CLONE_NEWIPC) == -1) {
  89. return 1;
  90. }
  91. kill(pid, SIGTERM);
  92. pid = 0;
  93. {
  94. char buf[4096];
  95. ssize_t rv;
  96. int fd;
  97. fd = open("/proc/sysvipc/shm", O_RDONLY);
  98. if (fd == -1) {
  99. return 1;
  100. }
  101. #define S32 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n"
  102. #define S64 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n"
  103. rv = read(fd, buf, sizeof(buf));
  104. if (rv == strlen(S32)) {
  105. assert(memcmp(buf, S32, strlen(S32)) == 0);
  106. } else if (rv == strlen(S64)) {
  107. assert(memcmp(buf, S64, strlen(S64)) == 0);
  108. } else {
  109. assert(0);
  110. }
  111. }
  112. return 0;
  113. }