1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Ceph fscrypt functionality
9 #include <crypto/sha2.h>
10 #include <linux/fscrypt.h>
12 #define CEPH_FSCRYPT_BLOCK_SHIFT 12
13 #define CEPH_FSCRYPT_BLOCK_SIZE (_AC(1, UL) << CEPH_FSCRYPT_BLOCK_SHIFT)
14 #define CEPH_FSCRYPT_BLOCK_MASK (~(CEPH_FSCRYPT_BLOCK_SIZE-1))
16 struct ceph_fs_client;
17 struct ceph_acl_sec_ctx;
18 struct ceph_mds_request;
22 char *name; // b64 encoded, possibly hashed
23 unsigned char *ctext; // binary crypttext (if any)
24 u32 name_len; // length of name buffer
25 u32 ctext_len; // length of crypttext
30 * Header for the crypted file when truncating the size, this
31 * will be sent to MDS, and the MDS will update the encrypted
32 * last block and then truncate the size.
34 struct ceph_fscrypt_truncate_size_header {
39 * It will be sizeof(assert_ver + file_offset + block_size)
40 * if the last block is empty when it's located in a file
41 * hole. Or the data_len will plus CEPH_FSCRYPT_BLOCK_SIZE.
50 struct ceph_fscrypt_auth {
53 u8 cfa_blob[FSCRYPT_SET_CONTEXT_MAX_SIZE];
56 #define CEPH_FSCRYPT_AUTH_VERSION 1
57 static inline u32 ceph_fscrypt_auth_len(struct ceph_fscrypt_auth *fa)
59 u32 ctxsize = le32_to_cpu(fa->cfa_blob_len);
61 return offsetof(struct ceph_fscrypt_auth, cfa_blob) + ctxsize;
64 #ifdef CONFIG_FS_ENCRYPTION
66 * We want to encrypt filenames when creating them, but the encrypted
67 * versions of those names may have illegal characters in them. To mitigate
68 * that, we base64 encode them, but that gives us a result that can exceed
71 * Follow a similar scheme to fscrypt itself, and cap the filename to a
72 * smaller size. If the ciphertext name is longer than the value below, then
73 * sha256 hash the remaining bytes.
75 * For the fscrypt_nokey_name struct the dirhash[2] member is useless in ceph
76 * so the corresponding struct will be:
78 * struct fscrypt_ceph_nokey_name {
80 * u8 sha256[SHA256_DIGEST_SIZE];
81 * }; // 180 bytes => 240 bytes base64-encoded, which is <= NAME_MAX (255)
83 * (240 bytes is the maximum size allowed for snapshot names to take into
84 * account the format: '_<SNAPSHOT-NAME>_<INODE-NUMBER>'.)
86 * Note that for long names that end up having their tail portion hashed, we
87 * must also store the full encrypted name (in the dentry's alternate_name
90 #define CEPH_NOHASH_NAME_MAX (180 - SHA256_DIGEST_SIZE)
92 #define CEPH_BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
94 int ceph_base64_encode(const u8 *src, int srclen, char *dst);
95 int ceph_base64_decode(const char *src, int srclen, u8 *dst);
97 void ceph_fscrypt_set_ops(struct super_block *sb);
99 void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc);
101 int ceph_fscrypt_prepare_context(struct inode *dir, struct inode *inode,
102 struct ceph_acl_sec_ctx *as);
103 void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req,
104 struct ceph_acl_sec_ctx *as);
105 int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name,
107 int ceph_encode_encrypted_fname(struct inode *parent, struct dentry *dentry,
110 static inline int ceph_fname_alloc_buffer(struct inode *parent,
111 struct fscrypt_str *fname)
113 if (!IS_ENCRYPTED(parent))
115 return fscrypt_fname_alloc_buffer(NAME_MAX, fname);
118 static inline void ceph_fname_free_buffer(struct inode *parent,
119 struct fscrypt_str *fname)
121 if (IS_ENCRYPTED(parent))
122 fscrypt_fname_free_buffer(fname);
125 int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
126 struct fscrypt_str *oname, bool *is_nokey);
127 int ceph_fscrypt_prepare_readdir(struct inode *dir);
129 static inline unsigned int ceph_fscrypt_blocks(u64 off, u64 len)
131 /* crypto blocks cannot span more than one page */
132 BUILD_BUG_ON(CEPH_FSCRYPT_BLOCK_SHIFT > PAGE_SHIFT);
134 return ((off+len+CEPH_FSCRYPT_BLOCK_SIZE-1) >> CEPH_FSCRYPT_BLOCK_SHIFT) -
135 (off >> CEPH_FSCRYPT_BLOCK_SHIFT);
139 * If we have an encrypted inode then we must adjust the offset and
140 * range of the on-the-wire read to cover an entire encryption block.
141 * The copy will be done using the original offset and length, after
142 * we've decrypted the result.
144 static inline void ceph_fscrypt_adjust_off_and_len(struct inode *inode,
147 if (IS_ENCRYPTED(inode)) {
148 *len = ceph_fscrypt_blocks(*off, *len) * CEPH_FSCRYPT_BLOCK_SIZE;
149 *off &= CEPH_FSCRYPT_BLOCK_MASK;
153 int ceph_fscrypt_decrypt_block_inplace(const struct inode *inode,
154 struct page *page, unsigned int len,
155 unsigned int offs, u64 lblk_num);
156 int ceph_fscrypt_encrypt_block_inplace(const struct inode *inode,
157 struct page *page, unsigned int len,
158 unsigned int offs, u64 lblk_num,
160 int ceph_fscrypt_decrypt_pages(struct inode *inode, struct page **page,
162 int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page,
163 u64 off, struct ceph_sparse_extent *map,
165 int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
168 static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
170 return fscrypt_is_bounce_page(page) ? fscrypt_pagecache_page(page) : page;
173 #else /* CONFIG_FS_ENCRYPTION */
175 static inline void ceph_fscrypt_set_ops(struct super_block *sb)
179 static inline void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc)
183 static inline int ceph_fscrypt_prepare_context(struct inode *dir,
185 struct ceph_acl_sec_ctx *as)
187 if (IS_ENCRYPTED(dir))
192 static inline void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req,
193 struct ceph_acl_sec_ctx *as_ctx)
197 static inline int ceph_encode_encrypted_dname(struct inode *parent,
198 struct qstr *d_name, char *buf)
200 memcpy(buf, d_name->name, d_name->len);
204 static inline int ceph_encode_encrypted_fname(struct inode *parent,
205 struct dentry *dentry, char *buf)
210 static inline int ceph_fname_alloc_buffer(struct inode *parent,
211 struct fscrypt_str *fname)
216 static inline void ceph_fname_free_buffer(struct inode *parent,
217 struct fscrypt_str *fname)
221 static inline int ceph_fname_to_usr(const struct ceph_fname *fname,
222 struct fscrypt_str *tname,
223 struct fscrypt_str *oname, bool *is_nokey)
225 oname->name = fname->name;
226 oname->len = fname->name_len;
230 static inline int ceph_fscrypt_prepare_readdir(struct inode *dir)
235 static inline void ceph_fscrypt_adjust_off_and_len(struct inode *inode,
240 static inline int ceph_fscrypt_decrypt_block_inplace(const struct inode *inode,
241 struct page *page, unsigned int len,
242 unsigned int offs, u64 lblk_num)
247 static inline int ceph_fscrypt_encrypt_block_inplace(const struct inode *inode,
248 struct page *page, unsigned int len,
249 unsigned int offs, u64 lblk_num,
255 static inline int ceph_fscrypt_decrypt_pages(struct inode *inode,
256 struct page **page, u64 off,
262 static inline int ceph_fscrypt_decrypt_extents(struct inode *inode,
263 struct page **page, u64 off,
264 struct ceph_sparse_extent *map,
270 static inline int ceph_fscrypt_encrypt_pages(struct inode *inode,
271 struct page **page, u64 off,
277 static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
281 #endif /* CONFIG_FS_ENCRYPTION */
283 static inline loff_t ceph_fscrypt_page_offset(struct page *page)
285 return page_offset(ceph_fscrypt_pagecache_page(page));
288 #endif /* _CEPH_CRYPTO_H */