Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp1128538ybm; Fri, 29 May 2020 23:05:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxq+PQVWDqD+B1MPijVJrqHzAS1pRvc6vOIzmTxlezVgm208SAdLlJNchDE9BxvHWHQIaqL X-Received: by 2002:a50:f052:: with SMTP id u18mr11548123edl.16.1590818720712; Fri, 29 May 2020 23:05:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590818720; cv=none; d=google.com; s=arc-20160816; b=u/f7XNamxFBHESmfqQx0rcVKDq+KeXLULrr5/0Ec59hmLeKJ2GpeqEIs13JbwZog+Z 5virRQBqbXAqMLrtWPza6sTtehzz/i9ZLU73Q8JUbFzlGLEHv4J+07K5lqaNbF1LbMgj 4Tf6NJepho4AJfghWwkjhQsp5meN/MjZDjAWF7TW0GaDEIqT+VUF0rYY4sO1HYz79C+1 bZSFoNEuJXaZli0fiPz3ZqjRIbL/+1OV2cSXVJKHHO5lIXX/19vfgYJg3QQiC2l451S0 bmnKR2M5eC4LdOKmoFza9Qweb8OwU4qRWpyjhmdzDvgzLAdvEx2GNycQnmn1k/rkDoGp zdJg== 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=rwm7t1eZAitmhCLo5d4rSXmwSazMFohhqjdZfVSOSLM=; b=f8/dezRPdjzbBpuo4J48NHCMzPcZHxfXu3kmvwQkhja6GZTthu0atlnP8Ag535EKBE qfOcSQpJGQuMLFa16xTrKAnoIBvdUXM6d+70QBor8/J4BAEuZxUOCQKcX8qC4JGhsa2b 8QsDEbrDzvxKP6eVzFhoWRT58NNVQcTZeGyn0QQJlCakGJoMAaIfGYEILUfGo+583bmS kItGF+YqPdJDR+DZR+gn5Up7dg1EjiNRWsIhEHG/LKq+J7U9St58mj4ZDsnJ8oes7jW2 TSV/iXBM0LIEBAIQRkP4qJPkbBf+23uKLXFD6NH+LO7gzaKecM98TDYU3bIQeU+WAvKb k5qQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="hwAUZ/ck"; 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 a20si6885343edr.516.2020.05.29.23.04.58; Fri, 29 May 2020 23:05:20 -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="hwAUZ/ck"; 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 S1728130AbgE3GEl (ORCPT + 99 others); Sat, 30 May 2020 02:04:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:39618 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725814AbgE3GEk (ORCPT ); Sat, 30 May 2020 02:04:40 -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 19B042074B; Sat, 30 May 2020 06:04:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590818680; bh=f42mP4uZFz68CGLoJzgxTIkgVm0ozHVg79XnQ76VYrw=; h=From:To:Cc:Subject:Date:From; b=hwAUZ/ckiy8+KY/KQA+ZB0NnCd4BzfTTXTIbUq75mFZmHL/ki9nixoGKsmDjoVexY vLa3tIHIldvD1NjDbp0A8tfmInodKPkv53MjuwqJ3MUf7Qrd/u/szji1LMLgqEjWxx wSzw1gkRdsj2itmgCti97l+5gm1703Q0bmgbo5NQ= From: Eric Biggers To: linux-f2fs-devel@lists.sourceforge.net Cc: linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org, stable@vger.kernel.org, Al Viro , Daniel Rosenberg , Gabriel Krisman Bertazi Subject: [PATCH] f2fs: avoid utf8_strncasecmp() with unstable name Date: Fri, 29 May 2020 23:04:18 -0700 Message-Id: <20200530060418.221707-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: 2c2eb7a300cd ("f2fs: Support case-insensitive file name lookups") Cc: # v5.4+ Cc: Al Viro Cc: Daniel Rosenberg Cc: Gabriel Krisman Bertazi Signed-off-by: Eric Biggers --- fs/f2fs/dir.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c index 44bfc464df787..5c179b72eb8a8 100644 --- a/fs/f2fs/dir.c +++ b/fs/f2fs/dir.c @@ -1083,6 +1083,7 @@ static int f2fs_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)) { if (len != name->len) @@ -1090,6 +1091,22 @@ static int f2fs_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 f2fs_ci_compare(inode, name, &qstr, false); } -- 2.26.2