Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4447814pxf; Tue, 30 Mar 2021 08:05:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/dI6ffbA+GnE5tu0BBJcXLkJbv43aPdcHg4Iz2pgWlsWiRPKi8BOyE/0s7yXPlN/ZqHz/ X-Received: by 2002:a05:600c:26d2:: with SMTP id 18mr4506557wmv.41.1617116708418; Tue, 30 Mar 2021 08:05:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617116708; cv=none; d=google.com; s=arc-20160816; b=kUW1yu3d36RvUPupW7vRRzQ90T1w6oUYFbjiG+La9hpV2UkTKhoHPgGHspEiU000DD Kvmpe/pJS+MbcDTOyIXIqhwzep9doQTP18fiq5rz6qFd1kQ7JkkvN3BIFMUNaHLFuGqA HhogU8C0RfFv15EmTGXLI0s0eqoSKPL7ZQUJpdR5eYbY4ITNPknArCv+u9XgH6Hxim0z mTtrqnDH3eqcVow6BakUxtNqpKBnGCKb4Prs7MG07IJcCqzSZIEo2QG+e6DhwGBECbbt trc/+ysmndBpQYkXLhGBpDMPeJ2Zfv7yGJpUci92fmmsU/xSZ63dHBFeXgbnnZq/ow+r uCbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=pgT+gQJGQNMN7XdpbCxxJDVn9zLR2XMrnLkDjx85/Xo=; b=aH3lqPEAYqQcYHbe3ncHWpdAyKmVRJ3hSz7biy97oW5tMgM/bcm7xyZAMDmpa8uxnd GdtxMt05WkRyahxogKctyU54z3mxRLHEtV2VO95VukTs6nKP3r9tRHFhFmXbbmBhOC1K RUlPvgn2bEuEDVbIuEGK2APtVJxs+Eknbxmi1QAQiK4P0Ve6v5Sc7yjtqYYIMNi0chLY n59dyMmF7COMaRGwrzrkfEhgz+a7Zrt+fHaB8adIP/AC2Bo+93yW8Kca2kdddwmds+ur LgFIJY/SJ5R5lFInaVxnXmXwjiu8oRQ0owuvMaoKhuxceXCcZJnc6LL+0pBXicgKB7Sp vXqA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a10si16368429edf.499.2021.03.30.08.04.40; Tue, 30 Mar 2021 08:05:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232184AbhC3PCn (ORCPT + 99 others); Tue, 30 Mar 2021 11:02:43 -0400 Received: from mx2.suse.de ([195.135.220.15]:57786 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231874AbhC3PCb (ORCPT ); Tue, 30 Mar 2021 11:02:31 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 7591EB30E; Tue, 30 Mar 2021 15:02:30 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id D3EF21E4353; Tue, 30 Mar 2021 17:02:29 +0200 (CEST) Date: Tue, 30 Mar 2021 17:02:29 +0200 From: Jan Kara To: Zhang Yi Cc: Jan Kara , "Theodore Y. Ts'o" , Ext4 Developers List , yangerkun , linfeilong@huawei.com Subject: Re: [BUG && Question] question of SB_ACTIVE flag in ext4_orphan_cleanup() Message-ID: <20210330150229.GC30749@quack2.suse.cz> References: <8a6864dd-7e6c-5268-2b5b-1010f99d2a1b@huawei.com> <20210322172551.GJ31783@quack2.suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Mon 29-03-21 17:20:35, Zhang Yi wrote: > On 2021/3/23 1:25, Jan Kara wrote: > > Hi! > > > > On Mon 22-03-21 23:24:23, Zhang Yi wrote: > >> We find a use after free problem when CONFIG_QUOTA is enabled, the detail of > >> this problem is below. > >> > >> mount_bdev() > >> ext4_fill_super() > >> sb->s_root = d_make_root(root); > >> ext4_orphan_cleanup() > >> sb->s_flags |= SB_ACTIVE; <--- 1. mark sb active > >> ext4_orphan_get() > >> ext4_truncate() > >> ext4_block_truncate_page() > >> mark_buffer_dirty <--- 2. dirty inode > >> iput() > >> iput_final <--- 3. put into lru list > >> ext4_mark_recovery_complete <--- 4. failed and return error > >> sb->s_root = NULL; > >> deactivate_locked_super() > >> kill_block_super() > >> generic_shutdown_super() > >> <--- 5. did not evict_inodes > >> put_super() > >> __put_super() > >> <--- 6. put super block > >> > >> Because of the truncated inodes was dirty and will write them back later, it > >> will trigger use after free problem. Now the question is why we need to set > >> SB_ACTIVE bit when enable CONFIG_QUOTA below? > >> > >> #ifdef CONFIG_QUOTA > >> /* Needed for iput() to work correctly and not trash data */ > >> sb->s_flags |= SB_ACTIVE; > >> > >> This code was merged long long ago in v2.6.6, IIUC, it may not affect > >> the quota statistics it we evict inode directly in the last iput. > >> In order to slove this UAF problem, I'm not sure is there any side effect > >> if we just remove this code, or remove SB_ACTIVE and call evict_inodes() > >> in the error path of ext4_fill_super(). > >> > >> Could you give some suggestions? > > > > That's a very good question. I do remember that I've added this code back > > then because otherwise orphan cleanup was loosing updates to quota files. > > But you're right that now I don't see how that could be happening and it > > would be nice if we could get rid of this hack (and even better if it also > > fixes the problem you've found). I guess I'll just try and test this change > > with various quota configurations to see whether something still breaks or > > not. Thanks report! > > > > Thanks for taking time to look at this, is this change OK under your various > quota test cases? Yes, I did tests both with journalled quotas and with ext4 quota feature and the quota accounting was correct after orphan recovery. So just removing the SB_ACTIVE setting is fine AFAICT. Will you send a patch or should I do it? Honza -- Jan Kara SUSE Labs, CR