summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérôme Glisse <jglisse@redhat.com>2019-04-10 15:37:47 -0400
committerSteve French <stfrench@microsoft.com>2019-04-24 13:33:59 -0400
commit13f5938d8264b5501368523c4513ff26608a33e8 (patch)
treec69e1c167cebb240986c2f079d3f989e7d23e033
parent652727bbe1b17993636346716ae5867627793647 (diff)
cifs: fix page reference leak with readv/writev
CIFS can leak pages reference gotten through GUP (get_user_pages*() through iov_iter_get_pages()). This happen if cifs_send_async_read() or cifs_write_from_iter() calls fail from within __cifs_readv() and __cifs_writev() respectively. This patch move page unreference to cifs_aio_ctx_release() which will happens on all code paths this is all simpler to follow for correctness. Signed-off-by: Jérôme Glisse <jglisse@redhat.com> Cc: Steve French <sfrench@samba.org> Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: linux-fsdevel@vger.kernel.org Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Stable <stable@vger.kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com> Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
-rw-r--r--fs/cifs/file.c15
-rw-r--r--fs/cifs/misc.c23
2 files changed, 23 insertions, 15 deletions
diff --git a/fs/cifs/file.c b/fs/cifs/file.c
index 9c0ccc06d172..7037a137fa53 100644
--- a/fs/cifs/file.c
+++ b/fs/cifs/file.c
@@ -2877,7 +2877,6 @@ static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
2877 struct cifs_tcon *tcon; 2877 struct cifs_tcon *tcon;
2878 struct cifs_sb_info *cifs_sb; 2878 struct cifs_sb_info *cifs_sb;
2879 struct dentry *dentry = ctx->cfile->dentry; 2879 struct dentry *dentry = ctx->cfile->dentry;
2880 unsigned int i;
2881 int rc; 2880 int rc;
2882 2881
2883 tcon = tlink_tcon(ctx->cfile->tlink); 2882 tcon = tlink_tcon(ctx->cfile->tlink);
@@ -2941,10 +2940,6 @@ restart_loop:
2941 kref_put(&wdata->refcount, cifs_uncached_writedata_release); 2940 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2942 } 2941 }
2943 2942
2944 if (!ctx->direct_io)
2945 for (i = 0; i < ctx->npages; i++)
2946 put_page(ctx->bv[i].bv_page);
2947
2948 cifs_stats_bytes_written(tcon, ctx->total_len); 2943 cifs_stats_bytes_written(tcon, ctx->total_len);
2949 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags); 2944 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
2950 2945
@@ -3582,7 +3577,6 @@ collect_uncached_read_data(struct cifs_aio_ctx *ctx)
3582 struct iov_iter *to = &ctx->iter; 3577 struct iov_iter *to = &ctx->iter;
3583 struct cifs_sb_info *cifs_sb; 3578 struct cifs_sb_info *cifs_sb;
3584 struct cifs_tcon *tcon; 3579 struct cifs_tcon *tcon;
3585 unsigned int i;
3586 int rc; 3580 int rc;
3587 3581
3588 tcon = tlink_tcon(ctx->cfile->tlink); 3582 tcon = tlink_tcon(ctx->cfile->tlink);
@@ -3666,15 +3660,8 @@ again:
3666 kref_put(&rdata->refcount, cifs_uncached_readdata_release); 3660 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3667 } 3661 }
3668 3662
3669 if (!ctx->direct_io) { 3663 if (!ctx->direct_io)
3670 for (i = 0; i < ctx->npages; i++) {
3671 if (ctx->should_dirty)
3672 set_page_dirty(ctx->bv[i].bv_page);
3673 put_page(ctx->bv[i].bv_page);
3674 }
3675
3676 ctx->total_len = ctx->len - iov_iter_count(to); 3664 ctx->total_len = ctx->len - iov_iter_count(to);
3677 }
3678 3665
3679 /* mask nodata case */ 3666 /* mask nodata case */
3680 if (rc == -ENODATA) 3667 if (rc == -ENODATA)
diff --git a/fs/cifs/misc.c b/fs/cifs/misc.c
index 1e1626a2cfc3..0dc6f08020ac 100644
--- a/fs/cifs/misc.c
+++ b/fs/cifs/misc.c
@@ -789,6 +789,11 @@ cifs_aio_ctx_alloc(void)
789{ 789{
790 struct cifs_aio_ctx *ctx; 790 struct cifs_aio_ctx *ctx;
791 791
792 /*
793 * Must use kzalloc to initialize ctx->bv to NULL and ctx->direct_io
794 * to false so that we know when we have to unreference pages within
795 * cifs_aio_ctx_release()
796 */
792 ctx = kzalloc(sizeof(struct cifs_aio_ctx), GFP_KERNEL); 797 ctx = kzalloc(sizeof(struct cifs_aio_ctx), GFP_KERNEL);
793 if (!ctx) 798 if (!ctx)
794 return NULL; 799 return NULL;
@@ -807,7 +812,23 @@ cifs_aio_ctx_release(struct kref *refcount)
807 struct cifs_aio_ctx, refcount); 812 struct cifs_aio_ctx, refcount);
808 813
809 cifsFileInfo_put(ctx->cfile); 814 cifsFileInfo_put(ctx->cfile);
810 kvfree(ctx->bv); 815
816 /*
817 * ctx->bv is only set if setup_aio_ctx_iter() was call successfuly
818 * which means that iov_iter_get_pages() was a success and thus that
819 * we have taken reference on pages.
820 */
821 if (ctx->bv) {
822 unsigned i;
823
824 for (i = 0; i < ctx->npages; i++) {
825 if (ctx->should_dirty)
826 set_page_dirty(ctx->bv[i].bv_page);
827 put_page(ctx->bv[i].bv_page);
828 }
829 kvfree(ctx->bv);
830 }
831
811 kfree(ctx); 832 kfree(ctx);
812} 833}
813 834