From: Ulrich Drepper Date: Wed, 25 Aug 1999 17:57:57 +0000 (+0000) Subject: Add checks to make sure we're really dealing with a master pseudo terminal, and X-Git-Tag: cvs/glibc_2-1-2~58 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=26a1ee568cf0e6d378056e0168e22c45889a309e;p=thirdparty%2Fglibc.git Add checks to make sure we're really dealing with a master pseudo terminal, and really returning the name of the associated slave pseudo terminal by checking the device number. --- diff --git a/sysdeps/unix/sysv/linux/ptsname.c b/sysdeps/unix/sysv/linux/ptsname.c index 5852e2b2956..10365792ddc 100644 --- a/sysdeps/unix/sysv/linux/ptsname.c +++ b/sysdeps/unix/sysv/linux/ptsname.c @@ -29,6 +29,23 @@ #include +/* Check if DEV corresponds to a master pseudo terminal device. */ +#define MASTER_P(Dev) \ + (major ((Dev)) == 2 \ + || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \ + || (major ((Dev)) >= 128 && major ((Dev)) < 136)) + +/* Check if DEV corresponds to a master pseudo terminal device. */ +#define SLAVE_P(Dev) \ + (major ((Dev)) == 3 \ + || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \ + || (major ((Dev)) >= 136 && major ((Dev)) < 144)) + +/* Note that major number 4 corresponds to the old BSD style pseudo + terminal devices. As of Linux 2.1.115 these are no longer + supported. They have been replaced by major numbers 2 (masters) + and 3 (slaves). */ + /* Directory where we can find the slave pty nodes. */ #define _PATH_DEVPTS "/dev/pts/" @@ -107,7 +124,16 @@ __ptsname_r (int fd, char *buf, size_t buflen) if (__fstat (fd, &st) < 0) return errno; + /* Check if FD really is a master pseudo terminal. */ + if (! MASTER_P (st.st_rdev)) + { + __set_errno (ENOTTY); + return ENOTTY; + } + ptyno = minor (st.st_rdev); + /* This is for the old BSD pseudo terminals. As of Linux + 2.1.115 these are no longer supported. */ if (major (st.st_rdev) == 4) ptyno -= 128; @@ -126,6 +152,15 @@ __ptsname_r (int fd, char *buf, size_t buflen) if (__xstat (_STAT_VER, buf, &st) < 0) return errno; + /* Check if the name we're about to return really corresponds to a + slave pseudo terminal. */ + if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev)) + { + /* This really is a configuration problem. */ + __set_errno (ENOTTY); + return ENOTTY; + } + __set_errno (save_errno); return 0; }