Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp204070ybl; Thu, 23 Jan 2020 21:50:33 -0800 (PST) X-Google-Smtp-Source: APXvYqyeeRws9vXIA/7trgBAi1QZ+gsEt9v/I3fkfpINQAcjkwALCa71RPLZtVPxc0MNZLZS+zN9 X-Received: by 2002:aca:a997:: with SMTP id s145mr1019217oie.71.1579845033244; Thu, 23 Jan 2020 21:50:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579845033; cv=none; d=google.com; s=arc-20160816; b=WTd7h2Lagb9rUc6rQmA9rEKFZrpt7E/zzezOT1M6rYjchh0/j+Oi/iiImX/vQTovRl NkkLmJBQnITwdvCTKZ63KlRUGu0/mu5ALCK7R/is7/PaA5H9DdyJ5RhIOL0P7JO4jyK4 wOSnQSbARB3SxDbe91eoAtAsyOlOYs5ucKd5ORt+UswnIyehsLcrroUNegB4ChpqVsIo 54ukOQb9IOb73tu5WHQo4MHucYLZS4jVP5Y+TZ3y/dXwXAdekJyWkOa9E8PXioOOIygn DzboMYHE3XuC7hKDUKSQnj6oqyJUDm9YSHDVBNBxHzJEMDuz9k7by+Cu1kqnmwetjei9 4DpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=KxVB/E86lNKNhPTU2/RM2/D0ts46IDAvVmg873JVtI4=; b=ReOIWgL8EjRWS30NH5Wf8qYNlt1qFfoYIvCNsgEAjWT9IN1QYb96Sm+vzdI0PiDx8N a9tA/3UjjST94rDZyDm2wWWFsn042w6ClFp3zb1c5/3zrfdlVNcEVenbS03BHENT2pLv frF/yF/Aqb0D0ZGwVOk4FH1euv+vPjwfsVLrVaOXkXiV5/ueZtx5VLsNWKCInULcyGwy EWiKEhNvqX+iJQldoYI1Bh5InByZJAdVEfb3Q51jgjMsbCmS1Y/NZXp0f7/0VF8xIyoI 190ZglIW/EzonKekVv1v7t9xmQwKhWNNMLJq3AxPGExSpXDpkrmPs7PtDEVSVho0txHH kOlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@aol.com header.s=a2048 header.b=J9TObx8d; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=aol.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l18si2259949oth.236.2020.01.23.21.50.13; Thu, 23 Jan 2020 21:50:33 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@aol.com header.s=a2048 header.b=J9TObx8d; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=aol.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726338AbgAXFEi (ORCPT + 99 others); Fri, 24 Jan 2020 00:04:38 -0500 Received: from sonic310-22.consmr.mail.gq1.yahoo.com ([98.137.69.148]:33005 "EHLO sonic310-22.consmr.mail.gq1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726300AbgAXFEh (ORCPT ); Fri, 24 Jan 2020 00:04:37 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aol.com; s=a2048; t=1579842276; bh=KxVB/E86lNKNhPTU2/RM2/D0ts46IDAvVmg873JVtI4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From:Subject; b=J9TObx8dEXgW7r+jATz5Yr4s7+z500v0s+p0CDTHFKdA73WjMIZOruK/shez9tLr/hBvzVDz+u0TK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CZCo.kDN6zq2weLm vjxw74U73JRTFVb0bWi2TMxWSJWP.dcyXaGyAmQ3jmd5_Tf8lDgOK3jf6iESV4T62w2VrP6xR2cO 9FN3aWwDMTu__52XTnQFlwZac2G00._Rx3JzWuQ8BTthW.Crl8RWFyfnm2bhk.ZfEjBLDQ3tv4Mz pmE7Lx61eSR46OzfqBD7INCLnP9g5HNHTug8vpXuCxza359Nk5t5Z7_ODqvCN_gyiRCvqAGsjPgR eC23JS3CpGagk7tcXhv4lOySQBng48w7csOecCr5EAK8djklj0crKAz9NbZgGS55q47ZSbEEh5uV ulqtzrz0mmPl0fJux3.ApAVOwpUu03rINtyqr4R7fe7fVBGE5aSa_tx7aLb2qBzuXFSvMie2qyxA MFvQhSzAhTGN0_akTcpm6KuGZbqGt4OURebaUVffRFXoERRSOFQJQIOuPZW2XA3ELlagAy_qZkH1 zWyDpETlchCSf0DUl_hpU5nD9o6rrY03LZfDqCF1G8C1l0RUgpxvp.4CQ1mUoOFDL93oXSkbjnwS DeOnHjBHxAkGJfexW8YD0mWiDo5y7ihu24KoLaBmD3bdD6PuP2mnYu9Y96Z_idwkl3IHLO2QpWku U8MKFwC7nHgeBhYJQwAqetIjP_3Zb7pql9YY1 Received: from sonic.gate.mail.ne1.yahoo.com by sonic310.consmr.mail.gq1.yahoo.com with HTTP; Fri, 24 Jan 2020 05:04:36 +0000 Received: by smtp405.mail.gq1.yahoo.com (Oath Hermes SMTP Server) with ESMTPA ID da8cd968291b387cfb3bcafe1576746f; Fri, 24 Jan 2020 05:04:32 +0000 (UTC) Date: Fri, 24 Jan 2020 13:04:25 +0800 From: Gao Xiang To: Eric Biggers Cc: linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, Alexander Viro , Daniel Rosenberg , Gabriel Krisman Bertazi Subject: Re: [PATCH] ext4: fix race conditions in ->d_compare() and ->d_hash() Message-ID: <20200124050423.GA31271@hsiangkao-HP-ZHAN-66-Pro-G1> References: <20200124041234.159740-1-ebiggers@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200124041234.159740-1-ebiggers@kernel.org> User-Agent: Mutt/1.10.1 (2018-07-13) X-Mailer: WebService/1.1.14873 hermes Apache-HttpAsyncClient/4.1.4 (Java/1.8.0_181) Content-Length: 2932 Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Hi Eric, On Thu, Jan 23, 2020 at 08:12:34PM -0800, Eric Biggers wrote: > From: Eric Biggers > > Since ->d_compare() and ->d_hash() can be called in RCU-walk mode, > ->d_parent and ->d_inode can be concurrently modified, and in > particular, ->d_inode may be changed to NULL. For ext4_d_hash() this > resulted in a reproducible NULL dereference if a lookup is done in a > directory being deleted, e.g. with: > > int main() > { > if (fork()) { > for (;;) { > mkdir("subdir", 0700); > rmdir("subdir"); > } > } else { > for (;;) > access("subdir/file", 0); > } > } > > ... or by running the 't_encrypted_d_revalidate' program from xfstests. > Both repros work in any directory on a filesystem with the encoding > feature, even if the directory doesn't actually have the casefold flag. > > I couldn't reproduce a crash in ext4_d_compare(), but it appears that a > similar crash is possible there. > > Fix these bugs by reading ->d_parent and ->d_inode using READ_ONCE() and > falling back to the case sensitive behavior if the inode is NULL. > > Reported-by: Al Viro > Fixes: b886ee3e778e ("ext4: Support case-insensitive file name lookups") > Cc: # v5.2+ > Signed-off-by: Eric Biggers > --- > fs/ext4/dir.c | 9 ++++++--- > 1 file changed, 6 insertions(+), 3 deletions(-) > > diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c > index 8964778aabefb..0129d14629881 100644 > --- a/fs/ext4/dir.c > +++ b/fs/ext4/dir.c > @@ -671,9 +671,11 @@ static int ext4_d_compare(const struct dentry *dentry, unsigned int len, > const char *str, const struct qstr *name) > { > struct qstr qstr = {.name = str, .len = len }; > - struct inode *inode = dentry->d_parent->d_inode; > + const struct dentry *parent = READ_ONCE(dentry->d_parent); I'm not sure if we really need READ_ONCE d_parent here (p.s. d_parent won't be NULL anyway), and d_seq will guard all its validity. If I'm wrong, correct me kindly... Otherwise, it looks good to me... Reviewed-by: Gao Xiang Thanks, Gao Xiang > + const struct inode *inode = READ_ONCE(parent->d_inode); > > - if (!IS_CASEFOLDED(inode) || !EXT4_SB(inode->i_sb)->s_encoding) { > + if (!inode || !IS_CASEFOLDED(inode) || > + !EXT4_SB(inode->i_sb)->s_encoding) { > if (len != name->len) > return -1; > return memcmp(str, name->name, len); > @@ -686,10 +688,11 @@ static int ext4_d_hash(const struct dentry *dentry, struct qstr *str) > { > const struct ext4_sb_info *sbi = EXT4_SB(dentry->d_sb); > const struct unicode_map *um = sbi->s_encoding; > + const struct inode *inode = READ_ONCE(dentry->d_inode); > unsigned char *norm; > int len, ret = 0; > > - if (!IS_CASEFOLDED(dentry->d_inode) || !um) > + if (!inode || !IS_CASEFOLDED(inode) || !um) > return 0; > > norm = kmalloc(PATH_MAX, GFP_ATOMIC); > -- > 2.25.0 >