From patchwork Mon Oct 24 11:29:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 8598 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp419479wru; Mon, 24 Oct 2022 05:16:22 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7FmF9VLUjkCUTpvzbo8Y4BArpRTWtunF9SOC4DtAGfLp6VJX98pDCSezkY+P3BScmTr0wK X-Received: by 2002:a50:eb05:0:b0:457:c6f5:5f65 with SMTP id y5-20020a50eb05000000b00457c6f55f65mr29800851edp.311.1666613781982; Mon, 24 Oct 2022 05:16:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666613781; cv=none; d=google.com; s=arc-20160816; b=P4V2biFN8Ugs0NT5Ug4SG7OYUk2ESzExJoY20f/EoBE4p02SpQUukPEx42JdGv7RLc blO3P1aF6PoECexJQ/m8WKZGI3uVP74BFQSFBoOmD7rpvMXE0sWWsKWyD20gx5wb4i9L 3NI1rsXwm+XVLWQm6S9iQANExXWxaVmD4/lS/r0KcsroiPeMqxiKFW+7u/wgoObbIRS7 /9cnFjawhMZT87kFdoIwlx9fxPKDGTsflPdKRTjpUt87za+OctBKm2eoo9XrVE/Cq0iQ OUUpmfimLm8+asvT6aKMFYu6E6RWMCFo0O/TptOUh5jlnyFhXD4ePHWC/UWqROJY1S6s t9QQ== 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=12KUCSBcr/iyD5t29KL9ux2tNnrQzhHGK8GdxJalo2g=; b=MXOEAIbSfgV4JSx/3nAFO2rzKb5msKtvVmIeqQCKwr67nq0tFCFcCx6wnNuEt9Hbd2 aPfW+EbOLfQmCTALTBGvcn7nbIPY+l9fVkjHAI/gKmPXR3HK8trYOb0nH2qNU6hPVW9f fO2WZ5bnF+Uinc0GE8uoKRmHkiQGTQ9prKwI1Qd386XNpVucUBXevPrp7xXYtzhTGk/t SNqFmb6JtTjaAeYG5yJFm/fD2hdlWv/R104vgyrTUGCb+7CYtmd5sXFGjfCNpB/FOHZL Yny+Fy6TlmvRZTD+V5HMh+Ub75TQ/bMwjPr7TcyvYq1ck5MHPqSnSEBLfk1HsafzP3eh +g0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ogUbeIKX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hq6-20020a1709073f0600b00787c0e9818csi32709120ejc.568.2022.10.24.05.15.55; Mon, 24 Oct 2022 05:16:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ogUbeIKX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S232683AbiJXMGi (ORCPT + 99 others); Mon, 24 Oct 2022 08:06:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232859AbiJXME3 (ORCPT ); Mon, 24 Oct 2022 08:04:29 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EF6B1F60F; Mon, 24 Oct 2022 04:50:41 -0700 (PDT) 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 ams.source.kernel.org (Postfix) with ESMTPS id BF199B81199; Mon, 24 Oct 2022 11:49:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22B0FC433D6; Mon, 24 Oct 2022 11:49:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666612168; bh=1lz+gyBvGW+uPkiXCZxREp6jG4QKXOV8PFWgjLYk+QI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ogUbeIKXuZJMVVVyPRE2N8uqN72EfH2/rJP0YlfXSb9XqB+WVBd3ZEz6yBt6rfx6M 9tJn6ftc47LfQB+OwQkrf6ArDnL+2LnjAejuboEZ//VROKNFkBlydSLj3VBNKJR8eI 3DWUmWyF6P0hnCKjFTKc0OmVU1pn9nF/uLwh10F4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, stable@kernel.org, Tadeusz Struk , syzbot+bd13648a53ed6933ca49@syzkaller.appspotmail.com, Jan Kara , Lukas Czerner , Theodore Tso Subject: [PATCH 4.14 068/210] ext4: avoid crash when inline data creation follows DIO write Date: Mon, 24 Oct 2022 13:29:45 +0200 Message-Id: <20221024112959.261264220@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112956.797777597@linuxfoundation.org> References: <20221024112956.797777597@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747571212887443384?= X-GMAIL-MSGID: =?utf-8?q?1747571212887443384?= From: Jan Kara commit 4bb26f2885ac6930984ee451b952c5a6042f2c0e upstream. When inode is created and written to using direct IO, there is nothing to clear the EXT4_STATE_MAY_INLINE_DATA flag. Thus when inode gets truncated later to say 1 byte and written using normal write, we will try to store the data as inline data. This confuses the code later because the inode now has both normal block and inline data allocated and the confusion manifests for example as: kernel BUG at fs/ext4/inode.c:2721! invalid opcode: 0000 [#1] PREEMPT SMP KASAN CPU: 0 PID: 359 Comm: repro Not tainted 5.19.0-rc8-00001-g31ba1e3b8305-dirty #15 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.0-1.fc36 04/01/2014 RIP: 0010:ext4_writepages+0x363d/0x3660 RSP: 0018:ffffc90000ccf260 EFLAGS: 00010293 RAX: ffffffff81e1abcd RBX: 0000008000000000 RCX: ffff88810842a180 RDX: 0000000000000000 RSI: 0000008000000000 RDI: 0000000000000000 RBP: ffffc90000ccf650 R08: ffffffff81e17d58 R09: ffffed10222c680b R10: dfffe910222c680c R11: 1ffff110222c680a R12: ffff888111634128 R13: ffffc90000ccf880 R14: 0000008410000000 R15: 0000000000000001 FS: 00007f72635d2640(0000) GS:ffff88811b000000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000565243379180 CR3: 000000010aa74000 CR4: 0000000000150eb0 Call Trace: do_writepages+0x397/0x640 filemap_fdatawrite_wbc+0x151/0x1b0 file_write_and_wait_range+0x1c9/0x2b0 ext4_sync_file+0x19e/0xa00 vfs_fsync_range+0x17b/0x190 ext4_buffered_write_iter+0x488/0x530 ext4_file_write_iter+0x449/0x1b90 vfs_write+0xbcd/0xf40 ksys_write+0x198/0x2c0 __x64_sys_write+0x7b/0x90 do_syscall_64+0x3d/0x90 entry_SYSCALL_64_after_hwframe+0x63/0xcd Fix the problem by clearing EXT4_STATE_MAY_INLINE_DATA when we are doing direct IO write to a file. Cc: stable@kernel.org Reported-by: Tadeusz Struk Reported-by: syzbot+bd13648a53ed6933ca49@syzkaller.appspotmail.com Link: https://syzkaller.appspot.com/bug?id=a1e89d09bbbcbd5c4cb45db230ee28c822953984 Signed-off-by: Jan Kara Reviewed-by: Lukas Czerner Tested-by: Tadeusz Struk Link: https://lore.kernel.org/r/20220727155753.13969-1-jack@suse.cz Signed-off-by: Theodore Ts'o Signed-off-by: Greg Kroah-Hartman --- fs/ext4/file.c | 6 ++++++ 1 file changed, 6 insertions(+) --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -588,6 +588,12 @@ static loff_t ext4_seek_data(struct file inode_unlock(inode); return -ENXIO; } + /* + * Make sure inline data cannot be created anymore since we are going + * to allocate blocks for DIO. We know the inode does not have any + * inline data now because ext4_dio_supported() checked for that. + */ + ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); blkbits = inode->i_sb->s_blocksize_bits; start = offset >> blkbits;