Skip to content

Commit bce7560

Browse files
committed
locks: consolidate checks for compatible filp->f_mode values in setlk 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>
1 parent ef12e72 commit bce7560

1 file changed

Lines changed: 12 additions & 34 deletions

File tree

‎fs/locks.c‎

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

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

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

2028-
error = -EBADF;
2029-
switch (flock.l_type) {
2030-
case F_RDLCK:
2031-
if (!(filp->f_mode & FMODE_READ))
2032-
goto out;
2033-
break;
2034-
case F_WRLCK:
2035-
if (!(filp->f_mode & FMODE_WRITE))
2036-
goto out;
2037-
break;
2038-
case F_UNLCK:
2039-
break;
2040-
default:
2041-
error = -EINVAL;
2042-
goto out;
2043-
}
2044-
20452040
error = do_lock_file_wait(filp, cmd, file_lock);
20462041

20472042
/*
@@ -2143,23 +2138,6 @@ int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
21432138
file_lock->fl_flags |= FL_SLEEP;
21442139
}
21452140

2146-
error = -EBADF;
2147-
switch (flock.l_type) {
2148-
case F_RDLCK:
2149-
if (!(filp->f_mode & FMODE_READ))
2150-
goto out;
2151-
break;
2152-
case F_WRLCK:
2153-
if (!(filp->f_mode & FMODE_WRITE))
2154-
goto out;
2155-
break;
2156-
case F_UNLCK:
2157-
break;
2158-
default:
2159-
error = -EINVAL;
2160-
goto out;
2161-
}
2162-
21632141
error = do_lock_file_wait(filp, cmd, file_lock);
21642142

21652143
/*

0 commit comments

Comments
 (0)