Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3481705rwe; Mon, 29 Aug 2022 12:40:48 -0700 (PDT) X-Google-Smtp-Source: AA6agR72zEbIIxOTmYM0gFXgk4We54JjJUpq8mbjgt2bNhkZ5wEixjiNmlEmJQTyNqTuyrBZ/Z+M X-Received: by 2002:a05:6402:292c:b0:448:353d:1c2b with SMTP id ee44-20020a056402292c00b00448353d1c2bmr8755210edb.232.1661802048607; Mon, 29 Aug 2022 12:40:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661802048; cv=none; d=google.com; s=arc-20160816; b=ZQmtiWZkQhZ+g2BSOdwk0m2rjUczhnsvwRMvbx5aFn71oaF/ekpDBxKHhfGlzyhf/V BA8s7tzhbZ8SGEjds5EmLOCakamxOKaujLLsDfBmw5lXkVu6HSNt/g9O0gQBB5/0jREh jQxY4pLh13FZdOfmHEJeypTlzKGuH+77lS2uPKoutv9UX/tUP0q0SJklkaK+6OWnDlMy cvrDnW2u3yPL3RpAGTToDc/eJN22Evwi8NLysJekLGfxPuUPh7KfJZLHH9kc6fotvX95 WIU9r7mf1853aHipBrA3KzseZLTAPFZPJOg1kEVv49d9dxiFelb/NBt7M4Nsyj2ZnlPA cUpw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=MJlRzx6VlbpmKdLXYdhZm1S0nWne5ZUmRJBORf7eDKY=; b=yMyhsdZygyB09AUlQv8qh/7xED6Hiapyk2PzDV+39QxBQtdPxJ5NBofnVX9FH6+DNv muXcA6x71z+jEGCHibZVkhJKE+Gd7e1j/qrsngtXAyoW1xDs375fDiDdWI2Yf24X6mGW 6QYhT41oGyzbZgUJFbfXicLxc3Wmfs9DfzL48aOWNQ4GGDl0g02ynViSd2KpoHlF2qgc GvuBQjPGpPaBMHpzBqLAMF0DTcbifUZvhPDD8iwpLsrc06tVehCzTSqDwzOXCTZH1iMW AP5wfcekUIRX5gKAThN3P+dEQi11ktRUSZQIjRXtd+aNiHQ0NJHJaYT7ULlASX24/Plb 9gyg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s11-20020a056402520b00b00446b907d8d2si8997482edd.7.2022.08.29.12.39.57; Mon, 29 Aug 2022 12:40:48 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229854AbiH2T0J (ORCPT + 99 others); Mon, 29 Aug 2022 15:26:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229681AbiH2T0G (ORCPT ); Mon, 29 Aug 2022 15:26:06 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BCDBA8B2C9; Mon, 29 Aug 2022 12:26:01 -0700 (PDT) Received: from rustam-GF63-Thin-9RCX.. (unknown [93.175.1.130]) by mail.ispras.ru (Postfix) with ESMTPSA id 52C7B40D403E; Mon, 29 Aug 2022 19:25:58 +0000 (UTC) From: Rustam Subkhankulov To: Alexander Viro Cc: Rustam Subkhankulov , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Alexey Khoroshilov , ldv-project@linuxtesting.org Subject: [PATCH v2] fs/inode.c: change the order of initialization in inode_init_always() Date: Mon, 29 Aug 2022 22:25:21 +0300 Message-Id: <20220829192521.694631-1-subkhankulov@ispras.ru> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220829141517.bcjbdk5zb74mrhgu@wittgenstein> References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, 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 If function security_inode_alloc() returns a nonzero value due to an error (e.g. fail to allocate memory), then some of the fields, including 'i_private', will not be initialized. After that, if the fs-specfic free_inode function is called in i_callback(), the nonzero value of 'i_private' field can be interpreted as initialized. As a result, this can cause dereferencing of random value pointer (e.g. nilfs2). In earlier versions, a similar situation could occur with the 'u' union in 'inode' structure. Found by Linux Verification Center (linuxtesting.org) with syzkaller. Signed-off-by: Rustam Subkhankulov Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") --- fs/inode.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fs/inode.c b/fs/inode.c index ba1de23c13c1..a2892d85993d 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,10 @@ 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.34.1