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