From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 469DEC2BB3F for ; Mon, 20 Nov 2023 15:37:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234211AbjKTPhP (ORCPT ); Mon, 20 Nov 2023 10:37:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231352AbjKTPhN (ORCPT ); Mon, 20 Nov 2023 10:37:13 -0500 Received: from 4.mo560.mail-out.ovh.net (4.mo560.mail-out.ovh.net [87.98.172.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D0E8A7 for ; Mon, 20 Nov 2023 07:37:08 -0800 (PST) Received: from director5.ghost.mail-out.ovh.net (unknown [10.109.156.34]) by mo560.mail-out.ovh.net (Postfix) with ESMTP id 8FA3425E44 for ; Mon, 20 Nov 2023 15:31:55 +0000 (UTC) Received: from ghost-submission-6684bf9d7b-lljcm (unknown [10.110.208.139]) by director5.ghost.mail-out.ovh.net (Postfix) with ESMTPS id CE6831FEBC; Mon, 20 Nov 2023 15:31:54 +0000 (UTC) Received: from RCM-web2.webmail.mail.ovh.net ([176.31.232.109]) by ghost-submission-6684bf9d7b-lljcm with ESMTPSA id Gli7Lup7W2WsAgAAgGBV+g (envelope-from ); Mon, 20 Nov 2023 15:31:54 +0000 MIME-Version: 1.0 Date: Mon, 20 Nov 2023 17:31:53 +0200 From: =?UTF-8?Q?Jos=C3=A9_Pekkarinen?= To: viro@zeniv.linux.org.uk, skhan@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, linux-kernel-mentees@lists.linux.dev, syzbot+cb729843d0f42a5c1a50@syzkaller.appspotmail.com Subject: Re: [PATCH] iov_iter: fix memleak in iov_iter_extract_pages In-Reply-To: <20231111075323.208181-1-jose.pekkarinen@foxhound.fi> References: <20231111075323.208181-1-jose.pekkarinen@foxhound.fi> User-Agent: Roundcube Webmail/1.4.15 Message-ID: <8ab69336548dfe4f556d7be83d90afeb@foxhound.fi> X-Sender: jose.pekkarinen@foxhound.fi Organization: Foxhound Ltd. X-Originating-IP: 192.42.116.182 X-Webmail-UserID: jose.pekkarinen@foxhound.fi Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Ovh-Tracer-Id: 8929230686578320904 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: 0 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedvkedrudegjedghedtucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecuhedttdenucenucfjughrpeggfffhvfevufgjfhgfkfigohhitgfgsehtkehjtddtreejnecuhfhrohhmpeflohhsrocurfgvkhhkrghrihhnvghnuceojhhoshgvrdhpvghkkhgrrhhinhgvnhesfhhogihhohhunhgurdhfiheqnecuggftrfgrthhtvghrnhepvdffhfegtdeuiefgkefhgeefheektdekffeufeehgeeuueehjeeijeelleetjeeinecuffhomhgrihhnpehshiiikhgrlhhlvghrrdgrphhpshhpohhtrdgtohhmnecukfhppeduvdejrddtrddtrddupdduledvrdegvddrudduiedrudekvddpudejiedrfedurddvfedvrddutdelnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepuddvjedrtddrtddruddpmhgrihhlfhhrohhmpeeojhhoshgvrdhpvghkkhgrrhhinhgvnhesfhhogihhohhunhgurdhfiheqpdhnsggprhgtphhtthhopedupdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdfovfetjfhoshhtpehmohehiedtpdhmohguvgepshhmthhpohhuth Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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: > [] __do_kmalloc_node mm/slab_common.c:984 > [inline] > [] __kmalloc_node+0x4b/0x150 mm/slab_common.c:992 > [] kmalloc_node include/linux/slab.h:602 [inline] > [] kvmalloc_node+0x99/0x170 mm/util.c:604 > [] kvmalloc include/linux/slab.h:720 [inline] > [] kvmalloc_array include/linux/slab.h:738 > [inline] > [] want_pages_array lib/iov_iter.c:985 [inline] > [] iov_iter_extract_user_pages > lib/iov_iter.c:1765 [inline] > [] iov_iter_extract_pages+0x1ee/0xa40 > lib/iov_iter.c:1831 > [] bio_map_user_iov+0x167/0x5d0 > block/blk-map.c:297 > [] blk_rq_map_user_iov+0x3e3/0xb30 > block/blk-map.c:664 > [] blk_rq_map_user block/blk-map.c:691 [inline] > [] blk_rq_map_user_io+0x143/0x160 > block/blk-map.c:724 > [] sg_io+0x285/0x510 > drivers/scsi/scsi_ioctl.c:456 > [] scsi_cdrom_send_packet+0x1b5/0x480 > drivers/scsi/scsi_ioctl.c:820 > [] scsi_ioctl+0xca/0xd30 > drivers/scsi/scsi_ioctl.c:903 > [] sg_ioctl+0x5f4/0x10a0 drivers/scsi/sg.c:1163 > [] vfs_ioctl fs/ioctl.c:51 [inline] > [] __do_sys_ioctl fs/ioctl.c:870 [inline] > [] __se_sys_ioctl fs/ioctl.c:856 [inline] > [] __x64_sys_ioctl+0xf2/0x140 fs/ioctl.c:856 > [] do_syscall_x64 arch/x86/entry/common.c:50 > [inline] > [] do_syscall_64+0x38/0xb0 > arch/x86/entry/common.c:80 > [] 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: > [] __do_kmalloc_node mm/slab_common.c:984 > [inline] > [] __kmalloc_node+0x4b/0x150 mm/slab_common.c:992 > [] kmalloc_node include/linux/slab.h:602 [inline] > [] kvmalloc_node+0x99/0x170 mm/util.c:604 > [] kvmalloc include/linux/slab.h:720 [inline] > [] kvmalloc_array include/linux/slab.h:738 > [inline] > [] want_pages_array lib/iov_iter.c:985 [inline] > [] iov_iter_extract_user_pages > lib/iov_iter.c:1765 [inline] > [] iov_iter_extract_pages+0x1ee/0xa40 > lib/iov_iter.c:1831 > [] bio_map_user_iov+0x167/0x5d0 > block/blk-map.c:297 > [] blk_rq_map_user_iov+0x3e3/0xb30 > block/blk-map.c:664 > [] blk_rq_map_user block/blk-map.c:691 [inline] > [] blk_rq_map_user_io+0x143/0x160 > block/blk-map.c:724 > [] sg_io+0x285/0x510 > drivers/scsi/scsi_ioctl.c:456 > [] scsi_cdrom_send_packet+0x1b5/0x480 > drivers/scsi/scsi_ioctl.c:820 > [] scsi_ioctl+0xca/0xd30 > drivers/scsi/scsi_ioctl.c:903 > [] sg_ioctl+0x5f4/0x10a0 drivers/scsi/sg.c:1163 > [] vfs_ioctl fs/ioctl.c:51 [inline] > [] __do_sys_ioctl fs/ioctl.c:870 [inline] > [] __se_sys_ioctl fs/ioctl.c:856 [inline] > [] __x64_sys_ioctl+0xf2/0x140 fs/ioctl.c:856 > [] do_syscall_x64 arch/x86/entry/common.c:50 > [inline] > [] do_syscall_64+0x38/0xb0 > arch/x86/entry/common.c:80 > [] 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: > [] __do_kmalloc_node mm/slab_common.c:984 > [inline] > [] __kmalloc_node+0x4b/0x150 mm/slab_common.c:992 > [] kmalloc_node include/linux/slab.h:602 [inline] > [] kvmalloc_node+0x99/0x170 mm/util.c:604 > [] kvmalloc include/linux/slab.h:720 [inline] > [] kvmalloc_array include/linux/slab.h:738 > [inline] > [] want_pages_array lib/iov_iter.c:985 [inline] > [] iov_iter_extract_user_pages > lib/iov_iter.c:1765 [inline] > [] iov_iter_extract_pages+0x1ee/0xa40 > lib/iov_iter.c:1831 > [] bio_map_user_iov+0x167/0x5d0 > block/blk-map.c:297 > [] blk_rq_map_user_iov+0x3e3/0xb30 > block/blk-map.c:664 > [] blk_rq_map_user block/blk-map.c:691 [inline] > [] blk_rq_map_user_io+0x143/0x160 > block/blk-map.c:724 > [] sg_io+0x285/0x510 > drivers/scsi/scsi_ioctl.c:456 > [] scsi_cdrom_send_packet+0x1b5/0x480 > drivers/scsi/scsi_ioctl.c:820 > [] scsi_ioctl+0xca/0xd30 > drivers/scsi/scsi_ioctl.c:903 > [] sg_ioctl+0x5f4/0x10a0 drivers/scsi/sg.c:1163 > [] vfs_ioctl fs/ioctl.c:51 [inline] > [] __do_sys_ioctl fs/ioctl.c:870 [inline] > [] __se_sys_ioctl fs/ioctl.c:856 [inline] > [] __x64_sys_ioctl+0xf2/0x140 fs/ioctl.c:856 > [] do_syscall_x64 arch/x86/entry/common.c:50 > [inline] > [] do_syscall_64+0x38/0xb0 > arch/x86/entry/common.c:80 > [] entry_SYSCALL_64_after_hwframe+0x63/0xcd > > Reported-by: syzbot+cb729843d0f42a5c1a50@syzkaller.appspotmail.com > 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 > --- > 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é.