Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp6739043rwi; Mon, 24 Oct 2022 05:37:44 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6XpHY4Us24M21o8hN5l6h6tV2rJMf/s9+ipW4Nqsw/H4yS9G73UU2wGpR4G/2mfO8fX8bq X-Received: by 2002:a05:6a00:16c8:b0:53b:3b9f:7283 with SMTP id l8-20020a056a0016c800b0053b3b9f7283mr33223972pfc.46.1666615064105; Mon, 24 Oct 2022 05:37:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666615064; cv=none; d=google.com; s=arc-20160816; b=rcIpexogXUaj6HWdC9p4vIkupH+dHx8CNTAkARLfrmXkqHTU4wTdBz3DE3o7D+70l6 nzLo+Yyi45o96ZbZr9kPg5tUSKq4raIVqsYNuoVa2WDIlxzujVPvb4BpnaR2MTHzf08Y 3lIm4I25hLu5AtEE+/pacndCsuh+hCPgf3ShyzqnCQ6kWRBHQe9jQ0fwYJQvJXYJeFAf DikAuEj8DMZuehGAHb2h6lZ0nCRxxgvBnJrhFXg9H2L2H4wpmhGnGIPvaa2rJJV5DM6n o/GyLnfAZ3pDf2UIaR1E+ud8ioHF6/hyYyK0qYNA7k/pyhfdmPbam+/Gu8nlpjYnkUSu BvwQ== 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=wiBsbzYX83WP9AE5nSjuz6/q7OEivkPR/55sQvr2Jw8=; b=jCAWzkaYrtDgNvLLVKQrSpbpXsfHbrrQA+kbMhRvL756eKaTDVWacSywm/r1RVmhsE sk6+ZhogeBtvcLjEciivsKifDptkkcM0nhWS2dYNo5MG3nmrTzUsXGnv2296sP19mJhU muec9fzexZn3KEcZWBHMSDdfNSiXjYu49LzRRz+9ZpzgUftMEDyLhb48L5Wgk64NbsxT Cg+ZDFekBMeGF+tbasvKvQIGSAEr4U6huu/I6qlAH30wV1MQDyU3OAz/EA/+rEv6gvjQ hTAywm+WU5vTMmoN8mkYdqvKlLht+jwW+HXFoMZqGVxxGgyESZB4rLbj1DOmbcMMPloZ oUBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Hn06oHh5; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e17-20020a17090301d100b001869ba21491si6220088plh.8.2022.10.24.05.37.32; Mon, 24 Oct 2022 05:37:44 -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=Hn06oHh5; 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 S231261AbiJXLuY (ORCPT + 99 others); Mon, 24 Oct 2022 07:50:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231757AbiJXLsW (ORCPT ); Mon, 24 Oct 2022 07:48:22 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EA9A10FEA; Mon, 24 Oct 2022 04:43:33 -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 sin.source.kernel.org (Postfix) with ESMTPS id 7034BCE1346; Mon, 24 Oct 2022 11:41:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67C44C433B5; Mon, 24 Oct 2022 11:41:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666611715; bh=62cxHPxacIeJObyCUvpGDXSutCGffzA9SR9xi6Ab0Pw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hn06oHh5ktdp0XJf8WF4EnQ6zX9elWei8SRkJJW+GiwHhVZun8sV9qvfXDFNlSjUZ fGtZVEWE89pKwTrFh6jj67Tra2adfId+1GdhMg3GZLrvlPzKgU+aI29/WpdUnjqOYV A4eWc2X67s9XDgMaIi4dg3HQe9ELLFSedESkv7kk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ryusuke Konishi , syzbot+b8c672b0e22615c80fe0@syzkaller.appspotmail.com, Khalid Masum , Andrew Morton Subject: [PATCH 4.9 054/159] nilfs2: fix use-after-free bug of struct nilfs_root Date: Mon, 24 Oct 2022 13:30:08 +0200 Message-Id: <20221024112951.405149610@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112949.358278806@linuxfoundation.org> References: <20221024112949.358278806@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 From: Ryusuke Konishi commit d325dc6eb763c10f591c239550b8c7e5466a5d09 upstream. If the beginning of the inode bitmap area is corrupted on disk, an inode with the same inode number as the root inode can be allocated and fail soon after. In this case, the subsequent call to nilfs_clear_inode() on that bogus root inode will wrongly decrement the reference counter of struct nilfs_root, and this will erroneously free struct nilfs_root, causing kernel oopses. This fixes the problem by changing nilfs_new_inode() to skip reserved inode numbers while repairing the inode bitmap. Link: https://lkml.kernel.org/r/20221003150519.39789-1-konishi.ryusuke@gmail.com Signed-off-by: Ryusuke Konishi Reported-by: syzbot+b8c672b0e22615c80fe0@syzkaller.appspotmail.com Reported-by: Khalid Masum Tested-by: Ryusuke Konishi Cc: Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- fs/nilfs2/inode.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) --- a/fs/nilfs2/inode.c +++ b/fs/nilfs2/inode.c @@ -344,6 +344,7 @@ struct inode *nilfs_new_inode(struct ino struct inode *inode; struct nilfs_inode_info *ii; struct nilfs_root *root; + struct buffer_head *bh; int err = -ENOMEM; ino_t ino; @@ -359,11 +360,26 @@ struct inode *nilfs_new_inode(struct ino ii->i_state = BIT(NILFS_I_NEW); ii->i_root = root; - err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh); + err = nilfs_ifile_create_inode(root->ifile, &ino, &bh); if (unlikely(err)) goto failed_ifile_create_inode; /* reference count of i_bh inherits from nilfs_mdt_read_block() */ + if (unlikely(ino < NILFS_USER_INO)) { + nilfs_msg(sb, KERN_WARNING, + "inode bitmap is inconsistent for reserved inodes"); + do { + brelse(bh); + err = nilfs_ifile_create_inode(root->ifile, &ino, &bh); + if (unlikely(err)) + goto failed_ifile_create_inode; + } while (ino < NILFS_USER_INO); + + nilfs_msg(sb, KERN_INFO, + "repaired inode bitmap for reserved inodes"); + } + ii->i_bh = bh; + atomic64_inc(&root->inodes_count); inode_init_owner(inode, dir, mode); inode->i_ino = ino;