[1/1] linux ttyname: return link if appropriate
Commit Message
Quoting Serge Hallyn (serge.hallyn@ubuntu.com):
> Quoting Florian Weimer (fweimer@redhat.com):
> > On 04/15/2016 06:46 PM, Serge Hallyn wrote:
> > >Quoting Florian Weimer (fweimer@redhat.com):
> > >>On 04/15/2016 05:29 PM, Serge Hallyn wrote:
> > >>>The current ttyname does the wrong thing in two cases:
> > >>>
> > >>>1. If the passed-in link (say /proc/self/fd/0) points to a
> > >>>device, say /dev/pts/2, in a parent mount namespace, and a
> > >>>/dev/pts/2 exists (in a different devpts) in the current
> > >>>namespace, then it returns /dev/pts/2. But /dev/pts/2 is
> > >>>NOT the current tty, it is a different file and device.
> > >>
> > >>Is this the first change?
> > >
> > >Right, it ensures that the filesystem of the two files is
> > >the same.
> > >
> > >>>2. If the passed-in link (say /proc/self/fd/0) points to
> > >>>a device, say /dev/pts/2, in a parent mount namespace, and
> > >>>/dev/pts/2 does not exist in the current namespace, it
> > >>>returns success but an empty name. As far as I can tell,
> > >>>there is no reason for it to not return /proc/self/fd/0.
> > >>>http://pubs.opengroup.org/onlinepubs/009695399/functions/ttyname.html
> > >>>does not say anything about not returning a link.
> > >>
> > >>Is it safe to drop the verification that ttyname ordinarily would do?
> > >
> > >Which verification do you mean exactly?
> >
> > That the file descriptor actually belongs to a PTY device listed
> > under /dev/pts.
>
> Oh, yeah. I think that adding a chck that this is a pts (using st_rdev)
> before returning "/proc/self/fd/N" (in my newly added block) would be good.
Something like:
From ba0dc51e90d884b107145924821a1e8caf43a468 Mon Sep 17 00:00:00 2001
From: Serge Hallyn <serge.hallyn@ubuntu.com>
Date: Fri, 15 Apr 2016 10:21:07 -0500
Subject: [PATCH 1/1] linux ttyname: return link if appropriate
The current ttyname does the wrong thing in two cases:
1. If the passed-in link (say /proc/self/fd/0) points to a
device, say /dev/pts/2, in a parent mount namespace, and a
/dev/pts/2 exists (in a different devpts) in the current
namespace, then it returns /dev/pts/2. But /dev/pts/2 is
NOT the current tty, it is a different file and device.
2. If the passed-in link (say /proc/self/fd/0) points to
a device, say /dev/pts/2, in a parent mount namespace, and
/dev/pts/2 does not exist in the current namespace, it
returns success but an empty name. As far as I can tell,
there is no reason for it to not return /proc/self/fd/0.
http://pubs.opengroup.org/onlinepubs/009695399/functions/ttyname.html
does not say anything about not returning a link.
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
---
sysdeps/unix/sysv/linux/ttyname.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
Comments
On 15 Apr 2016 18:47, Serge Hallyn wrote:
> Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
we don't use s-o-b tags
> +/*
> + * Return true if this is a UNIX98 pty device, as defined in
> + * linux/Documentation/devices.txt
> + */
GNU style is:
/* Return true if this is a UNIX98 pty device, as defined in
linux/Documentation/devices.txt. */
this applies to comments below too
> + if (is_pty (st) && strlen (procname) < buflen - 1)
> + {
> + memcpy (ttyname_buf, procname, strlen (procname));
> + ttyname_buf[strlen (procname)] = '\0';
since you already verified buflen, why not use strcpy ?
also, GNU style says 8 spaces -> 1 tab
-mike
Quoting Mike Frysinger (vapier@gentoo.org):
> On 15 Apr 2016 18:47, Serge Hallyn wrote:
> > Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
>
> we don't use s-o-b tags
>
> > +/*
> > + * Return true if this is a UNIX98 pty device, as defined in
> > + * linux/Documentation/devices.txt
> > + */
>
> GNU style is:
>
> /* Return true if this is a UNIX98 pty device, as defined in
> linux/Documentation/devices.txt. */
>
> this applies to comments below too
>
> > + if (is_pty (st) && strlen (procname) < buflen - 1)
> > + {
> > + memcpy (ttyname_buf, procname, strlen (procname));
> > + ttyname_buf[strlen (procname)] = '\0';
>
> since you already verified buflen, why not use strcpy ?
That actually had been my first inclination, not sure why I
switched it.
> also, GNU style says 8 spaces -> 1 tab
> -mike
There were a few other bugs as well. Replying with a new patch.
thanks,
-serge
@@ -33,6 +33,21 @@
char *__ttyname;
#endif
+/*
+ * Return true if this is a UNIX98 pty device, as defined in
+ * linux/Documentation/devices.txt
+ */
+static int
+is_pty (struct stat *sb)
+{
+#ifdef _STATBUF_ST_RDEV
+ int m = major (sb.st_rdev);
+ return (136 <= m && m <= 143);
+#else
+ return false;
+#endif
+}
+
static char *getttyname (const char *dev, dev_t mydev,
ino64_t myino, int save, int *dostat)
internal_function;
@@ -170,12 +185,22 @@ ttyname (int fd)
#ifdef _STATBUF_ST_RDEV
&& S_ISCHR (st1.st_mode)
&& st1.st_rdev == st.st_rdev
+ && st1.st_dev == st.st_dev
#else
&& st1.st_ino == st.st_ino
&& st1.st_dev == st.st_dev
#endif
)
return ttyname_buf;
+ /* If the link doesn't exist, then it points to a dvice in another
+ * namespace. If it is a UNIX98 pty, then return the /proc/self
+ * fd, as it points to a name unreachable in our namespace */
+ if (is_pty (st) && strlen (procname) < buflen - 1)
+ {
+ memcpy (ttyname_buf, procname, strlen (procname));
+ ttyname_buf[strlen (procname)] = '\0';
+ return ttyname_buf;
+ }
}
if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))