From d988e6496b65d119543ad814244e09724611c26b Mon Sep 17 00:00:00 2001 From: Martin Pieuchot Date: Mon, 9 Apr 2018 13:59:04 +0000 Subject: Revert previous, it introduced a bug found the hardway by landry@. --- sys/kern/kern_descrip.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 2079f460065..5c80fc47d4b 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_descrip.c,v 1.146 2018/04/09 09:57:13 mpi Exp $ */ +/* $OpenBSD: kern_descrip.c,v 1.147 2018/04/09 13:59:03 mpi Exp $ */ /* $NetBSD: kern_descrip.c,v 1.42 1996/03/30 22:24:38 christos Exp $ */ /* @@ -591,7 +591,7 @@ out: } /* - * Common code for dup, dup2, dupfdopen and fcntl(F_DUPFD). + * Common code for dup, dup2, and fcntl(F_DUPFD). */ int finishdup(struct proc *p, struct file *fp, int old, int new, @@ -1274,8 +1274,6 @@ dupfdopen(struct proc *p, int indx, int mode) struct filedesc *fdp = p->p_fd; int dupfd = p->p_dupfd; struct file *wfp; - int error, flags; - register_t dummy; fdpassertlocked(fdp); @@ -1299,26 +1297,24 @@ dupfdopen(struct proc *p, int indx, int mode) */ if ((wfp = fd_getfile(fdp, dupfd)) == NULL) return (EBADF); - FREF(wfp); /* * Check that the mode the file is being opened for is a * subset of the mode of the existing descriptor. */ - if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) { - FRELE(wfp, p); + if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) return (EACCES); - } + if (wfp->f_count == LONG_MAX-2) + return (EDEADLK); - flags = fdp->fd_ofileflags[indx] & UF_EXCLOSE; + fdp->fd_ofiles[indx] = wfp; + fdp->fd_ofileflags[indx] = (fdp->fd_ofileflags[indx] & UF_EXCLOSE) | + (fdp->fd_ofileflags[dupfd] & ~UF_EXCLOSE); if (ISSET(p->p_p->ps_flags, PS_PLEDGE)) - flags |= UF_PLEDGED; - - /* finishdup() does FRELE */ - error = finishdup(p, wfp, dupfd, indx, &dummy, 1); - if (error == 0) - fdp->fd_ofileflags[indx] |= flags; - return (error); + fdp->fd_ofileflags[indx] |= UF_PLEDGED; + wfp->f_count++; + fd_used(fdp, indx); + return (0); } /* -- cgit v1.2.3