public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Memory shortage can result in inconsistent flocks state
@ 2007-09-11 12:38 Pavel Emelyanov
  2007-09-12 19:06 ` J. Bruce Fields
  2007-09-13 19:27 ` Chuck Ebbert
  0 siblings, 2 replies; 7+ messages in thread
From: Pavel Emelyanov @ 2007-09-11 12:38 UTC (permalink / raw)
  To: Trond Myklebust, J. Bruce Fields, Andrew Morton
  Cc: Linux Kernel Mailing List, devel

When the flock_lock_file() is called to change the flock
from F_RDLCK to F_WRLCK or vice versa the existing flock
can be removed without appropriate warning.

Look:
        for_each_lock(inode, before) {
                struct file_lock *fl = *before;
                if (IS_POSIX(fl))
                        break;
                if (IS_LEASE(fl))
                        continue;
                if (filp != fl->fl_file)
                        continue;
                if (request->fl_type == fl->fl_type)
                        goto out;
                found = 1;
                locks_delete_lock(before); <<<<<< !
                break;
        }

if after this point the subsequent locks_alloc_lock() will
fail the return code will be -ENOMEM, but the existing lock
is already removed.

This is a known feature that such "re-locking" is not atomic,
but in the racy case the file should stay locked (although by
some other process), but in this case the file will be unlocked.

The proposal is to prepare the lock in advance keeping no chance
to fail in the future code.

Found during making the flocks pid-namespaces aware.

Signed-off-by: Pavel Emelyanov <xemul@openvz.org>

---

diff --git a/fs/locks.c b/fs/locks.c
index 0db1a14..f59d066 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -732,6 +732,14 @@ static int flock_lock_file(struct file *
 	lock_kernel();
 	if (request->fl_flags & FL_ACCESS)
 		goto find_conflict;
+
+	if (request->fl_type != F_UNLCK) {
+		error = -ENOMEM;
+		new_fl = locks_alloc_lock();
+		if (new_fl == NULL)
+			goto out;
+	}
+
 	for_each_lock(inode, before) {
 		struct file_lock *fl = *before;
 		if (IS_POSIX(fl))
@@ -753,10 +761,6 @@ static int flock_lock_file(struct file *
 		goto out;
 	}
 
-	error = -ENOMEM;
-	new_fl = locks_alloc_lock();
-	if (new_fl == NULL)
-		goto out;
 	/*
 	 * If a higher-priority process was blocked on the old file lock,
 	 * give it the opportunity to lock the file.

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-09-13 19:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-09-11 12:38 [PATCH] Memory shortage can result in inconsistent flocks state Pavel Emelyanov
2007-09-12 19:06 ` J. Bruce Fields
2007-09-13  6:04   ` Pavel Emelyanov
2007-09-13  7:16     ` Balbir Singh
2007-09-13 19:27 ` Chuck Ebbert
2007-09-13 19:34   ` J. Bruce Fields
2007-09-13 19:45     ` Chuck Ebbert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox