block: Fix multiwrite error handling
cover block: Fix multiwrite error handling
The bdrv_aio_multiwrite error handling has some bugs that lead to premature cleanup, causing use-after-free and double free problems.
Kevin Wolf (2):
block: Fix too early free in multiwrite
block: Handle multiwrite errors only when all requests have completed
block.c | 11 +++-------- 1 files changed, 3 insertions(+), 8 deletions(-)
PATCH 2/2 block: Handle multiwrite errors only when all requests have completed
Don't try to be clever by freeing all temporary data and calling all callbacks when the return value (an error) is certain. Doing so has at least two important problems:
* The temporary data that is freed (qiov, possibly zero buffer) is still used by the requests that have not yet completed.
* Calling the callbacks for all requests in the multiwrite means for the caller that it may free buffers etc. which are still in use.
Just remember the error value and do the cleanup when all requests have completed.
--- block.c | 5 +---- 1 files changed, 1 insertions(+), 4 deletions(-)
block.c +1 -4
@@ -2056,14 +2056,11 @@ static void multiwrite_cb(void *opaque, int ret)if (ret <0 && !mcb->error) { mcb->error = ret; -multiwrite_user_cb (mcb); } mcb->num_requests--;if (mcb->num_requests ==0 ) { -if (mcb->error ==0 ) { -multiwrite_user_cb (mcb); - } +multiwrite_user_cb (mcb);qemu_free (mcb); } }
Signature · 1 line
1.6.6.1
PATCH 1/2 block: Fix too early free in multiwrite
In reply to Kevin Wolfbdrv_aio_writev may call the callback immediately (and it will commonly do so in error cases). If num_requests doesn't have its final value yet, multiwrite_cb will falsely detect that all requests are completed and frees the mcb. However, the mcb is still used by other requests that are started only afterwards. When all requests are completed, it is freed for the second time.
Fix this by setting the right num_requests from the beginning.
--- block.c | 6 ++---- 1 files changed, 2 insertions(+), 4 deletions(-)
block.c +2 -4
@@ -2198,6 +2198,7 @@ int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) num_reqs =multiwrite_merge (bs, reqs, num_reqs, mcb);// Run the aio requests + mcb->num_requests = num_reqs;for (i =0 ; i < num_reqs; i++) { acb =bdrv_aio_writev (bs, reqs[i].sector, reqs[i].qiov, reqs[i].nb_sectors, multiwrite_cb, mcb);
@@ -2206,16 +2207,13 @@ int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)// We can only fail the whole thing if no request has been // submitted yet. Otherwise we'll wait for the submitted AIOs to // complete and report the error in the callback. -if (mcb->num_requests ==0 ) { +if (i ==0 ) { reqs[i].error = -EIO ; goto fail; }else { - mcb->num_requests++;multiwrite_cb (mcb, -EIO );break ; } - }else { - mcb->num_requests++; } }
Signature · 1 line
1.6.6.1
Re: [Qemu-devel] [PATCH 1/2] block: Fix too early free in multiwrite
Quoted 7 lines · Kevin Wolf
bdrv_aio_writevmay call the callback immediately (and it will commonly do so in error cases). Ifnum_requestsdoesn't have its final value yet,multiwrite_cbwill falsely detect that all requests are completed and frees the mcb. However, the mcb is still used by other requests that are started only afterwards. When all requests are completed, it is freed for the second time.Fix this by setting the right
num_requestsfrom the beginning.
Looks good to me.
Quoted 37 lines
Signed-off-by Kevin Wolf <[email protected]>---
block.c | 6 ++----1 files changed, 2 insertions(+), 4 deletions(-)block.c +2
-5@@ -2198,6 +2198,7 @@ int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); // Run the aio requests + mcb->num_requests = num_reqs; for (i = 0; i < num_reqs; i++) { acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, reqs[i].nb_sectors, multiwrite_cb, mcb);@@ -2206,16 +2207,13 @@ int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) // We can only fail the whole thing if no request has been // submitted yet. Otherwise we'll wait for the submitted AIOs to // complete and report the error in the callback. - if (mcb->num_requests == 0) { + if (i == 0) { reqs[i].error = -EIO; goto fail; } else { - mcb->num_requests++; multiwrite_cb(mcb, -EIO); break; } - } else { - mcb->num_requests++; } } --
Quoted 4 lines
1.6.6.1
Stefan
Re: [Qemu-devel] [PATCH 2/2] block: Handle multiwrite errors only when all requests have completed
In reply to Kevin WolfQuoted 24 lines · Kevin Wolf
Don't try to be clever by freeing all temporary data and calling all callbacks when the return value (an error) is certain. Doing so has at least two important problems:
* The temporary data that is freed (qiov, possibly zero buffer) is still used by the requests that have not yet completed.
* Calling the callbacks for all requests in the multiwrite means for the caller that it may free buffers etc. which are still in use.Just remember the error value and do the cleanup when all requests have completed.
Signed-off-by Kevin Wolf <[email protected]>---
block.c | 5 +----1 files changed, 1 insertions(+), 4 deletions(-)block.c +0
-0@@ -2056,14 +2056,11 @@ static void multiwrite_cb(void *opaque, int ret) if (ret < 0 && !mcb->error) {
block.c static void multiwrite_cb(void *opaque, int ret) · line 2063
Quoted 10 lines
block.c +1
-4mcb->error = ret; - multiwrite_user_cb(mcb); } mcb->num_requests--; if (mcb->num_requests == 0) { - if (mcb->error == 0) { - multiwrite_user_cb(mcb); - } + multiwrite_user_cb(mcb);
I am a little confused at this point. Here is the meat of bdrv_aio_multiwrite() from the kevin/devel branch:
// Run the aio requestsmcb->num_requests = num_reqs;
for (i = 0; i < num_reqs; i++) {
acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
reqs[i].nb_sectors, multiwrite_cb, mcb);
if (acb == NULL) {
// We can only fail the whole thing if no request has been
// submitted yet. Otherwise we'll wait for the submitted AIOs to
// complete and report the error in the callback.
if (i == 0) {
reqs[i].error = -EIO;
goto fail;
} else {
multiwrite_cb(mcb, -EIO);
break;
}
}
}If bdrv_aio_write() fails on request 2 out of 3, then mcb->num_requests = 3 but only 2 requests were issued before breaking the loop.
Now the 2 issued requests complete and call multiwrite_cb(). Since was mcb->num_requests = 3, it never reaches zero and neither multiwrite_user_cb(mcb) nor qemu_free(mcb) are ever called.
Is it safe to adjust mcb->num_requests = i before calling multiwrite_cb() and breaking the loop in the failure case? That way the num->num_requests will reach zero.
I hesitate a little because previous requests could have completed, multiwrite_cb() was called, and mcb->num_requests was decremented? Then the value of i cannot be used for mcb->num_requests because previous requests it counts have already completed.
Stefan
Re: [Qemu-devel] [PATCH 1/2] block: Fix too early free in multiwrite
In reply to Kevin WolfQuoted 7 lines · Kevin Wolf
bdrv_aio_writevmay call the callback immediately (and it will commonly do so in error cases). Ifnum_requestsdoesn't have its final value yet,multiwrite_cbwill falsely detect that all requests are completed and frees the mcb. However, the mcb is still used by other requests that are started only afterwards. When all requests are completed, it is freed for the second time.Fix this by setting the right
num_requestsfrom the beginning.
Looks good,
Re: [Qemu-devel] [PATCH 2/2] block: Handle multiwrite errors only when all requests have completed
In reply to Kevin WolfQuoted 11 lines · Kevin Wolf
Don't try to be clever by freeing all temporary data and calling all callbacks when the return value (an error) is certain. Doing so has at least two important problems:
* The temporary data that is freed (qiov, possibly zero buffer) is still used by the requests that have not yet completed.
* Calling the callbacks for all requests in the multiwrite means for the caller that it may free buffers etc. which are still in use.Just remember the error value and do the cleanup when all requests have completed.
Looks good.