Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2068290pxu; Tue, 24 Nov 2020 16:28:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJwb4YnOmrDSziy+oavMH4GQiXZAn14lMAdfKccJKh37NvBpceXuWE4ocuwCNhzFX7LV+AQ/ X-Received: by 2002:a17:906:e214:: with SMTP id gf20mr170267ejb.314.1606264097686; Tue, 24 Nov 2020 16:28:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606264097; cv=none; d=google.com; s=arc-20160816; b=vdTOC1J3tIbSCwUmTvpXhSCapy1j+pkPIkBd0Rlk8z986tLWVjNNoAxG+LPZ5h7hUi AK6zPoRRUjlz9cUs89KmbrPp3tnJ45hHsNGhIHd1iAJqkKEdKN8bPMrgyGI9XVFgjuUj GiHqaPtYK8m05I4vqpp58glPvKQ/mygYEpMzgPs70wbNdoSus3us4os1oQ7zSUpYvWLT 5q/AoEUsPct4RWxZ3fzTJmtETAp2tFC5CeVQjjlDf+epVu9detRb6Ll8K8Jx2D6g1ykw shY76be8g7PkNRqFH+EqUoLVWGjEvrMcTgMBwh4DSClwX7t8dQwmouwxko8SizFdlNZ7 UbQw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=EB6jokd6Nuo8xsVZiwurXH6vlJGHPVbLxVVa5n8JaRs=; b=AcflySdRsEZ/MXtYfcoAYx3fPSSz3kBhBwQkVatFauIg27+crodaWhirRtXJ/Ur5UX Tg4pl2a2nR5z5xp/xIZ5hvjDe+/ayowohWoyfWR/wW1yyIyRuLaLBZKL8BrJ+tHGZxf7 nOYikm70u2h4SzJgmOuLZluwPEbBGDP7fiNXL6AgRBAIbzJMuha4GZr/mocZb5/EcGT0 E7+63+IbfQmvYX7tE8ztyZzNzu5wXSdkzgUNqsuPywZJiwasqpZPrnWXy6CktJh0PDpV uT6DXN2dRz0HWBDPTu/pXMimzB4gJmXU0QcSlx2CAdPwSj3tTHJXCzltiyDz+ZXjjouf 4DVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Eb010EAp; 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 q26si328934ejb.105.2020.11.24.16.27.51; Tue, 24 Nov 2020 16:28:17 -0800 (PST) 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=Eb010EAp; 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 S1727347AbgKYAYz (ORCPT + 99 others); Tue, 24 Nov 2020 19:24:55 -0500 Received: from mail.kernel.org ([198.145.29.99]:35442 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727078AbgKYAYr (ORCPT ); Tue, 24 Nov 2020 19:24:47 -0500 Received: from sol.attlocal.net (172-10-235-113.lightspeed.sntcca.sbcglobal.net [172.10.235.113]) (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 082D6216C4; Wed, 25 Nov 2020 00:24:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1606263887; bh=fcM8tYFzSoGvarGTs3vXIKCmW4Asap3xvHg2/A974So=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Eb010EAp3vBsiChxefsvvZATamnn1O8zchIBp8dbCWYjSa4Hr8sgYhl8IweTMQetb EPBkMSTPNi/Izt3NR8Qx0fvS8pwwF7Y6Vel7K7xgGp2l6gAaBc31tC7zshg2v7eZis 6wOwF5w75J+V9aTea5m14m9nihQdh0seQ4Wa1s20= From: Eric Biggers To: linux-fscrypt@vger.kernel.org Cc: linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-mtd@lists.infradead.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 5/9] fscrypt: introduce fscrypt_prepare_readdir() Date: Tue, 24 Nov 2020 16:23:32 -0800 Message-Id: <20201125002336.274045-6-ebiggers@kernel.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201125002336.274045-1-ebiggers@kernel.org> References: <20201125002336.274045-1-ebiggers@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: Eric Biggers The last remaining use of fscrypt_get_encryption_info() from filesystems is for readdir (->iterate_shared()). Every other call is now in fs/crypto/ as part of some other higher-level operation. We need to add a new argument to fscrypt_get_encryption_info() to indicate whether the encryption policy to allowed to be unrecognized or not. Doing this is easier if we can work with high-level operations rather than direct filesystem use of fscrypt_get_encryption_info(). So add a function fscrypt_prepare_readdir() which wraps the call to fscrypt_get_encryption_info() for the readdir use case. Signed-off-by: Eric Biggers --- fs/crypto/hooks.c | 6 ++++++ fs/ext4/dir.c | 8 +++----- fs/ext4/namei.c | 2 +- fs/f2fs/dir.c | 2 +- fs/ubifs/dir.c | 2 +- include/linux/fscrypt.h | 24 ++++++++++++++++++++++++ 6 files changed, 36 insertions(+), 8 deletions(-) diff --git a/fs/crypto/hooks.c b/fs/crypto/hooks.c index c809a4afa057..82f351d3113a 100644 --- a/fs/crypto/hooks.c +++ b/fs/crypto/hooks.c @@ -114,6 +114,12 @@ int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry, } EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup); +int __fscrypt_prepare_readdir(struct inode *dir) +{ + return fscrypt_get_encryption_info(dir); +} +EXPORT_SYMBOL_GPL(__fscrypt_prepare_readdir); + /** * fscrypt_prepare_setflags() - prepare to change flags with FS_IOC_SETFLAGS * @inode: the inode on which flags are being changed diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c index 16bfbdd5007c..c6d16353326a 100644 --- a/fs/ext4/dir.c +++ b/fs/ext4/dir.c @@ -118,11 +118,9 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx) struct buffer_head *bh = NULL; struct fscrypt_str fstr = FSTR_INIT(NULL, 0); - if (IS_ENCRYPTED(inode)) { - err = fscrypt_get_encryption_info(inode); - if (err) - return err; - } + err = fscrypt_prepare_readdir(inode); + if (err) + return err; if (is_dx_dir(inode)) { err = ext4_dx_readdir(file, ctx); diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 7b31aea3e025..5fa8436cd5fa 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1004,7 +1004,7 @@ static int htree_dirblock_to_tree(struct file *dir_file, EXT4_DIR_REC_LEN(0)); /* Check if the directory is encrypted */ if (IS_ENCRYPTED(dir)) { - err = fscrypt_get_encryption_info(dir); + err = fscrypt_prepare_readdir(dir); if (err < 0) { brelse(bh); return err; diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c index 47bee953fc8d..049500f1e764 100644 --- a/fs/f2fs/dir.c +++ b/fs/f2fs/dir.c @@ -1022,7 +1022,7 @@ static int f2fs_readdir(struct file *file, struct dir_context *ctx) int err = 0; if (IS_ENCRYPTED(inode)) { - err = fscrypt_get_encryption_info(inode); + err = fscrypt_prepare_readdir(inode); if (err) goto out; diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c index 009fbf844d3e..1f33a5598b93 100644 --- a/fs/ubifs/dir.c +++ b/fs/ubifs/dir.c @@ -514,7 +514,7 @@ static int ubifs_readdir(struct file *file, struct dir_context *ctx) return 0; if (encrypted) { - err = fscrypt_get_encryption_info(dir); + err = fscrypt_prepare_readdir(dir); if (err) return err; diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h index 0c9e64969b73..8cbb26f55695 100644 --- a/include/linux/fscrypt.h +++ b/include/linux/fscrypt.h @@ -242,6 +242,7 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry, unsigned int flags); int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry, struct fscrypt_name *fname); +int __fscrypt_prepare_readdir(struct inode *dir); int fscrypt_prepare_setflags(struct inode *inode, unsigned int oldflags, unsigned int flags); int fscrypt_prepare_symlink(struct inode *dir, const char *target, @@ -537,6 +538,11 @@ static inline int __fscrypt_prepare_lookup(struct inode *dir, return -EOPNOTSUPP; } +static inline int __fscrypt_prepare_readdir(struct inode *dir) +{ + return -EOPNOTSUPP; +} + static inline int fscrypt_prepare_setflags(struct inode *inode, unsigned int oldflags, unsigned int flags) @@ -795,6 +801,24 @@ static inline int fscrypt_prepare_lookup(struct inode *dir, return 0; } +/** + * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory + * @dir: the directory inode + * + * If the directory is encrypted and it doesn't already have its encryption key + * set up, try to set it up so that the filenames will be listed in plaintext + * form rather than in no-key form. + * + * Return: 0 on success; -errno on error. Note that the encryption key being + * unavailable is not considered an error. + */ +static inline int fscrypt_prepare_readdir(struct inode *dir) +{ + if (IS_ENCRYPTED(dir)) + return __fscrypt_prepare_readdir(dir); + return 0; +} + /** * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's * attributes -- 2.29.2