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 47CFDC6379F for ; Sat, 18 Feb 2023 14:24:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229672AbjBROYV (ORCPT ); Sat, 18 Feb 2023 09:24:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229551AbjBROYU (ORCPT ); Sat, 18 Feb 2023 09:24:20 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6511B17CC3 for ; Sat, 18 Feb 2023 06:24:19 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id h10-20020a17090aa88a00b002349a303ca5so706109pjq.4 for ; Sat, 18 Feb 2023 06:24:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DssyoUE9bofDz+38OOssTs8uLU6fnH3aGJYNSo2UCao=; b=HcGFApiCIbm6Tu62yjG/5EiRp37h1/qPuygIVKkvusEyVIdvWTtJGGKq3Wpd/l5Csf 0BWtmisy8JdX+iUSmYEFx3ILpy47Zd+gRQ3rQhmWSkkR1atkISVySyhDW3kF2js2OOOi bGs2sE75Agz6vmAHEuyyXqQaHcD/TP2JD0bC35jzxFbLlEH73ximMrid6Wep/4nUNQA0 TUSszuU+h2JVTo8O7ZvdkgB4P4n+DpvJwx0vVEXIO5mm8jurQc8b5tsZ9RiL4wCBbqpY 1j6vDFnBTT53POr0w12+awJAAJxVVu4VeLkaYERWkZqqeHdPkpSD+9LyxlsaFYvHfGAo PRQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DssyoUE9bofDz+38OOssTs8uLU6fnH3aGJYNSo2UCao=; b=XmTUbo3a15WLxy0//5OkEDmgqviu11nN6cv/VA27FRsSJv+8Fp4FhAqbYhRDN5xZCB H4T7IKXPmQe2Ljmb1acCtwxiSS/HCu1qXETSwhP/b5XqglYH2X0W+o0p93eYb3I7hpOT AIAdg+StjomHrF/xuEtLEzgMzwudN8CuyCmzb1b4QGxoa4dIkmr+2FslOPlXpHuBCOjl G9SlBYl9ISOZrnNqoIJ4iHWv31e/QJDWgxX/8mjSRX9JWL/QISjpzUuDBt2ciKUvAOAT Hmgs7XTt50dghYlum4baIwzinvDCBx5OjN9PZPnxbiXNa2UUZLKygxzXpBv4O96M4XD6 yqiA== X-Gm-Message-State: AO0yUKWLlmaHhIElJk/2DFVk46t5BRmUlNKLprppCrfIvMvG7gKnk3VL Vitm7Q7InEg4FT44w+krhB4= X-Google-Smtp-Source: AK7set9P0I0b/RWAvLvy7M5VjU0hIxjmq42Ak+yFMDG82HuTYry1tCyvEJnGoUZxJ7yZiGgYTehLIQ== X-Received: by 2002:a17:902:f688:b0:19a:c4a0:5b1b with SMTP id l8-20020a170902f68800b0019ac4a05b1bmr536116plg.1.1676730258593; Sat, 18 Feb 2023 06:24:18 -0800 (PST) Received: from carrot.. (i121-118-78-205.s42.a014.ap.plala.or.jp. [121.118.78.205]) by smtp.gmail.com with ESMTPSA id 13-20020a170902ee4d00b001965f761e6dsm4764668plo.182.2023.02.18.06.24.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 06:24:17 -0800 (PST) From: Ryusuke Konishi To: Greg Kroah-Hartman , stable@vger.kernel.org Cc: Andrew Morton , syzbot+f0c4082ce5ebebdac63b@syzkaller.appspotmail.com Subject: [PATCH 5.10 5.15] nilfs2: fix underflow in second superblock position calculations Date: Sat, 18 Feb 2023 23:23:50 +0900 Message-Id: <20230218142350.2548-2-konishi.ryusuke@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230218142350.2548-1-konishi.ryusuke@gmail.com> References: <16767073279675@kroah.com> <20230218142350.2548-1-konishi.ryusuke@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org commit 99b9402a36f0799f25feee4465bfa4b8dfa74b4d upstream. Macro NILFS_SB2_OFFSET_BYTES, which computes the position of the second superblock, underflows when the argument device size is less than 4096 bytes. Therefore, when using this macro, it is necessary to check in advance that the device size is not less than a lower limit, or at least that underflow does not occur. The current nilfs2 implementation lacks this check, causing out-of-bound block access when mounting devices smaller than 4096 bytes: I/O error, dev loop0, sector 36028797018963960 op 0x0:(READ) flags 0x0 phys_seg 1 prio class 2 NILFS (loop0): unable to read secondary superblock (blocksize = 1024) In addition, when trying to resize the filesystem to a size below 4096 bytes, this underflow occurs in nilfs_resize_fs(), passing a huge number of segments to nilfs_sufile_resize(), corrupting parameters such as the number of segments in superblocks. This causes excessive loop iterations in nilfs_sufile_resize() during a subsequent resize ioctl, causing semaphore ns_segctor_sem to block for a long time and hang the writer thread: INFO: task segctord:5067 blocked for more than 143 seconds. Not tainted 6.2.0-rc8-syzkaller-00015-gf6feea56f66d #0 "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. task:segctord state:D stack:23456 pid:5067 ppid:2 flags:0x00004000 Call Trace: context_switch kernel/sched/core.c:5293 [inline] __schedule+0x1409/0x43f0 kernel/sched/core.c:6606 schedule+0xc3/0x190 kernel/sched/core.c:6682 rwsem_down_write_slowpath+0xfcf/0x14a0 kernel/locking/rwsem.c:1190 nilfs_transaction_lock+0x25c/0x4f0 fs/nilfs2/segment.c:357 nilfs_segctor_thread_construct fs/nilfs2/segment.c:2486 [inline] nilfs_segctor_thread+0x52f/0x1140 fs/nilfs2/segment.c:2570 kthread+0x270/0x300 kernel/kthread.c:376 ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:308 ... Call Trace: folio_mark_accessed+0x51c/0xf00 mm/swap.c:515 __nilfs_get_page_block fs/nilfs2/page.c:42 [inline] nilfs_grab_buffer+0x3d3/0x540 fs/nilfs2/page.c:61 nilfs_mdt_submit_block+0xd7/0x8f0 fs/nilfs2/mdt.c:121 nilfs_mdt_read_block+0xeb/0x430 fs/nilfs2/mdt.c:176 nilfs_mdt_get_block+0x12d/0xbb0 fs/nilfs2/mdt.c:251 nilfs_sufile_get_segment_usage_block fs/nilfs2/sufile.c:92 [inline] nilfs_sufile_truncate_range fs/nilfs2/sufile.c:679 [inline] nilfs_sufile_resize+0x7a3/0x12b0 fs/nilfs2/sufile.c:777 nilfs_resize_fs+0x20c/0xed0 fs/nilfs2/super.c:422 nilfs_ioctl_resize fs/nilfs2/ioctl.c:1033 [inline] nilfs_ioctl+0x137c/0x2440 fs/nilfs2/ioctl.c:1301 ... This fixes these issues by inserting appropriate minimum device size checks or anti-underflow checks, depending on where the macro is used. Link: https://lkml.kernel.org/r/0000000000004e1dfa05f4a48e6b@google.com Link: https://lkml.kernel.org/r/20230214224043.24141-1-konishi.ryusuke@gmail.com Signed-off-by: Ryusuke Konishi Reported-by: Tested-by: Ryusuke Konishi Cc: Signed-off-by: Andrew Morton --- Please apply this patch to the above stable trees instead of the patch that could not be applied to them last time. This replaces bdev_nr_bytes() with its equivalent reference since it does not yet exist in these kernels. With this tweak, this patch is applicable from v5.9 to v5.15. Also, this patch has been tested against the title stable trees. fs/nilfs2/ioctl.c | 7 +++++++ fs/nilfs2/super.c | 9 +++++++++ fs/nilfs2/the_nilfs.c | 8 +++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/fs/nilfs2/ioctl.c b/fs/nilfs2/ioctl.c index 640ac8fe891e..a8509f364bf5 100644 --- a/fs/nilfs2/ioctl.c +++ b/fs/nilfs2/ioctl.c @@ -1114,7 +1114,14 @@ static int nilfs_ioctl_set_alloc_range(struct inode *inode, void __user *argp) minseg = range[0] + segbytes - 1; do_div(minseg, segbytes); + + if (range[1] < 4096) + goto out; + maxseg = NILFS_SB2_OFFSET_BYTES(range[1]); + if (maxseg < segbytes) + goto out; + do_div(maxseg, segbytes); maxseg--; diff --git a/fs/nilfs2/super.c b/fs/nilfs2/super.c index 663e68c22db8..bc9690026225 100644 --- a/fs/nilfs2/super.c +++ b/fs/nilfs2/super.c @@ -408,6 +408,15 @@ int nilfs_resize_fs(struct super_block *sb, __u64 newsize) if (newsize > devsize) goto out; + /* + * Prevent underflow in second superblock position calculation. + * The exact minimum size check is done in nilfs_sufile_resize(). + */ + if (newsize < 4096) { + ret = -ENOSPC; + goto out; + } + /* * Write lock is required to protect some functions depending * on the number of segments, the number of reserved segments, diff --git a/fs/nilfs2/the_nilfs.c b/fs/nilfs2/the_nilfs.c index 6aa6cef0757f..1068ff40077c 100644 --- a/fs/nilfs2/the_nilfs.c +++ b/fs/nilfs2/the_nilfs.c @@ -544,9 +544,15 @@ static int nilfs_load_super_block(struct the_nilfs *nilfs, { struct nilfs_super_block **sbp = nilfs->ns_sbp; struct buffer_head **sbh = nilfs->ns_sbh; - u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size); + u64 sb2off, devsize = nilfs->ns_bdev->bd_inode->i_size; int valid[2], swp = 0; + if (devsize < NILFS_SEG_MIN_BLOCKS * NILFS_MIN_BLOCK_SIZE + 4096) { + nilfs_err(sb, "device size too small"); + return -EINVAL; + } + sb2off = NILFS_SB2_OFFSET_BYTES(devsize); + sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize, &sbh[0]); sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]); -- 2.31.1