devpts_pts.c 5.9 KB

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