Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1 | /** |
| 2 | * eCryptfs: Linux filesystem encryption layer |
| 3 | * |
| 4 | * Copyright (C) 1997-2004 Erez Zadok |
| 5 | * Copyright (C) 2001-2004 Stony Brook University |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 6 | * Copyright (C) 2004-2007 International Business Machines Corp. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 7 | * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> |
| 8 | * Michael C. Thompson <mcthomps@us.ibm.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of the |
| 13 | * License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, but |
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 23 | * 02111-1307, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/mount.h> |
| 28 | #include <linux/pagemap.h> |
| 29 | #include <linux/random.h> |
| 30 | #include <linux/compiler.h> |
| 31 | #include <linux/key.h> |
| 32 | #include <linux/namei.h> |
| 33 | #include <linux/crypto.h> |
| 34 | #include <linux/file.h> |
| 35 | #include <linux/scatterlist.h> |
| 36 | #include "ecryptfs_kernel.h" |
| 37 | |
| 38 | static int |
| 39 | ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
| 40 | struct page *dst_page, int dst_offset, |
| 41 | struct page *src_page, int src_offset, int size, |
| 42 | unsigned char *iv); |
| 43 | static int |
| 44 | ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
| 45 | struct page *dst_page, int dst_offset, |
| 46 | struct page *src_page, int src_offset, int size, |
| 47 | unsigned char *iv); |
| 48 | |
| 49 | /** |
| 50 | * ecryptfs_to_hex |
| 51 | * @dst: Buffer to take hex character representation of contents of |
| 52 | * src; must be at least of size (src_size * 2) |
| 53 | * @src: Buffer to be converted to a hex string respresentation |
| 54 | * @src_size: number of bytes to convert |
| 55 | */ |
| 56 | void ecryptfs_to_hex(char *dst, char *src, size_t src_size) |
| 57 | { |
| 58 | int x; |
| 59 | |
| 60 | for (x = 0; x < src_size; x++) |
| 61 | sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * ecryptfs_from_hex |
| 66 | * @dst: Buffer to take the bytes from src hex; must be at least of |
| 67 | * size (src_size / 2) |
| 68 | * @src: Buffer to be converted from a hex string respresentation to raw value |
| 69 | * @dst_size: size of dst buffer, or number of hex characters pairs to convert |
| 70 | */ |
| 71 | void ecryptfs_from_hex(char *dst, char *src, int dst_size) |
| 72 | { |
| 73 | int x; |
| 74 | char tmp[3] = { 0, }; |
| 75 | |
| 76 | for (x = 0; x < dst_size; x++) { |
| 77 | tmp[0] = src[x * 2]; |
| 78 | tmp[1] = src[x * 2 + 1]; |
| 79 | dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * ecryptfs_calculate_md5 - calculates the md5 of @src |
| 85 | * @dst: Pointer to 16 bytes of allocated memory |
| 86 | * @crypt_stat: Pointer to crypt_stat struct for the current inode |
| 87 | * @src: Data to be md5'd |
| 88 | * @len: Length of @src |
| 89 | * |
| 90 | * Uses the allocated crypto context that crypt_stat references to |
| 91 | * generate the MD5 sum of the contents of src. |
| 92 | */ |
| 93 | static int ecryptfs_calculate_md5(char *dst, |
| 94 | struct ecryptfs_crypt_stat *crypt_stat, |
| 95 | char *src, int len) |
| 96 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 97 | struct scatterlist sg; |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 98 | struct hash_desc desc = { |
| 99 | .tfm = crypt_stat->hash_tfm, |
| 100 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP |
| 101 | }; |
| 102 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 103 | |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 104 | mutex_lock(&crypt_stat->cs_hash_tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 105 | sg_init_one(&sg, (u8 *)src, len); |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 106 | if (!desc.tfm) { |
| 107 | desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0, |
| 108 | CRYPTO_ALG_ASYNC); |
| 109 | if (IS_ERR(desc.tfm)) { |
| 110 | rc = PTR_ERR(desc.tfm); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 111 | ecryptfs_printk(KERN_ERR, "Error attempting to " |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 112 | "allocate crypto context; rc = [%d]\n", |
| 113 | rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 114 | goto out; |
| 115 | } |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 116 | crypt_stat->hash_tfm = desc.tfm; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 117 | } |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 118 | crypto_hash_init(&desc); |
| 119 | crypto_hash_update(&desc, &sg, len); |
| 120 | crypto_hash_final(&desc, dst); |
| 121 | mutex_unlock(&crypt_stat->cs_hash_tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 122 | out: |
| 123 | return rc; |
| 124 | } |
| 125 | |
Michael Halcrow | cd9d67d | 2007-10-16 01:28:04 -0700 | [diff] [blame] | 126 | static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name, |
| 127 | char *cipher_name, |
| 128 | char *chaining_modifier) |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 129 | { |
| 130 | int cipher_name_len = strlen(cipher_name); |
| 131 | int chaining_modifier_len = strlen(chaining_modifier); |
| 132 | int algified_name_len; |
| 133 | int rc; |
| 134 | |
| 135 | algified_name_len = (chaining_modifier_len + cipher_name_len + 3); |
| 136 | (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL); |
Michael Halcrow | 7bd473f | 2006-11-02 22:06:56 -0800 | [diff] [blame] | 137 | if (!(*algified_name)) { |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 138 | rc = -ENOMEM; |
| 139 | goto out; |
| 140 | } |
| 141 | snprintf((*algified_name), algified_name_len, "%s(%s)", |
| 142 | chaining_modifier, cipher_name); |
| 143 | rc = 0; |
| 144 | out: |
| 145 | return rc; |
| 146 | } |
| 147 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 148 | /** |
| 149 | * ecryptfs_derive_iv |
| 150 | * @iv: destination for the derived iv vale |
| 151 | * @crypt_stat: Pointer to crypt_stat struct for the current inode |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 152 | * @offset: Offset of the extent whose IV we are to derive |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 153 | * |
| 154 | * Generate the initialization vector from the given root IV and page |
| 155 | * offset. |
| 156 | * |
| 157 | * Returns zero on success; non-zero on error. |
| 158 | */ |
| 159 | static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat, |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 160 | loff_t offset) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 161 | { |
| 162 | int rc = 0; |
| 163 | char dst[MD5_DIGEST_SIZE]; |
| 164 | char src[ECRYPTFS_MAX_IV_BYTES + 16]; |
| 165 | |
| 166 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 167 | ecryptfs_printk(KERN_DEBUG, "root iv:\n"); |
| 168 | ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes); |
| 169 | } |
| 170 | /* TODO: It is probably secure to just cast the least |
| 171 | * significant bits of the root IV into an unsigned long and |
| 172 | * add the offset to that rather than go through all this |
| 173 | * hashing business. -Halcrow */ |
| 174 | memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes); |
| 175 | memset((src + crypt_stat->iv_bytes), 0, 16); |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 176 | snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 177 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 178 | ecryptfs_printk(KERN_DEBUG, "source:\n"); |
| 179 | ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16)); |
| 180 | } |
| 181 | rc = ecryptfs_calculate_md5(dst, crypt_stat, src, |
| 182 | (crypt_stat->iv_bytes + 16)); |
| 183 | if (rc) { |
| 184 | ecryptfs_printk(KERN_WARNING, "Error attempting to compute " |
| 185 | "MD5 while generating IV for a page\n"); |
| 186 | goto out; |
| 187 | } |
| 188 | memcpy(iv, dst, crypt_stat->iv_bytes); |
| 189 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 190 | ecryptfs_printk(KERN_DEBUG, "derived iv:\n"); |
| 191 | ecryptfs_dump_hex(iv, crypt_stat->iv_bytes); |
| 192 | } |
| 193 | out: |
| 194 | return rc; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * ecryptfs_init_crypt_stat |
| 199 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. |
| 200 | * |
| 201 | * Initialize the crypt_stat structure. |
| 202 | */ |
| 203 | void |
| 204 | ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) |
| 205 | { |
| 206 | memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 207 | INIT_LIST_HEAD(&crypt_stat->keysig_list); |
| 208 | mutex_init(&crypt_stat->keysig_list_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 209 | mutex_init(&crypt_stat->cs_mutex); |
| 210 | mutex_init(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 211 | mutex_init(&crypt_stat->cs_hash_tfm_mutex); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 212 | crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 216 | * ecryptfs_destroy_crypt_stat |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 217 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. |
| 218 | * |
| 219 | * Releases all memory associated with a crypt_stat struct. |
| 220 | */ |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 221 | void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 222 | { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 223 | struct ecryptfs_key_sig *key_sig, *key_sig_tmp; |
| 224 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 225 | if (crypt_stat->tfm) |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 226 | crypto_free_blkcipher(crypt_stat->tfm); |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 227 | if (crypt_stat->hash_tfm) |
| 228 | crypto_free_hash(crypt_stat->hash_tfm); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 229 | mutex_lock(&crypt_stat->keysig_list_mutex); |
| 230 | list_for_each_entry_safe(key_sig, key_sig_tmp, |
| 231 | &crypt_stat->keysig_list, crypt_stat_list) { |
| 232 | list_del(&key_sig->crypt_stat_list); |
| 233 | kmem_cache_free(ecryptfs_key_sig_cache, key_sig); |
| 234 | } |
| 235 | mutex_unlock(&crypt_stat->keysig_list_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 236 | memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); |
| 237 | } |
| 238 | |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 239 | void ecryptfs_destroy_mount_crypt_stat( |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 240 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 241 | { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 242 | struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp; |
| 243 | |
| 244 | if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED)) |
| 245 | return; |
| 246 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 247 | list_for_each_entry_safe(auth_tok, auth_tok_tmp, |
| 248 | &mount_crypt_stat->global_auth_tok_list, |
| 249 | mount_crypt_stat_list) { |
| 250 | list_del(&auth_tok->mount_crypt_stat_list); |
| 251 | mount_crypt_stat->num_global_auth_toks--; |
| 252 | if (auth_tok->global_auth_tok_key |
| 253 | && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID)) |
| 254 | key_put(auth_tok->global_auth_tok_key); |
| 255 | kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok); |
| 256 | } |
| 257 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 258 | memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat)); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * virt_to_scatterlist |
| 263 | * @addr: Virtual address |
| 264 | * @size: Size of data; should be an even multiple of the block size |
| 265 | * @sg: Pointer to scatterlist array; set to NULL to obtain only |
| 266 | * the number of scatterlist structs required in array |
| 267 | * @sg_size: Max array size |
| 268 | * |
| 269 | * Fills in a scatterlist array with page references for a passed |
| 270 | * virtual address. |
| 271 | * |
| 272 | * Returns the number of scatterlist structs in array used |
| 273 | */ |
| 274 | int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg, |
| 275 | int sg_size) |
| 276 | { |
| 277 | int i = 0; |
| 278 | struct page *pg; |
| 279 | int offset; |
| 280 | int remainder_of_page; |
| 281 | |
| 282 | while (size > 0 && i < sg_size) { |
| 283 | pg = virt_to_page(addr); |
| 284 | offset = offset_in_page(addr); |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame^] | 285 | if (sg) |
| 286 | sg_set_page(&sg[i], pg, 0, offset); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 287 | remainder_of_page = PAGE_CACHE_SIZE - offset; |
| 288 | if (size >= remainder_of_page) { |
| 289 | if (sg) |
| 290 | sg[i].length = remainder_of_page; |
| 291 | addr += remainder_of_page; |
| 292 | size -= remainder_of_page; |
| 293 | } else { |
| 294 | if (sg) |
| 295 | sg[i].length = size; |
| 296 | addr += size; |
| 297 | size = 0; |
| 298 | } |
| 299 | i++; |
| 300 | } |
| 301 | if (size > 0) |
| 302 | return -ENOMEM; |
| 303 | return i; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * encrypt_scatterlist |
| 308 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. |
| 309 | * @dest_sg: Destination of encrypted data |
| 310 | * @src_sg: Data to be encrypted |
| 311 | * @size: Length of data to be encrypted |
| 312 | * @iv: iv to use during encryption |
| 313 | * |
| 314 | * Returns the number of bytes encrypted; negative value on error |
| 315 | */ |
| 316 | static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, |
| 317 | struct scatterlist *dest_sg, |
| 318 | struct scatterlist *src_sg, int size, |
| 319 | unsigned char *iv) |
| 320 | { |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 321 | struct blkcipher_desc desc = { |
| 322 | .tfm = crypt_stat->tfm, |
| 323 | .info = iv, |
| 324 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP |
| 325 | }; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 326 | int rc = 0; |
| 327 | |
| 328 | BUG_ON(!crypt_stat || !crypt_stat->tfm |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 329 | || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 330 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 331 | ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n", |
| 332 | crypt_stat->key_size); |
| 333 | ecryptfs_dump_hex(crypt_stat->key, |
| 334 | crypt_stat->key_size); |
| 335 | } |
| 336 | /* Consider doing this once, when the file is opened */ |
| 337 | mutex_lock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 338 | rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key, |
| 339 | crypt_stat->key_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 340 | if (rc) { |
| 341 | ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n", |
| 342 | rc); |
| 343 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
| 344 | rc = -EINVAL; |
| 345 | goto out; |
| 346 | } |
| 347 | ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 348 | crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 349 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
| 350 | out: |
| 351 | return rc; |
| 352 | } |
| 353 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 354 | /** |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 355 | * ecryptfs_lower_offset_for_extent |
| 356 | * |
| 357 | * Convert an eCryptfs page index into a lower byte offset |
| 358 | */ |
| 359 | void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num, |
| 360 | struct ecryptfs_crypt_stat *crypt_stat) |
| 361 | { |
| 362 | (*offset) = ((crypt_stat->extent_size |
| 363 | * crypt_stat->num_header_extents_at_front) |
| 364 | + (crypt_stat->extent_size * extent_num)); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * ecryptfs_encrypt_extent |
| 369 | * @enc_extent_page: Allocated page into which to encrypt the data in |
| 370 | * @page |
| 371 | * @crypt_stat: crypt_stat containing cryptographic context for the |
| 372 | * encryption operation |
| 373 | * @page: Page containing plaintext data extent to encrypt |
| 374 | * @extent_offset: Page extent offset for use in generating IV |
| 375 | * |
| 376 | * Encrypts one extent of data. |
| 377 | * |
| 378 | * Return zero on success; non-zero otherwise |
| 379 | */ |
| 380 | static int ecryptfs_encrypt_extent(struct page *enc_extent_page, |
| 381 | struct ecryptfs_crypt_stat *crypt_stat, |
| 382 | struct page *page, |
| 383 | unsigned long extent_offset) |
| 384 | { |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 385 | loff_t extent_base; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 386 | char extent_iv[ECRYPTFS_MAX_IV_BYTES]; |
| 387 | int rc; |
| 388 | |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 389 | extent_base = (((loff_t)page->index) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 390 | * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); |
| 391 | rc = ecryptfs_derive_iv(extent_iv, crypt_stat, |
| 392 | (extent_base + extent_offset)); |
| 393 | if (rc) { |
| 394 | ecryptfs_printk(KERN_ERR, "Error attempting to " |
| 395 | "derive IV for extent [0x%.16x]; " |
| 396 | "rc = [%d]\n", (extent_base + extent_offset), |
| 397 | rc); |
| 398 | goto out; |
| 399 | } |
| 400 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 401 | ecryptfs_printk(KERN_DEBUG, "Encrypting extent " |
| 402 | "with iv:\n"); |
| 403 | ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes); |
| 404 | ecryptfs_printk(KERN_DEBUG, "First 8 bytes before " |
| 405 | "encryption:\n"); |
| 406 | ecryptfs_dump_hex((char *) |
| 407 | (page_address(page) |
| 408 | + (extent_offset * crypt_stat->extent_size)), |
| 409 | 8); |
| 410 | } |
| 411 | rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0, |
| 412 | page, (extent_offset |
| 413 | * crypt_stat->extent_size), |
| 414 | crypt_stat->extent_size, extent_iv); |
| 415 | if (rc < 0) { |
| 416 | printk(KERN_ERR "%s: Error attempting to encrypt page with " |
| 417 | "page->index = [%ld], extent_offset = [%ld]; " |
| 418 | "rc = [%d]\n", __FUNCTION__, page->index, extent_offset, |
| 419 | rc); |
| 420 | goto out; |
| 421 | } |
| 422 | rc = 0; |
| 423 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 424 | ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; " |
| 425 | "rc = [%d]\n", (extent_base + extent_offset), |
| 426 | rc); |
| 427 | ecryptfs_printk(KERN_DEBUG, "First 8 bytes after " |
| 428 | "encryption:\n"); |
| 429 | ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8); |
| 430 | } |
| 431 | out: |
| 432 | return rc; |
| 433 | } |
| 434 | |
| 435 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 436 | * ecryptfs_encrypt_page |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 437 | * @page: Page mapped from the eCryptfs inode for the file; contains |
| 438 | * decrypted content that needs to be encrypted (to a temporary |
| 439 | * page; not in place) and written out to the lower file |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 440 | * |
| 441 | * Encrypt an eCryptfs page. This is done on a per-extent basis. Note |
| 442 | * that eCryptfs pages may straddle the lower pages -- for instance, |
| 443 | * if the file was created on a machine with an 8K page size |
| 444 | * (resulting in an 8K header), and then the file is copied onto a |
| 445 | * host with a 32K page size, then when reading page 0 of the eCryptfs |
| 446 | * file, 24K of page 0 of the lower file will be read and decrypted, |
| 447 | * and then 8K of page 1 of the lower file will be read and decrypted. |
| 448 | * |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 449 | * Returns zero on success; negative on error |
| 450 | */ |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 451 | int ecryptfs_encrypt_page(struct page *page) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 452 | { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 453 | struct inode *ecryptfs_inode; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 454 | struct ecryptfs_crypt_stat *crypt_stat; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 455 | char *enc_extent_virt = NULL; |
| 456 | struct page *enc_extent_page; |
| 457 | loff_t extent_offset; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 458 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 459 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 460 | ecryptfs_inode = page->mapping->host; |
| 461 | crypt_stat = |
| 462 | &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 463 | if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 464 | rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, |
| 465 | 0, PAGE_CACHE_SIZE); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 466 | if (rc) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 467 | printk(KERN_ERR "%s: Error attempting to copy " |
| 468 | "page at index [%ld]\n", __FUNCTION__, |
| 469 | page->index); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 470 | goto out; |
| 471 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 472 | enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER); |
| 473 | if (!enc_extent_virt) { |
| 474 | rc = -ENOMEM; |
| 475 | ecryptfs_printk(KERN_ERR, "Error allocating memory for " |
| 476 | "encrypted extent\n"); |
| 477 | goto out; |
| 478 | } |
| 479 | enc_extent_page = virt_to_page(enc_extent_virt); |
| 480 | for (extent_offset = 0; |
| 481 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); |
| 482 | extent_offset++) { |
| 483 | loff_t offset; |
| 484 | |
| 485 | rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page, |
| 486 | extent_offset); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 487 | if (rc) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 488 | printk(KERN_ERR "%s: Error encrypting extent; " |
| 489 | "rc = [%d]\n", __FUNCTION__, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 490 | goto out; |
| 491 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 492 | ecryptfs_lower_offset_for_extent( |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 493 | &offset, ((((loff_t)page->index) |
| 494 | * (PAGE_CACHE_SIZE |
| 495 | / crypt_stat->extent_size)) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 496 | + extent_offset), crypt_stat); |
| 497 | rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, |
| 498 | offset, crypt_stat->extent_size); |
| 499 | if (rc) { |
| 500 | ecryptfs_printk(KERN_ERR, "Error attempting " |
| 501 | "to write lower page; rc = [%d]" |
| 502 | "\n", rc); |
| 503 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 504 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 505 | extent_offset++; |
| 506 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 507 | out: |
| 508 | kfree(enc_extent_virt); |
| 509 | return rc; |
| 510 | } |
| 511 | |
| 512 | static int ecryptfs_decrypt_extent(struct page *page, |
| 513 | struct ecryptfs_crypt_stat *crypt_stat, |
| 514 | struct page *enc_extent_page, |
| 515 | unsigned long extent_offset) |
| 516 | { |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 517 | loff_t extent_base; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 518 | char extent_iv[ECRYPTFS_MAX_IV_BYTES]; |
| 519 | int rc; |
| 520 | |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 521 | extent_base = (((loff_t)page->index) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 522 | * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); |
| 523 | rc = ecryptfs_derive_iv(extent_iv, crypt_stat, |
| 524 | (extent_base + extent_offset)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 525 | if (rc) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 526 | ecryptfs_printk(KERN_ERR, "Error attempting to " |
| 527 | "derive IV for extent [0x%.16x]; " |
| 528 | "rc = [%d]\n", (extent_base + extent_offset), |
| 529 | rc); |
| 530 | goto out; |
| 531 | } |
| 532 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 533 | ecryptfs_printk(KERN_DEBUG, "Decrypting extent " |
| 534 | "with iv:\n"); |
| 535 | ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes); |
| 536 | ecryptfs_printk(KERN_DEBUG, "First 8 bytes before " |
| 537 | "decryption:\n"); |
| 538 | ecryptfs_dump_hex((char *) |
| 539 | (page_address(enc_extent_page) |
| 540 | + (extent_offset * crypt_stat->extent_size)), |
| 541 | 8); |
| 542 | } |
| 543 | rc = ecryptfs_decrypt_page_offset(crypt_stat, page, |
| 544 | (extent_offset |
| 545 | * crypt_stat->extent_size), |
| 546 | enc_extent_page, 0, |
| 547 | crypt_stat->extent_size, extent_iv); |
| 548 | if (rc < 0) { |
| 549 | printk(KERN_ERR "%s: Error attempting to decrypt to page with " |
| 550 | "page->index = [%ld], extent_offset = [%ld]; " |
| 551 | "rc = [%d]\n", __FUNCTION__, page->index, extent_offset, |
| 552 | rc); |
| 553 | goto out; |
| 554 | } |
| 555 | rc = 0; |
| 556 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 557 | ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; " |
| 558 | "rc = [%d]\n", (extent_base + extent_offset), |
| 559 | rc); |
| 560 | ecryptfs_printk(KERN_DEBUG, "First 8 bytes after " |
| 561 | "decryption:\n"); |
| 562 | ecryptfs_dump_hex((char *)(page_address(page) |
| 563 | + (extent_offset |
| 564 | * crypt_stat->extent_size)), 8); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 565 | } |
| 566 | out: |
| 567 | return rc; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * ecryptfs_decrypt_page |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 572 | * @page: Page mapped from the eCryptfs inode for the file; data read |
| 573 | * and decrypted from the lower file will be written into this |
| 574 | * page |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 575 | * |
| 576 | * Decrypt an eCryptfs page. This is done on a per-extent basis. Note |
| 577 | * that eCryptfs pages may straddle the lower pages -- for instance, |
| 578 | * if the file was created on a machine with an 8K page size |
| 579 | * (resulting in an 8K header), and then the file is copied onto a |
| 580 | * host with a 32K page size, then when reading page 0 of the eCryptfs |
| 581 | * file, 24K of page 0 of the lower file will be read and decrypted, |
| 582 | * and then 8K of page 1 of the lower file will be read and decrypted. |
| 583 | * |
| 584 | * Returns zero on success; negative on error |
| 585 | */ |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 586 | int ecryptfs_decrypt_page(struct page *page) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 587 | { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 588 | struct inode *ecryptfs_inode; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 589 | struct ecryptfs_crypt_stat *crypt_stat; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 590 | char *enc_extent_virt = NULL; |
| 591 | struct page *enc_extent_page; |
| 592 | unsigned long extent_offset; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 593 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 594 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 595 | ecryptfs_inode = page->mapping->host; |
| 596 | crypt_stat = |
| 597 | &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 598 | if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 599 | rc = ecryptfs_read_lower_page_segment(page, page->index, 0, |
| 600 | PAGE_CACHE_SIZE, |
| 601 | ecryptfs_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 602 | if (rc) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 603 | printk(KERN_ERR "%s: Error attempting to copy " |
| 604 | "page at index [%ld]\n", __FUNCTION__, |
| 605 | page->index); |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 606 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 607 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 608 | enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER); |
| 609 | if (!enc_extent_virt) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 610 | rc = -ENOMEM; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 611 | ecryptfs_printk(KERN_ERR, "Error allocating memory for " |
| 612 | "encrypted extent\n"); |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 613 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 614 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 615 | enc_extent_page = virt_to_page(enc_extent_virt); |
| 616 | for (extent_offset = 0; |
| 617 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); |
| 618 | extent_offset++) { |
| 619 | loff_t offset; |
| 620 | |
| 621 | ecryptfs_lower_offset_for_extent( |
| 622 | &offset, ((page->index * (PAGE_CACHE_SIZE |
| 623 | / crypt_stat->extent_size)) |
| 624 | + extent_offset), crypt_stat); |
| 625 | rc = ecryptfs_read_lower(enc_extent_virt, offset, |
| 626 | crypt_stat->extent_size, |
| 627 | ecryptfs_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 628 | if (rc) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 629 | ecryptfs_printk(KERN_ERR, "Error attempting " |
| 630 | "to read lower page; rc = [%d]" |
| 631 | "\n", rc); |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 632 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 633 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 634 | rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page, |
| 635 | extent_offset); |
| 636 | if (rc) { |
| 637 | printk(KERN_ERR "%s: Error encrypting extent; " |
| 638 | "rc = [%d]\n", __FUNCTION__, rc); |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 639 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 640 | } |
| 641 | extent_offset++; |
| 642 | } |
| 643 | out: |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 644 | kfree(enc_extent_virt); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 645 | return rc; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * decrypt_scatterlist |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 650 | * @crypt_stat: Cryptographic context |
| 651 | * @dest_sg: The destination scatterlist to decrypt into |
| 652 | * @src_sg: The source scatterlist to decrypt from |
| 653 | * @size: The number of bytes to decrypt |
| 654 | * @iv: The initialization vector to use for the decryption |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 655 | * |
| 656 | * Returns the number of bytes decrypted; negative value on error |
| 657 | */ |
| 658 | static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, |
| 659 | struct scatterlist *dest_sg, |
| 660 | struct scatterlist *src_sg, int size, |
| 661 | unsigned char *iv) |
| 662 | { |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 663 | struct blkcipher_desc desc = { |
| 664 | .tfm = crypt_stat->tfm, |
| 665 | .info = iv, |
| 666 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP |
| 667 | }; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 668 | int rc = 0; |
| 669 | |
| 670 | /* Consider doing this once, when the file is opened */ |
| 671 | mutex_lock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 672 | rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key, |
| 673 | crypt_stat->key_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 674 | if (rc) { |
| 675 | ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n", |
| 676 | rc); |
| 677 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
| 678 | rc = -EINVAL; |
| 679 | goto out; |
| 680 | } |
| 681 | ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 682 | rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 683 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
| 684 | if (rc) { |
| 685 | ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n", |
| 686 | rc); |
| 687 | goto out; |
| 688 | } |
| 689 | rc = size; |
| 690 | out: |
| 691 | return rc; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * ecryptfs_encrypt_page_offset |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 696 | * @crypt_stat: The cryptographic context |
| 697 | * @dst_page: The page to encrypt into |
| 698 | * @dst_offset: The offset in the page to encrypt into |
| 699 | * @src_page: The page to encrypt from |
| 700 | * @src_offset: The offset in the page to encrypt from |
| 701 | * @size: The number of bytes to encrypt |
| 702 | * @iv: The initialization vector to use for the encryption |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 703 | * |
| 704 | * Returns the number of bytes encrypted |
| 705 | */ |
| 706 | static int |
| 707 | ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
| 708 | struct page *dst_page, int dst_offset, |
| 709 | struct page *src_page, int src_offset, int size, |
| 710 | unsigned char *iv) |
| 711 | { |
| 712 | struct scatterlist src_sg, dst_sg; |
| 713 | |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 714 | sg_init_table(&src_sg, 1); |
| 715 | sg_init_table(&dst_sg, 1); |
| 716 | |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame^] | 717 | sg_set_page(&src_sg, src_page, size, src_offset); |
| 718 | sg_set_page(&dst_sg, dst_page, size, dst_offset); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 719 | return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * ecryptfs_decrypt_page_offset |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 724 | * @crypt_stat: The cryptographic context |
| 725 | * @dst_page: The page to decrypt into |
| 726 | * @dst_offset: The offset in the page to decrypt into |
| 727 | * @src_page: The page to decrypt from |
| 728 | * @src_offset: The offset in the page to decrypt from |
| 729 | * @size: The number of bytes to decrypt |
| 730 | * @iv: The initialization vector to use for the decryption |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 731 | * |
| 732 | * Returns the number of bytes decrypted |
| 733 | */ |
| 734 | static int |
| 735 | ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
| 736 | struct page *dst_page, int dst_offset, |
| 737 | struct page *src_page, int src_offset, int size, |
| 738 | unsigned char *iv) |
| 739 | { |
| 740 | struct scatterlist src_sg, dst_sg; |
| 741 | |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 742 | sg_init_table(&src_sg, 1); |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame^] | 743 | sg_set_page(&src_sg, src_page, size, src_offset); |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 744 | |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame^] | 745 | sg_init_table(&dst_sg, 1); |
| 746 | sg_set_page(&dst_sg, dst_page, size, dst_offset); |
| 747 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 748 | return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv); |
| 749 | } |
| 750 | |
| 751 | #define ECRYPTFS_MAX_SCATTERLIST_LEN 4 |
| 752 | |
| 753 | /** |
| 754 | * ecryptfs_init_crypt_ctx |
| 755 | * @crypt_stat: Uninitilized crypt stats structure |
| 756 | * |
| 757 | * Initialize the crypto context. |
| 758 | * |
| 759 | * TODO: Performance: Keep a cache of initialized cipher contexts; |
| 760 | * only init if needed |
| 761 | */ |
| 762 | int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat) |
| 763 | { |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 764 | char *full_alg_name; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 765 | int rc = -EINVAL; |
| 766 | |
| 767 | if (!crypt_stat->cipher) { |
| 768 | ecryptfs_printk(KERN_ERR, "No cipher specified\n"); |
| 769 | goto out; |
| 770 | } |
| 771 | ecryptfs_printk(KERN_DEBUG, |
| 772 | "Initializing cipher [%s]; strlen = [%d]; " |
| 773 | "key_size_bits = [%d]\n", |
| 774 | crypt_stat->cipher, (int)strlen(crypt_stat->cipher), |
| 775 | crypt_stat->key_size << 3); |
| 776 | if (crypt_stat->tfm) { |
| 777 | rc = 0; |
| 778 | goto out; |
| 779 | } |
| 780 | mutex_lock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 781 | rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, |
| 782 | crypt_stat->cipher, "cbc"); |
| 783 | if (rc) |
| 784 | goto out; |
| 785 | crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0, |
| 786 | CRYPTO_ALG_ASYNC); |
| 787 | kfree(full_alg_name); |
Akinobu Mita | de88777 | 2006-11-28 12:29:49 -0800 | [diff] [blame] | 788 | if (IS_ERR(crypt_stat->tfm)) { |
| 789 | rc = PTR_ERR(crypt_stat->tfm); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 790 | ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): " |
| 791 | "Error initializing cipher [%s]\n", |
| 792 | crypt_stat->cipher); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 793 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 794 | goto out; |
| 795 | } |
Herbert Xu | f1ddcaf | 2007-01-27 10:05:15 +1100 | [diff] [blame] | 796 | crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 797 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 798 | rc = 0; |
| 799 | out: |
| 800 | return rc; |
| 801 | } |
| 802 | |
| 803 | static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat) |
| 804 | { |
| 805 | int extent_size_tmp; |
| 806 | |
| 807 | crypt_stat->extent_mask = 0xFFFFFFFF; |
| 808 | crypt_stat->extent_shift = 0; |
| 809 | if (crypt_stat->extent_size == 0) |
| 810 | return; |
| 811 | extent_size_tmp = crypt_stat->extent_size; |
| 812 | while ((extent_size_tmp & 0x01) == 0) { |
| 813 | extent_size_tmp >>= 1; |
| 814 | crypt_stat->extent_mask <<= 1; |
| 815 | crypt_stat->extent_shift++; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat) |
| 820 | { |
| 821 | /* Default values; may be overwritten as we are parsing the |
| 822 | * packets. */ |
| 823 | crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE; |
| 824 | set_extent_mask_and_shift(crypt_stat); |
| 825 | crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 826 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
| 827 | crypt_stat->num_header_extents_at_front = 0; |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 828 | else { |
| 829 | if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) |
| 830 | crypt_stat->num_header_extents_at_front = |
| 831 | (ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE |
| 832 | / crypt_stat->extent_size); |
| 833 | else |
| 834 | crypt_stat->num_header_extents_at_front = |
| 835 | (PAGE_CACHE_SIZE / crypt_stat->extent_size); |
| 836 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /** |
| 840 | * ecryptfs_compute_root_iv |
| 841 | * @crypt_stats |
| 842 | * |
| 843 | * On error, sets the root IV to all 0's. |
| 844 | */ |
| 845 | int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) |
| 846 | { |
| 847 | int rc = 0; |
| 848 | char dst[MD5_DIGEST_SIZE]; |
| 849 | |
| 850 | BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE); |
| 851 | BUG_ON(crypt_stat->iv_bytes <= 0); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 852 | if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 853 | rc = -EINVAL; |
| 854 | ecryptfs_printk(KERN_WARNING, "Session key not valid; " |
| 855 | "cannot generate root IV\n"); |
| 856 | goto out; |
| 857 | } |
| 858 | rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key, |
| 859 | crypt_stat->key_size); |
| 860 | if (rc) { |
| 861 | ecryptfs_printk(KERN_WARNING, "Error attempting to compute " |
| 862 | "MD5 while generating root IV\n"); |
| 863 | goto out; |
| 864 | } |
| 865 | memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes); |
| 866 | out: |
| 867 | if (rc) { |
| 868 | memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 869 | crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 870 | } |
| 871 | return rc; |
| 872 | } |
| 873 | |
| 874 | static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) |
| 875 | { |
| 876 | get_random_bytes(crypt_stat->key, crypt_stat->key_size); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 877 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 878 | ecryptfs_compute_root_iv(crypt_stat); |
| 879 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 880 | ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n"); |
| 881 | ecryptfs_dump_hex(crypt_stat->key, |
| 882 | crypt_stat->key_size); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | /** |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 887 | * ecryptfs_copy_mount_wide_flags_to_inode_flags |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 888 | * @crypt_stat: The inode's cryptographic context |
| 889 | * @mount_crypt_stat: The mount point's cryptographic context |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 890 | * |
| 891 | * This function propagates the mount-wide flags to individual inode |
| 892 | * flags. |
| 893 | */ |
| 894 | static void ecryptfs_copy_mount_wide_flags_to_inode_flags( |
| 895 | struct ecryptfs_crypt_stat *crypt_stat, |
| 896 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 897 | { |
| 898 | if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) |
| 899 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; |
| 900 | if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) |
| 901 | crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED; |
| 902 | } |
| 903 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 904 | static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs( |
| 905 | struct ecryptfs_crypt_stat *crypt_stat, |
| 906 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 907 | { |
| 908 | struct ecryptfs_global_auth_tok *global_auth_tok; |
| 909 | int rc = 0; |
| 910 | |
| 911 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 912 | list_for_each_entry(global_auth_tok, |
| 913 | &mount_crypt_stat->global_auth_tok_list, |
| 914 | mount_crypt_stat_list) { |
| 915 | rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig); |
| 916 | if (rc) { |
| 917 | printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc); |
| 918 | mutex_unlock( |
| 919 | &mount_crypt_stat->global_auth_tok_list_mutex); |
| 920 | goto out; |
| 921 | } |
| 922 | } |
| 923 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 924 | out: |
| 925 | return rc; |
| 926 | } |
| 927 | |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 928 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 929 | * ecryptfs_set_default_crypt_stat_vals |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 930 | * @crypt_stat: The inode's cryptographic context |
| 931 | * @mount_crypt_stat: The mount point's cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 932 | * |
| 933 | * Default values in the event that policy does not override them. |
| 934 | */ |
| 935 | static void ecryptfs_set_default_crypt_stat_vals( |
| 936 | struct ecryptfs_crypt_stat *crypt_stat, |
| 937 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 938 | { |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 939 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
| 940 | mount_crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 941 | ecryptfs_set_default_sizes(crypt_stat); |
| 942 | strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER); |
| 943 | crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES; |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 944 | crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 945 | crypt_stat->file_version = ECRYPTFS_FILE_VERSION; |
| 946 | crypt_stat->mount_crypt_stat = mount_crypt_stat; |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * ecryptfs_new_file_context |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 951 | * @ecryptfs_dentry: The eCryptfs dentry |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 952 | * |
| 953 | * If the crypto context for the file has not yet been established, |
| 954 | * this is where we do that. Establishing a new crypto context |
| 955 | * involves the following decisions: |
| 956 | * - What cipher to use? |
| 957 | * - What set of authentication tokens to use? |
| 958 | * Here we just worry about getting enough information into the |
| 959 | * authentication tokens so that we know that they are available. |
| 960 | * We associate the available authentication tokens with the new file |
| 961 | * via the set of signatures in the crypt_stat struct. Later, when |
| 962 | * the headers are actually written out, we may again defer to |
| 963 | * userspace to perform the encryption of the session key; for the |
| 964 | * foreseeable future, this will be the case with public key packets. |
| 965 | * |
| 966 | * Returns zero on success; non-zero otherwise |
| 967 | */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 968 | int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry) |
| 969 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 970 | struct ecryptfs_crypt_stat *crypt_stat = |
| 971 | &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat; |
| 972 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 973 | &ecryptfs_superblock_to_private( |
| 974 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
| 975 | int cipher_name_len; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 976 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 977 | |
| 978 | ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat); |
Michael Halcrow | af655dc | 2007-10-16 01:28:00 -0700 | [diff] [blame] | 979 | crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 980 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
| 981 | mount_crypt_stat); |
| 982 | rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat, |
| 983 | mount_crypt_stat); |
| 984 | if (rc) { |
| 985 | printk(KERN_ERR "Error attempting to copy mount-wide key sigs " |
| 986 | "to the inode key sigs; rc = [%d]\n", rc); |
| 987 | goto out; |
| 988 | } |
| 989 | cipher_name_len = |
| 990 | strlen(mount_crypt_stat->global_default_cipher_name); |
| 991 | memcpy(crypt_stat->cipher, |
| 992 | mount_crypt_stat->global_default_cipher_name, |
| 993 | cipher_name_len); |
| 994 | crypt_stat->cipher[cipher_name_len] = '\0'; |
| 995 | crypt_stat->key_size = |
| 996 | mount_crypt_stat->global_default_cipher_key_size; |
| 997 | ecryptfs_generate_new_key(crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 998 | rc = ecryptfs_init_crypt_ctx(crypt_stat); |
| 999 | if (rc) |
| 1000 | ecryptfs_printk(KERN_ERR, "Error initializing cryptographic " |
| 1001 | "context for cipher [%s]: rc = [%d]\n", |
| 1002 | crypt_stat->cipher, rc); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1003 | out: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1004 | return rc; |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * contains_ecryptfs_marker - check for the ecryptfs marker |
| 1009 | * @data: The data block in which to check |
| 1010 | * |
| 1011 | * Returns one if marker found; zero if not found |
| 1012 | */ |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1013 | static int contains_ecryptfs_marker(char *data) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1014 | { |
| 1015 | u32 m_1, m_2; |
| 1016 | |
| 1017 | memcpy(&m_1, data, 4); |
| 1018 | m_1 = be32_to_cpu(m_1); |
| 1019 | memcpy(&m_2, (data + 4), 4); |
| 1020 | m_2 = be32_to_cpu(m_2); |
| 1021 | if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2) |
| 1022 | return 1; |
| 1023 | ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; " |
| 1024 | "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2, |
| 1025 | MAGIC_ECRYPTFS_MARKER); |
| 1026 | ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = " |
| 1027 | "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER)); |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
| 1031 | struct ecryptfs_flag_map_elem { |
| 1032 | u32 file_flag; |
| 1033 | u32 local_flag; |
| 1034 | }; |
| 1035 | |
| 1036 | /* Add support for additional flags by adding elements here. */ |
| 1037 | static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = { |
| 1038 | {0x00000001, ECRYPTFS_ENABLE_HMAC}, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1039 | {0x00000002, ECRYPTFS_ENCRYPTED}, |
| 1040 | {0x00000004, ECRYPTFS_METADATA_IN_XATTR} |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1041 | }; |
| 1042 | |
| 1043 | /** |
| 1044 | * ecryptfs_process_flags |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1045 | * @crypt_stat: The cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1046 | * @page_virt: Source data to be parsed |
| 1047 | * @bytes_read: Updated with the number of bytes read |
| 1048 | * |
| 1049 | * Returns zero on success; non-zero if the flag set is invalid |
| 1050 | */ |
| 1051 | static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat, |
| 1052 | char *page_virt, int *bytes_read) |
| 1053 | { |
| 1054 | int rc = 0; |
| 1055 | int i; |
| 1056 | u32 flags; |
| 1057 | |
| 1058 | memcpy(&flags, page_virt, 4); |
| 1059 | flags = be32_to_cpu(flags); |
| 1060 | for (i = 0; i < ((sizeof(ecryptfs_flag_map) |
| 1061 | / sizeof(struct ecryptfs_flag_map_elem))); i++) |
| 1062 | if (flags & ecryptfs_flag_map[i].file_flag) { |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1063 | crypt_stat->flags |= ecryptfs_flag_map[i].local_flag; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1064 | } else |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1065 | crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1066 | /* Version is in top 8 bits of the 32-bit flag vector */ |
| 1067 | crypt_stat->file_version = ((flags >> 24) & 0xFF); |
| 1068 | (*bytes_read) = 4; |
| 1069 | return rc; |
| 1070 | } |
| 1071 | |
| 1072 | /** |
| 1073 | * write_ecryptfs_marker |
| 1074 | * @page_virt: The pointer to in a page to begin writing the marker |
| 1075 | * @written: Number of bytes written |
| 1076 | * |
| 1077 | * Marker = 0x3c81b7f5 |
| 1078 | */ |
| 1079 | static void write_ecryptfs_marker(char *page_virt, size_t *written) |
| 1080 | { |
| 1081 | u32 m_1, m_2; |
| 1082 | |
| 1083 | get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); |
| 1084 | m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER); |
| 1085 | m_1 = cpu_to_be32(m_1); |
| 1086 | memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); |
| 1087 | m_2 = cpu_to_be32(m_2); |
| 1088 | memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2, |
| 1089 | (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); |
| 1090 | (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; |
| 1091 | } |
| 1092 | |
| 1093 | static void |
| 1094 | write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat, |
| 1095 | size_t *written) |
| 1096 | { |
| 1097 | u32 flags = 0; |
| 1098 | int i; |
| 1099 | |
| 1100 | for (i = 0; i < ((sizeof(ecryptfs_flag_map) |
| 1101 | / sizeof(struct ecryptfs_flag_map_elem))); i++) |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1102 | if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1103 | flags |= ecryptfs_flag_map[i].file_flag; |
| 1104 | /* Version is in top 8 bits of the 32-bit flag vector */ |
| 1105 | flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000); |
| 1106 | flags = cpu_to_be32(flags); |
| 1107 | memcpy(page_virt, &flags, 4); |
| 1108 | (*written) = 4; |
| 1109 | } |
| 1110 | |
| 1111 | struct ecryptfs_cipher_code_str_map_elem { |
| 1112 | char cipher_str[16]; |
| 1113 | u16 cipher_code; |
| 1114 | }; |
| 1115 | |
| 1116 | /* Add support for additional ciphers by adding elements here. The |
| 1117 | * cipher_code is whatever OpenPGP applicatoins use to identify the |
| 1118 | * ciphers. List in order of probability. */ |
| 1119 | static struct ecryptfs_cipher_code_str_map_elem |
| 1120 | ecryptfs_cipher_code_str_map[] = { |
| 1121 | {"aes",RFC2440_CIPHER_AES_128 }, |
| 1122 | {"blowfish", RFC2440_CIPHER_BLOWFISH}, |
| 1123 | {"des3_ede", RFC2440_CIPHER_DES3_EDE}, |
| 1124 | {"cast5", RFC2440_CIPHER_CAST_5}, |
| 1125 | {"twofish", RFC2440_CIPHER_TWOFISH}, |
| 1126 | {"cast6", RFC2440_CIPHER_CAST_6}, |
| 1127 | {"aes", RFC2440_CIPHER_AES_192}, |
| 1128 | {"aes", RFC2440_CIPHER_AES_256} |
| 1129 | }; |
| 1130 | |
| 1131 | /** |
| 1132 | * ecryptfs_code_for_cipher_string |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1133 | * @crypt_stat: The cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1134 | * |
| 1135 | * Returns zero on no match, or the cipher code on match |
| 1136 | */ |
| 1137 | u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat) |
| 1138 | { |
| 1139 | int i; |
| 1140 | u16 code = 0; |
| 1141 | struct ecryptfs_cipher_code_str_map_elem *map = |
| 1142 | ecryptfs_cipher_code_str_map; |
| 1143 | |
| 1144 | if (strcmp(crypt_stat->cipher, "aes") == 0) { |
| 1145 | switch (crypt_stat->key_size) { |
| 1146 | case 16: |
| 1147 | code = RFC2440_CIPHER_AES_128; |
| 1148 | break; |
| 1149 | case 24: |
| 1150 | code = RFC2440_CIPHER_AES_192; |
| 1151 | break; |
| 1152 | case 32: |
| 1153 | code = RFC2440_CIPHER_AES_256; |
| 1154 | } |
| 1155 | } else { |
| 1156 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) |
| 1157 | if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){ |
| 1158 | code = map[i].cipher_code; |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | return code; |
| 1163 | } |
| 1164 | |
| 1165 | /** |
| 1166 | * ecryptfs_cipher_code_to_string |
| 1167 | * @str: Destination to write out the cipher name |
| 1168 | * @cipher_code: The code to convert to cipher name string |
| 1169 | * |
| 1170 | * Returns zero on success |
| 1171 | */ |
| 1172 | int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code) |
| 1173 | { |
| 1174 | int rc = 0; |
| 1175 | int i; |
| 1176 | |
| 1177 | str[0] = '\0'; |
| 1178 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) |
| 1179 | if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code) |
| 1180 | strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str); |
| 1181 | if (str[0] == '\0') { |
| 1182 | ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: " |
| 1183 | "[%d]\n", cipher_code); |
| 1184 | rc = -EINVAL; |
| 1185 | } |
| 1186 | return rc; |
| 1187 | } |
| 1188 | |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1189 | int ecryptfs_read_and_validate_header_region(char *data, |
| 1190 | struct inode *ecryptfs_inode) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1191 | { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1192 | struct ecryptfs_crypt_stat *crypt_stat = |
| 1193 | &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1194 | int rc; |
| 1195 | |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1196 | rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size, |
| 1197 | ecryptfs_inode); |
| 1198 | if (rc) { |
| 1199 | printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n", |
| 1200 | __FUNCTION__, rc); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1201 | goto out; |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1202 | } |
| 1203 | if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) { |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1204 | rc = -EINVAL; |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1205 | ecryptfs_printk(KERN_DEBUG, "Valid marker not found\n"); |
| 1206 | } |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1207 | out: |
| 1208 | return rc; |
| 1209 | } |
| 1210 | |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1211 | void |
| 1212 | ecryptfs_write_header_metadata(char *virt, |
| 1213 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1214 | size_t *written) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1215 | { |
| 1216 | u32 header_extent_size; |
| 1217 | u16 num_header_extents_at_front; |
| 1218 | |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1219 | header_extent_size = (u32)crypt_stat->extent_size; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1220 | num_header_extents_at_front = |
| 1221 | (u16)crypt_stat->num_header_extents_at_front; |
| 1222 | header_extent_size = cpu_to_be32(header_extent_size); |
| 1223 | memcpy(virt, &header_extent_size, 4); |
| 1224 | virt += 4; |
| 1225 | num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front); |
| 1226 | memcpy(virt, &num_header_extents_at_front, 2); |
| 1227 | (*written) = 6; |
| 1228 | } |
| 1229 | |
| 1230 | struct kmem_cache *ecryptfs_header_cache_0; |
| 1231 | struct kmem_cache *ecryptfs_header_cache_1; |
| 1232 | struct kmem_cache *ecryptfs_header_cache_2; |
| 1233 | |
| 1234 | /** |
| 1235 | * ecryptfs_write_headers_virt |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1236 | * @page_virt: The virtual address to write the headers to |
| 1237 | * @size: Set to the number of bytes written by this function |
| 1238 | * @crypt_stat: The cryptographic context |
| 1239 | * @ecryptfs_dentry: The eCryptfs dentry |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1240 | * |
| 1241 | * Format version: 1 |
| 1242 | * |
| 1243 | * Header Extent: |
| 1244 | * Octets 0-7: Unencrypted file size (big-endian) |
| 1245 | * Octets 8-15: eCryptfs special marker |
| 1246 | * Octets 16-19: Flags |
| 1247 | * Octet 16: File format version number (between 0 and 255) |
| 1248 | * Octets 17-18: Reserved |
| 1249 | * Octet 19: Bit 1 (lsb): Reserved |
| 1250 | * Bit 2: Encrypted? |
| 1251 | * Bits 3-8: Reserved |
| 1252 | * Octets 20-23: Header extent size (big-endian) |
| 1253 | * Octets 24-25: Number of header extents at front of file |
| 1254 | * (big-endian) |
| 1255 | * Octet 26: Begin RFC 2440 authentication token packet set |
| 1256 | * Data Extent 0: |
| 1257 | * Lower data (CBC encrypted) |
| 1258 | * Data Extent 1: |
| 1259 | * Lower data (CBC encrypted) |
| 1260 | * ... |
| 1261 | * |
| 1262 | * Returns zero on success |
| 1263 | */ |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1264 | static int ecryptfs_write_headers_virt(char *page_virt, size_t *size, |
| 1265 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1266 | struct dentry *ecryptfs_dentry) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1267 | { |
| 1268 | int rc; |
| 1269 | size_t written; |
| 1270 | size_t offset; |
| 1271 | |
| 1272 | offset = ECRYPTFS_FILE_SIZE_BYTES; |
| 1273 | write_ecryptfs_marker((page_virt + offset), &written); |
| 1274 | offset += written; |
| 1275 | write_ecryptfs_flags((page_virt + offset), crypt_stat, &written); |
| 1276 | offset += written; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1277 | ecryptfs_write_header_metadata((page_virt + offset), crypt_stat, |
| 1278 | &written); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1279 | offset += written; |
| 1280 | rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat, |
| 1281 | ecryptfs_dentry, &written, |
| 1282 | PAGE_CACHE_SIZE - offset); |
| 1283 | if (rc) |
| 1284 | ecryptfs_printk(KERN_WARNING, "Error generating key packet " |
| 1285 | "set; rc = [%d]\n", rc); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1286 | if (size) { |
| 1287 | offset += written; |
| 1288 | *size = offset; |
| 1289 | } |
| 1290 | return rc; |
| 1291 | } |
| 1292 | |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1293 | static int |
| 1294 | ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat, |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1295 | struct dentry *ecryptfs_dentry, |
| 1296 | char *page_virt) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1297 | { |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1298 | int current_header_page; |
| 1299 | int header_pages; |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1300 | int rc; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1301 | |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1302 | rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, page_virt, |
| 1303 | 0, PAGE_CACHE_SIZE); |
| 1304 | if (rc) { |
| 1305 | printk(KERN_ERR "%s: Error attempting to write header " |
| 1306 | "information to lower file; rc = [%d]\n", __FUNCTION__, |
| 1307 | rc); |
Michael Halcrow | 7045660 | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 1308 | goto out; |
| 1309 | } |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1310 | header_pages = ((crypt_stat->extent_size |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1311 | * crypt_stat->num_header_extents_at_front) |
| 1312 | / PAGE_CACHE_SIZE); |
| 1313 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
| 1314 | current_header_page = 1; |
| 1315 | while (current_header_page < header_pages) { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1316 | loff_t offset; |
| 1317 | |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 1318 | offset = (((loff_t)current_header_page) << PAGE_CACHE_SHIFT); |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1319 | if ((rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, |
| 1320 | page_virt, offset, |
| 1321 | PAGE_CACHE_SIZE))) { |
| 1322 | printk(KERN_ERR "%s: Error attempting to write header " |
| 1323 | "information to lower file; rc = [%d]\n", |
| 1324 | __FUNCTION__, rc); |
Michael Halcrow | 7045660 | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 1325 | goto out; |
| 1326 | } |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1327 | current_header_page++; |
| 1328 | } |
Michael Halcrow | 7045660 | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 1329 | out: |
| 1330 | return rc; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1333 | static int |
| 1334 | ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry, |
| 1335 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1336 | char *page_virt, size_t size) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1337 | { |
| 1338 | int rc; |
| 1339 | |
| 1340 | rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt, |
| 1341 | size, 0); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1342 | return rc; |
| 1343 | } |
| 1344 | |
| 1345 | /** |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1346 | * ecryptfs_write_metadata |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1347 | * @ecryptfs_dentry: The eCryptfs dentry |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1348 | * |
| 1349 | * Write the file headers out. This will likely involve a userspace |
| 1350 | * callout, in which the session key is encrypted with one or more |
| 1351 | * public keys and/or the passphrase necessary to do the encryption is |
| 1352 | * retrieved via a prompt. Exactly what happens at this point should |
| 1353 | * be policy-dependent. |
| 1354 | * |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1355 | * TODO: Support header information spanning multiple pages |
| 1356 | * |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1357 | * Returns zero on success; non-zero on error |
| 1358 | */ |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1359 | int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1360 | { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1361 | struct ecryptfs_crypt_stat *crypt_stat = |
| 1362 | &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1363 | char *page_virt; |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1364 | size_t size = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1365 | int rc = 0; |
| 1366 | |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1367 | if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
| 1368 | if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1369 | printk(KERN_ERR "Key is invalid; bailing out\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1370 | rc = -EINVAL; |
| 1371 | goto out; |
| 1372 | } |
| 1373 | } else { |
| 1374 | rc = -EINVAL; |
| 1375 | ecryptfs_printk(KERN_WARNING, |
| 1376 | "Called with crypt_stat->encrypted == 0\n"); |
| 1377 | goto out; |
| 1378 | } |
| 1379 | /* Released in this function */ |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 1380 | page_virt = kmem_cache_zalloc(ecryptfs_header_cache_0, GFP_USER); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1381 | if (!page_virt) { |
| 1382 | ecryptfs_printk(KERN_ERR, "Out of memory\n"); |
| 1383 | rc = -ENOMEM; |
| 1384 | goto out; |
| 1385 | } |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1386 | rc = ecryptfs_write_headers_virt(page_virt, &size, crypt_stat, |
| 1387 | ecryptfs_dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1388 | if (unlikely(rc)) { |
| 1389 | ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n"); |
| 1390 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
| 1391 | goto out_free; |
| 1392 | } |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1393 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
| 1394 | rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, |
| 1395 | crypt_stat, page_virt, |
| 1396 | size); |
| 1397 | else |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1398 | rc = ecryptfs_write_metadata_to_contents(crypt_stat, |
| 1399 | ecryptfs_dentry, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1400 | page_virt); |
| 1401 | if (rc) { |
| 1402 | printk(KERN_ERR "Error writing metadata out to lower file; " |
| 1403 | "rc = [%d]\n", rc); |
| 1404 | goto out_free; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1405 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1406 | out_free: |
| 1407 | kmem_cache_free(ecryptfs_header_cache_0, page_virt); |
| 1408 | out: |
| 1409 | return rc; |
| 1410 | } |
| 1411 | |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1412 | #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0 |
| 1413 | #define ECRYPTFS_VALIDATE_HEADER_SIZE 1 |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1414 | static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1415 | char *virt, int *bytes_read, |
| 1416 | int validate_header_size) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1417 | { |
| 1418 | int rc = 0; |
| 1419 | u32 header_extent_size; |
| 1420 | u16 num_header_extents_at_front; |
| 1421 | |
Michael Halcrow | ecbdc93 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 1422 | memcpy(&header_extent_size, virt, sizeof(u32)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1423 | header_extent_size = be32_to_cpu(header_extent_size); |
Michael Halcrow | ecbdc93 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 1424 | virt += sizeof(u32); |
| 1425 | memcpy(&num_header_extents_at_front, virt, sizeof(u16)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1426 | num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1427 | crypt_stat->num_header_extents_at_front = |
| 1428 | (int)num_header_extents_at_front; |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1429 | (*bytes_read) = (sizeof(u32) + sizeof(u16)); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1430 | if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE) |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1431 | && ((crypt_stat->extent_size |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1432 | * crypt_stat->num_header_extents_at_front) |
| 1433 | < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1434 | rc = -EINVAL; |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1435 | printk(KERN_WARNING "Invalid number of header extents: [%zd]\n", |
| 1436 | crypt_stat->num_header_extents_at_front); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1437 | } |
| 1438 | return rc; |
| 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * set_default_header_data |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1443 | * @crypt_stat: The cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1444 | * |
| 1445 | * For version 0 file format; this function is only for backwards |
| 1446 | * compatibility for files created with the prior versions of |
| 1447 | * eCryptfs. |
| 1448 | */ |
| 1449 | static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat) |
| 1450 | { |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1451 | crypt_stat->num_header_extents_at_front = 2; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * ecryptfs_read_headers_virt |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1456 | * @page_virt: The virtual address into which to read the headers |
| 1457 | * @crypt_stat: The cryptographic context |
| 1458 | * @ecryptfs_dentry: The eCryptfs dentry |
| 1459 | * @validate_header_size: Whether to validate the header size while reading |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1460 | * |
| 1461 | * Read/parse the header data. The header format is detailed in the |
| 1462 | * comment block for the ecryptfs_write_headers_virt() function. |
| 1463 | * |
| 1464 | * Returns zero on success |
| 1465 | */ |
| 1466 | static int ecryptfs_read_headers_virt(char *page_virt, |
| 1467 | struct ecryptfs_crypt_stat *crypt_stat, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1468 | struct dentry *ecryptfs_dentry, |
| 1469 | int validate_header_size) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1470 | { |
| 1471 | int rc = 0; |
| 1472 | int offset; |
| 1473 | int bytes_read; |
| 1474 | |
| 1475 | ecryptfs_set_default_sizes(crypt_stat); |
| 1476 | crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private( |
| 1477 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
| 1478 | offset = ECRYPTFS_FILE_SIZE_BYTES; |
| 1479 | rc = contains_ecryptfs_marker(page_virt + offset); |
| 1480 | if (rc == 0) { |
| 1481 | rc = -EINVAL; |
| 1482 | goto out; |
| 1483 | } |
| 1484 | offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; |
| 1485 | rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset), |
| 1486 | &bytes_read); |
| 1487 | if (rc) { |
| 1488 | ecryptfs_printk(KERN_WARNING, "Error processing flags\n"); |
| 1489 | goto out; |
| 1490 | } |
| 1491 | if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) { |
| 1492 | ecryptfs_printk(KERN_WARNING, "File version is [%d]; only " |
| 1493 | "file version [%d] is supported by this " |
| 1494 | "version of eCryptfs\n", |
| 1495 | crypt_stat->file_version, |
| 1496 | ECRYPTFS_SUPPORTED_FILE_VERSION); |
| 1497 | rc = -EINVAL; |
| 1498 | goto out; |
| 1499 | } |
| 1500 | offset += bytes_read; |
| 1501 | if (crypt_stat->file_version >= 1) { |
| 1502 | rc = parse_header_metadata(crypt_stat, (page_virt + offset), |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1503 | &bytes_read, validate_header_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1504 | if (rc) { |
| 1505 | ecryptfs_printk(KERN_WARNING, "Error reading header " |
| 1506 | "metadata; rc = [%d]\n", rc); |
| 1507 | } |
| 1508 | offset += bytes_read; |
| 1509 | } else |
| 1510 | set_default_header_data(crypt_stat); |
| 1511 | rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset), |
| 1512 | ecryptfs_dentry); |
| 1513 | out: |
| 1514 | return rc; |
| 1515 | } |
| 1516 | |
| 1517 | /** |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1518 | * ecryptfs_read_xattr_region |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1519 | * @page_virt: The vitual address into which to read the xattr data |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1520 | * @ecryptfs_inode: The eCryptfs inode |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1521 | * |
| 1522 | * Attempts to read the crypto metadata from the extended attribute |
| 1523 | * region of the lower file. |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1524 | * |
| 1525 | * Returns zero on success; non-zero on error |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1526 | */ |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1527 | int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1528 | { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1529 | struct dentry *lower_dentry = |
| 1530 | ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1531 | ssize_t size; |
| 1532 | int rc = 0; |
| 1533 | |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1534 | size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME, |
| 1535 | page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1536 | if (size < 0) { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1537 | printk(KERN_ERR "Error attempting to read the [%s] " |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1538 | "xattr from the lower file; return value = [%zd]\n", |
| 1539 | ECRYPTFS_XATTR_NAME, size); |
| 1540 | rc = -EINVAL; |
| 1541 | goto out; |
| 1542 | } |
| 1543 | out: |
| 1544 | return rc; |
| 1545 | } |
| 1546 | |
| 1547 | int ecryptfs_read_and_validate_xattr_region(char *page_virt, |
| 1548 | struct dentry *ecryptfs_dentry) |
| 1549 | { |
| 1550 | int rc; |
| 1551 | |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1552 | rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1553 | if (rc) |
| 1554 | goto out; |
| 1555 | if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) { |
| 1556 | printk(KERN_WARNING "Valid data found in [%s] xattr, but " |
| 1557 | "the marker is invalid\n", ECRYPTFS_XATTR_NAME); |
| 1558 | rc = -EINVAL; |
| 1559 | } |
| 1560 | out: |
| 1561 | return rc; |
| 1562 | } |
| 1563 | |
| 1564 | /** |
| 1565 | * ecryptfs_read_metadata |
| 1566 | * |
| 1567 | * Common entry point for reading file metadata. From here, we could |
| 1568 | * retrieve the header information from the header region of the file, |
| 1569 | * the xattr region of the file, or some other repostory that is |
| 1570 | * stored separately from the file itself. The current implementation |
| 1571 | * supports retrieving the metadata information from the file contents |
| 1572 | * and from the xattr region. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1573 | * |
| 1574 | * Returns zero if valid headers found and parsed; non-zero otherwise |
| 1575 | */ |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1576 | int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1577 | { |
| 1578 | int rc = 0; |
| 1579 | char *page_virt = NULL; |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1580 | struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1581 | struct ecryptfs_crypt_stat *crypt_stat = |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1582 | &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1583 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 1584 | &ecryptfs_superblock_to_private( |
| 1585 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1586 | |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1587 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
| 1588 | mount_crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1589 | /* Read the first page from the underlying file */ |
Christoph Lameter | f7267c0 | 2006-12-06 20:33:15 -0800 | [diff] [blame] | 1590 | page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1591 | if (!page_virt) { |
| 1592 | rc = -ENOMEM; |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1593 | printk(KERN_ERR "%s: Unable to allocate page_virt\n", |
| 1594 | __FUNCTION__); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1595 | goto out; |
| 1596 | } |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1597 | rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size, |
| 1598 | ecryptfs_inode); |
| 1599 | if (!rc) |
| 1600 | rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, |
| 1601 | ecryptfs_dentry, |
| 1602 | ECRYPTFS_VALIDATE_HEADER_SIZE); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1603 | if (rc) { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1604 | rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1605 | if (rc) { |
| 1606 | printk(KERN_DEBUG "Valid eCryptfs headers not found in " |
| 1607 | "file header region or xattr region\n"); |
| 1608 | rc = -EINVAL; |
| 1609 | goto out; |
| 1610 | } |
| 1611 | rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, |
| 1612 | ecryptfs_dentry, |
| 1613 | ECRYPTFS_DONT_VALIDATE_HEADER_SIZE); |
| 1614 | if (rc) { |
| 1615 | printk(KERN_DEBUG "Valid eCryptfs headers not found in " |
| 1616 | "file xattr region either\n"); |
| 1617 | rc = -EINVAL; |
| 1618 | } |
| 1619 | if (crypt_stat->mount_crypt_stat->flags |
| 1620 | & ECRYPTFS_XATTR_METADATA_ENABLED) { |
| 1621 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; |
| 1622 | } else { |
| 1623 | printk(KERN_WARNING "Attempt to access file with " |
| 1624 | "crypto metadata only in the extended attribute " |
| 1625 | "region, but eCryptfs was mounted without " |
| 1626 | "xattr support enabled. eCryptfs will not treat " |
| 1627 | "this like an encrypted file.\n"); |
| 1628 | rc = -EINVAL; |
| 1629 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1630 | } |
| 1631 | out: |
| 1632 | if (page_virt) { |
| 1633 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
| 1634 | kmem_cache_free(ecryptfs_header_cache_1, page_virt); |
| 1635 | } |
| 1636 | return rc; |
| 1637 | } |
| 1638 | |
| 1639 | /** |
| 1640 | * ecryptfs_encode_filename - converts a plaintext file name to cipher text |
| 1641 | * @crypt_stat: The crypt_stat struct associated with the file anem to encode |
| 1642 | * @name: The plaintext name |
| 1643 | * @length: The length of the plaintext |
| 1644 | * @encoded_name: The encypted name |
| 1645 | * |
| 1646 | * Encrypts and encodes a filename into something that constitutes a |
| 1647 | * valid filename for a filesystem, with printable characters. |
| 1648 | * |
| 1649 | * We assume that we have a properly initialized crypto context, |
| 1650 | * pointed to by crypt_stat->tfm. |
| 1651 | * |
| 1652 | * TODO: Implement filename decoding and decryption here, in place of |
| 1653 | * memcpy. We are keeping the framework around for now to (1) |
| 1654 | * facilitate testing of the components needed to implement filename |
| 1655 | * encryption and (2) to provide a code base from which other |
| 1656 | * developers in the community can easily implement this feature. |
| 1657 | * |
| 1658 | * Returns the length of encoded filename; negative if error |
| 1659 | */ |
| 1660 | int |
| 1661 | ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat, |
| 1662 | const char *name, int length, char **encoded_name) |
| 1663 | { |
| 1664 | int error = 0; |
| 1665 | |
| 1666 | (*encoded_name) = kmalloc(length + 2, GFP_KERNEL); |
| 1667 | if (!(*encoded_name)) { |
| 1668 | error = -ENOMEM; |
| 1669 | goto out; |
| 1670 | } |
| 1671 | /* TODO: Filename encryption is a scheduled feature for a |
| 1672 | * future version of eCryptfs. This function is here only for |
| 1673 | * the purpose of providing a framework for other developers |
| 1674 | * to easily implement filename encryption. Hint: Replace this |
| 1675 | * memcpy() with a call to encrypt and encode the |
| 1676 | * filename, the set the length accordingly. */ |
| 1677 | memcpy((void *)(*encoded_name), (void *)name, length); |
| 1678 | (*encoded_name)[length] = '\0'; |
| 1679 | error = length + 1; |
| 1680 | out: |
| 1681 | return error; |
| 1682 | } |
| 1683 | |
| 1684 | /** |
| 1685 | * ecryptfs_decode_filename - converts the cipher text name to plaintext |
| 1686 | * @crypt_stat: The crypt_stat struct associated with the file |
| 1687 | * @name: The filename in cipher text |
| 1688 | * @length: The length of the cipher text name |
| 1689 | * @decrypted_name: The plaintext name |
| 1690 | * |
| 1691 | * Decodes and decrypts the filename. |
| 1692 | * |
| 1693 | * We assume that we have a properly initialized crypto context, |
| 1694 | * pointed to by crypt_stat->tfm. |
| 1695 | * |
| 1696 | * TODO: Implement filename decoding and decryption here, in place of |
| 1697 | * memcpy. We are keeping the framework around for now to (1) |
| 1698 | * facilitate testing of the components needed to implement filename |
| 1699 | * encryption and (2) to provide a code base from which other |
| 1700 | * developers in the community can easily implement this feature. |
| 1701 | * |
| 1702 | * Returns the length of decoded filename; negative if error |
| 1703 | */ |
| 1704 | int |
| 1705 | ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat, |
| 1706 | const char *name, int length, char **decrypted_name) |
| 1707 | { |
| 1708 | int error = 0; |
| 1709 | |
| 1710 | (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL); |
| 1711 | if (!(*decrypted_name)) { |
| 1712 | error = -ENOMEM; |
| 1713 | goto out; |
| 1714 | } |
| 1715 | /* TODO: Filename encryption is a scheduled feature for a |
| 1716 | * future version of eCryptfs. This function is here only for |
| 1717 | * the purpose of providing a framework for other developers |
| 1718 | * to easily implement filename encryption. Hint: Replace this |
| 1719 | * memcpy() with a call to decode and decrypt the |
| 1720 | * filename, the set the length accordingly. */ |
| 1721 | memcpy((void *)(*decrypted_name), (void *)name, length); |
| 1722 | (*decrypted_name)[length + 1] = '\0'; /* Only for convenience |
| 1723 | * in printing out the |
| 1724 | * string in debug |
| 1725 | * messages */ |
| 1726 | error = length; |
| 1727 | out: |
| 1728 | return error; |
| 1729 | } |
| 1730 | |
| 1731 | /** |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1732 | * ecryptfs_process_key_cipher - Perform key cipher initialization. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1733 | * @key_tfm: Crypto context for key material, set by this function |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1734 | * @cipher_name: Name of the cipher |
| 1735 | * @key_size: Size of the key in bytes |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1736 | * |
| 1737 | * Returns zero on success. Any crypto_tfm structs allocated here |
| 1738 | * should be released by other functions, such as on a superblock put |
| 1739 | * event, regardless of whether this function succeeds for fails. |
| 1740 | */ |
Michael Halcrow | cd9d67d | 2007-10-16 01:28:04 -0700 | [diff] [blame] | 1741 | static int |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1742 | ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm, |
| 1743 | char *cipher_name, size_t *key_size) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1744 | { |
| 1745 | char dummy_key[ECRYPTFS_MAX_KEY_BYTES]; |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1746 | char *full_alg_name; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1747 | int rc; |
| 1748 | |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1749 | *key_tfm = NULL; |
| 1750 | if (*key_size > ECRYPTFS_MAX_KEY_BYTES) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1751 | rc = -EINVAL; |
| 1752 | printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum " |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1753 | "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1754 | goto out; |
| 1755 | } |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1756 | rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name, |
| 1757 | "ecb"); |
| 1758 | if (rc) |
| 1759 | goto out; |
| 1760 | *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC); |
| 1761 | kfree(full_alg_name); |
| 1762 | if (IS_ERR(*key_tfm)) { |
| 1763 | rc = PTR_ERR(*key_tfm); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1764 | printk(KERN_ERR "Unable to allocate crypto cipher with name " |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1765 | "[%s]; rc = [%d]\n", cipher_name, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1766 | goto out; |
| 1767 | } |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1768 | crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
| 1769 | if (*key_size == 0) { |
| 1770 | struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm); |
| 1771 | |
| 1772 | *key_size = alg->max_keysize; |
| 1773 | } |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1774 | get_random_bytes(dummy_key, *key_size); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1775 | rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1776 | if (rc) { |
| 1777 | printk(KERN_ERR "Error attempting to set key of size [%Zd] for " |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1778 | "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1779 | rc = -EINVAL; |
| 1780 | goto out; |
| 1781 | } |
| 1782 | out: |
| 1783 | return rc; |
| 1784 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1785 | |
| 1786 | struct kmem_cache *ecryptfs_key_tfm_cache; |
| 1787 | struct list_head key_tfm_list; |
| 1788 | struct mutex key_tfm_list_mutex; |
| 1789 | |
| 1790 | int ecryptfs_init_crypto(void) |
| 1791 | { |
| 1792 | mutex_init(&key_tfm_list_mutex); |
| 1793 | INIT_LIST_HEAD(&key_tfm_list); |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 1797 | int ecryptfs_destroy_crypto(void) |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1798 | { |
| 1799 | struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp; |
| 1800 | |
| 1801 | mutex_lock(&key_tfm_list_mutex); |
| 1802 | list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list, |
| 1803 | key_tfm_list) { |
| 1804 | list_del(&key_tfm->key_tfm_list); |
| 1805 | if (key_tfm->key_tfm) |
| 1806 | crypto_free_blkcipher(key_tfm->key_tfm); |
| 1807 | kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm); |
| 1808 | } |
| 1809 | mutex_unlock(&key_tfm_list_mutex); |
| 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | int |
| 1814 | ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name, |
| 1815 | size_t key_size) |
| 1816 | { |
| 1817 | struct ecryptfs_key_tfm *tmp_tfm; |
| 1818 | int rc = 0; |
| 1819 | |
| 1820 | tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL); |
| 1821 | if (key_tfm != NULL) |
| 1822 | (*key_tfm) = tmp_tfm; |
| 1823 | if (!tmp_tfm) { |
| 1824 | rc = -ENOMEM; |
| 1825 | printk(KERN_ERR "Error attempting to allocate from " |
| 1826 | "ecryptfs_key_tfm_cache\n"); |
| 1827 | goto out; |
| 1828 | } |
| 1829 | mutex_init(&tmp_tfm->key_tfm_mutex); |
| 1830 | strncpy(tmp_tfm->cipher_name, cipher_name, |
| 1831 | ECRYPTFS_MAX_CIPHER_NAME_SIZE); |
| 1832 | tmp_tfm->key_size = key_size; |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1833 | rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm, |
| 1834 | tmp_tfm->cipher_name, |
| 1835 | &tmp_tfm->key_size); |
| 1836 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1837 | printk(KERN_ERR "Error attempting to initialize key TFM " |
| 1838 | "cipher with name = [%s]; rc = [%d]\n", |
| 1839 | tmp_tfm->cipher_name, rc); |
| 1840 | kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm); |
| 1841 | if (key_tfm != NULL) |
| 1842 | (*key_tfm) = NULL; |
| 1843 | goto out; |
| 1844 | } |
| 1845 | mutex_lock(&key_tfm_list_mutex); |
| 1846 | list_add(&tmp_tfm->key_tfm_list, &key_tfm_list); |
| 1847 | mutex_unlock(&key_tfm_list_mutex); |
| 1848 | out: |
| 1849 | return rc; |
| 1850 | } |
| 1851 | |
| 1852 | int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm, |
| 1853 | struct mutex **tfm_mutex, |
| 1854 | char *cipher_name) |
| 1855 | { |
| 1856 | struct ecryptfs_key_tfm *key_tfm; |
| 1857 | int rc = 0; |
| 1858 | |
| 1859 | (*tfm) = NULL; |
| 1860 | (*tfm_mutex) = NULL; |
| 1861 | mutex_lock(&key_tfm_list_mutex); |
| 1862 | list_for_each_entry(key_tfm, &key_tfm_list, key_tfm_list) { |
| 1863 | if (strcmp(key_tfm->cipher_name, cipher_name) == 0) { |
| 1864 | (*tfm) = key_tfm->key_tfm; |
| 1865 | (*tfm_mutex) = &key_tfm->key_tfm_mutex; |
| 1866 | mutex_unlock(&key_tfm_list_mutex); |
| 1867 | goto out; |
| 1868 | } |
| 1869 | } |
| 1870 | mutex_unlock(&key_tfm_list_mutex); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1871 | rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0); |
| 1872 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1873 | printk(KERN_ERR "Error adding new key_tfm to list; rc = [%d]\n", |
| 1874 | rc); |
| 1875 | goto out; |
| 1876 | } |
| 1877 | (*tfm) = key_tfm->key_tfm; |
| 1878 | (*tfm_mutex) = &key_tfm->key_tfm_mutex; |
| 1879 | out: |
| 1880 | return rc; |
| 1881 | } |