From 2edcc1f41aebd0a87cd76a3bec6182a6f7dd0e26 Mon Sep 17 00:00:00 2001 From: trasz Date: Sun, 18 Oct 2015 14:01:20 +0000 Subject: [PATCH] MFC r281726: Optimize the O_NOCTTY handling hack in linux_common_open(). MFC r281728: Add back fdrop() missed in r281726. Sponsored by: The FreeBSD Foundation git-svn-id: svn://svn.freebsd.org/base/stable/10@289505 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- sys/compat/linux/linux_file.c | 58 +++++++++++++++++------------------ 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/sys/compat/linux/linux_file.c b/sys/compat/linux/linux_file.c index 3076eece5..19104a445 100644 --- a/sys/compat/linux/linux_file.c +++ b/sys/compat/linux/linux_file.c @@ -132,39 +132,39 @@ linux_common_open(struct thread *td, int dirfd, char *path, int l_flags, int mod /* XXX LINUX_O_NOATIME: unable to be easily implemented. */ error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode); - - if (!error) { - fd = td->td_retval[0]; - /* - * XXX In between kern_open() and fget(), another process - * having the same filedesc could use that fd without - * checking below. - */ - error = fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp); - if (!error) { - sx_slock(&proctree_lock); - PROC_LOCK(p); - if (!(bsd_flags & O_NOCTTY) && - SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) { - PROC_UNLOCK(p); - sx_unlock(&proctree_lock); - /* XXXPJD: Verify if TIOCSCTTY is allowed. */ - if (fp->f_type == DTYPE_VNODE) - (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, - td->td_ucred, td); - } else { - PROC_UNLOCK(p); - sx_sunlock(&proctree_lock); - } + if (error != 0) + goto done; + + if (bsd_flags & O_NOCTTY) + goto done; + + /* + * XXX In between kern_open() and fget(), another process + * having the same filedesc could use that fd without + * checking below. + */ + fd = td->td_retval[0]; + if (fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp) == 0) { + if (fp->f_type != DTYPE_VNODE) { fdrop(fp, td); - /* - * XXX as above, fdrop()/kern_close() pair is racy. - */ - if (error) - kern_close(td, fd); + goto done; + } + sx_slock(&proctree_lock); + PROC_LOCK(p); + if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) { + PROC_UNLOCK(p); + sx_sunlock(&proctree_lock); + /* XXXPJD: Verify if TIOCSCTTY is allowed. */ + (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, + td->td_ucred, td); + } else { + PROC_UNLOCK(p); + sx_sunlock(&proctree_lock); } + fdrop(fp, td); } +done: #ifdef DEBUG if (ldebug(open)) printf(LMSG("open returns error %d"), error); -- 2.45.0