^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^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/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "fscrypt_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) void fscrypt_decrypt_bio(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct bio_vec *bv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct bvec_iter_all iter_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) bio_for_each_segment_all(bv, bio, iter_all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct page *page = bv->bv_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int ret = fscrypt_decrypt_pagecache_blocks(page, bv->bv_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) bv->bv_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) EXPORT_SYMBOL(fscrypt_decrypt_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) pgoff_t lblk, sector_t pblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) const unsigned int blockbits = inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int ret, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int num_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) bio = bio_alloc(GFP_NOFS, BIO_MAX_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) while (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned int blocks_this_page = min(len, blocks_per_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned int bytes_this_page = blocks_this_page << blockbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (num_pages == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) bio_set_dev(bio, inode->i_sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) bio->bi_iter.bi_sector =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pblk << (blockbits - SECTOR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ret = bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (WARN_ON(ret != bytes_this_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) num_pages++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) len -= blocks_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) lblk += blocks_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) pblk += blocks_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (num_pages == BIO_MAX_PAGES || !len ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) !fscrypt_mergeable_bio(bio, inode, lblk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) err = submit_bio_wait(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) bio_reset(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) num_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * @inode: the file's inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * @lblk: the first file logical block to zero out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * @pblk: the first filesystem physical block to zero out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * @len: number of blocks to zero out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * ciphertext blocks which decrypt to the all-zeroes block. The blocks must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * both logically and physically contiguous. It's also assumed that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * filesystem only uses a single block device, ->s_bdev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * Note that since each block uses a different IV, this involves writing a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * different ciphertext to each block; we can't simply reuse the same one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * Return: 0 on success; -errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) sector_t pblk, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) const unsigned int blockbits = inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) const unsigned int blocksize = 1 << blockbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) const unsigned int blocks_per_page_bits = PAGE_SHIFT - blockbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) const unsigned int blocks_per_page = 1 << blocks_per_page_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct page *pages[16]; /* write up to 16 pages at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unsigned int nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int ret, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (fscrypt_inode_uses_inline_crypto(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return fscrypt_zeroout_range_inline_crypt(inode, lblk, pblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) nr_pages = min_t(unsigned int, ARRAY_SIZE(pages),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) (len + blocks_per_page - 1) >> blocks_per_page_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * We need at least one page for ciphertext. Allocate the first one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * Any additional page allocations are allowed to fail, as they only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * help performance, and waiting on the mempool for them could deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) GFP_NOWAIT | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!pages[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) nr_pages = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (WARN_ON(nr_pages <= 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) bio = bio_alloc(GFP_NOFS, nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) bio_set_dev(bio, inode->i_sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) bio->bi_iter.bi_sector = pblk << (blockbits - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ZERO_PAGE(0), pages[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) blocksize, offset, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) lblk++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) pblk++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) len--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) offset += blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (offset == PAGE_SIZE || len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ret = bio_add_page(bio, pages[i++], offset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (WARN_ON(ret != offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) } while (i != nr_pages && len != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) err = submit_bio_wait(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) bio_reset(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) } while (len != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) for (i = 0; i < nr_pages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) fscrypt_free_bounce_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) EXPORT_SYMBOL(fscrypt_zeroout_range);