From patchwork Sat Aug 26 13:44:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 22365 Received: (qmail 62183 invoked by alias); 26 Aug 2017 14:21:24 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 62167 invoked by uid 89); 26 Aug 2017 14:21:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy=sk:christi, slave, safer, Close X-HELO: mx1.mailbox.org From: Christian Brauner To: libc-alpha@sourceware.org, stgraber@stgraber.org, serge@hallyn.com Cc: Christian Brauner Subject: [PATCH 2/2] openpty: use TIOCGPTPEER to open slave side fd Date: Sat, 26 Aug 2017 15:44:49 +0200 Message-Id: <20170826134449.26527-2-christian.brauner@ubuntu.com> In-Reply-To: <20170826134449.26527-1-christian.brauner@ubuntu.com> References: <20170826134449.26527-1-christian.brauner@ubuntu.com> 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 --- ChangeLog | 5 +++++ login/openpty.c | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index bc5fb8e27f..30829e4c16 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2017-08-26 Christian Brauner + + * login/openpty.c (openpty): If defined, use the TIOCGPTPEER ioctl call + to allocate the slave pty file descriptor. + 2017-08-26 Christian Brauner * login/openpty.c (openpty): Close slave pty file descriptor on error. diff --git a/login/openpty.c b/login/openpty.c index 8fbc66a3ef..293cc0a0db 100644 --- a/login/openpty.c +++ b/login/openpty.c @@ -104,10 +104,14 @@ openpty (int *amaster, int *aslave, char *name, if (unlockpt (master)) goto fail; +#ifdef TIOCGPTPEER + slave = ioctl (master, TIOCGPTPEER, O_RDWR | O_NOCTTY); +#else if (pts_name (master, &buf, sizeof (_buf))) goto fail; slave = open (buf, O_RDWR | O_NOCTTY); +#endif if (slave == -1) { if (buf != _buf) @@ -127,7 +131,13 @@ openpty (int *amaster, int *aslave, char *name, *amaster = master; *aslave = slave; if (name != NULL) - strcpy (name, buf); + { +#ifdef TIOCGPTPEER + if (pts_name (master, &buf, sizeof (_buf))) + goto fail; +#endif + strcpy (name, buf); + } if (buf != _buf) free (buf);