inline_crypt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Inline encryption support for fscrypt
  4. *
  5. * Copyright 2019 Google LLC
  6. */
  7. /*
  8. * With "inline encryption", the block layer handles the decryption/encryption
  9. * as part of the bio, instead of the filesystem doing the crypto itself via
  10. * crypto API. See Documentation/block/inline-encryption.rst. fscrypt still
  11. * provides the key and IV to use.
  12. */
  13. #include <linux/blk-crypto.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/sched/mm.h>
  17. #include <linux/slab.h>
  18. #include <linux/uio.h>
  19. #include "fscrypt_private.h"
  20. static struct block_device **fscrypt_get_devices(struct super_block *sb,
  21. unsigned int *num_devs)
  22. {
  23. struct block_device **devs;
  24. if (sb->s_cop->get_devices) {
  25. devs = sb->s_cop->get_devices(sb, num_devs);
  26. if (devs)
  27. return devs;
  28. }
  29. devs = kmalloc(sizeof(*devs), GFP_KERNEL);
  30. if (!devs)
  31. return ERR_PTR(-ENOMEM);
  32. devs[0] = sb->s_bdev;
  33. *num_devs = 1;
  34. return devs;
  35. }
  36. static unsigned int fscrypt_get_dun_bytes(const struct fscrypt_inode_info *ci)
  37. {
  38. const struct super_block *sb = ci->ci_inode->i_sb;
  39. unsigned int flags = fscrypt_policy_flags(&ci->ci_policy);
  40. int dun_bits;
  41. if (flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
  42. return offsetofend(union fscrypt_iv, nonce);
  43. if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64)
  44. return sizeof(__le64);
  45. if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
  46. return sizeof(__le32);
  47. /* Default case: IVs are just the file data unit index */
  48. dun_bits = fscrypt_max_file_dun_bits(sb, ci->ci_data_unit_bits);
  49. return DIV_ROUND_UP(dun_bits, 8);
  50. }
  51. /*
  52. * Log a message when starting to use blk-crypto (native) or blk-crypto-fallback
  53. * for an encryption mode for the first time. This is the blk-crypto
  54. * counterpart to the message logged when starting to use the crypto API for the
  55. * first time. A limitation is that these messages don't convey which specific
  56. * filesystems or files are using each implementation. However, *usually*
  57. * systems use just one implementation per mode, which makes these messages
  58. * helpful for debugging problems where the "wrong" implementation is used.
  59. */
  60. static void fscrypt_log_blk_crypto_impl(struct fscrypt_mode *mode,
  61. struct block_device **devs,
  62. unsigned int num_devs,
  63. const struct blk_crypto_config *cfg)
  64. {
  65. unsigned int i;
  66. for (i = 0; i < num_devs; i++) {
  67. if (!IS_ENABLED(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) ||
  68. blk_crypto_config_supported_natively(devs[i], cfg)) {
  69. if (!xchg(&mode->logged_blk_crypto_native, 1))
  70. pr_info("fscrypt: %s using blk-crypto (native)\n",
  71. mode->friendly_name);
  72. } else if (!xchg(&mode->logged_blk_crypto_fallback, 1)) {
  73. pr_info("fscrypt: %s using blk-crypto-fallback\n",
  74. mode->friendly_name);
  75. }
  76. }
  77. }
  78. /* Enable inline encryption for this file if supported. */
  79. int fscrypt_select_encryption_impl(struct fscrypt_inode_info *ci)
  80. {
  81. const struct inode *inode = ci->ci_inode;
  82. struct super_block *sb = inode->i_sb;
  83. struct blk_crypto_config crypto_cfg;
  84. struct block_device **devs;
  85. unsigned int num_devs;
  86. unsigned int i;
  87. /* The file must need contents encryption, not filenames encryption */
  88. if (!S_ISREG(inode->i_mode))
  89. return 0;
  90. /* The crypto mode must have a blk-crypto counterpart */
  91. if (ci->ci_mode->blk_crypto_mode == BLK_ENCRYPTION_MODE_INVALID)
  92. return 0;
  93. /* The filesystem must be mounted with -o inlinecrypt */
  94. if (!(sb->s_flags & SB_INLINECRYPT))
  95. return 0;
  96. /*
  97. * When a page contains multiple logically contiguous filesystem blocks,
  98. * some filesystem code only calls fscrypt_mergeable_bio() for the first
  99. * block in the page. This is fine for most of fscrypt's IV generation
  100. * strategies, where contiguous blocks imply contiguous IVs. But it
  101. * doesn't work with IV_INO_LBLK_32. For now, simply exclude
  102. * IV_INO_LBLK_32 with blocksize != PAGE_SIZE from inline encryption.
  103. */
  104. if ((fscrypt_policy_flags(&ci->ci_policy) &
  105. FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
  106. sb->s_blocksize != PAGE_SIZE)
  107. return 0;
  108. /*
  109. * On all the filesystem's block devices, blk-crypto must support the
  110. * crypto configuration that the file would use.
  111. */
  112. crypto_cfg.crypto_mode = ci->ci_mode->blk_crypto_mode;
  113. crypto_cfg.data_unit_size = 1U << ci->ci_data_unit_bits;
  114. crypto_cfg.dun_bytes = fscrypt_get_dun_bytes(ci);
  115. devs = fscrypt_get_devices(sb, &num_devs);
  116. if (IS_ERR(devs))
  117. return PTR_ERR(devs);
  118. for (i = 0; i < num_devs; i++) {
  119. if (!blk_crypto_config_supported(devs[i], &crypto_cfg))
  120. goto out_free_devs;
  121. }
  122. fscrypt_log_blk_crypto_impl(ci->ci_mode, devs, num_devs, &crypto_cfg);
  123. ci->ci_inlinecrypt = true;
  124. out_free_devs:
  125. kfree(devs);
  126. return 0;
  127. }
  128. int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
  129. const u8 *raw_key,
  130. const struct fscrypt_inode_info *ci)
  131. {
  132. const struct inode *inode = ci->ci_inode;
  133. struct super_block *sb = inode->i_sb;
  134. enum blk_crypto_mode_num crypto_mode = ci->ci_mode->blk_crypto_mode;
  135. struct blk_crypto_key *blk_key;
  136. struct block_device **devs;
  137. unsigned int num_devs;
  138. unsigned int i;
  139. int err;
  140. blk_key = kmalloc(sizeof(*blk_key), GFP_KERNEL);
  141. if (!blk_key)
  142. return -ENOMEM;
  143. err = blk_crypto_init_key(blk_key, raw_key, crypto_mode,
  144. fscrypt_get_dun_bytes(ci),
  145. 1U << ci->ci_data_unit_bits);
  146. if (err) {
  147. fscrypt_err(inode, "error %d initializing blk-crypto key", err);
  148. goto fail;
  149. }
  150. /* Start using blk-crypto on all the filesystem's block devices. */
  151. devs = fscrypt_get_devices(sb, &num_devs);
  152. if (IS_ERR(devs)) {
  153. err = PTR_ERR(devs);
  154. goto fail;
  155. }
  156. for (i = 0; i < num_devs; i++) {
  157. err = blk_crypto_start_using_key(devs[i], blk_key);
  158. if (err)
  159. break;
  160. }
  161. kfree(devs);
  162. if (err) {
  163. fscrypt_err(inode, "error %d starting to use blk-crypto", err);
  164. goto fail;
  165. }
  166. /*
  167. * Pairs with the smp_load_acquire() in fscrypt_is_key_prepared().
  168. * I.e., here we publish ->blk_key with a RELEASE barrier so that
  169. * concurrent tasks can ACQUIRE it. Note that this concurrency is only
  170. * possible for per-mode keys, not for per-file keys.
  171. */
  172. smp_store_release(&prep_key->blk_key, blk_key);
  173. return 0;
  174. fail:
  175. kfree_sensitive(blk_key);
  176. return err;
  177. }
  178. void fscrypt_destroy_inline_crypt_key(struct super_block *sb,
  179. struct fscrypt_prepared_key *prep_key)
  180. {
  181. struct blk_crypto_key *blk_key = prep_key->blk_key;
  182. struct block_device **devs;
  183. unsigned int num_devs;
  184. unsigned int i;
  185. if (!blk_key)
  186. return;
  187. /* Evict the key from all the filesystem's block devices. */
  188. devs = fscrypt_get_devices(sb, &num_devs);
  189. if (!IS_ERR(devs)) {
  190. for (i = 0; i < num_devs; i++)
  191. blk_crypto_evict_key(devs[i], blk_key);
  192. kfree(devs);
  193. }
  194. kfree_sensitive(blk_key);
  195. }
  196. bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
  197. {
  198. return inode->i_crypt_info->ci_inlinecrypt;
  199. }
  200. EXPORT_SYMBOL_GPL(__fscrypt_inode_uses_inline_crypto);
  201. static void fscrypt_generate_dun(const struct fscrypt_inode_info *ci,
  202. u64 lblk_num,
  203. u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
  204. {
  205. u64 index = lblk_num << ci->ci_data_units_per_block_bits;
  206. union fscrypt_iv iv;
  207. int i;
  208. fscrypt_generate_iv(&iv, index, ci);
  209. BUILD_BUG_ON(FSCRYPT_MAX_IV_SIZE > BLK_CRYPTO_MAX_IV_SIZE);
  210. memset(dun, 0, BLK_CRYPTO_MAX_IV_SIZE);
  211. for (i = 0; i < ci->ci_mode->ivsize/sizeof(dun[0]); i++)
  212. dun[i] = le64_to_cpu(iv.dun[i]);
  213. }
  214. /**
  215. * fscrypt_set_bio_crypt_ctx() - prepare a file contents bio for inline crypto
  216. * @bio: a bio which will eventually be submitted to the file
  217. * @inode: the file's inode
  218. * @first_lblk: the first file logical block number in the I/O
  219. * @gfp_mask: memory allocation flags - these must be a waiting mask so that
  220. * bio_crypt_set_ctx can't fail.
  221. *
  222. * If the contents of the file should be encrypted (or decrypted) with inline
  223. * encryption, then assign the appropriate encryption context to the bio.
  224. *
  225. * Normally the bio should be newly allocated (i.e. no pages added yet), as
  226. * otherwise fscrypt_mergeable_bio() won't work as intended.
  227. *
  228. * The encryption context will be freed automatically when the bio is freed.
  229. */
  230. void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
  231. u64 first_lblk, gfp_t gfp_mask)
  232. {
  233. const struct fscrypt_inode_info *ci;
  234. u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
  235. if (!fscrypt_inode_uses_inline_crypto(inode))
  236. return;
  237. ci = inode->i_crypt_info;
  238. fscrypt_generate_dun(ci, first_lblk, dun);
  239. bio_crypt_set_ctx(bio, ci->ci_enc_key.blk_key, dun, gfp_mask);
  240. }
  241. EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx);
  242. /* Extract the inode and logical block number from a buffer_head. */
  243. static bool bh_get_inode_and_lblk_num(const struct buffer_head *bh,
  244. const struct inode **inode_ret,
  245. u64 *lblk_num_ret)
  246. {
  247. struct folio *folio = bh->b_folio;
  248. const struct address_space *mapping;
  249. const struct inode *inode;
  250. /*
  251. * The ext4 journal (jbd2) can submit a buffer_head it directly created
  252. * for a non-pagecache page. fscrypt doesn't care about these.
  253. */
  254. mapping = folio_mapping(folio);
  255. if (!mapping)
  256. return false;
  257. inode = mapping->host;
  258. *inode_ret = inode;
  259. *lblk_num_ret = ((u64)folio->index << (PAGE_SHIFT - inode->i_blkbits)) +
  260. (bh_offset(bh) >> inode->i_blkbits);
  261. return true;
  262. }
  263. /**
  264. * fscrypt_set_bio_crypt_ctx_bh() - prepare a file contents bio for inline
  265. * crypto
  266. * @bio: a bio which will eventually be submitted to the file
  267. * @first_bh: the first buffer_head for which I/O will be submitted
  268. * @gfp_mask: memory allocation flags
  269. *
  270. * Same as fscrypt_set_bio_crypt_ctx(), except this takes a buffer_head instead
  271. * of an inode and block number directly.
  272. */
  273. void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
  274. const struct buffer_head *first_bh,
  275. gfp_t gfp_mask)
  276. {
  277. const struct inode *inode;
  278. u64 first_lblk;
  279. if (bh_get_inode_and_lblk_num(first_bh, &inode, &first_lblk))
  280. fscrypt_set_bio_crypt_ctx(bio, inode, first_lblk, gfp_mask);
  281. }
  282. EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh);
  283. /**
  284. * fscrypt_mergeable_bio() - test whether data can be added to a bio
  285. * @bio: the bio being built up
  286. * @inode: the inode for the next part of the I/O
  287. * @next_lblk: the next file logical block number in the I/O
  288. *
  289. * When building a bio which may contain data which should undergo inline
  290. * encryption (or decryption) via fscrypt, filesystems should call this function
  291. * to ensure that the resulting bio contains only contiguous data unit numbers.
  292. * This will return false if the next part of the I/O cannot be merged with the
  293. * bio because either the encryption key would be different or the encryption
  294. * data unit numbers would be discontiguous.
  295. *
  296. * fscrypt_set_bio_crypt_ctx() must have already been called on the bio.
  297. *
  298. * This function isn't required in cases where crypto-mergeability is ensured in
  299. * another way, such as I/O targeting only a single file (and thus a single key)
  300. * combined with fscrypt_limit_io_blocks() to ensure DUN contiguity.
  301. *
  302. * Return: true iff the I/O is mergeable
  303. */
  304. bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
  305. u64 next_lblk)
  306. {
  307. const struct bio_crypt_ctx *bc = bio->bi_crypt_context;
  308. u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
  309. if (!!bc != fscrypt_inode_uses_inline_crypto(inode))
  310. return false;
  311. if (!bc)
  312. return true;
  313. /*
  314. * Comparing the key pointers is good enough, as all I/O for each key
  315. * uses the same pointer. I.e., there's currently no need to support
  316. * merging requests where the keys are the same but the pointers differ.
  317. */
  318. if (bc->bc_key != inode->i_crypt_info->ci_enc_key.blk_key)
  319. return false;
  320. fscrypt_generate_dun(inode->i_crypt_info, next_lblk, next_dun);
  321. return bio_crypt_dun_is_contiguous(bc, bio->bi_iter.bi_size, next_dun);
  322. }
  323. EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio);
  324. /**
  325. * fscrypt_mergeable_bio_bh() - test whether data can be added to a bio
  326. * @bio: the bio being built up
  327. * @next_bh: the next buffer_head for which I/O will be submitted
  328. *
  329. * Same as fscrypt_mergeable_bio(), except this takes a buffer_head instead of
  330. * an inode and block number directly.
  331. *
  332. * Return: true iff the I/O is mergeable
  333. */
  334. bool fscrypt_mergeable_bio_bh(struct bio *bio,
  335. const struct buffer_head *next_bh)
  336. {
  337. const struct inode *inode;
  338. u64 next_lblk;
  339. if (!bh_get_inode_and_lblk_num(next_bh, &inode, &next_lblk))
  340. return !bio->bi_crypt_context;
  341. return fscrypt_mergeable_bio(bio, inode, next_lblk);
  342. }
  343. EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio_bh);
  344. /**
  345. * fscrypt_dio_supported() - check whether DIO (direct I/O) is supported on an
  346. * inode, as far as encryption is concerned
  347. * @inode: the inode in question
  348. *
  349. * Return: %true if there are no encryption constraints that prevent DIO from
  350. * being supported; %false if DIO is unsupported. (Note that in the
  351. * %true case, the filesystem might have other, non-encryption-related
  352. * constraints that prevent DIO from actually being supported. Also, on
  353. * encrypted files the filesystem is still responsible for only allowing
  354. * DIO when requests are filesystem-block-aligned.)
  355. */
  356. bool fscrypt_dio_supported(struct inode *inode)
  357. {
  358. int err;
  359. /* If the file is unencrypted, no veto from us. */
  360. if (!fscrypt_needs_contents_encryption(inode))
  361. return true;
  362. /*
  363. * We only support DIO with inline crypto, not fs-layer crypto.
  364. *
  365. * To determine whether the inode is using inline crypto, we have to set
  366. * up the key if it wasn't already done. This is because in the current
  367. * design of fscrypt, the decision of whether to use inline crypto or
  368. * not isn't made until the inode's encryption key is being set up. In
  369. * the DIO read/write case, the key will always be set up already, since
  370. * the file will be open. But in the case of statx(), the key might not
  371. * be set up yet, as the file might not have been opened yet.
  372. */
  373. err = fscrypt_require_key(inode);
  374. if (err) {
  375. /*
  376. * Key unavailable or couldn't be set up. This edge case isn't
  377. * worth worrying about; just report that DIO is unsupported.
  378. */
  379. return false;
  380. }
  381. return fscrypt_inode_uses_inline_crypto(inode);
  382. }
  383. EXPORT_SYMBOL_GPL(fscrypt_dio_supported);
  384. /**
  385. * fscrypt_limit_io_blocks() - limit I/O blocks to avoid discontiguous DUNs
  386. * @inode: the file on which I/O is being done
  387. * @lblk: the block at which the I/O is being started from
  388. * @nr_blocks: the number of blocks we want to submit starting at @lblk
  389. *
  390. * Determine the limit to the number of blocks that can be submitted in a bio
  391. * targeting @lblk without causing a data unit number (DUN) discontiguity.
  392. *
  393. * This is normally just @nr_blocks, as normally the DUNs just increment along
  394. * with the logical blocks. (Or the file is not encrypted.)
  395. *
  396. * In rare cases, fscrypt can be using an IV generation method that allows the
  397. * DUN to wrap around within logically contiguous blocks, and that wraparound
  398. * will occur. If this happens, a value less than @nr_blocks will be returned
  399. * so that the wraparound doesn't occur in the middle of a bio, which would
  400. * cause encryption/decryption to produce wrong results.
  401. *
  402. * Return: the actual number of blocks that can be submitted
  403. */
  404. u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks)
  405. {
  406. const struct fscrypt_inode_info *ci;
  407. u32 dun;
  408. if (!fscrypt_inode_uses_inline_crypto(inode))
  409. return nr_blocks;
  410. if (nr_blocks <= 1)
  411. return nr_blocks;
  412. ci = inode->i_crypt_info;
  413. if (!(fscrypt_policy_flags(&ci->ci_policy) &
  414. FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32))
  415. return nr_blocks;
  416. /* With IV_INO_LBLK_32, the DUN can wrap around from U32_MAX to 0. */
  417. dun = ci->ci_hashed_ino + lblk;
  418. return min_t(u64, nr_blocks, (u64)U32_MAX + 1 - dun);
  419. }
  420. EXPORT_SYMBOL_GPL(fscrypt_limit_io_blocks);