rbd: avoid double free memory on error path in rbd_dev_create()
Subsystems (2)
PATCH 1/1 rbd: avoid double free memory on error path in rbd_dev_create()
If rbd_dev_create() fails after assignment 'opts' to 'rbd_dev->opts', double free of 'rbd_options' happens:
one is in rbd_dev_free() and another one is in do_rbd_add().
Found by Linux Verification Center (linuxtesting.org) with SVACE.
--- drivers/block/rbd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/rbd.c +1 -1
@@ -5357,7 +5357,6 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,if (!rbd_dev)return NULL ; - rbd_dev->opts = opts;/* get an id and fill in device name */ rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
@@ -5372,6 +5371,7 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,if (!rbd_dev->task_wq) goto fail_dev_id; + rbd_dev->opts = opts;/* we have a ref from do_rbd_add() */ __module_get (THIS_MODULE );
Signature · 1 line
2.34.1
Ilya Dryomov
maintainerQuoted 35 lines · Natalia Petrova
If
rbd_dev_create()fails after assignment 'opts' to 'rbd_dev->opts', double free of 'rbd_options' happens:
one is inrbd_dev_free()and another one is indo_rbd_add().Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 1643dfa4c2c8 ("rbd: introduce a per-device ordered workqueue")Signed-off-by Natalia Petrova <[email protected]>Signed-off-by Alexey Khoroshilov <[email protected]>--- drivers/block/rbd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/block/rbd.c +1
-2@@ -5357,7 +5357,6 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,if (!rbd_dev)return NULL ; - rbd_dev->opts = opts;/* get an id and fill in device name */ rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,@@ -5372,6 +5371,7 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,if (!rbd_dev->task_wq) goto fail_dev_id; + rbd_dev->opts = opts;/* we have a ref from do_rbd_add() */ __module_get (THIS_MODULE ); --
Quoted 2 lines
2.34.1
Hi Natalia,
It seems like a similar issue is affecting rbd_dev->rbd_client and rbd_dev->spec. Unlike rbd_dev->opts, they are ref-counted and I'm guessing that the verification tool doesn't go that deep.
I'd prefer all three to be addressed in the same change, since it's the same error path. Would you be willing to look into that and post a new revision or should I treat just this patch as a bug report?
Thanks,
Ilya
Hi Ilya!
Thanks for your response! I don't quite understand your idea and suggestion. The patch is designed to avoid double free memory. I explored the code again and suppose there is another situation for rbd_dev->rbd_client and rbd_dev->spec. Free memory of these pointers is possible only once in rbd_dev_free() function. In do_rbd_add() deallocation memory is only for rbd_opts: drivers/block/rbd.c 7157. Correct me if I'm wrong.
Thanks,
Natalia
-----Original Message-----
From: Ilya Dryomov <[email protected]>
Sent: Monday, February 6, 2023 2:59 PM
To: Петрова Наталия Михайловна <[email protected]>
Cc: Dongsheng Yang <[email protected]>; Jens Axboe <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Alexey Khoroshilov <[email protected]>
Subject: Re: [PATCH] rbd: avoid double free memory on error path in rbd_dev_create()
Quoted 35 lines · Natalia Petrova
If
rbd_dev_create()fails after assignment 'opts' to 'rbd_dev->opts', double free of 'rbd_options' happens:
one is inrbd_dev_free()and another one is indo_rbd_add().Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 1643dfa4c2c8 ("rbd: introduce a per-device ordered workqueue")Signed-off-by Natalia Petrova <[email protected]>Signed-off-by Alexey Khoroshilov <[email protected]>--- drivers/block/rbd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/block/rbd.c index renamed from drivers/block/rbd.c +0
-0
04453f4a319c..ab6bfc352cde100644drivers/block/rbd.c +1
-3@@ -5357,7 +5357,6 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,if (!rbd_dev)return NULL ; - rbd_dev->opts = opts;/* get an id and fill in device name */ rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, @@ -5372,6 +5371,7 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, if (!rbd_dev->task_wq) goto fail_dev_id; + rbd_dev->opts = opts;/* we have a ref from do_rbd_add() */ __module_get (THIS_MODULE ); --
Quoted 2 lines
2.34.1
Hi Natalia,
It seems like a similar issue is affecting rbd_dev->rbd_client and rbd_dev->spec. Unlike rbd_dev->opts, they are ref-counted and I'm guessing that the verification tool doesn't go that deep.
I'd prefer all three to be addressed in the same change, since it's the same error path. Would you be willing to look into that and post a new revision or should I treat just this patch as a bug report?
Thanks,
Ilya
Quoted 2 lines · Петрова Наталия Михайловна
Hi Ilya!
Thanks for your response! I don't quite understand your idea and suggestion. The patch is designed to avoid double free memory. I explored the code again and suppose there is another situation forrbd_dev->rbd_clientandrbd_dev->spec. Free memory of these pointers is possible only once inrbd_dev_free()function. Indo_rbd_add()deallocation memory is only forrbd_opts:drivers/block/rbd.c7157.
Hi Петрова,
If the rbd_dev_create() fails, for spec it will be freed in rbd_dev_create()->rbd_spec_put() first and then in do_rbd_add() it will call rbd_spec_put() again.
It won't trigger double free but this should generate a warning when the refcount underflow, because the refcount_dec_and_test() will warn and then return false when underflow happens.
The same for rbd_client.
Thanks,
- Xiubo
Quoted 58 lines
Correct me if I'm wrong.
Thanks,
Natalia-----Original Message-----
From: Ilya Dryomov <[email protected]>
Sent: Monday, February 6, 2023 2:59 PM
To: Петрова Наталия Михайловна <[email protected]>
Cc: Dongsheng Yang <[email protected]>; Jens Axboe <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Alexey Khoroshilov <[email protected]>
Subject: Re: [PATCH] rbd: avoid double free memory on error path inrbd_dev_create()Quoted 34 lines · Natalia Petrova
If
rbd_dev_create()fails after assignment 'opts' to 'rbd_dev->opts', double free of 'rbd_options' happens:
one is inrbd_dev_free()and another one is indo_rbd_add().Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 1643dfa4c2c8 ("rbd: introduce a per-device ordered workqueue")Signed-off-by Natalia Petrova <[email protected]>Signed-off-by Alexey Khoroshilov <[email protected]>--- drivers/block/rbd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/block/rbd.c index renamed from drivers/block/rbd.c +0
-0
04453f4a319c..ab6bfc352cde100644drivers/block/rbd.c +1
-3@@ -5357,7 +5357,6 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,if (!rbd_dev)return NULL ; - rbd_dev->opts = opts;/* get an id and fill in device name */ rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, @@ -5372,6 +5371,7 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, if (!rbd_dev->task_wq) goto fail_dev_id; + rbd_dev->opts = opts;/* we have a ref from do_rbd_add() */ __module_get (THIS_MODULE ); --Quoted 2 lines
2.34.1
Hi Natalia,
It seems like a similar issue is affecting
rbd_dev->rbd_clientandrbd_dev->spec. Unlikerbd_dev->opts, they are ref-counted and I'm guessing that the verification tool doesn't go that deep.I'd prefer all three to be addressed in the same change, since it's the same error path. Would you be willing to look into that and post a new revision or should I treat just this patch as a bug report?
Thanks,
Ilya
Signature · 6 lines
Best Regards, Xiubo Li (李秀波) Email: [email protected]/[email protected] Slack: @Xiubo Li
PATCH v2 1/1 rbd: fix freeing memory of 'rbd_dev->opts', 'rbd_dev->spec', 'rbd_dev->rbd_client'
If the rbd_dev_create() fails after assignment 'opts' to 'rbd_dev->opts', double free of 'rbd_options' happens:
one is in rbd_dev_free() and another one is in do_rbd_add().
If the rbd_dev_create() fails, for 'spec' it will be freed in rbd_dev_create()->rbd_spec_put() first and then in do_rbd_add() it will call rbd_spec_put() again. The same for 'rbd_client'. Unlike 'rbd_dev->opts', 'rbd_dev->spec' and 'rbd_dev->rbd_client' are ref-counted, that's why the ref-count underflow warning should be generated in rbd_spec_put() and rbd_put_client() to handle the return values of kref_put().
Found by Linux Verification Center (linuxtesting.org) with SVACE.
---
v2: Remarks on the processing of 'rbd_dev->spec' and 'rbd_dev->rbd_client' by Ilya Dryomov <[email protected]> and Xiubo Li <[email protected]> were taken into account.
drivers/block/rbd.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/block/rbd.c +9 -5
@@ -889,8 +889,10 @@ static void rbd_client_release(struct kref *kref)*/ static void rbd_put_client (struct rbd_client *rbdc) { -if (rbdc) -kref_put (&rbdc->kref, rbd_client_release); +if (rbdc) { +if (!kref_put (&rbdc->kref, rbd_client_release)) +pr_warn ("The reference count underflow\n" ); + } }/*
@@ -5225,8 +5227,10 @@ static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)static void rbd_spec_free (struct kref *kref);static void rbd_spec_put (struct rbd_spec *spec) { -if (spec) -kref_put (&spec->kref, rbd_spec_free); +if (spec) { +if (!kref_put (&spec->kref, rbd_spec_free)) +pr_warn ("The reference count underflow\n" ); + } }static struct rbd_spec *rbd_spec_alloc (void )
@@ -5357,7 +5361,6 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,if (!rbd_dev)return NULL ; - rbd_dev->opts = opts;/* get an id and fill in device name */ rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
@@ -5372,6 +5375,7 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,if (!rbd_dev->task_wq) goto fail_dev_id; + rbd_dev->opts = opts;/* we have a ref from do_rbd_add() */ __module_get (THIS_MODULE );
Signature · 1 line
2.34.1
Ilya Dryomov
maintainerRe: [PATCH v2] rbd: fix freeing memory of 'rbd_dev->opts', 'rbd_dev->spec', 'rbd_dev->rbd_client'
Quoted 5 lines · Natalia Petrova
If the
rbd_dev_create()fails after assignment 'opts' to 'rbd_dev->opts', double free of 'rbd_options' happens:
one is inrbd_dev_free()and another one is indo_rbd_add().
Quoted 7 lines
If the
rbd_dev_create()fails, for 'spec' it will be freed in rbd_dev_create()->rbd_spec_put() first and then indo_rbd_add()it will callrbd_spec_put()again. The same for 'rbd_client'. Unlike 'rbd_dev->opts', 'rbd_dev->spec' and 'rbd_dev->rbd_client' are ref-counted, that's why the ref-count underflow warning should be generated inrbd_spec_put()andrbd_put_client()to handle the return values ofkref_put().
Hi Natalia,
I think you misinterpreted Xiubo. The underflow warning would be printed by kref_put() (if one is lucky and the freed memory doesn't get immediately reallocated and overwritten in which case straight memory corruption would occur). There is no need to attempt to print another warning here.
The problem is potential use-after-free on struct rbd_spec and struct rbd_client (which is what the warning is for). This use-after-free is very similar in nature to what the tool that you are using found for struct rbd_options (the same bug on the same error path) except that reference counting is involved: instead of kfree() being called directly, it's called indirectly from rbd_spec_free() and rbd_client_release() through rbd_spec_put() and rbd_put_client() respectively. Both of these structs have a refcount of 1 here which means that the first rbd_spec_free() or rbd_client_release() call is equivalent to kfree() and, when either of them is called again from do_rbd_add(), use-after-free would occur.
Hope this helps,
Ilya
Quoted 63 lines
Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 1643dfa4c2c8 ("rbd: introduce a per-device ordered workqueue")Signed-off-by Natalia Petrova <[email protected]>Signed-off-by Alexey Khoroshilov <[email protected]>Signed-off-by Nikita Zhandarovich <[email protected]>---
v2: Remarks on the processing of 'rbd_dev->spec' and 'rbd_dev->rbd_client' by Ilya Dryomov <[email protected]> and Xiubo Li <[email protected]> were taken into account.drivers/block/rbd.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)drivers/block/rbd.c +9
-6@@ -889,8 +889,10 @@ static void rbd_client_release(struct kref *kref)*/ static void rbd_put_client (struct rbd_client *rbdc) { -if (rbdc) -kref_put (&rbdc->kref, rbd_client_release); +if (rbdc) { +if (!kref_put (&rbdc->kref, rbd_client_release)) +pr_warn ("The reference count underflow\n" ); + } }/* @@ -5225,8 +5227,10 @@ static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)static void rbd_spec_free (struct kref *kref);static void rbd_spec_put (struct rbd_spec *spec) { -if (spec) -kref_put (&spec->kref, rbd_spec_free); +if (spec) { +if (!kref_put (&spec->kref, rbd_spec_free)) +pr_warn ("The reference count underflow\n" ); + } }static struct rbd_spec *rbd_spec_alloc (void )@@ -5357,7 +5361,6 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,if (!rbd_dev)return NULL ; - rbd_dev->opts = opts;/* get an id and fill in device name */ rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,@@ -5372,6 +5375,7 @@ static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,if (!rbd_dev->task_wq) goto fail_dev_id; + rbd_dev->opts = opts;/* we have a ref from do_rbd_add() */ __module_get (THIS_MODULE ); --2.34.1