All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: ceph-devel@vger.kernel.org
Cc: xiubli@redhat.com, idryomov@gmail.com, lhenriques@suse.de,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v12 06/54] ceph: crypto context handling for ceph
Date: Thu, 31 Mar 2022 11:30:42 -0400	[thread overview]
Message-ID: <20220331153130.41287-7-jlayton@kernel.org> (raw)
In-Reply-To: <20220331153130.41287-1-jlayton@kernel.org>

Have set_context do a setattr that sets the fscrypt_auth value, and
get_context just return the contents of that field.

Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/ceph/Makefile |  1 +
 fs/ceph/crypto.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ceph/crypto.h | 29 ++++++++++++++++++
 fs/ceph/inode.c  |  3 ++
 fs/ceph/super.c  |  3 ++
 5 files changed, 112 insertions(+)
 create mode 100644 fs/ceph/crypto.c
 create mode 100644 fs/ceph/crypto.h

diff --git a/fs/ceph/Makefile b/fs/ceph/Makefile
index 50c635dc7f71..1f77ca04c426 100644
--- a/fs/ceph/Makefile
+++ b/fs/ceph/Makefile
@@ -12,3 +12,4 @@ ceph-y := super.o inode.o dir.o file.o locks.o addr.o ioctl.o \
 
 ceph-$(CONFIG_CEPH_FSCACHE) += cache.o
 ceph-$(CONFIG_CEPH_FS_POSIX_ACL) += acl.o
+ceph-$(CONFIG_FS_ENCRYPTION) += crypto.o
diff --git a/fs/ceph/crypto.c b/fs/ceph/crypto.c
new file mode 100644
index 000000000000..a513ff373b13
--- /dev/null
+++ b/fs/ceph/crypto.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/ceph/ceph_debug.h>
+#include <linux/xattr.h>
+#include <linux/fscrypt.h>
+
+#include "super.h"
+#include "crypto.h"
+
+static int ceph_crypt_get_context(struct inode *inode, void *ctx, size_t len)
+{
+	struct ceph_inode_info *ci = ceph_inode(inode);
+	struct ceph_fscrypt_auth *cfa = (struct ceph_fscrypt_auth *)ci->fscrypt_auth;
+	u32 ctxlen;
+
+	/* Non existent or too short? */
+	if (!cfa || (ci->fscrypt_auth_len < (offsetof(struct ceph_fscrypt_auth, cfa_blob) + 1)))
+		return -ENOBUFS;
+
+	/* Some format we don't recognize? */
+	if (le32_to_cpu(cfa->cfa_version) != CEPH_FSCRYPT_AUTH_VERSION)
+		return -ENOBUFS;
+
+	ctxlen = le32_to_cpu(cfa->cfa_blob_len);
+	if (len < ctxlen)
+		return -ERANGE;
+
+	memcpy(ctx, cfa->cfa_blob, ctxlen);
+	return ctxlen;
+}
+
+static int ceph_crypt_set_context(struct inode *inode, const void *ctx, size_t len, void *fs_data)
+{
+	int ret;
+	struct iattr attr = { };
+	struct ceph_iattr cia = { };
+	struct ceph_fscrypt_auth *cfa;
+
+	WARN_ON_ONCE(fs_data);
+
+	if (len > FSCRYPT_SET_CONTEXT_MAX_SIZE)
+		return -EINVAL;
+
+	cfa = kzalloc(sizeof(*cfa), GFP_KERNEL);
+	if (!cfa)
+		return -ENOMEM;
+
+	cfa->cfa_version = cpu_to_le32(CEPH_FSCRYPT_AUTH_VERSION);
+	cfa->cfa_blob_len = cpu_to_le32(len);
+	memcpy(cfa->cfa_blob, ctx, len);
+
+	cia.fscrypt_auth = cfa;
+
+	ret = __ceph_setattr(inode, &attr, &cia);
+	if (ret == 0)
+		inode_set_flags(inode, S_ENCRYPTED, S_ENCRYPTED);
+	kfree(cia.fscrypt_auth);
+	return ret;
+}
+
+static bool ceph_crypt_empty_dir(struct inode *inode)
+{
+	struct ceph_inode_info *ci = ceph_inode(inode);
+
+	return ci->i_rsubdirs + ci->i_rfiles == 1;
+}
+
+static struct fscrypt_operations ceph_fscrypt_ops = {
+	.get_context		= ceph_crypt_get_context,
+	.set_context		= ceph_crypt_set_context,
+	.empty_dir		= ceph_crypt_empty_dir,
+};
+
+void ceph_fscrypt_set_ops(struct super_block *sb)
+{
+	fscrypt_set_ops(sb, &ceph_fscrypt_ops);
+}
diff --git a/fs/ceph/crypto.h b/fs/ceph/crypto.h
new file mode 100644
index 000000000000..6c3831c57c8d
--- /dev/null
+++ b/fs/ceph/crypto.h
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Ceph fscrypt functionality
+ */
+
+#ifndef _CEPH_CRYPTO_H
+#define _CEPH_CRYPTO_H
+
+#include <linux/fscrypt.h>
+
+struct ceph_fscrypt_auth {
+	__le32	cfa_version;
+	__le32	cfa_blob_len;
+	u8	cfa_blob[FSCRYPT_SET_CONTEXT_MAX_SIZE];
+} __packed;
+
+#ifdef CONFIG_FS_ENCRYPTION
+#define CEPH_FSCRYPT_AUTH_VERSION	1
+void ceph_fscrypt_set_ops(struct super_block *sb);
+
+#else /* CONFIG_FS_ENCRYPTION */
+
+static inline void ceph_fscrypt_set_ops(struct super_block *sb)
+{
+}
+
+#endif /* CONFIG_FS_ENCRYPTION */
+
+#endif
diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c
index bff2f31c3a56..9b333e9966f0 100644
--- a/fs/ceph/inode.c
+++ b/fs/ceph/inode.c
@@ -14,10 +14,12 @@
 #include <linux/random.h>
 #include <linux/sort.h>
 #include <linux/iversion.h>
