diff mbox series

[RFC,08/13] fscrypt: save session key credentials for extent infos

Message ID fd61bc691a89a48a5cf8ae5417c607d3d945d198.1693630890.git.sweettea-kernel@dorminy.me (mailing list archive)
State Superseded
Headers show
Series fscrypt: add extent encryption | expand

Commit Message

Sweet Tea Dorminy Sept. 2, 2023, 5:54 a.m. UTC
For v1 encryption policies using per-session keys, the thread which
opens the inode and therefore initializes the encryption info is part of
the session, so it can get the key from the session keyring. However,
for extent encryption, the extent infos are likely loaded from a
different thread, which does not have access to the session keyring.
This change saves the credentials of the inode opening thread and reuses
those credentials temporarily when dealing with extent infos, allowing
finding the encryption key correctly.

v1 encryption policies using per-session keys should probably not exist
for new usages such as extent encryption, but this makes more tests
work without change; maybe the right answer is to disallow v1 session
keys plus extent encryption and deal with editing tests to not use v1
session encryption so much.

Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
---
 fs/crypto/fscrypt_private.h |  9 +++++++++
 fs/crypto/keysetup.c        | 19 +++++++++++++++++++
 2 files changed, 28 insertions(+)
diff mbox series

Patch

diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
index 9320428f8915..fe48b61a524b 100644
--- a/fs/crypto/fscrypt_private.h
+++ b/fs/crypto/fscrypt_private.h
@@ -284,6 +284,15 @@  struct fscrypt_common_info {
 struct fscrypt_info {
 	struct fscrypt_common_info info;
 
+	/* Credential struct from the thread which created this info. This is
+	 * only used in v1 session keyrings with extent encryption; it allows
+	 * the thread creating extents for an inode to join the session
+	 * keyring temporarily, since otherwise the thread is usually part of
+	 * kernel writeback and therefore unrelated to the thread with the
+	 * right session key.
+	 */
+	struct cred *ci_session_creds;
+
 	/*
 	 * This inode's hash key for filenames.  This is a 128-bit SipHash-2-4
 	 * key.  This is only set for directories that use a keyed dirhash over
diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
index 4146b1380cb5..5e944ec4e36f 100644
--- a/fs/crypto/keysetup.c
+++ b/fs/crypto/keysetup.c
@@ -619,6 +619,9 @@  static void put_crypt_inode_info(struct fscrypt_info *ci)
 	free_prepared_key(&ci->info);
 	remove_info_from_mk_decrypted_list(&ci->info);
 
+	if (ci->ci_session_creds)
+		abort_creds(ci->ci_session_creds);
+
 	memzero_explicit(ci, sizeof(*ci));
 	kmem_cache_free(fscrypt_inode_info_cachep, ci);
 }
@@ -727,6 +730,9 @@  fscrypt_setup_encryption_info(struct inode *inode,
 	if (res)
 		goto out;
 
+	if (!mk)
+		crypt_inode_info->ci_session_creds = prepare_creds();
+
 	/*
 	 * Derive a secret dirhash key for directories that need it. It
 	 * should be impossible to set flags such that a v1 policy sets
@@ -979,6 +985,7 @@  fscrypt_setup_extent_info(struct inode *inode,
 	struct fscrypt_extent_info *crypt_extent_info;
 	struct fscrypt_common_info *crypt_info;
 	struct fscrypt_master_key *mk = NULL;
+	const struct cred *creds = NULL;
 	int res;
 
 	crypt_extent_info = kmem_cache_zalloc(fscrypt_extent_info_cachep,
@@ -987,8 +994,20 @@  fscrypt_setup_extent_info(struct inode *inode,
 		return -ENOMEM;
 	crypt_info = &crypt_extent_info->info;
 
+	if (inode->i_crypt_info->ci_session_creds) {
+		/*
+		 * The inode this is being created for is using a session key,
+		 * so we have to join this thread to that session temporarily
+		 * in order to be able to find the right key...
+		 */
+		creds = override_creds(inode->i_crypt_info->ci_session_creds);
+	}
+
 	res = fscrypt_setup_common_info(crypt_info, inode, policy, nonce,
 					CI_EXTENT, &mk);
+	if (creds)
+		revert_creds(creds);
+
 	if (res)
 		goto out;