Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * This contains encryption functions for per-file encryption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2015, Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2015, Motorola Mobility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Written by Michael Halcrow, 2014.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Filename encryption additions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *	Uday Savagaonkar, 2014
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Encryption policy handling additions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *	Ildar Muslukhov, 2014
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Add fscrypt_pullback_bio_page()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *	Jaegeuk Kim, 2015.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * This has not yet undergone a rigorous security audit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * The usage of AES-XTS should conform to recommendations in NIST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * Special Publication 800-38E and IEEE P1619/D16.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/mempool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <crypto/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "fscrypt_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static unsigned int num_prealloc_crypto_pages = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) module_param(num_prealloc_crypto_pages, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) MODULE_PARM_DESC(num_prealloc_crypto_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		"Number of crypto pages to preallocate");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static mempool_t *fscrypt_bounce_page_pool = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static struct workqueue_struct *fscrypt_read_workqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static DEFINE_MUTEX(fscrypt_init_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) struct kmem_cache *fscrypt_info_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) void fscrypt_enqueue_decrypt_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	queue_work(fscrypt_read_workqueue, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) EXPORT_SYMBOL(fscrypt_enqueue_decrypt_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	return mempool_alloc(fscrypt_bounce_page_pool, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * fscrypt_free_bounce_page() - free a ciphertext bounce page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * @bounce_page: the bounce page to free, or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * Free a bounce page that was allocated by fscrypt_encrypt_pagecache_blocks(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * or by fscrypt_alloc_bounce_page() directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) void fscrypt_free_bounce_page(struct page *bounce_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (!bounce_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	set_page_private(bounce_page, (unsigned long)NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	ClearPagePrivate(bounce_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	mempool_free(bounce_page, fscrypt_bounce_page_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) EXPORT_SYMBOL(fscrypt_free_bounce_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * Generate the IV for the given logical block number within the given file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * For filenames encryption, lblk_num == 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * Keep this in sync with fscrypt_limit_io_blocks().  fscrypt_limit_io_blocks()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * needs to know about any IV generation methods where the low bits of IV don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * simply contain the lblk_num (e.g., IV_INO_LBLK_32).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			 const struct fscrypt_info *ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	u8 flags = fscrypt_policy_flags(&ci->ci_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	memset(iv, 0, ci->ci_mode->ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		WARN_ON_ONCE(lblk_num > U32_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		WARN_ON_ONCE(ci->ci_inode->i_ino > U32_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		lblk_num |= (u64)ci->ci_inode->i_ino << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	} else if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		WARN_ON_ONCE(lblk_num > U32_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		lblk_num = (u32)(ci->ci_hashed_ino + lblk_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	} else if (flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		memcpy(iv->nonce, ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	iv->lblk_num = cpu_to_le64(lblk_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* Encrypt or decrypt a single filesystem block of file contents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			u64 lblk_num, struct page *src_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			struct page *dest_page, unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			unsigned int offs, gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	union fscrypt_iv iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct skcipher_request *req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	DECLARE_CRYPTO_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct scatterlist dst, src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct fscrypt_info *ci = inode->i_crypt_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct crypto_skcipher *tfm = ci->ci_enc_key.tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (WARN_ON_ONCE(len <= 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (WARN_ON_ONCE(len % FS_CRYPTO_BLOCK_SIZE != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	fscrypt_generate_iv(&iv, lblk_num, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	req = skcipher_request_alloc(tfm, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (!req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	skcipher_request_set_callback(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		crypto_req_done, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	sg_init_table(&dst, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	sg_set_page(&dst, dest_page, len, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	sg_init_table(&src, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	sg_set_page(&src, src_page, len, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	skcipher_request_set_crypt(req, &src, &dst, len, &iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (rw == FS_DECRYPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	skcipher_request_free(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		fscrypt_err(inode, "%scryption failed for block %llu: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			    (rw == FS_DECRYPT ? "De" : "En"), lblk_num, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  * fscrypt_encrypt_pagecache_blocks() - Encrypt filesystem blocks from a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  *					pagecache page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * @page:      The locked pagecache page containing the block(s) to encrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * @len:       Total size of the block(s) to encrypt.  Must be a nonzero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  *		multiple of the filesystem's block size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * @offs:      Byte offset within @page of the first block to encrypt.  Must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  *		a multiple of the filesystem's block size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * @gfp_flags: Memory allocation flags.  See details below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * A new bounce page is allocated, and the specified block(s) are encrypted into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * it.  In the bounce page, the ciphertext block(s) will be located at the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  * offsets at which the plaintext block(s) were located in the source page; any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  * other parts of the bounce page will be left uninitialized.  However, normally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * blocksize == PAGE_SIZE and the whole page is encrypted at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  * This is for use by the filesystem's ->writepages() method.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * The bounce page allocation is mempool-backed, so it will always succeed when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * @gfp_flags includes __GFP_DIRECT_RECLAIM, e.g. when it's GFP_NOFS.  However,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * only the first page of each bio can be allocated this way.  To prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * deadlocks, for any additional pages a mask like GFP_NOWAIT must be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * Return: the new encrypted bounce page on success; an ERR_PTR() on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 					      unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 					      unsigned int offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 					      gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	const struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	const unsigned int blockbits = inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	const unsigned int blocksize = 1 << blockbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	struct page *ciphertext_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	u64 lblk_num = ((u64)page->index << (PAGE_SHIFT - blockbits)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		       (offs >> blockbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (WARN_ON_ONCE(!PageLocked(page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offs, blocksize)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	ciphertext_page = fscrypt_alloc_bounce_page(gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (!ciphertext_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	for (i = offs; i < offs + len; i += blocksize, lblk_num++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 					  page, ciphertext_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 					  blocksize, i, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			fscrypt_free_bounce_page(ciphertext_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	SetPagePrivate(ciphertext_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	set_page_private(ciphertext_page, (unsigned long)page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	return ciphertext_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) EXPORT_SYMBOL(fscrypt_encrypt_pagecache_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * fscrypt_encrypt_block_inplace() - Encrypt a filesystem block in-place
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  * @inode:     The inode to which this block belongs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  * @page:      The page containing the block to encrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  * @len:       Size of block to encrypt.  Doesn't need to be a multiple of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)  *		fs block size, but must be a multiple of FS_CRYPTO_BLOCK_SIZE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)  * @offs:      Byte offset within @page at which the block to encrypt begins
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * @lblk_num:  Filesystem logical block number of the block, i.e. the 0-based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  *		number of the block within the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * @gfp_flags: Memory allocation flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * Encrypt a possibly-compressed filesystem block that is located in an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  * arbitrary page, not necessarily in the original pagecache page.  The @inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  * and @lblk_num must be specified, as they can't be determined from @page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  * Return: 0 on success; -errno on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				  unsigned int len, unsigned int offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				  u64 lblk_num, gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	return fscrypt_crypt_block(inode, FS_ENCRYPT, lblk_num, page, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 				   len, offs, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) EXPORT_SYMBOL(fscrypt_encrypt_block_inplace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  * fscrypt_decrypt_pagecache_blocks() - Decrypt filesystem blocks in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  *					pagecache page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * @page:      The locked pagecache page containing the block(s) to decrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  * @len:       Total size of the block(s) to decrypt.  Must be a nonzero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  *		multiple of the filesystem's block size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  * @offs:      Byte offset within @page of the first block to decrypt.  Must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  *		a multiple of the filesystem's block size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  * The specified block(s) are decrypted in-place within the pagecache page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  * which must still be locked and not uptodate.  Normally, blocksize ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)  * PAGE_SIZE and the whole page is decrypted at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  * This is for use by the filesystem's ->readpages() method.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  * Return: 0 on success; -errno on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 				     unsigned int offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	const struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	const unsigned int blockbits = inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	const unsigned int blocksize = 1 << blockbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	u64 lblk_num = ((u64)page->index << (PAGE_SHIFT - blockbits)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		       (offs >> blockbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (WARN_ON_ONCE(!PageLocked(page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offs, blocksize)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	for (i = offs; i < offs + len; i += blocksize, lblk_num++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		err = fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 					  page, blocksize, i, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) EXPORT_SYMBOL(fscrypt_decrypt_pagecache_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  * fscrypt_decrypt_block_inplace() - Decrypt a filesystem block in-place
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)  * @inode:     The inode to which this block belongs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)  * @page:      The page containing the block to decrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)  * @len:       Size of block to decrypt.  Doesn't need to be a multiple of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)  *		fs block size, but must be a multiple of FS_CRYPTO_BLOCK_SIZE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  * @offs:      Byte offset within @page at which the block to decrypt begins
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  * @lblk_num:  Filesystem logical block number of the block, i.e. the 0-based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  *		number of the block within the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  * Decrypt a possibly-compressed filesystem block that is located in an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  * arbitrary page, not necessarily in the original pagecache page.  The @inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  * and @lblk_num must be specified, as they can't be determined from @page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  * Return: 0 on success; -errno on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 				  unsigned int len, unsigned int offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 				  u64 lblk_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				   len, offs, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) EXPORT_SYMBOL(fscrypt_decrypt_block_inplace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  * fscrypt_initialize() - allocate major buffers for fs encryption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * @cop_flags:  fscrypt operations flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  * We only call this when we start accessing encrypted files, since it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  * results in memory getting allocated that wouldn't otherwise be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  * Return: 0 on success; -errno on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) int fscrypt_initialize(unsigned int cop_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	/* No need to allocate a bounce page pool if this FS won't use it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (cop_flags & FS_CFLG_OWN_PAGES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	mutex_lock(&fscrypt_init_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (fscrypt_bounce_page_pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	fscrypt_bounce_page_pool =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		mempool_create_page_pool(num_prealloc_crypto_pages, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (!fscrypt_bounce_page_pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	mutex_unlock(&fscrypt_init_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) void fscrypt_msg(const struct inode *inode, const char *level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		 const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 				      DEFAULT_RATELIMIT_BURST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	struct va_format vaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (!__ratelimit(&rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	vaf.fmt = fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	vaf.va = &args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	if (inode && inode->i_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		printk("%sfscrypt (%s, inode %lu): %pV\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		       level, inode->i_sb->s_id, inode->i_ino, &vaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	else if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		printk("%sfscrypt (%s): %pV\n", level, inode->i_sb->s_id, &vaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		printk("%sfscrypt: %pV\n", level, &vaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)  * fscrypt_init() - Set up for fs encryption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)  * Return: 0 on success; -errno on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static int __init fscrypt_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	 * Use an unbound workqueue to allow bios to be decrypted in parallel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	 * even when they happen to complete on the same CPU.  This sacrifices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	 * locality, but it's worthwhile since decryption is CPU-intensive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	 * Also use a high-priority workqueue to prioritize decryption work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	 * which blocks reads from completing, over regular application tasks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	fscrypt_read_workqueue = alloc_workqueue("fscrypt_read_queue",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 						 WQ_UNBOUND | WQ_HIGHPRI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 						 num_online_cpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (!fscrypt_read_workqueue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	fscrypt_info_cachep = KMEM_CACHE(fscrypt_info, SLAB_RECLAIM_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if (!fscrypt_info_cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		goto fail_free_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	err = fscrypt_init_keyring();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		goto fail_free_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) fail_free_info:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	kmem_cache_destroy(fscrypt_info_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) fail_free_queue:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	destroy_workqueue(fscrypt_read_workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) late_initcall(fscrypt_init)