On 2023-11-11 09:53, José Pekkarinen wrote:
> syzbot reports there is a memory leak in iov_iter_extract_pages where
> in
> the unlikely case of having an error in pin_user_pages_fast, the pages
> aren't free. This patch will free it before returning. Output of mem
> leak follows:
>
> BUG: memory leak
> unreferenced object 0xffff888109d2e400 (size 1024):
> comm "syz-executor121", pid 5006, jiffies 4294943225 (age 17.760s)
> hex dump (first 32 bytes):
> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
> backtrace:
> [<ffffffff81554bbb>] __do_kmalloc_node mm/slab_common.c:984
> [inline]
> [<ffffffff81554bbb>] __kmalloc_node+0x4b/0x150 mm/slab_common.c:992
> [<ffffffff815440f9>] kmalloc_node include/linux/slab.h:602 [inline]
> [<ffffffff815440f9>] kvmalloc_node+0x99/0x170 mm/util.c:604
> [<ffffffff824c52fe>] kvmalloc include/linux/slab.h:720 [inline]
> [<ffffffff824c52fe>] kvmalloc_array include/linux/slab.h:738
> [inline]
> [<ffffffff824c52fe>] want_pages_array lib/iov_iter.c:985 [inline]
> [<ffffffff824c52fe>] iov_iter_extract_user_pages
> lib/iov_iter.c:1765 [inline]
> [<ffffffff824c52fe>] iov_iter_extract_pages+0x1ee/0xa40
> lib/iov_iter.c:1831
> [<ffffffff824125a7>] bio_map_user_iov+0x167/0x5d0
> block/blk-map.c:297
> [<ffffffff82412df3>] blk_rq_map_user_iov+0x3e3/0xb30
> block/blk-map.c:664
> [<ffffffff82413943>] blk_rq_map_user block/blk-map.c:691 [inline]
> [<ffffffff82413943>] blk_rq_map_user_io+0x143/0x160
> block/blk-map.c:724
> [<ffffffff82ca0925>] sg_io+0x285/0x510
> drivers/scsi/scsi_ioctl.c:456
> [<ffffffff82ca1025>] scsi_cdrom_send_packet+0x1b5/0x480
> drivers/scsi/scsi_ioctl.c:820
> [<ffffffff82ca13ba>] scsi_ioctl+0xca/0xd30
> drivers/scsi/scsi_ioctl.c:903
> [<ffffffff82d35964>] sg_ioctl+0x5f4/0x10a0 drivers/scsi/sg.c:1163
> [<ffffffff8168e602>] vfs_ioctl fs/ioctl.c:51 [inline]
> [<ffffffff8168e602>] __do_sys_ioctl fs/ioctl.c:870 [inline]
> [<ffffffff8168e602>] __se_sys_ioctl fs/ioctl.c:856 [inline]
> [<ffffffff8168e602>] __x64_sys_ioctl+0xf2/0x140 fs/ioctl.c:856
> [<ffffffff84ad2bb8>] do_syscall_x64 arch/x86/entry/common.c:50
> [inline]
> [<ffffffff84ad2bb8>] do_syscall_64+0x38/0xb0
> arch/x86/entry/common.c:80
> [<ffffffff84c0008b>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
>
> BUG: memory leak
> unreferenced object 0xffff888109d2dc00 (size 1024):
> comm "syz-executor121", pid 5007, jiffies 4294943747 (age 12.540s)
> hex dump (first 32 bytes):
> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
> backtrace:
> [<ffffffff81554bbb>] __do_kmalloc_node mm/slab_common.c:984
> [inline]
> [<ffffffff81554bbb>] __kmalloc_node+0x4b/0x150 mm/slab_common.c:992
> [<ffffffff815440f9>] kmalloc_node include/linux/slab.h:602 [inline]
> [<ffffffff815440f9>] kvmalloc_node+0x99/0x170 mm/util.c:604
> [<ffffffff824c52fe>] kvmalloc include/linux/slab.h:720 [inline]
> [<ffffffff824c52fe>] kvmalloc_array include/linux/slab.h:738
> [inline]
> [<ffffffff824c52fe>] want_pages_array lib/iov_iter.c:985 [inline]
> [<ffffffff824c52fe>] iov_iter_extract_user_pages
> lib/iov_iter.c:1765 [inline]
> [<ffffffff824c52fe>] iov_iter_extract_pages+0x1ee/0xa40
> lib/iov_iter.c:1831
> [<ffffffff824125a7>] bio_map_user_iov+0x167/0x5d0
> block/blk-map.c:297
> [<ffffffff82412df3>] blk_rq_map_user_iov+0x3e3/0xb30
> block/blk-map.c:664
> [<ffffffff82413943>] blk_rq_map_user block/blk-map.c:691 [inline]
> [<ffffffff82413943>] blk_rq_map_user_io+0x143/0x160
> block/blk-map.c:724
> [<ffffffff82ca0925>] sg_io+0x285/0x510
> drivers/scsi/scsi_ioctl.c:456
> [<ffffffff82ca1025>] scsi_cdrom_send_packet+0x1b5/0x480
> drivers/scsi/scsi_ioctl.c:820
> [<ffffffff82ca13ba>] scsi_ioctl+0xca/0xd30
> drivers/scsi/scsi_ioctl.c:903
> [<ffffffff82d35964>] sg_ioctl+0x5f4/0x10a0 drivers/scsi/sg.c:1163
> [<ffffffff8168e602>] vfs_ioctl fs/ioctl.c:51 [inline]
> [<ffffffff8168e602>] __do_sys_ioctl fs/ioctl.c:870 [inline]
> [<ffffffff8168e602>] __se_sys_ioctl fs/ioctl.c:856 [inline]
> [<ffffffff8168e602>] __x64_sys_ioctl+0xf2/0x140 fs/ioctl.c:856
> [<ffffffff84ad2bb8>] do_syscall_x64 arch/x86/entry/common.c:50
> [inline]
> [<ffffffff84ad2bb8>] do_syscall_64+0x38/0xb0
> arch/x86/entry/common.c:80
> [<ffffffff84c0008b>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
>
> BUG: memory leak
> unreferenced object 0xffff888109d2d800 (size 1024):
> comm "syz-executor121", pid 5010, jiffies 4294944269 (age 7.320s)
> hex dump (first 32 bytes):
> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
> backtrace:
> [<ffffffff81554bbb>] __do_kmalloc_node mm/slab_common.c:984
> [inline]
> [<ffffffff81554bbb>] __kmalloc_node+0x4b/0x150 mm/slab_common.c:992
> [<ffffffff815440f9>] kmalloc_node include/linux/slab.h:602 [inline]
> [<ffffffff815440f9>] kvmalloc_node+0x99/0x170 mm/util.c:604
> [<ffffffff824c52fe>] kvmalloc include/linux/slab.h:720 [inline]
> [<ffffffff824c52fe>] kvmalloc_array include/linux/slab.h:738
> [inline]
> [<ffffffff824c52fe>] want_pages_array lib/iov_iter.c:985 [inline]
> [<ffffffff824c52fe>] iov_iter_extract_user_pages
> lib/iov_iter.c:1765 [inline]
> [<ffffffff824c52fe>] iov_iter_extract_pages+0x1ee/0xa40
> lib/iov_iter.c:1831
> [<ffffffff824125a7>] bio_map_user_iov+0x167/0x5d0
> block/blk-map.c:297
> [<ffffffff82412df3>] blk_rq_map_user_iov+0x3e3/0xb30
> block/blk-map.c:664
> [<ffffffff82413943>] blk_rq_map_user block/blk-map.c:691 [inline]
> [<ffffffff82413943>] blk_rq_map_user_io+0x143/0x160
> block/blk-map.c:724
> [<ffffffff82ca0925>] sg_io+0x285/0x510
> drivers/scsi/scsi_ioctl.c:456
> [<ffffffff82ca1025>] scsi_cdrom_send_packet+0x1b5/0x480
> drivers/scsi/scsi_ioctl.c:820
> [<ffffffff82ca13ba>] scsi_ioctl+0xca/0xd30
> drivers/scsi/scsi_ioctl.c:903
> [<ffffffff82d35964>] sg_ioctl+0x5f4/0x10a0 drivers/scsi/sg.c:1163
> [<ffffffff8168e602>] vfs_ioctl fs/ioctl.c:51 [inline]
> [<ffffffff8168e602>] __do_sys_ioctl fs/ioctl.c:870 [inline]
> [<ffffffff8168e602>] __se_sys_ioctl fs/ioctl.c:856 [inline]
> [<ffffffff8168e602>] __x64_sys_ioctl+0xf2/0x140 fs/ioctl.c:856
> [<ffffffff84ad2bb8>] do_syscall_x64 arch/x86/entry/common.c:50
> [inline]
> [<ffffffff84ad2bb8>] do_syscall_64+0x38/0xb0
> arch/x86/entry/common.c:80
> [<ffffffff84c0008b>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
>
> Reported-by: [email protected]
> Closes:
> https://syzkaller.appspot.com/bug?id=99c8551967f413d108cfdd2950a0cb5652de07b8
> Fixes: 7d58fe7310281 ("iov_iter: Add a function to extract a page list
> from an iterator")
> Signed-off-by: José Pekkarinen <[email protected]>
> ---
> lib/iov_iter.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/lib/iov_iter.c b/lib/iov_iter.c
> index 27234a820eeb..c3fd0448dead 100644
> --- a/lib/iov_iter.c
> +++ b/lib/iov_iter.c
> @@ -1780,8 +1780,10 @@ static ssize_t
> iov_iter_extract_user_pages(struct iov_iter *i,
> if (!maxpages)
> return -ENOMEM;
> res = pin_user_pages_fast(addr, maxpages, gup_flags, *pages);
> - if (unlikely(res <= 0))
> + if (unlikely(res <= 0)) {
> + kvfree(*pages);
> return res;
> + }
> maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - offset);
> iov_iter_advance(i, maxsize);
> return maxsize;
Ping.
José.