block: Do not unref bs->file on error in BD's open
PATCH 1/1 block: Do not unref bs->file on error in BD's open
The block layer takes care of removing the bs->file child if the block driver's bdrv_open()/bdrv_file_open() implementation fails. The block driver therefore does not need to do so, and indeed should not unless it sets bs->file to NULL afterwards -- because if this is not done, the bdrv_unref_child() in bdrv_open_inherit() will dereference the freed memory block at bs->file afterwards, which is not good.
We can now decide whether to add a "bs->file = NULL;" after each of the offending bdrv_unref_child() invocations, or just drop them altogether. The latter is simpler, so let's do that.
---
It's an issue only in blkdebug, blkreplace and blkverify, and only when an error occurs in their open functions; therefore I think this is fine to delay until 2.10.
However, it *is* a use-after-free newly introduced in 2.9, so that's where the question mark comes from...
--- block/blkdebug.c | 4 +--- block/blkreplay.c | 3 --- block/blkverify.c | 3 --- 3 files changed, 1 insertion(+), 9 deletions(-)
block/blkdebug.c +1 -3
@@ -389,14 +389,12 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, }else if (align) {error_setg (errp,"Invalid alignment" ); ret = -EINVAL ; - goto fail_unref; + goto out; } ret =0 ; goto out; -fail_unref: -bdrv_unref_child (bs, bs->file); out:if (ret <0 ) {g_free (s->config_file);
block/blkreplay.c +0 -3
@@ -37,9 +37,6 @@ static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags, ret =0 ; fail: -if (ret <0 ) { -bdrv_unref_child (bs, bs->file); - }return ret; }
block/blkverify.c +0 -3
@@ -142,9 +142,6 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags, ret =0 ; fail: -if (ret <0 ) { -bdrv_unref_child (bs, bs->file); - }qemu_opts_del (opts);return ret; }
Signature · 1 line
2.12.2
Quoted 18 lines · Max Reitz
The block layer takes care of removing the
bs->filechild if the block driver's bdrv_open()/bdrv_file_open() implementation fails. The block driver therefore does not need to do so, and indeed should not unless it setsbs->fileto NULL afterwards -- because if this is not done, thebdrv_unref_child()inbdrv_open_inherit()will dereference the freed memory block atbs->fileafterwards, which is not good.We can now decide whether to add a "
bs->file= NULL;" after each of the offendingbdrv_unref_child()invocations, or just drop them altogether. The latter is simpler, so let's do that.Cc qemu-stable <[email protected]>Signed-off-by Max Reitz <[email protected]>---
It's an issue only in blkdebug, blkreplace and blkverify, and only when an error occurs in their open functions; therefore I think this is fine to delay until 2.10.
Quoted 7 lines
However, it *is* a use-after-free newly introduced in 2.9, so that's where the question mark comes from...
--- block/blkdebug.c | 4 +--- block/blkreplay.c | 3 --- block/blkverify.c | 3 --- 3 files changed, 1 insertion(+), 9 deletions(-)
On its own, this patch is not worthy of an -rc5 (it only affects failure paths, and 2 of the 3 affected drivers are not for production use). But use-after-free is annoying, and the fact that blkreplay is affected could trip up a user that mistypes arguments causing a BD open to fail. I'd argue that since we're looking at -rc5 anyways, this is fair game for inclusion in that build, since it is a small enough patch and easy to review for correctness. Hence I changed the subject in my reply to '2.9?'
But I won't lose any sleep if we just wait for 2.9.1 and 2.10.
block/blkdebug.c static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, · line 394
Quoted 13 lines
block/blkdebug.c +1
-1@@ -389,14 +389,12 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, }else if (align) {error_setg (errp,"Invalid alignment" ); ret = -EINVAL ; - goto fail_unref; + goto out; }
And this means I get to rebase my blkdebug patches on top of yours, if yours goes in first.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 18 lines · Max Reitz
The block layer takes care of removing the
bs->filechild if the block driver's bdrv_open()/bdrv_file_open() implementation fails. The block driver therefore does not need to do so, and indeed should not unless it setsbs->fileto NULL afterwards -- because if this is not done, thebdrv_unref_child()inbdrv_open_inherit()will dereference the freed memory block atbs->fileafterwards, which is not good.We can now decide whether to add a "
bs->file= NULL;" after each of the offendingbdrv_unref_child()invocations, or just drop them altogether. The latter is simpler, so let's do that.Cc qemu-stable <[email protected]>Signed-off-by Max Reitz <[email protected]>---
It's an issue only in blkdebug, blkreplace and blkverify, and only when an error occurs in their open functions; therefore I think this is fine to delay until 2.10.
Quoted 7 lines
However, it *is* a use-after-free newly introduced in 2.9, so that's where the question mark comes from...
--- block/blkdebug.c | 4 +--- block/blkreplay.c | 3 --- block/blkverify.c | 3 --- 3 files changed, 1 insertion(+), 9 deletions(-)
Kevin Wolf
AppliedAm 13.04.2017 um 17:43 hat Max Reitz geschrieben:
Quoted 7 lines
The block layer takes care of removing the
bs->filechild if the block driver's bdrv_open()/bdrv_file_open() implementation fails. The block driver therefore does not need to do so, and indeed should not unless it setsbs->fileto NULL afterwards -- because if this is not done, thebdrv_unref_child()inbdrv_open_inherit()will dereference the freed memory block atbs->fileafterwards, which is not good.
Quoted 6 lines
We can now decide whether to add a "
bs->file= NULL;" after each of the offendingbdrv_unref_child()invocations, or just drop them altogether. The latter is simpler, so let's do that.Cc qemu-stable <[email protected]>Signed-off-by Max Reitz <[email protected]>
Thanks, applied to block-next.
Kevin