Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3553255pxb; Mon, 24 Jan 2022 12:04:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJz1hX4E5rXHbi+DgnfxpGlNx/DNBlvjD2lC/NSlKxmKirs1S45/bjVDgZC89CGs8AUfC3Dg X-Received: by 2002:a17:902:da8a:b0:14b:2e7b:b557 with SMTP id j10-20020a170902da8a00b0014b2e7bb557mr11327363plx.72.1643054684230; Mon, 24 Jan 2022 12:04:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643054684; cv=none; d=google.com; s=arc-20160816; b=YAErrmEYHcHFW5YXypjxcfG5k7XIh0PRJ2VFs6vkzEfbImfvuu5F5Oe9wbb3EKh6ao vE48kzjtzOrb8F51mPkzRxhGRd9Con3rRRY+HrodE88vVTlwuMzLYQuwEITKYHWQy+Ft P07tY2mmrS/iWHKWwBreBhoha0RfI/7Ddp+Lbp3jHBV38T6kPjCyK595e5Vpd5gFy4Hf EipAGgmyLmA9kcFgSkX1YYuj/XhPvvtLKGcY7pW+KBWkaEgeeQxx2pIjfiXFFnAi9lJa WGFS4JK5gWr0GgL7BgU7u+cYYlXR3NCJDVuc9RJskRHRqYOpu2DSRap8wbDT8IH+4vbQ ASWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ZXj9rrb4A+s84P2n5AhyOq2k6MsqbqJMy++DQQMH72E=; b=0Q5J8n45mN6Xk6FL+nQJDRGVY3/fVNtyLLp1HkNDgf0wMSVB1/BvxNkRwT+hSUT3+Q jhGJv4iv9kh/PD5SYsLAVjqA9/Bm/WtzZQrt6HNr8SOUlLLQap3tnPtynEhEeWEQgbcl 0GvM2l8gBP5kZRRYUNME1AnpVcTFpQsAy824zVaF06Be3iAK4Rb3Pk0XwH64VTdy9S58 zAUKmXktUgm+HEO+oBLux7ylwjEMekS185Z+bCtdcuDyn1oILbzQC5phj9F09N11Sqht ZZHS1APhbfJJOiHtJ+xre3+N1rYCgXhlyfw3L5P8P9j0v47ZSSDZnm8Vr4baK1hXX4rH K3PA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KNMaJFkw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u8si13771216pgm.303.2022.01.24.12.04.29; Mon, 24 Jan 2022 12:04:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KNMaJFkw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345806AbiAXTEi (ORCPT + 99 others); Mon, 24 Jan 2022 14:04:38 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:58834 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345314AbiAXTAB (ORCPT ); Mon, 24 Jan 2022 14:00:01 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 71A7C6090A; Mon, 24 Jan 2022 19:00:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E46AC340E5; Mon, 24 Jan 2022 19:00:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643050800; bh=FVnpSvec1j/MlZ+c/SLq9KK4B+zNtg27zPySVYQJosI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KNMaJFkw3ywS5u/e/u9bUiOvgXnHfCujfTXxLiBvfpON50X93gv79w9TKLok19vuL Dn+VpLxEUtqsOnyQM1ZkNWgugsujiYt7hKCbriaWabZJjBvI9qFi2E+mIc2RjlMRAN Ysk+5AHr6fZHjXyk8JsGt3ML2IZerp9gv80v4bvE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+23a02c7df2cf2bc93fa2@syzkaller.appspotmail.com, Xiongwei Song , Denis Efremov , Jens Axboe , Sasha Levin Subject: [PATCH 4.9 083/157] floppy: Add max size check for user space request Date: Mon, 24 Jan 2022 19:42:53 +0100 Message-Id: <20220124183935.415556544@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183932.787526760@linuxfoundation.org> References: <20220124183932.787526760@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Xiongwei Song [ Upstream commit 545a32498c536ee152331cd2e7d2416aa0f20e01 ] We need to check the max request size that is from user space before allocating pages. If the request size exceeds the limit, return -EINVAL. This check can avoid the warning below from page allocator. WARNING: CPU: 3 PID: 16525 at mm/page_alloc.c:5344 current_gfp_context include/linux/sched/mm.h:195 [inline] WARNING: CPU: 3 PID: 16525 at mm/page_alloc.c:5344 __alloc_pages+0x45d/0x500 mm/page_alloc.c:5356 Modules linked in: CPU: 3 PID: 16525 Comm: syz-executor.3 Not tainted 5.15.0-syzkaller #0 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.14.0-2 04/01/2014 RIP: 0010:__alloc_pages+0x45d/0x500 mm/page_alloc.c:5344 Code: be c9 00 00 00 48 c7 c7 20 4a 97 89 c6 05 62 32 a7 0b 01 e8 74 9a 42 07 e9 6a ff ff ff 0f 0b e9 a0 fd ff ff 40 80 e5 3f eb 88 <0f> 0b e9 18 ff ff ff 4c 89 ef 44 89 e6 45 31 ed e8 1e 76 ff ff e9 RSP: 0018:ffffc90023b87850 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 1ffff92004770f0b RCX: dffffc0000000000 RDX: 0000000000000000 RSI: 0000000000000033 RDI: 0000000000010cc1 RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000001 R10: ffffffff81bb4686 R11: 0000000000000001 R12: ffffffff902c1960 R13: 0000000000000033 R14: 0000000000000000 R15: ffff88804cf64a30 FS: 0000000000000000(0000) GS:ffff88802cd00000(0063) knlGS:00000000f44b4b40 CS: 0010 DS: 002b ES: 002b CR0: 0000000080050033 CR2: 000000002c921000 CR3: 000000004f507000 CR4: 0000000000150ee0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: alloc_pages+0x1a7/0x300 mm/mempolicy.c:2191 __get_free_pages+0x8/0x40 mm/page_alloc.c:5418 raw_cmd_copyin drivers/block/floppy.c:3113 [inline] raw_cmd_ioctl drivers/block/floppy.c:3160 [inline] fd_locked_ioctl+0x12e5/0x2820 drivers/block/floppy.c:3528 fd_ioctl drivers/block/floppy.c:3555 [inline] fd_compat_ioctl+0x891/0x1b60 drivers/block/floppy.c:3869 compat_blkdev_ioctl+0x3b8/0x810 block/ioctl.c:662 __do_compat_sys_ioctl+0x1c7/0x290 fs/ioctl.c:972 do_syscall_32_irqs_on arch/x86/entry/common.c:112 [inline] __do_fast_syscall_32+0x65/0xf0 arch/x86/entry/common.c:178 do_fast_syscall_32+0x2f/0x70 arch/x86/entry/common.c:203 entry_SYSENTER_compat_after_hwframe+0x4d/0x5c Reported-by: syzbot+23a02c7df2cf2bc93fa2@syzkaller.appspotmail.com Link: https://lore.kernel.org/r/20211116131033.27685-1-sxwjean@me.com Signed-off-by: Xiongwei Song Signed-off-by: Denis Efremov Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/block/floppy.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c index d508dda49dee4..7164be9710e51 100644 --- a/drivers/block/floppy.c +++ b/drivers/block/floppy.c @@ -3116,6 +3116,8 @@ static void raw_cmd_free(struct floppy_raw_cmd **ptr) } } +#define MAX_LEN (1UL << MAX_ORDER << PAGE_SHIFT) + static int raw_cmd_copyin(int cmd, void __user *param, struct floppy_raw_cmd **rcmd) { @@ -3153,7 +3155,7 @@ loop: ptr->resultcode = 0; if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) { - if (ptr->length <= 0) + if (ptr->length <= 0 || ptr->length >= MAX_LEN) return -EINVAL; ptr->kernel_data = (char *)fd_dma_mem_alloc(ptr->length); fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length); -- 2.34.1