Linux block layer
 help / color / mirror / Atom feed
* [PATCH] block: bio-integrity: fix memory leak in bio_integrity_map_user()
@ 2026-05-13  7:05 Dmitry Antipov
  2026-05-13 14:32 ` Caleb Sander Mateos
  2026-05-15 13:13 ` Christoph Hellwig
  0 siblings, 2 replies; 4+ messages in thread
From: Dmitry Antipov @ 2026-05-13  7:05 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Weidong Zhu, Chao Shi, Sungwoo Kim, Dave Tian, Keith Busch,
	Caleb Sander Mateos, Christoph Hellwig, linux-block, lvc-project,
	Dmitry Antipov

Since 'iov_iter_extract_pages()' may allocate new array of pages
even when it returns non-zero error value, matching cleanup with
'vfree()' should be performed on all return paths afterwards. So
adjust 'bio_integrity_map_user()' to ensure that both 'pages' and
'bvec' arrays are always freed on return.

Fixes: 8582792cf23b ("block: bio-integrity: Fix null-ptr-deref in bio_integrity_map_user()")
Fixes: 492c5d455969 ("block: bio-integrity: directly map user buffers")
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 block/bio-integrity.c | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)

diff --git a/block/bio-integrity.c b/block/bio-integrity.c
index e796de1a749e..53fb04adb09b 100644
--- a/block/bio-integrity.c
+++ b/block/bio-integrity.c
@@ -400,7 +400,7 @@ int bio_integrity_map_user(struct bio *bio, struct iov_iter *iter)
 	ret = iov_iter_extract_pages(iter, &pages, bytes, nr_vecs,
 					extraction_flags, &offset);
 	if (unlikely(ret < 0))
-		goto free_bvec;
+		goto out_free;
 
 	/*
 	 * Handle partial pinning. This can happen when pin_user_pages_fast()
@@ -414,16 +414,12 @@ int bio_integrity_map_user(struct bio *bio, struct iov_iter *iter)
 			for (i = 0; i < npinned; i++)
 				unpin_user_page(pages[i]);
 		}
-		if (pages != stack_pages)
-			kvfree(pages);
 		ret = -EFAULT;
-		goto free_bvec;
+		goto out_free;
 	}
 
 	nr_bvecs = bvec_from_pages(bvec, pages, nr_vecs, bytes, offset,
 				   &is_p2p);
-	if (pages != stack_pages)
-		kvfree(pages);
 	if (nr_bvecs > queue_max_integrity_segments(q))
 		copy = true;
 	if (is_p2p)
@@ -434,15 +430,10 @@ int bio_integrity_map_user(struct bio *bio, struct iov_iter *iter)
 	else
 		ret = bio_integrity_init_user(bio, bvec, nr_bvecs, bytes);
 	if (ret)
-		goto release_pages;
-	if (bvec != stack_vec)
-		kfree(bvec);
-
-	return 0;
-
-release_pages:
-	bio_integrity_unpin_bvec(bvec, nr_bvecs);
-free_bvec:
+		bio_integrity_unpin_bvec(bvec, nr_bvecs);
+out_free:
+	if (pages != stack_pages)
+		kvfree(pages);
 	if (bvec != stack_vec)
 		kfree(bvec);
 	return ret;
-- 
2.54.0


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

end of thread, other threads:[~2026-05-15 13:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-13  7:05 [PATCH] block: bio-integrity: fix memory leak in bio_integrity_map_user() Dmitry Antipov
2026-05-13 14:32 ` Caleb Sander Mateos
2026-05-13 15:43   ` Dmitry Antipov
2026-05-15 13:13 ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox