Skip to content

Commit 8fa2447

Browse files
swkim101gregkh
authored andcommitted
block: bio-integrity: Fix null-ptr-deref in bio_integrity_map_user()
[ Upstream commit 8582792 ] pin_user_pages_fast() can partially succeed and return the number of pages that were actually pinned. However, the bio_integrity_map_user() does not handle this partial pinning. This leads to a general protection fault since bvec_from_pages() dereferences an unpinned page address, which is 0. To fix this, add a check to verify that all requested memory is pinned. If partial pinning occurs, unpin the memory and return -EFAULT. Kernel Oops: Oops: general protection fault, probably for non-canonical address 0xdffffc0000000001: 0000 [#1] SMP KASAN NOPTI KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f] CPU: 0 UID: 0 PID: 1061 Comm: nvme-passthroug Not tainted 7.0.0-11783-g90957f9314e8-dirty #16 PREEMPT(lazy) Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.17.0-0-gb52ca86e094d-prebuilt.qemu.org 04/01/2014 RIP: 0010:bio_integrity_map_user.cold+0x1b0/0x9d6 Fixes: 492c5d4 ("block: bio-integrity: directly map user buffers") Acked-by: Chao Shi <cshi008@fiu.edu> Acked-by: Weidong Zhu <weizhu@fiu.edu> Acked-by: Dave Tian <daveti@purdue.edu> Signed-off-by: Sungwoo Kim <iam@sung-woo.kim> Tested-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com> Link: linux-blktests/blktests#244 Link: https://patch.msgid.link/20260512050929.541397-2-iam@sung-woo.kim Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 09e2231 commit 8fa2447

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

block/bio-integrity.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,24 @@ int bio_integrity_map_user(struct bio *bio, struct iov_iter *iter)
338338
if (unlikely(ret < 0))
339339
goto free_bvec;
340340

341+
/*
342+
* Handle partial pinning. This can happen when pin_user_pages_fast()
343+
* returns fewer pages than requested.
344+
*/
345+
if (user_backed_iter(iter) && unlikely(ret != bytes)) {
346+
if (ret > 0) {
347+
int npinned = DIV_ROUND_UP(offset + ret, PAGE_SIZE);
348+
int i;
349+
350+
for (i = 0; i < npinned; i++)
351+
unpin_user_page(pages[i]);
352+
}
353+
if (pages != stack_pages)
354+
kvfree(pages);
355+
ret = -EFAULT;
356+
goto free_bvec;
357+
}
358+
341359
nr_bvecs = bvec_from_pages(bvec, pages, nr_vecs, bytes, offset,
342360
&is_p2p);
343361
if (pages != stack_pages)

0 commit comments

Comments
 (0)