tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Making flock(2) more robust to invalid operations



Hi,

How about the attached patch which harden flock(2) against invalid
operations such as `LOCK_EX|LOCK_UN' ? The current implementation does
not make it fail ...

Thanks.

-- 
Nicolas Joly

Biological Software and Databanks.
Institut Pasteur, Paris.
Index: sys/kern/sys_descrip.c
===================================================================
RCS file: /cvsroot/src/sys/kern/sys_descrip.c,v
retrieving revision 1.16
diff -u -p -r1.16 sys_descrip.c
--- sys/kern/sys_descrip.c      10 Jun 2009 23:48:10 -0000      1.16
+++ sys/kern/sys_descrip.c      13 Oct 2009 15:30:41 -0000
@@ -585,21 +585,25 @@ sys_flock(struct lwp *l, const struct sy
        lf.l_whence = SEEK_SET;
        lf.l_start = 0;
        lf.l_len = 0;
-       if (how & LOCK_UN) {
+
+       switch (how & ~LOCK_NB) {
+       case LOCK_UN:
                lf.l_type = F_UNLCK;
                atomic_and_uint(&fp->f_flag, ~FHASLOCK);
                error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
                fd_putfile(fd);
                return error;
-       }
-       if (how & LOCK_EX) {
+       case LOCK_EX:
                lf.l_type = F_WRLCK;
-       } else if (how & LOCK_SH) {
+               break;
+       case LOCK_SH:
                lf.l_type = F_RDLCK;
-       } else {
+               break;
+       default:
                fd_putfile(fd);
                return EINVAL;
        }
+
        atomic_or_uint(&fp->f_flag, FHASLOCK);
        p = curproc;
        if (how & LOCK_NB) {
Index: lib/libc/sys/flock.2
===================================================================
RCS file: /cvsroot/src/lib/libc/sys/flock.2,v
retrieving revision 1.19
diff -u -p -r1.19 flock.2
--- lib/libc/sys/flock.2        13 May 2004 10:20:58 -0000      1.19
+++ lib/libc/sys/flock.2        13 Oct 2009 15:30:41 -0000
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)flock.2    8.2 (Berkeley) 12/11/93
 .\"
-.Dd December 11, 1993
+.Dd October 13, 2009
 .Dt FLOCK 2
 .Os
 .Sh NAME
@@ -136,7 +136,7 @@ refers to an object other than a file.
 .It Bq Er EINVAL
 The argument
 .Fa operation
-does not include one of
+does not include exactly one of
 .Dv LOCK_EX ,
 .Dv LOCK_SH
 or


Home | Main Index | Thread Index | Old Index