1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 258ae7468SRichard Weinberger /* 358ae7468SRichard Weinberger * This contains encryption functions for per-file encryption. 458ae7468SRichard Weinberger * 558ae7468SRichard Weinberger * Copyright (C) 2015, Google, Inc. 658ae7468SRichard Weinberger * Copyright (C) 2015, Motorola Mobility 758ae7468SRichard Weinberger * 858ae7468SRichard Weinberger * Written by Michael Halcrow, 2014. 958ae7468SRichard Weinberger * 1058ae7468SRichard Weinberger * Filename encryption additions 1158ae7468SRichard Weinberger * Uday Savagaonkar, 2014 1258ae7468SRichard Weinberger * Encryption policy handling additions 1358ae7468SRichard Weinberger * Ildar Muslukhov, 2014 1458ae7468SRichard Weinberger * Add fscrypt_pullback_bio_page() 1558ae7468SRichard Weinberger * Jaegeuk Kim, 2015. 1658ae7468SRichard Weinberger * 1758ae7468SRichard Weinberger * This has not yet undergone a rigorous security audit. 1858ae7468SRichard Weinberger * 1958ae7468SRichard Weinberger * The usage of AES-XTS should conform to recommendations in NIST 2058ae7468SRichard Weinberger * Special Publication 800-38E and IEEE P1619/D16. 2158ae7468SRichard Weinberger */ 2258ae7468SRichard Weinberger 2358ae7468SRichard Weinberger #include <linux/pagemap.h> 2458ae7468SRichard Weinberger #include <linux/module.h> 2558ae7468SRichard Weinberger #include <linux/bio.h> 2658ae7468SRichard Weinberger #include <linux/namei.h> 2758ae7468SRichard Weinberger #include "fscrypt_private.h" 2858ae7468SRichard Weinberger 291565bdadSEric Biggers void fscrypt_decrypt_bio(struct bio *bio) 3058ae7468SRichard Weinberger { 3158ae7468SRichard Weinberger struct bio_vec *bv; 326dc4f100SMing Lei struct bvec_iter_all iter_all; 3358ae7468SRichard Weinberger 342b070cfeSChristoph Hellwig bio_for_each_segment_all(bv, bio, iter_all) { 3558ae7468SRichard Weinberger struct page *page = bv->bv_page; 36ffceeefbSEric Biggers int ret = fscrypt_decrypt_pagecache_blocks(page, bv->bv_len, 37ffceeefbSEric Biggers bv->bv_offset); 38ff5d3a97SEric Biggers if (ret) 3958ae7468SRichard Weinberger SetPageError(page); 4058ae7468SRichard Weinberger } 410cb8dae4SEric Biggers } 420cb8dae4SEric Biggers EXPORT_SYMBOL(fscrypt_decrypt_bio); 430cb8dae4SEric Biggers 44*5fee3609SSatya Tangirala static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode, 45*5fee3609SSatya Tangirala pgoff_t lblk, sector_t pblk, 46*5fee3609SSatya Tangirala unsigned int len) 47*5fee3609SSatya Tangirala { 48*5fee3609SSatya Tangirala const unsigned int blockbits = inode->i_blkbits; 49*5fee3609SSatya Tangirala const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits); 50*5fee3609SSatya Tangirala struct bio *bio; 51*5fee3609SSatya Tangirala int ret, err = 0; 52*5fee3609SSatya Tangirala int num_pages = 0; 53*5fee3609SSatya Tangirala 54*5fee3609SSatya Tangirala /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */ 55*5fee3609SSatya Tangirala bio = bio_alloc(GFP_NOFS, BIO_MAX_PAGES); 56*5fee3609SSatya Tangirala 57*5fee3609SSatya Tangirala while (len) { 58*5fee3609SSatya Tangirala unsigned int blocks_this_page = min(len, blocks_per_page); 59*5fee3609SSatya Tangirala unsigned int bytes_this_page = blocks_this_page << blockbits; 60*5fee3609SSatya Tangirala 61*5fee3609SSatya Tangirala if (num_pages == 0) { 62*5fee3609SSatya Tangirala fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS); 63*5fee3609SSatya Tangirala bio_set_dev(bio, inode->i_sb->s_bdev); 64*5fee3609SSatya Tangirala bio->bi_iter.bi_sector = 65*5fee3609SSatya Tangirala pblk << (blockbits - SECTOR_SHIFT); 66*5fee3609SSatya Tangirala bio_set_op_attrs(bio, REQ_OP_WRITE, 0); 67*5fee3609SSatya Tangirala } 68*5fee3609SSatya Tangirala ret = bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0); 69*5fee3609SSatya Tangirala if (WARN_ON(ret != bytes_this_page)) { 70*5fee3609SSatya Tangirala err = -EIO; 71*5fee3609SSatya Tangirala goto out; 72*5fee3609SSatya Tangirala } 73*5fee3609SSatya Tangirala num_pages++; 74*5fee3609SSatya Tangirala len -= blocks_this_page; 75*5fee3609SSatya Tangirala lblk += blocks_this_page; 76*5fee3609SSatya Tangirala pblk += blocks_this_page; 77*5fee3609SSatya Tangirala if (num_pages == BIO_MAX_PAGES || !len || 78*5fee3609SSatya Tangirala !fscrypt_mergeable_bio(bio, inode, lblk)) { 79*5fee3609SSatya Tangirala err = submit_bio_wait(bio); 80*5fee3609SSatya Tangirala if (err) 81*5fee3609SSatya Tangirala goto out; 82*5fee3609SSatya Tangirala bio_reset(bio); 83*5fee3609SSatya Tangirala num_pages = 0; 84*5fee3609SSatya Tangirala } 85*5fee3609SSatya Tangirala } 86*5fee3609SSatya Tangirala out: 87*5fee3609SSatya Tangirala bio_put(bio); 88*5fee3609SSatya Tangirala return err; 89*5fee3609SSatya Tangirala } 90*5fee3609SSatya Tangirala 91796f12d7SEric Biggers /** 92796f12d7SEric Biggers * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file 93796f12d7SEric Biggers * @inode: the file's inode 94796f12d7SEric Biggers * @lblk: the first file logical block to zero out 95796f12d7SEric Biggers * @pblk: the first filesystem physical block to zero out 96796f12d7SEric Biggers * @len: number of blocks to zero out 97796f12d7SEric Biggers * 98796f12d7SEric Biggers * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write 99796f12d7SEric Biggers * ciphertext blocks which decrypt to the all-zeroes block. The blocks must be 100796f12d7SEric Biggers * both logically and physically contiguous. It's also assumed that the 101796f12d7SEric Biggers * filesystem only uses a single block device, ->s_bdev. 102796f12d7SEric Biggers * 103796f12d7SEric Biggers * Note that since each block uses a different IV, this involves writing a 104796f12d7SEric Biggers * different ciphertext to each block; we can't simply reuse the same one. 105796f12d7SEric Biggers * 106796f12d7SEric Biggers * Return: 0 on success; -errno on failure. 107796f12d7SEric Biggers */ 10858ae7468SRichard Weinberger int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, 10958ae7468SRichard Weinberger sector_t pblk, unsigned int len) 11058ae7468SRichard Weinberger { 111930d4539SEric Biggers const unsigned int blockbits = inode->i_blkbits; 112930d4539SEric Biggers const unsigned int blocksize = 1 << blockbits; 113796f12d7SEric Biggers const unsigned int blocks_per_page_bits = PAGE_SHIFT - blockbits; 114796f12d7SEric Biggers const unsigned int blocks_per_page = 1 << blocks_per_page_bits; 115796f12d7SEric Biggers struct page *pages[16]; /* write up to 16 pages at a time */ 116796f12d7SEric Biggers unsigned int nr_pages; 117796f12d7SEric Biggers unsigned int i; 118796f12d7SEric Biggers unsigned int offset; 11958ae7468SRichard Weinberger struct bio *bio; 120796f12d7SEric Biggers int ret, err; 12158ae7468SRichard Weinberger 122796f12d7SEric Biggers if (len == 0) 123796f12d7SEric Biggers return 0; 12458ae7468SRichard Weinberger 125*5fee3609SSatya Tangirala if (fscrypt_inode_uses_inline_crypto(inode)) 126*5fee3609SSatya Tangirala return fscrypt_zeroout_range_inline_crypt(inode, lblk, pblk, 127*5fee3609SSatya Tangirala len); 128*5fee3609SSatya Tangirala 129796f12d7SEric Biggers BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_PAGES); 130796f12d7SEric Biggers nr_pages = min_t(unsigned int, ARRAY_SIZE(pages), 131796f12d7SEric Biggers (len + blocks_per_page - 1) >> blocks_per_page_bits); 13258ae7468SRichard Weinberger 133796f12d7SEric Biggers /* 134796f12d7SEric Biggers * We need at least one page for ciphertext. Allocate the first one 135796f12d7SEric Biggers * from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail. 136796f12d7SEric Biggers * 137796f12d7SEric Biggers * Any additional page allocations are allowed to fail, as they only 138796f12d7SEric Biggers * help performance, and waiting on the mempool for them could deadlock. 139796f12d7SEric Biggers */ 140796f12d7SEric Biggers for (i = 0; i < nr_pages; i++) { 141796f12d7SEric Biggers pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS : 142796f12d7SEric Biggers GFP_NOWAIT | __GFP_NOWARN); 143796f12d7SEric Biggers if (!pages[i]) 144796f12d7SEric Biggers break; 14558ae7468SRichard Weinberger } 146796f12d7SEric Biggers nr_pages = i; 147796f12d7SEric Biggers if (WARN_ON(nr_pages <= 0)) 148796f12d7SEric Biggers return -EINVAL; 149796f12d7SEric Biggers 150796f12d7SEric Biggers /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */ 151796f12d7SEric Biggers bio = bio_alloc(GFP_NOFS, nr_pages); 152796f12d7SEric Biggers 153796f12d7SEric Biggers do { 15474d46992SChristoph Hellwig bio_set_dev(bio, inode->i_sb->s_bdev); 155930d4539SEric Biggers bio->bi_iter.bi_sector = pblk << (blockbits - 9); 15658ae7468SRichard Weinberger bio_set_op_attrs(bio, REQ_OP_WRITE, 0); 157796f12d7SEric Biggers 158796f12d7SEric Biggers i = 0; 159796f12d7SEric Biggers offset = 0; 160796f12d7SEric Biggers do { 161796f12d7SEric Biggers err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk, 162796f12d7SEric Biggers ZERO_PAGE(0), pages[i], 163796f12d7SEric Biggers blocksize, offset, GFP_NOFS); 16458ae7468SRichard Weinberger if (err) 165796f12d7SEric Biggers goto out; 16658ae7468SRichard Weinberger lblk++; 16758ae7468SRichard Weinberger pblk++; 168796f12d7SEric Biggers len--; 169796f12d7SEric Biggers offset += blocksize; 170796f12d7SEric Biggers if (offset == PAGE_SIZE || len == 0) { 171796f12d7SEric Biggers ret = bio_add_page(bio, pages[i++], offset, 0); 172796f12d7SEric Biggers if (WARN_ON(ret != offset)) { 173796f12d7SEric Biggers err = -EIO; 174796f12d7SEric Biggers goto out; 17558ae7468SRichard Weinberger } 176796f12d7SEric Biggers offset = 0; 177796f12d7SEric Biggers } 178796f12d7SEric Biggers } while (i != nr_pages && len != 0); 179796f12d7SEric Biggers 180796f12d7SEric Biggers err = submit_bio_wait(bio); 181796f12d7SEric Biggers if (err) 182796f12d7SEric Biggers goto out; 183796f12d7SEric Biggers bio_reset(bio); 184796f12d7SEric Biggers } while (len != 0); 18558ae7468SRichard Weinberger err = 0; 186796f12d7SEric Biggers out: 187796f12d7SEric Biggers bio_put(bio); 188796f12d7SEric Biggers for (i = 0; i < nr_pages; i++) 189796f12d7SEric Biggers fscrypt_free_bounce_page(pages[i]); 19058ae7468SRichard Weinberger return err; 19158ae7468SRichard Weinberger } 19258ae7468SRichard Weinberger EXPORT_SYMBOL(fscrypt_zeroout_range); 193