devpts_pts.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <sched.h>
  6. #include <stdbool.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/mount.h>
  13. #include <sys/wait.h>
  14. static bool terminal_dup2(int duplicate, int original)
  15. {
  16. int ret;
  17. ret = dup2(duplicate, original);
  18. if (ret < 0)
  19. return false;
  20. return true;
  21. }
  22. static int terminal_set_stdfds(int fd)
  23. {
  24. int i;
  25. if (fd < 0)
  26. return 0;
  27. for (i = 0; i < 3; i++)
  28. if (!terminal_dup2(fd, (int[]){STDIN_FILENO, STDOUT_FILENO,
  29. STDERR_FILENO}[i]))
  30. return -1;
  31. return 0;
  32. }
  33. static int login_pty(int fd)
  34. {
  35. int ret;
  36. setsid();
  37. ret = ioctl(fd, TIOCSCTTY, NULL);
  38. if (ret < 0)
  39. return -1;
  40. ret = terminal_set_stdfds(fd);
  41. if (ret < 0)
  42. return -1;
  43. if (fd > STDERR_FILENO)
  44. close(fd);
  45. return 0;
  46. }
  47. static int wait_for_pid(pid_t pid)
  48. {
  49. int status, ret;
  50. again:
  51. ret = waitpid(pid, &status, 0);
  52. if (ret == -1) {
  53. if (errno == EINTR)
  54. goto again;
  55. return -1;
  56. }
  57. if (ret != pid)
  58. goto again;
  59. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
  60. return -1;
  61. return 0;
  62. }
  63. static int resolve_procfd_symlink(int fd, char *buf, size_t buflen)
  64. {
  65. int ret;
  66. char procfd[4096];
  67. ret = snprintf(procfd, 4096, "/proc/self/fd/%d", fd);
  68. if (ret < 0 || ret >= 4096)
  69. return -1;
  70. ret = readlink(procfd, buf, buflen);
  71. if (ret < 0 || (size_t)ret >= buflen)
  72. return -1;
  73. buf[ret] = '\0';
  74. return 0;
  75. }
  76. static int do_tiocgptpeer(char *ptmx, char *expected_procfd_contents)
  77. {
  78. int ret;
  79. int master = -1, slave = -1, fret = -1;
  80. master = open(ptmx, O_RDWR | O_NOCTTY | O_CLOEXEC);
  81. if (master < 0) {
  82. fprintf(stderr, "Failed to open \"%s\": %s\n", ptmx,
  83. strerror(errno));
  84. return -1;
  85. }
  86. /*
  87. * grantpt() makes assumptions about /dev/pts/ so ignore it. It's also
  88. * not really needed.
  89. */
  90. ret = unlockpt(master);
  91. if (ret < 0) {
  92. fprintf(stderr, "Failed to unlock terminal\n");
  93. goto do_cleanup;
  94. }
  95. #ifdef TIOCGPTPEER
  96. slave = ioctl(master, TIOCGPTPEER, O_RDWR | O_NOCTTY | O_CLOEXEC);
  97. #endif
  98. if (slave < 0) {
  99. if (errno == EINVAL) {
  100. fprintf(stderr, "TIOCGPTPEER is not supported. "
  101. "Skipping test.\n");
  102. fret = EXIT_SUCCESS;
  103. }
  104. fprintf(stderr, "Failed to perform TIOCGPTPEER ioctl\n");
  105. goto do_cleanup;
  106. }
  107. pid_t pid = fork();
  108. if (pid < 0)
  109. goto do_cleanup;
  110. if (pid == 0) {
  111. char buf[4096];
  112. ret = login_pty(slave);
  113. if (ret < 0) {
  114. fprintf(stderr, "Failed to setup terminal\n");
  115. _exit(EXIT_FAILURE);
  116. }
  117. ret = resolve_procfd_symlink(STDIN_FILENO, buf, sizeof(buf));
  118. if (ret < 0) {
  119. fprintf(stderr, "Failed to retrieve pathname of pts "
  120. "slave file descriptor\n");
  121. _exit(EXIT_FAILURE);
  122. }
  123. if (strncmp(expected_procfd_contents, buf,
  124. strlen(expected_procfd_contents)) != 0) {
  125. fprintf(stderr, "Received invalid contents for "
  126. "\"/proc/<pid>/fd/%d\" symlink: %s\n",
  127. STDIN_FILENO, buf);
  128. _exit(-1);
  129. }
  130. fprintf(stderr, "Contents of \"/proc/<pid>/fd/%d\" "
  131. "symlink are valid: %s\n", STDIN_FILENO, buf);
  132. _exit(EXIT_SUCCESS);
  133. }
  134. ret = wait_for_pid(pid);
  135. if (ret < 0)
  136. goto do_cleanup;
  137. fret = EXIT_SUCCESS;
  138. do_cleanup:
  139. if (master >= 0)
  140. close(master);
  141. if (slave >= 0)
  142. close(slave);
  143. return fret;
  144. }
  145. static int verify_non_standard_devpts_mount(void)
  146. {
  147. char *mntpoint;
  148. int ret = -1;
  149. char devpts[] = P_tmpdir "/devpts_fs_XXXXXX";
  150. char ptmx[] = P_tmpdir "/devpts_fs_XXXXXX/ptmx";
  151. ret = umount("/dev/pts");
  152. if (ret < 0) {
  153. fprintf(stderr, "Failed to unmount \"/dev/pts\": %s\n",
  154. strerror(errno));
  155. return -1;
  156. }
  157. (void)umount("/dev/ptmx");
  158. mntpoint = mkdtemp(devpts);
  159. if (!mntpoint) {
  160. fprintf(stderr, "Failed to create temporary mountpoint: %s\n",
  161. strerror(errno));
  162. return -1;
  163. }
  164. ret = mount("devpts", mntpoint, "devpts", MS_NOSUID | MS_NOEXEC,
  165. "newinstance,ptmxmode=0666,mode=0620,gid=5");
  166. if (ret < 0) {
  167. fprintf(stderr, "Failed to mount devpts fs to \"%s\" in new "
  168. "mount namespace: %s\n", mntpoint,
  169. strerror(errno));
  170. unlink(mntpoint);
  171. return -1;
  172. }
  173. ret = snprintf(ptmx, sizeof(ptmx), "%s/ptmx", devpts);
  174. if (ret < 0 || (size_t)ret >= sizeof(ptmx)) {
  175. unlink(mntpoint);
  176. return -1;
  177. }
  178. ret = do_tiocgptpeer(ptmx, mntpoint);
  179. unlink(mntpoint);
  180. if (ret < 0)
  181. return -1;
  182. return 0;
  183. }
  184. static int verify_ptmx_bind_mount(void)
  185. {
  186. int ret;
  187. ret = mount("/dev/pts/ptmx", "/dev/ptmx", NULL, MS_BIND, NULL);
  188. if (ret < 0) {
  189. fprintf(stderr, "Failed to bind mount \"/dev/pts/ptmx\" to "
  190. "\"/dev/ptmx\" mount namespace\n");
  191. return -1;
  192. }
  193. ret = do_tiocgptpeer("/dev/ptmx", "/dev/pts/");
  194. if (ret < 0)
  195. return -1;
  196. return 0;
  197. }
  198. static int verify_invalid_ptmx_bind_mount(void)
  199. {
  200. int ret;
  201. char mntpoint_fd;
  202. char ptmx[] = P_tmpdir "/devpts_ptmx_XXXXXX";
  203. mntpoint_fd = mkstemp(ptmx);
  204. if (mntpoint_fd < 0) {
  205. fprintf(stderr, "Failed to create temporary directory: %s\n",
  206. strerror(errno));
  207. return -1;
  208. }
  209. ret = mount("/dev/pts/ptmx", ptmx, NULL, MS_BIND, NULL);
  210. close(mntpoint_fd);
  211. if (ret < 0) {
  212. fprintf(stderr, "Failed to bind mount \"/dev/pts/ptmx\" to "
  213. "\"%s\" mount namespace\n", ptmx);
  214. return -1;
  215. }
  216. ret = do_tiocgptpeer(ptmx, "/dev/pts/");
  217. if (ret == 0)
  218. return -1;
  219. return 0;
  220. }
  221. int main(int argc, char *argv[])
  222. {
  223. int ret;
  224. if (!isatty(STDIN_FILENO)) {
  225. fprintf(stderr, "Standard input file desciptor is not attached "
  226. "to a terminal. Skipping test\n");
  227. exit(EXIT_FAILURE);
  228. }
  229. ret = unshare(CLONE_NEWNS);
  230. if (ret < 0) {
  231. fprintf(stderr, "Failed to unshare mount namespace\n");
  232. exit(EXIT_FAILURE);
  233. }
  234. ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
  235. if (ret < 0) {
  236. fprintf(stderr, "Failed to make \"/\" MS_PRIVATE in new mount "
  237. "namespace\n");
  238. exit(EXIT_FAILURE);
  239. }
  240. ret = verify_ptmx_bind_mount();
  241. if (ret < 0)
  242. exit(EXIT_FAILURE);
  243. ret = verify_invalid_ptmx_bind_mount();
  244. if (ret < 0)
  245. exit(EXIT_FAILURE);
  246. ret = verify_non_standard_devpts_mount();
  247. if (ret < 0)
  248. exit(EXIT_FAILURE);
  249. exit(EXIT_SUCCESS);
  250. }