linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] block: make sure last_lookup set as NULL after part deleted
@ 2019-12-31 11:09 Yufen Yu
  2019-12-31 14:55 ` Hou Tao
  0 siblings, 1 reply; 21+ messages in thread
From: Yufen Yu @ 2019-12-31 11:09 UTC (permalink / raw)
  To: axboe; +Cc: linux-block, ming.lei, hch, zhengchuan, houtao1, yi.zhang,
	yuyufen

When delete partition executes concurrently with IOs issue,
it may cause use-after-free on part in disk_map_sector_rcu()
as following:

blk_account_io_start(req1)  delete_partition  blk_account_io_start(req2)

rcu_read_lock()
disk_map_sector_rcu
part = rcu_dereference(ptbl->part[4])
                           rcu_assign_pointer(ptbl->part[4], NULL);
                           rcu_assign_pointer(ptbl->last_lookup, NULL);
rcu_assign_pointer(ptbl->last_lookup, part);

                           hd_struct_kill(part)
!hd_struct_try_get
  part = &rq->rq_disk->part0;
rcu_read_unlock()
                           __delete_partition
                           call_rcu
                                            rcu_read_lock
                                            disk_map_sector_rcu
                                            part = rcu_dereference(ptbl->last_lookup);

                           delete_partition_work_fn
                           free(part)
                                            hd_struct_try_get(part)
                                            BUG_ON use-after-free

req1 try to get 'ptbl->part[4]', while the part is beening
deleted. Although the delete_partition() try to set last_lookup
as NULL, req1 can overwrite it as 'part[4]' again.

After calling call_rcu() and free() for the part, req2 can
get the part by last_lookup, resulting in use after free.

In fact, this bug has been reported by syzbot:
    https://lkml.org/lkml/2019/1/4/357

We fix the bug by rereading ptbl->part[i] after setting
last_lookup. If part[i] is NULL, that means the part will
be freed, we need to clear last_lookup.

Then we can make sure last_lookup have been set as NULL
before call_rcu(). Thus, others can not get the freed part.

Fixes: a6f23657d307 ("block: add one-hit cache for disk partition lookup")
Reported-by: Zheng Chuan <zhengchuan@huawei.com>
Signed-off-by: Yufen Yu <yuyufen@huawei.com>
---
 block/genhd.c             | 16 ++++++++++++++++
 block/partition-generic.c |  7 +++++++
 2 files changed, 23 insertions(+)

diff --git a/block/genhd.c b/block/genhd.c
index ff6268970ddc..39fa8999905f 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -293,7 +293,23 @@ struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
 		part = rcu_dereference(ptbl->part[i]);
 
 		if (part && sector_in_part(part, sector)) {
+			/*
+			 * After delete_partition() trying to delete this
+			 * part and setting last_lookup as 'NULL', we can
+			 * overwrite it here. Then, others may use the freed
+			 * part by reading last_lookup.
+			 *
+			 * To avoiding the use-after-free, we reread the
+			 * value of ptbl->part[i] here. If it has been set
+			 * as 'NULL', that means the part will be freed.
+			 * And we need to clear last_lookup also.
+			 */
 			rcu_assign_pointer(ptbl->last_lookup, part);
+			part = rcu_dereference(ptbl->part[i]);
+			if (part == NULL) {
+				rcu_assign_pointer(ptbl->last_lookup, NULL);
+				break;
+			}
 			return part;
 		}
 	}
diff --git a/block/partition-generic.c b/block/partition-generic.c
index 1d20c9cf213f..1e0065ed6f02 100644
--- a/block/partition-generic.c
+++ b/block/partition-generic.c
@@ -284,6 +284,13 @@ void delete_partition(struct gendisk *disk, int partno)
 		return;
 
 	rcu_assign_pointer(ptbl->part[partno], NULL);
+	/*
+	 * Without the memory barrier, disk_map_sector_rcu()
+	 * may read the old value after overwriting the
+	 * last_lookup. Then it can not clear last_lookup,
+	 * which may cause use-after-free.
+	 */
+	smp_mb();
 	rcu_assign_pointer(ptbl->last_lookup, NULL);
 	kobject_put(part->holder_dir);
 	device_del(part_to_dev(part));
-- 
2.17.2


^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2020-01-08  3:20 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-12-31 11:09 [PATCH] block: make sure last_lookup set as NULL after part deleted Yufen Yu
2019-12-31 14:55 ` Hou Tao
2019-12-31 23:11   ` Paul E. McKenney
2020-01-01  2:33     ` htbegin
2020-01-01  3:39       ` htbegin
2020-01-03 23:45     ` Joel Fernandes
2020-01-04  9:16       ` Hou Tao
2020-01-02  1:23   ` Ming Lei
2020-01-03  3:06     ` Hou Tao
2020-01-03  4:18       ` Ming Lei
2020-01-03  7:35         ` Hou Tao
2020-01-03  8:17           ` Ming Lei
2020-01-03 12:03             ` Yufen Yu
2020-01-03 15:16               ` Ming Lei
2020-01-06  7:39                 ` Yufen Yu
2020-01-06  8:11                   ` Ming Lei
2020-01-06  9:41                     ` Hou Tao
2020-01-06 10:05                       ` Ming Lei
2020-01-07 11:40                         ` Hou Tao
2020-01-08  3:19                           ` Ming Lei
2020-01-03 12:43   ` Yufen Yu

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).