]> granicus.if.org Git - spl/commitdiff
Fix race between getf() and areleasef()
authorRichard Yao <richard.yao@clusterhq.com>
Wed, 4 Nov 2015 21:41:13 +0000 (16:41 -0500)
committerBrian Behlendorf <behlendorf1@llnl.gov>
Thu, 3 Dec 2015 23:44:47 +0000 (15:44 -0800)
If a vnode is released asynchronously through areleasef(), it is
possible for the user process to reuse the file descriptor before
areleasef is called. When this happens, getf() will return a stale
reference, any operations in the kernel on that file descriptor will
fail (as it is closed) and the operations meant for that fd will
never occur from userspace's perspective.

We correct this by detecting this condition in getf(), doing a putf
on the old file handle, updating the file descriptor and proceeding
as if everything was fine. When the areleasef() is done, it will
harmlessly decrement the reference counter on the Illumos file handle.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #492

include/sys/user.h
module/spl/spl-vnode.c

index ebbe8f68eb834fda2814a8835fd4393cf4070a59..2b25dd33c01aa5bef72749be108a0225bd8061e1 100644 (file)
@@ -30,8 +30,8 @@
  * about the Linux task_struct. Since this is internal to our compatibility
  * layer, we make it an opaque type.
  *
- * XXX: If the descriptor changes under us, we would get an incorrect
- * reference.
+ * XXX: If the descriptor changes under us and we do not do a getf() between
+ * the change and using it, we would get an incorrect reference.
  */
 
 struct uf_info;
index ab9830d1867a04ea6577d534110d6f41a229b85c..86349c54aa512289f9c379fd25d510f337be1c6e 100644 (file)
@@ -656,6 +656,19 @@ vn_getf(int fd)
 
        fp = file_find(fd, current);
        if (fp) {
+               lfp = fget(fd);
+               fput(fp->f_file);
+               /*
+                * areleasef() can cause us to see a stale reference when
+                * userspace has reused a file descriptor before areleasef()
+                * has run. fput() the stale reference and replace it. We
+                * retain the original reference count such that the concurrent
+                * areleasef() will decrement its reference and terminate.
+                */
+               if (lfp != fp->f_file) {
+                       fp->f_file = lfp;
+                       fp->f_vnode->v_file = lfp;
+               }
                atomic_inc(&fp->f_ref);
                spin_unlock(&vn_file_lock);
                return (fp);