Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp13931310pxu; Mon, 4 Jan 2021 08:15:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJxtt52L9LneRRK2XlqHCJJEdOX0BKbYgIdxpW/eXjkI5gCLeuI4898rGHIysmc11sBc2WRx X-Received: by 2002:a17:907:3e85:: with SMTP id hs5mr69139161ejc.548.1609776932304; Mon, 04 Jan 2021 08:15:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609776932; cv=none; d=google.com; s=arc-20160816; b=a5Bs5fBzfRWKiu5Lgsavk3r1KUZALx8o0QSgmGnixQBrbRuKhbdvoXJW+E7WfU8v9v YLSpH2CKJ8T4oD5Mp+yutcoxrohqVLIh4gb2R5GOBlCnr8MaT92AbkuQMrGoPZZYrjkz 81M9j9EvNm3hCdKzqqx/9r64RdJnKvizM3FtgiiSiNLnnkAsfzdUsbrJ8JmtzjDL46ul tGpgqZSuYiuO3u4GvziBcCTkz704u9nvMPBPW13whpNNnaPnRxs8AXrwIr9yMrx//MFN kJ9AoerWDcTAzUj5Iu80usawSZvozCC09avvN+3TzpFM1olJiIHEOcJYYSHJ1AqUMst/ +WZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=BE/eGRG5sH3lI2EhHn6d5OsHZFGw6ju701iSC593pQE=; b=QdTH+/4ZH+SmiQffFgCT5yE22vL95FseMdCX7SVJ3m7qEMbP/EXC9w61OZutFETZoD Z6TTtgwfZH20O2iYX87+sD3Fdmg+7mtTa+tFTXxbKsefFehlaZgpMZPZ7AHgwaqatLYK wMYFQWI+CwvVEnviYdOKC4eNYS8ynkLV79zyxQEhwlBVELW48q3EGFOVGZYGtDl+ZbgO KwPu6rqjvS+WLcMrCmJ0B8EIja/Db7o6oWwHsJD8P+JJUsE+Pr2r5xg9Zl3UMkYuXx/D 4BH6gWhF+YLWO4tO8EraY0+VxiMYqLDd9h931erpcttJuTt/rBwCplGcqNi6Vt/I8Gk5 qZ0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=PYaeoU7X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lr15si27218189ejb.657.2021.01.04.08.15.08; Mon, 04 Jan 2021 08:15:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=PYaeoU7X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729673AbhADQL6 (ORCPT + 99 others); Mon, 4 Jan 2021 11:11:58 -0500 Received: from mail.kernel.org ([198.145.29.99]:36474 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728217AbhADQAI (ORCPT ); Mon, 4 Jan 2021 11:00:08 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 743692250F; Mon, 4 Jan 2021 15:59:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609775983; bh=1EWEb1iXr1V32zzy4+C603sUjUw475m7Utdlem6Ti+8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PYaeoU7XKuPSwsN1yEH0agTSvyZwg7m44m5eAfBM61ghs78JcqC6cjpVIEleWWY3j 9lrgCEDQM3cEnAbQ5UcHcjXwgk+ZM4kcGJR9Z4ANHz5Ym4hLNhCf+vVWIKA4U0E+rZ Kye+75VFJMwJIy10sYPamwMdtw3lkk2qWFnLdCWA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Eric Biggers Subject: [PATCH 5.4 07/47] fscrypt: add fscrypt_is_nokey_name() Date: Mon, 4 Jan 2021 16:57:06 +0100 Message-Id: <20210104155706.102878430@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210104155705.740576914@linuxfoundation.org> References: <20210104155705.740576914@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Biggers commit 159e1de201b6fca10bfec50405a3b53a561096a8 upstream. It's possible to create a duplicate filename in an encrypted directory by creating a file concurrently with adding the encryption key. Specifically, sys_open(O_CREAT) (or sys_mkdir(), sys_mknod(), or sys_symlink()) can lookup the target filename while the directory's encryption key hasn't been added yet, resulting in a negative no-key dentry. The VFS then calls ->create() (or ->mkdir(), ->mknod(), or ->symlink()) because the dentry is negative. Normally, ->create() would return -ENOKEY due to the directory's key being unavailable. However, if the key was added between the dentry lookup and ->create(), then the filesystem will go ahead and try to create the file. If the target filename happens to already exist as a normal name (not a no-key name), a duplicate filename may be added to the directory. In order to fix this, we need to fix the filesystems to prevent ->create(), ->mkdir(), ->mknod(), and ->symlink() on no-key names. (->rename() and ->link() need it too, but those are already handled correctly by fscrypt_prepare_rename() and fscrypt_prepare_link().) In preparation for this, add a helper function fscrypt_is_nokey_name() that filesystems can use to do this check. Use this helper function for the existing checks that fs/crypto/ does for rename and link. Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20201118075609.120337-2-ebiggers@kernel.org Signed-off-by: Eric Biggers Signed-off-by: Greg Kroah-Hartman --- fs/crypto/hooks.c | 10 +++++----- include/linux/fscrypt.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) --- a/fs/crypto/hooks.c +++ b/fs/crypto/hooks.c @@ -58,8 +58,8 @@ int __fscrypt_prepare_link(struct inode if (err) return err; - /* ... in case we looked up ciphertext name before key was added */ - if (dentry->d_flags & DCACHE_ENCRYPTED_NAME) + /* ... in case we looked up no-key name before key was added */ + if (fscrypt_is_nokey_name(dentry)) return -ENOKEY; if (!fscrypt_has_permitted_context(dir, inode)) @@ -83,9 +83,9 @@ int __fscrypt_prepare_rename(struct inod if (err) return err; - /* ... in case we looked up ciphertext name(s) before key was added */ - if ((old_dentry->d_flags | new_dentry->d_flags) & - DCACHE_ENCRYPTED_NAME) + /* ... in case we looked up no-key name(s) before key was added */ + if (fscrypt_is_nokey_name(old_dentry) || + fscrypt_is_nokey_name(new_dentry)) return -ENOKEY; if (old_dir != new_dir) { --- a/include/linux/fscrypt.h +++ b/include/linux/fscrypt.h @@ -100,6 +100,35 @@ static inline void fscrypt_handle_d_move dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME; } +/** + * fscrypt_is_nokey_name() - test whether a dentry is a no-key name + * @dentry: the dentry to check + * + * This returns true if the dentry is a no-key dentry. A no-key dentry is a + * dentry that was created in an encrypted directory that hasn't had its + * encryption key added yet. Such dentries may be either positive or negative. + * + * When a filesystem is asked to create a new filename in an encrypted directory + * and the new filename's dentry is a no-key dentry, it must fail the operation + * with ENOKEY. This includes ->create(), ->mkdir(), ->mknod(), ->symlink(), + * ->rename(), and ->link(). (However, ->rename() and ->link() are already + * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().) + * + * This is necessary because creating a filename requires the directory's + * encryption key, but just checking for the key on the directory inode during + * the final filesystem operation doesn't guarantee that the key was available + * during the preceding dentry lookup. And the key must have already been + * available during the dentry lookup in order for it to have been checked + * whether the filename already exists in the directory and for the new file's + * dentry not to be invalidated due to it incorrectly having the no-key flag. + * + * Return: %true if the dentry is a no-key name + */ +static inline bool fscrypt_is_nokey_name(const struct dentry *dentry) +{ + return dentry->d_flags & DCACHE_ENCRYPTED_NAME; +} + /* crypto.c */ extern void fscrypt_enqueue_decrypt_work(struct work_struct *); extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t); @@ -290,6 +319,11 @@ static inline void fscrypt_handle_d_move { } +static inline bool fscrypt_is_nokey_name(const struct dentry *dentry) +{ + return false; +} + /* crypto.c */ static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work) {