NFSv4: Ensure that we check lock exclusive/shared type against open modes
authorTrond Myklebust <Trond.Myklebust@netapp.com>
Wed, 18 Apr 2012 16:48:35 +0000 (12:48 -0400)
committerTrond Myklebust <Trond.Myklebust@netapp.com>
Thu, 19 Apr 2012 17:23:08 +0000 (13:23 -0400)
Since we may be simulating flock() locks using NFS byte range locks,
we can't rely on the VFS having checked the file open mode for us.

Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
Cc: stable@vger.kernel.org
fs/nfs/nfs4proc.c

index 3c787d02131be51b83aab2f52f29c7b540305f4c..ba837d977a1a0aae979409f8db688bee76992b1a 100644 (file)
@@ -4726,6 +4726,20 @@ nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
 
        if (state == NULL)
                return -ENOLCK;
+       /*
+        * Don't rely on the VFS having checked the file open mode,
+        * since it won't do this for flock() locks.
+        */
+       switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
+       case F_RDLCK:
+               if (!(filp->f_mode & FMODE_READ))
+                       return -EBADF;
+               break;
+       case F_WRLCK:
+               if (!(filp->f_mode & FMODE_WRITE))
+                       return -EBADF;
+       }
+
        do {
                status = nfs4_proc_setlk(state, cmd, request);
                if ((status != -EAGAIN) || IS_SETLK(cmd))