Commit f5aaaa3e authored by Andrew Morton's avatar Andrew Morton Committed by Linus Torvalds

[PATCH] file locking memory leak

From: Matthew Wilcox <willy@debian.org>

This patch fixes a memory leak in the file locking code.  Each attempt to
unlock a file would result in the leak of a file lock.  Many thanks to
Martin Josefsson for providing the testcase which enabled me to figure out
the problem.
parent a8424285
...@@ -221,7 +221,7 @@ void locks_copy_lock(struct file_lock *new, struct file_lock *fl) ...@@ -221,7 +221,7 @@ void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
static inline int flock_translate_cmd(int cmd) { static inline int flock_translate_cmd(int cmd) {
if (cmd & LOCK_MAND) if (cmd & LOCK_MAND)
return cmd & (LOCK_MAND | LOCK_RW); return cmd & (LOCK_MAND | LOCK_RW);
switch (cmd &~ LOCK_NB) { switch (cmd) {
case LOCK_SH: case LOCK_SH:
return F_RDLCK; return F_RDLCK;
case LOCK_EX: case LOCK_EX:
...@@ -233,8 +233,8 @@ static inline int flock_translate_cmd(int cmd) { ...@@ -233,8 +233,8 @@ static inline int flock_translate_cmd(int cmd) {
} }
/* Fill in a file_lock structure with an appropriate FLOCK lock. */ /* Fill in a file_lock structure with an appropriate FLOCK lock. */
static int flock_make_lock(struct file *filp, static int flock_make_lock(struct file *filp, struct file_lock **lock,
struct file_lock **lock, unsigned int cmd) unsigned int cmd)
{ {
struct file_lock *fl; struct file_lock *fl;
int type = flock_translate_cmd(cmd); int type = flock_translate_cmd(cmd);
...@@ -247,7 +247,7 @@ static int flock_make_lock(struct file *filp, ...@@ -247,7 +247,7 @@ static int flock_make_lock(struct file *filp,
fl->fl_file = filp; fl->fl_file = filp;
fl->fl_pid = current->tgid; fl->fl_pid = current->tgid;
fl->fl_flags = (cmd & LOCK_NB) ? FL_FLOCK : FL_FLOCK | FL_SLEEP; fl->fl_flags = FL_FLOCK;
fl->fl_type = type; fl->fl_type = type;
fl->fl_end = OFFSET_MAX; fl->fl_end = OFFSET_MAX;
...@@ -1265,9 +1265,8 @@ int fcntl_setlease(unsigned int fd, struct file *filp, long arg) ...@@ -1265,9 +1265,8 @@ int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
locks_free_lock(fl); locks_free_lock(fl);
goto out_unlock; goto out_unlock;
} }
fl->fl_next = *before;
*before = fl; locks_insert_lock(before, fl);
list_add(&fl->fl_link, &file_lock_list);
error = f_setown(filp, current->tgid, 1); error = f_setown(filp, current->tgid, 1);
out_unlock: out_unlock:
...@@ -1298,6 +1297,7 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd) ...@@ -1298,6 +1297,7 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
{ {
struct file *filp; struct file *filp;
struct file_lock *lock; struct file_lock *lock;
int can_sleep, unlock;
int error; int error;
error = -EBADF; error = -EBADF;
...@@ -1305,12 +1305,18 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd) ...@@ -1305,12 +1305,18 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
if (!filp) if (!filp)
goto out; goto out;
if ((cmd != LOCK_UN) && !(cmd & LOCK_MAND) && !(filp->f_mode & 3)) can_sleep = !(cmd & LOCK_NB);
cmd &= ~LOCK_NB;
unlock = (cmd == LOCK_UN);
if (!unlock && !(cmd & LOCK_MAND) && !(filp->f_mode & 3))
goto out_putf; goto out_putf;
error = flock_make_lock(filp, &lock, cmd); error = flock_make_lock(filp, &lock, cmd);
if (error) if (error)
goto out_putf; goto out_putf;
if (can_sleep)
lock->fl_flags |= FL_SLEEP;
error = security_file_lock(filp, cmd); error = security_file_lock(filp, cmd);
if (error) if (error)
...@@ -1318,7 +1324,7 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd) ...@@ -1318,7 +1324,7 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
for (;;) { for (;;) {
error = flock_lock_file(filp, lock); error = flock_lock_file(filp, lock);
if ((error != -EAGAIN) || (cmd & LOCK_NB)) if ((error != -EAGAIN) || !can_sleep)
break; break;
error = wait_event_interruptible(lock->fl_wait, !lock->fl_next); error = wait_event_interruptible(lock->fl_wait, !lock->fl_next);
if (!error) if (!error)
...@@ -1329,7 +1335,7 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd) ...@@ -1329,7 +1335,7 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
} }
out_free: out_free:
if (error) { if (list_empty(&lock->fl_link)) {
locks_free_lock(lock); locks_free_lock(lock);
} }
......
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