+#include <linux/fscrypt.h>
 
 #include "super.h"
 #include "mds_client.h"
 #include "cache.h"
+#include "crypto.h"
 #include <linux/ceph/decode.h>
 
 /*
@@ -644,6 +646,7 @@ void ceph_evict_inode(struct inode *inode)
 	clear_inode(inode);
 
 	ceph_fscache_unregister_inode_cookie(ci);
+	fscrypt_put_encryption_info(inode);
 
 	__ceph_remove_caps(ci);
 
diff --git a/fs/ceph/super.c b/fs/ceph/super.c
index a859921bbe96..52ff78f0462a 100644
--- a/fs/ceph/super.c
+++ b/fs/ceph/super.c
@@ -20,6 +20,7 @@
 #include "super.h"
 #include "mds_client.h"
 #include "cache.h"
+#include "crypto.h"
 
 #include <linux/ceph/ceph_features.h>
 #include <linux/ceph/decode.h>
@@ -1128,6 +1129,8 @@ static int ceph_set_super(struct super_block *s, struct fs_context *fc)
 	s->s_time_min = 0;
 	s->s_time_max = U32_MAX;
 
+	ceph_fscrypt_set_ops(s);
+
 	ret = set_anon_super_fc(s, fc);
 	if (ret != 0)
 		fsc->sb = NULL;
-- 
2.35.1


  parent reply	other threads:[~2022-03-31 15:32 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-31 15:30 [PATCH v12 00/54] ceph+fscrypt: fully-working prototype Jeff Layton
2022-03-31 15:30 ` [PATCH v12 01/54] vfs: export new_inode_pseudo Jeff Layton
2022-03-31 19:50   ` Al Viro
2022-03-31 22:23     ` Jeff Layton
2022-03-31 15:30 ` [PATCH v12 02/54] fscrypt: export fscrypt_base64url_encode and fscrypt_base64url_decode Jeff Layton
2022-03-31 15:30 ` [PATCH v12 03/54] fscrypt: export fscrypt_fname_encrypt and fscrypt_fname_encrypted_size Jeff Layton
2022-03-31 15:30 ` [PATCH v12 04/54] fscrypt: add fscrypt_context_for_new_inode Jeff Layton
2022-03-31 15:30 ` [PATCH v12 05/54] ceph: preallocate inode for ops that may create one Jeff Layton
2022-03-31 15:30 ` Jeff Layton [this message]
2022-03-31 15:30 ` [PATCH v12 07/54] ceph: support legacy v1 encryption policy keysetup Jeff Layton
2022-03-31 20:16   ` Eric Biggers
2022-04-01 10:22     ` Luís Henriques
2022-03-31 15:30 ` [PATCH v12 08/54] ceph: add a has_stable_inodes operation for ceph Jeff Layton
2022-03-31 20:03   ` Eric Biggers
2022-04-01 10:37     ` Jeff Layton
2022-04-01 18:16       ` Eric Biggers
2022-04-01 18:51         ` Jeff Layton
2022-03-31 15:30 ` [PATCH v12 09/54] ceph: ensure that we accept a new context from MDS for new inodes Jeff Layton
2022-03-31 15:30 ` [PATCH v12 10/54] ceph: add support for fscrypt_auth/fscrypt_file to cap messages Jeff Layton
2022-03-31 15:30 ` [PATCH v12 11/54] ceph: add ability to set fscrypt_auth via setattr Jeff Layton
2022-03-31 15:30 ` [PATCH v12 12/54] ceph: implement -o test_dummy_encryption mount option Jeff Layton
2022-03-31 15:30 ` [PATCH v12 13/54] ceph: decode alternate_name in lease info Jeff Layton
2022-03-31 15:30 ` [PATCH v12 14/54] ceph: add fscrypt ioctls Jeff Layton
2022-03-31 15:30 ` [PATCH v12 15/54] ceph: make the ioctl cmd more readable in debug log Jeff Layton
2022-03-31 15:30 ` [PATCH v12 16/54] ceph: make ceph_msdc_build_path use ref-walk Jeff Layton
2022-03-31 15:30 ` [PATCH v12 17/54] ceph: add encrypted fname handling to ceph_mdsc_build_path Jeff Layton
2022-03-31 15:30 ` [PATCH v12 18/54] ceph: send altname in MClientRequest Jeff Layton
2022-03-31 15:30 ` [PATCH v12 19/54] ceph: encode encrypted name in dentry release Jeff Layton
2022-03-31 15:30 ` [PATCH v12 20/54] ceph: properly set DCACHE_NOKEY_NAME flag in lookup Jeff Layton
2022-03-31 15:30 ` [PATCH v12 21/54] ceph: set DCACHE_NOKEY_NAME in atomic open Jeff Layton
2022-03-31 15:30 ` [PATCH v12 22/54] ceph: make d_revalidate call fscrypt revalidator for encrypted dentries Jeff Layton
2022-03-31 15:30 ` [PATCH v12 23/54] ceph: add helpers for converting names for userland presentation Jeff Layton
2022-03-31 15:31 ` [PATCH v12 24/54] ceph: fix base64 encoded name's length check in ceph_fname_to_usr() Jeff Layton
2022-03-31 15:31 ` [PATCH v12 25/54] ceph: add fscrypt support to ceph_fill_trace Jeff Layton
2022-03-31 15:31 ` [PATCH v12 26/54] ceph: pass the request to parse_reply_info_readdir() Jeff Layton
2022-03-31 15:31 ` [PATCH v12 27/54] ceph: add ceph_encode_encrypted_dname() helper Jeff Layton
2022-03-31 15:31 ` [PATCH v12 28/54] ceph: add support to readdir for encrypted filenames Jeff Layton
2022-03-31 15:31 ` [PATCH v12 29/54] ceph: create symlinks with encrypted and base64-encoded targets Jeff Layton
2022-03-31 15:31 ` [PATCH v12 30/54] ceph: make ceph_get_name decrypt filenames Jeff Layton
2022-03-31 15:31 ` [PATCH v12 31/54] ceph: add a new ceph.fscrypt.auth vxattr Jeff Layton
2022-03-31 15:31 ` [PATCH v12 32/54] ceph: add some fscrypt guardrails Jeff Layton
2022-03-31 15:31 ` [PATCH v12 33/54] ceph: don't allow changing layout on encrypted files/directories Jeff Layton
2022-03-31 15:31 ` [PATCH v12 34/54] libceph: add CEPH_OSD_OP_ASSERT_VER support Jeff Layton
2022-03-31 15:31 ` [PATCH v12 35/54] ceph: size handling for encrypted inodes in cap updates Jeff Layton
2022-03-31 15:31 ` [PATCH v12 36/54] ceph: fscrypt_file field handling in MClientRequest messages Jeff Layton
2022-03-31 15:31 ` [PATCH v12 37/54] ceph: get file size from fscrypt_file when present in inode traces Jeff Layton
2022-03-31 15:31 ` [PATCH v12 38/54] ceph: handle fscrypt fields in cap messages from MDS Jeff Layton
2022-03-31 15:31 ` [PATCH v12 39/54] ceph: add __ceph_get_caps helper support Jeff Layton
2022-03-31 15:31 ` [PATCH v12 40/54] ceph: add __ceph_sync_read " Jeff Layton
2022-03-31 15:31 ` [PATCH v12 41/54] ceph: add object version support for sync read Jeff Layton
2022-03-31 15:31 ` [PATCH v12 42/54] ceph: add infrastructure for file encryption and decryption Jeff Layton
2022-03-31 15:31 ` [PATCH v12 43/54] ceph: add truncate size handling support for fscrypt Jeff Layton
2022-03-31 15:31 ` [PATCH v12 44/54] libceph: allow ceph_osdc_new_request to accept a multi-op read Jeff Layton
2022-03-31 15:31 ` [PATCH v12 45/54] ceph: disable fallocate for encrypted inodes Jeff Layton
2022-03-31 15:31 ` [PATCH v12 46/54] ceph: disable copy offload on " Jeff Layton
2022-03-31 15:31 ` [PATCH v12 47/54] ceph: don't use special DIO path for " Jeff Layton
2022-03-31 15:31 ` [PATCH v12 48/54] ceph: align data in pages in ceph_sync_write Jeff Layton
2022-03-31 15:31 ` [PATCH v12 49/54] ceph: add read/modify/write to ceph_sync_write Jeff Layton
2022-03-31 15:31 ` [PATCH v12 50/54] ceph: plumb in decryption during sync reads Jeff Layton
2022-03-31 15:31 ` [PATCH v12 51/54] ceph: add fscrypt decryption support to ceph_netfs_issue_op Jeff Layton
2022-03-31 15:31 ` [PATCH v12 52/54] ceph: set i_blkbits to crypto block size for encrypted inodes Jeff Layton
2022-03-31 15:31 ` [PATCH v12 53/54] ceph: add encryption support to writepage Jeff Layton
2022-03-31 15:31 ` [PATCH v12 54/54] ceph: fscrypt support for writepages Jeff Layton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220331153130.41287-7-jlayton@kernel.org \
    --to=jlayton@kernel.org \
    --cc=ceph-devel@vger.kernel.org \
    --cc=idryomov@gmail.com \
    --cc=lhenriques@suse.de \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=xiubli@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.