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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Harvey Harrison | 29335c6 | 2008-07-23 21:30:06 -0700 | [diff] [blame] | 37 | #include <asm/unaligned.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 38 | #include "ecryptfs_kernel.h" |
| 39 | |
Tyler Hicks | 00a6994 | 2013-04-05 23:26:22 -0700 | [diff] [blame^] | 40 | #define DECRYPT 0 |
| 41 | #define ENCRYPT 1 |
| 42 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 43 | static int |
| 44 | ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
Tyler Hicks | 28916d1 | 2013-04-15 16:37:27 -0700 | [diff] [blame] | 45 | struct page *dst_page, struct page *src_page, |
| 46 | int offset, int size, unsigned char *iv); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 47 | static int |
| 48 | ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
Tyler Hicks | 28916d1 | 2013-04-15 16:37:27 -0700 | [diff] [blame] | 49 | struct page *dst_page, struct page *src_page, |
| 50 | int offset, int size, unsigned char *iv); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * ecryptfs_to_hex |
| 54 | * @dst: Buffer to take hex character representation of contents of |
| 55 | * src; must be at least of size (src_size * 2) |
| 56 | * @src: Buffer to be converted to a hex string respresentation |
| 57 | * @src_size: number of bytes to convert |
| 58 | */ |
| 59 | void ecryptfs_to_hex(char *dst, char *src, size_t src_size) |
| 60 | { |
| 61 | int x; |
| 62 | |
| 63 | for (x = 0; x < src_size; x++) |
| 64 | sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * ecryptfs_from_hex |
| 69 | * @dst: Buffer to take the bytes from src hex; must be at least of |
| 70 | * size (src_size / 2) |
| 71 | * @src: Buffer to be converted from a hex string respresentation to raw value |
| 72 | * @dst_size: size of dst buffer, or number of hex characters pairs to convert |
| 73 | */ |
| 74 | void ecryptfs_from_hex(char *dst, char *src, int dst_size) |
| 75 | { |
| 76 | int x; |
| 77 | char tmp[3] = { 0, }; |
| 78 | |
| 79 | for (x = 0; x < dst_size; x++) { |
| 80 | tmp[0] = src[x * 2]; |
| 81 | tmp[1] = src[x * 2 + 1]; |
| 82 | dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * ecryptfs_calculate_md5 - calculates the md5 of @src |
| 88 | * @dst: Pointer to 16 bytes of allocated memory |
| 89 | * @crypt_stat: Pointer to crypt_stat struct for the current inode |
| 90 | * @src: Data to be md5'd |
| 91 | * @len: Length of @src |
| 92 | * |
| 93 | * Uses the allocated crypto context that crypt_stat references to |
| 94 | * generate the MD5 sum of the contents of src. |
| 95 | */ |
| 96 | static int ecryptfs_calculate_md5(char *dst, |
| 97 | struct ecryptfs_crypt_stat *crypt_stat, |
| 98 | char *src, int len) |
| 99 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 100 | struct scatterlist sg; |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 101 | struct hash_desc desc = { |
| 102 | .tfm = crypt_stat->hash_tfm, |
| 103 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP |
| 104 | }; |
| 105 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 106 | |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 107 | mutex_lock(&crypt_stat->cs_hash_tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 108 | sg_init_one(&sg, (u8 *)src, len); |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 109 | if (!desc.tfm) { |
| 110 | desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0, |
| 111 | CRYPTO_ALG_ASYNC); |
| 112 | if (IS_ERR(desc.tfm)) { |
| 113 | rc = PTR_ERR(desc.tfm); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 114 | ecryptfs_printk(KERN_ERR, "Error attempting to " |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 115 | "allocate crypto context; rc = [%d]\n", |
| 116 | rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 117 | goto out; |
| 118 | } |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 119 | crypt_stat->hash_tfm = desc.tfm; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 120 | } |
Michael Halcrow | 8a29f2b | 2007-11-05 14:51:04 -0800 | [diff] [blame] | 121 | rc = crypto_hash_init(&desc); |
| 122 | if (rc) { |
| 123 | printk(KERN_ERR |
| 124 | "%s: Error initializing crypto hash; rc = [%d]\n", |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 125 | __func__, rc); |
Michael Halcrow | 8a29f2b | 2007-11-05 14:51:04 -0800 | [diff] [blame] | 126 | goto out; |
| 127 | } |
| 128 | rc = crypto_hash_update(&desc, &sg, len); |
| 129 | if (rc) { |
| 130 | printk(KERN_ERR |
| 131 | "%s: Error updating crypto hash; rc = [%d]\n", |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 132 | __func__, rc); |
Michael Halcrow | 8a29f2b | 2007-11-05 14:51:04 -0800 | [diff] [blame] | 133 | goto out; |
| 134 | } |
| 135 | rc = crypto_hash_final(&desc, dst); |
| 136 | if (rc) { |
| 137 | printk(KERN_ERR |
| 138 | "%s: Error finalizing crypto hash; rc = [%d]\n", |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 139 | __func__, rc); |
Michael Halcrow | 8a29f2b | 2007-11-05 14:51:04 -0800 | [diff] [blame] | 140 | goto out; |
| 141 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 142 | out: |
Michael Halcrow | 8a29f2b | 2007-11-05 14:51:04 -0800 | [diff] [blame] | 143 | mutex_unlock(&crypt_stat->cs_hash_tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 144 | return rc; |
| 145 | } |
| 146 | |
Michael Halcrow | cd9d67d | 2007-10-16 01:28:04 -0700 | [diff] [blame] | 147 | static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name, |
| 148 | char *cipher_name, |
| 149 | char *chaining_modifier) |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 150 | { |
| 151 | int cipher_name_len = strlen(cipher_name); |
| 152 | int chaining_modifier_len = strlen(chaining_modifier); |
| 153 | int algified_name_len; |
| 154 | int rc; |
| 155 | |
| 156 | algified_name_len = (chaining_modifier_len + cipher_name_len + 3); |
| 157 | (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL); |
Michael Halcrow | 7bd473f | 2006-11-02 22:06:56 -0800 | [diff] [blame] | 158 | if (!(*algified_name)) { |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 159 | rc = -ENOMEM; |
| 160 | goto out; |
| 161 | } |
| 162 | snprintf((*algified_name), algified_name_len, "%s(%s)", |
| 163 | chaining_modifier, cipher_name); |
| 164 | rc = 0; |
| 165 | out: |
| 166 | return rc; |
| 167 | } |
| 168 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 169 | /** |
| 170 | * ecryptfs_derive_iv |
| 171 | * @iv: destination for the derived iv vale |
| 172 | * @crypt_stat: Pointer to crypt_stat struct for the current inode |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 173 | * @offset: Offset of the extent whose IV we are to derive |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 174 | * |
| 175 | * Generate the initialization vector from the given root IV and page |
| 176 | * offset. |
| 177 | * |
| 178 | * Returns zero on success; non-zero on error. |
| 179 | */ |
Michael Halcrow | a34f60f | 2009-01-06 14:41:58 -0800 | [diff] [blame] | 180 | int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat, |
| 181 | loff_t offset) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 182 | { |
| 183 | int rc = 0; |
| 184 | char dst[MD5_DIGEST_SIZE]; |
| 185 | char src[ECRYPTFS_MAX_IV_BYTES + 16]; |
| 186 | |
| 187 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 188 | ecryptfs_printk(KERN_DEBUG, "root iv:\n"); |
| 189 | ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes); |
| 190 | } |
| 191 | /* TODO: It is probably secure to just cast the least |
| 192 | * significant bits of the root IV into an unsigned long and |
| 193 | * add the offset to that rather than go through all this |
| 194 | * hashing business. -Halcrow */ |
| 195 | memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes); |
| 196 | memset((src + crypt_stat->iv_bytes), 0, 16); |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 197 | snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 198 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 199 | ecryptfs_printk(KERN_DEBUG, "source:\n"); |
| 200 | ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16)); |
| 201 | } |
| 202 | rc = ecryptfs_calculate_md5(dst, crypt_stat, src, |
| 203 | (crypt_stat->iv_bytes + 16)); |
| 204 | if (rc) { |
| 205 | ecryptfs_printk(KERN_WARNING, "Error attempting to compute " |
| 206 | "MD5 while generating IV for a page\n"); |
| 207 | goto out; |
| 208 | } |
| 209 | memcpy(iv, dst, crypt_stat->iv_bytes); |
| 210 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 211 | ecryptfs_printk(KERN_DEBUG, "derived iv:\n"); |
| 212 | ecryptfs_dump_hex(iv, crypt_stat->iv_bytes); |
| 213 | } |
| 214 | out: |
| 215 | return rc; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * ecryptfs_init_crypt_stat |
| 220 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. |
| 221 | * |
| 222 | * Initialize the crypt_stat structure. |
| 223 | */ |
| 224 | void |
| 225 | ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) |
| 226 | { |
| 227 | memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 228 | INIT_LIST_HEAD(&crypt_stat->keysig_list); |
| 229 | mutex_init(&crypt_stat->keysig_list_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 230 | mutex_init(&crypt_stat->cs_mutex); |
| 231 | mutex_init(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 232 | mutex_init(&crypt_stat->cs_hash_tfm_mutex); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 233 | crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 237 | * ecryptfs_destroy_crypt_stat |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 238 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. |
| 239 | * |
| 240 | * Releases all memory associated with a crypt_stat struct. |
| 241 | */ |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 242 | void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 243 | { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 244 | struct ecryptfs_key_sig *key_sig, *key_sig_tmp; |
| 245 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 246 | if (crypt_stat->tfm) |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 247 | crypto_free_ablkcipher(crypt_stat->tfm); |
Michael Halcrow | 565d9724 | 2006-10-30 22:07:17 -0800 | [diff] [blame] | 248 | if (crypt_stat->hash_tfm) |
| 249 | crypto_free_hash(crypt_stat->hash_tfm); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 250 | list_for_each_entry_safe(key_sig, key_sig_tmp, |
| 251 | &crypt_stat->keysig_list, crypt_stat_list) { |
| 252 | list_del(&key_sig->crypt_stat_list); |
| 253 | kmem_cache_free(ecryptfs_key_sig_cache, key_sig); |
| 254 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 255 | memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); |
| 256 | } |
| 257 | |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 258 | void ecryptfs_destroy_mount_crypt_stat( |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 259 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 260 | { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 261 | struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp; |
| 262 | |
| 263 | if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED)) |
| 264 | return; |
| 265 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 266 | list_for_each_entry_safe(auth_tok, auth_tok_tmp, |
| 267 | &mount_crypt_stat->global_auth_tok_list, |
| 268 | mount_crypt_stat_list) { |
| 269 | list_del(&auth_tok->mount_crypt_stat_list); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 270 | if (auth_tok->global_auth_tok_key |
| 271 | && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID)) |
| 272 | key_put(auth_tok->global_auth_tok_key); |
| 273 | kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok); |
| 274 | } |
| 275 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 276 | memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat)); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * virt_to_scatterlist |
| 281 | * @addr: Virtual address |
| 282 | * @size: Size of data; should be an even multiple of the block size |
| 283 | * @sg: Pointer to scatterlist array; set to NULL to obtain only |
| 284 | * the number of scatterlist structs required in array |
| 285 | * @sg_size: Max array size |
| 286 | * |
| 287 | * Fills in a scatterlist array with page references for a passed |
| 288 | * virtual address. |
| 289 | * |
| 290 | * Returns the number of scatterlist structs in array used |
| 291 | */ |
| 292 | int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg, |
| 293 | int sg_size) |
| 294 | { |
| 295 | int i = 0; |
| 296 | struct page *pg; |
| 297 | int offset; |
| 298 | int remainder_of_page; |
| 299 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 300 | sg_init_table(sg, sg_size); |
| 301 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 302 | while (size > 0 && i < sg_size) { |
| 303 | pg = virt_to_page(addr); |
| 304 | offset = offset_in_page(addr); |
Dan Carpenter | a07c48a | 2013-01-26 10:48:42 +0300 | [diff] [blame] | 305 | sg_set_page(&sg[i], pg, 0, offset); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 306 | remainder_of_page = PAGE_CACHE_SIZE - offset; |
| 307 | if (size >= remainder_of_page) { |
Dan Carpenter | a07c48a | 2013-01-26 10:48:42 +0300 | [diff] [blame] | 308 | sg[i].length = remainder_of_page; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 309 | addr += remainder_of_page; |
| 310 | size -= remainder_of_page; |
| 311 | } else { |
Dan Carpenter | a07c48a | 2013-01-26 10:48:42 +0300 | [diff] [blame] | 312 | sg[i].length = size; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 313 | addr += size; |
| 314 | size = 0; |
| 315 | } |
| 316 | i++; |
| 317 | } |
| 318 | if (size > 0) |
| 319 | return -ENOMEM; |
| 320 | return i; |
| 321 | } |
| 322 | |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 323 | struct extent_crypt_result { |
| 324 | struct completion completion; |
| 325 | int rc; |
| 326 | }; |
| 327 | |
| 328 | static void extent_crypt_complete(struct crypto_async_request *req, int rc) |
| 329 | { |
| 330 | struct extent_crypt_result *ecr = req->data; |
| 331 | |
| 332 | if (rc == -EINPROGRESS) |
| 333 | return; |
| 334 | |
| 335 | ecr->rc = rc; |
| 336 | complete(&ecr->completion); |
| 337 | } |
| 338 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 339 | /** |
Tyler Hicks | 00a6994 | 2013-04-05 23:26:22 -0700 | [diff] [blame^] | 340 | * crypt_scatterlist |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 341 | * @crypt_stat: Pointer to the crypt_stat struct to initialize. |
Tyler Hicks | 00a6994 | 2013-04-05 23:26:22 -0700 | [diff] [blame^] | 342 | * @dest_sg: Destination of the data after performing the crypto operation |
| 343 | * @src_sg: Data to be encrypted or decrypted |
| 344 | * @size: Length of data |
| 345 | * @iv: IV to use |
| 346 | * @op: ENCRYPT or DECRYPT to indicate the desired operation |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 347 | * |
Tyler Hicks | 00a6994 | 2013-04-05 23:26:22 -0700 | [diff] [blame^] | 348 | * Returns the number of bytes encrypted or decrypted; negative value on error |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 349 | */ |
Tyler Hicks | 00a6994 | 2013-04-05 23:26:22 -0700 | [diff] [blame^] | 350 | static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, |
| 351 | struct scatterlist *dest_sg, |
| 352 | struct scatterlist *src_sg, int size, |
| 353 | unsigned char *iv, int op) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 354 | { |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 355 | struct ablkcipher_request *req = NULL; |
| 356 | struct extent_crypt_result ecr; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 357 | int rc = 0; |
| 358 | |
| 359 | BUG_ON(!crypt_stat || !crypt_stat->tfm |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 360 | || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 361 | if (unlikely(ecryptfs_verbosity > 0)) { |
Tyler Hicks | f24b388 | 2010-11-15 17:36:38 -0600 | [diff] [blame] | 362 | ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n", |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 363 | crypt_stat->key_size); |
| 364 | ecryptfs_dump_hex(crypt_stat->key, |
| 365 | crypt_stat->key_size); |
| 366 | } |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 367 | |
| 368 | init_completion(&ecr.completion); |
| 369 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 370 | mutex_lock(&crypt_stat->cs_tfm_mutex); |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 371 | req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS); |
| 372 | if (!req) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 373 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 374 | rc = -ENOMEM; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 375 | goto out; |
| 376 | } |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 377 | |
| 378 | ablkcipher_request_set_callback(req, |
| 379 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 380 | extent_crypt_complete, &ecr); |
| 381 | /* Consider doing this once, when the file is opened */ |
| 382 | if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { |
| 383 | rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key, |
| 384 | crypt_stat->key_size); |
| 385 | if (rc) { |
| 386 | ecryptfs_printk(KERN_ERR, |
| 387 | "Error setting key; rc = [%d]\n", |
| 388 | rc); |
| 389 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
| 390 | rc = -EINVAL; |
| 391 | goto out; |
| 392 | } |
| 393 | crypt_stat->flags |= ECRYPTFS_KEY_SET; |
| 394 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 395 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 396 | ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv); |
Tyler Hicks | 00a6994 | 2013-04-05 23:26:22 -0700 | [diff] [blame^] | 397 | rc = op == ENCRYPT ? crypto_ablkcipher_encrypt(req) : |
| 398 | crypto_ablkcipher_decrypt(req); |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 399 | if (rc == -EINPROGRESS || rc == -EBUSY) { |
| 400 | struct extent_crypt_result *ecr = req->base.data; |
| 401 | |
| 402 | wait_for_completion(&ecr->completion); |
| 403 | rc = ecr->rc; |
| 404 | INIT_COMPLETION(ecr->completion); |
| 405 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 406 | out: |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 407 | ablkcipher_request_free(req); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 408 | return rc; |
| 409 | } |
| 410 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 411 | /** |
Tyler Hicks | 24d1526 | 2013-04-15 17:28:09 -0700 | [diff] [blame] | 412 | * lower_offset_for_page |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 413 | * |
| 414 | * Convert an eCryptfs page index into a lower byte offset |
| 415 | */ |
Tyler Hicks | 24d1526 | 2013-04-15 17:28:09 -0700 | [diff] [blame] | 416 | static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat, |
| 417 | struct page *page) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 418 | { |
Tyler Hicks | 24d1526 | 2013-04-15 17:28:09 -0700 | [diff] [blame] | 419 | return ecryptfs_lower_header_size(crypt_stat) + |
| 420 | (page->index << PAGE_CACHE_SHIFT); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /** |
| 424 | * ecryptfs_encrypt_extent |
| 425 | * @enc_extent_page: Allocated page into which to encrypt the data in |
| 426 | * @page |
| 427 | * @crypt_stat: crypt_stat containing cryptographic context for the |
| 428 | * encryption operation |
| 429 | * @page: Page containing plaintext data extent to encrypt |
| 430 | * @extent_offset: Page extent offset for use in generating IV |
| 431 | * |
| 432 | * Encrypts one extent of data. |
| 433 | * |
| 434 | * Return zero on success; non-zero otherwise |
| 435 | */ |
| 436 | static int ecryptfs_encrypt_extent(struct page *enc_extent_page, |
| 437 | struct ecryptfs_crypt_stat *crypt_stat, |
| 438 | struct page *page, |
| 439 | unsigned long extent_offset) |
| 440 | { |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 441 | loff_t extent_base; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 442 | char extent_iv[ECRYPTFS_MAX_IV_BYTES]; |
| 443 | int rc; |
| 444 | |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 445 | extent_base = (((loff_t)page->index) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 446 | * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); |
| 447 | rc = ecryptfs_derive_iv(extent_iv, crypt_stat, |
| 448 | (extent_base + extent_offset)); |
| 449 | if (rc) { |
Joe Perches | 888d57b | 2010-11-10 15:46:16 -0800 | [diff] [blame] | 450 | ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for " |
| 451 | "extent [0x%.16llx]; rc = [%d]\n", |
| 452 | (unsigned long long)(extent_base + extent_offset), rc); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 453 | goto out; |
| 454 | } |
Tyler Hicks | 28916d1 | 2013-04-15 16:37:27 -0700 | [diff] [blame] | 455 | rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, page, |
Tyler Hicks | 12003e5 | 2013-04-16 23:21:24 -0700 | [diff] [blame] | 456 | extent_offset * crypt_stat->extent_size, |
| 457 | crypt_stat->extent_size, extent_iv); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 458 | if (rc < 0) { |
| 459 | printk(KERN_ERR "%s: Error attempting to encrypt page with " |
| 460 | "page->index = [%ld], extent_offset = [%ld]; " |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 461 | "rc = [%d]\n", __func__, page->index, extent_offset, |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 462 | rc); |
| 463 | goto out; |
| 464 | } |
| 465 | rc = 0; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 466 | out: |
| 467 | return rc; |
| 468 | } |
| 469 | |
| 470 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 471 | * ecryptfs_encrypt_page |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 472 | * @page: Page mapped from the eCryptfs inode for the file; contains |
| 473 | * decrypted content that needs to be encrypted (to a temporary |
| 474 | * page; not in place) and written out to the lower file |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 475 | * |
| 476 | * Encrypt an eCryptfs page. This is done on a per-extent basis. Note |
| 477 | * that eCryptfs pages may straddle the lower pages -- for instance, |
| 478 | * if the file was created on a machine with an 8K page size |
| 479 | * (resulting in an 8K header), and then the file is copied onto a |
| 480 | * host with a 32K page size, then when reading page 0 of the eCryptfs |
| 481 | * file, 24K of page 0 of the lower file will be read and decrypted, |
| 482 | * and then 8K of page 1 of the lower file will be read and decrypted. |
| 483 | * |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 484 | * Returns zero on success; negative on error |
| 485 | */ |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 486 | int ecryptfs_encrypt_page(struct page *page) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 487 | { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 488 | struct inode *ecryptfs_inode; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 489 | struct ecryptfs_crypt_stat *crypt_stat; |
Eric Sandeen | 7fcba05 | 2008-07-28 15:46:39 -0700 | [diff] [blame] | 490 | char *enc_extent_virt; |
| 491 | struct page *enc_extent_page = NULL; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 492 | loff_t extent_offset; |
Tyler Hicks | 0f89617 | 2013-04-15 16:16:24 -0700 | [diff] [blame] | 493 | loff_t lower_offset; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 494 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 495 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 496 | ecryptfs_inode = page->mapping->host; |
| 497 | crypt_stat = |
| 498 | &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); |
Tyler Hicks | 13a791b | 2009-04-13 15:29:27 -0500 | [diff] [blame] | 499 | BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)); |
Eric Sandeen | 7fcba05 | 2008-07-28 15:46:39 -0700 | [diff] [blame] | 500 | enc_extent_page = alloc_page(GFP_USER); |
| 501 | if (!enc_extent_page) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 502 | rc = -ENOMEM; |
| 503 | ecryptfs_printk(KERN_ERR, "Error allocating memory for " |
| 504 | "encrypted extent\n"); |
| 505 | goto out; |
| 506 | } |
Tyler Hicks | 0f89617 | 2013-04-15 16:16:24 -0700 | [diff] [blame] | 507 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 508 | for (extent_offset = 0; |
| 509 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); |
| 510 | extent_offset++) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 511 | rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page, |
| 512 | extent_offset); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 513 | if (rc) { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 514 | printk(KERN_ERR "%s: Error encrypting extent; " |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 515 | "rc = [%d]\n", __func__, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 516 | goto out; |
| 517 | } |
Tyler Hicks | 0f89617 | 2013-04-15 16:16:24 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Tyler Hicks | 24d1526 | 2013-04-15 17:28:09 -0700 | [diff] [blame] | 520 | lower_offset = lower_offset_for_page(crypt_stat, page); |
Tyler Hicks | 0f89617 | 2013-04-15 16:16:24 -0700 | [diff] [blame] | 521 | enc_extent_virt = kmap(enc_extent_page); |
| 522 | rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset, |
| 523 | PAGE_CACHE_SIZE); |
| 524 | kunmap(enc_extent_page); |
| 525 | if (rc < 0) { |
| 526 | ecryptfs_printk(KERN_ERR, |
| 527 | "Error attempting to write lower page; rc = [%d]\n", |
| 528 | rc); |
| 529 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 530 | } |
Tyler Hicks | 96a7b9c | 2009-09-16 19:04:20 -0500 | [diff] [blame] | 531 | rc = 0; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 532 | out: |
Eric Sandeen | 7fcba05 | 2008-07-28 15:46:39 -0700 | [diff] [blame] | 533 | if (enc_extent_page) { |
Eric Sandeen | 7fcba05 | 2008-07-28 15:46:39 -0700 | [diff] [blame] | 534 | __free_page(enc_extent_page); |
| 535 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 536 | return rc; |
| 537 | } |
| 538 | |
| 539 | static int ecryptfs_decrypt_extent(struct page *page, |
| 540 | struct ecryptfs_crypt_stat *crypt_stat, |
| 541 | struct page *enc_extent_page, |
| 542 | unsigned long extent_offset) |
| 543 | { |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 544 | loff_t extent_base; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 545 | char extent_iv[ECRYPTFS_MAX_IV_BYTES]; |
| 546 | int rc; |
| 547 | |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 548 | extent_base = (((loff_t)page->index) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 549 | * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); |
| 550 | rc = ecryptfs_derive_iv(extent_iv, crypt_stat, |
| 551 | (extent_base + extent_offset)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 552 | if (rc) { |
Joe Perches | 888d57b | 2010-11-10 15:46:16 -0800 | [diff] [blame] | 553 | ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for " |
| 554 | "extent [0x%.16llx]; rc = [%d]\n", |
| 555 | (unsigned long long)(extent_base + extent_offset), rc); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 556 | goto out; |
| 557 | } |
Tyler Hicks | 28916d1 | 2013-04-15 16:37:27 -0700 | [diff] [blame] | 558 | rc = ecryptfs_decrypt_page_offset(crypt_stat, page, enc_extent_page, |
Tyler Hicks | 12003e5 | 2013-04-16 23:21:24 -0700 | [diff] [blame] | 559 | extent_offset * crypt_stat->extent_size, |
| 560 | crypt_stat->extent_size, extent_iv); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 561 | if (rc < 0) { |
| 562 | printk(KERN_ERR "%s: Error attempting to decrypt to page with " |
| 563 | "page->index = [%ld], extent_offset = [%ld]; " |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 564 | "rc = [%d]\n", __func__, page->index, extent_offset, |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 565 | rc); |
| 566 | goto out; |
| 567 | } |
| 568 | rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 569 | out: |
| 570 | return rc; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * ecryptfs_decrypt_page |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 575 | * @page: Page mapped from the eCryptfs inode for the file; data read |
| 576 | * and decrypted from the lower file will be written into this |
| 577 | * page |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 578 | * |
| 579 | * Decrypt an eCryptfs page. This is done on a per-extent basis. Note |
| 580 | * that eCryptfs pages may straddle the lower pages -- for instance, |
| 581 | * if the file was created on a machine with an 8K page size |
| 582 | * (resulting in an 8K header), and then the file is copied onto a |
| 583 | * host with a 32K page size, then when reading page 0 of the eCryptfs |
| 584 | * file, 24K of page 0 of the lower file will be read and decrypted, |
| 585 | * and then 8K of page 1 of the lower file will be read and decrypted. |
| 586 | * |
| 587 | * Returns zero on success; negative on error |
| 588 | */ |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 589 | int ecryptfs_decrypt_page(struct page *page) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 590 | { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 591 | struct inode *ecryptfs_inode; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 592 | struct ecryptfs_crypt_stat *crypt_stat; |
Tyler Hicks | 9c6043f | 2013-04-06 00:41:48 -0700 | [diff] [blame] | 593 | char *page_virt; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 594 | unsigned long extent_offset; |
Tyler Hicks | 0f89617 | 2013-04-15 16:16:24 -0700 | [diff] [blame] | 595 | loff_t lower_offset; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 596 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 597 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 598 | ecryptfs_inode = page->mapping->host; |
| 599 | crypt_stat = |
| 600 | &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); |
Tyler Hicks | 13a791b | 2009-04-13 15:29:27 -0500 | [diff] [blame] | 601 | BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)); |
Tyler Hicks | 0f89617 | 2013-04-15 16:16:24 -0700 | [diff] [blame] | 602 | |
Tyler Hicks | 24d1526 | 2013-04-15 17:28:09 -0700 | [diff] [blame] | 603 | lower_offset = lower_offset_for_page(crypt_stat, page); |
Tyler Hicks | 9c6043f | 2013-04-06 00:41:48 -0700 | [diff] [blame] | 604 | page_virt = kmap(page); |
| 605 | rc = ecryptfs_read_lower(page_virt, lower_offset, PAGE_CACHE_SIZE, |
Tyler Hicks | 0f89617 | 2013-04-15 16:16:24 -0700 | [diff] [blame] | 606 | ecryptfs_inode); |
Tyler Hicks | 9c6043f | 2013-04-06 00:41:48 -0700 | [diff] [blame] | 607 | kunmap(page); |
Tyler Hicks | 0f89617 | 2013-04-15 16:16:24 -0700 | [diff] [blame] | 608 | if (rc < 0) { |
| 609 | ecryptfs_printk(KERN_ERR, |
| 610 | "Error attempting to read lower page; rc = [%d]\n", |
| 611 | rc); |
| 612 | goto out; |
| 613 | } |
| 614 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 615 | for (extent_offset = 0; |
| 616 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); |
| 617 | extent_offset++) { |
Tyler Hicks | 9c6043f | 2013-04-06 00:41:48 -0700 | [diff] [blame] | 618 | rc = ecryptfs_decrypt_extent(page, crypt_stat, page, |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 619 | extent_offset); |
| 620 | if (rc) { |
| 621 | printk(KERN_ERR "%s: Error encrypting extent; " |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 622 | "rc = [%d]\n", __func__, rc); |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 623 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 624 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 625 | } |
| 626 | out: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 627 | return rc; |
| 628 | } |
| 629 | |
| 630 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 631 | * ecryptfs_encrypt_page_offset |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 632 | * @crypt_stat: The cryptographic context |
| 633 | * @dst_page: The page to encrypt into |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 634 | * @src_page: The page to encrypt from |
Tyler Hicks | 28916d1 | 2013-04-15 16:37:27 -0700 | [diff] [blame] | 635 | * @offset: The byte offset into the dst_page and src_page |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 636 | * @size: The number of bytes to encrypt |
| 637 | * @iv: The initialization vector to use for the encryption |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 638 | * |
| 639 | * Returns the number of bytes encrypted |
| 640 | */ |
| 641 | static int |
| 642 | ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
Tyler Hicks | 28916d1 | 2013-04-15 16:37:27 -0700 | [diff] [blame] | 643 | struct page *dst_page, struct page *src_page, |
| 644 | int offset, int size, unsigned char *iv) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 645 | { |
| 646 | struct scatterlist src_sg, dst_sg; |
| 647 | |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 648 | sg_init_table(&src_sg, 1); |
| 649 | sg_init_table(&dst_sg, 1); |
| 650 | |
Tyler Hicks | 28916d1 | 2013-04-15 16:37:27 -0700 | [diff] [blame] | 651 | sg_set_page(&src_sg, src_page, size, offset); |
| 652 | sg_set_page(&dst_sg, dst_page, size, offset); |
Tyler Hicks | 00a6994 | 2013-04-05 23:26:22 -0700 | [diff] [blame^] | 653 | return crypt_scatterlist(crypt_stat, &dst_sg, &src_sg, |
| 654 | size, iv, ENCRYPT); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /** |
| 658 | * ecryptfs_decrypt_page_offset |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 659 | * @crypt_stat: The cryptographic context |
| 660 | * @dst_page: The page to decrypt into |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 661 | * @src_page: The page to decrypt from |
Tyler Hicks | 28916d1 | 2013-04-15 16:37:27 -0700 | [diff] [blame] | 662 | * @offset: The byte offset into the dst_page and src_page |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 663 | * @size: The number of bytes to decrypt |
| 664 | * @iv: The initialization vector to use for the decryption |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 665 | * |
| 666 | * Returns the number of bytes decrypted |
| 667 | */ |
| 668 | static int |
| 669 | ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, |
Tyler Hicks | 28916d1 | 2013-04-15 16:37:27 -0700 | [diff] [blame] | 670 | struct page *dst_page, struct page *src_page, |
| 671 | int offset, int size, unsigned char *iv) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 672 | { |
| 673 | struct scatterlist src_sg, dst_sg; |
| 674 | |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 675 | sg_init_table(&src_sg, 1); |
Tyler Hicks | 28916d1 | 2013-04-15 16:37:27 -0700 | [diff] [blame] | 676 | sg_set_page(&src_sg, src_page, size, offset); |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 677 | |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 678 | sg_init_table(&dst_sg, 1); |
Tyler Hicks | 28916d1 | 2013-04-15 16:37:27 -0700 | [diff] [blame] | 679 | sg_set_page(&dst_sg, dst_page, size, offset); |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 680 | |
Tyler Hicks | 00a6994 | 2013-04-05 23:26:22 -0700 | [diff] [blame^] | 681 | return crypt_scatterlist(crypt_stat, &dst_sg, &src_sg, |
| 682 | size, iv, DECRYPT); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | #define ECRYPTFS_MAX_SCATTERLIST_LEN 4 |
| 686 | |
| 687 | /** |
| 688 | * ecryptfs_init_crypt_ctx |
Uwe Kleine-König | 421f91d | 2010-06-11 12:17:00 +0200 | [diff] [blame] | 689 | * @crypt_stat: Uninitialized crypt stats structure |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 690 | * |
| 691 | * Initialize the crypto context. |
| 692 | * |
| 693 | * TODO: Performance: Keep a cache of initialized cipher contexts; |
| 694 | * only init if needed |
| 695 | */ |
| 696 | int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat) |
| 697 | { |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 698 | char *full_alg_name; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 699 | int rc = -EINVAL; |
| 700 | |
| 701 | if (!crypt_stat->cipher) { |
| 702 | ecryptfs_printk(KERN_ERR, "No cipher specified\n"); |
| 703 | goto out; |
| 704 | } |
| 705 | ecryptfs_printk(KERN_DEBUG, |
| 706 | "Initializing cipher [%s]; strlen = [%d]; " |
Tyler Hicks | f24b388 | 2010-11-15 17:36:38 -0600 | [diff] [blame] | 707 | "key_size_bits = [%zd]\n", |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 708 | crypt_stat->cipher, (int)strlen(crypt_stat->cipher), |
| 709 | crypt_stat->key_size << 3); |
| 710 | if (crypt_stat->tfm) { |
| 711 | rc = 0; |
| 712 | goto out; |
| 713 | } |
| 714 | mutex_lock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 715 | rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, |
| 716 | crypt_stat->cipher, "cbc"); |
| 717 | if (rc) |
Eric Sandeen | c8161f6 | 2007-12-22 14:03:26 -0800 | [diff] [blame] | 718 | goto out_unlock; |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 719 | crypt_stat->tfm = crypto_alloc_ablkcipher(full_alg_name, 0, 0); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 720 | kfree(full_alg_name); |
Akinobu Mita | de88777 | 2006-11-28 12:29:49 -0800 | [diff] [blame] | 721 | if (IS_ERR(crypt_stat->tfm)) { |
| 722 | rc = PTR_ERR(crypt_stat->tfm); |
Tyler Hicks | b0105ea | 2009-08-11 00:36:32 -0500 | [diff] [blame] | 723 | crypt_stat->tfm = NULL; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 724 | ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): " |
| 725 | "Error initializing cipher [%s]\n", |
| 726 | crypt_stat->cipher); |
Eric Sandeen | c8161f6 | 2007-12-22 14:03:26 -0800 | [diff] [blame] | 727 | goto out_unlock; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 728 | } |
Tyler Hicks | 4dfea4f | 2013-05-09 16:55:07 -0700 | [diff] [blame] | 729 | crypto_ablkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 730 | rc = 0; |
Eric Sandeen | c8161f6 | 2007-12-22 14:03:26 -0800 | [diff] [blame] | 731 | out_unlock: |
| 732 | mutex_unlock(&crypt_stat->cs_tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 733 | out: |
| 734 | return rc; |
| 735 | } |
| 736 | |
| 737 | static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat) |
| 738 | { |
| 739 | int extent_size_tmp; |
| 740 | |
| 741 | crypt_stat->extent_mask = 0xFFFFFFFF; |
| 742 | crypt_stat->extent_shift = 0; |
| 743 | if (crypt_stat->extent_size == 0) |
| 744 | return; |
| 745 | extent_size_tmp = crypt_stat->extent_size; |
| 746 | while ((extent_size_tmp & 0x01) == 0) { |
| 747 | extent_size_tmp >>= 1; |
| 748 | crypt_stat->extent_mask <<= 1; |
| 749 | crypt_stat->extent_shift++; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat) |
| 754 | { |
| 755 | /* Default values; may be overwritten as we are parsing the |
| 756 | * packets. */ |
| 757 | crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE; |
| 758 | set_extent_mask_and_shift(crypt_stat); |
| 759 | crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 760 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
Tyler Hicks | fa3ef1c | 2010-02-11 05:09:14 -0600 | [diff] [blame] | 761 | crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 762 | else { |
| 763 | if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) |
Tyler Hicks | fa3ef1c | 2010-02-11 05:09:14 -0600 | [diff] [blame] | 764 | crypt_stat->metadata_size = |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 765 | ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 766 | else |
Tyler Hicks | fa3ef1c | 2010-02-11 05:09:14 -0600 | [diff] [blame] | 767 | crypt_stat->metadata_size = PAGE_CACHE_SIZE; |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 768 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /** |
| 772 | * ecryptfs_compute_root_iv |
| 773 | * @crypt_stats |
| 774 | * |
| 775 | * On error, sets the root IV to all 0's. |
| 776 | */ |
| 777 | int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) |
| 778 | { |
| 779 | int rc = 0; |
| 780 | char dst[MD5_DIGEST_SIZE]; |
| 781 | |
| 782 | BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE); |
| 783 | BUG_ON(crypt_stat->iv_bytes <= 0); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 784 | if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 785 | rc = -EINVAL; |
| 786 | ecryptfs_printk(KERN_WARNING, "Session key not valid; " |
| 787 | "cannot generate root IV\n"); |
| 788 | goto out; |
| 789 | } |
| 790 | rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key, |
| 791 | crypt_stat->key_size); |
| 792 | if (rc) { |
| 793 | ecryptfs_printk(KERN_WARNING, "Error attempting to compute " |
| 794 | "MD5 while generating root IV\n"); |
| 795 | goto out; |
| 796 | } |
| 797 | memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes); |
| 798 | out: |
| 799 | if (rc) { |
| 800 | memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 801 | crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 802 | } |
| 803 | return rc; |
| 804 | } |
| 805 | |
| 806 | static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) |
| 807 | { |
| 808 | get_random_bytes(crypt_stat->key, crypt_stat->key_size); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 809 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 810 | ecryptfs_compute_root_iv(crypt_stat); |
| 811 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 812 | ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n"); |
| 813 | ecryptfs_dump_hex(crypt_stat->key, |
| 814 | crypt_stat->key_size); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | /** |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 819 | * ecryptfs_copy_mount_wide_flags_to_inode_flags |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 820 | * @crypt_stat: The inode's cryptographic context |
| 821 | * @mount_crypt_stat: The mount point's cryptographic context |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 822 | * |
| 823 | * This function propagates the mount-wide flags to individual inode |
| 824 | * flags. |
| 825 | */ |
| 826 | static void ecryptfs_copy_mount_wide_flags_to_inode_flags( |
| 827 | struct ecryptfs_crypt_stat *crypt_stat, |
| 828 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 829 | { |
| 830 | if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) |
| 831 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; |
| 832 | if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) |
| 833 | crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED; |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 834 | if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) { |
| 835 | crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES; |
| 836 | if (mount_crypt_stat->flags |
| 837 | & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK) |
| 838 | crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK; |
| 839 | else if (mount_crypt_stat->flags |
| 840 | & ECRYPTFS_GLOBAL_ENCFN_USE_FEK) |
| 841 | crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK; |
| 842 | } |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 843 | } |
| 844 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 845 | static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs( |
| 846 | struct ecryptfs_crypt_stat *crypt_stat, |
| 847 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 848 | { |
| 849 | struct ecryptfs_global_auth_tok *global_auth_tok; |
| 850 | int rc = 0; |
| 851 | |
Roland Dreier | aa06117 | 2009-07-01 15:48:18 -0700 | [diff] [blame] | 852 | mutex_lock(&crypt_stat->keysig_list_mutex); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 853 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); |
Roland Dreier | aa06117 | 2009-07-01 15:48:18 -0700 | [diff] [blame] | 854 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 855 | list_for_each_entry(global_auth_tok, |
| 856 | &mount_crypt_stat->global_auth_tok_list, |
| 857 | mount_crypt_stat_list) { |
Tyler Hicks | 84814d6 | 2009-03-13 13:51:59 -0700 | [diff] [blame] | 858 | if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK) |
| 859 | continue; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 860 | rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig); |
| 861 | if (rc) { |
| 862 | printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 863 | goto out; |
| 864 | } |
| 865 | } |
Roland Dreier | aa06117 | 2009-07-01 15:48:18 -0700 | [diff] [blame] | 866 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 867 | out: |
Roland Dreier | aa06117 | 2009-07-01 15:48:18 -0700 | [diff] [blame] | 868 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 869 | mutex_unlock(&crypt_stat->keysig_list_mutex); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 870 | return rc; |
| 871 | } |
| 872 | |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 873 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 874 | * ecryptfs_set_default_crypt_stat_vals |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 875 | * @crypt_stat: The inode's cryptographic context |
| 876 | * @mount_crypt_stat: The mount point's cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 877 | * |
| 878 | * Default values in the event that policy does not override them. |
| 879 | */ |
| 880 | static void ecryptfs_set_default_crypt_stat_vals( |
| 881 | struct ecryptfs_crypt_stat *crypt_stat, |
| 882 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 883 | { |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 884 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
| 885 | mount_crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 886 | ecryptfs_set_default_sizes(crypt_stat); |
| 887 | strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER); |
| 888 | crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES; |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 889 | crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 890 | crypt_stat->file_version = ECRYPTFS_FILE_VERSION; |
| 891 | crypt_stat->mount_crypt_stat = mount_crypt_stat; |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * ecryptfs_new_file_context |
Tyler Hicks | b59db43 | 2011-11-21 17:31:02 -0600 | [diff] [blame] | 896 | * @ecryptfs_inode: The eCryptfs inode |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 897 | * |
| 898 | * If the crypto context for the file has not yet been established, |
| 899 | * this is where we do that. Establishing a new crypto context |
| 900 | * involves the following decisions: |
| 901 | * - What cipher to use? |
| 902 | * - What set of authentication tokens to use? |
| 903 | * Here we just worry about getting enough information into the |
| 904 | * authentication tokens so that we know that they are available. |
| 905 | * We associate the available authentication tokens with the new file |
| 906 | * via the set of signatures in the crypt_stat struct. Later, when |
| 907 | * the headers are actually written out, we may again defer to |
| 908 | * userspace to perform the encryption of the session key; for the |
| 909 | * foreseeable future, this will be the case with public key packets. |
| 910 | * |
| 911 | * Returns zero on success; non-zero otherwise |
| 912 | */ |
Tyler Hicks | b59db43 | 2011-11-21 17:31:02 -0600 | [diff] [blame] | 913 | int ecryptfs_new_file_context(struct inode *ecryptfs_inode) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 914 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 915 | struct ecryptfs_crypt_stat *crypt_stat = |
Tyler Hicks | b59db43 | 2011-11-21 17:31:02 -0600 | [diff] [blame] | 916 | &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 917 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 918 | &ecryptfs_superblock_to_private( |
Tyler Hicks | b59db43 | 2011-11-21 17:31:02 -0600 | [diff] [blame] | 919 | ecryptfs_inode->i_sb)->mount_crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 920 | int cipher_name_len; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 921 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 922 | |
| 923 | ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat); |
Michael Halcrow | af655dc | 2007-10-16 01:28:00 -0700 | [diff] [blame] | 924 | crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 925 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
| 926 | mount_crypt_stat); |
| 927 | rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat, |
| 928 | mount_crypt_stat); |
| 929 | if (rc) { |
| 930 | printk(KERN_ERR "Error attempting to copy mount-wide key sigs " |
| 931 | "to the inode key sigs; rc = [%d]\n", rc); |
| 932 | goto out; |
| 933 | } |
| 934 | cipher_name_len = |
| 935 | strlen(mount_crypt_stat->global_default_cipher_name); |
| 936 | memcpy(crypt_stat->cipher, |
| 937 | mount_crypt_stat->global_default_cipher_name, |
| 938 | cipher_name_len); |
| 939 | crypt_stat->cipher[cipher_name_len] = '\0'; |
| 940 | crypt_stat->key_size = |
| 941 | mount_crypt_stat->global_default_cipher_key_size; |
| 942 | ecryptfs_generate_new_key(crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 943 | rc = ecryptfs_init_crypt_ctx(crypt_stat); |
| 944 | if (rc) |
| 945 | ecryptfs_printk(KERN_ERR, "Error initializing cryptographic " |
| 946 | "context for cipher [%s]: rc = [%d]\n", |
| 947 | crypt_stat->cipher, rc); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 948 | out: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 949 | return rc; |
| 950 | } |
| 951 | |
| 952 | /** |
Tyler Hicks | 7a86617 | 2011-05-02 00:39:54 -0500 | [diff] [blame] | 953 | * ecryptfs_validate_marker - check for the ecryptfs marker |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 954 | * @data: The data block in which to check |
| 955 | * |
Tyler Hicks | 7a86617 | 2011-05-02 00:39:54 -0500 | [diff] [blame] | 956 | * Returns zero if marker found; -EINVAL if not found |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 957 | */ |
Tyler Hicks | 7a86617 | 2011-05-02 00:39:54 -0500 | [diff] [blame] | 958 | static int ecryptfs_validate_marker(char *data) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 959 | { |
| 960 | u32 m_1, m_2; |
| 961 | |
Harvey Harrison | 29335c6 | 2008-07-23 21:30:06 -0700 | [diff] [blame] | 962 | m_1 = get_unaligned_be32(data); |
| 963 | m_2 = get_unaligned_be32(data + 4); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 964 | if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2) |
Tyler Hicks | 7a86617 | 2011-05-02 00:39:54 -0500 | [diff] [blame] | 965 | return 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 966 | ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; " |
| 967 | "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2, |
| 968 | MAGIC_ECRYPTFS_MARKER); |
| 969 | ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = " |
| 970 | "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER)); |
Tyler Hicks | 7a86617 | 2011-05-02 00:39:54 -0500 | [diff] [blame] | 971 | return -EINVAL; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | struct ecryptfs_flag_map_elem { |
| 975 | u32 file_flag; |
| 976 | u32 local_flag; |
| 977 | }; |
| 978 | |
| 979 | /* Add support for additional flags by adding elements here. */ |
| 980 | static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = { |
| 981 | {0x00000001, ECRYPTFS_ENABLE_HMAC}, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 982 | {0x00000002, ECRYPTFS_ENCRYPTED}, |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 983 | {0x00000004, ECRYPTFS_METADATA_IN_XATTR}, |
| 984 | {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES} |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 985 | }; |
| 986 | |
| 987 | /** |
| 988 | * ecryptfs_process_flags |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 989 | * @crypt_stat: The cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 990 | * @page_virt: Source data to be parsed |
| 991 | * @bytes_read: Updated with the number of bytes read |
| 992 | * |
| 993 | * Returns zero on success; non-zero if the flag set is invalid |
| 994 | */ |
| 995 | static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat, |
| 996 | char *page_virt, int *bytes_read) |
| 997 | { |
| 998 | int rc = 0; |
| 999 | int i; |
| 1000 | u32 flags; |
| 1001 | |
Harvey Harrison | 29335c6 | 2008-07-23 21:30:06 -0700 | [diff] [blame] | 1002 | flags = get_unaligned_be32(page_virt); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1003 | for (i = 0; i < ((sizeof(ecryptfs_flag_map) |
| 1004 | / sizeof(struct ecryptfs_flag_map_elem))); i++) |
| 1005 | if (flags & ecryptfs_flag_map[i].file_flag) { |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1006 | crypt_stat->flags |= ecryptfs_flag_map[i].local_flag; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1007 | } else |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1008 | crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1009 | /* Version is in top 8 bits of the 32-bit flag vector */ |
| 1010 | crypt_stat->file_version = ((flags >> 24) & 0xFF); |
| 1011 | (*bytes_read) = 4; |
| 1012 | return rc; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * write_ecryptfs_marker |
| 1017 | * @page_virt: The pointer to in a page to begin writing the marker |
| 1018 | * @written: Number of bytes written |
| 1019 | * |
| 1020 | * Marker = 0x3c81b7f5 |
| 1021 | */ |
| 1022 | static void write_ecryptfs_marker(char *page_virt, size_t *written) |
| 1023 | { |
| 1024 | u32 m_1, m_2; |
| 1025 | |
| 1026 | get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); |
| 1027 | m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER); |
Harvey Harrison | 29335c6 | 2008-07-23 21:30:06 -0700 | [diff] [blame] | 1028 | put_unaligned_be32(m_1, page_virt); |
| 1029 | page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2); |
| 1030 | put_unaligned_be32(m_2, page_virt); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1031 | (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; |
| 1032 | } |
| 1033 | |
Tyler Hicks | f4e60e6 | 2010-02-11 00:02:32 -0600 | [diff] [blame] | 1034 | void ecryptfs_write_crypt_stat_flags(char *page_virt, |
| 1035 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1036 | size_t *written) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1037 | { |
| 1038 | u32 flags = 0; |
| 1039 | int i; |
| 1040 | |
| 1041 | for (i = 0; i < ((sizeof(ecryptfs_flag_map) |
| 1042 | / sizeof(struct ecryptfs_flag_map_elem))); i++) |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1043 | if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1044 | flags |= ecryptfs_flag_map[i].file_flag; |
| 1045 | /* Version is in top 8 bits of the 32-bit flag vector */ |
| 1046 | flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000); |
Harvey Harrison | 29335c6 | 2008-07-23 21:30:06 -0700 | [diff] [blame] | 1047 | put_unaligned_be32(flags, page_virt); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1048 | (*written) = 4; |
| 1049 | } |
| 1050 | |
| 1051 | struct ecryptfs_cipher_code_str_map_elem { |
| 1052 | char cipher_str[16]; |
Trevor Highland | 19e66a6 | 2008-02-06 01:38:36 -0800 | [diff] [blame] | 1053 | u8 cipher_code; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1054 | }; |
| 1055 | |
| 1056 | /* Add support for additional ciphers by adding elements here. The |
| 1057 | * cipher_code is whatever OpenPGP applicatoins use to identify the |
| 1058 | * ciphers. List in order of probability. */ |
| 1059 | static struct ecryptfs_cipher_code_str_map_elem |
| 1060 | ecryptfs_cipher_code_str_map[] = { |
| 1061 | {"aes",RFC2440_CIPHER_AES_128 }, |
| 1062 | {"blowfish", RFC2440_CIPHER_BLOWFISH}, |
| 1063 | {"des3_ede", RFC2440_CIPHER_DES3_EDE}, |
| 1064 | {"cast5", RFC2440_CIPHER_CAST_5}, |
| 1065 | {"twofish", RFC2440_CIPHER_TWOFISH}, |
| 1066 | {"cast6", RFC2440_CIPHER_CAST_6}, |
| 1067 | {"aes", RFC2440_CIPHER_AES_192}, |
| 1068 | {"aes", RFC2440_CIPHER_AES_256} |
| 1069 | }; |
| 1070 | |
| 1071 | /** |
| 1072 | * ecryptfs_code_for_cipher_string |
Michael Halcrow | 9c79f34 | 2009-01-06 14:41:57 -0800 | [diff] [blame] | 1073 | * @cipher_name: The string alias for the cipher |
| 1074 | * @key_bytes: Length of key in bytes; used for AES code selection |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1075 | * |
| 1076 | * Returns zero on no match, or the cipher code on match |
| 1077 | */ |
Michael Halcrow | 9c79f34 | 2009-01-06 14:41:57 -0800 | [diff] [blame] | 1078 | u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1079 | { |
| 1080 | int i; |
Trevor Highland | 19e66a6 | 2008-02-06 01:38:36 -0800 | [diff] [blame] | 1081 | u8 code = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1082 | struct ecryptfs_cipher_code_str_map_elem *map = |
| 1083 | ecryptfs_cipher_code_str_map; |
| 1084 | |
Michael Halcrow | 9c79f34 | 2009-01-06 14:41:57 -0800 | [diff] [blame] | 1085 | if (strcmp(cipher_name, "aes") == 0) { |
| 1086 | switch (key_bytes) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1087 | case 16: |
| 1088 | code = RFC2440_CIPHER_AES_128; |
| 1089 | break; |
| 1090 | case 24: |
| 1091 | code = RFC2440_CIPHER_AES_192; |
| 1092 | break; |
| 1093 | case 32: |
| 1094 | code = RFC2440_CIPHER_AES_256; |
| 1095 | } |
| 1096 | } else { |
| 1097 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) |
Michael Halcrow | 9c79f34 | 2009-01-06 14:41:57 -0800 | [diff] [blame] | 1098 | if (strcmp(cipher_name, map[i].cipher_str) == 0) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1099 | code = map[i].cipher_code; |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | return code; |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * ecryptfs_cipher_code_to_string |
| 1108 | * @str: Destination to write out the cipher name |
| 1109 | * @cipher_code: The code to convert to cipher name string |
| 1110 | * |
| 1111 | * Returns zero on success |
| 1112 | */ |
Trevor Highland | 19e66a6 | 2008-02-06 01:38:36 -0800 | [diff] [blame] | 1113 | int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1114 | { |
| 1115 | int rc = 0; |
| 1116 | int i; |
| 1117 | |
| 1118 | str[0] = '\0'; |
| 1119 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) |
| 1120 | if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code) |
| 1121 | strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str); |
| 1122 | if (str[0] == '\0') { |
| 1123 | ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: " |
| 1124 | "[%d]\n", cipher_code); |
| 1125 | rc = -EINVAL; |
| 1126 | } |
| 1127 | return rc; |
| 1128 | } |
| 1129 | |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 1130 | int ecryptfs_read_and_validate_header_region(struct inode *inode) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1131 | { |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 1132 | u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES]; |
| 1133 | u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1134 | int rc; |
| 1135 | |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 1136 | rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES, |
| 1137 | inode); |
| 1138 | if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES) |
| 1139 | return rc >= 0 ? -EINVAL : rc; |
| 1140 | rc = ecryptfs_validate_marker(marker); |
| 1141 | if (!rc) |
| 1142 | ecryptfs_i_size_init(file_size, inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1143 | return rc; |
| 1144 | } |
| 1145 | |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1146 | void |
| 1147 | ecryptfs_write_header_metadata(char *virt, |
| 1148 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1149 | size_t *written) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1150 | { |
| 1151 | u32 header_extent_size; |
| 1152 | u16 num_header_extents_at_front; |
| 1153 | |
Michael Halcrow | 45eaab7 | 2007-10-16 01:28:05 -0700 | [diff] [blame] | 1154 | header_extent_size = (u32)crypt_stat->extent_size; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1155 | num_header_extents_at_front = |
Tyler Hicks | fa3ef1c | 2010-02-11 05:09:14 -0600 | [diff] [blame] | 1156 | (u16)(crypt_stat->metadata_size / crypt_stat->extent_size); |
Harvey Harrison | 29335c6 | 2008-07-23 21:30:06 -0700 | [diff] [blame] | 1157 | put_unaligned_be32(header_extent_size, virt); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1158 | virt += 4; |
Harvey Harrison | 29335c6 | 2008-07-23 21:30:06 -0700 | [diff] [blame] | 1159 | put_unaligned_be16(num_header_extents_at_front, virt); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1160 | (*written) = 6; |
| 1161 | } |
| 1162 | |
Tyler Hicks | 3063287 | 2011-05-24 05:11:12 -0500 | [diff] [blame] | 1163 | struct kmem_cache *ecryptfs_header_cache; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1164 | |
| 1165 | /** |
| 1166 | * ecryptfs_write_headers_virt |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1167 | * @page_virt: The virtual address to write the headers to |
Eric Sandeen | 87b811c3 | 2008-10-29 14:01:08 -0700 | [diff] [blame] | 1168 | * @max: The size of memory allocated at page_virt |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1169 | * @size: Set to the number of bytes written by this function |
| 1170 | * @crypt_stat: The cryptographic context |
| 1171 | * @ecryptfs_dentry: The eCryptfs dentry |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1172 | * |
| 1173 | * Format version: 1 |
| 1174 | * |
| 1175 | * Header Extent: |
| 1176 | * Octets 0-7: Unencrypted file size (big-endian) |
| 1177 | * Octets 8-15: eCryptfs special marker |
| 1178 | * Octets 16-19: Flags |
| 1179 | * Octet 16: File format version number (between 0 and 255) |
| 1180 | * Octets 17-18: Reserved |
| 1181 | * Octet 19: Bit 1 (lsb): Reserved |
| 1182 | * Bit 2: Encrypted? |
| 1183 | * Bits 3-8: Reserved |
| 1184 | * Octets 20-23: Header extent size (big-endian) |
| 1185 | * Octets 24-25: Number of header extents at front of file |
| 1186 | * (big-endian) |
| 1187 | * Octet 26: Begin RFC 2440 authentication token packet set |
| 1188 | * Data Extent 0: |
| 1189 | * Lower data (CBC encrypted) |
| 1190 | * Data Extent 1: |
| 1191 | * Lower data (CBC encrypted) |
| 1192 | * ... |
| 1193 | * |
| 1194 | * Returns zero on success |
| 1195 | */ |
Eric Sandeen | 87b811c3 | 2008-10-29 14:01:08 -0700 | [diff] [blame] | 1196 | static int ecryptfs_write_headers_virt(char *page_virt, size_t max, |
| 1197 | size_t *size, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1198 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1199 | struct dentry *ecryptfs_dentry) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1200 | { |
| 1201 | int rc; |
| 1202 | size_t written; |
| 1203 | size_t offset; |
| 1204 | |
| 1205 | offset = ECRYPTFS_FILE_SIZE_BYTES; |
| 1206 | write_ecryptfs_marker((page_virt + offset), &written); |
| 1207 | offset += written; |
Tyler Hicks | f4e60e6 | 2010-02-11 00:02:32 -0600 | [diff] [blame] | 1208 | ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat, |
| 1209 | &written); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1210 | offset += written; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1211 | ecryptfs_write_header_metadata((page_virt + offset), crypt_stat, |
| 1212 | &written); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1213 | offset += written; |
| 1214 | rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat, |
| 1215 | ecryptfs_dentry, &written, |
Eric Sandeen | 87b811c3 | 2008-10-29 14:01:08 -0700 | [diff] [blame] | 1216 | max - offset); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1217 | if (rc) |
| 1218 | ecryptfs_printk(KERN_WARNING, "Error generating key packet " |
| 1219 | "set; rc = [%d]\n", rc); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1220 | if (size) { |
| 1221 | offset += written; |
| 1222 | *size = offset; |
| 1223 | } |
| 1224 | return rc; |
| 1225 | } |
| 1226 | |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1227 | static int |
Tyler Hicks | b59db43 | 2011-11-21 17:31:02 -0600 | [diff] [blame] | 1228 | ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode, |
Tyler Hicks | 8faece5 | 2009-03-20 01:25:09 -0500 | [diff] [blame] | 1229 | char *virt, size_t virt_len) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1230 | { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1231 | int rc; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1232 | |
Tyler Hicks | b59db43 | 2011-11-21 17:31:02 -0600 | [diff] [blame] | 1233 | rc = ecryptfs_write_lower(ecryptfs_inode, virt, |
Tyler Hicks | 8faece5 | 2009-03-20 01:25:09 -0500 | [diff] [blame] | 1234 | 0, virt_len); |
Tyler Hicks | 96a7b9c | 2009-09-16 19:04:20 -0500 | [diff] [blame] | 1235 | if (rc < 0) |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1236 | printk(KERN_ERR "%s: Error attempting to write header " |
Tyler Hicks | 96a7b9c | 2009-09-16 19:04:20 -0500 | [diff] [blame] | 1237 | "information to lower file; rc = [%d]\n", __func__, rc); |
| 1238 | else |
| 1239 | rc = 0; |
Michael Halcrow | 7045660 | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 1240 | return rc; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1243 | static int |
| 1244 | ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry, |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1245 | char *page_virt, size_t size) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1246 | { |
| 1247 | int rc; |
| 1248 | |
| 1249 | rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt, |
| 1250 | size, 0); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1251 | return rc; |
| 1252 | } |
| 1253 | |
Tyler Hicks | 8faece5 | 2009-03-20 01:25:09 -0500 | [diff] [blame] | 1254 | static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask, |
| 1255 | unsigned int order) |
| 1256 | { |
| 1257 | struct page *page; |
| 1258 | |
| 1259 | page = alloc_pages(gfp_mask | __GFP_ZERO, order); |
| 1260 | if (page) |
| 1261 | return (unsigned long) page_address(page); |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1265 | /** |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1266 | * ecryptfs_write_metadata |
Tyler Hicks | b59db43 | 2011-11-21 17:31:02 -0600 | [diff] [blame] | 1267 | * @ecryptfs_dentry: The eCryptfs dentry, which should be negative |
| 1268 | * @ecryptfs_inode: The newly created eCryptfs inode |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1269 | * |
| 1270 | * Write the file headers out. This will likely involve a userspace |
| 1271 | * callout, in which the session key is encrypted with one or more |
| 1272 | * public keys and/or the passphrase necessary to do the encryption is |
| 1273 | * retrieved via a prompt. Exactly what happens at this point should |
| 1274 | * be policy-dependent. |
| 1275 | * |
| 1276 | * Returns zero on success; non-zero on error |
| 1277 | */ |
Tyler Hicks | b59db43 | 2011-11-21 17:31:02 -0600 | [diff] [blame] | 1278 | int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry, |
| 1279 | struct inode *ecryptfs_inode) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1280 | { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1281 | struct ecryptfs_crypt_stat *crypt_stat = |
Tyler Hicks | b59db43 | 2011-11-21 17:31:02 -0600 | [diff] [blame] | 1282 | &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
Tyler Hicks | 8faece5 | 2009-03-20 01:25:09 -0500 | [diff] [blame] | 1283 | unsigned int order; |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 1284 | char *virt; |
Tyler Hicks | 8faece5 | 2009-03-20 01:25:09 -0500 | [diff] [blame] | 1285 | size_t virt_len; |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1286 | size_t size = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1287 | int rc = 0; |
| 1288 | |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1289 | if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
| 1290 | if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1291 | printk(KERN_ERR "Key is invalid; bailing out\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1292 | rc = -EINVAL; |
| 1293 | goto out; |
| 1294 | } |
| 1295 | } else { |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 1296 | printk(KERN_WARNING "%s: Encrypted flag not set\n", |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 1297 | __func__); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1298 | rc = -EINVAL; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1299 | goto out; |
| 1300 | } |
Tyler Hicks | fa3ef1c | 2010-02-11 05:09:14 -0600 | [diff] [blame] | 1301 | virt_len = crypt_stat->metadata_size; |
Tyler Hicks | 8faece5 | 2009-03-20 01:25:09 -0500 | [diff] [blame] | 1302 | order = get_order(virt_len); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1303 | /* Released in this function */ |
Tyler Hicks | 8faece5 | 2009-03-20 01:25:09 -0500 | [diff] [blame] | 1304 | virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order); |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 1305 | if (!virt) { |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 1306 | printk(KERN_ERR "%s: Out of memory\n", __func__); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1307 | rc = -ENOMEM; |
| 1308 | goto out; |
| 1309 | } |
Tyler Hicks | bd4f0fe | 2011-02-23 00:14:19 -0600 | [diff] [blame] | 1310 | /* Zeroed page ensures the in-header unencrypted i_size is set to 0 */ |
Tyler Hicks | 8faece5 | 2009-03-20 01:25:09 -0500 | [diff] [blame] | 1311 | rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat, |
| 1312 | ecryptfs_dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1313 | if (unlikely(rc)) { |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 1314 | printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n", |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 1315 | __func__, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1316 | goto out_free; |
| 1317 | } |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1318 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
Tyler Hicks | 8faece5 | 2009-03-20 01:25:09 -0500 | [diff] [blame] | 1319 | rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt, |
| 1320 | size); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1321 | else |
Tyler Hicks | b59db43 | 2011-11-21 17:31:02 -0600 | [diff] [blame] | 1322 | rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt, |
Tyler Hicks | 8faece5 | 2009-03-20 01:25:09 -0500 | [diff] [blame] | 1323 | virt_len); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1324 | if (rc) { |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 1325 | printk(KERN_ERR "%s: Error writing metadata out to lower file; " |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 1326 | "rc = [%d]\n", __func__, rc); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1327 | goto out_free; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1328 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1329 | out_free: |
Tyler Hicks | 8faece5 | 2009-03-20 01:25:09 -0500 | [diff] [blame] | 1330 | free_pages((unsigned long)virt, order); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1331 | out: |
| 1332 | return rc; |
| 1333 | } |
| 1334 | |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1335 | #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0 |
| 1336 | #define ECRYPTFS_VALIDATE_HEADER_SIZE 1 |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1337 | static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1338 | char *virt, int *bytes_read, |
| 1339 | int validate_header_size) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1340 | { |
| 1341 | int rc = 0; |
| 1342 | u32 header_extent_size; |
| 1343 | u16 num_header_extents_at_front; |
| 1344 | |
Harvey Harrison | 29335c6 | 2008-07-23 21:30:06 -0700 | [diff] [blame] | 1345 | header_extent_size = get_unaligned_be32(virt); |
| 1346 | virt += sizeof(__be32); |
| 1347 | num_header_extents_at_front = get_unaligned_be16(virt); |
Tyler Hicks | fa3ef1c | 2010-02-11 05:09:14 -0600 | [diff] [blame] | 1348 | crypt_stat->metadata_size = (((size_t)num_header_extents_at_front |
| 1349 | * (size_t)header_extent_size)); |
Harvey Harrison | 29335c6 | 2008-07-23 21:30:06 -0700 | [diff] [blame] | 1350 | (*bytes_read) = (sizeof(__be32) + sizeof(__be16)); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1351 | if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE) |
Tyler Hicks | fa3ef1c | 2010-02-11 05:09:14 -0600 | [diff] [blame] | 1352 | && (crypt_stat->metadata_size |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1353 | < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1354 | rc = -EINVAL; |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 1355 | printk(KERN_WARNING "Invalid header size: [%zd]\n", |
Tyler Hicks | fa3ef1c | 2010-02-11 05:09:14 -0600 | [diff] [blame] | 1356 | crypt_stat->metadata_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1357 | } |
| 1358 | return rc; |
| 1359 | } |
| 1360 | |
| 1361 | /** |
| 1362 | * set_default_header_data |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1363 | * @crypt_stat: The cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1364 | * |
| 1365 | * For version 0 file format; this function is only for backwards |
| 1366 | * compatibility for files created with the prior versions of |
| 1367 | * eCryptfs. |
| 1368 | */ |
| 1369 | static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat) |
| 1370 | { |
Tyler Hicks | fa3ef1c | 2010-02-11 05:09:14 -0600 | [diff] [blame] | 1371 | crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Tyler Hicks | 3aeb86e | 2011-03-15 14:54:00 -0500 | [diff] [blame] | 1374 | void ecryptfs_i_size_init(const char *page_virt, struct inode *inode) |
| 1375 | { |
| 1376 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat; |
| 1377 | struct ecryptfs_crypt_stat *crypt_stat; |
| 1378 | u64 file_size; |
| 1379 | |
| 1380 | crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; |
| 1381 | mount_crypt_stat = |
| 1382 | &ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat; |
| 1383 | if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) { |
| 1384 | file_size = i_size_read(ecryptfs_inode_to_lower(inode)); |
| 1385 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
| 1386 | file_size += crypt_stat->metadata_size; |
| 1387 | } else |
| 1388 | file_size = get_unaligned_be64(page_virt); |
| 1389 | i_size_write(inode, (loff_t)file_size); |
| 1390 | crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED; |
| 1391 | } |
| 1392 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1393 | /** |
| 1394 | * ecryptfs_read_headers_virt |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1395 | * @page_virt: The virtual address into which to read the headers |
| 1396 | * @crypt_stat: The cryptographic context |
| 1397 | * @ecryptfs_dentry: The eCryptfs dentry |
| 1398 | * @validate_header_size: Whether to validate the header size while reading |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1399 | * |
| 1400 | * Read/parse the header data. The header format is detailed in the |
| 1401 | * comment block for the ecryptfs_write_headers_virt() function. |
| 1402 | * |
| 1403 | * Returns zero on success |
| 1404 | */ |
| 1405 | static int ecryptfs_read_headers_virt(char *page_virt, |
| 1406 | struct ecryptfs_crypt_stat *crypt_stat, |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1407 | struct dentry *ecryptfs_dentry, |
| 1408 | int validate_header_size) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1409 | { |
| 1410 | int rc = 0; |
| 1411 | int offset; |
| 1412 | int bytes_read; |
| 1413 | |
| 1414 | ecryptfs_set_default_sizes(crypt_stat); |
| 1415 | crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private( |
| 1416 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
| 1417 | offset = ECRYPTFS_FILE_SIZE_BYTES; |
Tyler Hicks | 7a86617 | 2011-05-02 00:39:54 -0500 | [diff] [blame] | 1418 | rc = ecryptfs_validate_marker(page_virt + offset); |
| 1419 | if (rc) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1420 | goto out; |
Tyler Hicks | 3aeb86e | 2011-03-15 14:54:00 -0500 | [diff] [blame] | 1421 | if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED)) |
| 1422 | ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1423 | offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; |
| 1424 | rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset), |
| 1425 | &bytes_read); |
| 1426 | if (rc) { |
| 1427 | ecryptfs_printk(KERN_WARNING, "Error processing flags\n"); |
| 1428 | goto out; |
| 1429 | } |
| 1430 | if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) { |
| 1431 | ecryptfs_printk(KERN_WARNING, "File version is [%d]; only " |
| 1432 | "file version [%d] is supported by this " |
| 1433 | "version of eCryptfs\n", |
| 1434 | crypt_stat->file_version, |
| 1435 | ECRYPTFS_SUPPORTED_FILE_VERSION); |
| 1436 | rc = -EINVAL; |
| 1437 | goto out; |
| 1438 | } |
| 1439 | offset += bytes_read; |
| 1440 | if (crypt_stat->file_version >= 1) { |
| 1441 | rc = parse_header_metadata(crypt_stat, (page_virt + offset), |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1442 | &bytes_read, validate_header_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1443 | if (rc) { |
| 1444 | ecryptfs_printk(KERN_WARNING, "Error reading header " |
| 1445 | "metadata; rc = [%d]\n", rc); |
| 1446 | } |
| 1447 | offset += bytes_read; |
| 1448 | } else |
| 1449 | set_default_header_data(crypt_stat); |
| 1450 | rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset), |
| 1451 | ecryptfs_dentry); |
| 1452 | out: |
| 1453 | return rc; |
| 1454 | } |
| 1455 | |
| 1456 | /** |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1457 | * ecryptfs_read_xattr_region |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1458 | * @page_virt: The vitual address into which to read the xattr data |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1459 | * @ecryptfs_inode: The eCryptfs inode |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1460 | * |
| 1461 | * Attempts to read the crypto metadata from the extended attribute |
| 1462 | * region of the lower file. |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1463 | * |
| 1464 | * Returns zero on success; non-zero on error |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1465 | */ |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1466 | int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1467 | { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1468 | struct dentry *lower_dentry = |
| 1469 | ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1470 | ssize_t size; |
| 1471 | int rc = 0; |
| 1472 | |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1473 | size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME, |
| 1474 | page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1475 | if (size < 0) { |
Michael Halcrow | 25bd817 | 2008-02-06 01:38:35 -0800 | [diff] [blame] | 1476 | if (unlikely(ecryptfs_verbosity > 0)) |
| 1477 | printk(KERN_INFO "Error attempting to read the [%s] " |
| 1478 | "xattr from the lower file; return value = " |
| 1479 | "[%zd]\n", ECRYPTFS_XATTR_NAME, size); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1480 | rc = -EINVAL; |
| 1481 | goto out; |
| 1482 | } |
| 1483 | out: |
| 1484 | return rc; |
| 1485 | } |
| 1486 | |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 1487 | int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry, |
Tyler Hicks | 3b06b3e | 2011-05-24 03:49:02 -0500 | [diff] [blame] | 1488 | struct inode *inode) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1489 | { |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 1490 | u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES]; |
| 1491 | u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1492 | int rc; |
| 1493 | |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 1494 | rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), |
| 1495 | ECRYPTFS_XATTR_NAME, file_size, |
| 1496 | ECRYPTFS_SIZE_AND_MARKER_BYTES); |
| 1497 | if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES) |
| 1498 | return rc >= 0 ? -EINVAL : rc; |
| 1499 | rc = ecryptfs_validate_marker(marker); |
| 1500 | if (!rc) |
| 1501 | ecryptfs_i_size_init(file_size, inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1502 | return rc; |
| 1503 | } |
| 1504 | |
| 1505 | /** |
| 1506 | * ecryptfs_read_metadata |
| 1507 | * |
| 1508 | * Common entry point for reading file metadata. From here, we could |
| 1509 | * retrieve the header information from the header region of the file, |
| 1510 | * the xattr region of the file, or some other repostory that is |
| 1511 | * stored separately from the file itself. The current implementation |
| 1512 | * supports retrieving the metadata information from the file contents |
| 1513 | * and from the xattr region. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1514 | * |
| 1515 | * Returns zero if valid headers found and parsed; non-zero otherwise |
| 1516 | */ |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1517 | int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1518 | { |
Tim Gardner | bb45036 | 2012-01-12 16:31:55 +0100 | [diff] [blame] | 1519 | int rc; |
| 1520 | char *page_virt; |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1521 | struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1522 | struct ecryptfs_crypt_stat *crypt_stat = |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1523 | &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1524 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 1525 | &ecryptfs_superblock_to_private( |
| 1526 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1527 | |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 1528 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
| 1529 | mount_crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1530 | /* Read the first page from the underlying file */ |
Tyler Hicks | 3063287 | 2011-05-24 05:11:12 -0500 | [diff] [blame] | 1531 | page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1532 | if (!page_virt) { |
| 1533 | rc = -ENOMEM; |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1534 | printk(KERN_ERR "%s: Unable to allocate page_virt\n", |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 1535 | __func__); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1536 | goto out; |
| 1537 | } |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1538 | rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size, |
| 1539 | ecryptfs_inode); |
Tyler Hicks | 96a7b9c | 2009-09-16 19:04:20 -0500 | [diff] [blame] | 1540 | if (rc >= 0) |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1541 | rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, |
| 1542 | ecryptfs_dentry, |
| 1543 | ECRYPTFS_VALIDATE_HEADER_SIZE); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1544 | if (rc) { |
Tim Gardner | bb45036 | 2012-01-12 16:31:55 +0100 | [diff] [blame] | 1545 | /* metadata is not in the file header, so try xattrs */ |
Tyler Hicks | 1984c23 | 2010-02-10 23:17:44 -0600 | [diff] [blame] | 1546 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1547 | rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1548 | if (rc) { |
| 1549 | printk(KERN_DEBUG "Valid eCryptfs headers not found in " |
Tim Gardner | 30373dc | 2012-01-12 16:31:55 +0100 | [diff] [blame] | 1550 | "file header region or xattr region, inode %lu\n", |
| 1551 | ecryptfs_inode->i_ino); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1552 | rc = -EINVAL; |
| 1553 | goto out; |
| 1554 | } |
| 1555 | rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, |
| 1556 | ecryptfs_dentry, |
| 1557 | ECRYPTFS_DONT_VALIDATE_HEADER_SIZE); |
| 1558 | if (rc) { |
| 1559 | printk(KERN_DEBUG "Valid eCryptfs headers not found in " |
Tim Gardner | 30373dc | 2012-01-12 16:31:55 +0100 | [diff] [blame] | 1560 | "file xattr region either, inode %lu\n", |
| 1561 | ecryptfs_inode->i_ino); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1562 | rc = -EINVAL; |
| 1563 | } |
| 1564 | if (crypt_stat->mount_crypt_stat->flags |
| 1565 | & ECRYPTFS_XATTR_METADATA_ENABLED) { |
| 1566 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; |
| 1567 | } else { |
| 1568 | printk(KERN_WARNING "Attempt to access file with " |
| 1569 | "crypto metadata only in the extended attribute " |
| 1570 | "region, but eCryptfs was mounted without " |
| 1571 | "xattr support enabled. eCryptfs will not treat " |
Tim Gardner | 30373dc | 2012-01-12 16:31:55 +0100 | [diff] [blame] | 1572 | "this like an encrypted file, inode %lu\n", |
| 1573 | ecryptfs_inode->i_ino); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1574 | rc = -EINVAL; |
| 1575 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1576 | } |
| 1577 | out: |
| 1578 | if (page_virt) { |
| 1579 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
Tyler Hicks | 3063287 | 2011-05-24 05:11:12 -0500 | [diff] [blame] | 1580 | kmem_cache_free(ecryptfs_header_cache, page_virt); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1581 | } |
| 1582 | return rc; |
| 1583 | } |
| 1584 | |
| 1585 | /** |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1586 | * ecryptfs_encrypt_filename - encrypt filename |
| 1587 | * |
| 1588 | * CBC-encrypts the filename. We do not want to encrypt the same |
| 1589 | * filename with the same key and IV, which may happen with hard |
| 1590 | * links, so we prepend random bits to each filename. |
| 1591 | * |
| 1592 | * Returns zero on success; non-zero otherwise |
| 1593 | */ |
| 1594 | static int |
| 1595 | ecryptfs_encrypt_filename(struct ecryptfs_filename *filename, |
| 1596 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1597 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 1598 | { |
| 1599 | int rc = 0; |
| 1600 | |
| 1601 | filename->encrypted_filename = NULL; |
| 1602 | filename->encrypted_filename_size = 0; |
| 1603 | if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK)) |
| 1604 | || (mount_crypt_stat && (mount_crypt_stat->flags |
| 1605 | & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) { |
| 1606 | size_t packet_size; |
| 1607 | size_t remaining_bytes; |
| 1608 | |
| 1609 | rc = ecryptfs_write_tag_70_packet( |
| 1610 | NULL, NULL, |
| 1611 | &filename->encrypted_filename_size, |
| 1612 | mount_crypt_stat, NULL, |
| 1613 | filename->filename_size); |
| 1614 | if (rc) { |
| 1615 | printk(KERN_ERR "%s: Error attempting to get packet " |
| 1616 | "size for tag 72; rc = [%d]\n", __func__, |
| 1617 | rc); |
| 1618 | filename->encrypted_filename_size = 0; |
| 1619 | goto out; |
| 1620 | } |
| 1621 | filename->encrypted_filename = |
| 1622 | kmalloc(filename->encrypted_filename_size, GFP_KERNEL); |
| 1623 | if (!filename->encrypted_filename) { |
| 1624 | printk(KERN_ERR "%s: Out of memory whilst attempting " |
Michael Halcrow | df261c5 | 2009-01-06 14:42:02 -0800 | [diff] [blame] | 1625 | "to kmalloc [%zd] bytes\n", __func__, |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1626 | filename->encrypted_filename_size); |
| 1627 | rc = -ENOMEM; |
| 1628 | goto out; |
| 1629 | } |
| 1630 | remaining_bytes = filename->encrypted_filename_size; |
| 1631 | rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename, |
| 1632 | &remaining_bytes, |
| 1633 | &packet_size, |
| 1634 | mount_crypt_stat, |
| 1635 | filename->filename, |
| 1636 | filename->filename_size); |
| 1637 | if (rc) { |
| 1638 | printk(KERN_ERR "%s: Error attempting to generate " |
| 1639 | "tag 70 packet; rc = [%d]\n", __func__, |
| 1640 | rc); |
| 1641 | kfree(filename->encrypted_filename); |
| 1642 | filename->encrypted_filename = NULL; |
| 1643 | filename->encrypted_filename_size = 0; |
| 1644 | goto out; |
| 1645 | } |
| 1646 | filename->encrypted_filename_size = packet_size; |
| 1647 | } else { |
| 1648 | printk(KERN_ERR "%s: No support for requested filename " |
| 1649 | "encryption method in this release\n", __func__); |
Tyler Hicks | df6ad33 | 2009-08-21 04:27:46 -0500 | [diff] [blame] | 1650 | rc = -EOPNOTSUPP; |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1651 | goto out; |
| 1652 | } |
| 1653 | out: |
| 1654 | return rc; |
| 1655 | } |
| 1656 | |
| 1657 | static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size, |
| 1658 | const char *name, size_t name_size) |
| 1659 | { |
| 1660 | int rc = 0; |
| 1661 | |
Tyler Hicks | fd9fc84 | 2009-02-06 18:06:51 -0600 | [diff] [blame] | 1662 | (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL); |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1663 | if (!(*copied_name)) { |
| 1664 | rc = -ENOMEM; |
| 1665 | goto out; |
| 1666 | } |
| 1667 | memcpy((void *)(*copied_name), (void *)name, name_size); |
| 1668 | (*copied_name)[(name_size)] = '\0'; /* Only for convenience |
| 1669 | * in printing out the |
| 1670 | * string in debug |
| 1671 | * messages */ |
Tyler Hicks | fd9fc84 | 2009-02-06 18:06:51 -0600 | [diff] [blame] | 1672 | (*copied_name_size) = name_size; |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1673 | out: |
| 1674 | return rc; |
| 1675 | } |
| 1676 | |
| 1677 | /** |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1678 | * ecryptfs_process_key_cipher - Perform key cipher initialization. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1679 | * @key_tfm: Crypto context for key material, set by this function |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1680 | * @cipher_name: Name of the cipher |
| 1681 | * @key_size: Size of the key in bytes |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1682 | * |
| 1683 | * Returns zero on success. Any crypto_tfm structs allocated here |
| 1684 | * should be released by other functions, such as on a superblock put |
| 1685 | * event, regardless of whether this function succeeds for fails. |
| 1686 | */ |
Michael Halcrow | cd9d67d | 2007-10-16 01:28:04 -0700 | [diff] [blame] | 1687 | static int |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1688 | ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm, |
| 1689 | char *cipher_name, size_t *key_size) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1690 | { |
| 1691 | char dummy_key[ECRYPTFS_MAX_KEY_BYTES]; |
Dan Carpenter | ece550f | 2010-01-19 12:34:32 +0300 | [diff] [blame] | 1692 | char *full_alg_name = NULL; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1693 | int rc; |
| 1694 | |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1695 | *key_tfm = NULL; |
| 1696 | if (*key_size > ECRYPTFS_MAX_KEY_BYTES) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1697 | rc = -EINVAL; |
Michael Halcrow | df261c5 | 2009-01-06 14:42:02 -0800 | [diff] [blame] | 1698 | printk(KERN_ERR "Requested key size is [%zd] bytes; maximum " |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1699 | "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1700 | goto out; |
| 1701 | } |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1702 | rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name, |
| 1703 | "ecb"); |
| 1704 | if (rc) |
| 1705 | goto out; |
| 1706 | *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1707 | if (IS_ERR(*key_tfm)) { |
| 1708 | rc = PTR_ERR(*key_tfm); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1709 | printk(KERN_ERR "Unable to allocate crypto cipher with name " |
Dave Hansen | 3826849 | 2009-08-27 09:47:07 -0700 | [diff] [blame] | 1710 | "[%s]; rc = [%d]\n", full_alg_name, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1711 | goto out; |
| 1712 | } |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1713 | crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
| 1714 | if (*key_size == 0) { |
| 1715 | struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm); |
| 1716 | |
| 1717 | *key_size = alg->max_keysize; |
| 1718 | } |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1719 | get_random_bytes(dummy_key, *key_size); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1720 | rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1721 | if (rc) { |
Michael Halcrow | df261c5 | 2009-01-06 14:42:02 -0800 | [diff] [blame] | 1722 | printk(KERN_ERR "Error attempting to set key of size [%zd] for " |
Dave Hansen | 3826849 | 2009-08-27 09:47:07 -0700 | [diff] [blame] | 1723 | "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name, |
| 1724 | rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1725 | rc = -EINVAL; |
| 1726 | goto out; |
| 1727 | } |
| 1728 | out: |
Dan Carpenter | ece550f | 2010-01-19 12:34:32 +0300 | [diff] [blame] | 1729 | kfree(full_alg_name); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1730 | return rc; |
| 1731 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1732 | |
| 1733 | struct kmem_cache *ecryptfs_key_tfm_cache; |
Adrian Bunk | 7896b63 | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 1734 | static struct list_head key_tfm_list; |
Eric Sandeen | af440f5 | 2008-02-06 01:38:37 -0800 | [diff] [blame] | 1735 | struct mutex key_tfm_list_mutex; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1736 | |
Jerome Marchand | 7371a38 | 2010-08-17 17:24:05 +0200 | [diff] [blame] | 1737 | int __init ecryptfs_init_crypto(void) |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1738 | { |
| 1739 | mutex_init(&key_tfm_list_mutex); |
| 1740 | INIT_LIST_HEAD(&key_tfm_list); |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
Eric Sandeen | af440f5 | 2008-02-06 01:38:37 -0800 | [diff] [blame] | 1744 | /** |
| 1745 | * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list |
| 1746 | * |
| 1747 | * Called only at module unload time |
| 1748 | */ |
Michael Halcrow | fcd1283 | 2007-10-16 01:28:01 -0700 | [diff] [blame] | 1749 | int ecryptfs_destroy_crypto(void) |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1750 | { |
| 1751 | struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp; |
| 1752 | |
| 1753 | mutex_lock(&key_tfm_list_mutex); |
| 1754 | list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list, |
| 1755 | key_tfm_list) { |
| 1756 | list_del(&key_tfm->key_tfm_list); |
| 1757 | if (key_tfm->key_tfm) |
| 1758 | crypto_free_blkcipher(key_tfm->key_tfm); |
| 1759 | kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm); |
| 1760 | } |
| 1761 | mutex_unlock(&key_tfm_list_mutex); |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
| 1765 | int |
| 1766 | ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name, |
| 1767 | size_t key_size) |
| 1768 | { |
| 1769 | struct ecryptfs_key_tfm *tmp_tfm; |
| 1770 | int rc = 0; |
| 1771 | |
Eric Sandeen | af440f5 | 2008-02-06 01:38:37 -0800 | [diff] [blame] | 1772 | BUG_ON(!mutex_is_locked(&key_tfm_list_mutex)); |
| 1773 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1774 | tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL); |
| 1775 | if (key_tfm != NULL) |
| 1776 | (*key_tfm) = tmp_tfm; |
| 1777 | if (!tmp_tfm) { |
| 1778 | rc = -ENOMEM; |
| 1779 | printk(KERN_ERR "Error attempting to allocate from " |
| 1780 | "ecryptfs_key_tfm_cache\n"); |
| 1781 | goto out; |
| 1782 | } |
| 1783 | mutex_init(&tmp_tfm->key_tfm_mutex); |
| 1784 | strncpy(tmp_tfm->cipher_name, cipher_name, |
| 1785 | ECRYPTFS_MAX_CIPHER_NAME_SIZE); |
Eric Sandeen | b886290 | 2007-12-22 14:03:24 -0800 | [diff] [blame] | 1786 | tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0'; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1787 | tmp_tfm->key_size = key_size; |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1788 | rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm, |
| 1789 | tmp_tfm->cipher_name, |
| 1790 | &tmp_tfm->key_size); |
| 1791 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1792 | printk(KERN_ERR "Error attempting to initialize key TFM " |
| 1793 | "cipher with name = [%s]; rc = [%d]\n", |
| 1794 | tmp_tfm->cipher_name, rc); |
| 1795 | kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm); |
| 1796 | if (key_tfm != NULL) |
| 1797 | (*key_tfm) = NULL; |
| 1798 | goto out; |
| 1799 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1800 | list_add(&tmp_tfm->key_tfm_list, &key_tfm_list); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1801 | out: |
| 1802 | return rc; |
| 1803 | } |
| 1804 | |
Eric Sandeen | af440f5 | 2008-02-06 01:38:37 -0800 | [diff] [blame] | 1805 | /** |
| 1806 | * ecryptfs_tfm_exists - Search for existing tfm for cipher_name. |
| 1807 | * @cipher_name: the name of the cipher to search for |
| 1808 | * @key_tfm: set to corresponding tfm if found |
| 1809 | * |
| 1810 | * Searches for cached key_tfm matching @cipher_name |
| 1811 | * Must be called with &key_tfm_list_mutex held |
| 1812 | * Returns 1 if found, with @key_tfm set |
| 1813 | * Returns 0 if not found, with @key_tfm set to NULL |
| 1814 | */ |
| 1815 | int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm) |
| 1816 | { |
| 1817 | struct ecryptfs_key_tfm *tmp_key_tfm; |
| 1818 | |
| 1819 | BUG_ON(!mutex_is_locked(&key_tfm_list_mutex)); |
| 1820 | |
| 1821 | list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) { |
| 1822 | if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) { |
| 1823 | if (key_tfm) |
| 1824 | (*key_tfm) = tmp_key_tfm; |
| 1825 | return 1; |
| 1826 | } |
| 1827 | } |
| 1828 | if (key_tfm) |
| 1829 | (*key_tfm) = NULL; |
| 1830 | return 0; |
| 1831 | } |
| 1832 | |
| 1833 | /** |
| 1834 | * ecryptfs_get_tfm_and_mutex_for_cipher_name |
| 1835 | * |
| 1836 | * @tfm: set to cached tfm found, or new tfm created |
| 1837 | * @tfm_mutex: set to mutex for cached tfm found, or new tfm created |
| 1838 | * @cipher_name: the name of the cipher to search for and/or add |
| 1839 | * |
| 1840 | * Sets pointers to @tfm & @tfm_mutex matching @cipher_name. |
| 1841 | * Searches for cached item first, and creates new if not found. |
| 1842 | * Returns 0 on success, non-zero if adding new cipher failed |
| 1843 | */ |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1844 | int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm, |
| 1845 | struct mutex **tfm_mutex, |
| 1846 | char *cipher_name) |
| 1847 | { |
| 1848 | struct ecryptfs_key_tfm *key_tfm; |
| 1849 | int rc = 0; |
| 1850 | |
| 1851 | (*tfm) = NULL; |
| 1852 | (*tfm_mutex) = NULL; |
Eric Sandeen | af440f5 | 2008-02-06 01:38:37 -0800 | [diff] [blame] | 1853 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1854 | mutex_lock(&key_tfm_list_mutex); |
Eric Sandeen | af440f5 | 2008-02-06 01:38:37 -0800 | [diff] [blame] | 1855 | if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) { |
| 1856 | rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0); |
| 1857 | if (rc) { |
| 1858 | printk(KERN_ERR "Error adding new key_tfm to list; " |
| 1859 | "rc = [%d]\n", rc); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1860 | goto out; |
| 1861 | } |
| 1862 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1863 | (*tfm) = key_tfm->key_tfm; |
| 1864 | (*tfm_mutex) = &key_tfm->key_tfm_mutex; |
| 1865 | out: |
Cyrill Gorcunov | 71fd517 | 2008-05-23 13:04:20 -0700 | [diff] [blame] | 1866 | mutex_unlock(&key_tfm_list_mutex); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1867 | return rc; |
| 1868 | } |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1869 | |
| 1870 | /* 64 characters forming a 6-bit target field */ |
| 1871 | static unsigned char *portable_filename_chars = ("-.0123456789ABCD" |
| 1872 | "EFGHIJKLMNOPQRST" |
| 1873 | "UVWXYZabcdefghij" |
| 1874 | "klmnopqrstuvwxyz"); |
| 1875 | |
| 1876 | /* We could either offset on every reverse map or just pad some 0x00's |
| 1877 | * at the front here */ |
Tyler Hicks | 0f751e6 | 2011-11-23 11:31:24 -0600 | [diff] [blame] | 1878 | static const unsigned char filename_rev_map[256] = { |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1879 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */ |
| 1880 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */ |
| 1881 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */ |
| 1882 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */ |
| 1883 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */ |
| 1884 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */ |
| 1885 | 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */ |
| 1886 | 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */ |
| 1887 | 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */ |
| 1888 | 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */ |
| 1889 | 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */ |
| 1890 | 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */ |
| 1891 | 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */ |
| 1892 | 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */ |
| 1893 | 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */ |
Tyler Hicks | 0f751e6 | 2011-11-23 11:31:24 -0600 | [diff] [blame] | 1894 | 0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */ |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1895 | }; |
| 1896 | |
| 1897 | /** |
| 1898 | * ecryptfs_encode_for_filename |
| 1899 | * @dst: Destination location for encoded filename |
| 1900 | * @dst_size: Size of the encoded filename in bytes |
| 1901 | * @src: Source location for the filename to encode |
| 1902 | * @src_size: Size of the source in bytes |
| 1903 | */ |
Cong Ding | 3702875 | 2012-12-07 22:21:56 +0000 | [diff] [blame] | 1904 | static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size, |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1905 | unsigned char *src, size_t src_size) |
| 1906 | { |
| 1907 | size_t num_blocks; |
| 1908 | size_t block_num = 0; |
| 1909 | size_t dst_offset = 0; |
| 1910 | unsigned char last_block[3]; |
| 1911 | |
| 1912 | if (src_size == 0) { |
| 1913 | (*dst_size) = 0; |
| 1914 | goto out; |
| 1915 | } |
| 1916 | num_blocks = (src_size / 3); |
| 1917 | if ((src_size % 3) == 0) { |
| 1918 | memcpy(last_block, (&src[src_size - 3]), 3); |
| 1919 | } else { |
| 1920 | num_blocks++; |
| 1921 | last_block[2] = 0x00; |
| 1922 | switch (src_size % 3) { |
| 1923 | case 1: |
| 1924 | last_block[0] = src[src_size - 1]; |
| 1925 | last_block[1] = 0x00; |
| 1926 | break; |
| 1927 | case 2: |
| 1928 | last_block[0] = src[src_size - 2]; |
| 1929 | last_block[1] = src[src_size - 1]; |
| 1930 | } |
| 1931 | } |
| 1932 | (*dst_size) = (num_blocks * 4); |
| 1933 | if (!dst) |
| 1934 | goto out; |
| 1935 | while (block_num < num_blocks) { |
| 1936 | unsigned char *src_block; |
| 1937 | unsigned char dst_block[4]; |
| 1938 | |
| 1939 | if (block_num == (num_blocks - 1)) |
| 1940 | src_block = last_block; |
| 1941 | else |
| 1942 | src_block = &src[block_num * 3]; |
| 1943 | dst_block[0] = ((src_block[0] >> 2) & 0x3F); |
| 1944 | dst_block[1] = (((src_block[0] << 4) & 0x30) |
| 1945 | | ((src_block[1] >> 4) & 0x0F)); |
| 1946 | dst_block[2] = (((src_block[1] << 2) & 0x3C) |
| 1947 | | ((src_block[2] >> 6) & 0x03)); |
| 1948 | dst_block[3] = (src_block[2] & 0x3F); |
| 1949 | dst[dst_offset++] = portable_filename_chars[dst_block[0]]; |
| 1950 | dst[dst_offset++] = portable_filename_chars[dst_block[1]]; |
| 1951 | dst[dst_offset++] = portable_filename_chars[dst_block[2]]; |
| 1952 | dst[dst_offset++] = portable_filename_chars[dst_block[3]]; |
| 1953 | block_num++; |
| 1954 | } |
| 1955 | out: |
| 1956 | return; |
| 1957 | } |
| 1958 | |
Tyler Hicks | 4a26620 | 2011-11-05 13:45:08 -0400 | [diff] [blame] | 1959 | static size_t ecryptfs_max_decoded_size(size_t encoded_size) |
| 1960 | { |
| 1961 | /* Not exact; conservatively long. Every block of 4 |
| 1962 | * encoded characters decodes into a block of 3 |
| 1963 | * decoded characters. This segment of code provides |
| 1964 | * the caller with the maximum amount of allocated |
| 1965 | * space that @dst will need to point to in a |
| 1966 | * subsequent call. */ |
| 1967 | return ((encoded_size + 1) * 3) / 4; |
| 1968 | } |
| 1969 | |
Michael Halcrow | 71c11c3 | 2009-01-06 14:42:05 -0800 | [diff] [blame] | 1970 | /** |
| 1971 | * ecryptfs_decode_from_filename |
| 1972 | * @dst: If NULL, this function only sets @dst_size and returns. If |
| 1973 | * non-NULL, this function decodes the encoded octets in @src |
| 1974 | * into the memory that @dst points to. |
| 1975 | * @dst_size: Set to the size of the decoded string. |
| 1976 | * @src: The encoded set of octets to decode. |
| 1977 | * @src_size: The size of the encoded set of octets to decode. |
| 1978 | */ |
| 1979 | static void |
| 1980 | ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size, |
| 1981 | const unsigned char *src, size_t src_size) |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1982 | { |
| 1983 | u8 current_bit_offset = 0; |
| 1984 | size_t src_byte_offset = 0; |
| 1985 | size_t dst_byte_offset = 0; |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1986 | |
| 1987 | if (dst == NULL) { |
Tyler Hicks | 4a26620 | 2011-11-05 13:45:08 -0400 | [diff] [blame] | 1988 | (*dst_size) = ecryptfs_max_decoded_size(src_size); |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 1989 | goto out; |
| 1990 | } |
| 1991 | while (src_byte_offset < src_size) { |
| 1992 | unsigned char src_byte = |
| 1993 | filename_rev_map[(int)src[src_byte_offset]]; |
| 1994 | |
| 1995 | switch (current_bit_offset) { |
| 1996 | case 0: |
| 1997 | dst[dst_byte_offset] = (src_byte << 2); |
| 1998 | current_bit_offset = 6; |
| 1999 | break; |
| 2000 | case 6: |
| 2001 | dst[dst_byte_offset++] |= (src_byte >> 4); |
| 2002 | dst[dst_byte_offset] = ((src_byte & 0xF) |
| 2003 | << 4); |
| 2004 | current_bit_offset = 4; |
| 2005 | break; |
| 2006 | case 4: |
| 2007 | dst[dst_byte_offset++] |= (src_byte >> 2); |
| 2008 | dst[dst_byte_offset] = (src_byte << 6); |
| 2009 | current_bit_offset = 2; |
| 2010 | break; |
| 2011 | case 2: |
| 2012 | dst[dst_byte_offset++] |= (src_byte); |
| 2013 | dst[dst_byte_offset] = 0; |
| 2014 | current_bit_offset = 0; |
| 2015 | break; |
| 2016 | } |
| 2017 | src_byte_offset++; |
| 2018 | } |
| 2019 | (*dst_size) = dst_byte_offset; |
| 2020 | out: |
Michael Halcrow | 71c11c3 | 2009-01-06 14:42:05 -0800 | [diff] [blame] | 2021 | return; |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | /** |
| 2025 | * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text |
| 2026 | * @crypt_stat: The crypt_stat struct associated with the file anem to encode |
| 2027 | * @name: The plaintext name |
| 2028 | * @length: The length of the plaintext |
| 2029 | * @encoded_name: The encypted name |
| 2030 | * |
| 2031 | * Encrypts and encodes a filename into something that constitutes a |
| 2032 | * valid filename for a filesystem, with printable characters. |
| 2033 | * |
| 2034 | * We assume that we have a properly initialized crypto context, |
| 2035 | * pointed to by crypt_stat->tfm. |
| 2036 | * |
| 2037 | * Returns zero on success; non-zero on otherwise |
| 2038 | */ |
| 2039 | int ecryptfs_encrypt_and_encode_filename( |
| 2040 | char **encoded_name, |
| 2041 | size_t *encoded_name_size, |
| 2042 | struct ecryptfs_crypt_stat *crypt_stat, |
| 2043 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat, |
| 2044 | const char *name, size_t name_size) |
| 2045 | { |
| 2046 | size_t encoded_name_no_prefix_size; |
| 2047 | int rc = 0; |
| 2048 | |
| 2049 | (*encoded_name) = NULL; |
| 2050 | (*encoded_name_size) = 0; |
| 2051 | if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES)) |
| 2052 | || (mount_crypt_stat && (mount_crypt_stat->flags |
| 2053 | & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) { |
| 2054 | struct ecryptfs_filename *filename; |
| 2055 | |
| 2056 | filename = kzalloc(sizeof(*filename), GFP_KERNEL); |
| 2057 | if (!filename) { |
| 2058 | printk(KERN_ERR "%s: Out of memory whilst attempting " |
Michael Halcrow | a8f1286 | 2009-01-06 14:42:03 -0800 | [diff] [blame] | 2059 | "to kzalloc [%zd] bytes\n", __func__, |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 2060 | sizeof(*filename)); |
| 2061 | rc = -ENOMEM; |
| 2062 | goto out; |
| 2063 | } |
| 2064 | filename->filename = (char *)name; |
| 2065 | filename->filename_size = name_size; |
| 2066 | rc = ecryptfs_encrypt_filename(filename, crypt_stat, |
| 2067 | mount_crypt_stat); |
| 2068 | if (rc) { |
| 2069 | printk(KERN_ERR "%s: Error attempting to encrypt " |
| 2070 | "filename; rc = [%d]\n", __func__, rc); |
| 2071 | kfree(filename); |
| 2072 | goto out; |
| 2073 | } |
| 2074 | ecryptfs_encode_for_filename( |
| 2075 | NULL, &encoded_name_no_prefix_size, |
| 2076 | filename->encrypted_filename, |
| 2077 | filename->encrypted_filename_size); |
| 2078 | if ((crypt_stat && (crypt_stat->flags |
| 2079 | & ECRYPTFS_ENCFN_USE_MOUNT_FNEK)) |
| 2080 | || (mount_crypt_stat |
| 2081 | && (mount_crypt_stat->flags |
| 2082 | & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) |
| 2083 | (*encoded_name_size) = |
| 2084 | (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE |
| 2085 | + encoded_name_no_prefix_size); |
| 2086 | else |
| 2087 | (*encoded_name_size) = |
| 2088 | (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE |
| 2089 | + encoded_name_no_prefix_size); |
| 2090 | (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL); |
| 2091 | if (!(*encoded_name)) { |
| 2092 | printk(KERN_ERR "%s: Out of memory whilst attempting " |
Michael Halcrow | a8f1286 | 2009-01-06 14:42:03 -0800 | [diff] [blame] | 2093 | "to kzalloc [%zd] bytes\n", __func__, |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 2094 | (*encoded_name_size)); |
| 2095 | rc = -ENOMEM; |
| 2096 | kfree(filename->encrypted_filename); |
| 2097 | kfree(filename); |
| 2098 | goto out; |
| 2099 | } |
| 2100 | if ((crypt_stat && (crypt_stat->flags |
| 2101 | & ECRYPTFS_ENCFN_USE_MOUNT_FNEK)) |
| 2102 | || (mount_crypt_stat |
| 2103 | && (mount_crypt_stat->flags |
| 2104 | & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) { |
| 2105 | memcpy((*encoded_name), |
| 2106 | ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX, |
| 2107 | ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE); |
| 2108 | ecryptfs_encode_for_filename( |
| 2109 | ((*encoded_name) |
| 2110 | + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE), |
| 2111 | &encoded_name_no_prefix_size, |
| 2112 | filename->encrypted_filename, |
| 2113 | filename->encrypted_filename_size); |
| 2114 | (*encoded_name_size) = |
| 2115 | (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE |
| 2116 | + encoded_name_no_prefix_size); |
| 2117 | (*encoded_name)[(*encoded_name_size)] = '\0'; |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 2118 | } else { |
Tyler Hicks | df6ad33 | 2009-08-21 04:27:46 -0500 | [diff] [blame] | 2119 | rc = -EOPNOTSUPP; |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 2120 | } |
| 2121 | if (rc) { |
| 2122 | printk(KERN_ERR "%s: Error attempting to encode " |
| 2123 | "encrypted filename; rc = [%d]\n", __func__, |
| 2124 | rc); |
| 2125 | kfree((*encoded_name)); |
| 2126 | (*encoded_name) = NULL; |
| 2127 | (*encoded_name_size) = 0; |
| 2128 | } |
| 2129 | kfree(filename->encrypted_filename); |
| 2130 | kfree(filename); |
| 2131 | } else { |
| 2132 | rc = ecryptfs_copy_filename(encoded_name, |
| 2133 | encoded_name_size, |
| 2134 | name, name_size); |
| 2135 | } |
| 2136 | out: |
| 2137 | return rc; |
| 2138 | } |
| 2139 | |
| 2140 | /** |
| 2141 | * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext |
| 2142 | * @plaintext_name: The plaintext name |
| 2143 | * @plaintext_name_size: The plaintext name size |
| 2144 | * @ecryptfs_dir_dentry: eCryptfs directory dentry |
| 2145 | * @name: The filename in cipher text |
| 2146 | * @name_size: The cipher text name size |
| 2147 | * |
| 2148 | * Decrypts and decodes the filename. |
| 2149 | * |
| 2150 | * Returns zero on error; non-zero otherwise |
| 2151 | */ |
| 2152 | int ecryptfs_decode_and_decrypt_filename(char **plaintext_name, |
| 2153 | size_t *plaintext_name_size, |
| 2154 | struct dentry *ecryptfs_dir_dentry, |
| 2155 | const char *name, size_t name_size) |
| 2156 | { |
Tyler Hicks | 2aac0cf | 2009-03-20 02:23:57 -0500 | [diff] [blame] | 2157 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 2158 | &ecryptfs_superblock_to_private( |
| 2159 | ecryptfs_dir_dentry->d_sb)->mount_crypt_stat; |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 2160 | char *decoded_name; |
| 2161 | size_t decoded_name_size; |
| 2162 | size_t packet_size; |
| 2163 | int rc = 0; |
| 2164 | |
Tyler Hicks | 2aac0cf | 2009-03-20 02:23:57 -0500 | [diff] [blame] | 2165 | if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) |
| 2166 | && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) |
| 2167 | && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 2168 | && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX, |
| 2169 | ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) { |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 2170 | const char *orig_name = name; |
| 2171 | size_t orig_name_size = name_size; |
| 2172 | |
| 2173 | name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE; |
| 2174 | name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE; |
Michael Halcrow | 71c11c3 | 2009-01-06 14:42:05 -0800 | [diff] [blame] | 2175 | ecryptfs_decode_from_filename(NULL, &decoded_name_size, |
| 2176 | name, name_size); |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 2177 | decoded_name = kmalloc(decoded_name_size, GFP_KERNEL); |
| 2178 | if (!decoded_name) { |
| 2179 | printk(KERN_ERR "%s: Out of memory whilst attempting " |
Michael Halcrow | df261c5 | 2009-01-06 14:42:02 -0800 | [diff] [blame] | 2180 | "to kmalloc [%zd] bytes\n", __func__, |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 2181 | decoded_name_size); |
| 2182 | rc = -ENOMEM; |
| 2183 | goto out; |
| 2184 | } |
Michael Halcrow | 71c11c3 | 2009-01-06 14:42:05 -0800 | [diff] [blame] | 2185 | ecryptfs_decode_from_filename(decoded_name, &decoded_name_size, |
| 2186 | name, name_size); |
Michael Halcrow | 51ca58d | 2009-01-06 14:41:59 -0800 | [diff] [blame] | 2187 | rc = ecryptfs_parse_tag_70_packet(plaintext_name, |
| 2188 | plaintext_name_size, |
| 2189 | &packet_size, |
| 2190 | mount_crypt_stat, |
| 2191 | decoded_name, |
| 2192 | decoded_name_size); |
| 2193 | if (rc) { |
| 2194 | printk(KERN_INFO "%s: Could not parse tag 70 packet " |
| 2195 | "from filename; copying through filename " |
| 2196 | "as-is\n", __func__); |
| 2197 | rc = ecryptfs_copy_filename(plaintext_name, |
| 2198 | plaintext_name_size, |
| 2199 | orig_name, orig_name_size); |
| 2200 | goto out_free; |
| 2201 | } |
| 2202 | } else { |
| 2203 | rc = ecryptfs_copy_filename(plaintext_name, |
| 2204 | plaintext_name_size, |
| 2205 | name, name_size); |
| 2206 | goto out; |
| 2207 | } |
| 2208 | out_free: |
| 2209 | kfree(decoded_name); |
| 2210 | out: |
| 2211 | return rc; |
| 2212 | } |
Tyler Hicks | 4a26620 | 2011-11-05 13:45:08 -0400 | [diff] [blame] | 2213 | |
| 2214 | #define ENC_NAME_MAX_BLOCKLEN_8_OR_16 143 |
| 2215 | |
| 2216 | int ecryptfs_set_f_namelen(long *namelen, long lower_namelen, |
| 2217 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 2218 | { |
| 2219 | struct blkcipher_desc desc; |
| 2220 | struct mutex *tfm_mutex; |
| 2221 | size_t cipher_blocksize; |
| 2222 | int rc; |
| 2223 | |
| 2224 | if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) { |
| 2225 | (*namelen) = lower_namelen; |
| 2226 | return 0; |
| 2227 | } |
| 2228 | |
| 2229 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, |
| 2230 | mount_crypt_stat->global_default_fn_cipher_name); |
| 2231 | if (unlikely(rc)) { |
| 2232 | (*namelen) = 0; |
| 2233 | return rc; |
| 2234 | } |
| 2235 | |
| 2236 | mutex_lock(tfm_mutex); |
| 2237 | cipher_blocksize = crypto_blkcipher_blocksize(desc.tfm); |
| 2238 | mutex_unlock(tfm_mutex); |
| 2239 | |
| 2240 | /* Return an exact amount for the common cases */ |
| 2241 | if (lower_namelen == NAME_MAX |
| 2242 | && (cipher_blocksize == 8 || cipher_blocksize == 16)) { |
| 2243 | (*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16; |
| 2244 | return 0; |
| 2245 | } |
| 2246 | |
| 2247 | /* Return a safe estimate for the uncommon cases */ |
| 2248 | (*namelen) = lower_namelen; |
| 2249 | (*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE; |
| 2250 | /* Since this is the max decoded size, subtract 1 "decoded block" len */ |
| 2251 | (*namelen) = ecryptfs_max_decoded_size(*namelen) - 3; |
| 2252 | (*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE; |
| 2253 | (*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES; |
| 2254 | /* Worst case is that the filename is padded nearly a full block size */ |
| 2255 | (*namelen) -= cipher_blocksize - 1; |
| 2256 | |
| 2257 | if ((*namelen) < 0) |
| 2258 | (*namelen) = 0; |
| 2259 | |
| 2260 | return 0; |
| 2261 | } |