Skip to contentSkip to conversation

dm: Fix UAF in run_timer_softirq()

PATCH New Luo Meng 1 participant 5 messages Nov 29, 2022 Device mapper

lore
Share & tools
lore

Luo Meng

author
#0 lore raw

v2 cover dm: Fix UAF in run_timer_softirq()

*** BLURB HERE ***

Luo Meng (4):
dm thin: Fix UAF in run_timer_softirq()
dm clone: Fix UAF in clone_dtr()
dm cache: Fix UAF in destroy()
dm integrity: Fix UAF in dm_integrity_dtr()

 drivers/md/dm-cache-target.c | 1 +
 drivers/md/dm-clone-target.c | 1 +
 drivers/md/dm-integrity.c    | 2 ++
 drivers/md/dm-thin.c         | 2 ++
 4 files changed, 6 insertions(+)
Signature · 6 lines
2.31.1

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author
#1 lore raw

PATCH v2 1/4 dm thin: Fix UAF in run_timer_softirq()

When dm_resume() and dm_destroy() are concurrent, it will lead to UAF, as follows:

 BUG: KASAN: use-after-free in __run_timers+0x173/0x710
 Write of size 8 at addr ffff88816d9490f0 by task swapper/0/0

 CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.0.0-rc6-00001-gaed496328fcd-dirty #26
 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS ?-20190727_073836-buildvm-ppc64le-16.ppc.fedoraproject.org-3.fc31 04/01/2014
 Call Trace:
  <IRQ>
  dump_stack_lvl+0x73/0x9f
  print_report.cold+0x132/0xaa2
  _raw_spin_lock_irqsave+0xcd/0x160
  __run_timers+0x173/0x710
  kasan_report+0xad/0x110
  __run_timers+0x173/0x710
  __asan_store8+0x9c/0x140
  __run_timers+0x173/0x710
  call_timer_fn+0x310/0x310
  pvclock_clocksource_read+0xfa/0x250
  kvm_clock_read+0x2c/0x70
  kvm_clock_get_cycles+0xd/0x20
  ktime_get+0x5c/0x110
  lapic_next_event+0x38/0x50
  clockevents_program_event+0xf1/0x1e0
  run_timer_softirq+0x49/0x90
  __do_softirq+0x16e/0x62c
  __irq_exit_rcu+0x1fa/0x270
  irq_exit_rcu+0x12/0x20
  sysvec_apic_timer_interrupt+0x8e/0xc0

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_put                            |
    atomic_dec(&md->holders)        |
                                    |     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 again in __pool_destroy().

Fixes 991d9fa02da0d (dm: add thin provisioning target)
---
 drivers/md/dm-thin.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/md/dm-thin.c +2 -0
index e76c96c760a9..dc271c107fb5 100644
@@ -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);
 
Signature · 6 lines
2.31.1

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author
#2 lore raw

PATCH v2 2/4 dm clone: Fix UAF in clone_dtr()

In reply to Luo Meng

Dm_clone also has the same UAF problem when dm_resume() and dm_destroy() are concurrent.

Therefore, canceling timer again in clone_dtr().

Fixes 7431b7835f554 (dm: add clone target)
---
 drivers/md/dm-clone-target.c | 1 +
 1 file changed, 1 insertion(+)
drivers/md/dm-clone-target.c +1 -0
index 2f1cc66d2641..29e0b85eeaf0 100644
@@ -1958,6 +1958,7 @@ static void clone_dtr(struct dm_target *ti)
 
 	mempool_exit(&clone->hydration_pool);
 	dm_kcopyd_client_destroy(clone->kcopyd_client);
+	cancel_delayed_work_sync(&clone->waker);
 	destroy_workqueue(clone->wq);
 	hash_table_exit(clone);
 	dm_clone_metadata_close(clone->cmd);
Signature · 6 lines
2.31.1

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author
#3 lore raw

PATCH v2 3/4 dm cache: Fix UAF in destroy()

In reply to Luo Meng

Dm_cache also has the same UAF problem when dm_resume() and dm_destroy() are concurrent.

Therefore, canceling timer again in destroy().

Fixes c6b4fcbad044e (dm: add cache target)
---
 drivers/md/dm-cache-target.c | 1 +
 1 file changed, 1 insertion(+)
drivers/md/dm-cache-target.c +1 -0
index 54a8d5c9a44e..624a6335c832 100644
@@ -1887,6 +1887,7 @@ static void destroy(struct cache *cache)
 	if (cache->prison)
 		dm_bio_prison_destroy_v2(cache->prison);
 
+	cancel_delayed_work_sync(&cache->waker);
 	if (cache->wq)
 		destroy_workqueue(cache->wq);
 
Signature · 6 lines
2.31.1

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author
#4 lore raw

PATCH v2 4/4 dm integrity: Fix UAF in dm_integrity_dtr()

In reply to Luo Meng

Dm_integrity also has the same UAF problem when dm_resume() and dm_destroy() are concurrent.

Therefore, canceling timer again in dm_integrity_dtr().

Fixes 7eada909bfd7a (dm: add integrity target)
---
 drivers/md/dm-integrity.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/md/dm-integrity.c +2 -0
index e97e9f97456d..1388ee35571e 100644
@@ -4558,6 +4558,8 @@ static void dm_integrity_dtr(struct dm_target *ti)
 	BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
 	BUG_ON(!list_empty(&ic->wait_list));
 
+	if (ic->mode == 'B')
+		cancel_delayed_work_sync(&ic->bitmap_flush_work);
 	if (ic->metadata_wq)
 		destroy_workqueue(ic->metadata_wq);
 	if (ic->wait_wq)
Signature · 6 lines
2.31.1

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel