From patchwork Tue Aug 1 22:08:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuxuan Luo X-Patchwork-Id: 1815666 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=FBDLWnaW; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RFq4c2gtxz1ydw for ; Wed, 2 Aug 2023 08:08:31 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1qQxXR-000617-EP; Tue, 01 Aug 2023 22:08:21 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1qQxXP-00060f-AT for kernel-team@lists.ubuntu.com; Tue, 01 Aug 2023 22:08:19 +0000 Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 873293F205 for ; Tue, 1 Aug 2023 22:08:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1690927698; bh=1aljZs0x9NNMuISJvylBe86VyksPY76kQJpXmQNknvk=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FBDLWnaWcGU9TEbOC/+0PNrKcR3dUAo3RwUunnORrDBaLOWVVZ9n6y7TzQF0TJ+lo Tw+TV1k3TDY8ScBM63P81574z8YDvGnte+O5fP+R7E+bWhw/H5KR9veuEj6s/smTHp 6KoRBNHv7Yp1ltB1EHUlzgpjCNBBvBS1PWnqulQWTAcpmbukPKRJS1SvieZ7rRybmP jNBXtgoRtP7uVpmGBqNAtwq4lQ9Z7N8QCQzVPp1Po2jh7ehvZHPAvfCM8jIo+aXIc5 i5r6oX04flA1pUFSwQhXfbS9m6amSFzifboVNmOwvOn7ioObINYEPGge+s0y3xTfcA k4cWk8UdhRylw== Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-6bc7b12ee26so10061643a34.0 for ; Tue, 01 Aug 2023 15:08:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690927697; x=1691532497; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1aljZs0x9NNMuISJvylBe86VyksPY76kQJpXmQNknvk=; b=UdfykGoz7Ut7/NxybJoWah+P2V7KHceq4QCak5/LUq3kkJUenQl8jO1RSdqDreljfq be8VRKggV0rkSosn/HVdn6UI6lSrmBXtNI7SAvMFa01fDWRi8QTWddXHq5hvuXRidSOC p6szHghRrUxqDNZ2UmDFZEMaY1P4O1x/HI8YRq4019iSozOZpp5Wi8vOW1LyCSpuZ4M4 rVeFnhJG6nmgVEsiV7/QbfvJzSbK9I4lgQ68m0WFNJiYuSbgGgYKQA8eA2G7zYLcDIxi QEv4mhDFzfNJ5eMu/bEVPHuL8wy9myEzNgwIWlqDDI7wpnX2EnRZGEiWc3UnmtqD1BaT R4Xg== X-Gm-Message-State: ABy/qLZR91XXeLgfbjfrAo4V2KWNHeSolnkwVB707V58t9gI1ypuNhFd 1bLc29b95iEJYEIhhJC1IxV0ra98m1T/KcggWVXU4m/lDpRzuN5P4PRoeZFYE+Foq/QRX7d5XHn SbTCcYL42IzyN4ifjrWSUPJFBIbxqemg+/bQHv5S3AEQOFHI7Y+AU X-Received: by 2002:a9d:748c:0:b0:6b7:30cc:6419 with SMTP id t12-20020a9d748c000000b006b730cc6419mr14954770otk.1.1690927696988; Tue, 01 Aug 2023 15:08:16 -0700 (PDT) X-Google-Smtp-Source: APBJJlEH2a2fhWtVixubfGeGg5ia5xMwa5oSAp2QOUVsRPi8ueBULzc4PdU3ByYMGz0lVDnYmgzMng== X-Received: by 2002:a9d:748c:0:b0:6b7:30cc:6419 with SMTP id t12-20020a9d748c000000b006b730cc6419mr14954756otk.1.1690927696679; Tue, 01 Aug 2023 15:08:16 -0700 (PDT) Received: from cache-ubuntu.hsd1.nj.comcast.net ([2001:67c:1562:8007::aac:4795]) by smtp.gmail.com with ESMTPSA id d7-20020a056830138700b006b89dafb721sm5391964otq.78.2023.08.01.15.08.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 15:08:16 -0700 (PDT) From: Yuxuan Luo To: kernel-team@lists.ubuntu.com Subject: [SRU][Lunar][PATCH 1/1] f2fs: fix to avoid NULL pointer dereference f2fs_write_end_io() Date: Tue, 1 Aug 2023 18:08:11 -0400 Message-Id: <20230801220812.127507-2-yuxuan.luo@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230801220812.127507-1-yuxuan.luo@canonical.com> References: <20230801220812.127507-1-yuxuan.luo@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Chao Yu butt3rflyh4ck reports a bug as below: When a thread always calls F2FS_IOC_RESIZE_FS to resize fs, if resize fs is failed, f2fs kernel thread would invoke callback function to update f2fs io info, it would call f2fs_write_end_io and may trigger null-ptr-deref in NODE_MAPPING. general protection fault, probably for non-canonical address KASAN: null-ptr-deref in range [0x0000000000000030-0x0000000000000037] RIP: 0010:NODE_MAPPING fs/f2fs/f2fs.h:1972 [inline] RIP: 0010:f2fs_write_end_io+0x727/0x1050 fs/f2fs/data.c:370 bio_endio+0x5af/0x6c0 block/bio.c:1608 req_bio_endio block/blk-mq.c:761 [inline] blk_update_request+0x5cc/0x1690 block/blk-mq.c:906 blk_mq_end_request+0x59/0x4c0 block/blk-mq.c:1023 lo_complete_rq+0x1c6/0x280 drivers/block/loop.c:370 blk_complete_reqs+0xad/0xe0 block/blk-mq.c:1101 __do_softirq+0x1d4/0x8ef kernel/softirq.c:571 run_ksoftirqd kernel/softirq.c:939 [inline] run_ksoftirqd+0x31/0x60 kernel/softirq.c:931 smpboot_thread_fn+0x659/0x9e0 kernel/smpboot.c:164 kthread+0x33e/0x440 kernel/kthread.c:379 ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:308 The root cause is below race case can cause leaving dirty metadata in f2fs after filesystem is remount as ro: Thread A Thread B - f2fs_ioc_resize_fs - f2fs_readonly --- return false - f2fs_resize_fs - f2fs_remount - write_checkpoint - set f2fs as ro - free_segment_range - update meta_inode's data Then, if f2fs_put_super() fails to write_checkpoint due to readonly status, and meta_inode's dirty data will be writebacked after node_inode is put, finally, f2fs_write_end_io will access NULL pointer on sbi->node_inode. Thread A IRQ context - f2fs_put_super - write_checkpoint fails - iput(node_inode) - node_inode = NULL - iput(meta_inode) - write_inode_now - f2fs_write_meta_page - f2fs_write_end_io - NODE_MAPPING(sbi) : access NULL pointer on node_inode Fixes: b4b10061ef98 ("f2fs: refactor resize_fs to avoid meta updates in progress") Reported-by: butt3rflyh4ck Closes: https://lore.kernel.org/r/1684480657-2375-1-git-send-email-yangtiezhu@loongson.cn Tested-by: butt3rflyh4ck Signed-off-by: Chao Yu Signed-off-by: Jaegeuk Kim (cherry picked from commit d8189834d4348ae608083e1f1f53792cfcc2a9bc) CVE-2023-2898 Signed-off-by: Yuxuan Luo --- fs/f2fs/f2fs.h | 2 +- fs/f2fs/file.c | 2 +- fs/f2fs/gc.c | 21 ++++++++++++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 42962ee0a1179..c1022ab6c3fe9 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -3852,7 +3852,7 @@ void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi); block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode); int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control); void f2fs_build_gc_manager(struct f2fs_sb_info *sbi); -int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count); +int f2fs_resize_fs(struct file *filp, __u64 block_count); int __init f2fs_create_garbage_collection_cache(void); void f2fs_destroy_garbage_collection_cache(void); diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index de92279a80020..948728cd15891 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -3282,7 +3282,7 @@ static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg) sizeof(block_count))) return -EFAULT; - return f2fs_resize_fs(sbi, block_count); + return f2fs_resize_fs(filp, block_count); } static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg) diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c index 72cda2f9380f2..08ada3f9168b0 100644 --- a/fs/f2fs/gc.c +++ b/fs/f2fs/gc.c @@ -2074,8 +2074,9 @@ static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs) } } -int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count) +int f2fs_resize_fs(struct file *filp, __u64 block_count) { + struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp)); __u64 old_block_count, shrunk_blocks; struct cp_control cpc = { CP_RESIZE, 0, 0, 0 }; unsigned int secs; @@ -2113,12 +2114,18 @@ int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count) return -EINVAL; } + err = mnt_want_write_file(filp); + if (err) + return err; + shrunk_blocks = old_block_count - block_count; secs = div_u64(shrunk_blocks, BLKS_PER_SEC(sbi)); /* stop other GC */ - if (!f2fs_down_write_trylock(&sbi->gc_lock)) - return -EAGAIN; + if (!f2fs_down_write_trylock(&sbi->gc_lock)) { + err = -EAGAIN; + goto out_drop_write; + } /* stop CP to protect MAIN_SEC in free_segment_range */ f2fs_lock_op(sbi); @@ -2138,10 +2145,18 @@ int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count) out_unlock: f2fs_unlock_op(sbi); f2fs_up_write(&sbi->gc_lock); +out_drop_write: + mnt_drop_write_file(filp); if (err) return err; freeze_super(sbi->sb); + + if (f2fs_readonly(sbi->sb)) { + thaw_super(sbi->sb); + return -EROFS; + } + f2fs_down_write(&sbi->gc_lock); f2fs_down_write(&sbi->cp_global_sem);