xref: /openbmc/linux/fs/ceph/crypto.c (revision 985b9ee8)
12d332d5bSJeff Layton // SPDX-License-Identifier: GPL-2.0
264e86f63SLuís Henriques /*
364e86f63SLuís Henriques  * The base64 encode/decode code was copied from fscrypt:
464e86f63SLuís Henriques  * Copyright (C) 2015, Google, Inc.
564e86f63SLuís Henriques  * Copyright (C) 2015, Motorola Mobility
664e86f63SLuís Henriques  * Written by Uday Savagaonkar, 2014.
764e86f63SLuís Henriques  * Modified by Jaegeuk Kim, 2015.
864e86f63SLuís Henriques  */
92d332d5bSJeff Layton #include <linux/ceph/ceph_debug.h>
102d332d5bSJeff Layton #include <linux/xattr.h>
112d332d5bSJeff Layton #include <linux/fscrypt.h>
1277cdb7e1SJeff Layton #include <linux/ceph/striper.h>
132d332d5bSJeff Layton 
142d332d5bSJeff Layton #include "super.h"
156b5717bdSJeff Layton #include "mds_client.h"
162d332d5bSJeff Layton #include "crypto.h"
172d332d5bSJeff Layton 
1864e86f63SLuís Henriques /*
1964e86f63SLuís Henriques  * The base64url encoding used by fscrypt includes the '_' character, which may
2064e86f63SLuís Henriques  * cause problems in snapshot names (which can not start with '_').  Thus, we
2164e86f63SLuís Henriques  * used the base64 encoding defined for IMAP mailbox names (RFC 3501) instead,
2264e86f63SLuís Henriques  * which replaces '-' and '_' by '+' and ','.
2364e86f63SLuís Henriques  */
2464e86f63SLuís Henriques static const char base64_table[65] =
2564e86f63SLuís Henriques 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
2664e86f63SLuís Henriques 
ceph_base64_encode(const u8 * src,int srclen,char * dst)2764e86f63SLuís Henriques int ceph_base64_encode(const u8 *src, int srclen, char *dst)
2864e86f63SLuís Henriques {
2964e86f63SLuís Henriques 	u32 ac = 0;
3064e86f63SLuís Henriques 	int bits = 0;
3164e86f63SLuís Henriques 	int i;
3264e86f63SLuís Henriques 	char *cp = dst;
3364e86f63SLuís Henriques 
3464e86f63SLuís Henriques 	for (i = 0; i < srclen; i++) {
3564e86f63SLuís Henriques 		ac = (ac << 8) | src[i];
3664e86f63SLuís Henriques 		bits += 8;
3764e86f63SLuís Henriques 		do {
3864e86f63SLuís Henriques 			bits -= 6;
3964e86f63SLuís Henriques 			*cp++ = base64_table[(ac >> bits) & 0x3f];
4064e86f63SLuís Henriques 		} while (bits >= 6);
4164e86f63SLuís Henriques 	}
4264e86f63SLuís Henriques 	if (bits)
4364e86f63SLuís Henriques 		*cp++ = base64_table[(ac << (6 - bits)) & 0x3f];
4464e86f63SLuís Henriques 	return cp - dst;
4564e86f63SLuís Henriques }
4664e86f63SLuís Henriques 
ceph_base64_decode(const char * src,int srclen,u8 * dst)4764e86f63SLuís Henriques int ceph_base64_decode(const char *src, int srclen, u8 *dst)
4864e86f63SLuís Henriques {
4964e86f63SLuís Henriques 	u32 ac = 0;
5064e86f63SLuís Henriques 	int bits = 0;
5164e86f63SLuís Henriques 	int i;
5264e86f63SLuís Henriques 	u8 *bp = dst;
5364e86f63SLuís Henriques 
5464e86f63SLuís Henriques 	for (i = 0; i < srclen; i++) {
5564e86f63SLuís Henriques 		const char *p = strchr(base64_table, src[i]);
5664e86f63SLuís Henriques 
5764e86f63SLuís Henriques 		if (p == NULL || src[i] == 0)
5864e86f63SLuís Henriques 			return -1;
5964e86f63SLuís Henriques 		ac = (ac << 6) | (p - base64_table);
6064e86f63SLuís Henriques 		bits += 6;
6164e86f63SLuís Henriques 		if (bits >= 8) {
6264e86f63SLuís Henriques 			bits -= 8;
6364e86f63SLuís Henriques 			*bp++ = (u8)(ac >> bits);
6464e86f63SLuís Henriques 		}
6564e86f63SLuís Henriques 	}
6664e86f63SLuís Henriques 	if (ac & ((1 << bits) - 1))
6764e86f63SLuís Henriques 		return -1;
6864e86f63SLuís Henriques 	return bp - dst;
6964e86f63SLuís Henriques }
7064e86f63SLuís Henriques 
ceph_crypt_get_context(struct inode * inode,void * ctx,size_t len)712d332d5bSJeff Layton static int ceph_crypt_get_context(struct inode *inode, void *ctx, size_t len)
722d332d5bSJeff Layton {
732d332d5bSJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
742d332d5bSJeff Layton 	struct ceph_fscrypt_auth *cfa = (struct ceph_fscrypt_auth *)ci->fscrypt_auth;
752d332d5bSJeff Layton 	u32 ctxlen;
762d332d5bSJeff Layton 
772d332d5bSJeff Layton 	/* Non existent or too short? */
782d332d5bSJeff Layton 	if (!cfa || (ci->fscrypt_auth_len < (offsetof(struct ceph_fscrypt_auth, cfa_blob) + 1)))
792d332d5bSJeff Layton 		return -ENOBUFS;
802d332d5bSJeff Layton 
812d332d5bSJeff Layton 	/* Some format we don't recognize? */
822d332d5bSJeff Layton 	if (le32_to_cpu(cfa->cfa_version) != CEPH_FSCRYPT_AUTH_VERSION)
832d332d5bSJeff Layton 		return -ENOBUFS;
842d332d5bSJeff Layton 
852d332d5bSJeff Layton 	ctxlen = le32_to_cpu(cfa->cfa_blob_len);
862d332d5bSJeff Layton 	if (len < ctxlen)
872d332d5bSJeff Layton 		return -ERANGE;
882d332d5bSJeff Layton 
892d332d5bSJeff Layton 	memcpy(ctx, cfa->cfa_blob, ctxlen);
902d332d5bSJeff Layton 	return ctxlen;
912d332d5bSJeff Layton }
922d332d5bSJeff Layton 
ceph_crypt_set_context(struct inode * inode,const void * ctx,size_t len,void * fs_data)932d332d5bSJeff Layton static int ceph_crypt_set_context(struct inode *inode, const void *ctx,
942d332d5bSJeff Layton 				  size_t len, void *fs_data)
952d332d5bSJeff Layton {
962d332d5bSJeff Layton 	int ret;
972d332d5bSJeff Layton 	struct iattr attr = { };
982d332d5bSJeff Layton 	struct ceph_iattr cia = { };
992d332d5bSJeff Layton 	struct ceph_fscrypt_auth *cfa;
1002d332d5bSJeff Layton 
1012d332d5bSJeff Layton 	WARN_ON_ONCE(fs_data);
1022d332d5bSJeff Layton 
1032d332d5bSJeff Layton 	if (len > FSCRYPT_SET_CONTEXT_MAX_SIZE)
1042d332d5bSJeff Layton 		return -EINVAL;
1052d332d5bSJeff Layton 
1062d332d5bSJeff Layton 	cfa = kzalloc(sizeof(*cfa), GFP_KERNEL);
1072d332d5bSJeff Layton 	if (!cfa)
1082d332d5bSJeff Layton 		return -ENOMEM;
1092d332d5bSJeff Layton 
1102d332d5bSJeff Layton 	cfa->cfa_version = cpu_to_le32(CEPH_FSCRYPT_AUTH_VERSION);
1112d332d5bSJeff Layton 	cfa->cfa_blob_len = cpu_to_le32(len);
1122d332d5bSJeff Layton 	memcpy(cfa->cfa_blob, ctx, len);
1132d332d5bSJeff Layton 
1142d332d5bSJeff Layton 	cia.fscrypt_auth = cfa;
1152d332d5bSJeff Layton 
1162d332d5bSJeff Layton 	ret = __ceph_setattr(inode, &attr, &cia);
1172d332d5bSJeff Layton 	if (ret == 0)
1182d332d5bSJeff Layton 		inode_set_flags(inode, S_ENCRYPTED, S_ENCRYPTED);
1192d332d5bSJeff Layton 	kfree(cia.fscrypt_auth);
1202d332d5bSJeff Layton 	return ret;
1212d332d5bSJeff Layton }
1222d332d5bSJeff Layton 
ceph_crypt_empty_dir(struct inode * inode)1232d332d5bSJeff Layton static bool ceph_crypt_empty_dir(struct inode *inode)
1242d332d5bSJeff Layton {
1252d332d5bSJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
1262d332d5bSJeff Layton 
1272d332d5bSJeff Layton 	return ci->i_rsubdirs + ci->i_rfiles == 1;
1282d332d5bSJeff Layton }
1292d332d5bSJeff Layton 
ceph_get_dummy_policy(struct super_block * sb)1306b5717bdSJeff Layton static const union fscrypt_policy *ceph_get_dummy_policy(struct super_block *sb)
1316b5717bdSJeff Layton {
132985b9ee8SXiubo Li 	return ceph_sb_to_fs_client(sb)->fsc_dummy_enc_policy.policy;
1336b5717bdSJeff Layton }
1346b5717bdSJeff Layton 
1352d332d5bSJeff Layton static struct fscrypt_operations ceph_fscrypt_ops = {
1362d332d5bSJeff Layton 	.get_context		= ceph_crypt_get_context,
1372d332d5bSJeff Layton 	.set_context		= ceph_crypt_set_context,
1386b5717bdSJeff Layton 	.get_dummy_policy	= ceph_get_dummy_policy,
1392d332d5bSJeff Layton 	.empty_dir		= ceph_crypt_empty_dir,
1402d332d5bSJeff Layton };
1412d332d5bSJeff Layton 
ceph_fscrypt_set_ops(struct super_block * sb)1422d332d5bSJeff Layton void ceph_fscrypt_set_ops(struct super_block *sb)
1432d332d5bSJeff Layton {
1442d332d5bSJeff Layton 	fscrypt_set_ops(sb, &ceph_fscrypt_ops);
1452d332d5bSJeff Layton }
1466b5717bdSJeff Layton 
ceph_fscrypt_free_dummy_policy(struct ceph_fs_client * fsc)1476b5717bdSJeff Layton void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc)
1486b5717bdSJeff Layton {
1496b5717bdSJeff Layton 	fscrypt_free_dummy_policy(&fsc->fsc_dummy_enc_policy);
1506b5717bdSJeff Layton }
1516b5717bdSJeff Layton 
ceph_fscrypt_prepare_context(struct inode * dir,struct inode * inode,struct ceph_acl_sec_ctx * as)1526b5717bdSJeff Layton int ceph_fscrypt_prepare_context(struct inode *dir, struct inode *inode,
1536b5717bdSJeff Layton 				 struct ceph_acl_sec_ctx *as)
1546b5717bdSJeff Layton {
1556b5717bdSJeff Layton 	int ret, ctxsize;
1566b5717bdSJeff Layton 	bool encrypted = false;
1576b5717bdSJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
1586b5717bdSJeff Layton 
1596b5717bdSJeff Layton 	ret = fscrypt_prepare_new_inode(dir, inode, &encrypted);
1606b5717bdSJeff Layton 	if (ret)
1616b5717bdSJeff Layton 		return ret;
1626b5717bdSJeff Layton 	if (!encrypted)
1636b5717bdSJeff Layton 		return 0;
1646b5717bdSJeff Layton 
1656b5717bdSJeff Layton 	as->fscrypt_auth = kzalloc(sizeof(*as->fscrypt_auth), GFP_KERNEL);
1666b5717bdSJeff Layton 	if (!as->fscrypt_auth)
1676b5717bdSJeff Layton 		return -ENOMEM;
1686b5717bdSJeff Layton 
1696b5717bdSJeff Layton 	ctxsize = fscrypt_context_for_new_inode(as->fscrypt_auth->cfa_blob,
1706b5717bdSJeff Layton 						inode);
1716b5717bdSJeff Layton 	if (ctxsize < 0)
1726b5717bdSJeff Layton 		return ctxsize;
1736b5717bdSJeff Layton 
1746b5717bdSJeff Layton 	as->fscrypt_auth->cfa_version = cpu_to_le32(CEPH_FSCRYPT_AUTH_VERSION);
1756b5717bdSJeff Layton 	as->fscrypt_auth->cfa_blob_len = cpu_to_le32(ctxsize);
1766b5717bdSJeff Layton 
1776b5717bdSJeff Layton 	WARN_ON_ONCE(ci->fscrypt_auth);
1786b5717bdSJeff Layton 	kfree(ci->fscrypt_auth);
1796b5717bdSJeff Layton 	ci->fscrypt_auth_len = ceph_fscrypt_auth_len(as->fscrypt_auth);
1806b5717bdSJeff Layton 	ci->fscrypt_auth = kmemdup(as->fscrypt_auth, ci->fscrypt_auth_len,
1816b5717bdSJeff Layton 				   GFP_KERNEL);
1826b5717bdSJeff Layton 	if (!ci->fscrypt_auth)
1836b5717bdSJeff Layton 		return -ENOMEM;
1846b5717bdSJeff Layton 
1856b5717bdSJeff Layton 	inode->i_flags |= S_ENCRYPTED;
1866b5717bdSJeff Layton 
1876b5717bdSJeff Layton 	return 0;
1886b5717bdSJeff Layton }
1896b5717bdSJeff Layton 
ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request * req,struct ceph_acl_sec_ctx * as)1906b5717bdSJeff Layton void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req,
1916b5717bdSJeff Layton 				struct ceph_acl_sec_ctx *as)
1926b5717bdSJeff Layton {
1936b5717bdSJeff Layton 	swap(req->r_fscrypt_auth, as->fscrypt_auth);
1946b5717bdSJeff Layton }
1953fd945a7SJeff Layton 
196dd66df00SLuís Henriques /*
197dd66df00SLuís Henriques  * User-created snapshots can't start with '_'.  Snapshots that start with this
198dd66df00SLuís Henriques  * character are special (hint: there aren't real snapshots) and use the
199dd66df00SLuís Henriques  * following format:
200dd66df00SLuís Henriques  *
201dd66df00SLuís Henriques  *   _<SNAPSHOT-NAME>_<INODE-NUMBER>
202dd66df00SLuís Henriques  *
203dd66df00SLuís Henriques  * where:
204dd66df00SLuís Henriques  *  - <SNAPSHOT-NAME> - the real snapshot name that may need to be decrypted,
205dd66df00SLuís Henriques  *  - <INODE-NUMBER> - the inode number (in decimal) for the actual snapshot
206dd66df00SLuís Henriques  *
207dd66df00SLuís Henriques  * This function parses these snapshot names and returns the inode
208dd66df00SLuís Henriques  * <INODE-NUMBER>.  'name_len' will also bet set with the <SNAPSHOT-NAME>
209dd66df00SLuís Henriques  * length.
210dd66df00SLuís Henriques  */
parse_longname(const struct inode * parent,const char * name,int * name_len)211dd66df00SLuís Henriques static struct inode *parse_longname(const struct inode *parent,
212dd66df00SLuís Henriques 				    const char *name, int *name_len)
2133fd945a7SJeff Layton {
214dd66df00SLuís Henriques 	struct inode *dir = NULL;
215dd66df00SLuís Henriques 	struct ceph_vino vino = { .snap = CEPH_NOSNAP };
216dd66df00SLuís Henriques 	char *inode_number;
217dd66df00SLuís Henriques 	char *name_end;
218dd66df00SLuís Henriques 	int orig_len = *name_len;
219dd66df00SLuís Henriques 	int ret = -EIO;
220dd66df00SLuís Henriques 
221dd66df00SLuís Henriques 	/* Skip initial '_' */
222dd66df00SLuís Henriques 	name++;
223dd66df00SLuís Henriques 	name_end = strrchr(name, '_');
224dd66df00SLuís Henriques 	if (!name_end) {
225dd66df00SLuís Henriques 		dout("Failed to parse long snapshot name: %s\n", name);
226dd66df00SLuís Henriques 		return ERR_PTR(-EIO);
227dd66df00SLuís Henriques 	}
228dd66df00SLuís Henriques 	*name_len = (name_end - name);
229dd66df00SLuís Henriques 	if (*name_len <= 0) {
230dd66df00SLuís Henriques 		pr_err("Failed to parse long snapshot name\n");
231dd66df00SLuís Henriques 		return ERR_PTR(-EIO);
232dd66df00SLuís Henriques 	}
233dd66df00SLuís Henriques 
234dd66df00SLuís Henriques 	/* Get the inode number */
235dd66df00SLuís Henriques 	inode_number = kmemdup_nul(name_end + 1,
236dd66df00SLuís Henriques 				   orig_len - *name_len - 2,
237dd66df00SLuís Henriques 				   GFP_KERNEL);
238dd66df00SLuís Henriques 	if (!inode_number)
239dd66df00SLuís Henriques 		return ERR_PTR(-ENOMEM);
240dd66df00SLuís Henriques 	ret = kstrtou64(inode_number, 10, &vino.ino);
241dd66df00SLuís Henriques 	if (ret) {
242dd66df00SLuís Henriques 		dout("Failed to parse inode number: %s\n", name);
243dd66df00SLuís Henriques 		dir = ERR_PTR(ret);
244dd66df00SLuís Henriques 		goto out;
245dd66df00SLuís Henriques 	}
246dd66df00SLuís Henriques 
247dd66df00SLuís Henriques 	/* And finally the inode */
248dd66df00SLuís Henriques 	dir = ceph_find_inode(parent->i_sb, vino);
249dd66df00SLuís Henriques 	if (!dir) {
250dd66df00SLuís Henriques 		/* This can happen if we're not mounting cephfs on the root */
251dd66df00SLuís Henriques 		dir = ceph_get_inode(parent->i_sb, vino, NULL);
252dd66df00SLuís Henriques 		if (IS_ERR(dir))
253dd66df00SLuís Henriques 			dout("Can't find inode %s (%s)\n", inode_number, name);
2542816a096SLuís Henriques 	}
255dd66df00SLuís Henriques 
256dd66df00SLuís Henriques out:
257dd66df00SLuís Henriques 	kfree(inode_number);
258dd66df00SLuís Henriques 	return dir;
259dd66df00SLuís Henriques }
260dd66df00SLuís Henriques 
ceph_encode_encrypted_dname(struct inode * parent,struct qstr * d_name,char * buf)261dd66df00SLuís Henriques int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name,
262dd66df00SLuís Henriques 				char *buf)
263dd66df00SLuís Henriques {
264dd66df00SLuís Henriques 	struct inode *dir = parent;
265dd66df00SLuís Henriques 	struct qstr iname;
2663fd945a7SJeff Layton 	u32 len;
267dd66df00SLuís Henriques 	int name_len;
2683fd945a7SJeff Layton 	int elen;
2693fd945a7SJeff Layton 	int ret;
270dd66df00SLuís Henriques 	u8 *cryptbuf = NULL;
2713fd945a7SJeff Layton 
272dd66df00SLuís Henriques 	iname.name = d_name->name;
273dd66df00SLuís Henriques 	name_len = d_name->len;
274dd66df00SLuís Henriques 
275dd66df00SLuís Henriques 	/* Handle the special case of snapshot names that start with '_' */
276dd66df00SLuís Henriques 	if ((ceph_snap(dir) == CEPH_SNAPDIR) && (name_len > 0) &&
277dd66df00SLuís Henriques 	    (iname.name[0] == '_')) {
278dd66df00SLuís Henriques 		dir = parse_longname(parent, iname.name, &name_len);
279dd66df00SLuís Henriques 		if (IS_ERR(dir))
280dd66df00SLuís Henriques 			return PTR_ERR(dir);
281dd66df00SLuís Henriques 		iname.name++; /* skip initial '_' */
282dd66df00SLuís Henriques 	}
283dd66df00SLuís Henriques 	iname.len = name_len;
284dd66df00SLuís Henriques 
285dd66df00SLuís Henriques 	if (!fscrypt_has_encryption_key(dir)) {
286af9ffa6dSXiubo Li 		memcpy(buf, d_name->name, d_name->len);
287dd66df00SLuís Henriques 		elen = d_name->len;
288dd66df00SLuís Henriques 		goto out;
289af9ffa6dSXiubo Li 	}
2903fd945a7SJeff Layton 
2913fd945a7SJeff Layton 	/*
2923fd945a7SJeff Layton 	 * Convert cleartext d_name to ciphertext. If result is longer than
2933fd945a7SJeff Layton 	 * CEPH_NOHASH_NAME_MAX, sha256 the remaining bytes
2943fd945a7SJeff Layton 	 *
2953fd945a7SJeff Layton 	 * See: fscrypt_setup_filename
2963fd945a7SJeff Layton 	 */
297dd66df00SLuís Henriques 	if (!fscrypt_fname_encrypted_size(dir, iname.len, NAME_MAX, &len)) {
298dd66df00SLuís Henriques 		elen = -ENAMETOOLONG;
299dd66df00SLuís Henriques 		goto out;
300dd66df00SLuís Henriques 	}
3013fd945a7SJeff Layton 
3023fd945a7SJeff Layton 	/* Allocate a buffer appropriate to hold the result */
3033fd945a7SJeff Layton 	cryptbuf = kmalloc(len > CEPH_NOHASH_NAME_MAX ? NAME_MAX : len,
3043fd945a7SJeff Layton 			   GFP_KERNEL);
305dd66df00SLuís Henriques 	if (!cryptbuf) {
306dd66df00SLuís Henriques 		elen = -ENOMEM;
307dd66df00SLuís Henriques 		goto out;
308dd66df00SLuís Henriques 	}
3093fd945a7SJeff Layton 
310dd66df00SLuís Henriques 	ret = fscrypt_fname_encrypt(dir, &iname, cryptbuf, len);
3113fd945a7SJeff Layton 	if (ret) {
312dd66df00SLuís Henriques 		elen = ret;
313dd66df00SLuís Henriques 		goto out;
3143fd945a7SJeff Layton 	}
3153fd945a7SJeff Layton 
3163fd945a7SJeff Layton 	/* hash the end if the name is long enough */
3173fd945a7SJeff Layton 	if (len > CEPH_NOHASH_NAME_MAX) {
3183fd945a7SJeff Layton 		u8 hash[SHA256_DIGEST_SIZE];
3193fd945a7SJeff Layton 		u8 *extra = cryptbuf + CEPH_NOHASH_NAME_MAX;
3203fd945a7SJeff Layton 
3213fd945a7SJeff Layton 		/*
3223fd945a7SJeff Layton 		 * hash the extra bytes and overwrite crypttext beyond that
3233fd945a7SJeff Layton 		 * point with it
3243fd945a7SJeff Layton 		 */
3253fd945a7SJeff Layton 		sha256(extra, len - CEPH_NOHASH_NAME_MAX, hash);
3263fd945a7SJeff Layton 		memcpy(extra, hash, SHA256_DIGEST_SIZE);
3273fd945a7SJeff Layton 		len = CEPH_NOHASH_NAME_MAX + SHA256_DIGEST_SIZE;
3283fd945a7SJeff Layton 	}
3293fd945a7SJeff Layton 
3303fd945a7SJeff Layton 	/* base64 encode the encrypted name */
3313fd945a7SJeff Layton 	elen = ceph_base64_encode(cryptbuf, len, buf);
3323fd945a7SJeff Layton 	dout("base64-encoded ciphertext name = %.*s\n", elen, buf);
333dd66df00SLuís Henriques 
334dd66df00SLuís Henriques 	/* To understand the 240 limit, see CEPH_NOHASH_NAME_MAX comments */
335dd66df00SLuís Henriques 	WARN_ON(elen > 240);
336dd66df00SLuís Henriques 	if ((elen > 0) && (dir != parent)) {
337dd66df00SLuís Henriques 		char tmp_buf[NAME_MAX];
338dd66df00SLuís Henriques 
339dd66df00SLuís Henriques 		elen = snprintf(tmp_buf, sizeof(tmp_buf), "_%.*s_%ld",
340dd66df00SLuís Henriques 				elen, buf, dir->i_ino);
341dd66df00SLuís Henriques 		memcpy(buf, tmp_buf, elen);
342dd66df00SLuís Henriques 	}
343dd66df00SLuís Henriques 
344dd66df00SLuís Henriques out:
345dd66df00SLuís Henriques 	kfree(cryptbuf);
346dd66df00SLuís Henriques 	if (dir != parent) {
347dd66df00SLuís Henriques 		if ((dir->i_state & I_NEW))
348dd66df00SLuís Henriques 			discard_new_inode(dir);
349dd66df00SLuís Henriques 		else
350dd66df00SLuís Henriques 			iput(dir);
351dd66df00SLuís Henriques 	}
3523fd945a7SJeff Layton 	return elen;
3533fd945a7SJeff Layton }
354457117f0SJeff Layton 
ceph_encode_encrypted_fname(struct inode * parent,struct dentry * dentry,char * buf)355dd66df00SLuís Henriques int ceph_encode_encrypted_fname(struct inode *parent, struct dentry *dentry,
356dd66df00SLuís Henriques 				char *buf)
357af9ffa6dSXiubo Li {
358af9ffa6dSXiubo Li 	WARN_ON_ONCE(!fscrypt_has_encryption_key(parent));
359af9ffa6dSXiubo Li 
360af9ffa6dSXiubo Li 	return ceph_encode_encrypted_dname(parent, &dentry->d_name, buf);
361af9ffa6dSXiubo Li }
362af9ffa6dSXiubo Li 
363457117f0SJeff Layton /**
364457117f0SJeff Layton  * ceph_fname_to_usr - convert a filename for userland presentation
365457117f0SJeff Layton  * @fname: ceph_fname to be converted
366457117f0SJeff Layton  * @tname: temporary name buffer to use for conversion (may be NULL)
367457117f0SJeff Layton  * @oname: where converted name should be placed
368457117f0SJeff Layton  * @is_nokey: set to true if key wasn't available during conversion (may be NULL)
369457117f0SJeff Layton  *
370457117f0SJeff Layton  * Given a filename (usually from the MDS), format it for presentation to
371457117f0SJeff Layton  * userland. If @parent is not encrypted, just pass it back as-is.
372457117f0SJeff Layton  *
373457117f0SJeff Layton  * Otherwise, base64 decode the string, and then ask fscrypt to format it
374457117f0SJeff Layton  * for userland presentation.
375457117f0SJeff Layton  *
376457117f0SJeff Layton  * Returns 0 on success or negative error code on error.
377457117f0SJeff Layton  */
ceph_fname_to_usr(const struct ceph_fname * fname,struct fscrypt_str * tname,struct fscrypt_str * oname,bool * is_nokey)378457117f0SJeff Layton int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
379457117f0SJeff Layton 		      struct fscrypt_str *oname, bool *is_nokey)
380457117f0SJeff Layton {
381dd66df00SLuís Henriques 	struct inode *dir = fname->dir;
382457117f0SJeff Layton 	struct fscrypt_str _tname = FSTR_INIT(NULL, 0);
383457117f0SJeff Layton 	struct fscrypt_str iname;
384dd66df00SLuís Henriques 	char *name = fname->name;
385dd66df00SLuís Henriques 	int name_len = fname->name_len;
386dd66df00SLuís Henriques 	int ret;
387457117f0SJeff Layton 
388457117f0SJeff Layton 	/* Sanity check that the resulting name will fit in the buffer */
389457117f0SJeff Layton 	if (fname->name_len > NAME_MAX || fname->ctext_len > NAME_MAX)
390457117f0SJeff Layton 		return -EIO;
391457117f0SJeff Layton 
392dd66df00SLuís Henriques 	/* Handle the special case of snapshot names that start with '_' */
393dd66df00SLuís Henriques 	if ((ceph_snap(dir) == CEPH_SNAPDIR) && (name_len > 0) &&
394dd66df00SLuís Henriques 	    (name[0] == '_')) {
395dd66df00SLuís Henriques 		dir = parse_longname(dir, name, &name_len);
396dd66df00SLuís Henriques 		if (IS_ERR(dir))
397dd66df00SLuís Henriques 			return PTR_ERR(dir);
398dd66df00SLuís Henriques 		name++; /* skip initial '_' */
399dd66df00SLuís Henriques 	}
400dd66df00SLuís Henriques 
401dd66df00SLuís Henriques 	if (!IS_ENCRYPTED(dir)) {
402dd66df00SLuís Henriques 		oname->name = fname->name;
403dd66df00SLuís Henriques 		oname->len = fname->name_len;
404dd66df00SLuís Henriques 		ret = 0;
405dd66df00SLuís Henriques 		goto out_inode;
406dd66df00SLuís Henriques 	}
407dd66df00SLuís Henriques 
408dd66df00SLuís Henriques 	ret = ceph_fscrypt_prepare_readdir(dir);
409dd66df00SLuís Henriques 	if (ret)
410dd66df00SLuís Henriques 		goto out_inode;
411457117f0SJeff Layton 
412457117f0SJeff Layton 	/*
413457117f0SJeff Layton 	 * Use the raw dentry name as sent by the MDS instead of
414457117f0SJeff Layton 	 * generating a nokey name via fscrypt.
415457117f0SJeff Layton 	 */
416dd66df00SLuís Henriques 	if (!fscrypt_has_encryption_key(dir)) {
417af9ffa6dSXiubo Li 		if (fname->no_copy)
418af9ffa6dSXiubo Li 			oname->name = fname->name;
419af9ffa6dSXiubo Li 		else
420457117f0SJeff Layton 			memcpy(oname->name, fname->name, fname->name_len);
421457117f0SJeff Layton 		oname->len = fname->name_len;
422457117f0SJeff Layton 		if (is_nokey)
423457117f0SJeff Layton 			*is_nokey = true;
424dd66df00SLuís Henriques 		ret = 0;
425dd66df00SLuís Henriques 		goto out_inode;
426457117f0SJeff Layton 	}
427457117f0SJeff Layton 
428457117f0SJeff Layton 	if (fname->ctext_len == 0) {
429457117f0SJeff Layton 		int declen;
430457117f0SJeff Layton 
431457117f0SJeff Layton 		if (!tname) {
432457117f0SJeff Layton 			ret = fscrypt_fname_alloc_buffer(NAME_MAX, &_tname);
433457117f0SJeff Layton 			if (ret)
434dd66df00SLuís Henriques 				goto out_inode;
435457117f0SJeff Layton 			tname = &_tname;
436457117f0SJeff Layton 		}
437457117f0SJeff Layton 
438dd66df00SLuís Henriques 		declen = ceph_base64_decode(name, name_len, tname->name);
439457117f0SJeff Layton 		if (declen <= 0) {
440457117f0SJeff Layton 			ret = -EIO;
441457117f0SJeff Layton 			goto out;
442457117f0SJeff Layton 		}
443457117f0SJeff Layton 		iname.name = tname->name;
444457117f0SJeff Layton 		iname.len = declen;
445457117f0SJeff Layton 	} else {
446457117f0SJeff Layton 		iname.name = fname->ctext;
447457117f0SJeff Layton 		iname.len = fname->ctext_len;
448457117f0SJeff Layton 	}
449457117f0SJeff Layton 
450dd66df00SLuís Henriques 	ret = fscrypt_fname_disk_to_usr(dir, 0, 0, &iname, oname);
451dd66df00SLuís Henriques 	if (!ret && (dir != fname->dir)) {
452dd66df00SLuís Henriques 		char tmp_buf[CEPH_BASE64_CHARS(NAME_MAX)];
453dd66df00SLuís Henriques 
454dd66df00SLuís Henriques 		name_len = snprintf(tmp_buf, sizeof(tmp_buf), "_%.*s_%ld",
455dd66df00SLuís Henriques 				    oname->len, oname->name, dir->i_ino);
456dd66df00SLuís Henriques 		memcpy(oname->name, tmp_buf, name_len);
457dd66df00SLuís Henriques 		oname->len = name_len;
458dd66df00SLuís Henriques 	}
459dd66df00SLuís Henriques 
460457117f0SJeff Layton out:
461457117f0SJeff Layton 	fscrypt_fname_free_buffer(&_tname);
462dd66df00SLuís Henriques out_inode:
46342b71826SLuis Henriques 	if (dir != fname->dir) {
464dd66df00SLuís Henriques 		if ((dir->i_state & I_NEW))
465dd66df00SLuís Henriques 			discard_new_inode(dir);
466dd66df00SLuís Henriques 		else
467dd66df00SLuís Henriques 			iput(dir);
468dd66df00SLuís Henriques 	}
469457117f0SJeff Layton 	return ret;
470457117f0SJeff Layton }
47114e034a6SLuís Henriques 
47214e034a6SLuís Henriques /**
47314e034a6SLuís Henriques  * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper
47414e034a6SLuís Henriques  * @dir: directory inode for readdir prep
47514e034a6SLuís Henriques  *
47614e034a6SLuís Henriques  * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as
47714e034a6SLuís Henriques  * non-complete if this call results in having the directory unlocked.
47814e034a6SLuís Henriques  *
47914e034a6SLuís Henriques  * Returns:
48014e034a6SLuís Henriques  *     1 - if directory was locked and key is now loaded (i.e. dir is unlocked)
48114e034a6SLuís Henriques  *     0 - if directory is still locked
48214e034a6SLuís Henriques  *   < 0 - if __fscrypt_prepare_readdir() fails
48314e034a6SLuís Henriques  */
ceph_fscrypt_prepare_readdir(struct inode * dir)48414e034a6SLuís Henriques int ceph_fscrypt_prepare_readdir(struct inode *dir)
48514e034a6SLuís Henriques {
48614e034a6SLuís Henriques 	bool had_key = fscrypt_has_encryption_key(dir);
48714e034a6SLuís Henriques 	int err;
48814e034a6SLuís Henriques 
48914e034a6SLuís Henriques 	if (!IS_ENCRYPTED(dir))
49014e034a6SLuís Henriques 		return 0;
49114e034a6SLuís Henriques 
49214e034a6SLuís Henriques 	err = __fscrypt_prepare_readdir(dir);
49314e034a6SLuís Henriques 	if (err)
49414e034a6SLuís Henriques 		return err;
49514e034a6SLuís Henriques 	if (!had_key && fscrypt_has_encryption_key(dir)) {
49614e034a6SLuís Henriques 		/* directory just got unlocked, mark it as not complete */
49714e034a6SLuís Henriques 		ceph_dir_clear_complete(dir);
49814e034a6SLuís Henriques 		return 1;
49914e034a6SLuís Henriques 	}
50014e034a6SLuís Henriques 	return 0;
50114e034a6SLuís Henriques }
50277cdb7e1SJeff Layton 
ceph_fscrypt_decrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num)50377cdb7e1SJeff Layton int ceph_fscrypt_decrypt_block_inplace(const struct inode *inode,
50477cdb7e1SJeff Layton 				  struct page *page, unsigned int len,
50577cdb7e1SJeff Layton 				  unsigned int offs, u64 lblk_num)
50677cdb7e1SJeff Layton {
50777cdb7e1SJeff Layton 	dout("%s: len %u offs %u blk %llu\n", __func__, len, offs, lblk_num);
50877cdb7e1SJeff Layton 	return fscrypt_decrypt_block_inplace(inode, page, len, offs, lblk_num);
50977cdb7e1SJeff Layton }
51077cdb7e1SJeff Layton 
ceph_fscrypt_encrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num,gfp_t gfp_flags)51177cdb7e1SJeff Layton int ceph_fscrypt_encrypt_block_inplace(const struct inode *inode,
51277cdb7e1SJeff Layton 				  struct page *page, unsigned int len,
51377cdb7e1SJeff Layton 				  unsigned int offs, u64 lblk_num,
51477cdb7e1SJeff Layton 				  gfp_t gfp_flags)
51577cdb7e1SJeff Layton {
51677cdb7e1SJeff Layton 	dout("%s: len %u offs %u blk %llu\n", __func__, len, offs, lblk_num);
51777cdb7e1SJeff Layton 	return fscrypt_encrypt_block_inplace(inode, page, len, offs, lblk_num,
51877cdb7e1SJeff Layton 					     gfp_flags);
51977cdb7e1SJeff Layton }
52077cdb7e1SJeff Layton 
52177cdb7e1SJeff Layton /**
52277cdb7e1SJeff Layton  * ceph_fscrypt_decrypt_pages - decrypt an array of pages
52377cdb7e1SJeff Layton  * @inode: pointer to inode associated with these pages
52477cdb7e1SJeff Layton  * @page: pointer to page array
52577cdb7e1SJeff Layton  * @off: offset into the file that the read data starts
52677cdb7e1SJeff Layton  * @len: max length to decrypt
52777cdb7e1SJeff Layton  *
52877cdb7e1SJeff Layton  * Decrypt an array of fscrypt'ed pages and return the amount of
52977cdb7e1SJeff Layton  * data decrypted. Any data in the page prior to the start of the
53077cdb7e1SJeff Layton  * first complete block in the read is ignored. Any incomplete
53177cdb7e1SJeff Layton  * crypto blocks at the end of the array are ignored (and should
53277cdb7e1SJeff Layton  * probably be zeroed by the caller).
53377cdb7e1SJeff Layton  *
53477cdb7e1SJeff Layton  * Returns the length of the decrypted data or a negative errno.
53577cdb7e1SJeff Layton  */
ceph_fscrypt_decrypt_pages(struct inode * inode,struct page ** page,u64 off,int len)53677cdb7e1SJeff Layton int ceph_fscrypt_decrypt_pages(struct inode *inode, struct page **page,
53777cdb7e1SJeff Layton 			       u64 off, int len)
53877cdb7e1SJeff Layton {
53977cdb7e1SJeff Layton 	int i, num_blocks;
54077cdb7e1SJeff Layton 	u64 baseblk = off >> CEPH_FSCRYPT_BLOCK_SHIFT;
54177cdb7e1SJeff Layton 	int ret = 0;
54277cdb7e1SJeff Layton 
54377cdb7e1SJeff Layton 	/*
54477cdb7e1SJeff Layton 	 * We can't deal with partial blocks on an encrypted file, so mask off
54577cdb7e1SJeff Layton 	 * the last bit.
54677cdb7e1SJeff Layton 	 */
54777cdb7e1SJeff Layton 	num_blocks = ceph_fscrypt_blocks(off, len & CEPH_FSCRYPT_BLOCK_MASK);
54877cdb7e1SJeff Layton 
54977cdb7e1SJeff Layton 	/* Decrypt each block */
55077cdb7e1SJeff Layton 	for (i = 0; i < num_blocks; ++i) {
55177cdb7e1SJeff Layton 		int blkoff = i << CEPH_FSCRYPT_BLOCK_SHIFT;
55277cdb7e1SJeff Layton 		int pgidx = blkoff >> PAGE_SHIFT;
55377cdb7e1SJeff Layton 		unsigned int pgoffs = offset_in_page(blkoff);
55477cdb7e1SJeff Layton 		int fret;
55577cdb7e1SJeff Layton 
55677cdb7e1SJeff Layton 		fret = ceph_fscrypt_decrypt_block_inplace(inode, page[pgidx],
55777cdb7e1SJeff Layton 				CEPH_FSCRYPT_BLOCK_SIZE, pgoffs,
55877cdb7e1SJeff Layton 				baseblk + i);
55977cdb7e1SJeff Layton 		if (fret < 0) {
56077cdb7e1SJeff Layton 			if (ret == 0)
56177cdb7e1SJeff Layton 				ret = fret;
56277cdb7e1SJeff Layton 			break;
56377cdb7e1SJeff Layton 		}
56477cdb7e1SJeff Layton 		ret += CEPH_FSCRYPT_BLOCK_SIZE;
56577cdb7e1SJeff Layton 	}
56677cdb7e1SJeff Layton 	return ret;
56777cdb7e1SJeff Layton }
56877cdb7e1SJeff Layton 
56977cdb7e1SJeff Layton /**
57077cdb7e1SJeff Layton  * ceph_fscrypt_decrypt_extents: decrypt received extents in given buffer
57177cdb7e1SJeff Layton  * @inode: inode associated with pages being decrypted
57277cdb7e1SJeff Layton  * @page: pointer to page array
57377cdb7e1SJeff Layton  * @off: offset into the file that the data in page[0] starts
57477cdb7e1SJeff Layton  * @map: pointer to extent array
57577cdb7e1SJeff Layton  * @ext_cnt: length of extent array
57677cdb7e1SJeff Layton  *
57777cdb7e1SJeff Layton  * Given an extent map and a page array, decrypt the received data in-place,
57877cdb7e1SJeff Layton  * skipping holes. Returns the offset into buffer of end of last decrypted
57977cdb7e1SJeff Layton  * block.
58077cdb7e1SJeff Layton  */
ceph_fscrypt_decrypt_extents(struct inode * inode,struct page ** page,u64 off,struct ceph_sparse_extent * map,u32 ext_cnt)58177cdb7e1SJeff Layton int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page,
58277cdb7e1SJeff Layton 				 u64 off, struct ceph_sparse_extent *map,
58377cdb7e1SJeff Layton 				 u32 ext_cnt)
58477cdb7e1SJeff Layton {
58577cdb7e1SJeff Layton 	int i, ret = 0;
58677cdb7e1SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
58777cdb7e1SJeff Layton 	u64 objno, objoff;
58877cdb7e1SJeff Layton 	u32 xlen;
58977cdb7e1SJeff Layton 
59077cdb7e1SJeff Layton 	/* Nothing to do for empty array */
59177cdb7e1SJeff Layton 	if (ext_cnt == 0) {
59277cdb7e1SJeff Layton 		dout("%s: empty array, ret 0\n", __func__);
59377cdb7e1SJeff Layton 		return 0;
59477cdb7e1SJeff Layton 	}
59577cdb7e1SJeff Layton 
59677cdb7e1SJeff Layton 	ceph_calc_file_object_mapping(&ci->i_layout, off, map[0].len,
59777cdb7e1SJeff Layton 				      &objno, &objoff, &xlen);
59877cdb7e1SJeff Layton 
59977cdb7e1SJeff Layton 	for (i = 0; i < ext_cnt; ++i) {
60077cdb7e1SJeff Layton 		struct ceph_sparse_extent *ext = &map[i];
60177cdb7e1SJeff Layton 		int pgsoff = ext->off - objoff;
60277cdb7e1SJeff Layton 		int pgidx = pgsoff >> PAGE_SHIFT;
60377cdb7e1SJeff Layton 		int fret;
60477cdb7e1SJeff Layton 
60577cdb7e1SJeff Layton 		if ((ext->off | ext->len) & ~CEPH_FSCRYPT_BLOCK_MASK) {
60677cdb7e1SJeff Layton 			pr_warn("%s: bad encrypted sparse extent idx %d off %llx len %llx\n",
60777cdb7e1SJeff Layton 				__func__, i, ext->off, ext->len);
60877cdb7e1SJeff Layton 			return -EIO;
60977cdb7e1SJeff Layton 		}
61077cdb7e1SJeff Layton 		fret = ceph_fscrypt_decrypt_pages(inode, &page[pgidx],
61177cdb7e1SJeff Layton 						 off + pgsoff, ext->len);
61277cdb7e1SJeff Layton 		dout("%s: [%d] 0x%llx~0x%llx fret %d\n", __func__, i,
61377cdb7e1SJeff Layton 				ext->off, ext->len, fret);
61477cdb7e1SJeff Layton 		if (fret < 0) {
61577cdb7e1SJeff Layton 			if (ret == 0)
61677cdb7e1SJeff Layton 				ret = fret;
61777cdb7e1SJeff Layton 			break;
61877cdb7e1SJeff Layton 		}
61977cdb7e1SJeff Layton 		ret = pgsoff + fret;
62077cdb7e1SJeff Layton 	}
62177cdb7e1SJeff Layton 	dout("%s: ret %d\n", __func__, ret);
62277cdb7e1SJeff Layton 	return ret;
62377cdb7e1SJeff Layton }
62477cdb7e1SJeff Layton 
62577cdb7e1SJeff Layton /**
62677cdb7e1SJeff Layton  * ceph_fscrypt_encrypt_pages - encrypt an array of pages
62777cdb7e1SJeff Layton  * @inode: pointer to inode associated with these pages
62877cdb7e1SJeff Layton  * @page: pointer to page array
62977cdb7e1SJeff Layton  * @off: offset into the file that the data starts
63077cdb7e1SJeff Layton  * @len: max length to encrypt
63177cdb7e1SJeff Layton  * @gfp: gfp flags to use for allocation
63277cdb7e1SJeff Layton  *
63377cdb7e1SJeff Layton  * Decrypt an array of cleartext pages and return the amount of
63477cdb7e1SJeff Layton  * data encrypted. Any data in the page prior to the start of the
63577cdb7e1SJeff Layton  * first complete block in the read is ignored. Any incomplete
63677cdb7e1SJeff Layton  * crypto blocks at the end of the array are ignored.
63777cdb7e1SJeff Layton  *
63877cdb7e1SJeff Layton  * Returns the length of the encrypted data or a negative errno.
63977cdb7e1SJeff Layton  */
ceph_fscrypt_encrypt_pages(struct inode * inode,struct page ** page,u64 off,int len,gfp_t gfp)64077cdb7e1SJeff Layton int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
64177cdb7e1SJeff Layton 				int len, gfp_t gfp)
64277cdb7e1SJeff Layton {
64377cdb7e1SJeff Layton 	int i, num_blocks;
64477cdb7e1SJeff Layton 	u64 baseblk = off >> CEPH_FSCRYPT_BLOCK_SHIFT;
64577cdb7e1SJeff Layton 	int ret = 0;
64677cdb7e1SJeff Layton 
64777cdb7e1SJeff Layton 	/*
64877cdb7e1SJeff Layton 	 * We can't deal with partial blocks on an encrypted file, so mask off
64977cdb7e1SJeff Layton 	 * the last bit.
65077cdb7e1SJeff Layton 	 */
65177cdb7e1SJeff Layton 	num_blocks = ceph_fscrypt_blocks(off, len & CEPH_FSCRYPT_BLOCK_MASK);
65277cdb7e1SJeff Layton 
65377cdb7e1SJeff Layton 	/* Encrypt each block */
65477cdb7e1SJeff Layton 	for (i = 0; i < num_blocks; ++i) {
65577cdb7e1SJeff Layton 		int blkoff = i << CEPH_FSCRYPT_BLOCK_SHIFT;
65677cdb7e1SJeff Layton 		int pgidx = blkoff >> PAGE_SHIFT;
65777cdb7e1SJeff Layton 		unsigned int pgoffs = offset_in_page(blkoff);
65877cdb7e1SJeff Layton 		int fret;
65977cdb7e1SJeff Layton 
66077cdb7e1SJeff Layton 		fret = ceph_fscrypt_encrypt_block_inplace(inode, page[pgidx],
66177cdb7e1SJeff Layton 				CEPH_FSCRYPT_BLOCK_SIZE, pgoffs,
66277cdb7e1SJeff Layton 				baseblk + i, gfp);
66377cdb7e1SJeff Layton 		if (fret < 0) {
66477cdb7e1SJeff Layton 			if (ret == 0)
66577cdb7e1SJeff Layton 				ret = fret;
66677cdb7e1SJeff Layton 			break;
66777cdb7e1SJeff Layton 		}
66877cdb7e1SJeff Layton 		ret += CEPH_FSCRYPT_BLOCK_SIZE;
66977cdb7e1SJeff Layton 	}
67077cdb7e1SJeff Layton 	return ret;
67177cdb7e1SJeff Layton }
672