crypto.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * The base64 encode/decode code was copied from fscrypt:
  4. * Copyright (C) 2015, Google, Inc.
  5. * Copyright (C) 2015, Motorola Mobility
  6. * Written by Uday Savagaonkar, 2014.
  7. * Modified by Jaegeuk Kim, 2015.
  8. */
  9. #include <linux/ceph/ceph_debug.h>
  10. #include <linux/xattr.h>
  11. #include <linux/fscrypt.h>
  12. #include <linux/ceph/striper.h>
  13. #include "super.h"
  14. #include "mds_client.h"
  15. #include "crypto.h"
  16. /*
  17. * The base64url encoding used by fscrypt includes the '_' character, which may
  18. * cause problems in snapshot names (which can not start with '_'). Thus, we
  19. * used the base64 encoding defined for IMAP mailbox names (RFC 3501) instead,
  20. * which replaces '-' and '_' by '+' and ','.
  21. */
  22. static const char base64_table[65] =
  23. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
  24. int ceph_base64_encode(const u8 *src, int srclen, char *dst)
  25. {
  26. u32 ac = 0;
  27. int bits = 0;
  28. int i;
  29. char *cp = dst;
  30. for (i = 0; i < srclen; i++) {
  31. ac = (ac << 8) | src[i];
  32. bits += 8;
  33. do {
  34. bits -= 6;
  35. *cp++ = base64_table[(ac >> bits) & 0x3f];
  36. } while (bits >= 6);
  37. }
  38. if (bits)
  39. *cp++ = base64_table[(ac << (6 - bits)) & 0x3f];
  40. return cp - dst;
  41. }
  42. int ceph_base64_decode(const char *src, int srclen, u8 *dst)
  43. {
  44. u32 ac = 0;
  45. int bits = 0;
  46. int i;
  47. u8 *bp = dst;
  48. for (i = 0; i < srclen; i++) {
  49. const char *p = strchr(base64_table, src[i]);
  50. if (p == NULL || src[i] == 0)
  51. return -1;
  52. ac = (ac << 6) | (p - base64_table);
  53. bits += 6;
  54. if (bits >= 8) {
  55. bits -= 8;
  56. *bp++ = (u8)(ac >> bits);
  57. }
  58. }
  59. if (ac & ((1 << bits) - 1))
  60. return -1;
  61. return bp - dst;
  62. }
  63. static int ceph_crypt_get_context(struct inode *inode, void *ctx, size_t len)
  64. {
  65. struct ceph_inode_info *ci = ceph_inode(inode);
  66. struct ceph_fscrypt_auth *cfa = (struct ceph_fscrypt_auth *)ci->fscrypt_auth;
  67. u32 ctxlen;
  68. /* Non existent or too short? */
  69. if (!cfa || (ci->fscrypt_auth_len < (offsetof(struct ceph_fscrypt_auth, cfa_blob) + 1)))
  70. return -ENOBUFS;
  71. /* Some format we don't recognize? */
  72. if (le32_to_cpu(cfa->cfa_version) != CEPH_FSCRYPT_AUTH_VERSION)
  73. return -ENOBUFS;
  74. ctxlen = le32_to_cpu(cfa->cfa_blob_len);
  75. if (len < ctxlen)
  76. return -ERANGE;
  77. memcpy(ctx, cfa->cfa_blob, ctxlen);
  78. return ctxlen;
  79. }
  80. static int ceph_crypt_set_context(struct inode *inode, const void *ctx,
  81. size_t len, void *fs_data)
  82. {
  83. int ret;
  84. struct iattr attr = { };
  85. struct ceph_iattr cia = { };
  86. struct ceph_fscrypt_auth *cfa;
  87. WARN_ON_ONCE(fs_data);
  88. if (len > FSCRYPT_SET_CONTEXT_MAX_SIZE)
  89. return -EINVAL;
  90. cfa = kzalloc(sizeof(*cfa), GFP_KERNEL);
  91. if (!cfa)
  92. return -ENOMEM;
  93. cfa->cfa_version = cpu_to_le32(CEPH_FSCRYPT_AUTH_VERSION);
  94. cfa->cfa_blob_len = cpu_to_le32(len);
  95. memcpy(cfa->cfa_blob, ctx, len);
  96. cia.fscrypt_auth = cfa;
  97. ret = __ceph_setattr(&nop_mnt_idmap, inode, &attr, &cia);
  98. if (ret == 0)
  99. inode_set_flags(inode, S_ENCRYPTED, S_ENCRYPTED);
  100. kfree(cia.fscrypt_auth);
  101. return ret;
  102. }
  103. static bool ceph_crypt_empty_dir(struct inode *inode)
  104. {
  105. struct ceph_inode_info *ci = ceph_inode(inode);
  106. return ci->i_rsubdirs + ci->i_rfiles == 1;
  107. }
  108. static const union fscrypt_policy *ceph_get_dummy_policy(struct super_block *sb)
  109. {
  110. return ceph_sb_to_fs_client(sb)->fsc_dummy_enc_policy.policy;
  111. }
  112. static struct fscrypt_operations ceph_fscrypt_ops = {
  113. .needs_bounce_pages = 1,
  114. .get_context = ceph_crypt_get_context,
  115. .set_context = ceph_crypt_set_context,
  116. .get_dummy_policy = ceph_get_dummy_policy,
  117. .empty_dir = ceph_crypt_empty_dir,
  118. };
  119. void ceph_fscrypt_set_ops(struct super_block *sb)
  120. {
  121. fscrypt_set_ops(sb, &ceph_fscrypt_ops);
  122. }
  123. void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc)
  124. {
  125. fscrypt_free_dummy_policy(&fsc->fsc_dummy_enc_policy);
  126. }
  127. int ceph_fscrypt_prepare_context(struct inode *dir, struct inode *inode,
  128. struct ceph_acl_sec_ctx *as)
  129. {
  130. int ret, ctxsize;
  131. bool encrypted = false;
  132. struct ceph_inode_info *ci = ceph_inode(inode);
  133. ret = fscrypt_prepare_new_inode(dir, inode, &encrypted);
  134. if (ret)
  135. return ret;
  136. if (!encrypted)
  137. return 0;
  138. as->fscrypt_auth = kzalloc(sizeof(*as->fscrypt_auth), GFP_KERNEL);
  139. if (!as->fscrypt_auth)
  140. return -ENOMEM;
  141. ctxsize = fscrypt_context_for_new_inode(as->fscrypt_auth->cfa_blob,
  142. inode);
  143. if (ctxsize < 0)
  144. return ctxsize;
  145. as->fscrypt_auth->cfa_version = cpu_to_le32(CEPH_FSCRYPT_AUTH_VERSION);
  146. as->fscrypt_auth->cfa_blob_len = cpu_to_le32(ctxsize);
  147. WARN_ON_ONCE(ci->fscrypt_auth);
  148. kfree(ci->fscrypt_auth);
  149. ci->fscrypt_auth_len = ceph_fscrypt_auth_len(as->fscrypt_auth);
  150. ci->fscrypt_auth = kmemdup(as->fscrypt_auth, ci->fscrypt_auth_len,
  151. GFP_KERNEL);
  152. if (!ci->fscrypt_auth)
  153. return -ENOMEM;
  154. inode->i_flags |= S_ENCRYPTED;
  155. return 0;
  156. }
  157. void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req,
  158. struct ceph_acl_sec_ctx *as)
  159. {
  160. swap(req->r_fscrypt_auth, as->fscrypt_auth);
  161. }
  162. /*
  163. * User-created snapshots can't start with '_'. Snapshots that start with this
  164. * character are special (hint: there aren't real snapshots) and use the
  165. * following format:
  166. *
  167. * _<SNAPSHOT-NAME>_<INODE-NUMBER>
  168. *
  169. * where:
  170. * - <SNAPSHOT-NAME> - the real snapshot name that may need to be decrypted,
  171. * - <INODE-NUMBER> - the inode number (in decimal) for the actual snapshot
  172. *
  173. * This function parses these snapshot names and returns the inode
  174. * <INODE-NUMBER>. 'name_len' will also bet set with the <SNAPSHOT-NAME>
  175. * length.
  176. */
  177. static struct inode *parse_longname(const struct inode *parent,
  178. const char *name, int *name_len)
  179. {
  180. struct ceph_client *cl = ceph_inode_to_client(parent);
  181. struct inode *dir = NULL;
  182. struct ceph_vino vino = { .snap = CEPH_NOSNAP };
  183. char *name_end, *inode_number;
  184. int ret = -EIO;
  185. /* NUL-terminate */
  186. char *str __free(kfree) = kmemdup_nul(name, *name_len, GFP_KERNEL);
  187. if (!str)
  188. return ERR_PTR(-ENOMEM);
  189. /* Skip initial '_' */
  190. str++;
  191. name_end = strrchr(str, '_');
  192. if (!name_end) {
  193. doutc(cl, "failed to parse long snapshot name: %s\n", str);
  194. return ERR_PTR(-EIO);
  195. }
  196. *name_len = (name_end - str);
  197. if (*name_len <= 0) {
  198. pr_err_client(cl, "failed to parse long snapshot name\n");
  199. return ERR_PTR(-EIO);
  200. }
  201. /* Get the inode number */
  202. inode_number = name_end + 1;
  203. ret = kstrtou64(inode_number, 10, &vino.ino);
  204. if (ret) {
  205. doutc(cl, "failed to parse inode number: %s\n", str);
  206. return ERR_PTR(ret);
  207. }
  208. /* And finally the inode */
  209. dir = ceph_find_inode(parent->i_sb, vino);
  210. if (!dir) {
  211. /* This can happen if we're not mounting cephfs on the root */
  212. dir = ceph_get_inode(parent->i_sb, vino, NULL);
  213. if (IS_ERR(dir))
  214. doutc(cl, "can't find inode %s (%s)\n", inode_number, name);
  215. }
  216. return dir;
  217. }
  218. int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name,
  219. char *buf)
  220. {
  221. struct ceph_client *cl = ceph_inode_to_client(parent);
  222. struct inode *dir = parent;
  223. struct qstr iname;
  224. u32 len;
  225. int name_len;
  226. int elen;
  227. int ret;
  228. u8 *cryptbuf = NULL;
  229. iname.name = d_name->name;
  230. name_len = d_name->len;
  231. /* Handle the special case of snapshot names that start with '_' */
  232. if ((ceph_snap(dir) == CEPH_SNAPDIR) && (name_len > 0) &&
  233. (iname.name[0] == '_')) {
  234. dir = parse_longname(parent, iname.name, &name_len);
  235. if (IS_ERR(dir))
  236. return PTR_ERR(dir);
  237. iname.name++; /* skip initial '_' */
  238. }
  239. iname.len = name_len;
  240. if (!fscrypt_has_encryption_key(dir)) {
  241. memcpy(buf, d_name->name, d_name->len);
  242. elen = d_name->len;
  243. goto out;
  244. }
  245. /*
  246. * Convert cleartext d_name to ciphertext. If result is longer than
  247. * CEPH_NOHASH_NAME_MAX, sha256 the remaining bytes
  248. *
  249. * See: fscrypt_setup_filename
  250. */
  251. if (!fscrypt_fname_encrypted_size(dir, iname.len, NAME_MAX, &len)) {
  252. elen = -ENAMETOOLONG;
  253. goto out;
  254. }
  255. /* Allocate a buffer appropriate to hold the result */
  256. cryptbuf = kmalloc(len > CEPH_NOHASH_NAME_MAX ? NAME_MAX : len,
  257. GFP_KERNEL);
  258. if (!cryptbuf) {
  259. elen = -ENOMEM;
  260. goto out;
  261. }
  262. ret = fscrypt_fname_encrypt(dir, &iname, cryptbuf, len);
  263. if (ret) {
  264. elen = ret;
  265. goto out;
  266. }
  267. /* hash the end if the name is long enough */
  268. if (len > CEPH_NOHASH_NAME_MAX) {
  269. u8 hash[SHA256_DIGEST_SIZE];
  270. u8 *extra = cryptbuf + CEPH_NOHASH_NAME_MAX;
  271. /*
  272. * hash the extra bytes and overwrite crypttext beyond that
  273. * point with it
  274. */
  275. sha256(extra, len - CEPH_NOHASH_NAME_MAX, hash);
  276. memcpy(extra, hash, SHA256_DIGEST_SIZE);
  277. len = CEPH_NOHASH_NAME_MAX + SHA256_DIGEST_SIZE;
  278. }
  279. /* base64 encode the encrypted name */
  280. elen = ceph_base64_encode(cryptbuf, len, buf);
  281. doutc(cl, "base64-encoded ciphertext name = %.*s\n", elen, buf);
  282. /* To understand the 240 limit, see CEPH_NOHASH_NAME_MAX comments */
  283. WARN_ON(elen > 240);
  284. if ((elen > 0) && (dir != parent)) {
  285. char tmp_buf[NAME_MAX];
  286. elen = snprintf(tmp_buf, sizeof(tmp_buf), "_%.*s_%ld",
  287. elen, buf, dir->i_ino);
  288. memcpy(buf, tmp_buf, elen);
  289. }
  290. out:
  291. kfree(cryptbuf);
  292. if (dir != parent) {
  293. if ((dir->i_state & I_NEW))
  294. discard_new_inode(dir);
  295. else
  296. iput(dir);
  297. }
  298. return elen;
  299. }
  300. int ceph_encode_encrypted_fname(struct inode *parent, struct dentry *dentry,
  301. char *buf)
  302. {
  303. WARN_ON_ONCE(!fscrypt_has_encryption_key(parent));
  304. return ceph_encode_encrypted_dname(parent, &dentry->d_name, buf);
  305. }
  306. /**
  307. * ceph_fname_to_usr - convert a filename for userland presentation
  308. * @fname: ceph_fname to be converted
  309. * @tname: temporary name buffer to use for conversion (may be NULL)
  310. * @oname: where converted name should be placed
  311. * @is_nokey: set to true if key wasn't available during conversion (may be NULL)
  312. *
  313. * Given a filename (usually from the MDS), format it for presentation to
  314. * userland. If @parent is not encrypted, just pass it back as-is.
  315. *
  316. * Otherwise, base64 decode the string, and then ask fscrypt to format it
  317. * for userland presentation.
  318. *
  319. * Returns 0 on success or negative error code on error.
  320. */
  321. int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
  322. struct fscrypt_str *oname, bool *is_nokey)
  323. {
  324. struct inode *dir = fname->dir;
  325. struct fscrypt_str _tname = FSTR_INIT(NULL, 0);
  326. struct fscrypt_str iname;
  327. char *name = fname->name;
  328. int name_len = fname->name_len;
  329. int ret;
  330. /* Sanity check that the resulting name will fit in the buffer */
  331. if (fname->name_len > NAME_MAX || fname->ctext_len > NAME_MAX)
  332. return -EIO;
  333. /* Handle the special case of snapshot names that start with '_' */
  334. if ((ceph_snap(dir) == CEPH_SNAPDIR) && (name_len > 0) &&
  335. (name[0] == '_')) {
  336. dir = parse_longname(dir, name, &name_len);
  337. if (IS_ERR(dir))
  338. return PTR_ERR(dir);
  339. name++; /* skip initial '_' */
  340. }
  341. if (!IS_ENCRYPTED(dir)) {
  342. oname->name = fname->name;
  343. oname->len = fname->name_len;
  344. ret = 0;
  345. goto out_inode;
  346. }
  347. ret = ceph_fscrypt_prepare_readdir(dir);
  348. if (ret)
  349. goto out_inode;
  350. /*
  351. * Use the raw dentry name as sent by the MDS instead of
  352. * generating a nokey name via fscrypt.
  353. */
  354. if (!fscrypt_has_encryption_key(dir)) {
  355. if (fname->no_copy)
  356. oname->name = fname->name;
  357. else
  358. memcpy(oname->name, fname->name, fname->name_len);
  359. oname->len = fname->name_len;
  360. if (is_nokey)
  361. *is_nokey = true;
  362. ret = 0;
  363. goto out_inode;
  364. }
  365. if (fname->ctext_len == 0) {
  366. int declen;
  367. if (!tname) {
  368. ret = fscrypt_fname_alloc_buffer(NAME_MAX, &_tname);
  369. if (ret)
  370. goto out_inode;
  371. tname = &_tname;
  372. }
  373. declen = ceph_base64_decode(name, name_len, tname->name);
  374. if (declen <= 0) {
  375. ret = -EIO;
  376. goto out;
  377. }
  378. iname.name = tname->name;
  379. iname.len = declen;
  380. } else {
  381. iname.name = fname->ctext;
  382. iname.len = fname->ctext_len;
  383. }
  384. ret = fscrypt_fname_disk_to_usr(dir, 0, 0, &iname, oname);
  385. if (!ret && (dir != fname->dir)) {
  386. char tmp_buf[CEPH_BASE64_CHARS(NAME_MAX)];
  387. name_len = snprintf(tmp_buf, sizeof(tmp_buf), "_%.*s_%ld",
  388. oname->len, oname->name, dir->i_ino);
  389. memcpy(oname->name, tmp_buf, name_len);
  390. oname->len = name_len;
  391. }
  392. out:
  393. fscrypt_fname_free_buffer(&_tname);
  394. out_inode:
  395. if (dir != fname->dir) {
  396. if ((dir->i_state & I_NEW))
  397. discard_new_inode(dir);
  398. else
  399. iput(dir);
  400. }
  401. return ret;
  402. }
  403. /**
  404. * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper
  405. * @dir: directory inode for readdir prep
  406. *
  407. * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as
  408. * non-complete if this call results in having the directory unlocked.
  409. *
  410. * Returns:
  411. * 1 - if directory was locked and key is now loaded (i.e. dir is unlocked)
  412. * 0 - if directory is still locked
  413. * < 0 - if __fscrypt_prepare_readdir() fails
  414. */
  415. int ceph_fscrypt_prepare_readdir(struct inode *dir)
  416. {
  417. bool had_key = fscrypt_has_encryption_key(dir);
  418. int err;
  419. if (!IS_ENCRYPTED(dir))
  420. return 0;
  421. err = __fscrypt_prepare_readdir(dir);
  422. if (err)
  423. return err;
  424. if (!had_key && fscrypt_has_encryption_key(dir)) {
  425. /* directory just got unlocked, mark it as not complete */
  426. ceph_dir_clear_complete(dir);
  427. return 1;
  428. }
  429. return 0;
  430. }
  431. int ceph_fscrypt_decrypt_block_inplace(const struct inode *inode,
  432. struct page *page, unsigned int len,
  433. unsigned int offs, u64 lblk_num)
  434. {
  435. struct ceph_client *cl = ceph_inode_to_client(inode);
  436. doutc(cl, "%p %llx.%llx len %u offs %u blk %llu\n", inode,
  437. ceph_vinop(inode), len, offs, lblk_num);
  438. return fscrypt_decrypt_block_inplace(inode, page, len, offs, lblk_num);
  439. }
  440. int ceph_fscrypt_encrypt_block_inplace(const struct inode *inode,
  441. struct page *page, unsigned int len,
  442. unsigned int offs, u64 lblk_num,
  443. gfp_t gfp_flags)
  444. {
  445. struct ceph_client *cl = ceph_inode_to_client(inode);
  446. doutc(cl, "%p %llx.%llx len %u offs %u blk %llu\n", inode,
  447. ceph_vinop(inode), len, offs, lblk_num);
  448. return fscrypt_encrypt_block_inplace(inode, page, len, offs, lblk_num,
  449. gfp_flags);
  450. }
  451. /**
  452. * ceph_fscrypt_decrypt_pages - decrypt an array of pages
  453. * @inode: pointer to inode associated with these pages
  454. * @page: pointer to page array
  455. * @off: offset into the file that the read data starts
  456. * @len: max length to decrypt
  457. *
  458. * Decrypt an array of fscrypt'ed pages and return the amount of
  459. * data decrypted. Any data in the page prior to the start of the
  460. * first complete block in the read is ignored. Any incomplete
  461. * crypto blocks at the end of the array are ignored (and should
  462. * probably be zeroed by the caller).
  463. *
  464. * Returns the length of the decrypted data or a negative errno.
  465. */
  466. int ceph_fscrypt_decrypt_pages(struct inode *inode, struct page **page,
  467. u64 off, int len)
  468. {
  469. int i, num_blocks;
  470. u64 baseblk = off >> CEPH_FSCRYPT_BLOCK_SHIFT;
  471. int ret = 0;
  472. /*
  473. * We can't deal with partial blocks on an encrypted file, so mask off
  474. * the last bit.
  475. */
  476. num_blocks = ceph_fscrypt_blocks(off, len & CEPH_FSCRYPT_BLOCK_MASK);
  477. /* Decrypt each block */
  478. for (i = 0; i < num_blocks; ++i) {
  479. int blkoff = i << CEPH_FSCRYPT_BLOCK_SHIFT;
  480. int pgidx = blkoff >> PAGE_SHIFT;
  481. unsigned int pgoffs = offset_in_page(blkoff);
  482. int fret;
  483. fret = ceph_fscrypt_decrypt_block_inplace(inode, page[pgidx],
  484. CEPH_FSCRYPT_BLOCK_SIZE, pgoffs,
  485. baseblk + i);
  486. if (fret < 0) {
  487. if (ret == 0)
  488. ret = fret;
  489. break;
  490. }
  491. ret += CEPH_FSCRYPT_BLOCK_SIZE;
  492. }
  493. return ret;
  494. }
  495. /**
  496. * ceph_fscrypt_decrypt_extents: decrypt received extents in given buffer
  497. * @inode: inode associated with pages being decrypted
  498. * @page: pointer to page array
  499. * @off: offset into the file that the data in page[0] starts
  500. * @map: pointer to extent array
  501. * @ext_cnt: length of extent array
  502. *
  503. * Given an extent map and a page array, decrypt the received data in-place,
  504. * skipping holes. Returns the offset into buffer of end of last decrypted
  505. * block.
  506. */
  507. int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page,
  508. u64 off, struct ceph_sparse_extent *map,
  509. u32 ext_cnt)
  510. {
  511. struct ceph_client *cl = ceph_inode_to_client(inode);
  512. int i, ret = 0;
  513. struct ceph_inode_info *ci = ceph_inode(inode);
  514. u64 objno, objoff;
  515. u32 xlen;
  516. /* Nothing to do for empty array */
  517. if (ext_cnt == 0) {
  518. doutc(cl, "%p %llx.%llx empty array, ret 0\n", inode,
  519. ceph_vinop(inode));
  520. return 0;
  521. }
  522. ceph_calc_file_object_mapping(&ci->i_layout, off, map[0].len,
  523. &objno, &objoff, &xlen);
  524. for (i = 0; i < ext_cnt; ++i) {
  525. struct ceph_sparse_extent *ext = &map[i];
  526. int pgsoff = ext->off - objoff;
  527. int pgidx = pgsoff >> PAGE_SHIFT;
  528. int fret;
  529. if ((ext->off | ext->len) & ~CEPH_FSCRYPT_BLOCK_MASK) {
  530. pr_warn_client(cl,
  531. "%p %llx.%llx bad encrypted sparse extent "
  532. "idx %d off %llx len %llx\n",
  533. inode, ceph_vinop(inode), i, ext->off,
  534. ext->len);
  535. return -EIO;
  536. }
  537. fret = ceph_fscrypt_decrypt_pages(inode, &page[pgidx],
  538. off + pgsoff, ext->len);
  539. doutc(cl, "%p %llx.%llx [%d] 0x%llx~0x%llx fret %d\n", inode,
  540. ceph_vinop(inode), i, ext->off, ext->len, fret);
  541. if (fret < 0) {
  542. if (ret == 0)
  543. ret = fret;
  544. break;
  545. }
  546. ret = pgsoff + fret;
  547. }
  548. doutc(cl, "ret %d\n", ret);
  549. return ret;
  550. }
  551. /**
  552. * ceph_fscrypt_encrypt_pages - encrypt an array of pages
  553. * @inode: pointer to inode associated with these pages
  554. * @page: pointer to page array
  555. * @off: offset into the file that the data starts
  556. * @len: max length to encrypt
  557. * @gfp: gfp flags to use for allocation
  558. *
  559. * Decrypt an array of cleartext pages and return the amount of
  560. * data encrypted. Any data in the page prior to the start of the
  561. * first complete block in the read is ignored. Any incomplete
  562. * crypto blocks at the end of the array are ignored.
  563. *
  564. * Returns the length of the encrypted data or a negative errno.
  565. */
  566. int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
  567. int len, gfp_t gfp)
  568. {
  569. int i, num_blocks;
  570. u64 baseblk = off >> CEPH_FSCRYPT_BLOCK_SHIFT;
  571. int ret = 0;
  572. /*
  573. * We can't deal with partial blocks on an encrypted file, so mask off
  574. * the last bit.
  575. */
  576. num_blocks = ceph_fscrypt_blocks(off, len & CEPH_FSCRYPT_BLOCK_MASK);
  577. /* Encrypt each block */
  578. for (i = 0; i < num_blocks; ++i) {
  579. int blkoff = i << CEPH_FSCRYPT_BLOCK_SHIFT;
  580. int pgidx = blkoff >> PAGE_SHIFT;
  581. unsigned int pgoffs = offset_in_page(blkoff);
  582. int fret;
  583. fret = ceph_fscrypt_encrypt_block_inplace(inode, page[pgidx],
  584. CEPH_FSCRYPT_BLOCK_SIZE, pgoffs,
  585. baseblk + i, gfp);
  586. if (fret < 0) {
  587. if (ret == 0)
  588. ret = fret;
  589. break;
  590. }
  591. ret += CEPH_FSCRYPT_BLOCK_SIZE;
  592. }
  593. return ret;
  594. }