From 96384aa4ffd2be9256cc74ce93d4c06cd907c7a9 Mon Sep 17 00:00:00 2001 From: deraadt Date: Sun, 17 May 2015 01:22:01 +0000 Subject: [PATCH] isatty() is used by stdio to determine the buffering mode. Add a F_ISATTY option to fcntl(), so that isatty() can use this rather than than the bloated ioctl() interface. Reducing uses of ioctl() by libc makes it easier to constrain programs with various kinds of systrace sandboxes. ok guenther, previously discussed as a concept with nicm --- lib/libc/gen/isatty.c | 9 +++------ sys/kern/kern_descrip.c | 12 +++++++++++- sys/sys/fcntl.h | 5 ++++- usr.bin/kdump/mksubr | 4 ++-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/libc/gen/isatty.c b/lib/libc/gen/isatty.c index 0a94648a29f..3b63f2575c4 100644 --- a/lib/libc/gen/isatty.c +++ b/lib/libc/gen/isatty.c @@ -1,4 +1,4 @@ -/* $OpenBSD: isatty.c,v 1.8 2013/04/17 17:40:35 tedu Exp $ */ +/* $OpenBSD: isatty.c,v 1.9 2015/05/17 01:22:01 deraadt Exp $ */ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -28,13 +28,10 @@ * SUCH DAMAGE. */ -#include -#include +#include int isatty(int fd) { - struct termios t; - - return (tcgetattr(fd, &t) != -1); + return (fcntl(fd, F_ISATTY)); } diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 93d6a9c4cfe..76b8542b3ec 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_descrip.c,v 1.119 2015/04/30 21:18:45 millert Exp $ */ +/* $OpenBSD: kern_descrip.c,v 1.120 2015/05/17 01:22:01 deraadt Exp $ */ /* $NetBSD: kern_descrip.c,v 1.42 1996/03/30 22:24:38 christos Exp $ */ /* @@ -397,6 +397,16 @@ restart: *retval = OFLAGS(fp->f_flag); break; + case F_ISATTY: + vp = (struct vnode *)fp->f_data; + if (fp->f_type == DTYPE_VNODE && (vp->v_flag & VISTTY)) + *retval = 1; + else { + *retval = 0; + error = ENOTTY; + } + break; + case F_SETFL: fp->f_flag &= ~FCNTLFLAGS; fp->f_flag |= FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS; diff --git a/sys/sys/fcntl.h b/sys/sys/fcntl.h index 47b7219561d..b08dcc72ff8 100644 --- a/sys/sys/fcntl.h +++ b/sys/sys/fcntl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: fcntl.h,v 1.20 2013/06/05 01:26:00 guenther Exp $ */ +/* $OpenBSD: fcntl.h,v 1.21 2015/05/17 01:22:01 deraadt Exp $ */ /* $NetBSD: fcntl.h,v 1.8 1995/03/26 20:24:12 jtc Exp $ */ /*- @@ -155,6 +155,9 @@ #if __POSIX_VISIBLE >= 200809 #define F_DUPFD_CLOEXEC 10 /* duplicate with FD_CLOEXEC set */ #endif +#if __BSD_VISIBLE +#define F_ISATTY 11 /* used by isatty(3) */ +#endif /* file descriptor flags (F_GETFD, F_SETFD) */ #define FD_CLOEXEC 1 /* close-on-exec flag */ diff --git a/usr.bin/kdump/mksubr b/usr.bin/kdump/mksubr index 57680a03753..709e464d48d 100644 --- a/usr.bin/kdump/mksubr +++ b/usr.bin/kdump/mksubr @@ -1,5 +1,5 @@ #!/bin/sh -# $OpenBSD: mksubr,v 1.27 2015/04/17 06:14:36 guenther Exp $ +# $OpenBSD: mksubr,v 1.28 2015/05/17 01:22:01 deraadt Exp $ # # Copyright (c) 2006 David Kirchner # @@ -394,7 +394,7 @@ cat <<_EOF_ } else if (arg1 == F_SETFL) { (void)putchar(','); doflagsname(arg, 0); - } else if (!fancy || (arg1 != F_GETFD && arg1 != F_GETFL)) + } else if (!fancy || (arg1 != F_GETFD && arg1 != F_GETFL && arg1 != F_ISATTY)) (void)printf(",%#x", arg); } -- 2.20.1