Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
iomap: fix a use after free in iomap_dio_rw
Browse files Browse the repository at this point in the history
[ Upstream commit 4ea899e ]

Introduce a local wait_for_completion variable to avoid an access to the
potentially freed dio struture after dropping the last reference count.

Also use the chance to document the completion behavior to make the
refcounting clear to the reader of the code.

Fixes: ff6a929 ("iomap: implement direct I/O")
Reported-by: Chandan Rajendra <[email protected]>
Reported-by: Darrick J. Wong <[email protected]>
Signed-off-by: Christoph Hellwig <[email protected]>
Tested-by: Chandan Rajendra <[email protected]>
Tested-by: Darrick J. Wong <[email protected]>
Reviewed-by: Dave Chinner <[email protected]>
Reviewed-by: Darrick J. Wong <[email protected]>
Signed-off-by: Darrick J. Wong <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
Christoph Hellwig authored and gregkh committed Mar 13, 2019
1 parent d23792f commit d9ba842
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions fs/iomap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
loff_t pos = iocb->ki_pos, start = pos;
loff_t end = iocb->ki_pos + count - 1, ret = 0;
unsigned int flags = IOMAP_DIRECT;
bool wait_for_completion = is_sync_kiocb(iocb);
struct blk_plug plug;
struct iomap_dio *dio;

Expand All @@ -1806,7 +1807,6 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
dio->end_io = end_io;
dio->error = 0;
dio->flags = 0;
dio->wait_for_completion = is_sync_kiocb(iocb);

dio->submit.iter = iter;
dio->submit.waiter = current;
Expand Down Expand Up @@ -1861,7 +1861,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
dio_warn_stale_pagecache(iocb->ki_filp);
ret = 0;

if (iov_iter_rw(iter) == WRITE && !dio->wait_for_completion &&
if (iov_iter_rw(iter) == WRITE && !wait_for_completion &&
!inode->i_sb->s_dio_done_wq) {
ret = sb_init_dio_done_wq(inode->i_sb);
if (ret < 0)
Expand All @@ -1877,7 +1877,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
if (ret <= 0) {
/* magic error code to fall back to buffered I/O */
if (ret == -ENOTBLK) {
dio->wait_for_completion = true;
wait_for_completion = true;
ret = 0;
}
break;
Expand All @@ -1899,8 +1899,24 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
if (dio->flags & IOMAP_DIO_WRITE_FUA)
dio->flags &= ~IOMAP_DIO_NEED_SYNC;

/*
* We are about to drop our additional submission reference, which
* might be the last reference to the dio. There are three three
* different ways we can progress here:
*
* (a) If this is the last reference we will always complete and free
* the dio ourselves.
* (b) If this is not the last reference, and we serve an asynchronous
* iocb, we must never touch the dio after the decrement, the
* I/O completion handler will complete and free it.
* (c) If this is not the last reference, but we serve a synchronous
* iocb, the I/O completion handler will wake us up on the drop
* of the final reference, and we will complete and free it here
* after we got woken by the I/O completion handler.
*/
dio->wait_for_completion = wait_for_completion;
if (!atomic_dec_and_test(&dio->ref)) {
if (!dio->wait_for_completion)
if (!wait_for_completion)
return -EIOCBQUEUED;

for (;;) {
Expand All @@ -1917,9 +1933,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
__set_current_state(TASK_RUNNING);
}

ret = iomap_dio_complete(dio);

return ret;
return iomap_dio_complete(dio);

out_free_dio:
kfree(dio);
Expand Down

0 comments on commit d9ba842

Please sign in to comment.