Commit d7a06983 authored by Jeff Layton's avatar Jeff Layton

locks: fix locks_mandatory_locked to respect file-private locks

As Trond pointed out, you can currently deadlock yourself by setting a
file-private lock on a file that requires mandatory locking and then
trying to do I/O on it.

Avoid this problem by plumbing some knowledge of file-private locks into
the mandatory locking code. In order to do this, we must pass down
information about the struct file that's being used to
locks_verify_locked.
Reported-by: default avatarTrond Myklebust <trond.myklebust@primarydata.com>
Signed-off-by: default avatarJeff Layton <jlayton@redhat.com>
Acked-by: default avatarJ. Bruce Fields <bfields@redhat.com>
parent 90478939
...@@ -1155,13 +1155,14 @@ EXPORT_SYMBOL(posix_lock_file_wait); ...@@ -1155,13 +1155,14 @@ EXPORT_SYMBOL(posix_lock_file_wait);
/** /**
* locks_mandatory_locked - Check for an active lock * locks_mandatory_locked - Check for an active lock
* @inode: the file to check * @file: the file to check
* *
* Searches the inode's list of locks to find any POSIX locks which conflict. * Searches the inode's list of locks to find any POSIX locks which conflict.
* This function is called from locks_verify_locked() only. * This function is called from locks_verify_locked() only.
*/ */
int locks_mandatory_locked(struct inode *inode) int locks_mandatory_locked(struct file *file)
{ {
struct inode *inode = file_inode(file);
fl_owner_t owner = current->files; fl_owner_t owner = current->files;
struct file_lock *fl; struct file_lock *fl;
...@@ -1172,7 +1173,7 @@ int locks_mandatory_locked(struct inode *inode) ...@@ -1172,7 +1173,7 @@ int locks_mandatory_locked(struct inode *inode)
for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
if (!IS_POSIX(fl)) if (!IS_POSIX(fl))
continue; continue;
if (fl->fl_owner != owner) if (fl->fl_owner != owner && fl->fl_owner != (fl_owner_t)file)
break; break;
} }
spin_unlock(&inode->i_lock); spin_unlock(&inode->i_lock);
......
...@@ -2542,7 +2542,7 @@ static int handle_truncate(struct file *filp) ...@@ -2542,7 +2542,7 @@ static int handle_truncate(struct file *filp)
/* /*
* Refuse to truncate files with mandatory locks held on them. * Refuse to truncate files with mandatory locks held on them.
*/ */
error = locks_verify_locked(inode); error = locks_verify_locked(filp);
if (!error) if (!error)
error = security_path_truncate(path); error = security_path_truncate(path);
if (!error) { if (!error) {
......
...@@ -1912,6 +1912,11 @@ extern int current_umask(void); ...@@ -1912,6 +1912,11 @@ extern int current_umask(void);
extern void ihold(struct inode * inode); extern void ihold(struct inode * inode);
extern void iput(struct inode *); extern void iput(struct inode *);
static inline struct inode *file_inode(struct file *f)
{
return f->f_inode;
}
/* /sys/fs */ /* /sys/fs */
extern struct kobject *fs_kobj; extern struct kobject *fs_kobj;
...@@ -1921,7 +1926,7 @@ extern struct kobject *fs_kobj; ...@@ -1921,7 +1926,7 @@ extern struct kobject *fs_kobj;
#define FLOCK_VERIFY_WRITE 2 #define FLOCK_VERIFY_WRITE 2
#ifdef CONFIG_FILE_LOCKING #ifdef CONFIG_FILE_LOCKING
extern int locks_mandatory_locked(struct inode *); extern int locks_mandatory_locked(struct file *);
extern int locks_mandatory_area(int, struct inode *, struct file *, loff_t, size_t); extern int locks_mandatory_area(int, struct inode *, struct file *, loff_t, size_t);
/* /*
...@@ -1944,10 +1949,10 @@ static inline int mandatory_lock(struct inode *ino) ...@@ -1944,10 +1949,10 @@ static inline int mandatory_lock(struct inode *ino)
return IS_MANDLOCK(ino) && __mandatory_lock(ino); return IS_MANDLOCK(ino) && __mandatory_lock(ino);
} }
static inline int locks_verify_locked(struct inode *inode) static inline int locks_verify_locked(struct file *file)
{ {
if (mandatory_lock(inode)) if (mandatory_lock(file_inode(file)))
return locks_mandatory_locked(inode); return locks_mandatory_locked(file);
return 0; return 0;
} }
...@@ -2008,7 +2013,7 @@ static inline int break_deleg_wait(struct inode **delegated_inode) ...@@ -2008,7 +2013,7 @@ static inline int break_deleg_wait(struct inode **delegated_inode)
} }
#else /* !CONFIG_FILE_LOCKING */ #else /* !CONFIG_FILE_LOCKING */
static inline int locks_mandatory_locked(struct inode *inode) static inline int locks_mandatory_locked(struct file *file)
{ {
return 0; return 0;
} }
...@@ -2030,7 +2035,7 @@ static inline int mandatory_lock(struct inode *inode) ...@@ -2030,7 +2035,7 @@ static inline int mandatory_lock(struct inode *inode)
return 0; return 0;
} }
static inline int locks_verify_locked(struct inode *inode) static inline int locks_verify_locked(struct file *file)
{ {
return 0; return 0;
} }
...@@ -2297,11 +2302,6 @@ static inline bool execute_ok(struct inode *inode) ...@@ -2297,11 +2302,6 @@ static inline bool execute_ok(struct inode *inode)
return (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode); return (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode);
} }
static inline struct inode *file_inode(struct file *f)
{
return f->f_inode;
}
static inline void file_start_write(struct file *file) static inline void file_start_write(struct file *file)
{ {
if (!S_ISREG(file_inode(file)->i_mode)) if (!S_ISREG(file_inode(file)->i_mode))
......
...@@ -1299,7 +1299,7 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr, ...@@ -1299,7 +1299,7 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
/* /*
* Make sure there are no mandatory locks on the file. * Make sure there are no mandatory locks on the file.
*/ */
if (locks_verify_locked(inode)) if (locks_verify_locked(file))
return -EAGAIN; return -EAGAIN;
vm_flags |= VM_SHARED | VM_MAYSHARE; vm_flags |= VM_SHARED | VM_MAYSHARE;
......
...@@ -995,7 +995,7 @@ static int validate_mmap_request(struct file *file, ...@@ -995,7 +995,7 @@ static int validate_mmap_request(struct file *file,
(file->f_mode & FMODE_WRITE)) (file->f_mode & FMODE_WRITE))
return -EACCES; return -EACCES;
if (locks_verify_locked(file_inode(file))) if (locks_verify_locked(file))
return -EAGAIN; return -EAGAIN;
if (!(capabilities & BDI_CAP_MAP_DIRECT)) if (!(capabilities & BDI_CAP_MAP_DIRECT))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment