// SPDX-License-Identifier: GPL-2.0 /* * Inline encryption support for fscrypt * * Copyright 2019 Google LLC */ /* * With "inline encryption", the block layer handles the decryption/encryption * as part of the bio, instead of the filesystem doing the crypto itself via * crypto API. See Documentation/block/inline-encryption.rst. fscrypt still * provides the key and IV to use. */ #include <linux/blk-crypto.h> #include <linux/blkdev.h> #include <linux/buffer_head.h> #include <linux/sched/mm.h> #include <linux/slab.h> #include <linux/uio.h> #include "fscrypt_private.h" static struct block_device **fscrypt_get_devices(struct super_block *sb, unsigned int *num_devs) { … } static unsigned int fscrypt_get_dun_bytes(const struct fscrypt_inode_info *ci) { … } /* * Log a message when starting to use blk-crypto (native) or blk-crypto-fallback * for an encryption mode for the first time. This is the blk-crypto * counterpart to the message logged when starting to use the crypto API for the * first time. A limitation is that these messages don't convey which specific * filesystems or files are using each implementation. However, *usually* * systems use just one implementation per mode, which makes these messages * helpful for debugging problems where the "wrong" implementation is used. */ static void fscrypt_log_blk_crypto_impl(struct fscrypt_mode *mode, struct block_device **devs, unsigned int num_devs, const struct blk_crypto_config *cfg) { … } /* Enable inline encryption for this file if supported. */ int fscrypt_select_encryption_impl(struct fscrypt_inode_info *ci) { … } int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key, const u8 *raw_key, const struct fscrypt_inode_info *ci) { … } void fscrypt_destroy_inline_crypt_key(struct super_block *sb, struct fscrypt_prepared_key *prep_key) { … } bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode) { … } EXPORT_SYMBOL_GPL(…); static void fscrypt_generate_dun(const struct fscrypt_inode_info *ci, u64 lblk_num, u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE]) { … } /** * fscrypt_set_bio_crypt_ctx() - prepare a file contents bio for inline crypto * @bio: a bio which will eventually be submitted to the file * @inode: the file's inode * @first_lblk: the first file logical block number in the I/O * @gfp_mask: memory allocation flags - these must be a waiting mask so that * bio_crypt_set_ctx can't fail. * * If the contents of the file should be encrypted (or decrypted) with inline * encryption, then assign the appropriate encryption context to the bio. * * Normally the bio should be newly allocated (i.e. no pages added yet), as * otherwise fscrypt_mergeable_bio() won't work as intended. * * The encryption context will be freed automatically when the bio is freed. */ void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode, u64 first_lblk, gfp_t gfp_mask) { … } EXPORT_SYMBOL_GPL(…); /* Extract the inode and logical block number from a buffer_head. */ static bool bh_get_inode_and_lblk_num(const struct buffer_head *bh, const struct inode **inode_ret, u64 *lblk_num_ret) { … } /** * fscrypt_set_bio_crypt_ctx_bh() - prepare a file contents bio for inline * crypto * @bio: a bio which will eventually be submitted to the file * @first_bh: the first buffer_head for which I/O will be submitted * @gfp_mask: memory allocation flags * * Same as fscrypt_set_bio_crypt_ctx(), except this takes a buffer_head instead * of an inode and block number directly. */ void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio, const struct buffer_head *first_bh, gfp_t gfp_mask) { … } EXPORT_SYMBOL_GPL(…); /** * fscrypt_mergeable_bio() - test whether data can be added to a bio * @bio: the bio being built up * @inode: the inode for the next part of the I/O * @next_lblk: the next file logical block number in the I/O * * When building a bio which may contain data which should undergo inline * encryption (or decryption) via fscrypt, filesystems should call this function * to ensure that the resulting bio contains only contiguous data unit numbers. * This will return false if the next part of the I/O cannot be merged with the * bio because either the encryption key would be different or the encryption * data unit numbers would be discontiguous. * * fscrypt_set_bio_crypt_ctx() must have already been called on the bio. * * This function isn't required in cases where crypto-mergeability is ensured in * another way, such as I/O targeting only a single file (and thus a single key) * combined with fscrypt_limit_io_blocks() to ensure DUN contiguity. * * Return: true iff the I/O is mergeable */ bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode, u64 next_lblk) { … } EXPORT_SYMBOL_GPL(…); /** * fscrypt_mergeable_bio_bh() - test whether data can be added to a bio * @bio: the bio being built up * @next_bh: the next buffer_head for which I/O will be submitted * * Same as fscrypt_mergeable_bio(), except this takes a buffer_head instead of * an inode and block number directly. * * Return: true iff the I/O is mergeable */ bool fscrypt_mergeable_bio_bh(struct bio *bio, const struct buffer_head *next_bh) { … } EXPORT_SYMBOL_GPL(…); /** * fscrypt_dio_supported() - check whether DIO (direct I/O) is supported on an * inode, as far as encryption is concerned * @inode: the inode in question * * Return: %true if there are no encryption constraints that prevent DIO from * being supported; %false if DIO is unsupported. (Note that in the * %true case, the filesystem might have other, non-encryption-related * constraints that prevent DIO from actually being supported. Also, on * encrypted files the filesystem is still responsible for only allowing * DIO when requests are filesystem-block-aligned.) */ bool fscrypt_dio_supported(struct inode *inode) { … } EXPORT_SYMBOL_GPL(…); /** * fscrypt_limit_io_blocks() - limit I/O blocks to avoid discontiguous DUNs * @inode: the file on which I/O is being done * @lblk: the block at which the I/O is being started from * @nr_blocks: the number of blocks we want to submit starting at @lblk * * Determine the limit to the number of blocks that can be submitted in a bio * targeting @lblk without causing a data unit number (DUN) discontiguity. * * This is normally just @nr_blocks, as normally the DUNs just increment along * with the logical blocks. (Or the file is not encrypted.) * * In rare cases, fscrypt can be using an IV generation method that allows the * DUN to wrap around within logically contiguous blocks, and that wraparound * will occur. If this happens, a value less than @nr_blocks will be returned * so that the wraparound doesn't occur in the middle of a bio, which would * cause encryption/decryption to produce wrong results. * * Return: the actual number of blocks that can be submitted */ u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks) { … } EXPORT_SYMBOL_GPL(…);