Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3353975rwb; Tue, 16 Aug 2022 01:03:21 -0700 (PDT) X-Google-Smtp-Source: AA6agR7WI81vh5xgmFGfWARRZFEm/D/gPwKWYhJiEO9ilXdYKIAzBfWdWiT+NNXQnIUOQ//r2axw X-Received: by 2002:a17:90b:3144:b0:1f5:2c6d:7dea with SMTP id ip4-20020a17090b314400b001f52c6d7deamr21903168pjb.220.1660637001439; Tue, 16 Aug 2022 01:03:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660637001; cv=none; d=google.com; s=arc-20160816; b=IqRTzoeiw/Qf7SRRBs1zLfMMzUfStcf+1STEWiUBly79dUsna0OMdNOiS2k1aFKWIK YKGrtv77zZcS8l08zHi+AtVTOYKXML65KHP0zNUgx94pMYeZPD7E1WyzA9+IizZxL5PI JEEdkz3aDBohv0fFD9dTVEdJTk5yYzgFhnDRwLCO4jjw6T7SLmz/Cw3F/iUW8P/wQiEq lZhwLqo6+0faB5UsOMHLEoeaLFVUKG4qbH3zqjK509BZYlR2z6gVAha2nHN+bFA+pSyh iZWYDlLwJfcoZJYHE9WCRjFE21QaNAspNJkSsE/javRSJCX+mXnr/fSLslHdoeXSq0Qo EXUg== 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 :message-id:date:subject:cc:to:from; bh=EaurB3q6BZzmM5OD2bAbheeAJQNczIABgo4/1pECcyI=; b=1CJCrIiI5as0nk4tKw13PdFBHsAosnFvBdlTOsQEoB0E9tsi6eSozmzHR+juJ7xegu 3pHT7EjfQCWtKCDMLqhh8XwyXQU2gWwCQDDr7+NoSGk0nXMbFjoYEpV0DcAB1kAnJF6u Lz3Wv1owEta3a65nn0umST6o7MIGiGS71fW54bBtJl14QWKtaL4ksWTWMdlCF10YDyOY DVDwPQclxsOE7UY+VTirkXtPJ3QWdCKpooj7uClmB4d+UaLxdB6l49QGPfFZYBQ0H3Ed S2pPuMr1eFF0UcSvSulVd2gvEz9Mkbbjg+/kXaXDZoIY2EK+zwQ6XWDwHth2/v+sq9St D2TA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y62-20020a638a41000000b0041d9a04c250si12614855pgd.346.2022.08.16.01.03.10; Tue, 16 Aug 2022 01:03: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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232208AbiHPHcj (ORCPT + 99 others); Tue, 16 Aug 2022 03:32:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232237AbiHPHcS (ORCPT ); Tue, 16 Aug 2022 03:32:18 -0400 Received: from hust.edu.cn (unknown [202.114.0.240]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D42945061; Mon, 15 Aug 2022 21:10:26 -0700 (PDT) Received: from localhost.localdomain ([172.16.0.254]) (user=dzm91@hust.edu.cn mech=LOGIN bits=0) by mx1.hust.edu.cn with ESMTP id 27G4A3WV004209-27G4A3WY004209 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 16 Aug 2022 12:10:10 +0800 From: Dongliang Mu To: Alexander Viro Cc: Dongliang Mu , butt3rflyh4ck , Hao Sun , Jiacheng Xu , stable@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] fs: fix UAF/GPF bug in nilfs_mdt_destroy Date: Tue, 16 Aug 2022 12:08:58 +0800 Message-Id: <20220816040859.659129-1-dzm91@hust.edu.cn> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-FEAS-AUTH-USER: dzm91@hust.edu.cn X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE 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: Dongliang Mu In alloc_inode, inode_init_always() could return -ENOMEM if security_inode_alloc() fails, which causes inode->i_private uninitialized. Then nilfs_is_metadata_file_inode() returns true and nilfs_free_inode() wrongly calls nilfs_mdt_destroy(), which frees the uninitialized inode->i_private and leads to crashes(e.g., UAF/GPF). Fix this by moving security_inode_alloc just prior to this_cpu_inc(nr_inodes) Link: https://lkml.kernel.org/r/CAFcO6XOcf1Jj2SeGt=jJV59wmhESeSKpfR0omdFRq+J9nD1vfQ@mail.gmail.com Reported-by: butt3rflyh4ck Reported-by: Hao Sun Reported-by: Jiacheng Xu Signed-off-by: Dongliang Mu Cc: Al Viro Cc: stable@vger.kernel.org --- v1->v2: move security_inode_alloc at the very end according to Al Viro other than initializing i_private before security_inode_alloc. fs/inode.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fs/inode.c b/fs/inode.c index 6462276dfdf0..49d1eb91728c 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -192,8 +192,6 @@ int inode_init_always(struct super_block *sb, struct inode *inode) inode->i_wb_frn_history = 0; #endif - if (security_inode_alloc(inode)) - goto out; spin_lock_init(&inode->i_lock); lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key); @@ -228,6 +226,9 @@ int inode_init_always(struct super_block *sb, struct inode *inode) inode->i_fsnotify_mask = 0; #endif inode->i_flctx = NULL; + + if (security_inode_alloc(inode)) + goto out; this_cpu_inc(nr_inodes); return 0; -- 2.35.1