From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/6935 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: isatty false positives and device state clobbering Date: Fri, 30 Jan 2015 22:29:53 -0500 Message-ID: <20150131032953.GA23935@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1422675013 3185 80.91.229.3 (31 Jan 2015 03:30:13 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 31 Jan 2015 03:30:13 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-6948-gllmg-musl=m.gmane.org@lists.openwall.com Sat Jan 31 04:30:13 2015 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1YHOl6-0005GB-Mh for gllmg-musl@m.gmane.org; Sat, 31 Jan 2015 04:30:08 +0100 Original-Received: (qmail 22098 invoked by uid 550); 31 Jan 2015 03:30:07 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 22087 invoked from network); 31 Jan 2015 03:30:06 -0000 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:6935 Archived-At: As can be seen by strace, the TCGETS ioctl used by isatty/fdopen/__stdout_write to determine whether a file descriptor is a terminal is aliased by the SNDCTL_TMR_TIMEBASE ioctl for OSS sound devices. This is an utterly stupid legacy mistake, but it means the ioctl could spuriously succeed and change the state (time base) of a midi sequencer device when it's intended just to query whether the device is a terminal. Even though it's unlikely to arise in practice, I'd like to find a clean solution to the problem. I see two general approaches: 1. Use fstat first and blacklist the sound device major before using the ioctl, or even hard-code a list of tty majors and determine positive tty status by device number. 2. Find an ioctl that doesn't clash with OSS (or anything else, but OSS is the only driver I know with this bogus ioctl space collision with ttys) and that doesn't change the tty state, and use that instead. I strongly prefer strategy 2; hard-coding device numbers seems really backwards and precludes portability of source/binaries to platforms that provide identical names and userspace API/ABI but different device numbering. OSS seems to use the range 0x5401 to 0x5408, so some possible candidates for strategy 2 seem to be: #define TIOCGPGRP 0x540F #define TIOCOUTQ 0x5411 #define TIOCGWINSZ 0x5413 #define FIONREAD 0x541B Perhaps TIOCGPGRP is best if it works for ttys that aren't the controlling tty for a process group, since it corresponds to a standard POSIX feature and would need to be present on any system where the tcgetpgrp() is implemented via ioctl. The others are nonstandard but widely supported extensions for querying terminal buffer state and window size. It's also worth checking whether these are defined differently on any particular archs (e.g. mips, uhg) and whether the definitions there might clash with OSS ioctl numbers, in which case selecting a different one would be preferable. Rich