Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp1135903ybm; Fri, 29 May 2020 23:20:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw7+jMa8Ih7qA0CAJwTI9zuzgKjHGZM9V+Y0tb7IFvtxM/cUspN0hNzQ5ZpLbdNZtzxaVRT X-Received: by 2002:a05:6402:22a6:: with SMTP id cx6mr11831977edb.76.1590819620006; Fri, 29 May 2020 23:20:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590819620; cv=none; d=google.com; s=arc-20160816; b=mDQStKjcwh6SFJECvp52Uwo2NMG+jVqbuS+eQYYY4Wy/AxX9zPBIAzQ5mZBtilPofu yAoCF79BrNLFTkiewsv9kv5makCXzeWILtStc/4PhZEUWxOsATweOToK51VV8ioZ/s+8 hTxiZR81Oc+LQHbzDiFK+UmQDJk3ogznlGIWlmUWmGLyneE8aMOQork6Dw+xImWQK8TN FgIEUxBk+y9oTpuV0qlTj2WsDicdFJb/0nXreyCEK4AGl1BYVs4S02You3pU/aCRFnUO /qkgkrshWMdg5Q1lC8Ek8VSI1ieAiCkHkPm4cwt96RS9FgccKcRPqLlZ731olWzJG917 whtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:message-id :in-reply-to:date:references:organization:subject:cc:to:from; bh=J0glF3H5946zPDWdiae7Gp/+QSDi4Qdumm0IQgdOR58=; b=VnrnkXVFRtd41+KYzE2Vf0BvhJgXTRFgNEkmpsw7h0zAvE3od+66BgaCzmeJOqSWMq XH2i4qD9PUb0UN6zshPKMXkc8h8pwhjnVrSYu+1pvKKUn1tqlJH8Cad2z3y+53dCGbuB stXv3rM8uz+EAQIhyqAvmT6FAQYAR6b+VsHEEQekolq2vIQj4+CmyW60c/yI2kuiMbOb YqWZYCtkUzt64h6XuKzn8fFNz7f3UMfoQpxd/xEYytRbykPs2A0DFU5U6mJAz06OJbk7 mablbk2RE1Wo+10STCRTWCrOgHu5nj/bGYwfDaok+wFHNsdfzdWTiU2Q+2HySN/ymPXX JdBQ== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id do11si8701705ejc.450.2020.05.29.23.19.52; Fri, 29 May 2020 23:20:19 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728706AbgE3GRL (ORCPT + 99 others); Sat, 30 May 2020 02:17:11 -0400 Received: from bhuna.collabora.co.uk ([46.235.227.227]:47558 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728657AbgE3GRL (ORCPT ); Sat, 30 May 2020 02:17:11 -0400 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: krisman) with ESMTPSA id 121BA2A49A0 From: Gabriel Krisman Bertazi To: Eric Biggers Cc: linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, stable@vger.kernel.org, Al Viro , Daniel Rosenberg , Gabriel Krisman Bertazi Subject: Re: [PATCH] ext4: avoid utf8_strncasecmp() with unstable name Organization: Collabora References: <20200530060216.221456-1-ebiggers@kernel.org> Date: Sat, 30 May 2020 02:17:02 -0400 In-Reply-To: <20200530060216.221456-1-ebiggers@kernel.org> (Eric Biggers's message of "Fri, 29 May 2020 23:02:16 -0700") Message-ID: <85d06mkkv5.fsf@collabora.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Eric Biggers writes: > From: Eric Biggers > > If the dentry name passed to ->d_compare() fits in dentry::d_iname, then > it may be concurrently modified by a rename. This can cause undefined > behavior (possibly out-of-bounds memory accesses or crashes) in > utf8_strncasecmp(), since fs/unicode/ isn't written to handle strings > that may be concurrently modified. > > Fix this by first copying the filename to a stack buffer if needed. > This way we get a stable snapshot of the filename. > > Fixes: b886ee3e778e ("ext4: Support case-insensitive file name lookups") > Cc: # v5.2+ > Cc: Al Viro > Cc: Daniel Rosenberg > Cc: Gabriel Krisman Bertazi > Signed-off-by: Eric Biggers > --- > fs/ext4/dir.c | 17 +++++++++++++++++ > 1 file changed, 17 insertions(+) > > diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c > index c654205f648dd..19aef8328bb18 100644 > --- a/fs/ext4/dir.c > +++ b/fs/ext4/dir.c > @@ -675,6 +675,7 @@ static int ext4_d_compare(const struct dentry *dentry, unsigned int len, > struct qstr qstr = {.name = str, .len = len }; > const struct dentry *parent = READ_ONCE(dentry->d_parent); > const struct inode *inode = READ_ONCE(parent->d_inode); > + char strbuf[DNAME_INLINE_LEN]; > > if (!inode || !IS_CASEFOLDED(inode) || > !EXT4_SB(inode->i_sb)->s_encoding) { > @@ -683,6 +684,22 @@ static int ext4_d_compare(const struct dentry *dentry, unsigned int len, > return memcmp(str, name->name, len); > } > > + /* > + * If the dentry name is stored in-line, then it may be concurrently > + * modified by a rename. If this happens, the VFS will eventually retry > + * the lookup, so it doesn't matter what ->d_compare() returns. > + * However, it's unsafe to call utf8_strncasecmp() with an unstable > + * string. Therefore, we have to copy the name into a temporary buffer. > + */ > + if (len <= DNAME_INLINE_LEN - 1) { > + unsigned int i; > + > + for (i = 0; i < len; i++) > + strbuf[i] = READ_ONCE(str[i]); > + strbuf[len] = 0; > + qstr.name = strbuf; > + } > + Could we avoid this if the casefolded version were cached in the dentry? Then we could use utf8_strncasecmp_folded which would be safe. Would this be acceptable for vfs? > return ext4_ci_compare(inode, name, &qstr, false); > } -- Gabriel Krisman Bertazi