Skip to content

Commit 4ea899e

Browse files
Christoph Hellwigdjwong
authored andcommitted
iomap: fix a use after free in iomap_dio_rw
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 <chandan@linux.ibm.com> Reported-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Christoph Hellwig <hch@lst.de> Tested-by: Chandan Rajendra <chandan@linux.ibm.com> Tested-by: Darrick J. Wong <darrick.wong@oracle.com> Reviewed-by: Dave Chinner <dchinner@redhat.com> Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
1 parent 8e47a45 commit 4ea899e

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

fs/iomap.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
18131813
loff_t pos = iocb->ki_pos, start = pos;
18141814
loff_t end = iocb->ki_pos + count - 1, ret = 0;
18151815
unsigned int flags = IOMAP_DIRECT;
1816+
bool wait_for_completion = is_sync_kiocb(iocb);
18161817
struct blk_plug plug;
18171818
struct iomap_dio *dio;
18181819

@@ -1832,7 +1833,6 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
18321833
dio->end_io = end_io;
18331834
dio->error = 0;
18341835
dio->flags = 0;
1835-
dio->wait_for_completion = is_sync_kiocb(iocb);
18361836

18371837
dio->submit.iter = iter;
18381838
dio->submit.waiter = current;
@@ -1887,7 +1887,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
18871887
dio_warn_stale_pagecache(iocb->ki_filp);
18881888
ret = 0;
18891889

1890-
if (iov_iter_rw(iter) == WRITE && !dio->wait_for_completion &&
1890+
if (iov_iter_rw(iter) == WRITE && !wait_for_completion &&
18911891
!inode->i_sb->s_dio_done_wq) {
18921892
ret = sb_init_dio_done_wq(inode->i_sb);
18931893
if (ret < 0)
@@ -1903,7 +1903,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
19031903
if (ret <= 0) {
19041904
/* magic error code to fall back to buffered I/O */
19051905
if (ret == -ENOTBLK) {
1906-
dio->wait_for_completion = true;
1906+
wait_for_completion = true;
19071907
ret = 0;
19081908
}
19091909
break;
@@ -1925,8 +1925,24 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
19251925
if (dio->flags & IOMAP_DIO_WRITE_FUA)
19261926
dio->flags &= ~IOMAP_DIO_NEED_SYNC;
19271927

1928+
/*
1929+
* We are about to drop our additional submission reference, which
1930+
* might be the last reference to the dio. There are three three
1931+
* different ways we can progress here:
1932+
*
1933+
* (a) If this is the last reference we will always complete and free
1934+
* the dio ourselves.
1935+
* (b) If this is not the last reference, and we serve an asynchronous
1936+
* iocb, we must never touch the dio after the decrement, the
1937+
* I/O completion handler will complete and free it.
1938+
* (c) If this is not the last reference, but we serve a synchronous
1939+
* iocb, the I/O completion handler will wake us up on the drop
1940+
* of the final reference, and we will complete and free it here
1941+
* after we got woken by the I/O completion handler.
1942+
*/
1943+
dio->wait_for_completion = wait_for_completion;
19281944
if (!atomic_dec_and_test(&dio->ref)) {
1929-
if (!dio->wait_for_completion)
1945+
if (!wait_for_completion)
19301946
return -EIOCBQUEUED;
19311947

19321948
for (;;) {
@@ -1943,9 +1959,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
19431959
__set_current_state(TASK_RUNNING);
19441960
}
19451961

1946-
ret = iomap_dio_complete(dio);
1947-
1948-
return ret;
1962+
return iomap_dio_complete(dio);
19491963

19501964
out_free_dio:
19511965
kfree(dio);

0 commit comments

Comments
 (0)