dm: Fix UAF in run_timer_softirq()
From: Luo Meng <[email protected]>
When dm_resume() and dm_destroy() are concurrent, it will lead to UAF.
One of the concurrency UAF can be shown as below:
use free
do_resume |
__find_device_hash_cell |
dm_get |
atomic_inc(&md->holders) |
| dm_destroy
| __dm_destroy
| if (!dm_suspended_md(md))
| atomic_read(&md->holders)
| msleep(1)
dm_resume |
__dm_resume |
dm_table_resume_targets | pool_resume |
do_waker #add delay work |
| dm_table_destroy
| pool_dtr
| __pool_dec
| __pool_destroy
| destroy_workqueue
| kfree(pool) # free pool
time out__do_softirq
run_timer_softirq # pool has already been freed
This can be easily reproduced using:
1. create thin-pool
2. dmsetup suspend pool
3. dmsetup resume pool
4. dmsetup remove_all # Concurrent with 3
The root cause of UAF bugs is that dm_resume() adds timer after dm_destroy() skips cancel timer beause of suspend status. After timeout, it will call run_timer_softirq(), however pool has already been freed. The concurrency UAF bug will happen.
Therefore, canceling timer is moved after md->holders is zero.
--- drivers/md/dm.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)
drivers/md/dm.c +13 -13
@@ -2420,6 +2420,19 @@ static void __dm_destroy(struct mapped_device *md, bool wait)blk_mark_disk_dead (md->disk); +/* +* Rare, but there may be I/O requests still going to complete, +* for example. Wait for all references to disappear. +* No one should increment the reference count of the mapped_device, +* after the mapped_device state becomes DMF_FREEING. +*/ +if (wait) +while (atomic_read (&md->holders)) +msleep (1 ); +else if (atomic_read (&md->holders)) +DMWARN ("%s: Forcibly removing mapped_device still in use! (%d users)" , +dm_device_name (md),atomic_read (&md->holders)); +/* * Take suspend_lock so that presuspend and postsuspend methods * do not race with internal suspend.
@@ -2436,19 +2449,6 @@ static void __dm_destroy(struct mapped_device *md, bool wait)dm_put_live_table (md, srcu_idx);mutex_unlock (&md->suspend_lock); -/* -* Rare, but there may be I/O requests still going to complete, -* for example. Wait for all references to disappear. -* No one should increment the reference count of the mapped_device, -* after the mapped_device state becomes DMF_FREEING. -*/ -if (wait) -while (atomic_read (&md->holders)) -msleep (1 ); -else if (atomic_read (&md->holders)) -DMWARN ("%s: Forcibly removing mapped_device still in use! (%d users)" , -dm_device_name (md),atomic_read (&md->holders)); -dm_table_destroy (__unbind (md));free_dev (md); }
Signature · 6 lines
2.31.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
Quoted 97 lines · Luo Meng
From: Luo Meng <[email protected]>
When
dm_resume()anddm_destroy()are concurrent, it will lead to UAF.One of the concurrency UAF can be shown as below:
use free
do_resume | __find_device_hash_cell | dm_get | atomic_inc(&md->holders) | | dm_destroy | __dm_destroy | if (!dm_suspended_md(md)) | atomic_read(&md->holders) | msleep(1) dm_resume | __dm_resume | dm_table_resume_targets |
pool_resume|
do_waker#add delay work |
|dm_table_destroy
|pool_dtr
|__pool_dec
|__pool_destroy
|destroy_workqueue
| kfree(pool) # free pool
time out__do_softirq
run_timer_softirq# pool has already been freedThis can be easily reproduced using:
1. create thin-pool
2. dmsetup suspend pool
3. dmsetup resume pool
4. dmsetupremove_all# Concurrent with 3The root cause of UAF bugs is that
dm_resume()adds timer afterdm_destroy()skips cancel timer beause of suspend status. After timeout, it will callrun_timer_softirq(), however pool has already been freed. The concurrency UAF bug will happen.Therefore, canceling timer is moved after
md->holdersis zero.Signed-off-by Luo Meng <[email protected]>--- drivers/md/dm.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)drivers/md/dm.c +13
-13@@ -2420,6 +2420,19 @@ static void __dm_destroy(struct mapped_device *md, bool wait)blk_mark_disk_dead (md->disk); +/* +* Rare, but there may be I/O requests still going to complete, +* for example. Wait for all references to disappear. +* No one should increment the reference count of the mapped_device, +* after the mapped_device state becomes DMF_FREEING. +*/ +if (wait) +while (atomic_read (&md->holders)) +msleep (1 ); +else if (atomic_read (&md->holders)) +DMWARN ("%s: Forcibly removing mapped_device still in use! (%d users)" , +dm_device_name (md),atomic_read (&md->holders)); +/* * Take suspend_lock so that presuspend and postsuspend methods * do not race with internal suspend. @@ -2436,19 +2449,6 @@ static void __dm_destroy(struct mapped_device *md, bool wait)dm_put_live_table (md, srcu_idx);mutex_unlock (&md->suspend_lock); -/* -* Rare, but there may be I/O requests still going to complete, -* for example. Wait for all references to disappear. -* No one should increment the reference count of the mapped_device, -* after the mapped_device state becomes DMF_FREEING. -*/ -if (wait) -while (atomic_read (&md->holders)) -msleep (1 ); -else if (atomic_read (&md->holders)) -DMWARN ("%s: Forcibly removing mapped_device still in use! (%d users)" , -dm_device_name (md),atomic_read (&md->holders)); -dm_table_destroy (__unbind (md));free_dev (md); }Signature · 1 line
2.31.1
Thanks for the report but your fix seems wrong. A thin-pool specific fix seems much more appropriate. Does this fix the issue?
drivers/md/dm-thin.c +2 -0
@@ -2889,6 +2889,8 @@ static void __pool_destroy(struct pool *pool)dm_bio_prison_destroy (pool->prison);dm_kcopyd_client_destroy (pool->copier); +cancel_delayed_work_sync (&pool->waker); +cancel_delayed_work_sync (&pool->no_space_timeout);if (pool->wq)destroy_workqueue (pool->wq);
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Quoted 110 lines · Mike Snitzer
Quoted 97 lines · Luo Meng
From: Luo Meng <[email protected]>
When
dm_resume()anddm_destroy()are concurrent, it will lead to UAF.One of the concurrency UAF can be shown as below:
use free
do_resume|__find_device_hash_cell | dm_get | atomic_inc(&md->holders) | | dm_destroy | __dm_destroy | if (!dm_suspended_md(md)) | atomic_read(&md->holders) | msleep(1) dm_resume | __dm_resume | dm_table_resume_targets |
pool_resume|
do_waker#add delay work |
|dm_table_destroy
|pool_dtr
|__pool_dec
|__pool_destroy
|destroy_workqueue
| kfree(pool) # free pool
time out__do_softirq
run_timer_softirq# pool has already been freedThis can be easily reproduced using:
1. create thin-pool
2. dmsetup suspend pool
3. dmsetup resume pool
4. dmsetupremove_all# Concurrent with 3The root cause of UAF bugs is that
dm_resume()adds timer afterdm_destroy()skips cancel timer beause of suspend status. After timeout, it will callrun_timer_softirq(), however pool has already been freed. The concurrency UAF bug will happen.Therefore, canceling timer is moved after
md->holdersis zero.Signed-off-by Luo Meng <[email protected]>--- drivers/md/dm.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)drivers/md/dm.c +13
-13@@ -2420,6 +2420,19 @@ static void __dm_destroy(struct mapped_device *md, bool wait)blk_mark_disk_dead (md->disk); +/* +* Rare, but there may be I/O requests still going to complete, +* for example. Wait for all references to disappear. +* No one should increment the reference count of the mapped_device, +* after the mapped_device state becomes DMF_FREEING. +*/ +if (wait) +while (atomic_read (&md->holders)) +msleep (1 ); +else if (atomic_read (&md->holders)) +DMWARN ("%s: Forcibly removing mapped_device still in use! (%d users)" , +dm_device_name (md),atomic_read (&md->holders)); +/* * Take suspend_lock so that presuspend and postsuspend methods * do not race with internal suspend. @@ -2436,19 +2449,6 @@ static void __dm_destroy(struct mapped_device *md, bool wait)dm_put_live_table (md, srcu_idx);mutex_unlock (&md->suspend_lock); -/* -* Rare, but there may be I/O requests still going to complete, -* for example. Wait for all references to disappear. -* No one should increment the reference count of the mapped_device, -* after the mapped_device state becomes DMF_FREEING. -*/ -if (wait) -while (atomic_read (&md->holders)) -msleep (1 ); -else if (atomic_read (&md->holders)) -DMWARN ("%s: Forcibly removing mapped_device still in use! (%d users)" , -dm_device_name (md),atomic_read (&md->holders)); -dm_table_destroy (__unbind (md));free_dev (md); }Signature · 1 line
2.31.1Thanks for the report but your fix seems wrong. A thin-pool specific fix seems much more appropriate. Does this fix the issue?
drivers/md/dm-thin.c +0
-0
drivers/md/dm-thin.c static void __pool_destroy(struct pool *pool) · line 2896
Quoted 8 lines
drivers/md/dm-thin.c +2
-0@@ -2889,6 +2889,8 @@ static void __pool_destroy(struct pool *pool)dm_bio_prison_destroy (pool->prison);dm_kcopyd_client_destroy (pool->copier); +cancel_delayed_work_sync (&pool->waker); +cancel_delayed_work_sync (&pool->no_space_timeout);if (pool->wq)destroy_workqueue (pool->wq);
Thanks for your reply.
However this issue exits not only in thin-pool, cache_target also has thisissus. Generic fix maybe more appropriate.
After adding cancel_delayed_work_sync() in __pool_destroy(), this will make it possible to call cancel_delayed_work_sync(&pool->waker) twice when dm is not suspend.
Luo Meng
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Quoted 118 lines · Luo Meng
Quoted 106 lines · Mike Snitzer
Quoted 94 lines · Luo Meng
From: Luo Meng <[email protected]>
When
dm_resume()anddm_destroy()are concurrent, it will lead to UAF.One of the concurrency UAF can be shown as below:
use free
do_resume|__find_device_hash_cell | dm_get | atomic_inc(&md->holders) | | dm_destroy | __dm_destroy | if (!dm_suspended_md(md)) | atomic_read(&md->holders) | msleep(1) dm_resume | __dm_resume | dm_table_resume_targets |
pool_resume|
do_waker#add delay work |
|dm_table_destroy
|pool_dtr
|__pool_dec
|__pool_destroy
|destroy_workqueue
| kfree(pool) # free pool
time out__do_softirq
run_timer_softirq# pool has already been freedThis can be easily reproduced using:
1. create thin-pool
2. dmsetup suspend pool
3. dmsetup resume pool
4. dmsetupremove_all# Concurrent with 3The root cause of UAF bugs is that
dm_resume()adds timer afterdm_destroy()skips cancel timer beause of suspend status. After timeout, it will callrun_timer_softirq(), however pool has already been freed. The concurrency UAF bug will happen.Therefore, canceling timer is moved after
md->holdersis zero.Signed-off-by Luo Meng <[email protected]>--- drivers/md/dm.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)drivers/md/dm.c +13
-13@@ -2420,6 +2420,19 @@ static void __dm_destroy(struct mapped_device *md, bool wait)blk_mark_disk_dead (md->disk); +/* +* Rare, but there may be I/O requests still going to complete, +* for example. Wait for all references to disappear. +* No one should increment the reference count of the mapped_device, +* after the mapped_device state becomes DMF_FREEING. +*/ +if (wait) +while (atomic_read (&md->holders)) +msleep (1 ); +else if (atomic_read (&md->holders)) +DMWARN ("%s: Forcibly removing mapped_device still in use! (%d users)" , +dm_device_name (md),atomic_read (&md->holders)); +/* * Take suspend_lock so that presuspend and postsuspend methods * do not race with internal suspend. @@ -2436,19 +2449,6 @@ static void __dm_destroy(struct mapped_device *md, bool wait)dm_put_live_table (md, srcu_idx);mutex_unlock (&md->suspend_lock); -/* -* Rare, but there may be I/O requests still going to complete, -* for example. Wait for all references to disappear. -* No one should increment the reference count of the mapped_device, -* after the mapped_device state becomes DMF_FREEING. -*/ -if (wait) -while (atomic_read (&md->holders)) -msleep (1 ); -else if (atomic_read (&md->holders)) -DMWARN ("%s: Forcibly removing mapped_device still in use! (%d users)" , -dm_device_name (md),atomic_read (&md->holders)); -dm_table_destroy (__unbind (md));free_dev (md); }Signature · 1 line
2.31.1Thanks for the report but your fix seems wrong. A thin-pool specific fix seems much more appropriate. Does this fix the issue?
drivers/md/dm-thin.c +0
-0drivers/md/dm-thin.c static void __pool_destroy(struct pool *pool) · line 2895
Quoted 7 lines
drivers/md/dm-thin.c +2
-0@@ -2889,6 +2889,8 @@ static void __pool_destroy(struct pool *pool)dm_bio_prison_destroy (pool->prison);dm_kcopyd_client_destroy (pool->copier); +cancel_delayed_work_sync (&pool->waker); +cancel_delayed_work_sync (&pool->no_space_timeout);if (pool->wq)destroy_workqueue (pool->wq);Thanks for your reply.
Quoted 2 lines
However this issue exits not only in thin-pool,
cache_targetalso has thisissus. Generic fix maybe more appropriate.
No, you've pointed out a problem with dm-thinp not properly tearing down its resources. Same for dm-cache. So a generic fix doesn't make sense. And your particular generic fix merely papers over the resource leak, while also causing DM core to no longer properly wait for outstanding IO to complete.
Quoted 3 lines
After adding
cancel_delayed_work_sync()in__pool_destroy(), this will make it possible to callcancel_delayed_work_sync(&pool->waker)twice when dm is not suspend.
That's not a problem, that's the cost of properly accounting for resources where we must.
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Quoted 130 lines · Mike Snitzer
Quoted 118 lines · Luo Meng
Quoted 106 lines · Mike Snitzer
Quoted 94 lines · Luo Meng
From: Luo Meng <[email protected]>
When
dm_resume()anddm_destroy()are concurrent, it will lead to UAF.One of the concurrency UAF can be shown as below:
use free
do_resume|__find_device_hash_cell | dm_get | atomic_inc(&md->holders) | | dm_destroy | __dm_destroy | if (!dm_suspended_md(md)) | atomic_read(&md->holders) | msleep(1) dm_resume | __dm_resume | dm_table_resume_targets | pool_resume | do_waker #add delay work | | dm_table_destroy | pool_dtr | __pool_dec | __pool_destroy | destroy_workqueue | kfree(pool) # free pool time out
__do_softirq
run_timer_softirq# pool has already been freedThis can be easily reproduced using:
1. create thin-pool
2. dmsetup suspend pool
3. dmsetup resume pool
4. dmsetupremove_all# Concurrent with 3The root cause of UAF bugs is that
dm_resume()adds timer afterdm_destroy()skips cancel timer beause of suspend status. After timeout, it will callrun_timer_softirq(), however pool has already been freed. The concurrency UAF bug will happen.Therefore, canceling timer is moved after
md->holdersis zero.Signed-off-by Luo Meng <[email protected]>--- drivers/md/dm.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)drivers/md/dm.c +13
-13@@ -2420,6 +2420,19 @@ static void __dm_destroy(struct mapped_device *md, bool wait)blk_mark_disk_dead (md->disk); +/* +* Rare, but there may be I/O requests still going to complete, +* for example. Wait for all references to disappear. +* No one should increment the reference count of the mapped_device, +* after the mapped_device state becomes DMF_FREEING. +*/ +if (wait) +while (atomic_read (&md->holders)) +msleep (1 ); +else if (atomic_read (&md->holders)) +DMWARN ("%s: Forcibly removing mapped_device still in use! (%d users)" , +dm_device_name (md),atomic_read (&md->holders)); +/* * Take suspend_lock so that presuspend and postsuspend methods * do not race with internal suspend. @@ -2436,19 +2449,6 @@ static void __dm_destroy(struct mapped_device *md, bool wait)dm_put_live_table (md, srcu_idx);mutex_unlock (&md->suspend_lock); -/* -* Rare, but there may be I/O requests still going to complete, -* for example. Wait for all references to disappear. -* No one should increment the reference count of the mapped_device, -* after the mapped_device state becomes DMF_FREEING. -*/ -if (wait) -while (atomic_read (&md->holders)) -msleep (1 ); -else if (atomic_read (&md->holders)) -DMWARN ("%s: Forcibly removing mapped_device still in use! (%d users)" , -dm_device_name (md),atomic_read (&md->holders)); -dm_table_destroy (__unbind (md));free_dev (md); }Signature · 1 line
2.31.1Thanks for the report but your fix seems wrong. A thin-pool specific fix seems much more appropriate. Does this fix the issue?
drivers/md/dm-thin.c +0
-0drivers/md/dm-thin.c static void __pool_destroy(struct pool *pool) · line 2895
Quoted 7 lines
drivers/md/dm-thin.c +2
-0@@ -2889,6 +2889,8 @@ static void __pool_destroy(struct pool *pool)dm_bio_prison_destroy (pool->prison);dm_kcopyd_client_destroy (pool->copier); +cancel_delayed_work_sync (&pool->waker); +cancel_delayed_work_sync (&pool->no_space_timeout);if (pool->wq)destroy_workqueue (pool->wq);Thanks for your reply.
Quoted 2 lines
However this issue exits not only in thin-pool,
cache_targetalso has thisissus. Generic fix maybe more appropriate.No, you've pointed out a problem with dm-thinp not properly tearing down its resources. Same for dm-cache. So a generic fix doesn't make sense. And your particular generic fix merely papers over the resource leak, while also causing DM core to no longer properly wait for outstanding IO to complete.
Quoted 7 lines
Quoted 3 lines
After adding
cancel_delayed_work_sync()in__pool_destroy(), this will make it possible to callcancel_delayed_work_sync(&pool->waker)twice when dm is not suspend.That's not a problem, that's the cost of properly accounting for resources where we must.
Thanks.
I will send v2 soon.
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel