Skip to content

Commit 92d7733

Browse files
shligitaxboe
authored andcommitted
block/loop: fix use after free
lo_rw_aio->call_read_iter-> 1 aops->direct_IO 2 iov_iter_revert lo_rw_aio_complete could happen between 1 and 2, the bio and bvec could be freed before 2, which accesses bvec. Signed-off-by: Shaohua Li <shli@fb.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 12cd3a2 commit 92d7733

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎drivers/block/loop.c‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,21 @@ static void lo_complete_rq(struct request *rq)
463463
blk_mq_end_request(rq, cmd->ret < 0 ? BLK_STS_IOERR : BLK_STS_OK);
464464
}
465465

466+
static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
467+
{
468+
if (!atomic_dec_and_test(&cmd->ref))
469+
return;
470+
kfree(cmd->bvec);
471+
cmd->bvec = NULL;
472+
blk_mq_complete_request(cmd->rq);
473+
}
474+
466475
static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2)
467476
{
468477
struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
469478

470-
kfree(cmd->bvec);
471-
cmd->bvec = NULL;
472479
cmd->ret = ret;
473-
blk_mq_complete_request(cmd->rq);
480+
lo_rw_aio_do_completion(cmd);
474481
}
475482

476483
static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
@@ -518,6 +525,7 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
518525
bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
519526
segments = bio_segments(bio);
520527
}
528+
atomic_set(&cmd->ref, 2);
521529

522530
iov_iter_bvec(&iter, ITER_BVEC | rw, bvec,
523531
segments, blk_rq_bytes(rq));
@@ -533,6 +541,8 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
533541
else
534542
ret = call_read_iter(file, &cmd->iocb, &iter);
535543

544+
lo_rw_aio_do_completion(cmd);
545+
536546
if (ret != -EIOCBQUEUED)
537547
cmd->iocb.ki_complete(&cmd->iocb, ret, 0);
538548
return 0;

‎drivers/block/loop.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ struct loop_cmd {
6868
struct kthread_work work;
6969
struct request *rq;
7070
struct list_head list;
71-
bool use_aio; /* use AIO interface to handle I/O */
71+
union {
72+
bool use_aio; /* use AIO interface to handle I/O */
73+
atomic_t ref; /* only for aio */
74+
};
7275
long ret;
7376
struct kiocb iocb;
7477
struct bio_vec *bvec;

0 commit comments

Comments
 (0)