Skip to content

Commit

Permalink
locks: have flock_lock_file take an inode pointer instead of a filp
Browse files Browse the repository at this point in the history
...and rename it to better describe how it works.

In order to fix a use-after-free in NFS, we need to be able to remove
locks from an inode after the filp associated with them may have already
been freed. flock_lock_file already only dereferences the filp to get to
the inode, so just change it so the callers do that.

All of the callers already pass in a lock request that has the fl_file
set properly, so we don't need to pass it in individually. With that
change it now only dereferences the filp to get to the inode, so just
push that out to the callers.

Signed-off-by: Jeff Layton <jeff.layton@primarydata.com>
Reviewed-by: "J. Bruce Fields" <bfields@fieldses.org>
Tested-by: "J. Bruce Fields" <bfields@fieldses.org>
  • Loading branch information
Jeff Layton committed Jul 13, 2015
1 parent ed05676 commit bcd7f78
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions fs/locks.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,12 +862,11 @@ static int posix_locks_deadlock(struct file_lock *caller_fl,
* whether or not a lock was successfully freed by testing the return
* value for -ENOENT.
*/
static int flock_lock_file(struct file *filp, struct file_lock *request)
static int flock_lock_inode(struct inode *inode, struct file_lock *request)
{
struct file_lock *new_fl = NULL;
struct file_lock *fl;
struct file_lock_context *ctx;
struct inode *inode = file_inode(filp);
int error = 0;
bool found = false;
LIST_HEAD(dispose);
Expand All @@ -890,7 +889,7 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
goto find_conflict;

list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
if (filp != fl->fl_file)
if (request->fl_file != fl->fl_file)
continue;
if (request->fl_type == fl->fl_type)
goto out;
Expand Down Expand Up @@ -1862,7 +1861,7 @@ int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
int error;
might_sleep();
for (;;) {
error = flock_lock_file(filp, fl);
error = flock_lock_inode(file_inode(filp), fl);
if (error != FILE_LOCK_DEFERRED)
break;
error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
Expand Down Expand Up @@ -2401,15 +2400,16 @@ locks_remove_flock(struct file *filp)
.fl_type = F_UNLCK,
.fl_end = OFFSET_MAX,
};
struct file_lock_context *flctx = file_inode(filp)->i_flctx;
struct inode *inode = file_inode(filp);
struct file_lock_context *flctx = inode->i_flctx;

if (list_empty(&flctx->flc_flock))
return;

if (filp->f_op->flock)
filp->f_op->flock(filp, F_SETLKW, &fl);
else
flock_lock_file(filp, &fl);
flock_lock_inode(inode, &fl);

if (fl.fl_ops && fl.fl_ops->fl_release_private)
fl.fl_ops->fl_release_private(&fl);
Expand Down

0 comments on commit bcd7f78

Please sign in to comment.