Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3250980rwe; Mon, 29 Aug 2022 08:16:31 -0700 (PDT) X-Google-Smtp-Source: AA6agR5PQib2g8KIvO583vAGVJqlzpkpbu8Lk3GM/qwKLiGFsgcT1BaqjKeSokLbeAbf0YKNd8nk X-Received: by 2002:a17:902:d4c9:b0:172:f480:bdd with SMTP id o9-20020a170902d4c900b00172f4800bddmr16936107plg.170.1661786191495; Mon, 29 Aug 2022 08:16:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661786191; cv=none; d=google.com; s=arc-20160816; b=jUmcjD9t/zFRBIuvzTXvjrOKqtJa+znU3dBcib9xk/OPSTieyaTZyeCEaI0EcEHDC+ 7V7Fd39p7SKqSX7GfjsL70WFgtUT9OmdnF20oiMewCNk91HmxJb2VA5/VAyhcfp6Zh6J +JpRzFRXzvWz8MRoF2Y9sQ2G6h3LTs2OzSZycJxsx+Mhm1kjgw//T3Yml5L6XAWVaMhM XIpp2IokdvZJ76Ukjn8enlSxFg20BkxOYy0YxBAaejxbxQdiB2Rrza5I2Hh9Qsl5qZMx 7NVfYxYi/AUi08sRTXyeUYQ6qc/42Iy+h9QJ9Ala/1Ycis8hEuVnRUQ9QH0OpvmjCxVD k+eQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=HWK56QEsrQIXb38Alg1Oet+PKxiyCXvx2oANoXHiuc8=; b=ralllpXwMQXyAoORVHb+tjMdN/nqe6GXw7saoh/NTA4NFGVjP4iF4X0Bh7B3J72b7V NFG0G30DQTYw6k1AH+uSjaEIO71uJ6t1UInVykxzNeNb+3ovi8F36x5PGvqx/ieDwrLh bONKvpbA8CE3TtQkOiMaa0XzkKIrM+wHY2Vw4VSEOXPe07icQ3KOr1aA46fnx28cgIYk PtpsHXKr9FGVEpqpVths389kJ/D/8Zr0h+wnww//CCgdAMvGIyMF3K8y0Ve+unWJZZZw HaPcgIWOR2DBaeeYgtUDoR2PoYA/DUVA3tN1W0p9XzXPT+l4PyDZ8w6j0ZffQQsqTBvp ixCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oczrsAAZ; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s2-20020a170902ea0200b001727d67b597si3459083plg.363.2022.08.29.08.16.17; Mon, 29 Aug 2022 08:16:31 -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=@kernel.org header.s=k20201202 header.b=oczrsAAZ; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230343AbiH2OP1 (ORCPT + 99 others); Mon, 29 Aug 2022 10:15:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229980AbiH2OPZ (ORCPT ); Mon, 29 Aug 2022 10:15:25 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73B7A1A39E; Mon, 29 Aug 2022 07:15:23 -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 0F36E60F5B; Mon, 29 Aug 2022 14:15:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 058FDC433C1; Mon, 29 Aug 2022 14:15:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1661782522; bh=+nyYdh/yy5EHKyi0lpYY9d3e+x1/y1ayeGH6tnploAE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=oczrsAAZRB7+HuHkBHmMN1BZNstE3epijQo1SngzYh79jhWGVjXhYhoODnqaZj5rd EMofYuKj0e/AkGWSwnUBefQ+SL9FTIXG5O7Ra/XhtTL272P4t44tPus4lBzW/6Pd55 IBtOa+iVodOoAaKqgVW7tFxJpi6ULCiHjlUISiRayaUU9mwuoRzLAbwEsBqnXFnhDc mLjLOGvOfc30vZ50DtGmdmVZumM6y5r9WTQcn1sQwFZnCWhNBKwxQfYvuvgtGfSEU1 Jt4Tt1ZyfJUaEyDJaeOSLCSJJg90Xxd6hxx4DVhg4IrWilJsmKf5XLeCxEwRcEq114 sXfWLkU9sFcJg== Date: Mon, 29 Aug 2022 16:15:17 +0200 From: Christian Brauner To: Alexey Khoroshilov Cc: Alexander Viro , Rustam Subkhankulov , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, ldv-project@linuxtesting.org Subject: Re: [PATCH] fs/inode.c: change the order of initialization in inode_init_always() Message-ID: <20220829141517.bcjbdk5zb74mrhgu@wittgenstein> References: <1661609366-26144-1-git-send-email-khoroshilov@ispras.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1661609366-26144-1-git-send-email-khoroshilov@ispras.ru> X-Spam-Status: No, score=-7.1 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,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 On Sat, Aug 27, 2022 at 05:09:26PM +0300, Alexey Khoroshilov wrote: > From: Rustam Subkhankulov > > If function 'security_inode_alloc()' returns a nonzero value at > [fs/inode.c: 195] 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 | 7 +++---- > 1 file changed, 3 insertions(+), 4 deletions(-) > > diff --git a/fs/inode.c b/fs/inode.c > index bd4da9c5207e..08d093737e8c 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); > > @@ -230,9 +228,10 @@ int inode_init_always(struct super_block *sb, struct inode *inode) > inode->i_flctx = NULL; > this_cpu_inc(nr_inodes); > > + if (security_inode_alloc(inode)) > + return -ENOMEM; This should probably be before this_cpu_inc(nr_inodes).