Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3386131pxb; Mon, 17 Jan 2022 19:17:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJxbVlczLUkvHmd4zba0ChFEO3WGiR6/pigoNi/AtJUY2JE4KAHCVfdvRURFSp5MbWAHGLQc X-Received: by 2002:a63:90c2:: with SMTP id a185mr13612101pge.211.1642475849828; Mon, 17 Jan 2022 19:17:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642475849; cv=none; d=google.com; s=arc-20160816; b=i4tnH/anS9FTKBXutxCoMp9aL+BoSlyLnZ0VNmSyW1Q3g29GUsZiZ10HnKSbWstcxL EPLtAhofpVB1yB38uHeDa5yO8TWy06lHEVvihW5oi7Tcb6rd+hk4ImqWnLZOiLwOzs/N EgIsctfuL/Zm1LZOWtaipYthFTFteS7gqQnHPSHqPzWKdHiAyoiLKrraW35Ms9eyv0vO 6TG7+JB/la+kNrnO1wwWuAPvXEJZ77SMbn0ZRjPmNDIRLNMTGmsffj/3OaoAa0qMQ+RU Y2VdHAD6SP0xFtPkZGSMpB8nnQtDJ1o/65ZdcHJzDieULMrkHTV7EDobJDmtDw8IBrg0 5mVQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=EH5aJe6384a60YgPRVIAlSzVJGLCl2ilFV/q762ICNU=; b=HovYHOnXpUej863Khagp6KJoodqh/ODp9HG4a7pH+xBju83SdiSZTRnoriX9vbs3rM Bl5zDQwx/qUidIOuFmwCJJKTwRA8GbG7tDQlbmV9v/HVSzJr56hYGcf29ChpHrTApDiS 17ILUoAe92CbazbQu5unwspU0mcP3rBVzjy3IAWNyLnVGr4Q+0xrhWFskTiZkjtkw8q0 /ljRuaaFkmwo/t035M0nZRh8FHyPKNhZAnZqDxUP6w04X1/FGm/po5G8rcdYT64c9BMQ hPYI07BLvj5X8f3xFkEzWDKjIFh28sgwEJw+0b+1oF+46feN0g87MdkHr7+inPQXKJjz fNcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fEyCgYoB; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e16si17101952pfv.41.2022.01.17.19.17.14; Mon, 17 Jan 2022 19:17:29 -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=@kernel.org header.s=k20201202 header.b=fEyCgYoB; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244963AbiARC1g (ORCPT + 99 others); Mon, 17 Jan 2022 21:27:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245699AbiARCZR (ORCPT ); Mon, 17 Jan 2022 21:25:17 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AE0FC0613AE; Mon, 17 Jan 2022 18:24:28 -0800 (PST) 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 9E1F66110F; Tue, 18 Jan 2022 02:24:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C6AAC36AF5; Tue, 18 Jan 2022 02:24:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642472667; bh=ebvfpE83+PqI3ZaZWF3SrL2qIONeao6SDSIY8ligs8Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fEyCgYoBwZ2qcB63M8Rdbdt0p7TsSmkc0SjyDbIxyajnA3SZRt19r02CTO+izKNdI Qj76cD7m27WYCDWcd0j5Cy5t5DC47nyDXj4C57+vd21xTsVhYOuIuj/7O0+UKsie+T Bu41MRk+aqP/zSwFWr3DlT1crYXSg8iPJzWlxcb22xJmkRFAoZpBLOouLyZjGgJssz /8sRI1RJlSqVp+qYn+z1G1xuxhpsk1NV091dYaCgAc5k1JgcQEl5xI4GQ1RtojwFIN uEtftfdfFQZux1uPBnra/o8uaxB8+eWICilp83F1P+0a594zQCe2yDmg+ptkin9yZq wsJcKZANTmC2Q== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Xiongwei Song , syzbot+23a02c7df2cf2bc93fa2@syzkaller.appspotmail.com, Denis Efremov , Jens Axboe , Sasha Levin , linux-block@vger.kernel.org Subject: [PATCH AUTOSEL 5.16 093/217] floppy: Add max size check for user space request Date: Mon, 17 Jan 2022 21:17:36 -0500 Message-Id: <20220118021940.1942199-93-sashal@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220118021940.1942199-1-sashal@kernel.org> References: <20220118021940.1942199-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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 c4267da716fe6..52112ed59dd02 100644 --- a/drivers/block/floppy.c +++ b/drivers/block/floppy.c @@ -3081,6 +3081,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) { @@ -3108,7 +3110,7 @@ static int raw_cmd_copyin(int cmd, void __user *param, 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