aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorJeff Layton <jlayton@redhat.com>2014-02-03 12:13:06 -0500
committerJeff Layton <jlayton@redhat.com>2014-03-31 08:24:42 -0400
commit24cbe7845ea50b636ab2218b9d648270ff55f148 (patch)
tree89769bf1271ff804c830d0ba7898294a5762b7a6 /include
parent18156e7e66e5147d2483382a1e3817d2401ca1a8 (diff)
locks: close potential race between setlease and open
As Al Viro points out, there is an unlikely, but possible race between opening a file and setting a lease on it. generic_add_lease is done with the i_lock held, but the inode->i_flock check in break_lease is lockless. It's possible for another task doing an open to do the entire pathwalk and call break_lease between the point where generic_add_lease checks for a conflicting open and adds the lease to the list. If this occurs, we can end up with a lease set on the file with a conflicting open. To guard against that, check again for a conflicting open after adding the lease to the i_flock list. If the above race occurs, then we can simply unwind the lease setting and return -EAGAIN. Because we take dentry references and acquire write access on the file before calling break_lease, we know that if the i_flock list is empty when the open caller goes to check it then the necessary refcounts have already been incremented. Thus the additional check for a conflicting open will see that there is one and the setlease call will fail. Cc: Bruce Fields <bfields@fieldses.org> Cc: David Howells <dhowells@redhat.com> Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> Reported-by: Al Viro <viro@ZenIV.linux.org.uk> Signed-off-by: Jeff Layton <jlayton@redhat.com> Signed-off-by: J. Bruce Fields <bfields@fieldses.org>
Diffstat (limited to 'include')
-rw-r--r--include/linux/fs.h6
1 files changed, 6 insertions, 0 deletions
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 09f553c59813..df8474408331 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1964,6 +1964,12 @@ static inline int locks_verify_truncate(struct inode *inode,
1964 1964
1965static inline int break_lease(struct inode *inode, unsigned int mode) 1965static inline int break_lease(struct inode *inode, unsigned int mode)
1966{ 1966{
1967 /*
1968 * Since this check is lockless, we must ensure that any refcounts
1969 * taken are done before checking inode->i_flock. Otherwise, we could
1970 * end up racing with tasks trying to set a new lease on this file.
1971 */
1972 smp_mb();
1967 if (inode->i_flock) 1973 if (inode->i_flock)
1968 return __break_lease(inode, mode, FL_LEASE); 1974 return __break_lease(inode, mode, FL_LEASE);
1969 return 0; 1975 return 0;