Re: [PATCH v3] Fix null-ptr-deref in bio_integrity_map_user()
From: Sungwoo Kim
Date: Wed Apr 22 2026 - 23:34:13 EST
This is a follow-up based on Sashiko's comments.
https://sashiko.dev/#/patchset/20260420020327.1667156-3-iam%40sung-woo.kim
On Sun, Apr 19, 2026 at 10:05 PM Sungwoo Kim <iam@xxxxxxxxxxxx> wrote:
>
[snip]
> diff --git a/block/bio-integrity.c b/block/bio-integrity.c
> index e79eaf047794..c8cfd15fb589 100644
> --- a/block/bio-integrity.c
> +++ b/block/bio-integrity.c
> @@ -402,6 +402,20 @@ int bio_integrity_map_user(struct bio *bio, struct iov_iter *iter)
> extraction_flags, &offset);
> if (unlikely(ret < 0))
> goto free_bvec;
It's pre-existing, though, free_bvec does not kvfree(pages), leading
to a memory leak.
> + /* Handle partial pinning. This can happen when pin_user_pages_fast()
> + * returns fewer pages than requested
> + */
> + if (unlikely(ret != bytes)) {
> + int npinned = DIV_ROUND_UP(offset + ret, PAGE_SIZE);
If ret == 0, npinned becomes 1, whereas there is no actually pinned memory
> + int i;
> +
> + for (i = 0; i < npinned; i++)
> + unpin_user_page(pages[i]);
... which results in invalid access here.
Also, pages[i] can be ITER_KVEC, ITER_BVEC, ITER_FOLIOQ, etc., which
do not pin memory.
Thus, calling unpin_user_page(pages[i]) unconditionally is incorrect.
To fix this, call unpin_user_page() when user_backed_iter(iter) is true.
> + if (pages != stack_pages)
> + kvfree(pages);
> + ret = -EFAULT;
> + goto free_bvec;
> + }
>
> nr_bvecs = bvec_from_pages(bvec, pages, nr_vecs, bytes, offset,
> &is_p2p);
> --
> 2.47.3
>