Module Name:    src
Committed By:   christos
Date:           Thu Nov  9 20:30:02 UTC 2017

Modified Files:
        src/lib/libc/stdio: fdopen.c flags.c fopen.c freopen.c
        src/lib/libc/sys: open.2
        src/sys/kern: vfs_vnops.c
        src/sys/sys: fcntl.h

Log Message:
Add O_REGULAR to enforce opening of only regular files
(like we have O_DIRECTORY for directories).
This is better than open(, O_NONBLOCK), fstat()+S_ISREG() because opening
devices can have side effects.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/lib/libc/stdio/fdopen.c
cvs rdiff -u -r1.18 -r1.19 src/lib/libc/stdio/flags.c
cvs rdiff -u -r1.16 -r1.17 src/lib/libc/stdio/fopen.c
cvs rdiff -u -r1.19 -r1.20 src/lib/libc/stdio/freopen.c
cvs rdiff -u -r1.57 -r1.58 src/lib/libc/sys/open.2
cvs rdiff -u -r1.195 -r1.196 src/sys/kern/vfs_vnops.c
cvs rdiff -u -r1.48 -r1.49 src/sys/sys/fcntl.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/stdio/fdopen.c
diff -u src/lib/libc/stdio/fdopen.c:1.17 src/lib/libc/stdio/fdopen.c:1.18
--- src/lib/libc/stdio/fdopen.c:1.17	Tue Jan 10 12:00:58 2017
+++ src/lib/libc/stdio/fdopen.c	Thu Nov  9 15:30:02 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: fdopen.c,v 1.17 2017/01/10 17:00:58 christos Exp $	*/
+/*	$NetBSD: fdopen.c,v 1.18 2017/11/09 20:30:02 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)fdopen.c	8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: fdopen.c,v 1.17 2017/01/10 17:00:58 christos Exp $");
+__RCSID("$NetBSD: fdopen.c,v 1.18 2017/11/09 20:30:02 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -92,7 +92,7 @@ fdopen(int fd, const char *mode)
 		return NULL;
 	}
 
-	if (oflags & O_NONBLOCK) {
+	if (oflags & O_REGULAR) {
 		struct stat st;
 		if (fstat(fd, &st) == -1) {
 			return NULL;

Index: src/lib/libc/stdio/flags.c
diff -u src/lib/libc/stdio/flags.c:1.18 src/lib/libc/stdio/flags.c:1.19
--- src/lib/libc/stdio/flags.c:1.18	Fri Nov  3 22:49:55 2017
+++ src/lib/libc/stdio/flags.c	Thu Nov  9 15:30:02 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: flags.c,v 1.18 2017/11/04 02:49:55 christos Exp $	*/
+/*	$NetBSD: flags.c,v 1.19 2017/11/09 20:30:02 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)flags.c	8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: flags.c,v 1.18 2017/11/04 02:49:55 christos Exp $");
+__RCSID("$NetBSD: flags.c,v 1.19 2017/11/09 20:30:02 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -105,7 +105,7 @@ __sflags(const char *mode, int *optr)
 			o |= O_CLOEXEC;
 			break;
 		case 'f':
-			o |= O_NONBLOCK;
+			o |= O_REGULAR;
 			break;
 		case 'l':
 			o |= O_NOFOLLOW;

Index: src/lib/libc/stdio/fopen.c
diff -u src/lib/libc/stdio/fopen.c:1.16 src/lib/libc/stdio/fopen.c:1.17
--- src/lib/libc/stdio/fopen.c:1.16	Sat Nov  4 03:26:35 2017
+++ src/lib/libc/stdio/fopen.c	Thu Nov  9 15:30:02 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: fopen.c,v 1.16 2017/11/04 07:26:35 kre Exp $	*/
+/*	$NetBSD: fopen.c,v 1.17 2017/11/09 20:30:02 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)fopen.c	8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: fopen.c,v 1.16 2017/11/04 07:26:35 kre Exp $");
+__RCSID("$NetBSD: fopen.c,v 1.17 2017/11/09 20:30:02 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -66,20 +66,6 @@ fopen(const char *file, const char *mode
 		return NULL;
 	if ((f = open(file, oflags, DEFFILEMODE)) < 0)
 		goto release;
-	if (oflags & O_NONBLOCK) {
-		struct stat st;
-		if (fstat(f, &st) == -1) {
-			int sverrno = errno;
-			(void)close(f);
-			errno = sverrno;
-			goto release;
-		}
-		if (!S_ISREG(st.st_mode)) {
-			(void)close(f);
-			errno = EFTYPE;
-			goto release;
-		}
-	}
 	/*
 	 * File descriptors are a full int, but _file is only a short.
 	 * If we get a valid file descriptor that is greater or equal to

Index: src/lib/libc/stdio/freopen.c
diff -u src/lib/libc/stdio/freopen.c:1.19 src/lib/libc/stdio/freopen.c:1.20
--- src/lib/libc/stdio/freopen.c:1.19	Tue Mar 27 11:05:42 2012
+++ src/lib/libc/stdio/freopen.c	Thu Nov  9 15:30:02 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: freopen.c,v 1.19 2012/03/27 15:05:42 christos Exp $	*/
+/*	$NetBSD: freopen.c,v 1.20 2017/11/09 20:30:02 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)freopen.c	8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: freopen.c,v 1.19 2012/03/27 15:05:42 christos Exp $");
+__RCSID("$NetBSD: freopen.c,v 1.20 2017/11/09 20:30:02 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -141,21 +141,6 @@ freopen(const char *file, const char *mo
 		return NULL;
 	}
 
-	if (oflags & O_NONBLOCK) {
-		struct stat st;
-		if (fstat(f, &st) == -1) {
-			sverrno = errno;
-			(void)close(f);
-			errno = sverrno;
-			return NULL;
-		}
-		if (!S_ISREG(st.st_mode)) {
-			(void)close(f);
-			errno = EFTYPE;
-			return NULL;
-		}
-	}
-
 	/*
 	 * If reopening something that was open before on a real file, try
 	 * to maintain the descriptor.  Various C library routines (perror)

Index: src/lib/libc/sys/open.2
diff -u src/lib/libc/sys/open.2:1.57 src/lib/libc/sys/open.2:1.58
--- src/lib/libc/sys/open.2:1.57	Sun May 14 08:30:37 2017
+++ src/lib/libc/sys/open.2	Thu Nov  9 15:30:02 2017
@@ -1,4 +1,4 @@
-.\"	$NetBSD: open.2,v 1.57 2017/05/14 12:30:37 wiz Exp $
+.\"	$NetBSD: open.2,v 1.58 2017/11/09 20:30:02 christos Exp $
 .\"
 .\" Copyright (c) 1980, 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)open.2	8.2 (Berkeley) 11/16/93
 .\"
-.Dd July 29, 2013
+.Dd November 9, 2017
 .Dt OPEN 2
 .Os
 .Sh NAME
@@ -188,6 +188,8 @@ using an interface that supports scatter
 element of the request must meet the above alignment constraints.
 .It Dv O_DIRECTORY
 Fail if the file is not a directory.
+.It Dv O_REGULAR
+Fail if the path does not refer to a regular file.
 .It Dv O_ASYNC
 Enable the
 .Dv SIGIO

Index: src/sys/kern/vfs_vnops.c
diff -u src/sys/kern/vfs_vnops.c:1.195 src/sys/kern/vfs_vnops.c:1.196
--- src/sys/kern/vfs_vnops.c:1.195	Thu Mar 30 05:13:37 2017
+++ src/sys/kern/vfs_vnops.c	Thu Nov  9 15:30:01 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_vnops.c,v 1.195 2017/03/30 09:13:37 hannken Exp $	*/
+/*	$NetBSD: vfs_vnops.c,v 1.196 2017/11/09 20:30:01 christos Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.195 2017/03/30 09:13:37 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.196 2017/11/09 20:30:01 christos Exp $");
 
 #include "veriexec.h"
 
@@ -299,6 +299,9 @@ vn_openchk(struct vnode *vp, kauth_cred_
 	if ((fflags & O_DIRECTORY) != 0 && vp->v_type != VDIR)
 		return ENOTDIR;
 
+	if ((fflags & O_REGULAR) != 0 && vp->v_type != VREG)
+		return EFTYPE;
+
 	if ((fflags & FREAD) != 0) {
 		permbits = VREAD;
 	}

Index: src/sys/sys/fcntl.h
diff -u src/sys/sys/fcntl.h:1.48 src/sys/sys/fcntl.h:1.49
--- src/sys/sys/fcntl.h:1.48	Tue Jan 10 18:08:13 2017
+++ src/sys/sys/fcntl.h	Thu Nov  9 15:30:01 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: fcntl.h,v 1.48 2017/01/10 23:08:13 christos Exp $	*/
+/*	$NetBSD: fcntl.h,v 1.49 2017/11/09 20:30:01 christos Exp $	*/
 
 /*-
  * Copyright (c) 1983, 1990, 1993
@@ -120,6 +120,7 @@
 #endif
 #if defined(_NETBSD_SOURCE)
 #define	O_NOSIGPIPE	0x01000000	/* don't deliver sigpipe */
+#define	O_REGULAR	0x02000000	/* fail if not a regular file */
 #endif
 
 #ifdef _KERNEL
@@ -131,7 +132,7 @@
 #define	O_MASK		(O_ACCMODE|O_NONBLOCK|O_APPEND|O_SHLOCK|O_EXLOCK|\
 			 O_ASYNC|O_SYNC|O_CREAT|O_TRUNC|O_EXCL|O_DSYNC|\
 			 O_RSYNC|O_NOCTTY|O_ALT_IO|O_NOFOLLOW|O_DIRECT|\
-			 O_DIRECTORY|O_CLOEXEC|O_NOSIGPIPE)
+			 O_DIRECTORY|O_CLOEXEC|O_NOSIGPIPE|O_REGULAR)
 
 #define	FMARK		0x00001000	/* mark during gc() */
 #define	FDEFER		0x00002000	/* defer for next gc pass */

Reply via email to