grantpt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Copyright (C) 1998-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <grp.h>
  19. #include <limits.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/resource.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include <unistd.h>
  27. #include "pty-private.h"
  28. /* Return the result of ptsname_r in the buffer pointed to by PTS,
  29. which should be of length BUF_LEN. If it is too long to fit in
  30. this buffer, a sufficiently long buffer is allocated using malloc,
  31. and returned in PTS. 0 is returned upon success, -1 otherwise. */
  32. static int
  33. pts_name (int fd, char **pts, size_t buf_len, struct stat64 *stp)
  34. {
  35. int rv;
  36. char *buf = *pts;
  37. for (;;)
  38. {
  39. char *new_buf;
  40. if (buf_len)
  41. {
  42. rv = __ptsname_internal (fd, buf, buf_len, stp);
  43. if (rv != 0)
  44. {
  45. if (rv == ENOTTY)
  46. /* ptsname_r returns with ENOTTY to indicate
  47. a descriptor not referring to a pty master.
  48. For this condition, grantpt must return EINVAL. */
  49. rv = EINVAL;
  50. errno = rv; /* Not necessarily set by __ptsname_r. */
  51. break;
  52. }
  53. if (memchr (buf, '\0', buf_len))
  54. /* We succeeded and the returned name fit in the buffer. */
  55. break;
  56. /* Try again with a longer buffer. */
  57. buf_len += buf_len; /* Double it */
  58. }
  59. else
  60. /* No initial buffer; start out by mallocing one. */
  61. buf_len = 128; /* First time guess. */
  62. if (buf != *pts)
  63. /* We've already malloced another buffer at least once. */
  64. new_buf = (char *) realloc (buf, buf_len);
  65. else
  66. new_buf = (char *) malloc (buf_len);
  67. if (! new_buf)
  68. {
  69. rv = -1;
  70. __set_errno (ENOMEM);
  71. break;
  72. }
  73. buf = new_buf;
  74. }
  75. if (rv == 0)
  76. *pts = buf; /* Return buffer to the user. */
  77. else if (buf != *pts)
  78. free (buf); /* Free what we malloced when returning an error. */
  79. return rv;
  80. }
  81. /* Change the ownership and access permission of the slave pseudo
  82. terminal associated with the master pseudo terminal specified
  83. by FD. */
  84. int
  85. grantpt (int fd)
  86. {
  87. int retval = -1;
  88. #ifdef PATH_MAX
  89. char _buf[PATH_MAX];
  90. #else
  91. char _buf[512];
  92. #endif
  93. char *buf = _buf;
  94. struct stat64 st;
  95. if (__glibc_unlikely (pts_name (fd, &buf, sizeof (_buf), &st)))
  96. {
  97. int save_errno = errno;
  98. /* Check, if the file descriptor is valid. pts_name returns the
  99. wrong errno number, so we cannot use that. */
  100. if (__libc_fcntl (fd, F_GETFD) == -1 && errno == EBADF)
  101. return -1;
  102. /* If the filedescriptor is no TTY, grantpt has to set errno
  103. to EINVAL. */
  104. if (save_errno == ENOTTY)
  105. __set_errno (EINVAL);
  106. else
  107. __set_errno (save_errno);
  108. return -1;
  109. }
  110. /* Make sure that we own the device. */
  111. uid_t uid = __getuid ();
  112. if (st.st_uid != uid)
  113. {
  114. if (__chown (buf, uid, st.st_gid) < 0)
  115. goto helper;
  116. }
  117. static int tty_gid = -1;
  118. if (__glibc_unlikely (tty_gid == -1))
  119. {
  120. char *grtmpbuf;
  121. struct group grbuf;
  122. size_t grbuflen = __sysconf (_SC_GETGR_R_SIZE_MAX);
  123. struct group *p;
  124. /* Get the group ID of the special `tty' group. */
  125. if (grbuflen == (size_t) -1L)
  126. /* `sysconf' does not support _SC_GETGR_R_SIZE_MAX.
  127. Try a moderate value. */
  128. grbuflen = 1024;
  129. grtmpbuf = (char *) __alloca (grbuflen);
  130. __getgrnam_r (TTY_GROUP, &grbuf, grtmpbuf, grbuflen, &p);
  131. if (p != NULL)
  132. tty_gid = p->gr_gid;
  133. }
  134. gid_t gid = tty_gid == -1 ? __getgid () : tty_gid;
  135. #if HAVE_PT_CHOWN
  136. /* Make sure the group of the device is that special group. */
  137. if (st.st_gid != gid)
  138. {
  139. if (__chown (buf, uid, gid) < 0)
  140. goto helper;
  141. }
  142. /* Make sure the permission mode is set to readable and writable by
  143. the owner, and writable by the group. */
  144. mode_t mode = S_IRUSR|S_IWUSR|S_IWGRP;
  145. #else
  146. /* When built without pt_chown, we have delegated the creation of the
  147. pty node with the right group and permission mode to the kernel, and
  148. non-root users are unlikely to be able to change it. Therefore let's
  149. consider that POSIX enforcement is the responsibility of the whole
  150. system and not only the GNU libc. Thus accept different group or
  151. permission mode. */
  152. /* Make sure the permission is set to readable and writable by the
  153. owner. For security reasons, make it writable by the group only
  154. when originally writable and when the group of the device is that
  155. special group. */
  156. mode_t mode = S_IRUSR|S_IWUSR|
  157. ((st.st_gid == gid) ? (st.st_mode & S_IWGRP) : 0);
  158. #endif
  159. if ((st.st_mode & ACCESSPERMS) != mode)
  160. {
  161. if (__chmod (buf, mode) < 0)
  162. goto helper;
  163. }
  164. retval = 0;
  165. goto cleanup;
  166. /* We have to use the helper program if it is available. */
  167. helper:;
  168. #if HAVE_PT_CHOWN
  169. pid_t pid = __fork ();
  170. if (pid == -1)
  171. goto cleanup;
  172. else if (pid == 0)
  173. {
  174. /* Disable core dumps. */
  175. struct rlimit rl = { 0, 0 };
  176. __setrlimit (RLIMIT_CORE, &rl);
  177. /* We pass the master pseudo terminal as file descriptor PTY_FILENO. */
  178. if (fd != PTY_FILENO)
  179. if (__dup2 (fd, PTY_FILENO) < 0)
  180. _exit (FAIL_EBADF);
  181. # ifdef CLOSE_ALL_FDS
  182. CLOSE_ALL_FDS ();
  183. # endif
  184. execle (_PATH_PT_CHOWN, __basename (_PATH_PT_CHOWN), NULL, NULL);
  185. _exit (FAIL_EXEC);
  186. }
  187. else
  188. {
  189. int w;
  190. if (__waitpid (pid, &w, 0) == -1)
  191. goto cleanup;
  192. if (!WIFEXITED (w))
  193. __set_errno (ENOEXEC);
  194. else
  195. switch (WEXITSTATUS (w))
  196. {
  197. case 0:
  198. retval = 0;
  199. break;
  200. case FAIL_EBADF:
  201. __set_errno (EBADF);
  202. break;
  203. case FAIL_EINVAL:
  204. __set_errno (EINVAL);
  205. break;
  206. case FAIL_EACCES:
  207. __set_errno (EACCES);
  208. break;
  209. case FAIL_EXEC:
  210. __set_errno (ENOEXEC);
  211. break;
  212. case FAIL_ENOMEM:
  213. __set_errno (ENOMEM);
  214. break;
  215. default:
  216. assert(! "grantpt: internal error: invalid exit code from pt_chown");
  217. }
  218. }
  219. #endif
  220. cleanup:
  221. if (buf != _buf)
  222. free (buf);
  223. return retval;
  224. }