Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp1128110ybm; Fri, 29 May 2020 23:04:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzcBUjsTTlqCKHLwWPVlS4sE78XAi+Sq3c0huhxKPvALl9vboZ2zerz/L5Nn9TSbG7+cofL X-Received: by 2002:a17:906:4088:: with SMTP id u8mr11206335ejj.422.1590818675318; Fri, 29 May 2020 23:04:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590818675; cv=none; d=google.com; s=arc-20160816; b=uo04atuxTeKTBN56GyS4Q7HV/DF4IYr/o8j0iu6mqqJ/13JdSmYLtZiOxHbRWthaOz bxUhcgkKsXScxducn2jTMDb43nidHplCJ2BWiN9rMmnRkXPqQHnjru2x6wFoWLhpR14S LIP1fLdXzjbusvYi8MUgYi6cTN5F6IXQ6Hob2bz9bHI5kFXyfvasMRs11y9KTk9FPDfQ bQzsUZ2VA7gvrd3/WcNsmjE4MgF/L4FLaA5JJ+wLC57lkFMAMYd7KHMxRNiKpanEp28/ kEn/G1Ryj2CdwzYs81NDJjdBT2HsdDjkw93CSkWlPYp1vmcvmQucAALRKEDSIwrpq6lp 7eBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=kaH1dQ3tpKXfSi0PRh7oQMprHDmCj4cU5ZgxXsXO1Z0=; b=QLXAoM2/1rq4o2le09M8a7Dlcf4sD6uJPC2u8RmBBJiwp9O4WvGkbDh/wqtpY5p6vi AhvRqB5+z6B0fbsy9ddrVBslJ4q/UZit6+yb2BcgJQozLOkJ2CBwSQQhPCAQ+o5JIUVu ipJtnPyGy4J39gMUmzYRuHsUYTHlYBhdSAZJdJ39jwJSvvIlymhIOOKbI4+54RsC3HkW 24YNmMXvmgi/E/MdNojtFTY63R3bSNvg5tns5tT4O2Rpb9ocJIByq9Rgy1Bp+pzb57fO bInpHfRNAo1W8mwBlwFrbnQaegtVJeatoFwCp3cyi20bPRkPxHRcE3FIvKqpbJZYKxjG vUNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=lrrRsyP3; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s18si6898905ejh.293.2020.05.29.23.03.59; Fri, 29 May 2020 23:04:35 -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; dkim=pass header.i=@kernel.org header.s=default header.b=lrrRsyP3; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725929AbgE3GD5 (ORCPT + 99 others); Sat, 30 May 2020 02:03:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:39532 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725814AbgE3GD5 (ORCPT ); Sat, 30 May 2020 02:03:57 -0400 Received: from sol.hsd1.ca.comcast.net (c-107-3-166-239.hsd1.ca.comcast.net [107.3.166.239]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 433992074B; Sat, 30 May 2020 06:03:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590818636; bh=ZjvXFGk6rT4s5wC0wE1lNel4D/QY9srvqLSKGJm/CMQ=; h=From:To:Cc:Subject:Date:From; b=lrrRsyP3m82WxwlvHO9HOUuF4RzTghWlEE3l0OFetv2TQkg7uhz4TVNe0Bk6xs2VI 4CMZL/fywTNgYXdJLGngqdLfOz88VTgM3TcUTvC6d2FVhertFysLnyOoMAIan8eqST bmxOUtL38U5flFgxIvL1i8zsdpWJSVBHNcBujMjM= From: Eric Biggers To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, stable@vger.kernel.org, Al Viro , Daniel Rosenberg , Gabriel Krisman Bertazi Subject: [PATCH] ext4: avoid utf8_strncasecmp() with unstable name Date: Fri, 29 May 2020 23:02:16 -0700 Message-Id: <20200530060216.221456-1-ebiggers@kernel.org> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org 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; + } + return ext4_ci_compare(inode, name, &qstr, false); } -- 2.26.2