[2/2] openpty: use TIOCGPTPEER to open slave side fd
Commit Message
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
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
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
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).
@@ -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.
@@ -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);