Skip to content

Commit cf01f4e

Browse files
committed
locks: only validate the lock vs. f_mode in F_SETLK codepaths
v2: replace missing break in switch statement (as pointed out by Dave Jones) commit bce7560 (locks: consolidate checks for compatible filp->f_mode values in setlk handlers) introduced a regression in the F_GETLK handler. flock64_to_posix_lock is a shared codepath between F_GETLK and F_SETLK, but the f_mode checks should only be applicable to the F_SETLK codepaths according to POSIX. Instead of just reverting the patch, add a new function to do this checking and have the F_SETLK handlers call it. Cc: Dave Jones <davej@redhat.com> Reported-and-Tested-by: Reuben Farrelly <reuben@reub.net> Signed-off-by: Jeff Layton <jlayton@poochiereds.net>
1 parent afcf0a2 commit cf01f4e

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

‎fs/locks.c‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -389,18 +389,6 @@ static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
389389
fl->fl_ops = NULL;
390390
fl->fl_lmops = NULL;
391391

392-
/* Ensure that fl->fl_filp has compatible f_mode */
393-
switch (l->l_type) {
394-
case F_RDLCK:
395-
if (!(filp->f_mode & FMODE_READ))
396-
return -EBADF;
397-
break;
398-
case F_WRLCK:
399-
if (!(filp->f_mode & FMODE_WRITE))
400-
return -EBADF;
401-
break;
402-
}
403-
404392
return assign_type(fl, l->l_type);
405393
}
406394

@@ -2034,6 +2022,22 @@ static int do_lock_file_wait(struct file *filp, unsigned int cmd,
20342022
return error;
20352023
}
20362024

2025+
/* Ensure that fl->fl_filp has compatible f_mode for F_SETLK calls */
2026+
static int
2027+
check_fmode_for_setlk(struct file_lock *fl)
2028+
{
2029+
switch (fl->fl_type) {
2030+
case F_RDLCK:
2031+
if (!(fl->fl_file->f_mode & FMODE_READ))
2032+
return -EBADF;
2033+
break;
2034+
case F_WRLCK:
2035+
if (!(fl->fl_file->f_mode & FMODE_WRITE))
2036+
return -EBADF;
2037+
}
2038+
return 0;
2039+
}
2040+
20372041
/* Apply the lock described by l to an open file descriptor.
20382042
* This implements both the F_SETLK and F_SETLKW commands of fcntl().
20392043
*/
@@ -2071,6 +2075,10 @@ int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
20712075
if (error)
20722076
goto out;
20732077

2078+
error = check_fmode_for_setlk(file_lock);
2079+
if (error)
2080+
goto out;
2081+
20742082
/*
20752083
* If the cmd is requesting file-private locks, then set the
20762084
* FL_OFDLCK flag and override the owner.
@@ -2206,6 +2214,10 @@ int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
22062214
if (error)
22072215
goto out;
22082216

2217+
error = check_fmode_for_setlk(file_lock);
2218+
if (error)
2219+
goto out;
2220+
22092221
/*
22102222
* If the cmd is requesting file-private locks, then set the
22112223
* FL_OFDLCK flag and override the owner.

0 commit comments

Comments
 (0)