Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src Add O_REGULAR to enforce opening of only regular files
details: https://anonhg.NetBSD.org/src/rev/99e8685d53ac
branches: trunk
changeset: 357406:99e8685d53ac
user: christos <christos%NetBSD.org@localhost>
date: Thu Nov 09 20:30:01 2017 +0000
description:
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.
diffstat:
lib/libc/stdio/fdopen.c | 6 +++---
lib/libc/stdio/flags.c | 6 +++---
lib/libc/stdio/fopen.c | 18 ++----------------
lib/libc/stdio/freopen.c | 19 ++-----------------
lib/libc/sys/open.2 | 6 ++++--
sys/kern/vfs_vnops.c | 7 +++++--
sys/sys/fcntl.h | 5 +++--
7 files changed, 22 insertions(+), 45 deletions(-)
diffs (214 lines):
diff -r 8d71c07f9e49 -r 99e8685d53ac lib/libc/stdio/fdopen.c
--- a/lib/libc/stdio/fdopen.c Thu Nov 09 20:27:50 2017 +0000
+++ b/lib/libc/stdio/fdopen.c Thu Nov 09 20:30:01 2017 +0000
@@ -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 @@
return NULL;
}
- if (oflags & O_NONBLOCK) {
+ if (oflags & O_REGULAR) {
struct stat st;
if (fstat(fd, &st) == -1) {
return NULL;
diff -r 8d71c07f9e49 -r 99e8685d53ac lib/libc/stdio/flags.c
--- a/lib/libc/stdio/flags.c Thu Nov 09 20:27:50 2017 +0000
+++ b/lib/libc/stdio/flags.c Thu Nov 09 20:30:01 2017 +0000
@@ -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 @@
o |= O_CLOEXEC;
break;
case 'f':
- o |= O_NONBLOCK;
+ o |= O_REGULAR;
break;
case 'l':
o |= O_NOFOLLOW;
diff -r 8d71c07f9e49 -r 99e8685d53ac lib/libc/stdio/fopen.c
--- a/lib/libc/stdio/fopen.c Thu Nov 09 20:27:50 2017 +0000
+++ b/lib/libc/stdio/fopen.c Thu Nov 09 20:30:01 2017 +0000
@@ -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 @@
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
diff -r 8d71c07f9e49 -r 99e8685d53ac lib/libc/stdio/freopen.c
--- a/lib/libc/stdio/freopen.c Thu Nov 09 20:27:50 2017 +0000
+++ b/lib/libc/stdio/freopen.c Thu Nov 09 20:30:01 2017 +0000
@@ -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 @@
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)
diff -r 8d71c07f9e49 -r 99e8685d53ac lib/libc/sys/open.2
--- a/lib/libc/sys/open.2 Thu Nov 09 20:27:50 2017 +0000
+++ b/lib/libc/sys/open.2 Thu Nov 09 20:30:01 2017 +0000
@@ -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 @@
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
diff -r 8d71c07f9e49 -r 99e8685d53ac sys/kern/vfs_vnops.c
--- a/sys/kern/vfs_vnops.c Thu Nov 09 20:27:50 2017 +0000
+++ b/sys/kern/vfs_vnops.c Thu Nov 09 20:30:01 2017 +0000
@@ -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 @@
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;
}
diff -r 8d71c07f9e49 -r 99e8685d53ac sys/sys/fcntl.h
--- a/sys/sys/fcntl.h Thu Nov 09 20:27:50 2017 +0000
+++ b/sys/sys/fcntl.h Thu Nov 09 20:30:01 2017 +0000
@@ -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 */
Home |
Main Index |
Thread Index |
Old Index