Skip to content

Commit

Permalink
locks: consolidate checks for compatible filp->f_mode values in setlk…
Browse files Browse the repository at this point in the history
… handlers

Move this check into flock64_to_posix_lock instead of duplicating it in
two places. This also fixes a minor wart in the code where we continue
referring to the struct flock after converting it to struct file_lock.

Acked-by: J. Bruce Fields <bfields@fieldses.org>
Signed-off-by: Jeff Layton <jlayton@redhat.com>
  • Loading branch information
Jeff Layton committed Mar 31, 2014
1 parent ef12e72 commit bce7560
Showing 1 changed file with 12 additions and 34 deletions.
46 changes: 12 additions & 34 deletions fs/locks.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,18 @@ static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
fl->fl_ops = NULL;
fl->fl_lmops = NULL;

/* Ensure that fl->fl_filp has compatible f_mode */
switch (l->l_type) {
case F_RDLCK:
if (!(filp->f_mode & FMODE_READ))
return -EBADF;
break;
case F_WRLCK:
if (!(filp->f_mode & FMODE_WRITE))
return -EBADF;
break;
}

return assign_type(fl, l->l_type);
}

Expand Down Expand Up @@ -2025,23 +2037,6 @@ int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
file_lock->fl_flags |= FL_SLEEP;
}

error = -EBADF;
switch (flock.l_type) {
case F_RDLCK:
if (!(filp->f_mode & FMODE_READ))
goto out;
break;
case F_WRLCK:
if (!(filp->f_mode & FMODE_WRITE))
goto out;
break;
case F_UNLCK:
break;
default:
error = -EINVAL;
goto out;
}

error = do_lock_file_wait(filp, cmd, file_lock);

/*
Expand Down Expand Up @@ -2143,23 +2138,6 @@ int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
file_lock->fl_flags |= FL_SLEEP;
}

error = -EBADF;
switch (flock.l_type) {
case F_RDLCK:
if (!(filp->f_mode & FMODE_READ))
goto out;
break;
case F_WRLCK:
if (!(filp->f_mode & FMODE_WRITE))
goto out;
break;
case F_UNLCK:
break;
default:
error = -EINVAL;
goto out;
}

error = do_lock_file_wait(filp, cmd, file_lock);

/*
Expand Down

0 comments on commit bce7560

Please sign in to comment.