Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 9 Nov 2006 13:41:19 +0100
From:      Ed Schouten <ed@fxq.nl>
To:        FreeBSD Hackers <freebsd-hackers@freebsd.org>
Subject:   [Patch] sys/kern/kern_descrip.c: remove double limit check in fcntl()
Message-ID:  <20061109124119.GB16100@hoeg.nl>

next in thread | raw e-mail | index | archive | help

--gatW/ieO32f1wygP
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hello,

I'm working on a project at school to develop a multimedia system (a la
Windows Media Center) based on FreeBSD. I was looking at some code in
sys/kern/kern_descrip.c to figure out how the fcntl() with F_DUPFD and
dup() differ.

I discovered that kern_fcntl() contains some redundant code. Right
before calling do_dup(), it locks the process and checks that the
minimal file descriptor is lower than the limit for the process. The
do_dup() call does exactly the same check almost at the beginning. This
causes the fcntl() call to call PROC_LOCK() once too much.

The patch below prevents this by performing this check by do_dup(). It
will prevent fcntl() from PROC_LOCK()'ing twice. It also fixes the
return value of fcntl(). The manual page states that it should return
EMFILE when it exceeds its limit, though the actual code sets EINVAL.

%%%
--- kern_descrip.c	Thu Nov  9 13:23:40 2006
+++ kern_descrip.c	Thu Nov  9 13:32:28 2006
@@ -355,7 +355,6 @@
 	struct proc *p;
 	char *pop;
 	struct vnode *vp;
-	u_int newmin;
 	int error, flg, tmp;
 	int giant_locked;
=20
@@ -393,16 +392,7 @@
 	case F_DUPFD:
 		/* mtx_assert(&Giant, MA_NOTOWNED); */
 		FILEDESC_UNLOCK(fdp);
-		newmin =3D arg;
-		PROC_LOCK(p);
-		if (newmin >=3D lim_cur(p, RLIMIT_NOFILE) ||
-		    newmin >=3D maxfilesperproc) {
-			PROC_UNLOCK(p);
-			error =3D EINVAL;
-			break;
-		}
-		PROC_UNLOCK(p);
-		error =3D do_dup(td, DUP_VARIABLE, fd, newmin, td->td_retval);
+		error =3D do_dup(td, DUP_VARIABLE, fd, arg, td->td_retval);
 		break;
=20
 	case F_GETFD:
%%%

--=20
 Ed Schouten <ed@fxq.nl>
 WWW: http://g-rave.nl/

--gatW/ieO32f1wygP
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFUyHv52SDGA2eCwURAmHFAJwPccsflvlDh7pQhIIUP+J4Qj6A9gCeMH0J
QGPZX0iudeCpI9i6Y2K1cN0=
=Z0Tk
-----END PGP SIGNATURE-----

--gatW/ieO32f1wygP--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20061109124119.GB16100>