From patchwork Thu Apr 19 09:45:03 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [4/4] NFSv4: Ensure that we check lock exclusive/shared type against open modes Date: Wed, 18 Apr 2012 23:45:03 -0000 From: Luis Henriques X-Patchwork-Id: 153711 Message-Id: <1334828703-11838-5-git-send-email-luis.henriques@canonical.com> To: kernel-team@lists.ubuntu.com From: Trond Myklebust BugLink: http://bugs.launchpad.net/bugs/974664 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 Cc: stable@vger.kernel.org (cherry picked from commit 5de4815015e550bdd33f39650554325540356f0c git://git.linux-nfs.org/projects/trondmy/linux-nfs.git) Signed-off-by: Luis Henriques --- fs/nfs/nfs4proc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c index 39c9cd2..8b16560 100644 --- a/fs/nfs/nfs4proc.c +++ b/fs/nfs/nfs4proc.c @@ -4601,6 +4601,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))