unprivileged-remount-test.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #define _GNU_SOURCE
  2. #include <sched.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/mount.h>
  8. #include <sys/wait.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <grp.h>
  13. #include <stdbool.h>
  14. #include <stdarg.h>
  15. #ifndef CLONE_NEWNS
  16. # define CLONE_NEWNS 0x00020000
  17. #endif
  18. #ifndef CLONE_NEWUTS
  19. # define CLONE_NEWUTS 0x04000000
  20. #endif
  21. #ifndef CLONE_NEWIPC
  22. # define CLONE_NEWIPC 0x08000000
  23. #endif
  24. #ifndef CLONE_NEWNET
  25. # define CLONE_NEWNET 0x40000000
  26. #endif
  27. #ifndef CLONE_NEWUSER
  28. # define CLONE_NEWUSER 0x10000000
  29. #endif
  30. #ifndef CLONE_NEWPID
  31. # define CLONE_NEWPID 0x20000000
  32. #endif
  33. #ifndef MS_RELATIME
  34. #define MS_RELATIME (1 << 21)
  35. #endif
  36. #ifndef MS_STRICTATIME
  37. #define MS_STRICTATIME (1 << 24)
  38. #endif
  39. static void die(char *fmt, ...)
  40. {
  41. va_list ap;
  42. va_start(ap, fmt);
  43. vfprintf(stderr, fmt, ap);
  44. va_end(ap);
  45. exit(EXIT_FAILURE);
  46. }
  47. static void write_file(char *filename, char *fmt, ...)
  48. {
  49. char buf[4096];
  50. int fd;
  51. ssize_t written;
  52. int buf_len;
  53. va_list ap;
  54. va_start(ap, fmt);
  55. buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
  56. va_end(ap);
  57. if (buf_len < 0) {
  58. die("vsnprintf failed: %s\n",
  59. strerror(errno));
  60. }
  61. if (buf_len >= sizeof(buf)) {
  62. die("vsnprintf output truncated\n");
  63. }
  64. fd = open(filename, O_WRONLY);
  65. if (fd < 0) {
  66. die("open of %s failed: %s\n",
  67. filename, strerror(errno));
  68. }
  69. written = write(fd, buf, buf_len);
  70. if (written != buf_len) {
  71. if (written >= 0) {
  72. die("short write to %s\n", filename);
  73. } else {
  74. die("write to %s failed: %s\n",
  75. filename, strerror(errno));
  76. }
  77. }
  78. if (close(fd) != 0) {
  79. die("close of %s failed: %s\n",
  80. filename, strerror(errno));
  81. }
  82. }
  83. static void create_and_enter_userns(void)
  84. {
  85. uid_t uid;
  86. gid_t gid;
  87. uid = getuid();
  88. gid = getgid();
  89. if (unshare(CLONE_NEWUSER) !=0) {
  90. die("unshare(CLONE_NEWUSER) failed: %s\n",
  91. strerror(errno));
  92. }
  93. write_file("/proc/self/uid_map", "0 %d 1", uid);
  94. write_file("/proc/self/gid_map", "0 %d 1", gid);
  95. if (setgroups(0, NULL) != 0) {
  96. die("setgroups failed: %s\n",
  97. strerror(errno));
  98. }
  99. if (setgid(0) != 0) {
  100. die ("setgid(0) failed %s\n",
  101. strerror(errno));
  102. }
  103. if (setuid(0) != 0) {
  104. die("setuid(0) failed %s\n",
  105. strerror(errno));
  106. }
  107. }
  108. static
  109. bool test_unpriv_remount(int mount_flags, int remount_flags, int invalid_flags)
  110. {
  111. pid_t child;
  112. child = fork();
  113. if (child == -1) {
  114. die("fork failed: %s\n",
  115. strerror(errno));
  116. }
  117. if (child != 0) { /* parent */
  118. pid_t pid;
  119. int status;
  120. pid = waitpid(child, &status, 0);
  121. if (pid == -1) {
  122. die("waitpid failed: %s\n",
  123. strerror(errno));
  124. }
  125. if (pid != child) {
  126. die("waited for %d got %d\n",
  127. child, pid);
  128. }
  129. if (!WIFEXITED(status)) {
  130. die("child did not terminate cleanly\n");
  131. }
  132. return WEXITSTATUS(status) == EXIT_SUCCESS ? true : false;
  133. }
  134. create_and_enter_userns();
  135. if (unshare(CLONE_NEWNS) != 0) {
  136. die("unshare(CLONE_NEWNS) failed: %s\n",
  137. strerror(errno));
  138. }
  139. if (mount("testing", "/tmp", "ramfs", mount_flags, NULL) != 0) {
  140. die("mount of /tmp failed: %s\n",
  141. strerror(errno));
  142. }
  143. create_and_enter_userns();
  144. if (unshare(CLONE_NEWNS) != 0) {
  145. die("unshare(CLONE_NEWNS) failed: %s\n",
  146. strerror(errno));
  147. }
  148. if (mount("/tmp", "/tmp", "none",
  149. MS_REMOUNT | MS_BIND | remount_flags, NULL) != 0) {
  150. /* system("cat /proc/self/mounts"); */
  151. die("remount of /tmp failed: %s\n",
  152. strerror(errno));
  153. }
  154. if (mount("/tmp", "/tmp", "none",
  155. MS_REMOUNT | MS_BIND | invalid_flags, NULL) == 0) {
  156. /* system("cat /proc/self/mounts"); */
  157. die("remount of /tmp with invalid flags "
  158. "succeeded unexpectedly\n");
  159. }
  160. exit(EXIT_SUCCESS);
  161. }
  162. static bool test_unpriv_remount_simple(int mount_flags)
  163. {
  164. return test_unpriv_remount(mount_flags, mount_flags, 0);
  165. }
  166. static bool test_unpriv_remount_atime(int mount_flags, int invalid_flags)
  167. {
  168. return test_unpriv_remount(mount_flags, mount_flags, invalid_flags);
  169. }
  170. int main(int argc, char **argv)
  171. {
  172. if (!test_unpriv_remount_simple(MS_RDONLY|MS_NODEV)) {
  173. die("MS_RDONLY malfunctions\n");
  174. }
  175. if (!test_unpriv_remount_simple(MS_NODEV)) {
  176. die("MS_NODEV malfunctions\n");
  177. }
  178. if (!test_unpriv_remount_simple(MS_NOSUID|MS_NODEV)) {
  179. die("MS_NOSUID malfunctions\n");
  180. }
  181. if (!test_unpriv_remount_simple(MS_NOEXEC|MS_NODEV)) {
  182. die("MS_NOEXEC malfunctions\n");
  183. }
  184. if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODEV,
  185. MS_NOATIME|MS_NODEV))
  186. {
  187. die("MS_RELATIME malfunctions\n");
  188. }
  189. if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODEV,
  190. MS_NOATIME|MS_NODEV))
  191. {
  192. die("MS_STRICTATIME malfunctions\n");
  193. }
  194. if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODEV,
  195. MS_STRICTATIME|MS_NODEV))
  196. {
  197. die("MS_RELATIME malfunctions\n");
  198. }
  199. if (!test_unpriv_remount_atime(MS_RELATIME|MS_NODIRATIME|MS_NODEV,
  200. MS_NOATIME|MS_NODEV))
  201. {
  202. die("MS_RELATIME malfunctions\n");
  203. }
  204. if (!test_unpriv_remount_atime(MS_STRICTATIME|MS_NODIRATIME|MS_NODEV,
  205. MS_NOATIME|MS_NODEV))
  206. {
  207. die("MS_RELATIME malfunctions\n");
  208. }
  209. if (!test_unpriv_remount_atime(MS_NOATIME|MS_NODIRATIME|MS_NODEV,
  210. MS_STRICTATIME|MS_NODEV))
  211. {
  212. die("MS_RELATIME malfunctions\n");
  213. }
  214. if (!test_unpriv_remount(MS_STRICTATIME|MS_NODEV, MS_NODEV,
  215. MS_NOATIME|MS_NODEV))
  216. {
  217. die("Default atime malfunctions\n");
  218. }
  219. return EXIT_SUCCESS;
  220. }