This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH 2/2] openpty: use TIOCGPTPEER to open slave side fd
- From: Christian Brauner <christian dot brauner at ubuntu dot com>
- To: libc-alpha at sourceware dot org, stgraber at stgraber dot org, serge at hallyn dot com
- Cc: Christian Brauner <christian dot brauner at ubuntu dot com>
- Date: Sat, 26 Aug 2017 15:44:49 +0200
- Subject: [PATCH 2/2] openpty: use TIOCGPTPEER to open slave side fd
- Authentication-results: sourceware.org; auth=none
- 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 <christian.brauner@ubuntu.com>
---
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 <christian.brauner@ubuntu.com>
+
+ * login/openpty.c (openpty): If defined, use the TIOCGPTPEER ioctl call
+ to allocate the slave pty file descriptor.
+
2017-08-26 Christian Brauner <christian.brauner@ubuntu.com>
* 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);
--
2.14.1