diff options
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/exec_script.c | 3 | ||||
-rw-r--r-- | sys/kern/kern_descrip.c | 69 | ||||
-rw-r--r-- | sys/kern/kern_event.c | 11 | ||||
-rw-r--r-- | sys/kern/kern_exec.c | 12 | ||||
-rw-r--r-- | sys/kern/sys_generic.c | 53 | ||||
-rw-r--r-- | sys/kern/sys_pipe.c | 4 | ||||
-rw-r--r-- | sys/kern/uipc_syscalls.c | 9 | ||||
-rw-r--r-- | sys/kern/uipc_usrreq.c | 5 | ||||
-rw-r--r-- | sys/kern/vfs_syscalls.c | 60 |
9 files changed, 88 insertions, 138 deletions
diff --git a/sys/kern/exec_script.c b/sys/kern/exec_script.c index 1ac12a36ef6..ee852e32222 100644 --- a/sys/kern/exec_script.c +++ b/sys/kern/exec_script.c @@ -1,4 +1,4 @@ -/* $OpenBSD: exec_script.c,v 1.13 2001/06/22 14:14:08 deraadt Exp $ */ +/* $OpenBSD: exec_script.c,v 1.14 2001/10/26 12:03:27 art Exp $ */ /* $NetBSD: exec_script.c,v 1.13 1996/02/04 02:15:06 christos Exp $ */ /* @@ -180,6 +180,7 @@ check_shell: fp->f_ops = &vnops; fp->f_data = (caddr_t) epp->ep_vp; fp->f_flag = FREAD; + FILE_SET_MATURE(fp); } #endif diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 77d7d8fdc6a..46ca7af89f6 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_descrip.c,v 1.39 2001/10/26 10:39:31 art Exp $ */ +/* $OpenBSD: kern_descrip.c,v 1.40 2001/10/26 12:03:27 art Exp $ */ /* $NetBSD: kern_descrip.c,v 1.42 1996/03/30 22:24:38 christos Exp $ */ /* @@ -183,6 +183,22 @@ fd_unused(fdp, fd) fdp->fd_lastfile = find_last_set(fdp, fd); } +struct file * +fd_getfile(fdp, fd) + struct filedesc *fdp; + int fd; +{ + struct file *fp; + + if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) + return (NULL); + + if (!FILE_IS_USABLE(fp)) + return (NULL); + + return (fp); +} + /* * System calls on descriptors. */ @@ -206,7 +222,7 @@ sys_dup(p, v, retval) int error; restart: - if ((u_int)old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) + if (fd_getfile(fdp, old) == NULL) return (EBADF); if ((error = fdalloc(p, 0, &new)) != 0) { if (error == ENOSPC) { @@ -237,8 +253,9 @@ sys_dup2(p, v, retval) int i, error; restart: - if ((u_int)old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL || - (u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur || + if (fd_getfile(fdp, old) == NULL) + return (EBADF); + if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur || (u_int)new >= maxfiles) return (EBADF); if (old == new) { @@ -283,8 +300,7 @@ sys_fcntl(p, v, retval) int newmin; restart: - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL) + if ((fp = fd_getfile(fdp, fd)) == NULL) return (EBADF); switch (SCARG(uap, cmd)) { @@ -519,7 +535,7 @@ sys_close(p, v, retval) int fd = SCARG(uap, fd); register struct filedesc *fdp = p->p_fd; - if ((u_int)fd >= fdp->fd_nfiles) + if (fd_getfile(fdp, fd) == NULL) return (EBADF); return (fdrelease(p, fd)); } @@ -544,8 +560,7 @@ sys_fstat(p, v, retval) struct stat ub; int error; - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL) + if ((fp = fd_getfile(fdp, fd)) == NULL) return (EBADF); error = (*fp->f_ops->fo_stat)(fp, &ub, p); if (error == 0) { @@ -578,8 +593,7 @@ sys_fpathconf(p, v, retval) struct file *fp; struct vnode *vp; - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL) + if ((fp = fd_getfile(fdp, fd)) == NULL) return (EBADF); switch (fp->f_type) { case DTYPE_PIPE: @@ -725,29 +739,6 @@ fdexpand(p) } /* - * Check to see whether n user file descriptors - * are available to the process p. - */ -int -fdavail(p, n) - struct proc *p; - register int n; -{ - register struct filedesc *fdp = p->p_fd; - register struct file **fpp; - register int i, lim; - - lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); - if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0) - return (1); - fpp = &fdp->fd_ofiles[fdp->fd_freefile]; - for (i = min(lim, fdp->fd_nfiles) - fdp->fd_freefile; --i >= 0; fpp++) - if (*fpp == NULL && --n <= 0) - return (1); - return (0); -} - -/* * Create a new open file structure and allocate * a file decriptor for the process that refers to it. */ @@ -782,6 +773,7 @@ restart: nfiles++; fp = pool_get(&file_pool, PR_WAITOK); bzero(fp, sizeof(struct file)); + fp->f_iflags = FIF_LARVAL; if ((fq = p->p_fd->fd_ofiles[0]) != NULL) { LIST_INSERT_AFTER(fq, fp, f_list); } else { @@ -1066,8 +1058,7 @@ sys_flock(p, v, retval) struct vnode *vp; struct flock lf; - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL) + if ((fp = fd_getfile(fdp, fd)) == NULL) return (EBADF); if (fp->f_type != DTYPE_VNODE) return (EOPNOTSUPP); @@ -1141,8 +1132,10 @@ dupfdopen(fdp, indx, dfd, mode, error) * as the new descriptor. */ fp = fdp->fd_ofiles[indx]; - if ((u_int)dfd >= fdp->fd_nfiles || - (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp) + if ((wfp = fd_getfile(fdp, dfd)) == NULL) + return (EBADF); + + if (fp == wfp) return (EBADF); /* diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c index e7aaffebca1..b5f15fcb6cb 100644 --- a/sys/kern/kern_event.c +++ b/sys/kern/kern_event.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_event.c,v 1.9 2001/07/17 01:51:37 provos Exp $ */ +/* $OpenBSD: kern_event.c,v 1.10 2001/10/26 12:03:27 art Exp $ */ /*- * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org> @@ -294,7 +294,8 @@ sys_kqueue(struct proc *p, void *v, register_t *retval) if (fdp->fd_knlistsize < 0) fdp->fd_knlistsize = 0; /* this process has a kq */ kq->kq_fdp = fdp; - return (error); + FILE_SET_MATURE(fp); + return (0); } int @@ -315,8 +316,7 @@ sys_kevent(struct proc *p, void *v, register_t *retval) struct timespec ts; int i, n, nerrors, error; - if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL || + if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL || (fp->f_type != DTYPE_KQUEUE)) return (EBADF); @@ -403,8 +403,7 @@ kqueue_register(struct kqueue *kq, struct kevent *kev, struct proc *p) if (fops->f_isfd) { /* validate descriptor */ - if ((u_int)kev->ident >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[kev->ident]) == NULL) + if ((fp = fd_getfile(fdp, kev->ident)) == NULL) return (EBADF); fp->f_count++; diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c index bb4ccaa8db6..ca619f10f2a 100644 --- a/sys/kern/kern_exec.c +++ b/sys/kern/kern_exec.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_exec.c,v 1.57 2001/09/19 20:50:58 mickey Exp $ */ +/* $OpenBSD: kern_exec.c,v 1.58 2001/10/26 12:03:27 art Exp $ */ /* $NetBSD: kern_exec.c,v 1.75 1996/02/09 18:59:28 christos Exp $ */ /*- @@ -506,9 +506,12 @@ sys_execve(p, v, retval) for (i = 0; i < 3; i++) { struct file *fp = NULL; - if (i < p->p_fd->fd_nfiles) - fp = p->p_fd->fd_ofiles[i]; - + /* + * NOTE - This will never return NULL because of + * unmature fds. The file descriptor table is not + * shared because we're suid. + */ + fp = fd_getfile(p->p_fd, i); #ifdef PROCFS /* * Close descriptors that are writing to procfs. @@ -558,6 +561,7 @@ sys_execve(p, v, retval) fp->f_type = DTYPE_VNODE; fp->f_ops = &vnops; fp->f_data = (caddr_t)vp; + FILE_SET_MATURE(fp); } } } else diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c index 5c2b5a3e2c5..278ffc24d97 100644 --- a/sys/kern/sys_generic.c +++ b/sys/kern/sys_generic.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sys_generic.c,v 1.29 2001/05/16 12:52:58 ho Exp $ */ +/* $OpenBSD: sys_generic.c,v 1.30 2001/10/26 12:03:27 art Exp $ */ /* $NetBSD: sys_generic.c,v 1.24 1996/03/29 00:25:32 cgd Exp $ */ /* @@ -86,17 +86,11 @@ sys_read(p, v, retval) struct file *fp; struct filedesc *fdp = p->p_fd; - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL || -#if notyet - (fp->f_iflags & FIF_WANTCLOSE) != 0 || -#endif - (fp->f_flag & FREAD) == 0) + if ((fp = fd_getfile(fdp, fd)) == NULL) + return (EBADF); + if ((fp->f_flag & FREAD) == 0) return (EBADF); -#if notyet - FILE_USE(fp); -#endif /* dofileread() will unuse the descriptor for us */ return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte), &fp->f_offset, retval)); @@ -182,17 +176,11 @@ sys_readv(p, v, retval) struct file *fp; struct filedesc *fdp = p->p_fd; - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL || -#if notyet - (fp->f_iflags & FIF_WANTCLOSE) != 0 || -#endif - (fp->f_flag & FREAD) == 0) + if ((fp = fd_getfile(fdp, fd)) == NULL) + return (EBADF); + if ((fp->f_flag & FREAD) == 0) return (EBADF); -#if notyet - FILE_USE(fp); -#endif /* dofilereadv() will unuse the descriptor for us */ return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt), &fp->f_offset, retval)); @@ -309,17 +297,11 @@ sys_write(p, v, retval) struct file *fp; struct filedesc *fdp = p->p_fd; - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL || -#if notyet - (fp->f_iflags & FIF_WANTCLOSE) != 0 || -#endif - (fp->f_flag & FWRITE) == 0) + if ((fp = fd_getfile(fdp, fd)) == NULL) + return (EBADF); + if ((fp->f_flag & FWRITE) == 0) return (EBADF); -#if notyet - FILE_USE(fp); -#endif /* dofilewrite() will unuse the descriptor for us */ return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte), &fp->f_offset, retval)); @@ -408,17 +390,11 @@ sys_writev(p, v, retval) struct file *fp; struct filedesc *fdp = p->p_fd; - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL || -#if notyet - (fp->f_iflags & FIF_WANTCLOSE) != 0 || -#endif - (fp->f_flag & FWRITE) == 0) + if ((fp = fd_getfile(fdp, fd)) == NULL) + return (EBADF); + if ((fp->f_flag & FWRITE) == 0) return (EBADF); -#if notyet - FILE_USE(fp); -#endif /* dofilewritev() will unuse the descriptor for us */ return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt), &fp->f_offset, retval)); @@ -544,8 +520,7 @@ sys_ioctl(p, v, retval) char stkbuf[STK_PARAMS]; fdp = p->p_fd; - if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL) + if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) return (EBADF); if ((fp->f_flag & (FREAD | FWRITE)) == 0) diff --git a/sys/kern/sys_pipe.c b/sys/kern/sys_pipe.c index 13084a9ed56..48290fb70dd 100644 --- a/sys/kern/sys_pipe.c +++ b/sys/kern/sys_pipe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sys_pipe.c,v 1.38 2001/09/19 20:50:58 mickey Exp $ */ +/* $OpenBSD: sys_pipe.c,v 1.39 2001/10/26 12:03:27 art Exp $ */ /* * Copyright (c) 1996 John S. Dyson @@ -143,6 +143,8 @@ sys_opipe(p, v, retval) rpipe->pipe_peer = wpipe; wpipe->pipe_peer = rpipe; + FILE_SET_MATURE(rf); + FILE_SET_MATURE(wf); return (0); free3: ffree(rf); diff --git a/sys/kern/uipc_syscalls.c b/sys/kern/uipc_syscalls.c index 28ef6de78ee..c25ad2915ca 100644 --- a/sys/kern/uipc_syscalls.c +++ b/sys/kern/uipc_syscalls.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_syscalls.c,v 1.42 2001/09/20 17:02:31 mpech Exp $ */ +/* $OpenBSD: uipc_syscalls.c,v 1.43 2001/10/26 12:03:27 art Exp $ */ /* $NetBSD: uipc_syscalls.c,v 1.19 1996/02/09 19:00:48 christos Exp $ */ /* @@ -91,6 +91,7 @@ sys_socket(p, v, retval) ffree(fp); } else { fp->f_data = (caddr_t)so; + FILE_SET_MATURE(fp); *retval = fd; } return (error); @@ -230,6 +231,7 @@ sys_accept(p, v, retval) } m_freem(nam); splx(s); + FILE_SET_MATURE(fp); return (error); } @@ -336,6 +338,8 @@ sys_socketpair(p, v, retval) } error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, rsv), 2 * sizeof (int)); + FILE_SET_MATURE(fp1); + FILE_SET_MATURE(fp2); if (error == 0) return (error); free4: @@ -1075,8 +1079,7 @@ getsock(fdp, fdes, fpp) { register struct file *fp; - if ((unsigned)fdes >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fdes]) == NULL) + if ((fp = fd_getfile(fdp, fdes)) == NULL) return (EBADF); if (fp->f_type != DTYPE_SOCKET) return (ENOTSOCK); diff --git a/sys/kern/uipc_usrreq.c b/sys/kern/uipc_usrreq.c index bb11b3d6a0f..46b675b3c96 100644 --- a/sys/kern/uipc_usrreq.c +++ b/sys/kern/uipc_usrreq.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_usrreq.c,v 1.14 2001/10/26 10:39:31 art Exp $ */ +/* $OpenBSD: uipc_usrreq.c,v 1.15 2001/10/26 12:03:27 art Exp $ */ /* $NetBSD: uipc_usrreq.c,v 1.18 1996/02/09 19:00:50 christos Exp $ */ /* @@ -751,8 +751,7 @@ unp_internalize(control, p) ip = (int *)(cm + 1); for (i = 0; i < oldfds; i++) { fd = *ip++; - if ((unsigned)fd >= fdp->fd_nfiles || - fdp->fd_ofiles[fd] == NULL) + if (fd_getfile(fdp, fd) == NULL) return (EBADF); if (fdp->fd_ofiles[fd]->f_count == LONG_MAX-2 || fdp->fd_ofiles[fd]->f_msgcount == LONG_MAX-2) diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c index 2e1cf5f8103..36f606fe474 100644 --- a/sys/kern/vfs_syscalls.c +++ b/sys/kern/vfs_syscalls.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vfs_syscalls.c,v 1.80 2001/07/26 20:24:47 millert Exp $ */ +/* $OpenBSD: vfs_syscalls.c,v 1.81 2001/10/26 12:03:27 art Exp $ */ /* $NetBSD: vfs_syscalls.c,v 1.71 1996/04/23 10:29:02 mycroft Exp $ */ /* @@ -937,6 +937,7 @@ sys_open(p, v, retval) } VOP_UNLOCK(vp, 0, p); *retval = indx; + FILE_SET_MATURE(fp); return (0); } @@ -1098,6 +1099,7 @@ sys_fhopen(p, v, retval) } VOP_UNLOCK(vp, 0, p); *retval = indx; + FILE_SET_MATURE(fp); return (0); bad: @@ -1506,8 +1508,7 @@ sys_lseek(p, v, retval) struct vnode *vp; int error, special; - if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL) + if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) return (EBADF); if (fp->f_type != DTYPE_VNODE) return (ESPIPE); @@ -2599,7 +2600,7 @@ getvnode(fdp, fd, fpp) { struct file *fp; - if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) + if ((fp = fd_getfile(fdp, fd)) == NULL) return (EBADF); if (fp->f_type != DTYPE_VNODE) return (EINVAL); @@ -2629,17 +2630,10 @@ sys_pread(p, v, retval) off_t offset; int error, fd = SCARG(uap, fd); - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL || -#if notyet - (fp->f_iflags & FIF_WANTCLOSE) != 0 || -#endif - (fp->f_flag & FREAD) == 0) + if ((fp = fd_getfile(fdp, fd)) == NULL) + return (EBADF); + if ((fp->f_flag & FREAD) == 0) return (EBADF); - -#if notyet - FILE_USE(fp); -#endif vp = (struct vnode *)fp->f_data; if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { @@ -2682,17 +2676,10 @@ sys_preadv(p, v, retval) off_t offset; int error, fd = SCARG(uap, fd); - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL || -#if notyet - (fp->f_iflags & FIF_WANTCLOSE) != 0 || -#endif - (fp->f_flag & FREAD) == 0) + if ((fp = fd_getfile(fdp, fd)) == NULL) + return (EBADF); + if ((fp->f_flag & FREAD) == 0) return (EBADF); - -#if notyet - FILE_USE(fp); -#endif vp = (struct vnode *)fp->f_data; if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { @@ -2735,17 +2722,10 @@ sys_pwrite(p, v, retval) off_t offset; int error, fd = SCARG(uap, fd); - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL || -#if notyet - (fp->f_iflags & FIF_WANTCLOSE) != 0 || -#endif - (fp->f_flag & FWRITE) == 0) + if ((fp = fd_getfile(fdp, fd)) == NULL) + return (EBADF); + if ((fp->f_flag & FWRITE) == 0) return (EBADF); - -#if notyet - FILE_USE(fp); -#endif vp = (struct vnode *)fp->f_data; if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { @@ -2789,17 +2769,11 @@ sys_pwritev(p, v, retval) off_t offset; int error, fd = SCARG(uap, fd); - if ((u_int)fd >= fdp->fd_nfiles || - (fp = fdp->fd_ofiles[fd]) == NULL || -#if notyet - (fp->f_iflags & FIF_WANTCLOSE) != 0 || -#endif - (fp->f_flag & FWRITE) == 0) + if ((fp = fd_getfile(fdp, fd)) == NULL) + return (EBADF); + if ((fp->f_flag & FWRITE) == 0) return (EBADF); -#if notyet - FILE_USE(fp); -#endif vp = (struct vnode *)fp->f_data; if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { error = ESPIPE; |