summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>1997-10-24 09:04:28 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>1997-10-24 09:04:28 +0000
commitf0b5b76ae1bb6285bef428dc271caf7b927eec68 (patch)
tree704c1091074cacc4b5ccac0ef6ee298d5f1c114c
parentbc5ae0c375886c01a3b0f4ef84651c1c99ba71ea (diff)
prevent open() with wrong flags
-rw-r--r--lib/libc/sys/open.22
-rw-r--r--sys/kern/vfs_vnops.c4
-rw-r--r--sys/sys/fcntl.h11
3 files changed, 12 insertions, 5 deletions
diff --git a/lib/libc/sys/open.2 b/lib/libc/sys/open.2
index 8b76371e021..4716156aa92 100644
--- a/lib/libc/sys/open.2
+++ b/lib/libc/sys/open.2
@@ -180,6 +180,8 @@ Too many symbolic links were encountered in translating the pathname.
.It Bq Er EISDIR
The named file is a directory, and the arguments specify
it is to be opened for writing.
+.It Bq Er EINVAL
+The flags specified for opening the file are not valid.
.It Bq Er EROFS
The named file resides on a read-only file system,
and the file is to be modified.
diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c
index 2dc3f778374..ee781e8d79d 100644
--- a/sys/kern/vfs_vnops.c
+++ b/sys/kern/vfs_vnops.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vfs_vnops.c,v 1.7 1997/10/06 20:20:15 deraadt Exp $ */
+/* $OpenBSD: vfs_vnops.c,v 1.8 1997/10/24 09:04:26 deraadt Exp $ */
/* $NetBSD: vfs_vnops.c,v 1.20 1996/02/04 02:18:41 christos Exp $ */
/*
@@ -74,6 +74,8 @@ vn_open(ndp, fmode, cmode)
struct vattr va;
int error;
+ if ((fmode & (FREAD|FWRITE)) == 0)
+ return (EINVAL);
if (fmode & O_CREAT) {
ndp->ni_cnd.cn_nameiop = CREATE;
ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
diff --git a/sys/sys/fcntl.h b/sys/sys/fcntl.h
index da92fd3c6ce..8b72150e986 100644
--- a/sys/sys/fcntl.h
+++ b/sys/sys/fcntl.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: fcntl.h,v 1.3 1996/11/03 06:35:20 deraadt Exp $ */
+/* $OpenBSD: fcntl.h,v 1.4 1997/10/24 09:04:24 deraadt Exp $ */
/* $NetBSD: fcntl.h,v 1.8 1995/03/26 20:24:12 jtc Exp $ */
/*-
@@ -100,9 +100,12 @@
#define O_NOCTTY 0x8000 /* don't assign controlling terminal */
#ifdef _KERNEL
-/* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */
-#define FFLAGS(oflags) ((oflags) + 1)
-#define OFLAGS(fflags) ((fflags) - 1)
+/*
+ * convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE.
+ * For out-of-range values for the flags, be slightly careful (but lossy).
+ */
+#define FFLAGS(oflags) (((oflags) & ~O_ACCMODE) | (((oflags) + 1) & O_ACCMODE))
+#define OFLAGS(fflags) (((fflags) & ~O_ACCMODE) | (((fflags) - 1) & O_ACCMODE))
/* bits to save after open */
#define FMASK (FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FNONBLOCK)