Skip to content

Commit b7d6c30

Browse files
Ming Leiaxboe
authored andcommitted
block: fix use-after-free on cached last_lookup partition
delete_partition() clears the cached last_lookup partition. However the .last_lookup cache may be overwritten by one IO path after it is cleared from delete_partition(). Then another IO path may use the cached deleting partition after hd_struct_free() is called, then use-after-free is triggered on the cached partition. Fixes the issue by the following approach: 1) always get the partition's refcount via hd_struct_try_get() before setting .last_lookup 2) move clearing .last_lookup from delete_partition() to hd_struct_free() which is the release handle of the partition's percpu-refcount, so that no IO path can cache deleteing partition via .last_lookup. It is one candidate approach of Yufen's patch[1] which adds overhead in fast path by indirect lookup which may introduce one extra cacheline in IO path. Also this patch relies on percpu-refcount's protection, and it is easier to understand and verify. [1] https://lore.kernel.org/linux-block/[email protected]/T/#t Reported-by: Yufen Yu <[email protected]> Signed-off-by: Ming Lei <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Hou Tao <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent aa880ad commit b7d6c30

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

block/blk-core.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,18 +1362,6 @@ void blk_account_io_start(struct request *rq, bool new_io)
13621362
part_stat_inc(part, merges[rw]);
13631363
} else {
13641364
part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
1365-
if (!hd_struct_try_get(part)) {
1366-
/*
1367-
* The partition is already being removed,
1368-
* the request will be accounted on the disk only
1369-
*
1370-
* We take a reference on disk->part0 although that
1371-
* partition will never be deleted, so we can treat
1372-
* it as any other partition.
1373-
*/
1374-
part = &rq->rq_disk->part0;
1375-
hd_struct_get(part);
1376-
}
13771365
part_inc_in_flight(rq->q, part, rw);
13781366
rq->part = part;
13791367
}

block/genhd.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,12 @@ static inline int sector_in_part(struct hd_struct *part, sector_t sector)
344344
* primarily used for stats accounting.
345345
*
346346
* CONTEXT:
347-
* RCU read locked. The returned partition pointer is valid only
348-
* while preemption is disabled.
347+
* RCU read locked. The returned partition pointer is always valid
348+
* because its refcount is grabbed.
349349
*
350350
* RETURNS:
351351
* Found partition on success, part0 is returned if no partition matches
352+
* or the matched partition is being deleted.
352353
*/
353354
struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
354355
{
@@ -359,17 +360,25 @@ struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
359360
ptbl = rcu_dereference(disk->part_tbl);
360361

361362
part = rcu_dereference(ptbl->last_lookup);
362-
if (part && sector_in_part(part, sector))
363+
if (part && sector_in_part(part, sector) && hd_struct_try_get(part))
363364
return part;
364365

365366
for (i = 1; i < ptbl->len; i++) {
366367
part = rcu_dereference(ptbl->part[i]);
367368

368369
if (part && sector_in_part(part, sector)) {
370+
/*
371+
* only live partition can be cached for lookup,
372+
* so use-after-free on cached & deleting partition
373+
* can be avoided
374+
*/
375+
if (!hd_struct_try_get(part))
376+
break;
369377
rcu_assign_pointer(ptbl->last_lookup, part);
370378
return part;
371379
}
372380
}
381+
hd_struct_get(&disk->part0);
373382
return &disk->part0;
374383
}
375384

block/partitions/core.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ static void hd_struct_free_work(struct work_struct *work)
288288
static void hd_struct_free(struct percpu_ref *ref)
289289
{
290290
struct hd_struct *part = container_of(ref, struct hd_struct, ref);
291+
struct gendisk *disk = part_to_disk(part);
292+
struct disk_part_tbl *ptbl =
293+
rcu_dereference_protected(disk->part_tbl, 1);
294+
295+
rcu_assign_pointer(ptbl->last_lookup, NULL);
296+
put_device(disk_to_dev(disk));
291297

292298
INIT_RCU_WORK(&part->rcu_work, hd_struct_free_work);
293299
queue_rcu_work(system_wq, &part->rcu_work);
@@ -309,8 +315,12 @@ void delete_partition(struct gendisk *disk, struct hd_struct *part)
309315
struct disk_part_tbl *ptbl =
310316
rcu_dereference_protected(disk->part_tbl, 1);
311317

318+
/*
319+
* ->part_tbl is referenced in this part's release handler, so
320+
* we have to hold the disk device
321+
*/
322+
get_device(disk_to_dev(part_to_disk(part)));
312323
rcu_assign_pointer(ptbl->part[part->partno], NULL);
313-
rcu_assign_pointer(ptbl->last_lookup, NULL);
314324
kobject_put(part->holder_dir);
315325
device_del(part_to_dev(part));
316326

0 commit comments

Comments
 (0)