xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * This contains encryption functions for per-file encryption. |
| 4 | * |
| 5 | * Copyright (C) 2015, Google, Inc. |
| 6 | * Copyright (C) 2015, Motorola Mobility |
| 7 | * |
| 8 | * Written by Michael Halcrow, 2014. |
| 9 | * |
| 10 | * Filename encryption additions |
| 11 | * Uday Savagaonkar, 2014 |
| 12 | * Encryption policy handling additions |
| 13 | * Ildar Muslukhov, 2014 |
| 14 | * Add fscrypt_pullback_bio_page() |
| 15 | * Jaegeuk Kim, 2015. |
| 16 | * |
| 17 | * This has not yet undergone a rigorous security audit. |
| 18 | * |
| 19 | * The usage of AES-XTS should conform to recommendations in NIST |
| 20 | * Special Publication 800-38E and IEEE P1619/D16. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/pagemap.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/bio.h> |
| 26 | #include <linux/namei.h> |
| 27 | #include "fscrypt_private.h" |
| 28 | |
| 29 | void fscrypt_decrypt_bio(struct bio *bio) |
| 30 | { |
| 31 | struct bio_vec *bv; |
| 32 | int i; |
| 33 | |
| 34 | bio_for_each_segment_all(bv, bio, i) { |
| 35 | struct page *page = bv->bv_page; |
| 36 | int ret = fscrypt_decrypt_pagecache_blocks(page, bv->bv_len, |
| 37 | bv->bv_offset); |
| 38 | if (ret) |
| 39 | SetPageError(page); |
| 40 | } |
| 41 | } |
| 42 | EXPORT_SYMBOL(fscrypt_decrypt_bio); |
| 43 | |
| 44 | int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, |
| 45 | sector_t pblk, unsigned int len) |
| 46 | { |
| 47 | const unsigned int blockbits = inode->i_blkbits; |
| 48 | const unsigned int blocksize = 1 << blockbits; |
| 49 | const bool inlinecrypt = fscrypt_inode_uses_inline_crypto(inode); |
| 50 | struct page *ciphertext_page; |
| 51 | struct bio *bio; |
| 52 | int ret, err = 0; |
| 53 | |
| 54 | if (inlinecrypt) { |
| 55 | ciphertext_page = ZERO_PAGE(0); |
| 56 | } else { |
| 57 | ciphertext_page = fscrypt_alloc_bounce_page(GFP_NOWAIT); |
| 58 | if (!ciphertext_page) |
| 59 | return -ENOMEM; |
| 60 | } |
| 61 | |
| 62 | while (len--) { |
| 63 | if (!inlinecrypt) { |
| 64 | err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk, |
| 65 | ZERO_PAGE(0), ciphertext_page, |
| 66 | blocksize, 0, GFP_NOFS); |
| 67 | if (err) |
| 68 | goto errout; |
| 69 | } |
| 70 | |
| 71 | bio = bio_alloc(GFP_NOWAIT, 1); |
| 72 | if (!bio) { |
| 73 | err = -ENOMEM; |
| 74 | goto errout; |
| 75 | } |
| 76 | fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOIO); |
| 77 | |
| 78 | bio_set_dev(bio, inode->i_sb->s_bdev); |
| 79 | bio->bi_iter.bi_sector = pblk << (blockbits - 9); |
| 80 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
| 81 | ret = bio_add_page(bio, ciphertext_page, blocksize, 0); |
| 82 | if (WARN_ON(ret != blocksize)) { |
| 83 | /* should never happen! */ |
| 84 | bio_put(bio); |
| 85 | err = -EIO; |
| 86 | goto errout; |
| 87 | } |
| 88 | err = submit_bio_wait(bio); |
| 89 | if (err == 0 && bio->bi_status) |
| 90 | err = -EIO; |
| 91 | bio_put(bio); |
| 92 | if (err) |
| 93 | goto errout; |
| 94 | lblk++; |
| 95 | pblk++; |
| 96 | } |
| 97 | err = 0; |
| 98 | errout: |
| 99 | if (!inlinecrypt) |
| 100 | fscrypt_free_bounce_page(ciphertext_page); |
| 101 | return err; |
| 102 | } |
| 103 | EXPORT_SYMBOL(fscrypt_zeroout_range); |