From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11895 Path: news.gmane.org!.POSTED!not-for-mail From: Christian Brauner Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH 1/1] openpty: use TIOCGPTPEER to open slave side fd Date: Fri, 1 Sep 2017 17:35:58 +0200 Message-ID: <20170901153558.29715-2-christian.brauner@ubuntu.com> References: <20170901153558.29715-1-christian.brauner@ubuntu.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1504280203 17463 195.159.176.226 (1 Sep 2017 15:36:43 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 1 Sep 2017 15:36:43 +0000 (UTC) Cc: Christian Brauner To: musl@lists.openwall.com Original-X-From: musl-return-11908-gllmg-musl=m.gmane.org@lists.openwall.com Fri Sep 01 17:36:29 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1dnnzT-0003Gp-2l for gllmg-musl@m.gmane.org; Fri, 01 Sep 2017 17:36:15 +0200 Original-Received: (qmail 30719 invoked by uid 550); 1 Sep 2017 15:36:18 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 30677 invoked from network); 1 Sep 2017 15:36:18 -0000 X-Virus-Scanned: amavisd-new at heinlein-support.de In-Reply-To: <20170901153558.29715-1-christian.brauner@ubuntu.com> Xref: news.gmane.org gmane.linux.lib.musl.general:11895 Archived-At: Newer kernels expose the ioctl TIOCGPTPEER [1] call to userspace which allows to safely allocate a file descriptor for a pty slave based solely on the master file descriptor. This allows us to avoid path-based operations and makes this function a lot safer in the face of devpts mounts in different mount namespaces. [1]: https://patchwork.kernel.org/patch/9760743/ Signed-off-by: Christian Brauner --- src/misc/openpty.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/misc/openpty.c b/src/misc/openpty.c index c1074060..9eab7a37 100644 --- a/src/misc/openpty.c +++ b/src/misc/openpty.c @@ -3,13 +3,14 @@ #include #include #include +#include #include /* Nonstandard, but vastly superior to the standard functions */ int openpty(int *pm, int *ps, char *name, const struct termios *tio, const struct winsize *ws) { - int m, s, n=0, cs; + int m, s = -1, cs; char buf[20]; m = open("/dev/ptmx", O_RDWR|O_NOCTTY); @@ -17,13 +18,38 @@ int openpty(int *pm, int *ps, char *name, const struct termios *tio, const struc pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); + if (!name) name = buf; + +#ifdef TIOCGPTPEER + int ret; + + /* Try to allocate slave fd solely based on the master fd in case the + * kernel supports it. + */ + s = ioctl(m, TIOCGPTPEER, O_RDWR|O_NOCTTY); + if (s < 0) + goto fail; + + /* Check the contents of the symlink in case devpts has been mounted in + * a non-standard location. + */ + ret = ttyname_r(s, name, sizeof buf); + if (ret) + goto fail; + + if (strncmp(name, "/dev/pts/", 9)) + goto fail; +#else + int n = 0; + if (ioctl(m, TIOCSPTLCK, &n) || ioctl (m, TIOCGPTN, &n)) goto fail; - if (!name) name = buf; snprintf(name, sizeof buf, "/dev/pts/%d", n); - if ((s = open(name, O_RDWR|O_NOCTTY)) < 0) + s = open(name, O_RDWR|O_NOCTTY); + if (s < 0) goto fail; +#endif if (tio) tcsetattr(s, TCSANOW, tio); if (ws) ioctl(s, TIOCSWINSZ, ws); @@ -35,6 +61,8 @@ int openpty(int *pm, int *ps, char *name, const struct termios *tio, const struc return 0; fail: close(m); + if (s >= 0) + close(s); pthread_setcancelstate(cs, 0); return -1; } -- 2.14.1