* [Qemu-devel] [PATCH v2] block: fix leaks in bdrv_open_driver()
@ 2017-07-01 15:39 Manos Pitsidianakis
2017-07-11 15:16 ` Kevin Wolf
0 siblings, 1 reply; 5+ messages in thread
From: Manos Pitsidianakis @ 2017-07-01 15:39 UTC (permalink / raw)
To: qemu-devel
Cc: qemu-block, Kevin Wolf, Stefan Hajnoczi, Alberto Garcia,
Max Reitz
bdrv_open_driver() is called in two places, bdrv_new_open_driver() and
bdrv_open_common(). In the latter, failure cleanup in is in its caller,
bdrv_open_inherit(), which unrefs the bs->file of the failed driver open if it
exists.
Let's move the bs->file cleanup to bdrv_open_driver() to take care of all
callers and do not set bs->drv to NULL unless the driver's open function
failed. When bs is destroyed by removing its last reference, bdrv_close()
checks bs->drv to perform the needed cleanups and also call the driver's close
function.
Signed-off-by: Manos Pitsidianakis <el13635@mail.ntua.gr>
---
v2:
move bdrv_unref_child(bs, bs->file) to bdrv_open_driver
do not set bs->drv to NULL if open succeeds
block.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/block.c b/block.c
index 694396281b..df2a46990c 100644
--- a/block.c
+++ b/block.c
@@ -1091,6 +1091,7 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
{
Error *local_err = NULL;
int ret;
+ bool open_failed;
bdrv_assign_node_name(bs, node_name, &local_err);
if (local_err) {
@@ -1111,7 +1112,9 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
ret = 0;
}
- if (ret < 0) {
+ open_failed = ret < 0;
+
+ if (open_failed) {
if (local_err) {
error_propagate(errp, local_err);
} else if (bs->filename[0]) {
@@ -1142,10 +1145,15 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
return 0;
free_and_fail:
- /* FIXME Close bs first if already opened*/
- g_free(bs->opaque);
- bs->opaque = NULL;
- bs->drv = NULL;
+ if (open_failed) {
+ g_free(bs->opaque);
+ bs->opaque = NULL;
+ bs->drv = NULL;
+ }
+ if (bs->file != NULL) {
+ bdrv_unref_child(bs, bs->file);
+ bs->file = NULL;
+ }
return ret;
}
@@ -2607,9 +2615,6 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,
fail:
blk_unref(file);
- if (bs->file != NULL) {
- bdrv_unref_child(bs, bs->file);
- }
QDECREF(snapshot_options);
QDECREF(bs->explicit_options);
QDECREF(bs->options);
--
2.11.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH v2] block: fix leaks in bdrv_open_driver()
2017-07-01 15:39 [Qemu-devel] [PATCH v2] block: fix leaks in bdrv_open_driver() Manos Pitsidianakis
@ 2017-07-11 15:16 ` Kevin Wolf
2017-07-11 18:50 ` Manos Pitsidianakis
0 siblings, 1 reply; 5+ messages in thread
From: Kevin Wolf @ 2017-07-11 15:16 UTC (permalink / raw)
To: Manos Pitsidianakis
Cc: qemu-devel, qemu-block, Stefan Hajnoczi, Alberto Garcia,
Max Reitz
Am 01.07.2017 um 17:39 hat Manos Pitsidianakis geschrieben:
> bdrv_open_driver() is called in two places, bdrv_new_open_driver() and
> bdrv_open_common(). In the latter, failure cleanup in is in its caller,
> bdrv_open_inherit(), which unrefs the bs->file of the failed driver open if it
> exists.
>
> Let's move the bs->file cleanup to bdrv_open_driver() to take care of all
> callers and do not set bs->drv to NULL unless the driver's open function
> failed. When bs is destroyed by removing its last reference, bdrv_close()
> checks bs->drv to perform the needed cleanups and also call the driver's close
> function.
>
> Signed-off-by: Manos Pitsidianakis <el13635@mail.ntua.gr>
> ---
>
> v2:
> move bdrv_unref_child(bs, bs->file) to bdrv_open_driver
> do not set bs->drv to NULL if open succeeds
>
> block.c | 21 +++++++++++++--------
> 1 file changed, 13 insertions(+), 8 deletions(-)
>
> diff --git a/block.c b/block.c
> index 694396281b..df2a46990c 100644
> --- a/block.c
> +++ b/block.c
> @@ -1091,6 +1091,7 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
> {
> Error *local_err = NULL;
> int ret;
> + bool open_failed;
>
> bdrv_assign_node_name(bs, node_name, &local_err);
> if (local_err) {
> @@ -1111,7 +1112,9 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
> ret = 0;
> }
>
> - if (ret < 0) {
> + open_failed = ret < 0;
> +
> + if (open_failed) {
> if (local_err) {
> error_propagate(errp, local_err);
> } else if (bs->filename[0]) {
> @@ -1142,10 +1145,15 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
> return 0;
>
> free_and_fail:
> - /* FIXME Close bs first if already opened*/
> - g_free(bs->opaque);
> - bs->opaque = NULL;
> - bs->drv = NULL;
> + if (open_failed) {
> + g_free(bs->opaque);
> + bs->opaque = NULL;
> + bs->drv = NULL;
> + }
> + if (bs->file != NULL) {
> + bdrv_unref_child(bs, bs->file);
> + bs->file = NULL;
> + }
Is this bdrv_unref_child() safe if we leave bs->drv set? Format drivers
expect that if an image is opened, it also has a valid bs->file.
For example, if I add ret = -1 after refresh_total_sectors() (because I
couldn't find an easier way to make it fail intentionally), I get an
ugly heap corruption crash instead of a nice error message with this
patch.
Kevin
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH v2] block: fix leaks in bdrv_open_driver()
2017-07-11 15:16 ` Kevin Wolf
@ 2017-07-11 18:50 ` Manos Pitsidianakis
2017-07-12 8:33 ` Kevin Wolf
0 siblings, 1 reply; 5+ messages in thread
From: Manos Pitsidianakis @ 2017-07-11 18:50 UTC (permalink / raw)
To: Kevin Wolf
Cc: qemu-devel, qemu-block, Stefan Hajnoczi, Alberto Garcia,
Max Reitz
[-- Attachment #1: Type: text/plain, Size: 3151 bytes --]
On Tue, Jul 11, 2017 at 05:16:17PM +0200, Kevin Wolf wrote:
>Am 01.07.2017 um 17:39 hat Manos Pitsidianakis geschrieben:
>> bdrv_open_driver() is called in two places, bdrv_new_open_driver() and
>> bdrv_open_common(). In the latter, failure cleanup in is in its caller,
>> bdrv_open_inherit(), which unrefs the bs->file of the failed driver open if it
>> exists.
>>
>> Let's move the bs->file cleanup to bdrv_open_driver() to take care of all
>> callers and do not set bs->drv to NULL unless the driver's open function
>> failed. When bs is destroyed by removing its last reference, bdrv_close()
>> checks bs->drv to perform the needed cleanups and also call the driver's close
>> function.
>>
>> Signed-off-by: Manos Pitsidianakis <el13635@mail.ntua.gr>
>> ---
>>
>> v2:
>> move bdrv_unref_child(bs, bs->file) to bdrv_open_driver
>> do not set bs->drv to NULL if open succeeds
>>
>> block.c | 21 +++++++++++++--------
>> 1 file changed, 13 insertions(+), 8 deletions(-)
>>
>> diff --git a/block.c b/block.c
>> index 694396281b..df2a46990c 100644
>> --- a/block.c
>> +++ b/block.c
>> @@ -1091,6 +1091,7 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
>> {
>> Error *local_err = NULL;
>> int ret;
>> + bool open_failed;
>>
>> bdrv_assign_node_name(bs, node_name, &local_err);
>> if (local_err) {
>> @@ -1111,7 +1112,9 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
>> ret = 0;
>> }
>>
>> - if (ret < 0) {
>> + open_failed = ret < 0;
>> +
>> + if (open_failed) {
>> if (local_err) {
>> error_propagate(errp, local_err);
>> } else if (bs->filename[0]) {
>> @@ -1142,10 +1145,15 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
>> return 0;
>>
>> free_and_fail:
>> - /* FIXME Close bs first if already opened*/
>> - g_free(bs->opaque);
>> - bs->opaque = NULL;
>> - bs->drv = NULL;
>> + if (open_failed) {
>> + g_free(bs->opaque);
>> + bs->opaque = NULL;
>> + bs->drv = NULL;
>> + }
>> + if (bs->file != NULL) {
>> + bdrv_unref_child(bs, bs->file);
>> + bs->file = NULL;
>> + }
>
>Is this bdrv_unref_child() safe if we leave bs->drv set? Format drivers
>expect that if an image is opened, it also has a valid bs->file.
>
>For example, if I add ret = -1 after refresh_total_sectors() (because I
>couldn't find an easier way to make it fail intentionally), I get an
>ugly heap corruption crash instead of a nice error message with this
>patch.
>
This is triggered by bdrv_open_inherit doing
QDECREF(bs->explicit_options) and leaving the dangling pointer. Not
setting bs->drv means bdrv_close was called and tried to decref it
again, causing the heap error. Setting bs->explicit_options = NULL;
right below that fixes the heap corruption for me.
I can send a seperate fix for this. I also saw that there's no reason to
use a boolean, a label would do just fine so I can change that and
finalize the patch in the next version if everything is okay with it.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH v2] block: fix leaks in bdrv_open_driver()
2017-07-11 18:50 ` Manos Pitsidianakis
@ 2017-07-12 8:33 ` Kevin Wolf
2017-07-12 8:39 ` Manos Pitsidianakis
0 siblings, 1 reply; 5+ messages in thread
From: Kevin Wolf @ 2017-07-12 8:33 UTC (permalink / raw)
To: Manos Pitsidianakis, qemu-devel, qemu-block, Stefan Hajnoczi,
Alberto Garcia, Max Reitz
[-- Attachment #1: Type: text/plain, Size: 3527 bytes --]
Am 11.07.2017 um 20:50 hat Manos Pitsidianakis geschrieben:
> On Tue, Jul 11, 2017 at 05:16:17PM +0200, Kevin Wolf wrote:
> >Am 01.07.2017 um 17:39 hat Manos Pitsidianakis geschrieben:
> >>bdrv_open_driver() is called in two places, bdrv_new_open_driver() and
> >>bdrv_open_common(). In the latter, failure cleanup in is in its caller,
> >>bdrv_open_inherit(), which unrefs the bs->file of the failed driver open if it
> >>exists.
> >>
> >>Let's move the bs->file cleanup to bdrv_open_driver() to take care of all
> >>callers and do not set bs->drv to NULL unless the driver's open function
> >>failed. When bs is destroyed by removing its last reference, bdrv_close()
> >>checks bs->drv to perform the needed cleanups and also call the driver's close
> >>function.
> >>
> >>Signed-off-by: Manos Pitsidianakis <el13635@mail.ntua.gr>
> >>---
> >>
> >>v2:
> >> move bdrv_unref_child(bs, bs->file) to bdrv_open_driver
> >> do not set bs->drv to NULL if open succeeds
> >>
> >> block.c | 21 +++++++++++++--------
> >> 1 file changed, 13 insertions(+), 8 deletions(-)
> >>
> >>diff --git a/block.c b/block.c
> >>index 694396281b..df2a46990c 100644
> >>--- a/block.c
> >>+++ b/block.c
> >>@@ -1091,6 +1091,7 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
> >> {
> >> Error *local_err = NULL;
> >> int ret;
> >>+ bool open_failed;
> >>
> >> bdrv_assign_node_name(bs, node_name, &local_err);
> >> if (local_err) {
> >>@@ -1111,7 +1112,9 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
> >> ret = 0;
> >> }
> >>
> >>- if (ret < 0) {
> >>+ open_failed = ret < 0;
> >>+
> >>+ if (open_failed) {
> >> if (local_err) {
> >> error_propagate(errp, local_err);
> >> } else if (bs->filename[0]) {
> >>@@ -1142,10 +1145,15 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
> >> return 0;
> >>
> >> free_and_fail:
> >>- /* FIXME Close bs first if already opened*/
> >>- g_free(bs->opaque);
> >>- bs->opaque = NULL;
> >>- bs->drv = NULL;
> >>+ if (open_failed) {
> >>+ g_free(bs->opaque);
> >>+ bs->opaque = NULL;
> >>+ bs->drv = NULL;
> >>+ }
> >>+ if (bs->file != NULL) {
> >>+ bdrv_unref_child(bs, bs->file);
> >>+ bs->file = NULL;
> >>+ }
> >
> >Is this bdrv_unref_child() safe if we leave bs->drv set? Format drivers
> >expect that if an image is opened, it also has a valid bs->file.
> >
> >For example, if I add ret = -1 after refresh_total_sectors() (because I
> >couldn't find an easier way to make it fail intentionally), I get an
> >ugly heap corruption crash instead of a nice error message with this
> >patch.
> >
> This is triggered by bdrv_open_inherit doing
> QDECREF(bs->explicit_options) and leaving the dangling pointer. Not
> setting bs->drv means bdrv_close was called and tried to decref it
> again, causing the heap error. Setting bs->explicit_options = NULL;
> right below that fixes the heap corruption for me.
Wouldn't it be better to call drv->bdrv_close() instead and then set
bs->drv/opaque = NULL like for the other error path?
> I can send a seperate fix for this.
No, this doesn't fail before this patch, so it's a regression and we
can't merge the patch without a fix. You need to respin this one.
> I also saw that there's no reason to use a boolean, a label would do
> just fine so I can change that and finalize the patch in the next
> version if everything is okay with it.
Yes, that sounds better.
Kevin
[-- Attachment #2: Type: application/pgp-signature, Size: 836 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH v2] block: fix leaks in bdrv_open_driver()
2017-07-12 8:33 ` Kevin Wolf
@ 2017-07-12 8:39 ` Manos Pitsidianakis
0 siblings, 0 replies; 5+ messages in thread
From: Manos Pitsidianakis @ 2017-07-12 8:39 UTC (permalink / raw)
To: Kevin Wolf
Cc: qemu-devel, qemu-block, Stefan Hajnoczi, Alberto Garcia,
Max Reitz
[-- Attachment #1: Type: text/plain, Size: 4020 bytes --]
On Wed, Jul 12, 2017 at 10:33:37AM +0200, Kevin Wolf wrote:
>Am 11.07.2017 um 20:50 hat Manos Pitsidianakis geschrieben:
>> On Tue, Jul 11, 2017 at 05:16:17PM +0200, Kevin Wolf wrote:
>> >Am 01.07.2017 um 17:39 hat Manos Pitsidianakis geschrieben:
>> >>bdrv_open_driver() is called in two places, bdrv_new_open_driver() and
>> >>bdrv_open_common(). In the latter, failure cleanup in is in its caller,
>> >>bdrv_open_inherit(), which unrefs the bs->file of the failed driver open if it
>> >>exists.
>> >>
>> >>Let's move the bs->file cleanup to bdrv_open_driver() to take care of all
>> >>callers and do not set bs->drv to NULL unless the driver's open function
>> >>failed. When bs is destroyed by removing its last reference, bdrv_close()
>> >>checks bs->drv to perform the needed cleanups and also call the driver's close
>> >>function.
>> >>
>> >>Signed-off-by: Manos Pitsidianakis <el13635@mail.ntua.gr>
>> >>---
>> >>
>> >>v2:
>> >> move bdrv_unref_child(bs, bs->file) to bdrv_open_driver
>> >> do not set bs->drv to NULL if open succeeds
>> >>
>> >> block.c | 21 +++++++++++++--------
>> >> 1 file changed, 13 insertions(+), 8 deletions(-)
>> >>
>> >>diff --git a/block.c b/block.c
>> >>index 694396281b..df2a46990c 100644
>> >>--- a/block.c
>> >>+++ b/block.c
>> >>@@ -1091,6 +1091,7 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
>> >> {
>> >> Error *local_err = NULL;
>> >> int ret;
>> >>+ bool open_failed;
>> >>
>> >> bdrv_assign_node_name(bs, node_name, &local_err);
>> >> if (local_err) {
>> >>@@ -1111,7 +1112,9 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
>> >> ret = 0;
>> >> }
>> >>
>> >>- if (ret < 0) {
>> >>+ open_failed = ret < 0;
>> >>+
>> >>+ if (open_failed) {
>> >> if (local_err) {
>> >> error_propagate(errp, local_err);
>> >> } else if (bs->filename[0]) {
>> >>@@ -1142,10 +1145,15 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
>> >> return 0;
>> >>
>> >> free_and_fail:
>> >>- /* FIXME Close bs first if already opened*/
>> >>- g_free(bs->opaque);
>> >>- bs->opaque = NULL;
>> >>- bs->drv = NULL;
>> >>+ if (open_failed) {
>> >>+ g_free(bs->opaque);
>> >>+ bs->opaque = NULL;
>> >>+ bs->drv = NULL;
>> >>+ }
>> >>+ if (bs->file != NULL) {
>> >>+ bdrv_unref_child(bs, bs->file);
>> >>+ bs->file = NULL;
>> >>+ }
>> >
>> >Is this bdrv_unref_child() safe if we leave bs->drv set? Format drivers
>> >expect that if an image is opened, it also has a valid bs->file.
>> >
>> >For example, if I add ret = -1 after refresh_total_sectors() (because I
>> >couldn't find an easier way to make it fail intentionally), I get an
>> >ugly heap corruption crash instead of a nice error message with this
>> >patch.
>> >
>> This is triggered by bdrv_open_inherit doing
>> QDECREF(bs->explicit_options) and leaving the dangling pointer. Not
>> setting bs->drv means bdrv_close was called and tried to decref it
>> again, causing the heap error. Setting bs->explicit_options = NULL;
>> right below that fixes the heap corruption for me.
>
>Wouldn't it be better to call drv->bdrv_close() instead and then set
>bs->drv/opaque = NULL like for the other error path?
That was my first approach but I thought it wouldn't look nice since
bdrv_close is called anyway on delete. I will do it in the next version.
>
>> I can send a seperate fix for this.
>
>No, this doesn't fail before this patch, so it's a regression and we
>can't merge the patch without a fix. You need to respin this one.
Yes, I meant to first fix this and then apply this patch. It's a
dangling pointer anyway.
>
>> I also saw that there's no reason to use a boolean, a label would do
>> just fine so I can change that and finalize the patch in the next
>> version if everything is okay with it.
>
>Yes, that sounds better.
>
>Kevin
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2017-07-12 8:40 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-07-01 15:39 [Qemu-devel] [PATCH v2] block: fix leaks in bdrv_open_driver() Manos Pitsidianakis
2017-07-11 15:16 ` Kevin Wolf
2017-07-11 18:50 ` Manos Pitsidianakis
2017-07-12 8:33 ` Kevin Wolf
2017-07-12 8:39 ` Manos Pitsidianakis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).