Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

dm: clear cloned request bio pointer when last clone bio completes

Stale rq->bio values have been observed to cause double-initialization of
cloned bios in request-based device-mapper targets, leading to
use-after-free and double-free scenarios.

One such case occurs when using dm-multipath on top of a PCIe NVMe
namespace, where cloned request bios are freed during
blk_complete_request(), but rq->bio is left intact. Subsequent clone
teardown then attempts to free the same bios again via
blk_rq_unprep_clone().

The resulting double-free path looks like:

nvme_pci_complete_batch()
nvme_complete_batch()
blk_mq_end_request_batch()
blk_complete_request() // called on a DM clone request
bio_endio() // first free of all clone bios
...
rq->end_io() // end_clone_request()
dm_complete_request(tio->orig)
dm_softirq_done()
dm_done()
dm_end_request()
blk_rq_unprep_clone() // second free of clone bios

Fix this by clearing the clone request's bio pointer when the last cloned
bio completes, ensuring that later teardown paths do not attempt to free
already-released bios.

Signed-off-by: Michael Liang <mliang@purestorage.com>
Reviewed-by: Mohamed Khalfella <mkhalfella@purestorage.com>
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Cc: stable@vger.kernel.org

authored by

Michael Liang and committed by
Mikulas Patocka
fb8a6c18 c84e21a8

+10 -3
+10 -3
drivers/md/dm-rq.c
··· 109 109 */ 110 110 tio->completed += nr_bytes; 111 111 112 + if (!is_last) 113 + return; 114 + /* 115 + * At this moment we know this is the last bio of the cloned request, 116 + * and all cloned bios have been released, so reset the clone request's 117 + * bio pointer to avoid double free. 118 + */ 119 + tio->clone->bio = NULL; 120 + exit: 112 121 /* 113 122 * Update the original request. 114 123 * Do not use blk_mq_end_request() here, because it may complete 115 124 * the original request before the clone, and break the ordering. 116 125 */ 117 - if (is_last) 118 - exit: 119 - blk_update_request(tio->orig, BLK_STS_OK, tio->completed); 126 + blk_update_request(tio->orig, BLK_STS_OK, tio->completed); 120 127 } 121 128 122 129 static struct dm_rq_target_io *tio_from_request(struct request *rq)