diff mbox

[2/2] openpty: use TIOCGPTPEER to open slave side fd

Message ID 20170826134449.26527-2-christian.brauner@ubuntu.com
State New
Headers show

Commit Message

Christian Brauner Aug. 26, 2017, 1:44 p.m. UTC
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(-)

Comments

Florian Weimer Aug. 28, 2017, 7:34 a.m. UTC | #1
On 08/26/2017 03:44 PM, Christian Brauner wrote:
> +#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

I don't think you can #ifdef out existing code this way without
introducing failures on older kernels.  You need to try the ioctl first,
and if that fails, use the old pts_name code.

Thanks,
Florian
Christian Brauner Aug. 28, 2017, 11:14 a.m. UTC | #2
On Mon, Aug 28, 2017 at 09:34:11AM +0200, Florian Weimer wrote:
> On 08/26/2017 03:44 PM, Christian Brauner wrote:
> > +#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
> 
> I don't think you can #ifdef out existing code this way without
> introducing failures on older kernels.  You need to try the ioctl first,
> and if that fails, use the old pts_name code.

Cool. Will resend the [PATCH 2/2] soon. I take it that [PATH 1/1] holds up as it
stands.

Thanks!
Christian

> 
> Thanks,
> Florian
Joseph Myers Aug. 28, 2017, 11:39 a.m. UTC | #3
On Mon, 28 Aug 2017, Florian Weimer wrote:

> On 08/26/2017 03:44 PM, Christian Brauner wrote:
> > +#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
> 
> I don't think you can #ifdef out existing code this way without
> introducing failures on older kernels.  You need to try the ioctl first,
> and if that fails, use the old pts_name code.

And in principle there should be appropriate __ASSUME_* conditionals so 
that when building with a new-enough --enable-kernel the old code can be 
conditioned out (however, because this code is not Linux-specific, the old 
case would need to stay in the code even when __ASSUME_TIOCGPTPEER is 
defined unconditionally in the Linux kernel-features.h).
diff mbox

Patch

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);