Skip to content

Commit d296626

Browse files
morbidrsakdave
authored andcommitted
btrfs: fix use-after-free waiting for encoded read endios
Fix a use-after-free in the I/O completion path for encoded reads by using a completion instead of a wait_queue for synchronizing the destruction of 'struct btrfs_encoded_read_private'. Fixes: 1881fba ("btrfs: add BTRFS_IOC_ENCODED_READ ioctl") CC: [email protected] # 6.1+ Reviewed-by: Filipe Manana <[email protected]> Reviewed-by: Qu Wenruo <[email protected]> Signed-off-by: Johannes Thumshirn <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent f10bef7 commit d296626

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

fs/btrfs/inode.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9078,9 +9078,9 @@ static ssize_t btrfs_encoded_read_inline(
90789078
}
90799079

90809080
struct btrfs_encoded_read_private {
9081-
wait_queue_head_t wait;
9081+
struct completion done;
90829082
void *uring_ctx;
9083-
atomic_t pending;
9083+
refcount_t pending_refs;
90849084
blk_status_t status;
90859085
};
90869086

@@ -9099,14 +9099,14 @@ static void btrfs_encoded_read_endio(struct btrfs_bio *bbio)
90999099
*/
91009100
WRITE_ONCE(priv->status, bbio->bio.bi_status);
91019101
}
9102-
if (atomic_dec_and_test(&priv->pending)) {
9102+
if (refcount_dec_and_test(&priv->pending_refs)) {
91039103
int err = blk_status_to_errno(READ_ONCE(priv->status));
91049104

91059105
if (priv->uring_ctx) {
91069106
btrfs_uring_read_extent_endio(priv->uring_ctx, err);
91079107
kfree(priv);
91089108
} else {
9109-
wake_up(&priv->wait);
9109+
complete(&priv->done);
91109110
}
91119111
}
91129112
bio_put(&bbio->bio);
@@ -9126,8 +9126,8 @@ int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
91269126
if (!priv)
91279127
return -ENOMEM;
91289128

9129-
init_waitqueue_head(&priv->wait);
9130-
atomic_set(&priv->pending, 1);
9129+
init_completion(&priv->done);
9130+
refcount_set(&priv->pending_refs, 1);
91319131
priv->status = 0;
91329132
priv->uring_ctx = uring_ctx;
91339133

@@ -9140,7 +9140,7 @@ int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
91409140
size_t bytes = min_t(u64, disk_io_size, PAGE_SIZE);
91419141

91429142
if (bio_add_page(&bbio->bio, pages[i], bytes, 0) < bytes) {
9143-
atomic_inc(&priv->pending);
9143+
refcount_inc(&priv->pending_refs);
91449144
btrfs_submit_bbio(bbio, 0);
91459145

91469146
bbio = btrfs_bio_alloc(BIO_MAX_VECS, REQ_OP_READ, fs_info,
@@ -9155,11 +9155,11 @@ int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
91559155
disk_io_size -= bytes;
91569156
} while (disk_io_size);
91579157

9158-
atomic_inc(&priv->pending);
9158+
refcount_inc(&priv->pending_refs);
91599159
btrfs_submit_bbio(bbio, 0);
91609160

91619161
if (uring_ctx) {
9162-
if (atomic_dec_return(&priv->pending) == 0) {
9162+
if (refcount_dec_and_test(&priv->pending_refs)) {
91639163
ret = blk_status_to_errno(READ_ONCE(priv->status));
91649164
btrfs_uring_read_extent_endio(uring_ctx, ret);
91659165
kfree(priv);
@@ -9168,8 +9168,8 @@ int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
91689168

91699169
return -EIOCBQUEUED;
91709170
} else {
9171-
if (atomic_dec_return(&priv->pending) != 0)
9172-
io_wait_event(priv->wait, !atomic_read(&priv->pending));
9171+
if (!refcount_dec_and_test(&priv->pending_refs))
9172+
wait_for_completion_io(&priv->done);
91739173
/* See btrfs_encoded_read_endio() for ordering. */
91749174
ret = blk_status_to_errno(READ_ONCE(priv->status));
91759175
kfree(priv);

0 commit comments

Comments
 (0)