From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.0 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB112C433DF for ; Tue, 23 Jun 2020 20:20:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A4599206C3 for ; Tue, 23 Jun 2020 20:20:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592943650; bh=Y/euwcTHW76+GV8HNgM6r8X11HguTT8I2SsQJLqj9/w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=zp83lKZxH1+v7wSlpi3jIiPWmAHez9G6QN50qIgVRU0bqs9fT/aeX1CHQXylHeefv uYmbS7ECksVlaCulRG4srjZ7ZLw8Se2j+r00XjUMRyjXKMstK5haoJxioHgQ2plKtq rBD+hc++7HC30d10KV6is1PkZF0iYakc87m4K63Q= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389972AbgFWUUs (ORCPT ); Tue, 23 Jun 2020 16:20:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:38108 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389674AbgFWUUm (ORCPT ); Tue, 23 Jun 2020 16:20:42 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5F1622078A; Tue, 23 Jun 2020 20:20:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592943641; bh=Y/euwcTHW76+GV8HNgM6r8X11HguTT8I2SsQJLqj9/w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xXfPpFU0TZv9PWvVa85/oOA+dnBaJako+VpDtKVXcuqS0qzCWKMlwkapzCObfcawW 0+JPTDAdbiWXZLE6GU8aHbqnlqOkWEHaRQtUCugeUMGLXn6C5KPi7ihxyN43p8vlie vRjC7S1Ko0dI4vgaSS0PWYb8WCgGleB631kY2Z/I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Al Viro , Daniel Rosenberg , Gabriel Krisman Bertazi , Eric Biggers , Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 5.7 440/477] f2fs: avoid utf8_strncasecmp() with unstable name Date: Tue, 23 Jun 2020 21:57:17 +0200 Message-Id: <20200623195428.330571320@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200623195407.572062007@linuxfoundation.org> References: <20200623195407.572062007@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Biggers [ Upstream commit fc3bb095ab02b9e7d89a069ade2cead15c64c504 ] 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 Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/dir.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c index 44eb12a00cd0e..54e90dbb09e78 100644 --- a/fs/f2fs/dir.c +++ b/fs/f2fs/dir.c @@ -1076,11 +1076,27 @@ static int f2fs_d_compare(const struct dentry *dentry, unsigned int len, const struct inode *dir = READ_ONCE(parent->d_inode); const struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); struct qstr entry = QSTR_INIT(str, len); + char strbuf[DNAME_INLINE_LEN]; int res; if (!dir || !IS_CASEFOLDED(dir)) goto fallback; + /* + * 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) { + memcpy(strbuf, str, len); + strbuf[len] = 0; + entry.name = strbuf; + /* prevent compiler from optimizing out the temporary buffer */ + barrier(); + } + res = utf8_strncasecmp(sbi->s_encoding, name, &entry); if (res >= 0) return res; -- 2.25.1