Skip to content

Commit 14186fe

Browse files
committed
Merge tag 'locks-v3.15-4' of git://git.samba.org/jlayton/linux
Pull file locking fix from Jeff Layton: "Fix for regression in handling of F_GETLK commands" * tag 'locks-v3.15-4' of git://git.samba.org/jlayton/linux: locks: only validate the lock vs. f_mode in F_SETLK codepaths
2 parents 77d9278 + cf01f4e commit 14186fe

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

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)