Zhen Kong | ee7bdc6 | 2019-03-14 10:55:19 -0700 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Per-File-Key (PFK) - f2fs |
| 8 | * |
| 9 | * This driver is used for working with EXT4/F2FS crypt extension |
| 10 | * |
| 11 | * The key information is stored in node by EXT4/F2FS when file is first opened |
| 12 | * and will be later accessed by Block Device Driver to actually load the key |
| 13 | * to encryption hw. |
| 14 | * |
| 15 | * PFK exposes API's for loading and removing keys from encryption hw |
| 16 | * and also API to determine whether 2 adjacent blocks can be agregated by |
| 17 | * Block Layer in one request to encryption hw. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #define pr_fmt(fmt) "pfk_f2fs [%s]: " fmt, __func__ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/printk.h> |
| 27 | |
| 28 | #include "fscrypt_ice.h" |
| 29 | #include "pfk_f2fs.h" |
| 30 | |
| 31 | static bool pfk_f2fs_ready; |
| 32 | |
| 33 | /* |
| 34 | * pfk_f2fs_deinit() - Deinit function, should be invoked by upper PFK layer |
| 35 | */ |
| 36 | void pfk_f2fs_deinit(void) |
| 37 | { |
| 38 | pfk_f2fs_ready = false; |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * pfk_f2fs_init() - Init function, should be invoked by upper PFK layer |
| 43 | */ |
| 44 | int __init pfk_f2fs_init(void) |
| 45 | { |
| 46 | pfk_f2fs_ready = true; |
| 47 | pr_info("PFK F2FS inited successfully\n"); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * pfk_f2fs_is_ready() - driver is initialized and ready. |
| 54 | * |
| 55 | * Return: true if the driver is ready. |
| 56 | */ |
| 57 | static inline bool pfk_f2fs_is_ready(void) |
| 58 | { |
| 59 | return pfk_f2fs_ready; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * pfk_is_f2fs_type() - return true if inode belongs to ICE F2FS PFE |
| 64 | * @inode: inode pointer |
| 65 | */ |
| 66 | bool pfk_is_f2fs_type(const struct inode *inode) |
| 67 | { |
| 68 | if (!pfe_is_inode_filesystem_type(inode, "f2fs")) |
| 69 | return false; |
| 70 | |
| 71 | return fscrypt_should_be_processed_by_ice(inode); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * pfk_f2fs_parse_cipher() - parse cipher from inode to enum |
| 76 | * @inode: inode |
| 77 | * @algo: pointer to store the output enum (can be null) |
| 78 | * |
| 79 | * return 0 in case of success, error otherwise (i.e not supported cipher) |
| 80 | */ |
| 81 | static int pfk_f2fs_parse_cipher(const struct inode *inode, |
| 82 | enum ice_cryto_algo_mode *algo) |
| 83 | { |
| 84 | /* |
| 85 | * currently only AES XTS algo is supported |
| 86 | * in the future, table with supported ciphers might |
| 87 | * be introduced |
| 88 | */ |
| 89 | if (!inode) |
| 90 | return -EINVAL; |
| 91 | |
| 92 | if (!fscrypt_is_aes_xts_cipher(inode)) { |
| 93 | pr_err("f2fs alghoritm is not supported by pfk\n"); |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | |
| 97 | if (algo) |
| 98 | *algo = ICE_CRYPTO_ALGO_MODE_AES_XTS; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | int pfk_f2fs_parse_inode(const struct bio *bio, |
| 104 | const struct inode *inode, |
| 105 | struct pfk_key_info *key_info, |
| 106 | enum ice_cryto_algo_mode *algo, |
| 107 | bool *is_pfe) |
| 108 | { |
| 109 | int ret = 0; |
| 110 | |
| 111 | if (!is_pfe) |
| 112 | return -EINVAL; |
| 113 | |
| 114 | /* |
| 115 | * only a few errors below can indicate that |
| 116 | * this function was not invoked within PFE context, |
| 117 | * otherwise we will consider it PFE |
| 118 | */ |
| 119 | *is_pfe = true; |
| 120 | |
| 121 | if (!pfk_f2fs_is_ready()) |
| 122 | return -ENODEV; |
| 123 | |
| 124 | if (!inode) |
| 125 | return -EINVAL; |
| 126 | |
| 127 | if (!key_info) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | key_info->key = fscrypt_get_ice_encryption_key(inode); |
| 131 | if (!key_info->key) { |
| 132 | pr_err("could not parse key from f2fs\n"); |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | |
| 136 | key_info->key_size = fscrypt_get_ice_encryption_key_size(inode); |
| 137 | if (!key_info->key_size) { |
| 138 | pr_err("could not parse key size from f2fs\n"); |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | |
| 142 | key_info->salt = fscrypt_get_ice_encryption_salt(inode); |
| 143 | if (!key_info->salt) { |
| 144 | pr_err("could not parse salt from f2fs\n"); |
| 145 | return -EINVAL; |
| 146 | } |
| 147 | |
| 148 | key_info->salt_size = fscrypt_get_ice_encryption_salt_size(inode); |
| 149 | if (!key_info->salt_size) { |
| 150 | pr_err("could not parse salt size from f2fs\n"); |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | ret = pfk_f2fs_parse_cipher(inode, algo); |
| 155 | if (ret != 0) { |
| 156 | pr_err("not supported cipher\n"); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | bool pfk_f2fs_allow_merge_bio(const struct bio *bio1, |
| 164 | const struct bio *bio2, const struct inode *inode1, |
| 165 | const struct inode *inode2) |
| 166 | { |
| 167 | bool mergeable; |
| 168 | |
| 169 | /* if there is no f2fs pfk, don't disallow merging blocks */ |
| 170 | if (!pfk_f2fs_is_ready()) |
| 171 | return true; |
| 172 | |
| 173 | if (!inode1 || !inode2) |
| 174 | return false; |
| 175 | |
| 176 | mergeable = fscrypt_is_ice_encryption_info_equal(inode1, inode2); |
| 177 | if (!mergeable) |
| 178 | return false; |
| 179 | |
| 180 | |
| 181 | /* ICE allows only consecutive iv_key stream. */ |
| 182 | if (!bio_dun(bio1) && !bio_dun(bio2)) |
| 183 | return true; |
| 184 | else if (!bio_dun(bio1) || !bio_dun(bio2)) |
| 185 | return false; |
| 186 | |
| 187 | return bio_end_dun(bio1) == bio_dun(bio2); |
| 188 | } |