aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Corbet <corbet@lwn.net>2008-12-05 18:12:48 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2008-12-05 18:35:10 -0500
commit218d11a8b071b23b76c484fd5f72a4fe3306801e (patch)
tree6656f841973496b0717117f8031d22bea019fd00
parentf2f1fa78a155524b849edf359e42a3001ea652c0 (diff)
Fix a race condition in FASYNC handling
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>
-rw-r--r--fs/fcntl.c7
-rw-r--r--fs/ioctl.c12
2 files changed, 15 insertions, 4 deletions
diff --git a/fs/fcntl.c b/fs/fcntl.c
index ac4f7db9f134..549daf8005fb 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -19,6 +19,7 @@
19#include <linux/signal.h> 19#include <linux/signal.h>
20#include <linux/rcupdate.h> 20#include <linux/rcupdate.h>
21#include <linux/pid_namespace.h> 21#include <linux/pid_namespace.h>
22#include <linux/smp_lock.h>
22 23
23#include <asm/poll.h> 24#include <asm/poll.h>
24#include <asm/siginfo.h> 25#include <asm/siginfo.h>
@@ -175,6 +176,11 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
175 if (error) 176 if (error)
176 return error; 177 return error;
177 178
179 /*
180 * We still need a lock here for now to keep multiple FASYNC calls
181 * from racing with each other.
182 */
183 lock_kernel();
178 if ((arg ^ filp->f_flags) & FASYNC) { 184 if ((arg ^ filp->f_flags) & FASYNC) {
179 if (filp->f_op && filp->f_op->fasync) { 185 if (filp->f_op && filp->f_op->fasync) {
180 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0); 186 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)
185 191
186 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK); 192 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
187 out: 193 out:
194 unlock_kernel();
188 return error; 195 return error;
189} 196}
190 197
diff --git a/fs/ioctl.c b/fs/ioctl.c
index d152856c371b..43e8b2c0664b 100644
--- a/fs/ioctl.c
+++ b/fs/ioctl.c
@@ -400,11 +400,9 @@ static int ioctl_fioasync(unsigned int fd, struct file *filp,
400 400
401 /* Did FASYNC state change ? */ 401 /* Did FASYNC state change ? */
402 if ((flag ^ filp->f_flags) & FASYNC) { 402 if ((flag ^ filp->f_flags) & FASYNC) {
403 if (filp->f_op && filp->f_op->fasync) { 403 if (filp->f_op && filp->f_op->fasync)
404 lock_kernel();
405 error = filp->f_op->fasync(fd, filp, on); 404 error = filp->f_op->fasync(fd, filp, on);
406 unlock_kernel(); 405 else
407 } else
408 error = -ENOTTY; 406 error = -ENOTTY;
409 } 407 }
410 if (error) 408 if (error)
@@ -440,11 +438,17 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
440 break; 438 break;
441 439
442 case FIONBIO: 440 case FIONBIO:
441 /* BKL needed to avoid races tweaking f_flags */
442 lock_kernel();
443 error = ioctl_fionbio(filp, argp); 443 error = ioctl_fionbio(filp, argp);
444 unlock_kernel();
444 break; 445 break;
445 446
446 case FIOASYNC: 447 case FIOASYNC:
448 /* BKL needed to avoid races tweaking f_flags */
449 lock_kernel();
447 error = ioctl_fioasync(fd, filp, argp); 450 error = ioctl_fioasync(fd, filp, argp);
451 unlock_kernel();
448 break; 452 break;
449 453
450 case FIOQSIZE: 454 case FIOQSIZE: