Fix a race condition in FASYNC handling
authorJonathan Corbet <corbet@lwn.net>
Fri, 5 Dec 2008 23:12:48 +0000 (16:12 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 5 Dec 2008 23:35:10 +0000 (15:35 -0800)
Changeset a238b790d5f99c7832f9b73ac8847025815b85f7 (Call fasync()
functions without the BKL) introduced a race which could leave
file->f_flags in a state inconsistent with what the underlying
driver/filesystem believes.  Revert that change, and also fix the same
races in ioctl_fioasync() and ioctl_fionbio().

This is a minimal, short-term fix; the real fix will not involve the
BKL.

Reported-by: Oleg Nesterov <oleg@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: stable@kernel.org
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
fs/fcntl.c
fs/ioctl.c

index ac4f7db9f13452790e4d41daa8e6fc9633506259..549daf8005fb5da252252d7e5bf0e73185250169 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/signal.h>
 #include <linux/rcupdate.h>
 #include <linux/pid_namespace.h>
+#include <linux/smp_lock.h>
 
 #include <asm/poll.h>
 #include <asm/siginfo.h>
@@ -175,6 +176,11 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
        if (error)
                return error;
 
+       /*
+        * We still need a lock here for now to keep multiple FASYNC calls
+        * from racing with each other.
+        */
+       lock_kernel();
        if ((arg ^ filp->f_flags) & FASYNC) {
                if (filp->f_op && filp->f_op->fasync) {
                        error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
@@ -185,6 +191,7 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
 
        filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
  out:
+       unlock_kernel();
        return error;
 }
 
index d152856c371be4906b7effec1f3c42842a4cee2d..43e8b2c0664b1b3d302a6d46e3fa40185edb5ae7 100644 (file)
@@ -400,11 +400,9 @@ static int ioctl_fioasync(unsigned int fd, struct file *filp,
 
        /* Did FASYNC state change ? */
        if ((flag ^ filp->f_flags) & FASYNC) {
-               if (filp->f_op && filp->f_op->fasync) {
-                       lock_kernel();
+               if (filp->f_op && filp->f_op->fasync)
                        error = filp->f_op->fasync(fd, filp, on);
-                       unlock_kernel();
-               } else
+               else
                        error = -ENOTTY;
        }
        if (error)
@@ -440,11 +438,17 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
                break;
 
        case FIONBIO:
+               /* BKL needed to avoid races tweaking f_flags */
+               lock_kernel();
                error = ioctl_fionbio(filp, argp);
+               unlock_kernel();
                break;
 
        case FIOASYNC:
+               /* BKL needed to avoid races tweaking f_flags */
+               lock_kernel();
                error = ioctl_fioasync(fd, filp, argp);
+               unlock_kernel();
                break;
 
        case FIOQSIZE: