From patchwork Mon Oct 24 11:29:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 8713 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp423130wru; Mon, 24 Oct 2022 05:24:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4HTKzUm9n4jd2YO9kz2g0TcieWjsRpRcwxoluQtihxm7PpkofGm1y1I1f16lcSwxt4Aazb X-Received: by 2002:a05:6a00:450d:b0:562:51ad:7cdf with SMTP id cw13-20020a056a00450d00b0056251ad7cdfmr32944936pfb.54.1666614250133; Mon, 24 Oct 2022 05:24:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666614250; cv=none; d=google.com; s=arc-20160816; b=XN7eSgldJDvU7EZ7ikAFB4tVbFwa7Eu7e5BZmxHoodFcWWrQfT+VFlv65jy7rakol1 0Qrzl1xbRBuMm+QPMozBb1KcZv3p7RMF60TALWG9j0fYAGe5I1sAkX0IRcUbq/Zbos3+ jOfHVzKwhb9efJLrniuZsCbT6XlrZ5Z7v9bKdG+S7epltVfIWdOx9+0BdvfVx31i4gMK 9PA7ucYHctHqMSaJJyC3z/0gvQ5nLsvVuQ6qDMEKcuvUcITS/coLwqo4YwhPWs+vhHaF iYSPpjxc+6zX4+NpBtu22ev130BAzuoLPFXuPEoqivf9URhJZ5V5R++2XfGrZrVUwEII wAjw== 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=kpYHaUuqvI2YUJGcHswZBPY+osAcTf9K2CkjYsg6TQM=; b=b3uKVwU/S4f0HRPT9SCPY7q0bNCJWE/Laz9yTg2p0XwKzSHXikRwwoSxr/HFFdH1AJ IW2u0vA12SlkOEZyCd0KzuThUWZ9KbEIuGv3xD7BUp46/m5QBhHd71r3iu/8N3iQCenA kgOpfKl1sR1VhSiIPtXtBQ1KS8Rga/QiWtbx3f2dHh6JDSDSLryQQY5ivW99nrwDMaB2 P5njlvWyt8d2SF96ciEONv1fqe/ufJ80FtSrTkevepmwtcc2gAhWfGUWt9aaklgIWii0 PuGhx9DL2Hz5JfQ+8hu0G+1k5uYrwtISiLwZdhDHeHzzmZJ4GgPxi/yrq8mG7sN37F1P 54Dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0BXq945c; 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 n5-20020a170902e54500b00186a397324asi4897953plf.373.2022.10.24.05.23.56; Mon, 24 Oct 2022 05:24:10 -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=0BXq945c; 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 S233300AbiJXMXG (ORCPT + 99 others); Mon, 24 Oct 2022 08:23:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233360AbiJXMVD (ORCPT ); Mon, 24 Oct 2022 08:21:03 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 430C776444; Mon, 24 Oct 2022 04:59:11 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 902C0612E3; Mon, 24 Oct 2022 11:58:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5A99C433C1; Mon, 24 Oct 2022 11:58:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666612701; bh=nThvYlMCokv6gXc3/lKR2WMS3hubHiyAx5INGUND/qw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0BXq945cxcZO5UNQZAwFUe//L1At9wNF5iuHsie45G0MijMa9A2MFLfXGDXN7kvbp a2WqIMaDvNdTPtFxXx3+dv1ch7PkTzkaL7MLa/ep9sNJG9xi55YfEIS8JBZBUgjQZE 35D8WwlWlfY2WQY5tKCFmDMeQeF7v33CEVaJSWDI= 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.19 057/229] nilfs2: fix use-after-free bug of struct nilfs_root Date: Mon, 24 Oct 2022 13:29:36 +0200 Message-Id: <20221024113000.930887573@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112959.085534368@linuxfoundation.org> References: <20221024112959.085534368@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?1747571704013447573?= X-GMAIL-MSGID: =?utf-8?q?1747571704013447573?= 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 @@ -340,6 +340,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; @@ -355,11 +356,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;