* [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* Re: [PATCH] block: bio-integrity: fix memory leak in bio_integrity_map_user()
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
1 sibling, 1 reply; 4+ messages in thread
From: Caleb Sander Mateos @ 2026-05-13 14:32 UTC (permalink / raw)
To: Dmitry Antipov
Cc: Jens Axboe, Weidong Zhu, Chao Shi, Sungwoo Kim, Dave Tian,
Keith Busch, Christoph Hellwig, linux-block, lvc-project
On Wed, May 13, 2026 at 12:05 AM Dmitry Antipov <dmantipov@yandex.ru> wrote:
>
> 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.
I preferred your previous patch changing iov_iter_extract_pages() to
free the page array on error. (I left some minor comments on the code
change, but the general approach seems good.) Even though
bio_integrity_map_user() is the only current caller that requests
iov_iter_extract_pages() allocate a page array, others could be added
in the future and forget to do this freeing. And it's conventional for
functions to release any allocated resources on failure. Is there a
reason you decided to modify bio_integrity_map_user() instead?
Best,
Caleb
>
> 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 [flat|nested] 4+ messages in thread* Re: [PATCH] block: bio-integrity: fix memory leak in bio_integrity_map_user()
2026-05-13 14:32 ` Caleb Sander Mateos
@ 2026-05-13 15:43 ` Dmitry Antipov
0 siblings, 0 replies; 4+ messages in thread
From: Dmitry Antipov @ 2026-05-13 15:43 UTC (permalink / raw)
To: Caleb Sander Mateos
Cc: Jens Axboe, Weidong Zhu, Chao Shi, Sungwoo Kim, Dave Tian,
Keith Busch, Christoph Hellwig, linux-block, lvc-project
On Wed, 2026-05-13 at 07:32 -0700, Caleb Sander Mateos wrote:
> I preferred your previous patch changing iov_iter_extract_pages() to
> free the page array on error.
Me too.
> Even though bio_integrity_map_user() is the only current caller that
> requests iov_iter_extract_pages() allocate a page array, others could
> be added in the future and forget to do this freeing.
It was in the past. Again, the whole thing was started from 6.12, see
https://lore.kernel.org/stable/20260505094529.406783-1-dmantipov@yandex.ru
(the commit message is somewhat messy BTW), and I'm definitely interested
in supporting that longterm too.
> Is there a reason you decided to modify bio_integrity_map_user() instead?
Sure. Such an ad-hoc quick fixes are much more likely to be integrated
within a reasonable time and effort from the maintainer. OTOH better wait for
Jens' answer before doing anything else.
Dmitry
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] block: bio-integrity: fix memory leak in bio_integrity_map_user()
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-15 13:13 ` Christoph Hellwig
1 sibling, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2026-05-15 13:13 UTC (permalink / raw)
To: Dmitry Antipov
Cc: Jens Axboe, Weidong Zhu, Chao Shi, Sungwoo Kim, Dave Tian,
Keith Busch, Caleb Sander Mateos, Christoph Hellwig, linux-block,
lvc-project, Kanchan Joshi, Anuj Gupta
On Wed, May 13, 2026 at 10:05:15AM +0300, Dmitry Antipov wrote:
> 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>
The change looks fine:
Reviewed-by: Christoph Hellwig <hch@lst.de>
We'd still be much better off converting it to iov_iter_extract_bvecs
to share common code, and make it more efficient by no needing the
separate pages allocation.
> ---
> 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
---end quoted text---
^ permalink raw reply [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