summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristian Brauner <christian.brauner@ubuntu.com>2017-10-08 14:10:46 +0200
committerChristian Brauner <christian.brauner@ubuntu.com>2017-10-08 17:47:58 +0200
commit645ac9aaf89e3311949828546df6334322f48933 (patch)
treec3fcdd65127627d5f889060c145f238d953e2b1f
parent98e0742024d4c13c08a6076b3d119c250e7d0118 (diff)
downloadglibc-645ac9aaf89e3311949828546df6334322f48933.tar.gz
glibc-645ac9aaf89e3311949828546df6334322f48933.tar.xz
glibc-645ac9aaf89e3311949828546df6334322f48933.zip
openpty: use TIOCGPTPEER to open slave side fd
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>
-rw-r--r--ChangeLog3
-rw-r--r--login/openpty.c30
2 files changed, 27 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index e1e98adf1f..da396fff20 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,9 @@
 
 	* login/openpty.c (openpty): Close slave pty file descriptor on error.
 
+	* login/openpty.c (openpty): If defined, use the TIOCGPTPEER ioctl()
+	call to allocate the slave pty file descriptor.
+
 2017-10-06  Joseph Myers  <joseph@codesourcery.com>
 
 	* sysdeps/ieee754/ldbl-128/s_fma.c: Include <libm-alias-double.h>.
diff --git a/login/openpty.c b/login/openpty.c
index 9e556c27a5..6703128ea8 100644
--- a/login/openpty.c
+++ b/login/openpty.c
@@ -94,6 +94,8 @@ openpty (int *amaster, int *aslave, char *name,
   char *buf = _buf;
   int master, ret = -1, slave = -1;
 
+  *buf = '\0';
+
   master = getpt ();
   if (master == -1)
     return -1;
@@ -104,12 +106,22 @@ openpty (int *amaster, int *aslave, char *name,
   if (unlockpt (master))
     goto on_error;
 
-  if (pts_name (master, &buf, sizeof (_buf)))
-    goto on_error;
-
-  slave = open (buf, O_RDWR | O_NOCTTY);
+#ifdef TIOCGPTPEER
+  /* Try to allocate slave fd solely based on master fd first. */
+  slave = ioctl (master, TIOCGPTPEER, O_RDWR | O_NOCTTY);
+#endif
   if (slave == -1)
-    goto on_error;
+    {
+      /* Fallback to path-based slave fd allocation in case kernel doesn't
+       * support TIOCGPTPEER.
+       */
+      if (pts_name (master, &buf, sizeof (_buf)))
+        goto on_error;
+
+      slave = open (buf, O_RDWR | O_NOCTTY);
+      if (slave == -1)
+        goto on_error;
+    }
 
   /* XXX Should we ignore errors here?  */
   if (termp)
@@ -122,7 +134,13 @@ openpty (int *amaster, int *aslave, char *name,
   *amaster = master;
   *aslave = slave;
   if (name != NULL)
-    strcpy (name, buf);
+    {
+      if (*buf == '\0')
+        if (pts_name (master, &buf, sizeof (_buf)))
+          goto on_error;
+
+      strcpy (name, buf);
+    }
 
   ret = 0;