summaryrefslogtreecommitdiff
path: root/sys/kern
diff options
context:
space:
mode:
authorMartin Pieuchot <mpi@cvs.openbsd.org>2018-05-28 08:55:12 +0000
committerMartin Pieuchot <mpi@cvs.openbsd.org>2018-05-28 08:55:12 +0000
commite7d195cdf41f4161b5851871187dace57b6c1b0c (patch)
tree24b12765d4524fbefe6213f060a4963bdc6c6660 /sys/kern
parentfd1e9ad8d30cfeb9d0cec034161aeaf2c5a26f2e (diff)
Returns EBUSY if dup2(2) is called for a LARVAL file.
This prevents a panic due to a double free if a program exits after having called accept(2) and dup2(2) on the same fd but without the corresponding connect(5). It will also allows us to simplify file descriptor locking. The error code has been choosed to match Linux's behavior. Pointed by Mathieu on tech@ after a discussion with guenther@. ok visa@
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/kern_descrip.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index 1d0e4b73edf..2d048683cd4 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_descrip.c,v 1.158 2018/05/08 09:03:58 mpi Exp $ */
+/* $OpenBSD: kern_descrip.c,v 1.159 2018/05/28 08:55:11 mpi Exp $ */
/* $NetBSD: kern_descrip.c,v 1.42 1996/03/30 22:24:38 christos Exp $ */
/*
@@ -634,13 +634,14 @@ finishdup(struct proc *p, struct file *fp, int old, int new,
return (EDEADLK);
}
- /*
- * Don't fd_getfile here. We want to closef LARVAL files and
- * closef can deal with that.
- */
oldfp = fdp->fd_ofiles[new];
- if (oldfp != NULL)
+ if (oldfp != NULL) {
+ if (!FILE_IS_USABLE(oldfp)) {
+ FRELE(fp, p);
+ return (EBUSY);
+ }
FREF(oldfp);
+ }
fdp->fd_ofiles[new] = fp;
fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] & ~UF_EXCLOSE;