From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10392 Path: news.gmane.org!.POSTED!not-for-mail From: Szabolcs Nagy Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH] verify that ttyname refers to the same file as the fd Date: Sat, 20 Aug 2016 21:04:31 +0200 Message-ID: <20160820190430.GF1280@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1471719891 551 195.159.176.226 (20 Aug 2016 19:04:51 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 20 Aug 2016 19:04:51 +0000 (UTC) User-Agent: Mutt/1.6.0 (2016-04-01) To: musl@lists.openwall.com Original-X-From: musl-return-10405-gllmg-musl=m.gmane.org@lists.openwall.com Sat Aug 20 21:04:46 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1bbBZV-0008Kt-Uv for gllmg-musl@m.gmane.org; Sat, 20 Aug 2016 21:04:46 +0200 Original-Received: (qmail 11858 invoked by uid 550); 20 Aug 2016 19:04:44 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 11823 invoked from network); 20 Aug 2016 19:04:43 -0000 Mail-Followup-To: musl@lists.openwall.com Content-Disposition: inline Xref: news.gmane.org gmane.linux.lib.musl.general:10392 Archived-At: linux containers use separate mount namespace so the /proc symlink might not point to the right device if the fd was opened in the parent namespace, in this case return ENOENT. --- related glibc issue (glibc also searches through /dev/pts): https://sourceware.org/ml/libc-alpha/2016-08/msg00312.html errno is not yet decided: https://sourceware.org/ml/libc-alpha/2016-08/msg00337.html src/unistd/ttyname_r.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/unistd/ttyname_r.c b/src/unistd/ttyname_r.c index 8bac7b2..a38ba4f 100644 --- a/src/unistd/ttyname_r.c +++ b/src/unistd/ttyname_r.c @@ -1,10 +1,12 @@ #include #include +#include void __procfdname(char *, unsigned); int ttyname_r(int fd, char *name, size_t size) { + struct stat st1, st2; char procname[sizeof "/proc/self/fd/" + 3*sizeof(int) + 2]; ssize_t l; @@ -15,8 +17,13 @@ int ttyname_r(int fd, char *name, size_t size) if (l < 0) return errno; else if (l == size) return ERANGE; - else { - name[l] = 0; - return 0; - } + + name[l] = 0; + + if (stat(name, &st1) || fstat(fd, &st2)) + return errno; + if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) + return ENOENT; + + return 0; } -- 2.8.1