Commit 84814d642a4f1f294bd675ab11aae1ca54c6cedb

Authored by Tyler Hicks
Committed by Linus Torvalds
1 parent 15e7b87676

eCryptfs: don't encrypt file key with filename key

eCryptfs has file encryption keys (FEK), file encryption key encryption
keys (FEKEK), and filename encryption keys (FNEK).  The per-file FEK is
encrypted with one or more FEKEKs and stored in the header of the
encrypted file.  I noticed that the FEK is also being encrypted by the
FNEK.  This is a problem if a user wants to use a different FNEK than
their FEKEK, as their file contents will still be accessible with the
FNEK.

This is a minimalistic patch which prevents the FNEKs signatures from
being copied to the inode signatures list.  Ultimately, it keeps the FEK
from being encrypted with a FNEK.

Signed-off-by: Tyler Hicks <tyhicks@linux.vnet.ibm.com>
Cc: Serge Hallyn <serue@us.ibm.com>
Acked-by: Dustin Kirkland <kirkland@canonical.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 4 changed files with 9 additions and 4 deletions Inline Diff

fs/ecryptfs/crypto.c
1 /** 1 /**
2 * eCryptfs: Linux filesystem encryption layer 2 * eCryptfs: Linux filesystem encryption layer
3 * 3 *
4 * Copyright (C) 1997-2004 Erez Zadok 4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University 5 * Copyright (C) 2001-2004 Stony Brook University
6 * Copyright (C) 2004-2007 International Business Machines Corp. 6 * Copyright (C) 2004-2007 International Business Machines Corp.
7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com> 8 * Michael C. Thompson <mcthomps@us.ibm.com>
9 * 9 *
10 * This program is free software; you can redistribute it and/or 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 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 12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version. 13 * License, or (at your option) any later version.
14 * 14 *
15 * This program is distributed in the hope that it will be useful, but 15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of 16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details. 18 * General Public License for more details.
19 * 19 *
20 * You should have received a copy of the GNU General Public License 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 21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA. 23 * 02111-1307, USA.
24 */ 24 */
25 25
26 #include <linux/fs.h> 26 #include <linux/fs.h>
27 #include <linux/mount.h> 27 #include <linux/mount.h>
28 #include <linux/pagemap.h> 28 #include <linux/pagemap.h>
29 #include <linux/random.h> 29 #include <linux/random.h>
30 #include <linux/compiler.h> 30 #include <linux/compiler.h>
31 #include <linux/key.h> 31 #include <linux/key.h>
32 #include <linux/namei.h> 32 #include <linux/namei.h>
33 #include <linux/crypto.h> 33 #include <linux/crypto.h>
34 #include <linux/file.h> 34 #include <linux/file.h>
35 #include <linux/scatterlist.h> 35 #include <linux/scatterlist.h>
36 #include <asm/unaligned.h> 36 #include <asm/unaligned.h>
37 #include "ecryptfs_kernel.h" 37 #include "ecryptfs_kernel.h"
38 38
39 static int 39 static int
40 ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, 40 ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
41 struct page *dst_page, int dst_offset, 41 struct page *dst_page, int dst_offset,
42 struct page *src_page, int src_offset, int size, 42 struct page *src_page, int src_offset, int size,
43 unsigned char *iv); 43 unsigned char *iv);
44 static int 44 static int
45 ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, 45 ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
46 struct page *dst_page, int dst_offset, 46 struct page *dst_page, int dst_offset,
47 struct page *src_page, int src_offset, int size, 47 struct page *src_page, int src_offset, int size,
48 unsigned char *iv); 48 unsigned char *iv);
49 49
50 /** 50 /**
51 * ecryptfs_to_hex 51 * ecryptfs_to_hex
52 * @dst: Buffer to take hex character representation of contents of 52 * @dst: Buffer to take hex character representation of contents of
53 * src; must be at least of size (src_size * 2) 53 * src; must be at least of size (src_size * 2)
54 * @src: Buffer to be converted to a hex string respresentation 54 * @src: Buffer to be converted to a hex string respresentation
55 * @src_size: number of bytes to convert 55 * @src_size: number of bytes to convert
56 */ 56 */
57 void ecryptfs_to_hex(char *dst, char *src, size_t src_size) 57 void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
58 { 58 {
59 int x; 59 int x;
60 60
61 for (x = 0; x < src_size; x++) 61 for (x = 0; x < src_size; x++)
62 sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]); 62 sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
63 } 63 }
64 64
65 /** 65 /**
66 * ecryptfs_from_hex 66 * ecryptfs_from_hex
67 * @dst: Buffer to take the bytes from src hex; must be at least of 67 * @dst: Buffer to take the bytes from src hex; must be at least of
68 * size (src_size / 2) 68 * size (src_size / 2)
69 * @src: Buffer to be converted from a hex string respresentation to raw value 69 * @src: Buffer to be converted from a hex string respresentation to raw value
70 * @dst_size: size of dst buffer, or number of hex characters pairs to convert 70 * @dst_size: size of dst buffer, or number of hex characters pairs to convert
71 */ 71 */
72 void ecryptfs_from_hex(char *dst, char *src, int dst_size) 72 void ecryptfs_from_hex(char *dst, char *src, int dst_size)
73 { 73 {
74 int x; 74 int x;
75 char tmp[3] = { 0, }; 75 char tmp[3] = { 0, };
76 76
77 for (x = 0; x < dst_size; x++) { 77 for (x = 0; x < dst_size; x++) {
78 tmp[0] = src[x * 2]; 78 tmp[0] = src[x * 2];
79 tmp[1] = src[x * 2 + 1]; 79 tmp[1] = src[x * 2 + 1];
80 dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16); 80 dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
81 } 81 }
82 } 82 }
83 83
84 /** 84 /**
85 * ecryptfs_calculate_md5 - calculates the md5 of @src 85 * ecryptfs_calculate_md5 - calculates the md5 of @src
86 * @dst: Pointer to 16 bytes of allocated memory 86 * @dst: Pointer to 16 bytes of allocated memory
87 * @crypt_stat: Pointer to crypt_stat struct for the current inode 87 * @crypt_stat: Pointer to crypt_stat struct for the current inode
88 * @src: Data to be md5'd 88 * @src: Data to be md5'd
89 * @len: Length of @src 89 * @len: Length of @src
90 * 90 *
91 * Uses the allocated crypto context that crypt_stat references to 91 * Uses the allocated crypto context that crypt_stat references to
92 * generate the MD5 sum of the contents of src. 92 * generate the MD5 sum of the contents of src.
93 */ 93 */
94 static int ecryptfs_calculate_md5(char *dst, 94 static int ecryptfs_calculate_md5(char *dst,
95 struct ecryptfs_crypt_stat *crypt_stat, 95 struct ecryptfs_crypt_stat *crypt_stat,
96 char *src, int len) 96 char *src, int len)
97 { 97 {
98 struct scatterlist sg; 98 struct scatterlist sg;
99 struct hash_desc desc = { 99 struct hash_desc desc = {
100 .tfm = crypt_stat->hash_tfm, 100 .tfm = crypt_stat->hash_tfm,
101 .flags = CRYPTO_TFM_REQ_MAY_SLEEP 101 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
102 }; 102 };
103 int rc = 0; 103 int rc = 0;
104 104
105 mutex_lock(&crypt_stat->cs_hash_tfm_mutex); 105 mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
106 sg_init_one(&sg, (u8 *)src, len); 106 sg_init_one(&sg, (u8 *)src, len);
107 if (!desc.tfm) { 107 if (!desc.tfm) {
108 desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0, 108 desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
109 CRYPTO_ALG_ASYNC); 109 CRYPTO_ALG_ASYNC);
110 if (IS_ERR(desc.tfm)) { 110 if (IS_ERR(desc.tfm)) {
111 rc = PTR_ERR(desc.tfm); 111 rc = PTR_ERR(desc.tfm);
112 ecryptfs_printk(KERN_ERR, "Error attempting to " 112 ecryptfs_printk(KERN_ERR, "Error attempting to "
113 "allocate crypto context; rc = [%d]\n", 113 "allocate crypto context; rc = [%d]\n",
114 rc); 114 rc);
115 goto out; 115 goto out;
116 } 116 }
117 crypt_stat->hash_tfm = desc.tfm; 117 crypt_stat->hash_tfm = desc.tfm;
118 } 118 }
119 rc = crypto_hash_init(&desc); 119 rc = crypto_hash_init(&desc);
120 if (rc) { 120 if (rc) {
121 printk(KERN_ERR 121 printk(KERN_ERR
122 "%s: Error initializing crypto hash; rc = [%d]\n", 122 "%s: Error initializing crypto hash; rc = [%d]\n",
123 __func__, rc); 123 __func__, rc);
124 goto out; 124 goto out;
125 } 125 }
126 rc = crypto_hash_update(&desc, &sg, len); 126 rc = crypto_hash_update(&desc, &sg, len);
127 if (rc) { 127 if (rc) {
128 printk(KERN_ERR 128 printk(KERN_ERR
129 "%s: Error updating crypto hash; rc = [%d]\n", 129 "%s: Error updating crypto hash; rc = [%d]\n",
130 __func__, rc); 130 __func__, rc);
131 goto out; 131 goto out;
132 } 132 }
133 rc = crypto_hash_final(&desc, dst); 133 rc = crypto_hash_final(&desc, dst);
134 if (rc) { 134 if (rc) {
135 printk(KERN_ERR 135 printk(KERN_ERR
136 "%s: Error finalizing crypto hash; rc = [%d]\n", 136 "%s: Error finalizing crypto hash; rc = [%d]\n",
137 __func__, rc); 137 __func__, rc);
138 goto out; 138 goto out;
139 } 139 }
140 out: 140 out:
141 mutex_unlock(&crypt_stat->cs_hash_tfm_mutex); 141 mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
142 return rc; 142 return rc;
143 } 143 }
144 144
145 static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name, 145 static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
146 char *cipher_name, 146 char *cipher_name,
147 char *chaining_modifier) 147 char *chaining_modifier)
148 { 148 {
149 int cipher_name_len = strlen(cipher_name); 149 int cipher_name_len = strlen(cipher_name);
150 int chaining_modifier_len = strlen(chaining_modifier); 150 int chaining_modifier_len = strlen(chaining_modifier);
151 int algified_name_len; 151 int algified_name_len;
152 int rc; 152 int rc;
153 153
154 algified_name_len = (chaining_modifier_len + cipher_name_len + 3); 154 algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
155 (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL); 155 (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
156 if (!(*algified_name)) { 156 if (!(*algified_name)) {
157 rc = -ENOMEM; 157 rc = -ENOMEM;
158 goto out; 158 goto out;
159 } 159 }
160 snprintf((*algified_name), algified_name_len, "%s(%s)", 160 snprintf((*algified_name), algified_name_len, "%s(%s)",
161 chaining_modifier, cipher_name); 161 chaining_modifier, cipher_name);
162 rc = 0; 162 rc = 0;
163 out: 163 out:
164 return rc; 164 return rc;
165 } 165 }
166 166
167 /** 167 /**
168 * ecryptfs_derive_iv 168 * ecryptfs_derive_iv
169 * @iv: destination for the derived iv vale 169 * @iv: destination for the derived iv vale
170 * @crypt_stat: Pointer to crypt_stat struct for the current inode 170 * @crypt_stat: Pointer to crypt_stat struct for the current inode
171 * @offset: Offset of the extent whose IV we are to derive 171 * @offset: Offset of the extent whose IV we are to derive
172 * 172 *
173 * Generate the initialization vector from the given root IV and page 173 * Generate the initialization vector from the given root IV and page
174 * offset. 174 * offset.
175 * 175 *
176 * Returns zero on success; non-zero on error. 176 * Returns zero on success; non-zero on error.
177 */ 177 */
178 int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat, 178 int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
179 loff_t offset) 179 loff_t offset)
180 { 180 {
181 int rc = 0; 181 int rc = 0;
182 char dst[MD5_DIGEST_SIZE]; 182 char dst[MD5_DIGEST_SIZE];
183 char src[ECRYPTFS_MAX_IV_BYTES + 16]; 183 char src[ECRYPTFS_MAX_IV_BYTES + 16];
184 184
185 if (unlikely(ecryptfs_verbosity > 0)) { 185 if (unlikely(ecryptfs_verbosity > 0)) {
186 ecryptfs_printk(KERN_DEBUG, "root iv:\n"); 186 ecryptfs_printk(KERN_DEBUG, "root iv:\n");
187 ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes); 187 ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
188 } 188 }
189 /* TODO: It is probably secure to just cast the least 189 /* TODO: It is probably secure to just cast the least
190 * significant bits of the root IV into an unsigned long and 190 * significant bits of the root IV into an unsigned long and
191 * add the offset to that rather than go through all this 191 * add the offset to that rather than go through all this
192 * hashing business. -Halcrow */ 192 * hashing business. -Halcrow */
193 memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes); 193 memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
194 memset((src + crypt_stat->iv_bytes), 0, 16); 194 memset((src + crypt_stat->iv_bytes), 0, 16);
195 snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset); 195 snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
196 if (unlikely(ecryptfs_verbosity > 0)) { 196 if (unlikely(ecryptfs_verbosity > 0)) {
197 ecryptfs_printk(KERN_DEBUG, "source:\n"); 197 ecryptfs_printk(KERN_DEBUG, "source:\n");
198 ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16)); 198 ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
199 } 199 }
200 rc = ecryptfs_calculate_md5(dst, crypt_stat, src, 200 rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
201 (crypt_stat->iv_bytes + 16)); 201 (crypt_stat->iv_bytes + 16));
202 if (rc) { 202 if (rc) {
203 ecryptfs_printk(KERN_WARNING, "Error attempting to compute " 203 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
204 "MD5 while generating IV for a page\n"); 204 "MD5 while generating IV for a page\n");
205 goto out; 205 goto out;
206 } 206 }
207 memcpy(iv, dst, crypt_stat->iv_bytes); 207 memcpy(iv, dst, crypt_stat->iv_bytes);
208 if (unlikely(ecryptfs_verbosity > 0)) { 208 if (unlikely(ecryptfs_verbosity > 0)) {
209 ecryptfs_printk(KERN_DEBUG, "derived iv:\n"); 209 ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
210 ecryptfs_dump_hex(iv, crypt_stat->iv_bytes); 210 ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
211 } 211 }
212 out: 212 out:
213 return rc; 213 return rc;
214 } 214 }
215 215
216 /** 216 /**
217 * ecryptfs_init_crypt_stat 217 * ecryptfs_init_crypt_stat
218 * @crypt_stat: Pointer to the crypt_stat struct to initialize. 218 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
219 * 219 *
220 * Initialize the crypt_stat structure. 220 * Initialize the crypt_stat structure.
221 */ 221 */
222 void 222 void
223 ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) 223 ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
224 { 224 {
225 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); 225 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
226 INIT_LIST_HEAD(&crypt_stat->keysig_list); 226 INIT_LIST_HEAD(&crypt_stat->keysig_list);
227 mutex_init(&crypt_stat->keysig_list_mutex); 227 mutex_init(&crypt_stat->keysig_list_mutex);
228 mutex_init(&crypt_stat->cs_mutex); 228 mutex_init(&crypt_stat->cs_mutex);
229 mutex_init(&crypt_stat->cs_tfm_mutex); 229 mutex_init(&crypt_stat->cs_tfm_mutex);
230 mutex_init(&crypt_stat->cs_hash_tfm_mutex); 230 mutex_init(&crypt_stat->cs_hash_tfm_mutex);
231 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED; 231 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
232 } 232 }
233 233
234 /** 234 /**
235 * ecryptfs_destroy_crypt_stat 235 * ecryptfs_destroy_crypt_stat
236 * @crypt_stat: Pointer to the crypt_stat struct to initialize. 236 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
237 * 237 *
238 * Releases all memory associated with a crypt_stat struct. 238 * Releases all memory associated with a crypt_stat struct.
239 */ 239 */
240 void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat) 240 void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
241 { 241 {
242 struct ecryptfs_key_sig *key_sig, *key_sig_tmp; 242 struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
243 243
244 if (crypt_stat->tfm) 244 if (crypt_stat->tfm)
245 crypto_free_blkcipher(crypt_stat->tfm); 245 crypto_free_blkcipher(crypt_stat->tfm);
246 if (crypt_stat->hash_tfm) 246 if (crypt_stat->hash_tfm)
247 crypto_free_hash(crypt_stat->hash_tfm); 247 crypto_free_hash(crypt_stat->hash_tfm);
248 mutex_lock(&crypt_stat->keysig_list_mutex); 248 mutex_lock(&crypt_stat->keysig_list_mutex);
249 list_for_each_entry_safe(key_sig, key_sig_tmp, 249 list_for_each_entry_safe(key_sig, key_sig_tmp,
250 &crypt_stat->keysig_list, crypt_stat_list) { 250 &crypt_stat->keysig_list, crypt_stat_list) {
251 list_del(&key_sig->crypt_stat_list); 251 list_del(&key_sig->crypt_stat_list);
252 kmem_cache_free(ecryptfs_key_sig_cache, key_sig); 252 kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
253 } 253 }
254 mutex_unlock(&crypt_stat->keysig_list_mutex); 254 mutex_unlock(&crypt_stat->keysig_list_mutex);
255 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat)); 255 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
256 } 256 }
257 257
258 void ecryptfs_destroy_mount_crypt_stat( 258 void ecryptfs_destroy_mount_crypt_stat(
259 struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 259 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
260 { 260 {
261 struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp; 261 struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
262 262
263 if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED)) 263 if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
264 return; 264 return;
265 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 265 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
266 list_for_each_entry_safe(auth_tok, auth_tok_tmp, 266 list_for_each_entry_safe(auth_tok, auth_tok_tmp,
267 &mount_crypt_stat->global_auth_tok_list, 267 &mount_crypt_stat->global_auth_tok_list,
268 mount_crypt_stat_list) { 268 mount_crypt_stat_list) {
269 list_del(&auth_tok->mount_crypt_stat_list); 269 list_del(&auth_tok->mount_crypt_stat_list);
270 mount_crypt_stat->num_global_auth_toks--; 270 mount_crypt_stat->num_global_auth_toks--;
271 if (auth_tok->global_auth_tok_key 271 if (auth_tok->global_auth_tok_key
272 && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID)) 272 && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
273 key_put(auth_tok->global_auth_tok_key); 273 key_put(auth_tok->global_auth_tok_key);
274 kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok); 274 kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
275 } 275 }
276 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 276 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
277 memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat)); 277 memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
278 } 278 }
279 279
280 /** 280 /**
281 * virt_to_scatterlist 281 * virt_to_scatterlist
282 * @addr: Virtual address 282 * @addr: Virtual address
283 * @size: Size of data; should be an even multiple of the block size 283 * @size: Size of data; should be an even multiple of the block size
284 * @sg: Pointer to scatterlist array; set to NULL to obtain only 284 * @sg: Pointer to scatterlist array; set to NULL to obtain only
285 * the number of scatterlist structs required in array 285 * the number of scatterlist structs required in array
286 * @sg_size: Max array size 286 * @sg_size: Max array size
287 * 287 *
288 * Fills in a scatterlist array with page references for a passed 288 * Fills in a scatterlist array with page references for a passed
289 * virtual address. 289 * virtual address.
290 * 290 *
291 * Returns the number of scatterlist structs in array used 291 * Returns the number of scatterlist structs in array used
292 */ 292 */
293 int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg, 293 int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
294 int sg_size) 294 int sg_size)
295 { 295 {
296 int i = 0; 296 int i = 0;
297 struct page *pg; 297 struct page *pg;
298 int offset; 298 int offset;
299 int remainder_of_page; 299 int remainder_of_page;
300 300
301 sg_init_table(sg, sg_size); 301 sg_init_table(sg, sg_size);
302 302
303 while (size > 0 && i < sg_size) { 303 while (size > 0 && i < sg_size) {
304 pg = virt_to_page(addr); 304 pg = virt_to_page(addr);
305 offset = offset_in_page(addr); 305 offset = offset_in_page(addr);
306 if (sg) 306 if (sg)
307 sg_set_page(&sg[i], pg, 0, offset); 307 sg_set_page(&sg[i], pg, 0, offset);
308 remainder_of_page = PAGE_CACHE_SIZE - offset; 308 remainder_of_page = PAGE_CACHE_SIZE - offset;
309 if (size >= remainder_of_page) { 309 if (size >= remainder_of_page) {
310 if (sg) 310 if (sg)
311 sg[i].length = remainder_of_page; 311 sg[i].length = remainder_of_page;
312 addr += remainder_of_page; 312 addr += remainder_of_page;
313 size -= remainder_of_page; 313 size -= remainder_of_page;
314 } else { 314 } else {
315 if (sg) 315 if (sg)
316 sg[i].length = size; 316 sg[i].length = size;
317 addr += size; 317 addr += size;
318 size = 0; 318 size = 0;
319 } 319 }
320 i++; 320 i++;
321 } 321 }
322 if (size > 0) 322 if (size > 0)
323 return -ENOMEM; 323 return -ENOMEM;
324 return i; 324 return i;
325 } 325 }
326 326
327 /** 327 /**
328 * encrypt_scatterlist 328 * encrypt_scatterlist
329 * @crypt_stat: Pointer to the crypt_stat struct to initialize. 329 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
330 * @dest_sg: Destination of encrypted data 330 * @dest_sg: Destination of encrypted data
331 * @src_sg: Data to be encrypted 331 * @src_sg: Data to be encrypted
332 * @size: Length of data to be encrypted 332 * @size: Length of data to be encrypted
333 * @iv: iv to use during encryption 333 * @iv: iv to use during encryption
334 * 334 *
335 * Returns the number of bytes encrypted; negative value on error 335 * Returns the number of bytes encrypted; negative value on error
336 */ 336 */
337 static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, 337 static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
338 struct scatterlist *dest_sg, 338 struct scatterlist *dest_sg,
339 struct scatterlist *src_sg, int size, 339 struct scatterlist *src_sg, int size,
340 unsigned char *iv) 340 unsigned char *iv)
341 { 341 {
342 struct blkcipher_desc desc = { 342 struct blkcipher_desc desc = {
343 .tfm = crypt_stat->tfm, 343 .tfm = crypt_stat->tfm,
344 .info = iv, 344 .info = iv,
345 .flags = CRYPTO_TFM_REQ_MAY_SLEEP 345 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
346 }; 346 };
347 int rc = 0; 347 int rc = 0;
348 348
349 BUG_ON(!crypt_stat || !crypt_stat->tfm 349 BUG_ON(!crypt_stat || !crypt_stat->tfm
350 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)); 350 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
351 if (unlikely(ecryptfs_verbosity > 0)) { 351 if (unlikely(ecryptfs_verbosity > 0)) {
352 ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n", 352 ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
353 crypt_stat->key_size); 353 crypt_stat->key_size);
354 ecryptfs_dump_hex(crypt_stat->key, 354 ecryptfs_dump_hex(crypt_stat->key,
355 crypt_stat->key_size); 355 crypt_stat->key_size);
356 } 356 }
357 /* Consider doing this once, when the file is opened */ 357 /* Consider doing this once, when the file is opened */
358 mutex_lock(&crypt_stat->cs_tfm_mutex); 358 mutex_lock(&crypt_stat->cs_tfm_mutex);
359 if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) { 359 if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
360 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key, 360 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
361 crypt_stat->key_size); 361 crypt_stat->key_size);
362 crypt_stat->flags |= ECRYPTFS_KEY_SET; 362 crypt_stat->flags |= ECRYPTFS_KEY_SET;
363 } 363 }
364 if (rc) { 364 if (rc) {
365 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n", 365 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
366 rc); 366 rc);
367 mutex_unlock(&crypt_stat->cs_tfm_mutex); 367 mutex_unlock(&crypt_stat->cs_tfm_mutex);
368 rc = -EINVAL; 368 rc = -EINVAL;
369 goto out; 369 goto out;
370 } 370 }
371 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size); 371 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
372 crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size); 372 crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
373 mutex_unlock(&crypt_stat->cs_tfm_mutex); 373 mutex_unlock(&crypt_stat->cs_tfm_mutex);
374 out: 374 out:
375 return rc; 375 return rc;
376 } 376 }
377 377
378 /** 378 /**
379 * ecryptfs_lower_offset_for_extent 379 * ecryptfs_lower_offset_for_extent
380 * 380 *
381 * Convert an eCryptfs page index into a lower byte offset 381 * Convert an eCryptfs page index into a lower byte offset
382 */ 382 */
383 static void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num, 383 static void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
384 struct ecryptfs_crypt_stat *crypt_stat) 384 struct ecryptfs_crypt_stat *crypt_stat)
385 { 385 {
386 (*offset) = (crypt_stat->num_header_bytes_at_front 386 (*offset) = (crypt_stat->num_header_bytes_at_front
387 + (crypt_stat->extent_size * extent_num)); 387 + (crypt_stat->extent_size * extent_num));
388 } 388 }
389 389
390 /** 390 /**
391 * ecryptfs_encrypt_extent 391 * ecryptfs_encrypt_extent
392 * @enc_extent_page: Allocated page into which to encrypt the data in 392 * @enc_extent_page: Allocated page into which to encrypt the data in
393 * @page 393 * @page
394 * @crypt_stat: crypt_stat containing cryptographic context for the 394 * @crypt_stat: crypt_stat containing cryptographic context for the
395 * encryption operation 395 * encryption operation
396 * @page: Page containing plaintext data extent to encrypt 396 * @page: Page containing plaintext data extent to encrypt
397 * @extent_offset: Page extent offset for use in generating IV 397 * @extent_offset: Page extent offset for use in generating IV
398 * 398 *
399 * Encrypts one extent of data. 399 * Encrypts one extent of data.
400 * 400 *
401 * Return zero on success; non-zero otherwise 401 * Return zero on success; non-zero otherwise
402 */ 402 */
403 static int ecryptfs_encrypt_extent(struct page *enc_extent_page, 403 static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
404 struct ecryptfs_crypt_stat *crypt_stat, 404 struct ecryptfs_crypt_stat *crypt_stat,
405 struct page *page, 405 struct page *page,
406 unsigned long extent_offset) 406 unsigned long extent_offset)
407 { 407 {
408 loff_t extent_base; 408 loff_t extent_base;
409 char extent_iv[ECRYPTFS_MAX_IV_BYTES]; 409 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
410 int rc; 410 int rc;
411 411
412 extent_base = (((loff_t)page->index) 412 extent_base = (((loff_t)page->index)
413 * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); 413 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
414 rc = ecryptfs_derive_iv(extent_iv, crypt_stat, 414 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
415 (extent_base + extent_offset)); 415 (extent_base + extent_offset));
416 if (rc) { 416 if (rc) {
417 ecryptfs_printk(KERN_ERR, "Error attempting to " 417 ecryptfs_printk(KERN_ERR, "Error attempting to "
418 "derive IV for extent [0x%.16x]; " 418 "derive IV for extent [0x%.16x]; "
419 "rc = [%d]\n", (extent_base + extent_offset), 419 "rc = [%d]\n", (extent_base + extent_offset),
420 rc); 420 rc);
421 goto out; 421 goto out;
422 } 422 }
423 if (unlikely(ecryptfs_verbosity > 0)) { 423 if (unlikely(ecryptfs_verbosity > 0)) {
424 ecryptfs_printk(KERN_DEBUG, "Encrypting extent " 424 ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
425 "with iv:\n"); 425 "with iv:\n");
426 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes); 426 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
427 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before " 427 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
428 "encryption:\n"); 428 "encryption:\n");
429 ecryptfs_dump_hex((char *) 429 ecryptfs_dump_hex((char *)
430 (page_address(page) 430 (page_address(page)
431 + (extent_offset * crypt_stat->extent_size)), 431 + (extent_offset * crypt_stat->extent_size)),
432 8); 432 8);
433 } 433 }
434 rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0, 434 rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
435 page, (extent_offset 435 page, (extent_offset
436 * crypt_stat->extent_size), 436 * crypt_stat->extent_size),
437 crypt_stat->extent_size, extent_iv); 437 crypt_stat->extent_size, extent_iv);
438 if (rc < 0) { 438 if (rc < 0) {
439 printk(KERN_ERR "%s: Error attempting to encrypt page with " 439 printk(KERN_ERR "%s: Error attempting to encrypt page with "
440 "page->index = [%ld], extent_offset = [%ld]; " 440 "page->index = [%ld], extent_offset = [%ld]; "
441 "rc = [%d]\n", __func__, page->index, extent_offset, 441 "rc = [%d]\n", __func__, page->index, extent_offset,
442 rc); 442 rc);
443 goto out; 443 goto out;
444 } 444 }
445 rc = 0; 445 rc = 0;
446 if (unlikely(ecryptfs_verbosity > 0)) { 446 if (unlikely(ecryptfs_verbosity > 0)) {
447 ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; " 447 ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
448 "rc = [%d]\n", (extent_base + extent_offset), 448 "rc = [%d]\n", (extent_base + extent_offset),
449 rc); 449 rc);
450 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after " 450 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
451 "encryption:\n"); 451 "encryption:\n");
452 ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8); 452 ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
453 } 453 }
454 out: 454 out:
455 return rc; 455 return rc;
456 } 456 }
457 457
458 /** 458 /**
459 * ecryptfs_encrypt_page 459 * ecryptfs_encrypt_page
460 * @page: Page mapped from the eCryptfs inode for the file; contains 460 * @page: Page mapped from the eCryptfs inode for the file; contains
461 * decrypted content that needs to be encrypted (to a temporary 461 * decrypted content that needs to be encrypted (to a temporary
462 * page; not in place) and written out to the lower file 462 * page; not in place) and written out to the lower file
463 * 463 *
464 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note 464 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
465 * that eCryptfs pages may straddle the lower pages -- for instance, 465 * that eCryptfs pages may straddle the lower pages -- for instance,
466 * if the file was created on a machine with an 8K page size 466 * if the file was created on a machine with an 8K page size
467 * (resulting in an 8K header), and then the file is copied onto a 467 * (resulting in an 8K header), and then the file is copied onto a
468 * host with a 32K page size, then when reading page 0 of the eCryptfs 468 * host with a 32K page size, then when reading page 0 of the eCryptfs
469 * file, 24K of page 0 of the lower file will be read and decrypted, 469 * file, 24K of page 0 of the lower file will be read and decrypted,
470 * and then 8K of page 1 of the lower file will be read and decrypted. 470 * and then 8K of page 1 of the lower file will be read and decrypted.
471 * 471 *
472 * Returns zero on success; negative on error 472 * Returns zero on success; negative on error
473 */ 473 */
474 int ecryptfs_encrypt_page(struct page *page) 474 int ecryptfs_encrypt_page(struct page *page)
475 { 475 {
476 struct inode *ecryptfs_inode; 476 struct inode *ecryptfs_inode;
477 struct ecryptfs_crypt_stat *crypt_stat; 477 struct ecryptfs_crypt_stat *crypt_stat;
478 char *enc_extent_virt; 478 char *enc_extent_virt;
479 struct page *enc_extent_page = NULL; 479 struct page *enc_extent_page = NULL;
480 loff_t extent_offset; 480 loff_t extent_offset;
481 int rc = 0; 481 int rc = 0;
482 482
483 ecryptfs_inode = page->mapping->host; 483 ecryptfs_inode = page->mapping->host;
484 crypt_stat = 484 crypt_stat =
485 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); 485 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
486 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 486 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
487 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 487 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page,
488 0, PAGE_CACHE_SIZE); 488 0, PAGE_CACHE_SIZE);
489 if (rc) 489 if (rc)
490 printk(KERN_ERR "%s: Error attempting to copy " 490 printk(KERN_ERR "%s: Error attempting to copy "
491 "page at index [%ld]\n", __func__, 491 "page at index [%ld]\n", __func__,
492 page->index); 492 page->index);
493 goto out; 493 goto out;
494 } 494 }
495 enc_extent_page = alloc_page(GFP_USER); 495 enc_extent_page = alloc_page(GFP_USER);
496 if (!enc_extent_page) { 496 if (!enc_extent_page) {
497 rc = -ENOMEM; 497 rc = -ENOMEM;
498 ecryptfs_printk(KERN_ERR, "Error allocating memory for " 498 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
499 "encrypted extent\n"); 499 "encrypted extent\n");
500 goto out; 500 goto out;
501 } 501 }
502 enc_extent_virt = kmap(enc_extent_page); 502 enc_extent_virt = kmap(enc_extent_page);
503 for (extent_offset = 0; 503 for (extent_offset = 0;
504 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); 504 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
505 extent_offset++) { 505 extent_offset++) {
506 loff_t offset; 506 loff_t offset;
507 507
508 rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page, 508 rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
509 extent_offset); 509 extent_offset);
510 if (rc) { 510 if (rc) {
511 printk(KERN_ERR "%s: Error encrypting extent; " 511 printk(KERN_ERR "%s: Error encrypting extent; "
512 "rc = [%d]\n", __func__, rc); 512 "rc = [%d]\n", __func__, rc);
513 goto out; 513 goto out;
514 } 514 }
515 ecryptfs_lower_offset_for_extent( 515 ecryptfs_lower_offset_for_extent(
516 &offset, ((((loff_t)page->index) 516 &offset, ((((loff_t)page->index)
517 * (PAGE_CACHE_SIZE 517 * (PAGE_CACHE_SIZE
518 / crypt_stat->extent_size)) 518 / crypt_stat->extent_size))
519 + extent_offset), crypt_stat); 519 + extent_offset), crypt_stat);
520 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, 520 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
521 offset, crypt_stat->extent_size); 521 offset, crypt_stat->extent_size);
522 if (rc) { 522 if (rc) {
523 ecryptfs_printk(KERN_ERR, "Error attempting " 523 ecryptfs_printk(KERN_ERR, "Error attempting "
524 "to write lower page; rc = [%d]" 524 "to write lower page; rc = [%d]"
525 "\n", rc); 525 "\n", rc);
526 goto out; 526 goto out;
527 } 527 }
528 } 528 }
529 out: 529 out:
530 if (enc_extent_page) { 530 if (enc_extent_page) {
531 kunmap(enc_extent_page); 531 kunmap(enc_extent_page);
532 __free_page(enc_extent_page); 532 __free_page(enc_extent_page);
533 } 533 }
534 return rc; 534 return rc;
535 } 535 }
536 536
537 static int ecryptfs_decrypt_extent(struct page *page, 537 static int ecryptfs_decrypt_extent(struct page *page,
538 struct ecryptfs_crypt_stat *crypt_stat, 538 struct ecryptfs_crypt_stat *crypt_stat,
539 struct page *enc_extent_page, 539 struct page *enc_extent_page,
540 unsigned long extent_offset) 540 unsigned long extent_offset)
541 { 541 {
542 loff_t extent_base; 542 loff_t extent_base;
543 char extent_iv[ECRYPTFS_MAX_IV_BYTES]; 543 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
544 int rc; 544 int rc;
545 545
546 extent_base = (((loff_t)page->index) 546 extent_base = (((loff_t)page->index)
547 * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); 547 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
548 rc = ecryptfs_derive_iv(extent_iv, crypt_stat, 548 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
549 (extent_base + extent_offset)); 549 (extent_base + extent_offset));
550 if (rc) { 550 if (rc) {
551 ecryptfs_printk(KERN_ERR, "Error attempting to " 551 ecryptfs_printk(KERN_ERR, "Error attempting to "
552 "derive IV for extent [0x%.16x]; " 552 "derive IV for extent [0x%.16x]; "
553 "rc = [%d]\n", (extent_base + extent_offset), 553 "rc = [%d]\n", (extent_base + extent_offset),
554 rc); 554 rc);
555 goto out; 555 goto out;
556 } 556 }
557 if (unlikely(ecryptfs_verbosity > 0)) { 557 if (unlikely(ecryptfs_verbosity > 0)) {
558 ecryptfs_printk(KERN_DEBUG, "Decrypting extent " 558 ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
559 "with iv:\n"); 559 "with iv:\n");
560 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes); 560 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
561 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before " 561 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
562 "decryption:\n"); 562 "decryption:\n");
563 ecryptfs_dump_hex((char *) 563 ecryptfs_dump_hex((char *)
564 (page_address(enc_extent_page) 564 (page_address(enc_extent_page)
565 + (extent_offset * crypt_stat->extent_size)), 565 + (extent_offset * crypt_stat->extent_size)),
566 8); 566 8);
567 } 567 }
568 rc = ecryptfs_decrypt_page_offset(crypt_stat, page, 568 rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
569 (extent_offset 569 (extent_offset
570 * crypt_stat->extent_size), 570 * crypt_stat->extent_size),
571 enc_extent_page, 0, 571 enc_extent_page, 0,
572 crypt_stat->extent_size, extent_iv); 572 crypt_stat->extent_size, extent_iv);
573 if (rc < 0) { 573 if (rc < 0) {
574 printk(KERN_ERR "%s: Error attempting to decrypt to page with " 574 printk(KERN_ERR "%s: Error attempting to decrypt to page with "
575 "page->index = [%ld], extent_offset = [%ld]; " 575 "page->index = [%ld], extent_offset = [%ld]; "
576 "rc = [%d]\n", __func__, page->index, extent_offset, 576 "rc = [%d]\n", __func__, page->index, extent_offset,
577 rc); 577 rc);
578 goto out; 578 goto out;
579 } 579 }
580 rc = 0; 580 rc = 0;
581 if (unlikely(ecryptfs_verbosity > 0)) { 581 if (unlikely(ecryptfs_verbosity > 0)) {
582 ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; " 582 ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; "
583 "rc = [%d]\n", (extent_base + extent_offset), 583 "rc = [%d]\n", (extent_base + extent_offset),
584 rc); 584 rc);
585 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after " 585 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
586 "decryption:\n"); 586 "decryption:\n");
587 ecryptfs_dump_hex((char *)(page_address(page) 587 ecryptfs_dump_hex((char *)(page_address(page)
588 + (extent_offset 588 + (extent_offset
589 * crypt_stat->extent_size)), 8); 589 * crypt_stat->extent_size)), 8);
590 } 590 }
591 out: 591 out:
592 return rc; 592 return rc;
593 } 593 }
594 594
595 /** 595 /**
596 * ecryptfs_decrypt_page 596 * ecryptfs_decrypt_page
597 * @page: Page mapped from the eCryptfs inode for the file; data read 597 * @page: Page mapped from the eCryptfs inode for the file; data read
598 * and decrypted from the lower file will be written into this 598 * and decrypted from the lower file will be written into this
599 * page 599 * page
600 * 600 *
601 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note 601 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
602 * that eCryptfs pages may straddle the lower pages -- for instance, 602 * that eCryptfs pages may straddle the lower pages -- for instance,
603 * if the file was created on a machine with an 8K page size 603 * if the file was created on a machine with an 8K page size
604 * (resulting in an 8K header), and then the file is copied onto a 604 * (resulting in an 8K header), and then the file is copied onto a
605 * host with a 32K page size, then when reading page 0 of the eCryptfs 605 * host with a 32K page size, then when reading page 0 of the eCryptfs
606 * file, 24K of page 0 of the lower file will be read and decrypted, 606 * file, 24K of page 0 of the lower file will be read and decrypted,
607 * and then 8K of page 1 of the lower file will be read and decrypted. 607 * and then 8K of page 1 of the lower file will be read and decrypted.
608 * 608 *
609 * Returns zero on success; negative on error 609 * Returns zero on success; negative on error
610 */ 610 */
611 int ecryptfs_decrypt_page(struct page *page) 611 int ecryptfs_decrypt_page(struct page *page)
612 { 612 {
613 struct inode *ecryptfs_inode; 613 struct inode *ecryptfs_inode;
614 struct ecryptfs_crypt_stat *crypt_stat; 614 struct ecryptfs_crypt_stat *crypt_stat;
615 char *enc_extent_virt; 615 char *enc_extent_virt;
616 struct page *enc_extent_page = NULL; 616 struct page *enc_extent_page = NULL;
617 unsigned long extent_offset; 617 unsigned long extent_offset;
618 int rc = 0; 618 int rc = 0;
619 619
620 ecryptfs_inode = page->mapping->host; 620 ecryptfs_inode = page->mapping->host;
621 crypt_stat = 621 crypt_stat =
622 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); 622 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
623 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 623 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
624 rc = ecryptfs_read_lower_page_segment(page, page->index, 0, 624 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
625 PAGE_CACHE_SIZE, 625 PAGE_CACHE_SIZE,
626 ecryptfs_inode); 626 ecryptfs_inode);
627 if (rc) 627 if (rc)
628 printk(KERN_ERR "%s: Error attempting to copy " 628 printk(KERN_ERR "%s: Error attempting to copy "
629 "page at index [%ld]\n", __func__, 629 "page at index [%ld]\n", __func__,
630 page->index); 630 page->index);
631 goto out; 631 goto out;
632 } 632 }
633 enc_extent_page = alloc_page(GFP_USER); 633 enc_extent_page = alloc_page(GFP_USER);
634 if (!enc_extent_page) { 634 if (!enc_extent_page) {
635 rc = -ENOMEM; 635 rc = -ENOMEM;
636 ecryptfs_printk(KERN_ERR, "Error allocating memory for " 636 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
637 "encrypted extent\n"); 637 "encrypted extent\n");
638 goto out; 638 goto out;
639 } 639 }
640 enc_extent_virt = kmap(enc_extent_page); 640 enc_extent_virt = kmap(enc_extent_page);
641 for (extent_offset = 0; 641 for (extent_offset = 0;
642 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); 642 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
643 extent_offset++) { 643 extent_offset++) {
644 loff_t offset; 644 loff_t offset;
645 645
646 ecryptfs_lower_offset_for_extent( 646 ecryptfs_lower_offset_for_extent(
647 &offset, ((page->index * (PAGE_CACHE_SIZE 647 &offset, ((page->index * (PAGE_CACHE_SIZE
648 / crypt_stat->extent_size)) 648 / crypt_stat->extent_size))
649 + extent_offset), crypt_stat); 649 + extent_offset), crypt_stat);
650 rc = ecryptfs_read_lower(enc_extent_virt, offset, 650 rc = ecryptfs_read_lower(enc_extent_virt, offset,
651 crypt_stat->extent_size, 651 crypt_stat->extent_size,
652 ecryptfs_inode); 652 ecryptfs_inode);
653 if (rc) { 653 if (rc) {
654 ecryptfs_printk(KERN_ERR, "Error attempting " 654 ecryptfs_printk(KERN_ERR, "Error attempting "
655 "to read lower page; rc = [%d]" 655 "to read lower page; rc = [%d]"
656 "\n", rc); 656 "\n", rc);
657 goto out; 657 goto out;
658 } 658 }
659 rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page, 659 rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
660 extent_offset); 660 extent_offset);
661 if (rc) { 661 if (rc) {
662 printk(KERN_ERR "%s: Error encrypting extent; " 662 printk(KERN_ERR "%s: Error encrypting extent; "
663 "rc = [%d]\n", __func__, rc); 663 "rc = [%d]\n", __func__, rc);
664 goto out; 664 goto out;
665 } 665 }
666 } 666 }
667 out: 667 out:
668 if (enc_extent_page) { 668 if (enc_extent_page) {
669 kunmap(enc_extent_page); 669 kunmap(enc_extent_page);
670 __free_page(enc_extent_page); 670 __free_page(enc_extent_page);
671 } 671 }
672 return rc; 672 return rc;
673 } 673 }
674 674
675 /** 675 /**
676 * decrypt_scatterlist 676 * decrypt_scatterlist
677 * @crypt_stat: Cryptographic context 677 * @crypt_stat: Cryptographic context
678 * @dest_sg: The destination scatterlist to decrypt into 678 * @dest_sg: The destination scatterlist to decrypt into
679 * @src_sg: The source scatterlist to decrypt from 679 * @src_sg: The source scatterlist to decrypt from
680 * @size: The number of bytes to decrypt 680 * @size: The number of bytes to decrypt
681 * @iv: The initialization vector to use for the decryption 681 * @iv: The initialization vector to use for the decryption
682 * 682 *
683 * Returns the number of bytes decrypted; negative value on error 683 * Returns the number of bytes decrypted; negative value on error
684 */ 684 */
685 static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat, 685 static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
686 struct scatterlist *dest_sg, 686 struct scatterlist *dest_sg,
687 struct scatterlist *src_sg, int size, 687 struct scatterlist *src_sg, int size,
688 unsigned char *iv) 688 unsigned char *iv)
689 { 689 {
690 struct blkcipher_desc desc = { 690 struct blkcipher_desc desc = {
691 .tfm = crypt_stat->tfm, 691 .tfm = crypt_stat->tfm,
692 .info = iv, 692 .info = iv,
693 .flags = CRYPTO_TFM_REQ_MAY_SLEEP 693 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
694 }; 694 };
695 int rc = 0; 695 int rc = 0;
696 696
697 /* Consider doing this once, when the file is opened */ 697 /* Consider doing this once, when the file is opened */
698 mutex_lock(&crypt_stat->cs_tfm_mutex); 698 mutex_lock(&crypt_stat->cs_tfm_mutex);
699 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key, 699 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
700 crypt_stat->key_size); 700 crypt_stat->key_size);
701 if (rc) { 701 if (rc) {
702 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n", 702 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
703 rc); 703 rc);
704 mutex_unlock(&crypt_stat->cs_tfm_mutex); 704 mutex_unlock(&crypt_stat->cs_tfm_mutex);
705 rc = -EINVAL; 705 rc = -EINVAL;
706 goto out; 706 goto out;
707 } 707 }
708 ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size); 708 ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
709 rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size); 709 rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
710 mutex_unlock(&crypt_stat->cs_tfm_mutex); 710 mutex_unlock(&crypt_stat->cs_tfm_mutex);
711 if (rc) { 711 if (rc) {
712 ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n", 712 ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
713 rc); 713 rc);
714 goto out; 714 goto out;
715 } 715 }
716 rc = size; 716 rc = size;
717 out: 717 out:
718 return rc; 718 return rc;
719 } 719 }
720 720
721 /** 721 /**
722 * ecryptfs_encrypt_page_offset 722 * ecryptfs_encrypt_page_offset
723 * @crypt_stat: The cryptographic context 723 * @crypt_stat: The cryptographic context
724 * @dst_page: The page to encrypt into 724 * @dst_page: The page to encrypt into
725 * @dst_offset: The offset in the page to encrypt into 725 * @dst_offset: The offset in the page to encrypt into
726 * @src_page: The page to encrypt from 726 * @src_page: The page to encrypt from
727 * @src_offset: The offset in the page to encrypt from 727 * @src_offset: The offset in the page to encrypt from
728 * @size: The number of bytes to encrypt 728 * @size: The number of bytes to encrypt
729 * @iv: The initialization vector to use for the encryption 729 * @iv: The initialization vector to use for the encryption
730 * 730 *
731 * Returns the number of bytes encrypted 731 * Returns the number of bytes encrypted
732 */ 732 */
733 static int 733 static int
734 ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, 734 ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
735 struct page *dst_page, int dst_offset, 735 struct page *dst_page, int dst_offset,
736 struct page *src_page, int src_offset, int size, 736 struct page *src_page, int src_offset, int size,
737 unsigned char *iv) 737 unsigned char *iv)
738 { 738 {
739 struct scatterlist src_sg, dst_sg; 739 struct scatterlist src_sg, dst_sg;
740 740
741 sg_init_table(&src_sg, 1); 741 sg_init_table(&src_sg, 1);
742 sg_init_table(&dst_sg, 1); 742 sg_init_table(&dst_sg, 1);
743 743
744 sg_set_page(&src_sg, src_page, size, src_offset); 744 sg_set_page(&src_sg, src_page, size, src_offset);
745 sg_set_page(&dst_sg, dst_page, size, dst_offset); 745 sg_set_page(&dst_sg, dst_page, size, dst_offset);
746 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv); 746 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
747 } 747 }
748 748
749 /** 749 /**
750 * ecryptfs_decrypt_page_offset 750 * ecryptfs_decrypt_page_offset
751 * @crypt_stat: The cryptographic context 751 * @crypt_stat: The cryptographic context
752 * @dst_page: The page to decrypt into 752 * @dst_page: The page to decrypt into
753 * @dst_offset: The offset in the page to decrypt into 753 * @dst_offset: The offset in the page to decrypt into
754 * @src_page: The page to decrypt from 754 * @src_page: The page to decrypt from
755 * @src_offset: The offset in the page to decrypt from 755 * @src_offset: The offset in the page to decrypt from
756 * @size: The number of bytes to decrypt 756 * @size: The number of bytes to decrypt
757 * @iv: The initialization vector to use for the decryption 757 * @iv: The initialization vector to use for the decryption
758 * 758 *
759 * Returns the number of bytes decrypted 759 * Returns the number of bytes decrypted
760 */ 760 */
761 static int 761 static int
762 ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat, 762 ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
763 struct page *dst_page, int dst_offset, 763 struct page *dst_page, int dst_offset,
764 struct page *src_page, int src_offset, int size, 764 struct page *src_page, int src_offset, int size,
765 unsigned char *iv) 765 unsigned char *iv)
766 { 766 {
767 struct scatterlist src_sg, dst_sg; 767 struct scatterlist src_sg, dst_sg;
768 768
769 sg_init_table(&src_sg, 1); 769 sg_init_table(&src_sg, 1);
770 sg_set_page(&src_sg, src_page, size, src_offset); 770 sg_set_page(&src_sg, src_page, size, src_offset);
771 771
772 sg_init_table(&dst_sg, 1); 772 sg_init_table(&dst_sg, 1);
773 sg_set_page(&dst_sg, dst_page, size, dst_offset); 773 sg_set_page(&dst_sg, dst_page, size, dst_offset);
774 774
775 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv); 775 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
776 } 776 }
777 777
778 #define ECRYPTFS_MAX_SCATTERLIST_LEN 4 778 #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
779 779
780 /** 780 /**
781 * ecryptfs_init_crypt_ctx 781 * ecryptfs_init_crypt_ctx
782 * @crypt_stat: Uninitilized crypt stats structure 782 * @crypt_stat: Uninitilized crypt stats structure
783 * 783 *
784 * Initialize the crypto context. 784 * Initialize the crypto context.
785 * 785 *
786 * TODO: Performance: Keep a cache of initialized cipher contexts; 786 * TODO: Performance: Keep a cache of initialized cipher contexts;
787 * only init if needed 787 * only init if needed
788 */ 788 */
789 int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat) 789 int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
790 { 790 {
791 char *full_alg_name; 791 char *full_alg_name;
792 int rc = -EINVAL; 792 int rc = -EINVAL;
793 793
794 if (!crypt_stat->cipher) { 794 if (!crypt_stat->cipher) {
795 ecryptfs_printk(KERN_ERR, "No cipher specified\n"); 795 ecryptfs_printk(KERN_ERR, "No cipher specified\n");
796 goto out; 796 goto out;
797 } 797 }
798 ecryptfs_printk(KERN_DEBUG, 798 ecryptfs_printk(KERN_DEBUG,
799 "Initializing cipher [%s]; strlen = [%d]; " 799 "Initializing cipher [%s]; strlen = [%d]; "
800 "key_size_bits = [%d]\n", 800 "key_size_bits = [%d]\n",
801 crypt_stat->cipher, (int)strlen(crypt_stat->cipher), 801 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
802 crypt_stat->key_size << 3); 802 crypt_stat->key_size << 3);
803 if (crypt_stat->tfm) { 803 if (crypt_stat->tfm) {
804 rc = 0; 804 rc = 0;
805 goto out; 805 goto out;
806 } 806 }
807 mutex_lock(&crypt_stat->cs_tfm_mutex); 807 mutex_lock(&crypt_stat->cs_tfm_mutex);
808 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, 808 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
809 crypt_stat->cipher, "cbc"); 809 crypt_stat->cipher, "cbc");
810 if (rc) 810 if (rc)
811 goto out_unlock; 811 goto out_unlock;
812 crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0, 812 crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
813 CRYPTO_ALG_ASYNC); 813 CRYPTO_ALG_ASYNC);
814 kfree(full_alg_name); 814 kfree(full_alg_name);
815 if (IS_ERR(crypt_stat->tfm)) { 815 if (IS_ERR(crypt_stat->tfm)) {
816 rc = PTR_ERR(crypt_stat->tfm); 816 rc = PTR_ERR(crypt_stat->tfm);
817 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): " 817 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
818 "Error initializing cipher [%s]\n", 818 "Error initializing cipher [%s]\n",
819 crypt_stat->cipher); 819 crypt_stat->cipher);
820 goto out_unlock; 820 goto out_unlock;
821 } 821 }
822 crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY); 822 crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
823 rc = 0; 823 rc = 0;
824 out_unlock: 824 out_unlock:
825 mutex_unlock(&crypt_stat->cs_tfm_mutex); 825 mutex_unlock(&crypt_stat->cs_tfm_mutex);
826 out: 826 out:
827 return rc; 827 return rc;
828 } 828 }
829 829
830 static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat) 830 static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
831 { 831 {
832 int extent_size_tmp; 832 int extent_size_tmp;
833 833
834 crypt_stat->extent_mask = 0xFFFFFFFF; 834 crypt_stat->extent_mask = 0xFFFFFFFF;
835 crypt_stat->extent_shift = 0; 835 crypt_stat->extent_shift = 0;
836 if (crypt_stat->extent_size == 0) 836 if (crypt_stat->extent_size == 0)
837 return; 837 return;
838 extent_size_tmp = crypt_stat->extent_size; 838 extent_size_tmp = crypt_stat->extent_size;
839 while ((extent_size_tmp & 0x01) == 0) { 839 while ((extent_size_tmp & 0x01) == 0) {
840 extent_size_tmp >>= 1; 840 extent_size_tmp >>= 1;
841 crypt_stat->extent_mask <<= 1; 841 crypt_stat->extent_mask <<= 1;
842 crypt_stat->extent_shift++; 842 crypt_stat->extent_shift++;
843 } 843 }
844 } 844 }
845 845
846 void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat) 846 void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
847 { 847 {
848 /* Default values; may be overwritten as we are parsing the 848 /* Default values; may be overwritten as we are parsing the
849 * packets. */ 849 * packets. */
850 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE; 850 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
851 set_extent_mask_and_shift(crypt_stat); 851 set_extent_mask_and_shift(crypt_stat);
852 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES; 852 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
853 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 853 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
854 crypt_stat->num_header_bytes_at_front = 0; 854 crypt_stat->num_header_bytes_at_front = 0;
855 else { 855 else {
856 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) 856 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
857 crypt_stat->num_header_bytes_at_front = 857 crypt_stat->num_header_bytes_at_front =
858 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; 858 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
859 else 859 else
860 crypt_stat->num_header_bytes_at_front = PAGE_CACHE_SIZE; 860 crypt_stat->num_header_bytes_at_front = PAGE_CACHE_SIZE;
861 } 861 }
862 } 862 }
863 863
864 /** 864 /**
865 * ecryptfs_compute_root_iv 865 * ecryptfs_compute_root_iv
866 * @crypt_stats 866 * @crypt_stats
867 * 867 *
868 * On error, sets the root IV to all 0's. 868 * On error, sets the root IV to all 0's.
869 */ 869 */
870 int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat) 870 int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
871 { 871 {
872 int rc = 0; 872 int rc = 0;
873 char dst[MD5_DIGEST_SIZE]; 873 char dst[MD5_DIGEST_SIZE];
874 874
875 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE); 875 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
876 BUG_ON(crypt_stat->iv_bytes <= 0); 876 BUG_ON(crypt_stat->iv_bytes <= 0);
877 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { 877 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
878 rc = -EINVAL; 878 rc = -EINVAL;
879 ecryptfs_printk(KERN_WARNING, "Session key not valid; " 879 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
880 "cannot generate root IV\n"); 880 "cannot generate root IV\n");
881 goto out; 881 goto out;
882 } 882 }
883 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key, 883 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
884 crypt_stat->key_size); 884 crypt_stat->key_size);
885 if (rc) { 885 if (rc) {
886 ecryptfs_printk(KERN_WARNING, "Error attempting to compute " 886 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
887 "MD5 while generating root IV\n"); 887 "MD5 while generating root IV\n");
888 goto out; 888 goto out;
889 } 889 }
890 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes); 890 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
891 out: 891 out:
892 if (rc) { 892 if (rc) {
893 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes); 893 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
894 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING; 894 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
895 } 895 }
896 return rc; 896 return rc;
897 } 897 }
898 898
899 static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat) 899 static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
900 { 900 {
901 get_random_bytes(crypt_stat->key, crypt_stat->key_size); 901 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
902 crypt_stat->flags |= ECRYPTFS_KEY_VALID; 902 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
903 ecryptfs_compute_root_iv(crypt_stat); 903 ecryptfs_compute_root_iv(crypt_stat);
904 if (unlikely(ecryptfs_verbosity > 0)) { 904 if (unlikely(ecryptfs_verbosity > 0)) {
905 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n"); 905 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
906 ecryptfs_dump_hex(crypt_stat->key, 906 ecryptfs_dump_hex(crypt_stat->key,
907 crypt_stat->key_size); 907 crypt_stat->key_size);
908 } 908 }
909 } 909 }
910 910
911 /** 911 /**
912 * ecryptfs_copy_mount_wide_flags_to_inode_flags 912 * ecryptfs_copy_mount_wide_flags_to_inode_flags
913 * @crypt_stat: The inode's cryptographic context 913 * @crypt_stat: The inode's cryptographic context
914 * @mount_crypt_stat: The mount point's cryptographic context 914 * @mount_crypt_stat: The mount point's cryptographic context
915 * 915 *
916 * This function propagates the mount-wide flags to individual inode 916 * This function propagates the mount-wide flags to individual inode
917 * flags. 917 * flags.
918 */ 918 */
919 static void ecryptfs_copy_mount_wide_flags_to_inode_flags( 919 static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
920 struct ecryptfs_crypt_stat *crypt_stat, 920 struct ecryptfs_crypt_stat *crypt_stat,
921 struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 921 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
922 { 922 {
923 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) 923 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
924 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; 924 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
925 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) 925 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
926 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED; 926 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
927 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) { 927 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
928 crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES; 928 crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
929 if (mount_crypt_stat->flags 929 if (mount_crypt_stat->flags
930 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK) 930 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
931 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK; 931 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
932 else if (mount_crypt_stat->flags 932 else if (mount_crypt_stat->flags
933 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK) 933 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
934 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK; 934 crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
935 } 935 }
936 } 936 }
937 937
938 static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs( 938 static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
939 struct ecryptfs_crypt_stat *crypt_stat, 939 struct ecryptfs_crypt_stat *crypt_stat,
940 struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 940 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
941 { 941 {
942 struct ecryptfs_global_auth_tok *global_auth_tok; 942 struct ecryptfs_global_auth_tok *global_auth_tok;
943 int rc = 0; 943 int rc = 0;
944 944
945 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 945 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
946 list_for_each_entry(global_auth_tok, 946 list_for_each_entry(global_auth_tok,
947 &mount_crypt_stat->global_auth_tok_list, 947 &mount_crypt_stat->global_auth_tok_list,
948 mount_crypt_stat_list) { 948 mount_crypt_stat_list) {
949 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
950 continue;
949 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig); 951 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
950 if (rc) { 952 if (rc) {
951 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc); 953 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
952 mutex_unlock( 954 mutex_unlock(
953 &mount_crypt_stat->global_auth_tok_list_mutex); 955 &mount_crypt_stat->global_auth_tok_list_mutex);
954 goto out; 956 goto out;
955 } 957 }
956 } 958 }
957 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 959 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
958 out: 960 out:
959 return rc; 961 return rc;
960 } 962 }
961 963
962 /** 964 /**
963 * ecryptfs_set_default_crypt_stat_vals 965 * ecryptfs_set_default_crypt_stat_vals
964 * @crypt_stat: The inode's cryptographic context 966 * @crypt_stat: The inode's cryptographic context
965 * @mount_crypt_stat: The mount point's cryptographic context 967 * @mount_crypt_stat: The mount point's cryptographic context
966 * 968 *
967 * Default values in the event that policy does not override them. 969 * Default values in the event that policy does not override them.
968 */ 970 */
969 static void ecryptfs_set_default_crypt_stat_vals( 971 static void ecryptfs_set_default_crypt_stat_vals(
970 struct ecryptfs_crypt_stat *crypt_stat, 972 struct ecryptfs_crypt_stat *crypt_stat,
971 struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 973 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
972 { 974 {
973 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, 975 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
974 mount_crypt_stat); 976 mount_crypt_stat);
975 ecryptfs_set_default_sizes(crypt_stat); 977 ecryptfs_set_default_sizes(crypt_stat);
976 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER); 978 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
977 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES; 979 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
978 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID); 980 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
979 crypt_stat->file_version = ECRYPTFS_FILE_VERSION; 981 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
980 crypt_stat->mount_crypt_stat = mount_crypt_stat; 982 crypt_stat->mount_crypt_stat = mount_crypt_stat;
981 } 983 }
982 984
983 /** 985 /**
984 * ecryptfs_new_file_context 986 * ecryptfs_new_file_context
985 * @ecryptfs_dentry: The eCryptfs dentry 987 * @ecryptfs_dentry: The eCryptfs dentry
986 * 988 *
987 * If the crypto context for the file has not yet been established, 989 * If the crypto context for the file has not yet been established,
988 * this is where we do that. Establishing a new crypto context 990 * this is where we do that. Establishing a new crypto context
989 * involves the following decisions: 991 * involves the following decisions:
990 * - What cipher to use? 992 * - What cipher to use?
991 * - What set of authentication tokens to use? 993 * - What set of authentication tokens to use?
992 * Here we just worry about getting enough information into the 994 * Here we just worry about getting enough information into the
993 * authentication tokens so that we know that they are available. 995 * authentication tokens so that we know that they are available.
994 * We associate the available authentication tokens with the new file 996 * We associate the available authentication tokens with the new file
995 * via the set of signatures in the crypt_stat struct. Later, when 997 * via the set of signatures in the crypt_stat struct. Later, when
996 * the headers are actually written out, we may again defer to 998 * the headers are actually written out, we may again defer to
997 * userspace to perform the encryption of the session key; for the 999 * userspace to perform the encryption of the session key; for the
998 * foreseeable future, this will be the case with public key packets. 1000 * foreseeable future, this will be the case with public key packets.
999 * 1001 *
1000 * Returns zero on success; non-zero otherwise 1002 * Returns zero on success; non-zero otherwise
1001 */ 1003 */
1002 int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry) 1004 int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
1003 { 1005 {
1004 struct ecryptfs_crypt_stat *crypt_stat = 1006 struct ecryptfs_crypt_stat *crypt_stat =
1005 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat; 1007 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
1006 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 1008 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1007 &ecryptfs_superblock_to_private( 1009 &ecryptfs_superblock_to_private(
1008 ecryptfs_dentry->d_sb)->mount_crypt_stat; 1010 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1009 int cipher_name_len; 1011 int cipher_name_len;
1010 int rc = 0; 1012 int rc = 0;
1011 1013
1012 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat); 1014 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
1013 crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID); 1015 crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
1014 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, 1016 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1015 mount_crypt_stat); 1017 mount_crypt_stat);
1016 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat, 1018 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
1017 mount_crypt_stat); 1019 mount_crypt_stat);
1018 if (rc) { 1020 if (rc) {
1019 printk(KERN_ERR "Error attempting to copy mount-wide key sigs " 1021 printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
1020 "to the inode key sigs; rc = [%d]\n", rc); 1022 "to the inode key sigs; rc = [%d]\n", rc);
1021 goto out; 1023 goto out;
1022 } 1024 }
1023 cipher_name_len = 1025 cipher_name_len =
1024 strlen(mount_crypt_stat->global_default_cipher_name); 1026 strlen(mount_crypt_stat->global_default_cipher_name);
1025 memcpy(crypt_stat->cipher, 1027 memcpy(crypt_stat->cipher,
1026 mount_crypt_stat->global_default_cipher_name, 1028 mount_crypt_stat->global_default_cipher_name,
1027 cipher_name_len); 1029 cipher_name_len);
1028 crypt_stat->cipher[cipher_name_len] = '\0'; 1030 crypt_stat->cipher[cipher_name_len] = '\0';
1029 crypt_stat->key_size = 1031 crypt_stat->key_size =
1030 mount_crypt_stat->global_default_cipher_key_size; 1032 mount_crypt_stat->global_default_cipher_key_size;
1031 ecryptfs_generate_new_key(crypt_stat); 1033 ecryptfs_generate_new_key(crypt_stat);
1032 rc = ecryptfs_init_crypt_ctx(crypt_stat); 1034 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1033 if (rc) 1035 if (rc)
1034 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic " 1036 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1035 "context for cipher [%s]: rc = [%d]\n", 1037 "context for cipher [%s]: rc = [%d]\n",
1036 crypt_stat->cipher, rc); 1038 crypt_stat->cipher, rc);
1037 out: 1039 out:
1038 return rc; 1040 return rc;
1039 } 1041 }
1040 1042
1041 /** 1043 /**
1042 * contains_ecryptfs_marker - check for the ecryptfs marker 1044 * contains_ecryptfs_marker - check for the ecryptfs marker
1043 * @data: The data block in which to check 1045 * @data: The data block in which to check
1044 * 1046 *
1045 * Returns one if marker found; zero if not found 1047 * Returns one if marker found; zero if not found
1046 */ 1048 */
1047 static int contains_ecryptfs_marker(char *data) 1049 static int contains_ecryptfs_marker(char *data)
1048 { 1050 {
1049 u32 m_1, m_2; 1051 u32 m_1, m_2;
1050 1052
1051 m_1 = get_unaligned_be32(data); 1053 m_1 = get_unaligned_be32(data);
1052 m_2 = get_unaligned_be32(data + 4); 1054 m_2 = get_unaligned_be32(data + 4);
1053 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2) 1055 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
1054 return 1; 1056 return 1;
1055 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; " 1057 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1056 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2, 1058 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1057 MAGIC_ECRYPTFS_MARKER); 1059 MAGIC_ECRYPTFS_MARKER);
1058 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = " 1060 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1059 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER)); 1061 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1060 return 0; 1062 return 0;
1061 } 1063 }
1062 1064
1063 struct ecryptfs_flag_map_elem { 1065 struct ecryptfs_flag_map_elem {
1064 u32 file_flag; 1066 u32 file_flag;
1065 u32 local_flag; 1067 u32 local_flag;
1066 }; 1068 };
1067 1069
1068 /* Add support for additional flags by adding elements here. */ 1070 /* Add support for additional flags by adding elements here. */
1069 static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = { 1071 static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1070 {0x00000001, ECRYPTFS_ENABLE_HMAC}, 1072 {0x00000001, ECRYPTFS_ENABLE_HMAC},
1071 {0x00000002, ECRYPTFS_ENCRYPTED}, 1073 {0x00000002, ECRYPTFS_ENCRYPTED},
1072 {0x00000004, ECRYPTFS_METADATA_IN_XATTR}, 1074 {0x00000004, ECRYPTFS_METADATA_IN_XATTR},
1073 {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES} 1075 {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
1074 }; 1076 };
1075 1077
1076 /** 1078 /**
1077 * ecryptfs_process_flags 1079 * ecryptfs_process_flags
1078 * @crypt_stat: The cryptographic context 1080 * @crypt_stat: The cryptographic context
1079 * @page_virt: Source data to be parsed 1081 * @page_virt: Source data to be parsed
1080 * @bytes_read: Updated with the number of bytes read 1082 * @bytes_read: Updated with the number of bytes read
1081 * 1083 *
1082 * Returns zero on success; non-zero if the flag set is invalid 1084 * Returns zero on success; non-zero if the flag set is invalid
1083 */ 1085 */
1084 static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat, 1086 static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1085 char *page_virt, int *bytes_read) 1087 char *page_virt, int *bytes_read)
1086 { 1088 {
1087 int rc = 0; 1089 int rc = 0;
1088 int i; 1090 int i;
1089 u32 flags; 1091 u32 flags;
1090 1092
1091 flags = get_unaligned_be32(page_virt); 1093 flags = get_unaligned_be32(page_virt);
1092 for (i = 0; i < ((sizeof(ecryptfs_flag_map) 1094 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1093 / sizeof(struct ecryptfs_flag_map_elem))); i++) 1095 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1094 if (flags & ecryptfs_flag_map[i].file_flag) { 1096 if (flags & ecryptfs_flag_map[i].file_flag) {
1095 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag; 1097 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
1096 } else 1098 } else
1097 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag); 1099 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
1098 /* Version is in top 8 bits of the 32-bit flag vector */ 1100 /* Version is in top 8 bits of the 32-bit flag vector */
1099 crypt_stat->file_version = ((flags >> 24) & 0xFF); 1101 crypt_stat->file_version = ((flags >> 24) & 0xFF);
1100 (*bytes_read) = 4; 1102 (*bytes_read) = 4;
1101 return rc; 1103 return rc;
1102 } 1104 }
1103 1105
1104 /** 1106 /**
1105 * write_ecryptfs_marker 1107 * write_ecryptfs_marker
1106 * @page_virt: The pointer to in a page to begin writing the marker 1108 * @page_virt: The pointer to in a page to begin writing the marker
1107 * @written: Number of bytes written 1109 * @written: Number of bytes written
1108 * 1110 *
1109 * Marker = 0x3c81b7f5 1111 * Marker = 0x3c81b7f5
1110 */ 1112 */
1111 static void write_ecryptfs_marker(char *page_virt, size_t *written) 1113 static void write_ecryptfs_marker(char *page_virt, size_t *written)
1112 { 1114 {
1113 u32 m_1, m_2; 1115 u32 m_1, m_2;
1114 1116
1115 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2)); 1117 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1116 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER); 1118 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
1117 put_unaligned_be32(m_1, page_virt); 1119 put_unaligned_be32(m_1, page_virt);
1118 page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2); 1120 page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
1119 put_unaligned_be32(m_2, page_virt); 1121 put_unaligned_be32(m_2, page_virt);
1120 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; 1122 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1121 } 1123 }
1122 1124
1123 static void 1125 static void
1124 write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat, 1126 write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1125 size_t *written) 1127 size_t *written)
1126 { 1128 {
1127 u32 flags = 0; 1129 u32 flags = 0;
1128 int i; 1130 int i;
1129 1131
1130 for (i = 0; i < ((sizeof(ecryptfs_flag_map) 1132 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1131 / sizeof(struct ecryptfs_flag_map_elem))); i++) 1133 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1132 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag) 1134 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
1133 flags |= ecryptfs_flag_map[i].file_flag; 1135 flags |= ecryptfs_flag_map[i].file_flag;
1134 /* Version is in top 8 bits of the 32-bit flag vector */ 1136 /* Version is in top 8 bits of the 32-bit flag vector */
1135 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000); 1137 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
1136 put_unaligned_be32(flags, page_virt); 1138 put_unaligned_be32(flags, page_virt);
1137 (*written) = 4; 1139 (*written) = 4;
1138 } 1140 }
1139 1141
1140 struct ecryptfs_cipher_code_str_map_elem { 1142 struct ecryptfs_cipher_code_str_map_elem {
1141 char cipher_str[16]; 1143 char cipher_str[16];
1142 u8 cipher_code; 1144 u8 cipher_code;
1143 }; 1145 };
1144 1146
1145 /* Add support for additional ciphers by adding elements here. The 1147 /* Add support for additional ciphers by adding elements here. The
1146 * cipher_code is whatever OpenPGP applicatoins use to identify the 1148 * cipher_code is whatever OpenPGP applicatoins use to identify the
1147 * ciphers. List in order of probability. */ 1149 * ciphers. List in order of probability. */
1148 static struct ecryptfs_cipher_code_str_map_elem 1150 static struct ecryptfs_cipher_code_str_map_elem
1149 ecryptfs_cipher_code_str_map[] = { 1151 ecryptfs_cipher_code_str_map[] = {
1150 {"aes",RFC2440_CIPHER_AES_128 }, 1152 {"aes",RFC2440_CIPHER_AES_128 },
1151 {"blowfish", RFC2440_CIPHER_BLOWFISH}, 1153 {"blowfish", RFC2440_CIPHER_BLOWFISH},
1152 {"des3_ede", RFC2440_CIPHER_DES3_EDE}, 1154 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
1153 {"cast5", RFC2440_CIPHER_CAST_5}, 1155 {"cast5", RFC2440_CIPHER_CAST_5},
1154 {"twofish", RFC2440_CIPHER_TWOFISH}, 1156 {"twofish", RFC2440_CIPHER_TWOFISH},
1155 {"cast6", RFC2440_CIPHER_CAST_6}, 1157 {"cast6", RFC2440_CIPHER_CAST_6},
1156 {"aes", RFC2440_CIPHER_AES_192}, 1158 {"aes", RFC2440_CIPHER_AES_192},
1157 {"aes", RFC2440_CIPHER_AES_256} 1159 {"aes", RFC2440_CIPHER_AES_256}
1158 }; 1160 };
1159 1161
1160 /** 1162 /**
1161 * ecryptfs_code_for_cipher_string 1163 * ecryptfs_code_for_cipher_string
1162 * @cipher_name: The string alias for the cipher 1164 * @cipher_name: The string alias for the cipher
1163 * @key_bytes: Length of key in bytes; used for AES code selection 1165 * @key_bytes: Length of key in bytes; used for AES code selection
1164 * 1166 *
1165 * Returns zero on no match, or the cipher code on match 1167 * Returns zero on no match, or the cipher code on match
1166 */ 1168 */
1167 u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes) 1169 u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
1168 { 1170 {
1169 int i; 1171 int i;
1170 u8 code = 0; 1172 u8 code = 0;
1171 struct ecryptfs_cipher_code_str_map_elem *map = 1173 struct ecryptfs_cipher_code_str_map_elem *map =
1172 ecryptfs_cipher_code_str_map; 1174 ecryptfs_cipher_code_str_map;
1173 1175
1174 if (strcmp(cipher_name, "aes") == 0) { 1176 if (strcmp(cipher_name, "aes") == 0) {
1175 switch (key_bytes) { 1177 switch (key_bytes) {
1176 case 16: 1178 case 16:
1177 code = RFC2440_CIPHER_AES_128; 1179 code = RFC2440_CIPHER_AES_128;
1178 break; 1180 break;
1179 case 24: 1181 case 24:
1180 code = RFC2440_CIPHER_AES_192; 1182 code = RFC2440_CIPHER_AES_192;
1181 break; 1183 break;
1182 case 32: 1184 case 32:
1183 code = RFC2440_CIPHER_AES_256; 1185 code = RFC2440_CIPHER_AES_256;
1184 } 1186 }
1185 } else { 1187 } else {
1186 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) 1188 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1187 if (strcmp(cipher_name, map[i].cipher_str) == 0) { 1189 if (strcmp(cipher_name, map[i].cipher_str) == 0) {
1188 code = map[i].cipher_code; 1190 code = map[i].cipher_code;
1189 break; 1191 break;
1190 } 1192 }
1191 } 1193 }
1192 return code; 1194 return code;
1193 } 1195 }
1194 1196
1195 /** 1197 /**
1196 * ecryptfs_cipher_code_to_string 1198 * ecryptfs_cipher_code_to_string
1197 * @str: Destination to write out the cipher name 1199 * @str: Destination to write out the cipher name
1198 * @cipher_code: The code to convert to cipher name string 1200 * @cipher_code: The code to convert to cipher name string
1199 * 1201 *
1200 * Returns zero on success 1202 * Returns zero on success
1201 */ 1203 */
1202 int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code) 1204 int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
1203 { 1205 {
1204 int rc = 0; 1206 int rc = 0;
1205 int i; 1207 int i;
1206 1208
1207 str[0] = '\0'; 1209 str[0] = '\0';
1208 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++) 1210 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1209 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code) 1211 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1210 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str); 1212 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1211 if (str[0] == '\0') { 1213 if (str[0] == '\0') {
1212 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: " 1214 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1213 "[%d]\n", cipher_code); 1215 "[%d]\n", cipher_code);
1214 rc = -EINVAL; 1216 rc = -EINVAL;
1215 } 1217 }
1216 return rc; 1218 return rc;
1217 } 1219 }
1218 1220
1219 int ecryptfs_read_and_validate_header_region(char *data, 1221 int ecryptfs_read_and_validate_header_region(char *data,
1220 struct inode *ecryptfs_inode) 1222 struct inode *ecryptfs_inode)
1221 { 1223 {
1222 struct ecryptfs_crypt_stat *crypt_stat = 1224 struct ecryptfs_crypt_stat *crypt_stat =
1223 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); 1225 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
1224 int rc; 1226 int rc;
1225 1227
1226 if (crypt_stat->extent_size == 0) 1228 if (crypt_stat->extent_size == 0)
1227 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE; 1229 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
1228 rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size, 1230 rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
1229 ecryptfs_inode); 1231 ecryptfs_inode);
1230 if (rc) { 1232 if (rc) {
1231 printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n", 1233 printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
1232 __func__, rc); 1234 __func__, rc);
1233 goto out; 1235 goto out;
1234 } 1236 }
1235 if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) { 1237 if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
1236 rc = -EINVAL; 1238 rc = -EINVAL;
1237 } 1239 }
1238 out: 1240 out:
1239 return rc; 1241 return rc;
1240 } 1242 }
1241 1243
1242 void 1244 void
1243 ecryptfs_write_header_metadata(char *virt, 1245 ecryptfs_write_header_metadata(char *virt,
1244 struct ecryptfs_crypt_stat *crypt_stat, 1246 struct ecryptfs_crypt_stat *crypt_stat,
1245 size_t *written) 1247 size_t *written)
1246 { 1248 {
1247 u32 header_extent_size; 1249 u32 header_extent_size;
1248 u16 num_header_extents_at_front; 1250 u16 num_header_extents_at_front;
1249 1251
1250 header_extent_size = (u32)crypt_stat->extent_size; 1252 header_extent_size = (u32)crypt_stat->extent_size;
1251 num_header_extents_at_front = 1253 num_header_extents_at_front =
1252 (u16)(crypt_stat->num_header_bytes_at_front 1254 (u16)(crypt_stat->num_header_bytes_at_front
1253 / crypt_stat->extent_size); 1255 / crypt_stat->extent_size);
1254 put_unaligned_be32(header_extent_size, virt); 1256 put_unaligned_be32(header_extent_size, virt);
1255 virt += 4; 1257 virt += 4;
1256 put_unaligned_be16(num_header_extents_at_front, virt); 1258 put_unaligned_be16(num_header_extents_at_front, virt);
1257 (*written) = 6; 1259 (*written) = 6;
1258 } 1260 }
1259 1261
1260 struct kmem_cache *ecryptfs_header_cache_1; 1262 struct kmem_cache *ecryptfs_header_cache_1;
1261 struct kmem_cache *ecryptfs_header_cache_2; 1263 struct kmem_cache *ecryptfs_header_cache_2;
1262 1264
1263 /** 1265 /**
1264 * ecryptfs_write_headers_virt 1266 * ecryptfs_write_headers_virt
1265 * @page_virt: The virtual address to write the headers to 1267 * @page_virt: The virtual address to write the headers to
1266 * @max: The size of memory allocated at page_virt 1268 * @max: The size of memory allocated at page_virt
1267 * @size: Set to the number of bytes written by this function 1269 * @size: Set to the number of bytes written by this function
1268 * @crypt_stat: The cryptographic context 1270 * @crypt_stat: The cryptographic context
1269 * @ecryptfs_dentry: The eCryptfs dentry 1271 * @ecryptfs_dentry: The eCryptfs dentry
1270 * 1272 *
1271 * Format version: 1 1273 * Format version: 1
1272 * 1274 *
1273 * Header Extent: 1275 * Header Extent:
1274 * Octets 0-7: Unencrypted file size (big-endian) 1276 * Octets 0-7: Unencrypted file size (big-endian)
1275 * Octets 8-15: eCryptfs special marker 1277 * Octets 8-15: eCryptfs special marker
1276 * Octets 16-19: Flags 1278 * Octets 16-19: Flags
1277 * Octet 16: File format version number (between 0 and 255) 1279 * Octet 16: File format version number (between 0 and 255)
1278 * Octets 17-18: Reserved 1280 * Octets 17-18: Reserved
1279 * Octet 19: Bit 1 (lsb): Reserved 1281 * Octet 19: Bit 1 (lsb): Reserved
1280 * Bit 2: Encrypted? 1282 * Bit 2: Encrypted?
1281 * Bits 3-8: Reserved 1283 * Bits 3-8: Reserved
1282 * Octets 20-23: Header extent size (big-endian) 1284 * Octets 20-23: Header extent size (big-endian)
1283 * Octets 24-25: Number of header extents at front of file 1285 * Octets 24-25: Number of header extents at front of file
1284 * (big-endian) 1286 * (big-endian)
1285 * Octet 26: Begin RFC 2440 authentication token packet set 1287 * Octet 26: Begin RFC 2440 authentication token packet set
1286 * Data Extent 0: 1288 * Data Extent 0:
1287 * Lower data (CBC encrypted) 1289 * Lower data (CBC encrypted)
1288 * Data Extent 1: 1290 * Data Extent 1:
1289 * Lower data (CBC encrypted) 1291 * Lower data (CBC encrypted)
1290 * ... 1292 * ...
1291 * 1293 *
1292 * Returns zero on success 1294 * Returns zero on success
1293 */ 1295 */
1294 static int ecryptfs_write_headers_virt(char *page_virt, size_t max, 1296 static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
1295 size_t *size, 1297 size_t *size,
1296 struct ecryptfs_crypt_stat *crypt_stat, 1298 struct ecryptfs_crypt_stat *crypt_stat,
1297 struct dentry *ecryptfs_dentry) 1299 struct dentry *ecryptfs_dentry)
1298 { 1300 {
1299 int rc; 1301 int rc;
1300 size_t written; 1302 size_t written;
1301 size_t offset; 1303 size_t offset;
1302 1304
1303 offset = ECRYPTFS_FILE_SIZE_BYTES; 1305 offset = ECRYPTFS_FILE_SIZE_BYTES;
1304 write_ecryptfs_marker((page_virt + offset), &written); 1306 write_ecryptfs_marker((page_virt + offset), &written);
1305 offset += written; 1307 offset += written;
1306 write_ecryptfs_flags((page_virt + offset), crypt_stat, &written); 1308 write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1307 offset += written; 1309 offset += written;
1308 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat, 1310 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1309 &written); 1311 &written);
1310 offset += written; 1312 offset += written;
1311 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat, 1313 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1312 ecryptfs_dentry, &written, 1314 ecryptfs_dentry, &written,
1313 max - offset); 1315 max - offset);
1314 if (rc) 1316 if (rc)
1315 ecryptfs_printk(KERN_WARNING, "Error generating key packet " 1317 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1316 "set; rc = [%d]\n", rc); 1318 "set; rc = [%d]\n", rc);
1317 if (size) { 1319 if (size) {
1318 offset += written; 1320 offset += written;
1319 *size = offset; 1321 *size = offset;
1320 } 1322 }
1321 return rc; 1323 return rc;
1322 } 1324 }
1323 1325
1324 static int 1326 static int
1325 ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat, 1327 ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
1326 struct dentry *ecryptfs_dentry, 1328 struct dentry *ecryptfs_dentry,
1327 char *virt) 1329 char *virt)
1328 { 1330 {
1329 int rc; 1331 int rc;
1330 1332
1331 rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, virt, 1333 rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, virt,
1332 0, crypt_stat->num_header_bytes_at_front); 1334 0, crypt_stat->num_header_bytes_at_front);
1333 if (rc) 1335 if (rc)
1334 printk(KERN_ERR "%s: Error attempting to write header " 1336 printk(KERN_ERR "%s: Error attempting to write header "
1335 "information to lower file; rc = [%d]\n", __func__, 1337 "information to lower file; rc = [%d]\n", __func__,
1336 rc); 1338 rc);
1337 return rc; 1339 return rc;
1338 } 1340 }
1339 1341
1340 static int 1342 static int
1341 ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry, 1343 ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1342 struct ecryptfs_crypt_stat *crypt_stat, 1344 struct ecryptfs_crypt_stat *crypt_stat,
1343 char *page_virt, size_t size) 1345 char *page_virt, size_t size)
1344 { 1346 {
1345 int rc; 1347 int rc;
1346 1348
1347 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt, 1349 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1348 size, 0); 1350 size, 0);
1349 return rc; 1351 return rc;
1350 } 1352 }
1351 1353
1352 /** 1354 /**
1353 * ecryptfs_write_metadata 1355 * ecryptfs_write_metadata
1354 * @ecryptfs_dentry: The eCryptfs dentry 1356 * @ecryptfs_dentry: The eCryptfs dentry
1355 * 1357 *
1356 * Write the file headers out. This will likely involve a userspace 1358 * Write the file headers out. This will likely involve a userspace
1357 * callout, in which the session key is encrypted with one or more 1359 * callout, in which the session key is encrypted with one or more
1358 * public keys and/or the passphrase necessary to do the encryption is 1360 * public keys and/or the passphrase necessary to do the encryption is
1359 * retrieved via a prompt. Exactly what happens at this point should 1361 * retrieved via a prompt. Exactly what happens at this point should
1360 * be policy-dependent. 1362 * be policy-dependent.
1361 * 1363 *
1362 * Returns zero on success; non-zero on error 1364 * Returns zero on success; non-zero on error
1363 */ 1365 */
1364 int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry) 1366 int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
1365 { 1367 {
1366 struct ecryptfs_crypt_stat *crypt_stat = 1368 struct ecryptfs_crypt_stat *crypt_stat =
1367 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat; 1369 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
1368 char *virt; 1370 char *virt;
1369 size_t size = 0; 1371 size_t size = 0;
1370 int rc = 0; 1372 int rc = 0;
1371 1373
1372 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 1374 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1373 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) { 1375 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
1374 printk(KERN_ERR "Key is invalid; bailing out\n"); 1376 printk(KERN_ERR "Key is invalid; bailing out\n");
1375 rc = -EINVAL; 1377 rc = -EINVAL;
1376 goto out; 1378 goto out;
1377 } 1379 }
1378 } else { 1380 } else {
1379 printk(KERN_WARNING "%s: Encrypted flag not set\n", 1381 printk(KERN_WARNING "%s: Encrypted flag not set\n",
1380 __func__); 1382 __func__);
1381 rc = -EINVAL; 1383 rc = -EINVAL;
1382 goto out; 1384 goto out;
1383 } 1385 }
1384 /* Released in this function */ 1386 /* Released in this function */
1385 virt = (char *)get_zeroed_page(GFP_KERNEL); 1387 virt = (char *)get_zeroed_page(GFP_KERNEL);
1386 if (!virt) { 1388 if (!virt) {
1387 printk(KERN_ERR "%s: Out of memory\n", __func__); 1389 printk(KERN_ERR "%s: Out of memory\n", __func__);
1388 rc = -ENOMEM; 1390 rc = -ENOMEM;
1389 goto out; 1391 goto out;
1390 } 1392 }
1391 rc = ecryptfs_write_headers_virt(virt, PAGE_CACHE_SIZE, &size, 1393 rc = ecryptfs_write_headers_virt(virt, PAGE_CACHE_SIZE, &size,
1392 crypt_stat, ecryptfs_dentry); 1394 crypt_stat, ecryptfs_dentry);
1393 if (unlikely(rc)) { 1395 if (unlikely(rc)) {
1394 printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n", 1396 printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
1395 __func__, rc); 1397 __func__, rc);
1396 goto out_free; 1398 goto out_free;
1397 } 1399 }
1398 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 1400 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1399 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, 1401 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
1400 crypt_stat, virt, size); 1402 crypt_stat, virt, size);
1401 else 1403 else
1402 rc = ecryptfs_write_metadata_to_contents(crypt_stat, 1404 rc = ecryptfs_write_metadata_to_contents(crypt_stat,
1403 ecryptfs_dentry, virt); 1405 ecryptfs_dentry, virt);
1404 if (rc) { 1406 if (rc) {
1405 printk(KERN_ERR "%s: Error writing metadata out to lower file; " 1407 printk(KERN_ERR "%s: Error writing metadata out to lower file; "
1406 "rc = [%d]\n", __func__, rc); 1408 "rc = [%d]\n", __func__, rc);
1407 goto out_free; 1409 goto out_free;
1408 } 1410 }
1409 out_free: 1411 out_free:
1410 free_page((unsigned long)virt); 1412 free_page((unsigned long)virt);
1411 out: 1413 out:
1412 return rc; 1414 return rc;
1413 } 1415 }
1414 1416
1415 #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0 1417 #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1416 #define ECRYPTFS_VALIDATE_HEADER_SIZE 1 1418 #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
1417 static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat, 1419 static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
1418 char *virt, int *bytes_read, 1420 char *virt, int *bytes_read,
1419 int validate_header_size) 1421 int validate_header_size)
1420 { 1422 {
1421 int rc = 0; 1423 int rc = 0;
1422 u32 header_extent_size; 1424 u32 header_extent_size;
1423 u16 num_header_extents_at_front; 1425 u16 num_header_extents_at_front;
1424 1426
1425 header_extent_size = get_unaligned_be32(virt); 1427 header_extent_size = get_unaligned_be32(virt);
1426 virt += sizeof(__be32); 1428 virt += sizeof(__be32);
1427 num_header_extents_at_front = get_unaligned_be16(virt); 1429 num_header_extents_at_front = get_unaligned_be16(virt);
1428 crypt_stat->num_header_bytes_at_front = 1430 crypt_stat->num_header_bytes_at_front =
1429 (((size_t)num_header_extents_at_front 1431 (((size_t)num_header_extents_at_front
1430 * (size_t)header_extent_size)); 1432 * (size_t)header_extent_size));
1431 (*bytes_read) = (sizeof(__be32) + sizeof(__be16)); 1433 (*bytes_read) = (sizeof(__be32) + sizeof(__be16));
1432 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE) 1434 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
1433 && (crypt_stat->num_header_bytes_at_front 1435 && (crypt_stat->num_header_bytes_at_front
1434 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) { 1436 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
1435 rc = -EINVAL; 1437 rc = -EINVAL;
1436 printk(KERN_WARNING "Invalid header size: [%zd]\n", 1438 printk(KERN_WARNING "Invalid header size: [%zd]\n",
1437 crypt_stat->num_header_bytes_at_front); 1439 crypt_stat->num_header_bytes_at_front);
1438 } 1440 }
1439 return rc; 1441 return rc;
1440 } 1442 }
1441 1443
1442 /** 1444 /**
1443 * set_default_header_data 1445 * set_default_header_data
1444 * @crypt_stat: The cryptographic context 1446 * @crypt_stat: The cryptographic context
1445 * 1447 *
1446 * For version 0 file format; this function is only for backwards 1448 * For version 0 file format; this function is only for backwards
1447 * compatibility for files created with the prior versions of 1449 * compatibility for files created with the prior versions of
1448 * eCryptfs. 1450 * eCryptfs.
1449 */ 1451 */
1450 static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat) 1452 static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1451 { 1453 {
1452 crypt_stat->num_header_bytes_at_front = 1454 crypt_stat->num_header_bytes_at_front =
1453 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; 1455 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
1454 } 1456 }
1455 1457
1456 /** 1458 /**
1457 * ecryptfs_read_headers_virt 1459 * ecryptfs_read_headers_virt
1458 * @page_virt: The virtual address into which to read the headers 1460 * @page_virt: The virtual address into which to read the headers
1459 * @crypt_stat: The cryptographic context 1461 * @crypt_stat: The cryptographic context
1460 * @ecryptfs_dentry: The eCryptfs dentry 1462 * @ecryptfs_dentry: The eCryptfs dentry
1461 * @validate_header_size: Whether to validate the header size while reading 1463 * @validate_header_size: Whether to validate the header size while reading
1462 * 1464 *
1463 * Read/parse the header data. The header format is detailed in the 1465 * Read/parse the header data. The header format is detailed in the
1464 * comment block for the ecryptfs_write_headers_virt() function. 1466 * comment block for the ecryptfs_write_headers_virt() function.
1465 * 1467 *
1466 * Returns zero on success 1468 * Returns zero on success
1467 */ 1469 */
1468 static int ecryptfs_read_headers_virt(char *page_virt, 1470 static int ecryptfs_read_headers_virt(char *page_virt,
1469 struct ecryptfs_crypt_stat *crypt_stat, 1471 struct ecryptfs_crypt_stat *crypt_stat,
1470 struct dentry *ecryptfs_dentry, 1472 struct dentry *ecryptfs_dentry,
1471 int validate_header_size) 1473 int validate_header_size)
1472 { 1474 {
1473 int rc = 0; 1475 int rc = 0;
1474 int offset; 1476 int offset;
1475 int bytes_read; 1477 int bytes_read;
1476 1478
1477 ecryptfs_set_default_sizes(crypt_stat); 1479 ecryptfs_set_default_sizes(crypt_stat);
1478 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private( 1480 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1479 ecryptfs_dentry->d_sb)->mount_crypt_stat; 1481 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1480 offset = ECRYPTFS_FILE_SIZE_BYTES; 1482 offset = ECRYPTFS_FILE_SIZE_BYTES;
1481 rc = contains_ecryptfs_marker(page_virt + offset); 1483 rc = contains_ecryptfs_marker(page_virt + offset);
1482 if (rc == 0) { 1484 if (rc == 0) {
1483 rc = -EINVAL; 1485 rc = -EINVAL;
1484 goto out; 1486 goto out;
1485 } 1487 }
1486 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; 1488 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1487 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset), 1489 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1488 &bytes_read); 1490 &bytes_read);
1489 if (rc) { 1491 if (rc) {
1490 ecryptfs_printk(KERN_WARNING, "Error processing flags\n"); 1492 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1491 goto out; 1493 goto out;
1492 } 1494 }
1493 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) { 1495 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1494 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only " 1496 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1495 "file version [%d] is supported by this " 1497 "file version [%d] is supported by this "
1496 "version of eCryptfs\n", 1498 "version of eCryptfs\n",
1497 crypt_stat->file_version, 1499 crypt_stat->file_version,
1498 ECRYPTFS_SUPPORTED_FILE_VERSION); 1500 ECRYPTFS_SUPPORTED_FILE_VERSION);
1499 rc = -EINVAL; 1501 rc = -EINVAL;
1500 goto out; 1502 goto out;
1501 } 1503 }
1502 offset += bytes_read; 1504 offset += bytes_read;
1503 if (crypt_stat->file_version >= 1) { 1505 if (crypt_stat->file_version >= 1) {
1504 rc = parse_header_metadata(crypt_stat, (page_virt + offset), 1506 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
1505 &bytes_read, validate_header_size); 1507 &bytes_read, validate_header_size);
1506 if (rc) { 1508 if (rc) {
1507 ecryptfs_printk(KERN_WARNING, "Error reading header " 1509 ecryptfs_printk(KERN_WARNING, "Error reading header "
1508 "metadata; rc = [%d]\n", rc); 1510 "metadata; rc = [%d]\n", rc);
1509 } 1511 }
1510 offset += bytes_read; 1512 offset += bytes_read;
1511 } else 1513 } else
1512 set_default_header_data(crypt_stat); 1514 set_default_header_data(crypt_stat);
1513 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset), 1515 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1514 ecryptfs_dentry); 1516 ecryptfs_dentry);
1515 out: 1517 out:
1516 return rc; 1518 return rc;
1517 } 1519 }
1518 1520
1519 /** 1521 /**
1520 * ecryptfs_read_xattr_region 1522 * ecryptfs_read_xattr_region
1521 * @page_virt: The vitual address into which to read the xattr data 1523 * @page_virt: The vitual address into which to read the xattr data
1522 * @ecryptfs_inode: The eCryptfs inode 1524 * @ecryptfs_inode: The eCryptfs inode
1523 * 1525 *
1524 * Attempts to read the crypto metadata from the extended attribute 1526 * Attempts to read the crypto metadata from the extended attribute
1525 * region of the lower file. 1527 * region of the lower file.
1526 * 1528 *
1527 * Returns zero on success; non-zero on error 1529 * Returns zero on success; non-zero on error
1528 */ 1530 */
1529 int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode) 1531 int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
1530 { 1532 {
1531 struct dentry *lower_dentry = 1533 struct dentry *lower_dentry =
1532 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry; 1534 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
1533 ssize_t size; 1535 ssize_t size;
1534 int rc = 0; 1536 int rc = 0;
1535 1537
1536 size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME, 1538 size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1537 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE); 1539 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1538 if (size < 0) { 1540 if (size < 0) {
1539 if (unlikely(ecryptfs_verbosity > 0)) 1541 if (unlikely(ecryptfs_verbosity > 0))
1540 printk(KERN_INFO "Error attempting to read the [%s] " 1542 printk(KERN_INFO "Error attempting to read the [%s] "
1541 "xattr from the lower file; return value = " 1543 "xattr from the lower file; return value = "
1542 "[%zd]\n", ECRYPTFS_XATTR_NAME, size); 1544 "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
1543 rc = -EINVAL; 1545 rc = -EINVAL;
1544 goto out; 1546 goto out;
1545 } 1547 }
1546 out: 1548 out:
1547 return rc; 1549 return rc;
1548 } 1550 }
1549 1551
1550 int ecryptfs_read_and_validate_xattr_region(char *page_virt, 1552 int ecryptfs_read_and_validate_xattr_region(char *page_virt,
1551 struct dentry *ecryptfs_dentry) 1553 struct dentry *ecryptfs_dentry)
1552 { 1554 {
1553 int rc; 1555 int rc;
1554 1556
1555 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode); 1557 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
1556 if (rc) 1558 if (rc)
1557 goto out; 1559 goto out;
1558 if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) { 1560 if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
1559 printk(KERN_WARNING "Valid data found in [%s] xattr, but " 1561 printk(KERN_WARNING "Valid data found in [%s] xattr, but "
1560 "the marker is invalid\n", ECRYPTFS_XATTR_NAME); 1562 "the marker is invalid\n", ECRYPTFS_XATTR_NAME);
1561 rc = -EINVAL; 1563 rc = -EINVAL;
1562 } 1564 }
1563 out: 1565 out:
1564 return rc; 1566 return rc;
1565 } 1567 }
1566 1568
1567 /** 1569 /**
1568 * ecryptfs_read_metadata 1570 * ecryptfs_read_metadata
1569 * 1571 *
1570 * Common entry point for reading file metadata. From here, we could 1572 * Common entry point for reading file metadata. From here, we could
1571 * retrieve the header information from the header region of the file, 1573 * retrieve the header information from the header region of the file,
1572 * the xattr region of the file, or some other repostory that is 1574 * the xattr region of the file, or some other repostory that is
1573 * stored separately from the file itself. The current implementation 1575 * stored separately from the file itself. The current implementation
1574 * supports retrieving the metadata information from the file contents 1576 * supports retrieving the metadata information from the file contents
1575 * and from the xattr region. 1577 * and from the xattr region.
1576 * 1578 *
1577 * Returns zero if valid headers found and parsed; non-zero otherwise 1579 * Returns zero if valid headers found and parsed; non-zero otherwise
1578 */ 1580 */
1579 int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) 1581 int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
1580 { 1582 {
1581 int rc = 0; 1583 int rc = 0;
1582 char *page_virt = NULL; 1584 char *page_virt = NULL;
1583 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode; 1585 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
1584 struct ecryptfs_crypt_stat *crypt_stat = 1586 struct ecryptfs_crypt_stat *crypt_stat =
1585 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; 1587 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1586 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 1588 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1587 &ecryptfs_superblock_to_private( 1589 &ecryptfs_superblock_to_private(
1588 ecryptfs_dentry->d_sb)->mount_crypt_stat; 1590 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1589 1591
1590 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, 1592 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1591 mount_crypt_stat); 1593 mount_crypt_stat);
1592 /* Read the first page from the underlying file */ 1594 /* Read the first page from the underlying file */
1593 page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER); 1595 page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
1594 if (!page_virt) { 1596 if (!page_virt) {
1595 rc = -ENOMEM; 1597 rc = -ENOMEM;
1596 printk(KERN_ERR "%s: Unable to allocate page_virt\n", 1598 printk(KERN_ERR "%s: Unable to allocate page_virt\n",
1597 __func__); 1599 __func__);
1598 goto out; 1600 goto out;
1599 } 1601 }
1600 rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size, 1602 rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1601 ecryptfs_inode); 1603 ecryptfs_inode);
1602 if (!rc) 1604 if (!rc)
1603 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, 1605 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1604 ecryptfs_dentry, 1606 ecryptfs_dentry,
1605 ECRYPTFS_VALIDATE_HEADER_SIZE); 1607 ECRYPTFS_VALIDATE_HEADER_SIZE);
1606 if (rc) { 1608 if (rc) {
1607 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode); 1609 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
1608 if (rc) { 1610 if (rc) {
1609 printk(KERN_DEBUG "Valid eCryptfs headers not found in " 1611 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1610 "file header region or xattr region\n"); 1612 "file header region or xattr region\n");
1611 rc = -EINVAL; 1613 rc = -EINVAL;
1612 goto out; 1614 goto out;
1613 } 1615 }
1614 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat, 1616 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1615 ecryptfs_dentry, 1617 ecryptfs_dentry,
1616 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE); 1618 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1617 if (rc) { 1619 if (rc) {
1618 printk(KERN_DEBUG "Valid eCryptfs headers not found in " 1620 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1619 "file xattr region either\n"); 1621 "file xattr region either\n");
1620 rc = -EINVAL; 1622 rc = -EINVAL;
1621 } 1623 }
1622 if (crypt_stat->mount_crypt_stat->flags 1624 if (crypt_stat->mount_crypt_stat->flags
1623 & ECRYPTFS_XATTR_METADATA_ENABLED) { 1625 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1624 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; 1626 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1625 } else { 1627 } else {
1626 printk(KERN_WARNING "Attempt to access file with " 1628 printk(KERN_WARNING "Attempt to access file with "
1627 "crypto metadata only in the extended attribute " 1629 "crypto metadata only in the extended attribute "
1628 "region, but eCryptfs was mounted without " 1630 "region, but eCryptfs was mounted without "
1629 "xattr support enabled. eCryptfs will not treat " 1631 "xattr support enabled. eCryptfs will not treat "
1630 "this like an encrypted file.\n"); 1632 "this like an encrypted file.\n");
1631 rc = -EINVAL; 1633 rc = -EINVAL;
1632 } 1634 }
1633 } 1635 }
1634 out: 1636 out:
1635 if (page_virt) { 1637 if (page_virt) {
1636 memset(page_virt, 0, PAGE_CACHE_SIZE); 1638 memset(page_virt, 0, PAGE_CACHE_SIZE);
1637 kmem_cache_free(ecryptfs_header_cache_1, page_virt); 1639 kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1638 } 1640 }
1639 return rc; 1641 return rc;
1640 } 1642 }
1641 1643
1642 /** 1644 /**
1643 * ecryptfs_encrypt_filename - encrypt filename 1645 * ecryptfs_encrypt_filename - encrypt filename
1644 * 1646 *
1645 * CBC-encrypts the filename. We do not want to encrypt the same 1647 * CBC-encrypts the filename. We do not want to encrypt the same
1646 * filename with the same key and IV, which may happen with hard 1648 * filename with the same key and IV, which may happen with hard
1647 * links, so we prepend random bits to each filename. 1649 * links, so we prepend random bits to each filename.
1648 * 1650 *
1649 * Returns zero on success; non-zero otherwise 1651 * Returns zero on success; non-zero otherwise
1650 */ 1652 */
1651 static int 1653 static int
1652 ecryptfs_encrypt_filename(struct ecryptfs_filename *filename, 1654 ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
1653 struct ecryptfs_crypt_stat *crypt_stat, 1655 struct ecryptfs_crypt_stat *crypt_stat,
1654 struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 1656 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
1655 { 1657 {
1656 int rc = 0; 1658 int rc = 0;
1657 1659
1658 filename->encrypted_filename = NULL; 1660 filename->encrypted_filename = NULL;
1659 filename->encrypted_filename_size = 0; 1661 filename->encrypted_filename_size = 0;
1660 if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK)) 1662 if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
1661 || (mount_crypt_stat && (mount_crypt_stat->flags 1663 || (mount_crypt_stat && (mount_crypt_stat->flags
1662 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) { 1664 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
1663 size_t packet_size; 1665 size_t packet_size;
1664 size_t remaining_bytes; 1666 size_t remaining_bytes;
1665 1667
1666 rc = ecryptfs_write_tag_70_packet( 1668 rc = ecryptfs_write_tag_70_packet(
1667 NULL, NULL, 1669 NULL, NULL,
1668 &filename->encrypted_filename_size, 1670 &filename->encrypted_filename_size,
1669 mount_crypt_stat, NULL, 1671 mount_crypt_stat, NULL,
1670 filename->filename_size); 1672 filename->filename_size);
1671 if (rc) { 1673 if (rc) {
1672 printk(KERN_ERR "%s: Error attempting to get packet " 1674 printk(KERN_ERR "%s: Error attempting to get packet "
1673 "size for tag 72; rc = [%d]\n", __func__, 1675 "size for tag 72; rc = [%d]\n", __func__,
1674 rc); 1676 rc);
1675 filename->encrypted_filename_size = 0; 1677 filename->encrypted_filename_size = 0;
1676 goto out; 1678 goto out;
1677 } 1679 }
1678 filename->encrypted_filename = 1680 filename->encrypted_filename =
1679 kmalloc(filename->encrypted_filename_size, GFP_KERNEL); 1681 kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
1680 if (!filename->encrypted_filename) { 1682 if (!filename->encrypted_filename) {
1681 printk(KERN_ERR "%s: Out of memory whilst attempting " 1683 printk(KERN_ERR "%s: Out of memory whilst attempting "
1682 "to kmalloc [%zd] bytes\n", __func__, 1684 "to kmalloc [%zd] bytes\n", __func__,
1683 filename->encrypted_filename_size); 1685 filename->encrypted_filename_size);
1684 rc = -ENOMEM; 1686 rc = -ENOMEM;
1685 goto out; 1687 goto out;
1686 } 1688 }
1687 remaining_bytes = filename->encrypted_filename_size; 1689 remaining_bytes = filename->encrypted_filename_size;
1688 rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename, 1690 rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
1689 &remaining_bytes, 1691 &remaining_bytes,
1690 &packet_size, 1692 &packet_size,
1691 mount_crypt_stat, 1693 mount_crypt_stat,
1692 filename->filename, 1694 filename->filename,
1693 filename->filename_size); 1695 filename->filename_size);
1694 if (rc) { 1696 if (rc) {
1695 printk(KERN_ERR "%s: Error attempting to generate " 1697 printk(KERN_ERR "%s: Error attempting to generate "
1696 "tag 70 packet; rc = [%d]\n", __func__, 1698 "tag 70 packet; rc = [%d]\n", __func__,
1697 rc); 1699 rc);
1698 kfree(filename->encrypted_filename); 1700 kfree(filename->encrypted_filename);
1699 filename->encrypted_filename = NULL; 1701 filename->encrypted_filename = NULL;
1700 filename->encrypted_filename_size = 0; 1702 filename->encrypted_filename_size = 0;
1701 goto out; 1703 goto out;
1702 } 1704 }
1703 filename->encrypted_filename_size = packet_size; 1705 filename->encrypted_filename_size = packet_size;
1704 } else { 1706 } else {
1705 printk(KERN_ERR "%s: No support for requested filename " 1707 printk(KERN_ERR "%s: No support for requested filename "
1706 "encryption method in this release\n", __func__); 1708 "encryption method in this release\n", __func__);
1707 rc = -ENOTSUPP; 1709 rc = -ENOTSUPP;
1708 goto out; 1710 goto out;
1709 } 1711 }
1710 out: 1712 out:
1711 return rc; 1713 return rc;
1712 } 1714 }
1713 1715
1714 static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size, 1716 static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
1715 const char *name, size_t name_size) 1717 const char *name, size_t name_size)
1716 { 1718 {
1717 int rc = 0; 1719 int rc = 0;
1718 1720
1719 (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL); 1721 (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
1720 if (!(*copied_name)) { 1722 if (!(*copied_name)) {
1721 rc = -ENOMEM; 1723 rc = -ENOMEM;
1722 goto out; 1724 goto out;
1723 } 1725 }
1724 memcpy((void *)(*copied_name), (void *)name, name_size); 1726 memcpy((void *)(*copied_name), (void *)name, name_size);
1725 (*copied_name)[(name_size)] = '\0'; /* Only for convenience 1727 (*copied_name)[(name_size)] = '\0'; /* Only for convenience
1726 * in printing out the 1728 * in printing out the
1727 * string in debug 1729 * string in debug
1728 * messages */ 1730 * messages */
1729 (*copied_name_size) = name_size; 1731 (*copied_name_size) = name_size;
1730 out: 1732 out:
1731 return rc; 1733 return rc;
1732 } 1734 }
1733 1735
1734 /** 1736 /**
1735 * ecryptfs_process_key_cipher - Perform key cipher initialization. 1737 * ecryptfs_process_key_cipher - Perform key cipher initialization.
1736 * @key_tfm: Crypto context for key material, set by this function 1738 * @key_tfm: Crypto context for key material, set by this function
1737 * @cipher_name: Name of the cipher 1739 * @cipher_name: Name of the cipher
1738 * @key_size: Size of the key in bytes 1740 * @key_size: Size of the key in bytes
1739 * 1741 *
1740 * Returns zero on success. Any crypto_tfm structs allocated here 1742 * Returns zero on success. Any crypto_tfm structs allocated here
1741 * should be released by other functions, such as on a superblock put 1743 * should be released by other functions, such as on a superblock put
1742 * event, regardless of whether this function succeeds for fails. 1744 * event, regardless of whether this function succeeds for fails.
1743 */ 1745 */
1744 static int 1746 static int
1745 ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm, 1747 ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1746 char *cipher_name, size_t *key_size) 1748 char *cipher_name, size_t *key_size)
1747 { 1749 {
1748 char dummy_key[ECRYPTFS_MAX_KEY_BYTES]; 1750 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
1749 char *full_alg_name; 1751 char *full_alg_name;
1750 int rc; 1752 int rc;
1751 1753
1752 *key_tfm = NULL; 1754 *key_tfm = NULL;
1753 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) { 1755 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
1754 rc = -EINVAL; 1756 rc = -EINVAL;
1755 printk(KERN_ERR "Requested key size is [%zd] bytes; maximum " 1757 printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
1756 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES); 1758 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
1757 goto out; 1759 goto out;
1758 } 1760 }
1759 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name, 1761 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1760 "ecb"); 1762 "ecb");
1761 if (rc) 1763 if (rc)
1762 goto out; 1764 goto out;
1763 *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC); 1765 *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
1764 kfree(full_alg_name); 1766 kfree(full_alg_name);
1765 if (IS_ERR(*key_tfm)) { 1767 if (IS_ERR(*key_tfm)) {
1766 rc = PTR_ERR(*key_tfm); 1768 rc = PTR_ERR(*key_tfm);
1767 printk(KERN_ERR "Unable to allocate crypto cipher with name " 1769 printk(KERN_ERR "Unable to allocate crypto cipher with name "
1768 "[%s]; rc = [%d]\n", cipher_name, rc); 1770 "[%s]; rc = [%d]\n", cipher_name, rc);
1769 goto out; 1771 goto out;
1770 } 1772 }
1771 crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY); 1773 crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1772 if (*key_size == 0) { 1774 if (*key_size == 0) {
1773 struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm); 1775 struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
1774 1776
1775 *key_size = alg->max_keysize; 1777 *key_size = alg->max_keysize;
1776 } 1778 }
1777 get_random_bytes(dummy_key, *key_size); 1779 get_random_bytes(dummy_key, *key_size);
1778 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size); 1780 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
1779 if (rc) { 1781 if (rc) {
1780 printk(KERN_ERR "Error attempting to set key of size [%zd] for " 1782 printk(KERN_ERR "Error attempting to set key of size [%zd] for "
1781 "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc); 1783 "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
1782 rc = -EINVAL; 1784 rc = -EINVAL;
1783 goto out; 1785 goto out;
1784 } 1786 }
1785 out: 1787 out:
1786 return rc; 1788 return rc;
1787 } 1789 }
1788 1790
1789 struct kmem_cache *ecryptfs_key_tfm_cache; 1791 struct kmem_cache *ecryptfs_key_tfm_cache;
1790 static struct list_head key_tfm_list; 1792 static struct list_head key_tfm_list;
1791 struct mutex key_tfm_list_mutex; 1793 struct mutex key_tfm_list_mutex;
1792 1794
1793 int ecryptfs_init_crypto(void) 1795 int ecryptfs_init_crypto(void)
1794 { 1796 {
1795 mutex_init(&key_tfm_list_mutex); 1797 mutex_init(&key_tfm_list_mutex);
1796 INIT_LIST_HEAD(&key_tfm_list); 1798 INIT_LIST_HEAD(&key_tfm_list);
1797 return 0; 1799 return 0;
1798 } 1800 }
1799 1801
1800 /** 1802 /**
1801 * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list 1803 * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
1802 * 1804 *
1803 * Called only at module unload time 1805 * Called only at module unload time
1804 */ 1806 */
1805 int ecryptfs_destroy_crypto(void) 1807 int ecryptfs_destroy_crypto(void)
1806 { 1808 {
1807 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp; 1809 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1808 1810
1809 mutex_lock(&key_tfm_list_mutex); 1811 mutex_lock(&key_tfm_list_mutex);
1810 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list, 1812 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1811 key_tfm_list) { 1813 key_tfm_list) {
1812 list_del(&key_tfm->key_tfm_list); 1814 list_del(&key_tfm->key_tfm_list);
1813 if (key_tfm->key_tfm) 1815 if (key_tfm->key_tfm)
1814 crypto_free_blkcipher(key_tfm->key_tfm); 1816 crypto_free_blkcipher(key_tfm->key_tfm);
1815 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm); 1817 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1816 } 1818 }
1817 mutex_unlock(&key_tfm_list_mutex); 1819 mutex_unlock(&key_tfm_list_mutex);
1818 return 0; 1820 return 0;
1819 } 1821 }
1820 1822
1821 int 1823 int
1822 ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name, 1824 ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1823 size_t key_size) 1825 size_t key_size)
1824 { 1826 {
1825 struct ecryptfs_key_tfm *tmp_tfm; 1827 struct ecryptfs_key_tfm *tmp_tfm;
1826 int rc = 0; 1828 int rc = 0;
1827 1829
1828 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex)); 1830 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1829 1831
1830 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL); 1832 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1831 if (key_tfm != NULL) 1833 if (key_tfm != NULL)
1832 (*key_tfm) = tmp_tfm; 1834 (*key_tfm) = tmp_tfm;
1833 if (!tmp_tfm) { 1835 if (!tmp_tfm) {
1834 rc = -ENOMEM; 1836 rc = -ENOMEM;
1835 printk(KERN_ERR "Error attempting to allocate from " 1837 printk(KERN_ERR "Error attempting to allocate from "
1836 "ecryptfs_key_tfm_cache\n"); 1838 "ecryptfs_key_tfm_cache\n");
1837 goto out; 1839 goto out;
1838 } 1840 }
1839 mutex_init(&tmp_tfm->key_tfm_mutex); 1841 mutex_init(&tmp_tfm->key_tfm_mutex);
1840 strncpy(tmp_tfm->cipher_name, cipher_name, 1842 strncpy(tmp_tfm->cipher_name, cipher_name,
1841 ECRYPTFS_MAX_CIPHER_NAME_SIZE); 1843 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1842 tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0'; 1844 tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
1843 tmp_tfm->key_size = key_size; 1845 tmp_tfm->key_size = key_size;
1844 rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm, 1846 rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1845 tmp_tfm->cipher_name, 1847 tmp_tfm->cipher_name,
1846 &tmp_tfm->key_size); 1848 &tmp_tfm->key_size);
1847 if (rc) { 1849 if (rc) {
1848 printk(KERN_ERR "Error attempting to initialize key TFM " 1850 printk(KERN_ERR "Error attempting to initialize key TFM "
1849 "cipher with name = [%s]; rc = [%d]\n", 1851 "cipher with name = [%s]; rc = [%d]\n",
1850 tmp_tfm->cipher_name, rc); 1852 tmp_tfm->cipher_name, rc);
1851 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm); 1853 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1852 if (key_tfm != NULL) 1854 if (key_tfm != NULL)
1853 (*key_tfm) = NULL; 1855 (*key_tfm) = NULL;
1854 goto out; 1856 goto out;
1855 } 1857 }
1856 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list); 1858 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1857 out: 1859 out:
1858 return rc; 1860 return rc;
1859 } 1861 }
1860 1862
1861 /** 1863 /**
1862 * ecryptfs_tfm_exists - Search for existing tfm for cipher_name. 1864 * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
1863 * @cipher_name: the name of the cipher to search for 1865 * @cipher_name: the name of the cipher to search for
1864 * @key_tfm: set to corresponding tfm if found 1866 * @key_tfm: set to corresponding tfm if found
1865 * 1867 *
1866 * Searches for cached key_tfm matching @cipher_name 1868 * Searches for cached key_tfm matching @cipher_name
1867 * Must be called with &key_tfm_list_mutex held 1869 * Must be called with &key_tfm_list_mutex held
1868 * Returns 1 if found, with @key_tfm set 1870 * Returns 1 if found, with @key_tfm set
1869 * Returns 0 if not found, with @key_tfm set to NULL 1871 * Returns 0 if not found, with @key_tfm set to NULL
1870 */ 1872 */
1871 int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm) 1873 int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
1872 { 1874 {
1873 struct ecryptfs_key_tfm *tmp_key_tfm; 1875 struct ecryptfs_key_tfm *tmp_key_tfm;
1874 1876
1875 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex)); 1877 BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1876 1878
1877 list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) { 1879 list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
1878 if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) { 1880 if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
1879 if (key_tfm) 1881 if (key_tfm)
1880 (*key_tfm) = tmp_key_tfm; 1882 (*key_tfm) = tmp_key_tfm;
1881 return 1; 1883 return 1;
1882 } 1884 }
1883 } 1885 }
1884 if (key_tfm) 1886 if (key_tfm)
1885 (*key_tfm) = NULL; 1887 (*key_tfm) = NULL;
1886 return 0; 1888 return 0;
1887 } 1889 }
1888 1890
1889 /** 1891 /**
1890 * ecryptfs_get_tfm_and_mutex_for_cipher_name 1892 * ecryptfs_get_tfm_and_mutex_for_cipher_name
1891 * 1893 *
1892 * @tfm: set to cached tfm found, or new tfm created 1894 * @tfm: set to cached tfm found, or new tfm created
1893 * @tfm_mutex: set to mutex for cached tfm found, or new tfm created 1895 * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
1894 * @cipher_name: the name of the cipher to search for and/or add 1896 * @cipher_name: the name of the cipher to search for and/or add
1895 * 1897 *
1896 * Sets pointers to @tfm & @tfm_mutex matching @cipher_name. 1898 * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
1897 * Searches for cached item first, and creates new if not found. 1899 * Searches for cached item first, and creates new if not found.
1898 * Returns 0 on success, non-zero if adding new cipher failed 1900 * Returns 0 on success, non-zero if adding new cipher failed
1899 */ 1901 */
1900 int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm, 1902 int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1901 struct mutex **tfm_mutex, 1903 struct mutex **tfm_mutex,
1902 char *cipher_name) 1904 char *cipher_name)
1903 { 1905 {
1904 struct ecryptfs_key_tfm *key_tfm; 1906 struct ecryptfs_key_tfm *key_tfm;
1905 int rc = 0; 1907 int rc = 0;
1906 1908
1907 (*tfm) = NULL; 1909 (*tfm) = NULL;
1908 (*tfm_mutex) = NULL; 1910 (*tfm_mutex) = NULL;
1909 1911
1910 mutex_lock(&key_tfm_list_mutex); 1912 mutex_lock(&key_tfm_list_mutex);
1911 if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) { 1913 if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
1912 rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0); 1914 rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
1913 if (rc) { 1915 if (rc) {
1914 printk(KERN_ERR "Error adding new key_tfm to list; " 1916 printk(KERN_ERR "Error adding new key_tfm to list; "
1915 "rc = [%d]\n", rc); 1917 "rc = [%d]\n", rc);
1916 goto out; 1918 goto out;
1917 } 1919 }
1918 } 1920 }
1919 (*tfm) = key_tfm->key_tfm; 1921 (*tfm) = key_tfm->key_tfm;
1920 (*tfm_mutex) = &key_tfm->key_tfm_mutex; 1922 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1921 out: 1923 out:
1922 mutex_unlock(&key_tfm_list_mutex); 1924 mutex_unlock(&key_tfm_list_mutex);
1923 return rc; 1925 return rc;
1924 } 1926 }
1925 1927
1926 /* 64 characters forming a 6-bit target field */ 1928 /* 64 characters forming a 6-bit target field */
1927 static unsigned char *portable_filename_chars = ("-.0123456789ABCD" 1929 static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
1928 "EFGHIJKLMNOPQRST" 1930 "EFGHIJKLMNOPQRST"
1929 "UVWXYZabcdefghij" 1931 "UVWXYZabcdefghij"
1930 "klmnopqrstuvwxyz"); 1932 "klmnopqrstuvwxyz");
1931 1933
1932 /* We could either offset on every reverse map or just pad some 0x00's 1934 /* We could either offset on every reverse map or just pad some 0x00's
1933 * at the front here */ 1935 * at the front here */
1934 static const unsigned char filename_rev_map[] = { 1936 static const unsigned char filename_rev_map[] = {
1935 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */ 1937 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
1936 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */ 1938 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
1937 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */ 1939 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
1938 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */ 1940 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
1939 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */ 1941 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
1940 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */ 1942 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
1941 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */ 1943 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
1942 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */ 1944 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
1943 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */ 1945 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
1944 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */ 1946 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
1945 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */ 1947 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
1946 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */ 1948 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
1947 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */ 1949 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
1948 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */ 1950 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
1949 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */ 1951 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
1950 0x3D, 0x3E, 0x3F 1952 0x3D, 0x3E, 0x3F
1951 }; 1953 };
1952 1954
1953 /** 1955 /**
1954 * ecryptfs_encode_for_filename 1956 * ecryptfs_encode_for_filename
1955 * @dst: Destination location for encoded filename 1957 * @dst: Destination location for encoded filename
1956 * @dst_size: Size of the encoded filename in bytes 1958 * @dst_size: Size of the encoded filename in bytes
1957 * @src: Source location for the filename to encode 1959 * @src: Source location for the filename to encode
1958 * @src_size: Size of the source in bytes 1960 * @src_size: Size of the source in bytes
1959 */ 1961 */
1960 void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size, 1962 void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
1961 unsigned char *src, size_t src_size) 1963 unsigned char *src, size_t src_size)
1962 { 1964 {
1963 size_t num_blocks; 1965 size_t num_blocks;
1964 size_t block_num = 0; 1966 size_t block_num = 0;
1965 size_t dst_offset = 0; 1967 size_t dst_offset = 0;
1966 unsigned char last_block[3]; 1968 unsigned char last_block[3];
1967 1969
1968 if (src_size == 0) { 1970 if (src_size == 0) {
1969 (*dst_size) = 0; 1971 (*dst_size) = 0;
1970 goto out; 1972 goto out;
1971 } 1973 }
1972 num_blocks = (src_size / 3); 1974 num_blocks = (src_size / 3);
1973 if ((src_size % 3) == 0) { 1975 if ((src_size % 3) == 0) {
1974 memcpy(last_block, (&src[src_size - 3]), 3); 1976 memcpy(last_block, (&src[src_size - 3]), 3);
1975 } else { 1977 } else {
1976 num_blocks++; 1978 num_blocks++;
1977 last_block[2] = 0x00; 1979 last_block[2] = 0x00;
1978 switch (src_size % 3) { 1980 switch (src_size % 3) {
1979 case 1: 1981 case 1:
1980 last_block[0] = src[src_size - 1]; 1982 last_block[0] = src[src_size - 1];
1981 last_block[1] = 0x00; 1983 last_block[1] = 0x00;
1982 break; 1984 break;
1983 case 2: 1985 case 2:
1984 last_block[0] = src[src_size - 2]; 1986 last_block[0] = src[src_size - 2];
1985 last_block[1] = src[src_size - 1]; 1987 last_block[1] = src[src_size - 1];
1986 } 1988 }
1987 } 1989 }
1988 (*dst_size) = (num_blocks * 4); 1990 (*dst_size) = (num_blocks * 4);
1989 if (!dst) 1991 if (!dst)
1990 goto out; 1992 goto out;
1991 while (block_num < num_blocks) { 1993 while (block_num < num_blocks) {
1992 unsigned char *src_block; 1994 unsigned char *src_block;
1993 unsigned char dst_block[4]; 1995 unsigned char dst_block[4];
1994 1996
1995 if (block_num == (num_blocks - 1)) 1997 if (block_num == (num_blocks - 1))
1996 src_block = last_block; 1998 src_block = last_block;
1997 else 1999 else
1998 src_block = &src[block_num * 3]; 2000 src_block = &src[block_num * 3];
1999 dst_block[0] = ((src_block[0] >> 2) & 0x3F); 2001 dst_block[0] = ((src_block[0] >> 2) & 0x3F);
2000 dst_block[1] = (((src_block[0] << 4) & 0x30) 2002 dst_block[1] = (((src_block[0] << 4) & 0x30)
2001 | ((src_block[1] >> 4) & 0x0F)); 2003 | ((src_block[1] >> 4) & 0x0F));
2002 dst_block[2] = (((src_block[1] << 2) & 0x3C) 2004 dst_block[2] = (((src_block[1] << 2) & 0x3C)
2003 | ((src_block[2] >> 6) & 0x03)); 2005 | ((src_block[2] >> 6) & 0x03));
2004 dst_block[3] = (src_block[2] & 0x3F); 2006 dst_block[3] = (src_block[2] & 0x3F);
2005 dst[dst_offset++] = portable_filename_chars[dst_block[0]]; 2007 dst[dst_offset++] = portable_filename_chars[dst_block[0]];
2006 dst[dst_offset++] = portable_filename_chars[dst_block[1]]; 2008 dst[dst_offset++] = portable_filename_chars[dst_block[1]];
2007 dst[dst_offset++] = portable_filename_chars[dst_block[2]]; 2009 dst[dst_offset++] = portable_filename_chars[dst_block[2]];
2008 dst[dst_offset++] = portable_filename_chars[dst_block[3]]; 2010 dst[dst_offset++] = portable_filename_chars[dst_block[3]];
2009 block_num++; 2011 block_num++;
2010 } 2012 }
2011 out: 2013 out:
2012 return; 2014 return;
2013 } 2015 }
2014 2016
2015 /** 2017 /**
2016 * ecryptfs_decode_from_filename 2018 * ecryptfs_decode_from_filename
2017 * @dst: If NULL, this function only sets @dst_size and returns. If 2019 * @dst: If NULL, this function only sets @dst_size and returns. If
2018 * non-NULL, this function decodes the encoded octets in @src 2020 * non-NULL, this function decodes the encoded octets in @src
2019 * into the memory that @dst points to. 2021 * into the memory that @dst points to.
2020 * @dst_size: Set to the size of the decoded string. 2022 * @dst_size: Set to the size of the decoded string.
2021 * @src: The encoded set of octets to decode. 2023 * @src: The encoded set of octets to decode.
2022 * @src_size: The size of the encoded set of octets to decode. 2024 * @src_size: The size of the encoded set of octets to decode.
2023 */ 2025 */
2024 static void 2026 static void
2025 ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size, 2027 ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
2026 const unsigned char *src, size_t src_size) 2028 const unsigned char *src, size_t src_size)
2027 { 2029 {
2028 u8 current_bit_offset = 0; 2030 u8 current_bit_offset = 0;
2029 size_t src_byte_offset = 0; 2031 size_t src_byte_offset = 0;
2030 size_t dst_byte_offset = 0; 2032 size_t dst_byte_offset = 0;
2031 2033
2032 if (dst == NULL) { 2034 if (dst == NULL) {
2033 /* Not exact; conservatively long. Every block of 4 2035 /* Not exact; conservatively long. Every block of 4
2034 * encoded characters decodes into a block of 3 2036 * encoded characters decodes into a block of 3
2035 * decoded characters. This segment of code provides 2037 * decoded characters. This segment of code provides
2036 * the caller with the maximum amount of allocated 2038 * the caller with the maximum amount of allocated
2037 * space that @dst will need to point to in a 2039 * space that @dst will need to point to in a
2038 * subsequent call. */ 2040 * subsequent call. */
2039 (*dst_size) = (((src_size + 1) * 3) / 4); 2041 (*dst_size) = (((src_size + 1) * 3) / 4);
2040 goto out; 2042 goto out;
2041 } 2043 }
2042 while (src_byte_offset < src_size) { 2044 while (src_byte_offset < src_size) {
2043 unsigned char src_byte = 2045 unsigned char src_byte =
2044 filename_rev_map[(int)src[src_byte_offset]]; 2046 filename_rev_map[(int)src[src_byte_offset]];
2045 2047
2046 switch (current_bit_offset) { 2048 switch (current_bit_offset) {
2047 case 0: 2049 case 0:
2048 dst[dst_byte_offset] = (src_byte << 2); 2050 dst[dst_byte_offset] = (src_byte << 2);
2049 current_bit_offset = 6; 2051 current_bit_offset = 6;
2050 break; 2052 break;
2051 case 6: 2053 case 6:
2052 dst[dst_byte_offset++] |= (src_byte >> 4); 2054 dst[dst_byte_offset++] |= (src_byte >> 4);
2053 dst[dst_byte_offset] = ((src_byte & 0xF) 2055 dst[dst_byte_offset] = ((src_byte & 0xF)
2054 << 4); 2056 << 4);
2055 current_bit_offset = 4; 2057 current_bit_offset = 4;
2056 break; 2058 break;
2057 case 4: 2059 case 4:
2058 dst[dst_byte_offset++] |= (src_byte >> 2); 2060 dst[dst_byte_offset++] |= (src_byte >> 2);
2059 dst[dst_byte_offset] = (src_byte << 6); 2061 dst[dst_byte_offset] = (src_byte << 6);
2060 current_bit_offset = 2; 2062 current_bit_offset = 2;
2061 break; 2063 break;
2062 case 2: 2064 case 2:
2063 dst[dst_byte_offset++] |= (src_byte); 2065 dst[dst_byte_offset++] |= (src_byte);
2064 dst[dst_byte_offset] = 0; 2066 dst[dst_byte_offset] = 0;
2065 current_bit_offset = 0; 2067 current_bit_offset = 0;
2066 break; 2068 break;
2067 } 2069 }
2068 src_byte_offset++; 2070 src_byte_offset++;
2069 } 2071 }
2070 (*dst_size) = dst_byte_offset; 2072 (*dst_size) = dst_byte_offset;
2071 out: 2073 out:
2072 return; 2074 return;
2073 } 2075 }
2074 2076
2075 /** 2077 /**
2076 * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text 2078 * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
2077 * @crypt_stat: The crypt_stat struct associated with the file anem to encode 2079 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
2078 * @name: The plaintext name 2080 * @name: The plaintext name
2079 * @length: The length of the plaintext 2081 * @length: The length of the plaintext
2080 * @encoded_name: The encypted name 2082 * @encoded_name: The encypted name
2081 * 2083 *
2082 * Encrypts and encodes a filename into something that constitutes a 2084 * Encrypts and encodes a filename into something that constitutes a
2083 * valid filename for a filesystem, with printable characters. 2085 * valid filename for a filesystem, with printable characters.
2084 * 2086 *
2085 * We assume that we have a properly initialized crypto context, 2087 * We assume that we have a properly initialized crypto context,
2086 * pointed to by crypt_stat->tfm. 2088 * pointed to by crypt_stat->tfm.
2087 * 2089 *
2088 * Returns zero on success; non-zero on otherwise 2090 * Returns zero on success; non-zero on otherwise
2089 */ 2091 */
2090 int ecryptfs_encrypt_and_encode_filename( 2092 int ecryptfs_encrypt_and_encode_filename(
2091 char **encoded_name, 2093 char **encoded_name,
2092 size_t *encoded_name_size, 2094 size_t *encoded_name_size,
2093 struct ecryptfs_crypt_stat *crypt_stat, 2095 struct ecryptfs_crypt_stat *crypt_stat,
2094 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 2096 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
2095 const char *name, size_t name_size) 2097 const char *name, size_t name_size)
2096 { 2098 {
2097 size_t encoded_name_no_prefix_size; 2099 size_t encoded_name_no_prefix_size;
2098 int rc = 0; 2100 int rc = 0;
2099 2101
2100 (*encoded_name) = NULL; 2102 (*encoded_name) = NULL;
2101 (*encoded_name_size) = 0; 2103 (*encoded_name_size) = 0;
2102 if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES)) 2104 if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES))
2103 || (mount_crypt_stat && (mount_crypt_stat->flags 2105 || (mount_crypt_stat && (mount_crypt_stat->flags
2104 & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) { 2106 & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) {
2105 struct ecryptfs_filename *filename; 2107 struct ecryptfs_filename *filename;
2106 2108
2107 filename = kzalloc(sizeof(*filename), GFP_KERNEL); 2109 filename = kzalloc(sizeof(*filename), GFP_KERNEL);
2108 if (!filename) { 2110 if (!filename) {
2109 printk(KERN_ERR "%s: Out of memory whilst attempting " 2111 printk(KERN_ERR "%s: Out of memory whilst attempting "
2110 "to kzalloc [%zd] bytes\n", __func__, 2112 "to kzalloc [%zd] bytes\n", __func__,
2111 sizeof(*filename)); 2113 sizeof(*filename));
2112 rc = -ENOMEM; 2114 rc = -ENOMEM;
2113 goto out; 2115 goto out;
2114 } 2116 }
2115 filename->filename = (char *)name; 2117 filename->filename = (char *)name;
2116 filename->filename_size = name_size; 2118 filename->filename_size = name_size;
2117 rc = ecryptfs_encrypt_filename(filename, crypt_stat, 2119 rc = ecryptfs_encrypt_filename(filename, crypt_stat,
2118 mount_crypt_stat); 2120 mount_crypt_stat);
2119 if (rc) { 2121 if (rc) {
2120 printk(KERN_ERR "%s: Error attempting to encrypt " 2122 printk(KERN_ERR "%s: Error attempting to encrypt "
2121 "filename; rc = [%d]\n", __func__, rc); 2123 "filename; rc = [%d]\n", __func__, rc);
2122 kfree(filename); 2124 kfree(filename);
2123 goto out; 2125 goto out;
2124 } 2126 }
2125 ecryptfs_encode_for_filename( 2127 ecryptfs_encode_for_filename(
2126 NULL, &encoded_name_no_prefix_size, 2128 NULL, &encoded_name_no_prefix_size,
2127 filename->encrypted_filename, 2129 filename->encrypted_filename,
2128 filename->encrypted_filename_size); 2130 filename->encrypted_filename_size);
2129 if ((crypt_stat && (crypt_stat->flags 2131 if ((crypt_stat && (crypt_stat->flags
2130 & ECRYPTFS_ENCFN_USE_MOUNT_FNEK)) 2132 & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
2131 || (mount_crypt_stat 2133 || (mount_crypt_stat
2132 && (mount_crypt_stat->flags 2134 && (mount_crypt_stat->flags
2133 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) 2135 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)))
2134 (*encoded_name_size) = 2136 (*encoded_name_size) =
2135 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE 2137 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2136 + encoded_name_no_prefix_size); 2138 + encoded_name_no_prefix_size);
2137 else 2139 else
2138 (*encoded_name_size) = 2140 (*encoded_name_size) =
2139 (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE 2141 (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2140 + encoded_name_no_prefix_size); 2142 + encoded_name_no_prefix_size);
2141 (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL); 2143 (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
2142 if (!(*encoded_name)) { 2144 if (!(*encoded_name)) {
2143 printk(KERN_ERR "%s: Out of memory whilst attempting " 2145 printk(KERN_ERR "%s: Out of memory whilst attempting "
2144 "to kzalloc [%zd] bytes\n", __func__, 2146 "to kzalloc [%zd] bytes\n", __func__,
2145 (*encoded_name_size)); 2147 (*encoded_name_size));
2146 rc = -ENOMEM; 2148 rc = -ENOMEM;
2147 kfree(filename->encrypted_filename); 2149 kfree(filename->encrypted_filename);
2148 kfree(filename); 2150 kfree(filename);
2149 goto out; 2151 goto out;
2150 } 2152 }
2151 if ((crypt_stat && (crypt_stat->flags 2153 if ((crypt_stat && (crypt_stat->flags
2152 & ECRYPTFS_ENCFN_USE_MOUNT_FNEK)) 2154 & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
2153 || (mount_crypt_stat 2155 || (mount_crypt_stat
2154 && (mount_crypt_stat->flags 2156 && (mount_crypt_stat->flags
2155 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) { 2157 & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
2156 memcpy((*encoded_name), 2158 memcpy((*encoded_name),
2157 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX, 2159 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2158 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE); 2160 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
2159 ecryptfs_encode_for_filename( 2161 ecryptfs_encode_for_filename(
2160 ((*encoded_name) 2162 ((*encoded_name)
2161 + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE), 2163 + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
2162 &encoded_name_no_prefix_size, 2164 &encoded_name_no_prefix_size,
2163 filename->encrypted_filename, 2165 filename->encrypted_filename,
2164 filename->encrypted_filename_size); 2166 filename->encrypted_filename_size);
2165 (*encoded_name_size) = 2167 (*encoded_name_size) =
2166 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE 2168 (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
2167 + encoded_name_no_prefix_size); 2169 + encoded_name_no_prefix_size);
2168 (*encoded_name)[(*encoded_name_size)] = '\0'; 2170 (*encoded_name)[(*encoded_name_size)] = '\0';
2169 (*encoded_name_size)++; 2171 (*encoded_name_size)++;
2170 } else { 2172 } else {
2171 rc = -ENOTSUPP; 2173 rc = -ENOTSUPP;
2172 } 2174 }
2173 if (rc) { 2175 if (rc) {
2174 printk(KERN_ERR "%s: Error attempting to encode " 2176 printk(KERN_ERR "%s: Error attempting to encode "
2175 "encrypted filename; rc = [%d]\n", __func__, 2177 "encrypted filename; rc = [%d]\n", __func__,
2176 rc); 2178 rc);
2177 kfree((*encoded_name)); 2179 kfree((*encoded_name));
2178 (*encoded_name) = NULL; 2180 (*encoded_name) = NULL;
2179 (*encoded_name_size) = 0; 2181 (*encoded_name_size) = 0;
2180 } 2182 }
2181 kfree(filename->encrypted_filename); 2183 kfree(filename->encrypted_filename);
2182 kfree(filename); 2184 kfree(filename);
2183 } else { 2185 } else {
2184 rc = ecryptfs_copy_filename(encoded_name, 2186 rc = ecryptfs_copy_filename(encoded_name,
2185 encoded_name_size, 2187 encoded_name_size,
2186 name, name_size); 2188 name, name_size);
2187 } 2189 }
2188 out: 2190 out:
2189 return rc; 2191 return rc;
2190 } 2192 }
2191 2193
2192 /** 2194 /**
2193 * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext 2195 * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
2194 * @plaintext_name: The plaintext name 2196 * @plaintext_name: The plaintext name
2195 * @plaintext_name_size: The plaintext name size 2197 * @plaintext_name_size: The plaintext name size
2196 * @ecryptfs_dir_dentry: eCryptfs directory dentry 2198 * @ecryptfs_dir_dentry: eCryptfs directory dentry
2197 * @name: The filename in cipher text 2199 * @name: The filename in cipher text
2198 * @name_size: The cipher text name size 2200 * @name_size: The cipher text name size
2199 * 2201 *
2200 * Decrypts and decodes the filename. 2202 * Decrypts and decodes the filename.
2201 * 2203 *
2202 * Returns zero on error; non-zero otherwise 2204 * Returns zero on error; non-zero otherwise
2203 */ 2205 */
2204 int ecryptfs_decode_and_decrypt_filename(char **plaintext_name, 2206 int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
2205 size_t *plaintext_name_size, 2207 size_t *plaintext_name_size,
2206 struct dentry *ecryptfs_dir_dentry, 2208 struct dentry *ecryptfs_dir_dentry,
2207 const char *name, size_t name_size) 2209 const char *name, size_t name_size)
2208 { 2210 {
2209 char *decoded_name; 2211 char *decoded_name;
2210 size_t decoded_name_size; 2212 size_t decoded_name_size;
2211 size_t packet_size; 2213 size_t packet_size;
2212 int rc = 0; 2214 int rc = 0;
2213 2215
2214 if ((name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) 2216 if ((name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
2215 && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX, 2217 && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2216 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) { 2218 ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) {
2217 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 2219 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2218 &ecryptfs_superblock_to_private( 2220 &ecryptfs_superblock_to_private(
2219 ecryptfs_dir_dentry->d_sb)->mount_crypt_stat; 2221 ecryptfs_dir_dentry->d_sb)->mount_crypt_stat;
2220 const char *orig_name = name; 2222 const char *orig_name = name;
2221 size_t orig_name_size = name_size; 2223 size_t orig_name_size = name_size;
2222 2224
2223 name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE; 2225 name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2224 name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE; 2226 name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
2225 ecryptfs_decode_from_filename(NULL, &decoded_name_size, 2227 ecryptfs_decode_from_filename(NULL, &decoded_name_size,
2226 name, name_size); 2228 name, name_size);
2227 decoded_name = kmalloc(decoded_name_size, GFP_KERNEL); 2229 decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
2228 if (!decoded_name) { 2230 if (!decoded_name) {
2229 printk(KERN_ERR "%s: Out of memory whilst attempting " 2231 printk(KERN_ERR "%s: Out of memory whilst attempting "
2230 "to kmalloc [%zd] bytes\n", __func__, 2232 "to kmalloc [%zd] bytes\n", __func__,
2231 decoded_name_size); 2233 decoded_name_size);
2232 rc = -ENOMEM; 2234 rc = -ENOMEM;
2233 goto out; 2235 goto out;
2234 } 2236 }
2235 ecryptfs_decode_from_filename(decoded_name, &decoded_name_size, 2237 ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
2236 name, name_size); 2238 name, name_size);
2237 rc = ecryptfs_parse_tag_70_packet(plaintext_name, 2239 rc = ecryptfs_parse_tag_70_packet(plaintext_name,
2238 plaintext_name_size, 2240 plaintext_name_size,
2239 &packet_size, 2241 &packet_size,
2240 mount_crypt_stat, 2242 mount_crypt_stat,
2241 decoded_name, 2243 decoded_name,
2242 decoded_name_size); 2244 decoded_name_size);
2243 if (rc) { 2245 if (rc) {
2244 printk(KERN_INFO "%s: Could not parse tag 70 packet " 2246 printk(KERN_INFO "%s: Could not parse tag 70 packet "
2245 "from filename; copying through filename " 2247 "from filename; copying through filename "
2246 "as-is\n", __func__); 2248 "as-is\n", __func__);
2247 rc = ecryptfs_copy_filename(plaintext_name, 2249 rc = ecryptfs_copy_filename(plaintext_name,
2248 plaintext_name_size, 2250 plaintext_name_size,
2249 orig_name, orig_name_size); 2251 orig_name, orig_name_size);
2250 goto out_free; 2252 goto out_free;
2251 } 2253 }
2252 } else { 2254 } else {
2253 rc = ecryptfs_copy_filename(plaintext_name, 2255 rc = ecryptfs_copy_filename(plaintext_name,
2254 plaintext_name_size, 2256 plaintext_name_size,
2255 name, name_size); 2257 name, name_size);
2256 goto out; 2258 goto out;
2257 } 2259 }
2258 out_free: 2260 out_free:
2259 kfree(decoded_name); 2261 kfree(decoded_name);
2260 out: 2262 out:
2261 return rc; 2263 return rc;
2262 } 2264 }
2263 2265
fs/ecryptfs/ecryptfs_kernel.h
1 /** 1 /**
2 * eCryptfs: Linux filesystem encryption layer 2 * eCryptfs: Linux filesystem encryption layer
3 * Kernel declarations. 3 * Kernel declarations.
4 * 4 *
5 * Copyright (C) 1997-2003 Erez Zadok 5 * Copyright (C) 1997-2003 Erez Zadok
6 * Copyright (C) 2001-2003 Stony Brook University 6 * Copyright (C) 2001-2003 Stony Brook University
7 * Copyright (C) 2004-2008 International Business Machines Corp. 7 * Copyright (C) 2004-2008 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 8 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9 * Trevor S. Highland <trevor.highland@gmail.com> 9 * Trevor S. Highland <trevor.highland@gmail.com>
10 * Tyler Hicks <tyhicks@ou.edu> 10 * Tyler Hicks <tyhicks@ou.edu>
11 * 11 *
12 * This program is free software; you can redistribute it and/or 12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as 13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the 14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version. 15 * License, or (at your option) any later version.
16 * 16 *
17 * This program is distributed in the hope that it will be useful, but 17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of 18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details. 20 * General Public License for more details.
21 * 21 *
22 * You should have received a copy of the GNU General Public License 22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software 23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA. 25 * 02111-1307, USA.
26 */ 26 */
27 27
28 #ifndef ECRYPTFS_KERNEL_H 28 #ifndef ECRYPTFS_KERNEL_H
29 #define ECRYPTFS_KERNEL_H 29 #define ECRYPTFS_KERNEL_H
30 30
31 #include <keys/user-type.h> 31 #include <keys/user-type.h>
32 #include <linux/fs.h> 32 #include <linux/fs.h>
33 #include <linux/fs_stack.h> 33 #include <linux/fs_stack.h>
34 #include <linux/namei.h> 34 #include <linux/namei.h>
35 #include <linux/scatterlist.h> 35 #include <linux/scatterlist.h>
36 #include <linux/hash.h> 36 #include <linux/hash.h>
37 #include <linux/nsproxy.h> 37 #include <linux/nsproxy.h>
38 38
39 /* Version verification for shared data structures w/ userspace */ 39 /* Version verification for shared data structures w/ userspace */
40 #define ECRYPTFS_VERSION_MAJOR 0x00 40 #define ECRYPTFS_VERSION_MAJOR 0x00
41 #define ECRYPTFS_VERSION_MINOR 0x04 41 #define ECRYPTFS_VERSION_MINOR 0x04
42 #define ECRYPTFS_SUPPORTED_FILE_VERSION 0x03 42 #define ECRYPTFS_SUPPORTED_FILE_VERSION 0x03
43 /* These flags indicate which features are supported by the kernel 43 /* These flags indicate which features are supported by the kernel
44 * module; userspace tools such as the mount helper read 44 * module; userspace tools such as the mount helper read
45 * ECRYPTFS_VERSIONING_MASK from a sysfs handle in order to determine 45 * ECRYPTFS_VERSIONING_MASK from a sysfs handle in order to determine
46 * how to behave. */ 46 * how to behave. */
47 #define ECRYPTFS_VERSIONING_PASSPHRASE 0x00000001 47 #define ECRYPTFS_VERSIONING_PASSPHRASE 0x00000001
48 #define ECRYPTFS_VERSIONING_PUBKEY 0x00000002 48 #define ECRYPTFS_VERSIONING_PUBKEY 0x00000002
49 #define ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH 0x00000004 49 #define ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH 0x00000004
50 #define ECRYPTFS_VERSIONING_POLICY 0x00000008 50 #define ECRYPTFS_VERSIONING_POLICY 0x00000008
51 #define ECRYPTFS_VERSIONING_XATTR 0x00000010 51 #define ECRYPTFS_VERSIONING_XATTR 0x00000010
52 #define ECRYPTFS_VERSIONING_MULTKEY 0x00000020 52 #define ECRYPTFS_VERSIONING_MULTKEY 0x00000020
53 #define ECRYPTFS_VERSIONING_DEVMISC 0x00000040 53 #define ECRYPTFS_VERSIONING_DEVMISC 0x00000040
54 #define ECRYPTFS_VERSIONING_HMAC 0x00000080 54 #define ECRYPTFS_VERSIONING_HMAC 0x00000080
55 #define ECRYPTFS_VERSIONING_FILENAME_ENCRYPTION 0x00000100 55 #define ECRYPTFS_VERSIONING_FILENAME_ENCRYPTION 0x00000100
56 #define ECRYPTFS_VERSIONING_GCM 0x00000200 56 #define ECRYPTFS_VERSIONING_GCM 0x00000200
57 #define ECRYPTFS_VERSIONING_MASK (ECRYPTFS_VERSIONING_PASSPHRASE \ 57 #define ECRYPTFS_VERSIONING_MASK (ECRYPTFS_VERSIONING_PASSPHRASE \
58 | ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH \ 58 | ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH \
59 | ECRYPTFS_VERSIONING_PUBKEY \ 59 | ECRYPTFS_VERSIONING_PUBKEY \
60 | ECRYPTFS_VERSIONING_XATTR \ 60 | ECRYPTFS_VERSIONING_XATTR \
61 | ECRYPTFS_VERSIONING_MULTKEY \ 61 | ECRYPTFS_VERSIONING_MULTKEY \
62 | ECRYPTFS_VERSIONING_DEVMISC \ 62 | ECRYPTFS_VERSIONING_DEVMISC \
63 | ECRYPTFS_VERSIONING_FILENAME_ENCRYPTION) 63 | ECRYPTFS_VERSIONING_FILENAME_ENCRYPTION)
64 #define ECRYPTFS_MAX_PASSWORD_LENGTH 64 64 #define ECRYPTFS_MAX_PASSWORD_LENGTH 64
65 #define ECRYPTFS_MAX_PASSPHRASE_BYTES ECRYPTFS_MAX_PASSWORD_LENGTH 65 #define ECRYPTFS_MAX_PASSPHRASE_BYTES ECRYPTFS_MAX_PASSWORD_LENGTH
66 #define ECRYPTFS_SALT_SIZE 8 66 #define ECRYPTFS_SALT_SIZE 8
67 #define ECRYPTFS_SALT_SIZE_HEX (ECRYPTFS_SALT_SIZE*2) 67 #define ECRYPTFS_SALT_SIZE_HEX (ECRYPTFS_SALT_SIZE*2)
68 /* The original signature size is only for what is stored on disk; all 68 /* The original signature size is only for what is stored on disk; all
69 * in-memory representations are expanded hex, so it better adapted to 69 * in-memory representations are expanded hex, so it better adapted to
70 * be passed around or referenced on the command line */ 70 * be passed around or referenced on the command line */
71 #define ECRYPTFS_SIG_SIZE 8 71 #define ECRYPTFS_SIG_SIZE 8
72 #define ECRYPTFS_SIG_SIZE_HEX (ECRYPTFS_SIG_SIZE*2) 72 #define ECRYPTFS_SIG_SIZE_HEX (ECRYPTFS_SIG_SIZE*2)
73 #define ECRYPTFS_PASSWORD_SIG_SIZE ECRYPTFS_SIG_SIZE_HEX 73 #define ECRYPTFS_PASSWORD_SIG_SIZE ECRYPTFS_SIG_SIZE_HEX
74 #define ECRYPTFS_MAX_KEY_BYTES 64 74 #define ECRYPTFS_MAX_KEY_BYTES 64
75 #define ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES 512 75 #define ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES 512
76 #define ECRYPTFS_DEFAULT_IV_BYTES 16 76 #define ECRYPTFS_DEFAULT_IV_BYTES 16
77 #define ECRYPTFS_FILE_VERSION 0x03 77 #define ECRYPTFS_FILE_VERSION 0x03
78 #define ECRYPTFS_DEFAULT_EXTENT_SIZE 4096 78 #define ECRYPTFS_DEFAULT_EXTENT_SIZE 4096
79 #define ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE 8192 79 #define ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE 8192
80 #define ECRYPTFS_DEFAULT_MSG_CTX_ELEMS 32 80 #define ECRYPTFS_DEFAULT_MSG_CTX_ELEMS 32
81 #define ECRYPTFS_DEFAULT_SEND_TIMEOUT HZ 81 #define ECRYPTFS_DEFAULT_SEND_TIMEOUT HZ
82 #define ECRYPTFS_MAX_MSG_CTX_TTL (HZ*3) 82 #define ECRYPTFS_MAX_MSG_CTX_TTL (HZ*3)
83 #define ECRYPTFS_MAX_PKI_NAME_BYTES 16 83 #define ECRYPTFS_MAX_PKI_NAME_BYTES 16
84 #define ECRYPTFS_DEFAULT_NUM_USERS 4 84 #define ECRYPTFS_DEFAULT_NUM_USERS 4
85 #define ECRYPTFS_MAX_NUM_USERS 32768 85 #define ECRYPTFS_MAX_NUM_USERS 32768
86 #define ECRYPTFS_XATTR_NAME "user.ecryptfs" 86 #define ECRYPTFS_XATTR_NAME "user.ecryptfs"
87 87
88 #define RFC2440_CIPHER_DES3_EDE 0x02 88 #define RFC2440_CIPHER_DES3_EDE 0x02
89 #define RFC2440_CIPHER_CAST_5 0x03 89 #define RFC2440_CIPHER_CAST_5 0x03
90 #define RFC2440_CIPHER_BLOWFISH 0x04 90 #define RFC2440_CIPHER_BLOWFISH 0x04
91 #define RFC2440_CIPHER_AES_128 0x07 91 #define RFC2440_CIPHER_AES_128 0x07
92 #define RFC2440_CIPHER_AES_192 0x08 92 #define RFC2440_CIPHER_AES_192 0x08
93 #define RFC2440_CIPHER_AES_256 0x09 93 #define RFC2440_CIPHER_AES_256 0x09
94 #define RFC2440_CIPHER_TWOFISH 0x0a 94 #define RFC2440_CIPHER_TWOFISH 0x0a
95 #define RFC2440_CIPHER_CAST_6 0x0b 95 #define RFC2440_CIPHER_CAST_6 0x0b
96 96
97 #define RFC2440_CIPHER_RSA 0x01 97 #define RFC2440_CIPHER_RSA 0x01
98 98
99 /** 99 /**
100 * For convenience, we may need to pass around the encrypted session 100 * For convenience, we may need to pass around the encrypted session
101 * key between kernel and userspace because the authentication token 101 * key between kernel and userspace because the authentication token
102 * may not be extractable. For example, the TPM may not release the 102 * may not be extractable. For example, the TPM may not release the
103 * private key, instead requiring the encrypted data and returning the 103 * private key, instead requiring the encrypted data and returning the
104 * decrypted data. 104 * decrypted data.
105 */ 105 */
106 struct ecryptfs_session_key { 106 struct ecryptfs_session_key {
107 #define ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT 0x00000001 107 #define ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT 0x00000001
108 #define ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT 0x00000002 108 #define ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT 0x00000002
109 #define ECRYPTFS_CONTAINS_DECRYPTED_KEY 0x00000004 109 #define ECRYPTFS_CONTAINS_DECRYPTED_KEY 0x00000004
110 #define ECRYPTFS_CONTAINS_ENCRYPTED_KEY 0x00000008 110 #define ECRYPTFS_CONTAINS_ENCRYPTED_KEY 0x00000008
111 u32 flags; 111 u32 flags;
112 u32 encrypted_key_size; 112 u32 encrypted_key_size;
113 u32 decrypted_key_size; 113 u32 decrypted_key_size;
114 u8 encrypted_key[ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES]; 114 u8 encrypted_key[ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES];
115 u8 decrypted_key[ECRYPTFS_MAX_KEY_BYTES]; 115 u8 decrypted_key[ECRYPTFS_MAX_KEY_BYTES];
116 }; 116 };
117 117
118 struct ecryptfs_password { 118 struct ecryptfs_password {
119 u32 password_bytes; 119 u32 password_bytes;
120 s32 hash_algo; 120 s32 hash_algo;
121 u32 hash_iterations; 121 u32 hash_iterations;
122 u32 session_key_encryption_key_bytes; 122 u32 session_key_encryption_key_bytes;
123 #define ECRYPTFS_PERSISTENT_PASSWORD 0x01 123 #define ECRYPTFS_PERSISTENT_PASSWORD 0x01
124 #define ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET 0x02 124 #define ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET 0x02
125 u32 flags; 125 u32 flags;
126 /* Iterated-hash concatenation of salt and passphrase */ 126 /* Iterated-hash concatenation of salt and passphrase */
127 u8 session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; 127 u8 session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
128 u8 signature[ECRYPTFS_PASSWORD_SIG_SIZE + 1]; 128 u8 signature[ECRYPTFS_PASSWORD_SIG_SIZE + 1];
129 /* Always in expanded hex */ 129 /* Always in expanded hex */
130 u8 salt[ECRYPTFS_SALT_SIZE]; 130 u8 salt[ECRYPTFS_SALT_SIZE];
131 }; 131 };
132 132
133 enum ecryptfs_token_types {ECRYPTFS_PASSWORD, ECRYPTFS_PRIVATE_KEY}; 133 enum ecryptfs_token_types {ECRYPTFS_PASSWORD, ECRYPTFS_PRIVATE_KEY};
134 134
135 struct ecryptfs_private_key { 135 struct ecryptfs_private_key {
136 u32 key_size; 136 u32 key_size;
137 u32 data_len; 137 u32 data_len;
138 u8 signature[ECRYPTFS_PASSWORD_SIG_SIZE + 1]; 138 u8 signature[ECRYPTFS_PASSWORD_SIG_SIZE + 1];
139 char pki_type[ECRYPTFS_MAX_PKI_NAME_BYTES + 1]; 139 char pki_type[ECRYPTFS_MAX_PKI_NAME_BYTES + 1];
140 u8 data[]; 140 u8 data[];
141 }; 141 };
142 142
143 /* May be a password or a private key */ 143 /* May be a password or a private key */
144 struct ecryptfs_auth_tok { 144 struct ecryptfs_auth_tok {
145 u16 version; /* 8-bit major and 8-bit minor */ 145 u16 version; /* 8-bit major and 8-bit minor */
146 u16 token_type; 146 u16 token_type;
147 #define ECRYPTFS_ENCRYPT_ONLY 0x00000001 147 #define ECRYPTFS_ENCRYPT_ONLY 0x00000001
148 u32 flags; 148 u32 flags;
149 struct ecryptfs_session_key session_key; 149 struct ecryptfs_session_key session_key;
150 u8 reserved[32]; 150 u8 reserved[32];
151 union { 151 union {
152 struct ecryptfs_password password; 152 struct ecryptfs_password password;
153 struct ecryptfs_private_key private_key; 153 struct ecryptfs_private_key private_key;
154 } token; 154 } token;
155 } __attribute__ ((packed)); 155 } __attribute__ ((packed));
156 156
157 void ecryptfs_dump_auth_tok(struct ecryptfs_auth_tok *auth_tok); 157 void ecryptfs_dump_auth_tok(struct ecryptfs_auth_tok *auth_tok);
158 extern void ecryptfs_to_hex(char *dst, char *src, size_t src_size); 158 extern void ecryptfs_to_hex(char *dst, char *src, size_t src_size);
159 extern void ecryptfs_from_hex(char *dst, char *src, int dst_size); 159 extern void ecryptfs_from_hex(char *dst, char *src, int dst_size);
160 160
161 struct ecryptfs_key_record { 161 struct ecryptfs_key_record {
162 unsigned char type; 162 unsigned char type;
163 size_t enc_key_size; 163 size_t enc_key_size;
164 unsigned char sig[ECRYPTFS_SIG_SIZE]; 164 unsigned char sig[ECRYPTFS_SIG_SIZE];
165 unsigned char enc_key[ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES]; 165 unsigned char enc_key[ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES];
166 }; 166 };
167 167
168 struct ecryptfs_auth_tok_list { 168 struct ecryptfs_auth_tok_list {
169 struct ecryptfs_auth_tok *auth_tok; 169 struct ecryptfs_auth_tok *auth_tok;
170 struct list_head list; 170 struct list_head list;
171 }; 171 };
172 172
173 struct ecryptfs_crypt_stat; 173 struct ecryptfs_crypt_stat;
174 struct ecryptfs_mount_crypt_stat; 174 struct ecryptfs_mount_crypt_stat;
175 175
176 struct ecryptfs_page_crypt_context { 176 struct ecryptfs_page_crypt_context {
177 struct page *page; 177 struct page *page;
178 #define ECRYPTFS_PREPARE_COMMIT_MODE 0 178 #define ECRYPTFS_PREPARE_COMMIT_MODE 0
179 #define ECRYPTFS_WRITEPAGE_MODE 1 179 #define ECRYPTFS_WRITEPAGE_MODE 1
180 unsigned int mode; 180 unsigned int mode;
181 union { 181 union {
182 struct file *lower_file; 182 struct file *lower_file;
183 struct writeback_control *wbc; 183 struct writeback_control *wbc;
184 } param; 184 } param;
185 }; 185 };
186 186
187 static inline struct ecryptfs_auth_tok * 187 static inline struct ecryptfs_auth_tok *
188 ecryptfs_get_key_payload_data(struct key *key) 188 ecryptfs_get_key_payload_data(struct key *key)
189 { 189 {
190 return (struct ecryptfs_auth_tok *) 190 return (struct ecryptfs_auth_tok *)
191 (((struct user_key_payload*)key->payload.data)->data); 191 (((struct user_key_payload*)key->payload.data)->data);
192 } 192 }
193 193
194 #define ECRYPTFS_SUPER_MAGIC 0xf15f 194 #define ECRYPTFS_SUPER_MAGIC 0xf15f
195 #define ECRYPTFS_MAX_KEYSET_SIZE 1024 195 #define ECRYPTFS_MAX_KEYSET_SIZE 1024
196 #define ECRYPTFS_MAX_CIPHER_NAME_SIZE 32 196 #define ECRYPTFS_MAX_CIPHER_NAME_SIZE 32
197 #define ECRYPTFS_MAX_NUM_ENC_KEYS 64 197 #define ECRYPTFS_MAX_NUM_ENC_KEYS 64
198 #define ECRYPTFS_MAX_IV_BYTES 16 /* 128 bits */ 198 #define ECRYPTFS_MAX_IV_BYTES 16 /* 128 bits */
199 #define ECRYPTFS_SALT_BYTES 2 199 #define ECRYPTFS_SALT_BYTES 2
200 #define MAGIC_ECRYPTFS_MARKER 0x3c81b7f5 200 #define MAGIC_ECRYPTFS_MARKER 0x3c81b7f5
201 #define MAGIC_ECRYPTFS_MARKER_SIZE_BYTES 8 /* 4*2 */ 201 #define MAGIC_ECRYPTFS_MARKER_SIZE_BYTES 8 /* 4*2 */
202 #define ECRYPTFS_FILE_SIZE_BYTES (sizeof(u64)) 202 #define ECRYPTFS_FILE_SIZE_BYTES (sizeof(u64))
203 #define ECRYPTFS_DEFAULT_CIPHER "aes" 203 #define ECRYPTFS_DEFAULT_CIPHER "aes"
204 #define ECRYPTFS_DEFAULT_KEY_BYTES 16 204 #define ECRYPTFS_DEFAULT_KEY_BYTES 16
205 #define ECRYPTFS_DEFAULT_HASH "md5" 205 #define ECRYPTFS_DEFAULT_HASH "md5"
206 #define ECRYPTFS_TAG_70_DIGEST ECRYPTFS_DEFAULT_HASH 206 #define ECRYPTFS_TAG_70_DIGEST ECRYPTFS_DEFAULT_HASH
207 #define ECRYPTFS_TAG_1_PACKET_TYPE 0x01 207 #define ECRYPTFS_TAG_1_PACKET_TYPE 0x01
208 #define ECRYPTFS_TAG_3_PACKET_TYPE 0x8C 208 #define ECRYPTFS_TAG_3_PACKET_TYPE 0x8C
209 #define ECRYPTFS_TAG_11_PACKET_TYPE 0xED 209 #define ECRYPTFS_TAG_11_PACKET_TYPE 0xED
210 #define ECRYPTFS_TAG_64_PACKET_TYPE 0x40 210 #define ECRYPTFS_TAG_64_PACKET_TYPE 0x40
211 #define ECRYPTFS_TAG_65_PACKET_TYPE 0x41 211 #define ECRYPTFS_TAG_65_PACKET_TYPE 0x41
212 #define ECRYPTFS_TAG_66_PACKET_TYPE 0x42 212 #define ECRYPTFS_TAG_66_PACKET_TYPE 0x42
213 #define ECRYPTFS_TAG_67_PACKET_TYPE 0x43 213 #define ECRYPTFS_TAG_67_PACKET_TYPE 0x43
214 #define ECRYPTFS_TAG_70_PACKET_TYPE 0x46 /* FNEK-encrypted filename 214 #define ECRYPTFS_TAG_70_PACKET_TYPE 0x46 /* FNEK-encrypted filename
215 * as dentry name */ 215 * as dentry name */
216 #define ECRYPTFS_TAG_71_PACKET_TYPE 0x47 /* FNEK-encrypted filename in 216 #define ECRYPTFS_TAG_71_PACKET_TYPE 0x47 /* FNEK-encrypted filename in
217 * metadata */ 217 * metadata */
218 #define ECRYPTFS_TAG_72_PACKET_TYPE 0x48 /* FEK-encrypted filename as 218 #define ECRYPTFS_TAG_72_PACKET_TYPE 0x48 /* FEK-encrypted filename as
219 * dentry name */ 219 * dentry name */
220 #define ECRYPTFS_TAG_73_PACKET_TYPE 0x49 /* FEK-encrypted filename as 220 #define ECRYPTFS_TAG_73_PACKET_TYPE 0x49 /* FEK-encrypted filename as
221 * metadata */ 221 * metadata */
222 /* Constraint: ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES >= 222 /* Constraint: ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES >=
223 * ECRYPTFS_MAX_IV_BYTES */ 223 * ECRYPTFS_MAX_IV_BYTES */
224 #define ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 16 224 #define ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 16
225 #define ECRYPTFS_NON_NULL 0x42 /* A reasonable substitute for NULL */ 225 #define ECRYPTFS_NON_NULL 0x42 /* A reasonable substitute for NULL */
226 #define MD5_DIGEST_SIZE 16 226 #define MD5_DIGEST_SIZE 16
227 #define ECRYPTFS_TAG_70_DIGEST_SIZE MD5_DIGEST_SIZE 227 #define ECRYPTFS_TAG_70_DIGEST_SIZE MD5_DIGEST_SIZE
228 #define ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX "ECRYPTFS_FEK_ENCRYPTED." 228 #define ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX "ECRYPTFS_FEK_ENCRYPTED."
229 #define ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE 23 229 #define ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE 23
230 #define ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX "ECRYPTFS_FNEK_ENCRYPTED." 230 #define ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX "ECRYPTFS_FNEK_ENCRYPTED."
231 #define ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE 24 231 #define ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE 24
232 #define ECRYPTFS_ENCRYPTED_DENTRY_NAME_LEN (18 + 1 + 4 + 1 + 32) 232 #define ECRYPTFS_ENCRYPTED_DENTRY_NAME_LEN (18 + 1 + 4 + 1 + 32)
233 233
234 struct ecryptfs_key_sig { 234 struct ecryptfs_key_sig {
235 struct list_head crypt_stat_list; 235 struct list_head crypt_stat_list;
236 char keysig[ECRYPTFS_SIG_SIZE_HEX]; 236 char keysig[ECRYPTFS_SIG_SIZE_HEX];
237 }; 237 };
238 238
239 struct ecryptfs_filename { 239 struct ecryptfs_filename {
240 struct list_head crypt_stat_list; 240 struct list_head crypt_stat_list;
241 #define ECRYPTFS_FILENAME_CONTAINS_DECRYPTED 0x00000001 241 #define ECRYPTFS_FILENAME_CONTAINS_DECRYPTED 0x00000001
242 u32 flags; 242 u32 flags;
243 u32 seq_no; 243 u32 seq_no;
244 char *filename; 244 char *filename;
245 char *encrypted_filename; 245 char *encrypted_filename;
246 size_t filename_size; 246 size_t filename_size;
247 size_t encrypted_filename_size; 247 size_t encrypted_filename_size;
248 char fnek_sig[ECRYPTFS_SIG_SIZE_HEX]; 248 char fnek_sig[ECRYPTFS_SIG_SIZE_HEX];
249 char dentry_name[ECRYPTFS_ENCRYPTED_DENTRY_NAME_LEN + 1]; 249 char dentry_name[ECRYPTFS_ENCRYPTED_DENTRY_NAME_LEN + 1];
250 }; 250 };
251 251
252 /** 252 /**
253 * This is the primary struct associated with each encrypted file. 253 * This is the primary struct associated with each encrypted file.
254 * 254 *
255 * TODO: cache align/pack? 255 * TODO: cache align/pack?
256 */ 256 */
257 struct ecryptfs_crypt_stat { 257 struct ecryptfs_crypt_stat {
258 #define ECRYPTFS_STRUCT_INITIALIZED 0x00000001 258 #define ECRYPTFS_STRUCT_INITIALIZED 0x00000001
259 #define ECRYPTFS_POLICY_APPLIED 0x00000002 259 #define ECRYPTFS_POLICY_APPLIED 0x00000002
260 #define ECRYPTFS_NEW_FILE 0x00000004 260 #define ECRYPTFS_NEW_FILE 0x00000004
261 #define ECRYPTFS_ENCRYPTED 0x00000008 261 #define ECRYPTFS_ENCRYPTED 0x00000008
262 #define ECRYPTFS_SECURITY_WARNING 0x00000010 262 #define ECRYPTFS_SECURITY_WARNING 0x00000010
263 #define ECRYPTFS_ENABLE_HMAC 0x00000020 263 #define ECRYPTFS_ENABLE_HMAC 0x00000020
264 #define ECRYPTFS_ENCRYPT_IV_PAGES 0x00000040 264 #define ECRYPTFS_ENCRYPT_IV_PAGES 0x00000040
265 #define ECRYPTFS_KEY_VALID 0x00000080 265 #define ECRYPTFS_KEY_VALID 0x00000080
266 #define ECRYPTFS_METADATA_IN_XATTR 0x00000100 266 #define ECRYPTFS_METADATA_IN_XATTR 0x00000100
267 #define ECRYPTFS_VIEW_AS_ENCRYPTED 0x00000200 267 #define ECRYPTFS_VIEW_AS_ENCRYPTED 0x00000200
268 #define ECRYPTFS_KEY_SET 0x00000400 268 #define ECRYPTFS_KEY_SET 0x00000400
269 #define ECRYPTFS_ENCRYPT_FILENAMES 0x00000800 269 #define ECRYPTFS_ENCRYPT_FILENAMES 0x00000800
270 #define ECRYPTFS_ENCFN_USE_MOUNT_FNEK 0x00001000 270 #define ECRYPTFS_ENCFN_USE_MOUNT_FNEK 0x00001000
271 #define ECRYPTFS_ENCFN_USE_FEK 0x00002000 271 #define ECRYPTFS_ENCFN_USE_FEK 0x00002000
272 u32 flags; 272 u32 flags;
273 unsigned int file_version; 273 unsigned int file_version;
274 size_t iv_bytes; 274 size_t iv_bytes;
275 size_t num_header_bytes_at_front; 275 size_t num_header_bytes_at_front;
276 size_t extent_size; /* Data extent size; default is 4096 */ 276 size_t extent_size; /* Data extent size; default is 4096 */
277 size_t key_size; 277 size_t key_size;
278 size_t extent_shift; 278 size_t extent_shift;
279 unsigned int extent_mask; 279 unsigned int extent_mask;
280 struct ecryptfs_mount_crypt_stat *mount_crypt_stat; 280 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
281 struct crypto_blkcipher *tfm; 281 struct crypto_blkcipher *tfm;
282 struct crypto_hash *hash_tfm; /* Crypto context for generating 282 struct crypto_hash *hash_tfm; /* Crypto context for generating
283 * the initialization vectors */ 283 * the initialization vectors */
284 unsigned char cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE]; 284 unsigned char cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE];
285 unsigned char key[ECRYPTFS_MAX_KEY_BYTES]; 285 unsigned char key[ECRYPTFS_MAX_KEY_BYTES];
286 unsigned char root_iv[ECRYPTFS_MAX_IV_BYTES]; 286 unsigned char root_iv[ECRYPTFS_MAX_IV_BYTES];
287 struct list_head keysig_list; 287 struct list_head keysig_list;
288 struct mutex keysig_list_mutex; 288 struct mutex keysig_list_mutex;
289 struct mutex cs_tfm_mutex; 289 struct mutex cs_tfm_mutex;
290 struct mutex cs_hash_tfm_mutex; 290 struct mutex cs_hash_tfm_mutex;
291 struct mutex cs_mutex; 291 struct mutex cs_mutex;
292 }; 292 };
293 293
294 /* inode private data. */ 294 /* inode private data. */
295 struct ecryptfs_inode_info { 295 struct ecryptfs_inode_info {
296 struct inode vfs_inode; 296 struct inode vfs_inode;
297 struct inode *wii_inode; 297 struct inode *wii_inode;
298 struct file *lower_file; 298 struct file *lower_file;
299 struct mutex lower_file_mutex; 299 struct mutex lower_file_mutex;
300 struct ecryptfs_crypt_stat crypt_stat; 300 struct ecryptfs_crypt_stat crypt_stat;
301 }; 301 };
302 302
303 /* dentry private data. Each dentry must keep track of a lower 303 /* dentry private data. Each dentry must keep track of a lower
304 * vfsmount too. */ 304 * vfsmount too. */
305 struct ecryptfs_dentry_info { 305 struct ecryptfs_dentry_info {
306 struct path lower_path; 306 struct path lower_path;
307 struct ecryptfs_crypt_stat *crypt_stat; 307 struct ecryptfs_crypt_stat *crypt_stat;
308 }; 308 };
309 309
310 /** 310 /**
311 * ecryptfs_global_auth_tok - A key used to encrypt all new files under the mountpoint 311 * ecryptfs_global_auth_tok - A key used to encrypt all new files under the mountpoint
312 * @flags: Status flags 312 * @flags: Status flags
313 * @mount_crypt_stat_list: These auth_toks hang off the mount-wide 313 * @mount_crypt_stat_list: These auth_toks hang off the mount-wide
314 * cryptographic context. Every time a new 314 * cryptographic context. Every time a new
315 * inode comes into existence, eCryptfs copies 315 * inode comes into existence, eCryptfs copies
316 * the auth_toks on that list to the set of 316 * the auth_toks on that list to the set of
317 * auth_toks on the inode's crypt_stat 317 * auth_toks on the inode's crypt_stat
318 * @global_auth_tok_key: The key from the user's keyring for the sig 318 * @global_auth_tok_key: The key from the user's keyring for the sig
319 * @global_auth_tok: The key contents 319 * @global_auth_tok: The key contents
320 * @sig: The key identifier 320 * @sig: The key identifier
321 * 321 *
322 * ecryptfs_global_auth_tok structs refer to authentication token keys 322 * ecryptfs_global_auth_tok structs refer to authentication token keys
323 * in the user keyring that apply to newly created files. A list of 323 * in the user keyring that apply to newly created files. A list of
324 * these objects hangs off of the mount_crypt_stat struct for any 324 * these objects hangs off of the mount_crypt_stat struct for any
325 * given eCryptfs mount. This struct maintains a reference to both the 325 * given eCryptfs mount. This struct maintains a reference to both the
326 * key contents and the key itself so that the key can be put on 326 * key contents and the key itself so that the key can be put on
327 * unmount. 327 * unmount.
328 */ 328 */
329 struct ecryptfs_global_auth_tok { 329 struct ecryptfs_global_auth_tok {
330 #define ECRYPTFS_AUTH_TOK_INVALID 0x00000001 330 #define ECRYPTFS_AUTH_TOK_INVALID 0x00000001
331 #define ECRYPTFS_AUTH_TOK_FNEK 0x00000002
331 u32 flags; 332 u32 flags;
332 struct list_head mount_crypt_stat_list; 333 struct list_head mount_crypt_stat_list;
333 struct key *global_auth_tok_key; 334 struct key *global_auth_tok_key;
334 struct ecryptfs_auth_tok *global_auth_tok; 335 struct ecryptfs_auth_tok *global_auth_tok;
335 unsigned char sig[ECRYPTFS_SIG_SIZE_HEX + 1]; 336 unsigned char sig[ECRYPTFS_SIG_SIZE_HEX + 1];
336 }; 337 };
337 338
338 /** 339 /**
339 * ecryptfs_key_tfm - Persistent key tfm 340 * ecryptfs_key_tfm - Persistent key tfm
340 * @key_tfm: crypto API handle to the key 341 * @key_tfm: crypto API handle to the key
341 * @key_size: Key size in bytes 342 * @key_size: Key size in bytes
342 * @key_tfm_mutex: Mutex to ensure only one operation in eCryptfs is 343 * @key_tfm_mutex: Mutex to ensure only one operation in eCryptfs is
343 * using the persistent TFM at any point in time 344 * using the persistent TFM at any point in time
344 * @key_tfm_list: Handle to hang this off the module-wide TFM list 345 * @key_tfm_list: Handle to hang this off the module-wide TFM list
345 * @cipher_name: String name for the cipher for this TFM 346 * @cipher_name: String name for the cipher for this TFM
346 * 347 *
347 * Typically, eCryptfs will use the same ciphers repeatedly throughout 348 * Typically, eCryptfs will use the same ciphers repeatedly throughout
348 * the course of its operations. In order to avoid unnecessarily 349 * the course of its operations. In order to avoid unnecessarily
349 * destroying and initializing the same cipher repeatedly, eCryptfs 350 * destroying and initializing the same cipher repeatedly, eCryptfs
350 * keeps a list of crypto API contexts around to use when needed. 351 * keeps a list of crypto API contexts around to use when needed.
351 */ 352 */
352 struct ecryptfs_key_tfm { 353 struct ecryptfs_key_tfm {
353 struct crypto_blkcipher *key_tfm; 354 struct crypto_blkcipher *key_tfm;
354 size_t key_size; 355 size_t key_size;
355 struct mutex key_tfm_mutex; 356 struct mutex key_tfm_mutex;
356 struct list_head key_tfm_list; 357 struct list_head key_tfm_list;
357 unsigned char cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1]; 358 unsigned char cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1];
358 }; 359 };
359 360
360 extern struct mutex key_tfm_list_mutex; 361 extern struct mutex key_tfm_list_mutex;
361 362
362 /** 363 /**
363 * This struct is to enable a mount-wide passphrase/salt combo. This 364 * This struct is to enable a mount-wide passphrase/salt combo. This
364 * is more or less a stopgap to provide similar functionality to other 365 * is more or less a stopgap to provide similar functionality to other
365 * crypto filesystems like EncFS or CFS until full policy support is 366 * crypto filesystems like EncFS or CFS until full policy support is
366 * implemented in eCryptfs. 367 * implemented in eCryptfs.
367 */ 368 */
368 struct ecryptfs_mount_crypt_stat { 369 struct ecryptfs_mount_crypt_stat {
369 /* Pointers to memory we do not own, do not free these */ 370 /* Pointers to memory we do not own, do not free these */
370 #define ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED 0x00000001 371 #define ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED 0x00000001
371 #define ECRYPTFS_XATTR_METADATA_ENABLED 0x00000002 372 #define ECRYPTFS_XATTR_METADATA_ENABLED 0x00000002
372 #define ECRYPTFS_ENCRYPTED_VIEW_ENABLED 0x00000004 373 #define ECRYPTFS_ENCRYPTED_VIEW_ENABLED 0x00000004
373 #define ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED 0x00000008 374 #define ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED 0x00000008
374 #define ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES 0x00000010 375 #define ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES 0x00000010
375 #define ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK 0x00000020 376 #define ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK 0x00000020
376 #define ECRYPTFS_GLOBAL_ENCFN_USE_FEK 0x00000040 377 #define ECRYPTFS_GLOBAL_ENCFN_USE_FEK 0x00000040
377 u32 flags; 378 u32 flags;
378 struct list_head global_auth_tok_list; 379 struct list_head global_auth_tok_list;
379 struct mutex global_auth_tok_list_mutex; 380 struct mutex global_auth_tok_list_mutex;
380 size_t num_global_auth_toks; 381 size_t num_global_auth_toks;
381 size_t global_default_cipher_key_size; 382 size_t global_default_cipher_key_size;
382 size_t global_default_fn_cipher_key_bytes; 383 size_t global_default_fn_cipher_key_bytes;
383 unsigned char global_default_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE 384 unsigned char global_default_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE
384 + 1]; 385 + 1];
385 unsigned char global_default_fn_cipher_name[ 386 unsigned char global_default_fn_cipher_name[
386 ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1]; 387 ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1];
387 char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX + 1]; 388 char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX + 1];
388 }; 389 };
389 390
390 /* superblock private data. */ 391 /* superblock private data. */
391 struct ecryptfs_sb_info { 392 struct ecryptfs_sb_info {
392 struct super_block *wsi_sb; 393 struct super_block *wsi_sb;
393 struct ecryptfs_mount_crypt_stat mount_crypt_stat; 394 struct ecryptfs_mount_crypt_stat mount_crypt_stat;
394 }; 395 };
395 396
396 /* file private data. */ 397 /* file private data. */
397 struct ecryptfs_file_info { 398 struct ecryptfs_file_info {
398 struct file *wfi_file; 399 struct file *wfi_file;
399 struct ecryptfs_crypt_stat *crypt_stat; 400 struct ecryptfs_crypt_stat *crypt_stat;
400 }; 401 };
401 402
402 /* auth_tok <=> encrypted_session_key mappings */ 403 /* auth_tok <=> encrypted_session_key mappings */
403 struct ecryptfs_auth_tok_list_item { 404 struct ecryptfs_auth_tok_list_item {
404 unsigned char encrypted_session_key[ECRYPTFS_MAX_KEY_BYTES]; 405 unsigned char encrypted_session_key[ECRYPTFS_MAX_KEY_BYTES];
405 struct list_head list; 406 struct list_head list;
406 struct ecryptfs_auth_tok auth_tok; 407 struct ecryptfs_auth_tok auth_tok;
407 }; 408 };
408 409
409 struct ecryptfs_message { 410 struct ecryptfs_message {
410 /* Can never be greater than ecryptfs_message_buf_len */ 411 /* Can never be greater than ecryptfs_message_buf_len */
411 /* Used to find the parent msg_ctx */ 412 /* Used to find the parent msg_ctx */
412 /* Inherits from msg_ctx->index */ 413 /* Inherits from msg_ctx->index */
413 u32 index; 414 u32 index;
414 u32 data_len; 415 u32 data_len;
415 u8 data[]; 416 u8 data[];
416 }; 417 };
417 418
418 struct ecryptfs_msg_ctx { 419 struct ecryptfs_msg_ctx {
419 #define ECRYPTFS_MSG_CTX_STATE_FREE 0x01 420 #define ECRYPTFS_MSG_CTX_STATE_FREE 0x01
420 #define ECRYPTFS_MSG_CTX_STATE_PENDING 0x02 421 #define ECRYPTFS_MSG_CTX_STATE_PENDING 0x02
421 #define ECRYPTFS_MSG_CTX_STATE_DONE 0x03 422 #define ECRYPTFS_MSG_CTX_STATE_DONE 0x03
422 #define ECRYPTFS_MSG_CTX_STATE_NO_REPLY 0x04 423 #define ECRYPTFS_MSG_CTX_STATE_NO_REPLY 0x04
423 u8 state; 424 u8 state;
424 #define ECRYPTFS_MSG_HELO 100 425 #define ECRYPTFS_MSG_HELO 100
425 #define ECRYPTFS_MSG_QUIT 101 426 #define ECRYPTFS_MSG_QUIT 101
426 #define ECRYPTFS_MSG_REQUEST 102 427 #define ECRYPTFS_MSG_REQUEST 102
427 #define ECRYPTFS_MSG_RESPONSE 103 428 #define ECRYPTFS_MSG_RESPONSE 103
428 u8 type; 429 u8 type;
429 u32 index; 430 u32 index;
430 /* Counter converts to a sequence number. Each message sent 431 /* Counter converts to a sequence number. Each message sent
431 * out for which we expect a response has an associated 432 * out for which we expect a response has an associated
432 * sequence number. The response must have the same sequence 433 * sequence number. The response must have the same sequence
433 * number as the counter for the msg_stc for the message to be 434 * number as the counter for the msg_stc for the message to be
434 * valid. */ 435 * valid. */
435 u32 counter; 436 u32 counter;
436 size_t msg_size; 437 size_t msg_size;
437 struct ecryptfs_message *msg; 438 struct ecryptfs_message *msg;
438 struct task_struct *task; 439 struct task_struct *task;
439 struct list_head node; 440 struct list_head node;
440 struct list_head daemon_out_list; 441 struct list_head daemon_out_list;
441 struct mutex mux; 442 struct mutex mux;
442 }; 443 };
443 444
444 struct ecryptfs_daemon; 445 struct ecryptfs_daemon;
445 446
446 struct ecryptfs_daemon { 447 struct ecryptfs_daemon {
447 #define ECRYPTFS_DAEMON_IN_READ 0x00000001 448 #define ECRYPTFS_DAEMON_IN_READ 0x00000001
448 #define ECRYPTFS_DAEMON_IN_POLL 0x00000002 449 #define ECRYPTFS_DAEMON_IN_POLL 0x00000002
449 #define ECRYPTFS_DAEMON_ZOMBIE 0x00000004 450 #define ECRYPTFS_DAEMON_ZOMBIE 0x00000004
450 #define ECRYPTFS_DAEMON_MISCDEV_OPEN 0x00000008 451 #define ECRYPTFS_DAEMON_MISCDEV_OPEN 0x00000008
451 u32 flags; 452 u32 flags;
452 u32 num_queued_msg_ctx; 453 u32 num_queued_msg_ctx;
453 struct pid *pid; 454 struct pid *pid;
454 uid_t euid; 455 uid_t euid;
455 struct user_namespace *user_ns; 456 struct user_namespace *user_ns;
456 struct task_struct *task; 457 struct task_struct *task;
457 struct mutex mux; 458 struct mutex mux;
458 struct list_head msg_ctx_out_queue; 459 struct list_head msg_ctx_out_queue;
459 wait_queue_head_t wait; 460 wait_queue_head_t wait;
460 struct hlist_node euid_chain; 461 struct hlist_node euid_chain;
461 }; 462 };
462 463
463 extern struct mutex ecryptfs_daemon_hash_mux; 464 extern struct mutex ecryptfs_daemon_hash_mux;
464 465
465 static inline struct ecryptfs_file_info * 466 static inline struct ecryptfs_file_info *
466 ecryptfs_file_to_private(struct file *file) 467 ecryptfs_file_to_private(struct file *file)
467 { 468 {
468 return (struct ecryptfs_file_info *)file->private_data; 469 return (struct ecryptfs_file_info *)file->private_data;
469 } 470 }
470 471
471 static inline void 472 static inline void
472 ecryptfs_set_file_private(struct file *file, 473 ecryptfs_set_file_private(struct file *file,
473 struct ecryptfs_file_info *file_info) 474 struct ecryptfs_file_info *file_info)
474 { 475 {
475 file->private_data = file_info; 476 file->private_data = file_info;
476 } 477 }
477 478
478 static inline struct file *ecryptfs_file_to_lower(struct file *file) 479 static inline struct file *ecryptfs_file_to_lower(struct file *file)
479 { 480 {
480 return ((struct ecryptfs_file_info *)file->private_data)->wfi_file; 481 return ((struct ecryptfs_file_info *)file->private_data)->wfi_file;
481 } 482 }
482 483
483 static inline void 484 static inline void
484 ecryptfs_set_file_lower(struct file *file, struct file *lower_file) 485 ecryptfs_set_file_lower(struct file *file, struct file *lower_file)
485 { 486 {
486 ((struct ecryptfs_file_info *)file->private_data)->wfi_file = 487 ((struct ecryptfs_file_info *)file->private_data)->wfi_file =
487 lower_file; 488 lower_file;
488 } 489 }
489 490
490 static inline struct ecryptfs_inode_info * 491 static inline struct ecryptfs_inode_info *
491 ecryptfs_inode_to_private(struct inode *inode) 492 ecryptfs_inode_to_private(struct inode *inode)
492 { 493 {
493 return container_of(inode, struct ecryptfs_inode_info, vfs_inode); 494 return container_of(inode, struct ecryptfs_inode_info, vfs_inode);
494 } 495 }
495 496
496 static inline struct inode *ecryptfs_inode_to_lower(struct inode *inode) 497 static inline struct inode *ecryptfs_inode_to_lower(struct inode *inode)
497 { 498 {
498 return ecryptfs_inode_to_private(inode)->wii_inode; 499 return ecryptfs_inode_to_private(inode)->wii_inode;
499 } 500 }
500 501
501 static inline void 502 static inline void
502 ecryptfs_set_inode_lower(struct inode *inode, struct inode *lower_inode) 503 ecryptfs_set_inode_lower(struct inode *inode, struct inode *lower_inode)
503 { 504 {
504 ecryptfs_inode_to_private(inode)->wii_inode = lower_inode; 505 ecryptfs_inode_to_private(inode)->wii_inode = lower_inode;
505 } 506 }
506 507
507 static inline struct ecryptfs_sb_info * 508 static inline struct ecryptfs_sb_info *
508 ecryptfs_superblock_to_private(struct super_block *sb) 509 ecryptfs_superblock_to_private(struct super_block *sb)
509 { 510 {
510 return (struct ecryptfs_sb_info *)sb->s_fs_info; 511 return (struct ecryptfs_sb_info *)sb->s_fs_info;
511 } 512 }
512 513
513 static inline void 514 static inline void
514 ecryptfs_set_superblock_private(struct super_block *sb, 515 ecryptfs_set_superblock_private(struct super_block *sb,
515 struct ecryptfs_sb_info *sb_info) 516 struct ecryptfs_sb_info *sb_info)
516 { 517 {
517 sb->s_fs_info = sb_info; 518 sb->s_fs_info = sb_info;
518 } 519 }
519 520
520 static inline struct super_block * 521 static inline struct super_block *
521 ecryptfs_superblock_to_lower(struct super_block *sb) 522 ecryptfs_superblock_to_lower(struct super_block *sb)
522 { 523 {
523 return ((struct ecryptfs_sb_info *)sb->s_fs_info)->wsi_sb; 524 return ((struct ecryptfs_sb_info *)sb->s_fs_info)->wsi_sb;
524 } 525 }
525 526
526 static inline void 527 static inline void
527 ecryptfs_set_superblock_lower(struct super_block *sb, 528 ecryptfs_set_superblock_lower(struct super_block *sb,
528 struct super_block *lower_sb) 529 struct super_block *lower_sb)
529 { 530 {
530 ((struct ecryptfs_sb_info *)sb->s_fs_info)->wsi_sb = lower_sb; 531 ((struct ecryptfs_sb_info *)sb->s_fs_info)->wsi_sb = lower_sb;
531 } 532 }
532 533
533 static inline struct ecryptfs_dentry_info * 534 static inline struct ecryptfs_dentry_info *
534 ecryptfs_dentry_to_private(struct dentry *dentry) 535 ecryptfs_dentry_to_private(struct dentry *dentry)
535 { 536 {
536 return (struct ecryptfs_dentry_info *)dentry->d_fsdata; 537 return (struct ecryptfs_dentry_info *)dentry->d_fsdata;
537 } 538 }
538 539
539 static inline void 540 static inline void
540 ecryptfs_set_dentry_private(struct dentry *dentry, 541 ecryptfs_set_dentry_private(struct dentry *dentry,
541 struct ecryptfs_dentry_info *dentry_info) 542 struct ecryptfs_dentry_info *dentry_info)
542 { 543 {
543 dentry->d_fsdata = dentry_info; 544 dentry->d_fsdata = dentry_info;
544 } 545 }
545 546
546 static inline struct dentry * 547 static inline struct dentry *
547 ecryptfs_dentry_to_lower(struct dentry *dentry) 548 ecryptfs_dentry_to_lower(struct dentry *dentry)
548 { 549 {
549 return ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.dentry; 550 return ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.dentry;
550 } 551 }
551 552
552 static inline void 553 static inline void
553 ecryptfs_set_dentry_lower(struct dentry *dentry, struct dentry *lower_dentry) 554 ecryptfs_set_dentry_lower(struct dentry *dentry, struct dentry *lower_dentry)
554 { 555 {
555 ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.dentry = 556 ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.dentry =
556 lower_dentry; 557 lower_dentry;
557 } 558 }
558 559
559 static inline struct vfsmount * 560 static inline struct vfsmount *
560 ecryptfs_dentry_to_lower_mnt(struct dentry *dentry) 561 ecryptfs_dentry_to_lower_mnt(struct dentry *dentry)
561 { 562 {
562 return ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.mnt; 563 return ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.mnt;
563 } 564 }
564 565
565 static inline void 566 static inline void
566 ecryptfs_set_dentry_lower_mnt(struct dentry *dentry, struct vfsmount *lower_mnt) 567 ecryptfs_set_dentry_lower_mnt(struct dentry *dentry, struct vfsmount *lower_mnt)
567 { 568 {
568 ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.mnt = 569 ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.mnt =
569 lower_mnt; 570 lower_mnt;
570 } 571 }
571 572
572 #define ecryptfs_printk(type, fmt, arg...) \ 573 #define ecryptfs_printk(type, fmt, arg...) \
573 __ecryptfs_printk(type "%s: " fmt, __func__, ## arg); 574 __ecryptfs_printk(type "%s: " fmt, __func__, ## arg);
574 void __ecryptfs_printk(const char *fmt, ...); 575 void __ecryptfs_printk(const char *fmt, ...);
575 576
576 extern const struct file_operations ecryptfs_main_fops; 577 extern const struct file_operations ecryptfs_main_fops;
577 extern const struct file_operations ecryptfs_dir_fops; 578 extern const struct file_operations ecryptfs_dir_fops;
578 extern const struct inode_operations ecryptfs_main_iops; 579 extern const struct inode_operations ecryptfs_main_iops;
579 extern const struct inode_operations ecryptfs_dir_iops; 580 extern const struct inode_operations ecryptfs_dir_iops;
580 extern const struct inode_operations ecryptfs_symlink_iops; 581 extern const struct inode_operations ecryptfs_symlink_iops;
581 extern const struct super_operations ecryptfs_sops; 582 extern const struct super_operations ecryptfs_sops;
582 extern struct dentry_operations ecryptfs_dops; 583 extern struct dentry_operations ecryptfs_dops;
583 extern struct address_space_operations ecryptfs_aops; 584 extern struct address_space_operations ecryptfs_aops;
584 extern int ecryptfs_verbosity; 585 extern int ecryptfs_verbosity;
585 extern unsigned int ecryptfs_message_buf_len; 586 extern unsigned int ecryptfs_message_buf_len;
586 extern signed long ecryptfs_message_wait_timeout; 587 extern signed long ecryptfs_message_wait_timeout;
587 extern unsigned int ecryptfs_number_of_users; 588 extern unsigned int ecryptfs_number_of_users;
588 589
589 extern struct kmem_cache *ecryptfs_auth_tok_list_item_cache; 590 extern struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
590 extern struct kmem_cache *ecryptfs_file_info_cache; 591 extern struct kmem_cache *ecryptfs_file_info_cache;
591 extern struct kmem_cache *ecryptfs_dentry_info_cache; 592 extern struct kmem_cache *ecryptfs_dentry_info_cache;
592 extern struct kmem_cache *ecryptfs_inode_info_cache; 593 extern struct kmem_cache *ecryptfs_inode_info_cache;
593 extern struct kmem_cache *ecryptfs_sb_info_cache; 594 extern struct kmem_cache *ecryptfs_sb_info_cache;
594 extern struct kmem_cache *ecryptfs_header_cache_1; 595 extern struct kmem_cache *ecryptfs_header_cache_1;
595 extern struct kmem_cache *ecryptfs_header_cache_2; 596 extern struct kmem_cache *ecryptfs_header_cache_2;
596 extern struct kmem_cache *ecryptfs_xattr_cache; 597 extern struct kmem_cache *ecryptfs_xattr_cache;
597 extern struct kmem_cache *ecryptfs_key_record_cache; 598 extern struct kmem_cache *ecryptfs_key_record_cache;
598 extern struct kmem_cache *ecryptfs_key_sig_cache; 599 extern struct kmem_cache *ecryptfs_key_sig_cache;
599 extern struct kmem_cache *ecryptfs_global_auth_tok_cache; 600 extern struct kmem_cache *ecryptfs_global_auth_tok_cache;
600 extern struct kmem_cache *ecryptfs_key_tfm_cache; 601 extern struct kmem_cache *ecryptfs_key_tfm_cache;
601 extern struct kmem_cache *ecryptfs_open_req_cache; 602 extern struct kmem_cache *ecryptfs_open_req_cache;
602 603
603 struct ecryptfs_open_req { 604 struct ecryptfs_open_req {
604 #define ECRYPTFS_REQ_PROCESSED 0x00000001 605 #define ECRYPTFS_REQ_PROCESSED 0x00000001
605 #define ECRYPTFS_REQ_DROPPED 0x00000002 606 #define ECRYPTFS_REQ_DROPPED 0x00000002
606 #define ECRYPTFS_REQ_ZOMBIE 0x00000004 607 #define ECRYPTFS_REQ_ZOMBIE 0x00000004
607 u32 flags; 608 u32 flags;
608 struct file **lower_file; 609 struct file **lower_file;
609 struct dentry *lower_dentry; 610 struct dentry *lower_dentry;
610 struct vfsmount *lower_mnt; 611 struct vfsmount *lower_mnt;
611 wait_queue_head_t wait; 612 wait_queue_head_t wait;
612 struct mutex mux; 613 struct mutex mux;
613 struct list_head kthread_ctl_list; 614 struct list_head kthread_ctl_list;
614 }; 615 };
615 616
616 #define ECRYPTFS_INTERPOSE_FLAG_D_ADD 0x00000001 617 #define ECRYPTFS_INTERPOSE_FLAG_D_ADD 0x00000001
617 int ecryptfs_interpose(struct dentry *hidden_dentry, 618 int ecryptfs_interpose(struct dentry *hidden_dentry,
618 struct dentry *this_dentry, struct super_block *sb, 619 struct dentry *this_dentry, struct super_block *sb,
619 u32 flags); 620 u32 flags);
620 int ecryptfs_lookup_and_interpose_lower(struct dentry *ecryptfs_dentry, 621 int ecryptfs_lookup_and_interpose_lower(struct dentry *ecryptfs_dentry,
621 struct dentry *lower_dentry, 622 struct dentry *lower_dentry,
622 struct ecryptfs_crypt_stat *crypt_stat, 623 struct ecryptfs_crypt_stat *crypt_stat,
623 struct inode *ecryptfs_dir_inode, 624 struct inode *ecryptfs_dir_inode,
624 struct nameidata *ecryptfs_nd); 625 struct nameidata *ecryptfs_nd);
625 int ecryptfs_decode_and_decrypt_filename(char **decrypted_name, 626 int ecryptfs_decode_and_decrypt_filename(char **decrypted_name,
626 size_t *decrypted_name_size, 627 size_t *decrypted_name_size,
627 struct dentry *ecryptfs_dentry, 628 struct dentry *ecryptfs_dentry,
628 const char *name, size_t name_size); 629 const char *name, size_t name_size);
629 int ecryptfs_fill_zeros(struct file *file, loff_t new_length); 630 int ecryptfs_fill_zeros(struct file *file, loff_t new_length);
630 int ecryptfs_encrypt_and_encode_filename( 631 int ecryptfs_encrypt_and_encode_filename(
631 char **encoded_name, 632 char **encoded_name,
632 size_t *encoded_name_size, 633 size_t *encoded_name_size,
633 struct ecryptfs_crypt_stat *crypt_stat, 634 struct ecryptfs_crypt_stat *crypt_stat,
634 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 635 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
635 const char *name, size_t name_size); 636 const char *name, size_t name_size);
636 struct dentry *ecryptfs_lower_dentry(struct dentry *this_dentry); 637 struct dentry *ecryptfs_lower_dentry(struct dentry *this_dentry);
637 void ecryptfs_dump_hex(char *data, int bytes); 638 void ecryptfs_dump_hex(char *data, int bytes);
638 int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg, 639 int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
639 int sg_size); 640 int sg_size);
640 int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat); 641 int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat);
641 void ecryptfs_rotate_iv(unsigned char *iv); 642 void ecryptfs_rotate_iv(unsigned char *iv);
642 void ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat); 643 void ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat);
643 void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat); 644 void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat);
644 void ecryptfs_destroy_mount_crypt_stat( 645 void ecryptfs_destroy_mount_crypt_stat(
645 struct ecryptfs_mount_crypt_stat *mount_crypt_stat); 646 struct ecryptfs_mount_crypt_stat *mount_crypt_stat);
646 int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat); 647 int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat);
647 int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode); 648 int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode);
648 int ecryptfs_encrypt_page(struct page *page); 649 int ecryptfs_encrypt_page(struct page *page);
649 int ecryptfs_decrypt_page(struct page *page); 650 int ecryptfs_decrypt_page(struct page *page);
650 int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry); 651 int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry);
651 int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry); 652 int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry);
652 int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry); 653 int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry);
653 int ecryptfs_read_and_validate_header_region(char *data, 654 int ecryptfs_read_and_validate_header_region(char *data,
654 struct inode *ecryptfs_inode); 655 struct inode *ecryptfs_inode);
655 int ecryptfs_read_and_validate_xattr_region(char *page_virt, 656 int ecryptfs_read_and_validate_xattr_region(char *page_virt,
656 struct dentry *ecryptfs_dentry); 657 struct dentry *ecryptfs_dentry);
657 u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes); 658 u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes);
658 int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code); 659 int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code);
659 void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat); 660 void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat);
660 int ecryptfs_generate_key_packet_set(char *dest_base, 661 int ecryptfs_generate_key_packet_set(char *dest_base,
661 struct ecryptfs_crypt_stat *crypt_stat, 662 struct ecryptfs_crypt_stat *crypt_stat,
662 struct dentry *ecryptfs_dentry, 663 struct dentry *ecryptfs_dentry,
663 size_t *len, size_t max); 664 size_t *len, size_t max);
664 int 665 int
665 ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, 666 ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
666 unsigned char *src, struct dentry *ecryptfs_dentry); 667 unsigned char *src, struct dentry *ecryptfs_dentry);
667 int ecryptfs_truncate(struct dentry *dentry, loff_t new_length); 668 int ecryptfs_truncate(struct dentry *dentry, loff_t new_length);
668 int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode); 669 int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode);
669 int ecryptfs_inode_set(struct inode *inode, void *lower_inode); 670 int ecryptfs_inode_set(struct inode *inode, void *lower_inode);
670 void ecryptfs_init_inode(struct inode *inode, struct inode *lower_inode); 671 void ecryptfs_init_inode(struct inode *inode, struct inode *lower_inode);
671 ssize_t 672 ssize_t
672 ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name, 673 ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
673 void *value, size_t size); 674 void *value, size_t size);
674 int 675 int
675 ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value, 676 ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
676 size_t size, int flags); 677 size_t size, int flags);
677 int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode); 678 int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode);
678 int ecryptfs_process_helo(uid_t euid, struct user_namespace *user_ns, 679 int ecryptfs_process_helo(uid_t euid, struct user_namespace *user_ns,
679 struct pid *pid); 680 struct pid *pid);
680 int ecryptfs_process_quit(uid_t euid, struct user_namespace *user_ns, 681 int ecryptfs_process_quit(uid_t euid, struct user_namespace *user_ns,
681 struct pid *pid); 682 struct pid *pid);
682 int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid, 683 int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
683 struct user_namespace *user_ns, struct pid *pid, 684 struct user_namespace *user_ns, struct pid *pid,
684 u32 seq); 685 u32 seq);
685 int ecryptfs_send_message(char *data, int data_len, 686 int ecryptfs_send_message(char *data, int data_len,
686 struct ecryptfs_msg_ctx **msg_ctx); 687 struct ecryptfs_msg_ctx **msg_ctx);
687 int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx, 688 int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
688 struct ecryptfs_message **emsg); 689 struct ecryptfs_message **emsg);
689 int ecryptfs_init_messaging(void); 690 int ecryptfs_init_messaging(void);
690 void ecryptfs_release_messaging(void); 691 void ecryptfs_release_messaging(void);
691 692
692 void 693 void
693 ecryptfs_write_header_metadata(char *virt, 694 ecryptfs_write_header_metadata(char *virt,
694 struct ecryptfs_crypt_stat *crypt_stat, 695 struct ecryptfs_crypt_stat *crypt_stat,
695 size_t *written); 696 size_t *written);
696 int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig); 697 int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig);
697 int 698 int
698 ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 699 ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
699 char *sig); 700 char *sig, u32 global_auth_tok_flags);
700 int ecryptfs_get_global_auth_tok_for_sig( 701 int ecryptfs_get_global_auth_tok_for_sig(
701 struct ecryptfs_global_auth_tok **global_auth_tok, 702 struct ecryptfs_global_auth_tok **global_auth_tok,
702 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig); 703 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig);
703 int 704 int
704 ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name, 705 ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
705 size_t key_size); 706 size_t key_size);
706 int ecryptfs_init_crypto(void); 707 int ecryptfs_init_crypto(void);
707 int ecryptfs_destroy_crypto(void); 708 int ecryptfs_destroy_crypto(void);
708 int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm); 709 int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm);
709 int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm, 710 int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
710 struct mutex **tfm_mutex, 711 struct mutex **tfm_mutex,
711 char *cipher_name); 712 char *cipher_name);
712 int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, 713 int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
713 struct ecryptfs_auth_tok **auth_tok, 714 struct ecryptfs_auth_tok **auth_tok,
714 char *sig); 715 char *sig);
715 int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data, 716 int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
716 loff_t offset, size_t size); 717 loff_t offset, size_t size);
717 int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode, 718 int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
718 struct page *page_for_lower, 719 struct page *page_for_lower,
719 size_t offset_in_page, size_t size); 720 size_t offset_in_page, size_t size);
720 int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset, 721 int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset,
721 size_t size); 722 size_t size);
722 int ecryptfs_read_lower(char *data, loff_t offset, size_t size, 723 int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
723 struct inode *ecryptfs_inode); 724 struct inode *ecryptfs_inode);
724 int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs, 725 int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
725 pgoff_t page_index, 726 pgoff_t page_index,
726 size_t offset_in_page, size_t size, 727 size_t offset_in_page, size_t size,
727 struct inode *ecryptfs_inode); 728 struct inode *ecryptfs_inode);
728 struct page *ecryptfs_get_locked_page(struct file *file, loff_t index); 729 struct page *ecryptfs_get_locked_page(struct file *file, loff_t index);
729 int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon); 730 int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon);
730 int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid, 731 int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid,
731 struct user_namespace *user_ns); 732 struct user_namespace *user_ns);
732 int ecryptfs_parse_packet_length(unsigned char *data, size_t *size, 733 int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
733 size_t *length_size); 734 size_t *length_size);
734 int ecryptfs_write_packet_length(char *dest, size_t size, 735 int ecryptfs_write_packet_length(char *dest, size_t size,
735 size_t *packet_size_length); 736 size_t *packet_size_length);
736 int ecryptfs_init_ecryptfs_miscdev(void); 737 int ecryptfs_init_ecryptfs_miscdev(void);
737 void ecryptfs_destroy_ecryptfs_miscdev(void); 738 void ecryptfs_destroy_ecryptfs_miscdev(void);
738 int ecryptfs_send_miscdev(char *data, size_t data_size, 739 int ecryptfs_send_miscdev(char *data, size_t data_size,
739 struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type, 740 struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
740 u16 msg_flags, struct ecryptfs_daemon *daemon); 741 u16 msg_flags, struct ecryptfs_daemon *daemon);
741 void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx); 742 void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx);
742 int 743 int
743 ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid, 744 ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
744 struct user_namespace *user_ns, struct pid *pid); 745 struct user_namespace *user_ns, struct pid *pid);
745 int ecryptfs_init_kthread(void); 746 int ecryptfs_init_kthread(void);
746 void ecryptfs_destroy_kthread(void); 747 void ecryptfs_destroy_kthread(void);
747 int ecryptfs_privileged_open(struct file **lower_file, 748 int ecryptfs_privileged_open(struct file **lower_file,
748 struct dentry *lower_dentry, 749 struct dentry *lower_dentry,
749 struct vfsmount *lower_mnt, 750 struct vfsmount *lower_mnt,
750 const struct cred *cred); 751 const struct cred *cred);
751 int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry); 752 int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry);
752 int 753 int
753 ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, 754 ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
754 size_t *packet_size, 755 size_t *packet_size,
755 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 756 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
756 char *filename, size_t filename_size); 757 char *filename, size_t filename_size);
757 int 758 int
758 ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, 759 ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
759 size_t *packet_size, 760 size_t *packet_size,
760 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 761 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
761 char *data, size_t max_packet_size); 762 char *data, size_t max_packet_size);
762 int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat, 763 int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
763 loff_t offset); 764 loff_t offset);
764 765
765 #endif /* #ifndef ECRYPTFS_KERNEL_H */ 766 #endif /* #ifndef ECRYPTFS_KERNEL_H */
766 767
fs/ecryptfs/keystore.c
1 /** 1 /**
2 * eCryptfs: Linux filesystem encryption layer 2 * eCryptfs: Linux filesystem encryption layer
3 * In-kernel key management code. Includes functions to parse and 3 * In-kernel key management code. Includes functions to parse and
4 * write authentication token-related packets with the underlying 4 * write authentication token-related packets with the underlying
5 * file. 5 * file.
6 * 6 *
7 * Copyright (C) 2004-2006 International Business Machines Corp. 7 * Copyright (C) 2004-2006 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com> 8 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
9 * Michael C. Thompson <mcthomps@us.ibm.com> 9 * Michael C. Thompson <mcthomps@us.ibm.com>
10 * Trevor S. Highland <trevor.highland@gmail.com> 10 * Trevor S. Highland <trevor.highland@gmail.com>
11 * 11 *
12 * This program is free software; you can redistribute it and/or 12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as 13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the 14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version. 15 * License, or (at your option) any later version.
16 * 16 *
17 * This program is distributed in the hope that it will be useful, but 17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of 18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details. 20 * General Public License for more details.
21 * 21 *
22 * You should have received a copy of the GNU General Public License 22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software 23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA. 25 * 02111-1307, USA.
26 */ 26 */
27 27
28 #include <linux/string.h> 28 #include <linux/string.h>
29 #include <linux/syscalls.h> 29 #include <linux/syscalls.h>
30 #include <linux/pagemap.h> 30 #include <linux/pagemap.h>
31 #include <linux/key.h> 31 #include <linux/key.h>
32 #include <linux/random.h> 32 #include <linux/random.h>
33 #include <linux/crypto.h> 33 #include <linux/crypto.h>
34 #include <linux/scatterlist.h> 34 #include <linux/scatterlist.h>
35 #include "ecryptfs_kernel.h" 35 #include "ecryptfs_kernel.h"
36 36
37 /** 37 /**
38 * request_key returned an error instead of a valid key address; 38 * request_key returned an error instead of a valid key address;
39 * determine the type of error, make appropriate log entries, and 39 * determine the type of error, make appropriate log entries, and
40 * return an error code. 40 * return an error code.
41 */ 41 */
42 static int process_request_key_err(long err_code) 42 static int process_request_key_err(long err_code)
43 { 43 {
44 int rc = 0; 44 int rc = 0;
45 45
46 switch (err_code) { 46 switch (err_code) {
47 case -ENOKEY: 47 case -ENOKEY:
48 ecryptfs_printk(KERN_WARNING, "No key\n"); 48 ecryptfs_printk(KERN_WARNING, "No key\n");
49 rc = -ENOENT; 49 rc = -ENOENT;
50 break; 50 break;
51 case -EKEYEXPIRED: 51 case -EKEYEXPIRED:
52 ecryptfs_printk(KERN_WARNING, "Key expired\n"); 52 ecryptfs_printk(KERN_WARNING, "Key expired\n");
53 rc = -ETIME; 53 rc = -ETIME;
54 break; 54 break;
55 case -EKEYREVOKED: 55 case -EKEYREVOKED:
56 ecryptfs_printk(KERN_WARNING, "Key revoked\n"); 56 ecryptfs_printk(KERN_WARNING, "Key revoked\n");
57 rc = -EINVAL; 57 rc = -EINVAL;
58 break; 58 break;
59 default: 59 default:
60 ecryptfs_printk(KERN_WARNING, "Unknown error code: " 60 ecryptfs_printk(KERN_WARNING, "Unknown error code: "
61 "[0x%.16x]\n", err_code); 61 "[0x%.16x]\n", err_code);
62 rc = -EINVAL; 62 rc = -EINVAL;
63 } 63 }
64 return rc; 64 return rc;
65 } 65 }
66 66
67 /** 67 /**
68 * ecryptfs_parse_packet_length 68 * ecryptfs_parse_packet_length
69 * @data: Pointer to memory containing length at offset 69 * @data: Pointer to memory containing length at offset
70 * @size: This function writes the decoded size to this memory 70 * @size: This function writes the decoded size to this memory
71 * address; zero on error 71 * address; zero on error
72 * @length_size: The number of bytes occupied by the encoded length 72 * @length_size: The number of bytes occupied by the encoded length
73 * 73 *
74 * Returns zero on success; non-zero on error 74 * Returns zero on success; non-zero on error
75 */ 75 */
76 int ecryptfs_parse_packet_length(unsigned char *data, size_t *size, 76 int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
77 size_t *length_size) 77 size_t *length_size)
78 { 78 {
79 int rc = 0; 79 int rc = 0;
80 80
81 (*length_size) = 0; 81 (*length_size) = 0;
82 (*size) = 0; 82 (*size) = 0;
83 if (data[0] < 192) { 83 if (data[0] < 192) {
84 /* One-byte length */ 84 /* One-byte length */
85 (*size) = (unsigned char)data[0]; 85 (*size) = (unsigned char)data[0];
86 (*length_size) = 1; 86 (*length_size) = 1;
87 } else if (data[0] < 224) { 87 } else if (data[0] < 224) {
88 /* Two-byte length */ 88 /* Two-byte length */
89 (*size) = (((unsigned char)(data[0]) - 192) * 256); 89 (*size) = (((unsigned char)(data[0]) - 192) * 256);
90 (*size) += ((unsigned char)(data[1]) + 192); 90 (*size) += ((unsigned char)(data[1]) + 192);
91 (*length_size) = 2; 91 (*length_size) = 2;
92 } else if (data[0] == 255) { 92 } else if (data[0] == 255) {
93 /* Five-byte length; we're not supposed to see this */ 93 /* Five-byte length; we're not supposed to see this */
94 ecryptfs_printk(KERN_ERR, "Five-byte packet length not " 94 ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
95 "supported\n"); 95 "supported\n");
96 rc = -EINVAL; 96 rc = -EINVAL;
97 goto out; 97 goto out;
98 } else { 98 } else {
99 ecryptfs_printk(KERN_ERR, "Error parsing packet length\n"); 99 ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
100 rc = -EINVAL; 100 rc = -EINVAL;
101 goto out; 101 goto out;
102 } 102 }
103 out: 103 out:
104 return rc; 104 return rc;
105 } 105 }
106 106
107 /** 107 /**
108 * ecryptfs_write_packet_length 108 * ecryptfs_write_packet_length
109 * @dest: The byte array target into which to write the length. Must 109 * @dest: The byte array target into which to write the length. Must
110 * have at least 5 bytes allocated. 110 * have at least 5 bytes allocated.
111 * @size: The length to write. 111 * @size: The length to write.
112 * @packet_size_length: The number of bytes used to encode the packet 112 * @packet_size_length: The number of bytes used to encode the packet
113 * length is written to this address. 113 * length is written to this address.
114 * 114 *
115 * Returns zero on success; non-zero on error. 115 * Returns zero on success; non-zero on error.
116 */ 116 */
117 int ecryptfs_write_packet_length(char *dest, size_t size, 117 int ecryptfs_write_packet_length(char *dest, size_t size,
118 size_t *packet_size_length) 118 size_t *packet_size_length)
119 { 119 {
120 int rc = 0; 120 int rc = 0;
121 121
122 if (size < 192) { 122 if (size < 192) {
123 dest[0] = size; 123 dest[0] = size;
124 (*packet_size_length) = 1; 124 (*packet_size_length) = 1;
125 } else if (size < 65536) { 125 } else if (size < 65536) {
126 dest[0] = (((size - 192) / 256) + 192); 126 dest[0] = (((size - 192) / 256) + 192);
127 dest[1] = ((size - 192) % 256); 127 dest[1] = ((size - 192) % 256);
128 (*packet_size_length) = 2; 128 (*packet_size_length) = 2;
129 } else { 129 } else {
130 rc = -EINVAL; 130 rc = -EINVAL;
131 ecryptfs_printk(KERN_WARNING, 131 ecryptfs_printk(KERN_WARNING,
132 "Unsupported packet size: [%d]\n", size); 132 "Unsupported packet size: [%d]\n", size);
133 } 133 }
134 return rc; 134 return rc;
135 } 135 }
136 136
137 static int 137 static int
138 write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key, 138 write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
139 char **packet, size_t *packet_len) 139 char **packet, size_t *packet_len)
140 { 140 {
141 size_t i = 0; 141 size_t i = 0;
142 size_t data_len; 142 size_t data_len;
143 size_t packet_size_len; 143 size_t packet_size_len;
144 char *message; 144 char *message;
145 int rc; 145 int rc;
146 146
147 /* 147 /*
148 * ***** TAG 64 Packet Format ***** 148 * ***** TAG 64 Packet Format *****
149 * | Content Type | 1 byte | 149 * | Content Type | 1 byte |
150 * | Key Identifier Size | 1 or 2 bytes | 150 * | Key Identifier Size | 1 or 2 bytes |
151 * | Key Identifier | arbitrary | 151 * | Key Identifier | arbitrary |
152 * | Encrypted File Encryption Key Size | 1 or 2 bytes | 152 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
153 * | Encrypted File Encryption Key | arbitrary | 153 * | Encrypted File Encryption Key | arbitrary |
154 */ 154 */
155 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX 155 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX
156 + session_key->encrypted_key_size); 156 + session_key->encrypted_key_size);
157 *packet = kmalloc(data_len, GFP_KERNEL); 157 *packet = kmalloc(data_len, GFP_KERNEL);
158 message = *packet; 158 message = *packet;
159 if (!message) { 159 if (!message) {
160 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 160 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
161 rc = -ENOMEM; 161 rc = -ENOMEM;
162 goto out; 162 goto out;
163 } 163 }
164 message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE; 164 message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE;
165 rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, 165 rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
166 &packet_size_len); 166 &packet_size_len);
167 if (rc) { 167 if (rc) {
168 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " 168 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
169 "header; cannot generate packet length\n"); 169 "header; cannot generate packet length\n");
170 goto out; 170 goto out;
171 } 171 }
172 i += packet_size_len; 172 i += packet_size_len;
173 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); 173 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
174 i += ECRYPTFS_SIG_SIZE_HEX; 174 i += ECRYPTFS_SIG_SIZE_HEX;
175 rc = ecryptfs_write_packet_length(&message[i], 175 rc = ecryptfs_write_packet_length(&message[i],
176 session_key->encrypted_key_size, 176 session_key->encrypted_key_size,
177 &packet_size_len); 177 &packet_size_len);
178 if (rc) { 178 if (rc) {
179 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " 179 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
180 "header; cannot generate packet length\n"); 180 "header; cannot generate packet length\n");
181 goto out; 181 goto out;
182 } 182 }
183 i += packet_size_len; 183 i += packet_size_len;
184 memcpy(&message[i], session_key->encrypted_key, 184 memcpy(&message[i], session_key->encrypted_key,
185 session_key->encrypted_key_size); 185 session_key->encrypted_key_size);
186 i += session_key->encrypted_key_size; 186 i += session_key->encrypted_key_size;
187 *packet_len = i; 187 *packet_len = i;
188 out: 188 out:
189 return rc; 189 return rc;
190 } 190 }
191 191
192 static int 192 static int
193 parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code, 193 parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code,
194 struct ecryptfs_message *msg) 194 struct ecryptfs_message *msg)
195 { 195 {
196 size_t i = 0; 196 size_t i = 0;
197 char *data; 197 char *data;
198 size_t data_len; 198 size_t data_len;
199 size_t m_size; 199 size_t m_size;
200 size_t message_len; 200 size_t message_len;
201 u16 checksum = 0; 201 u16 checksum = 0;
202 u16 expected_checksum = 0; 202 u16 expected_checksum = 0;
203 int rc; 203 int rc;
204 204
205 /* 205 /*
206 * ***** TAG 65 Packet Format ***** 206 * ***** TAG 65 Packet Format *****
207 * | Content Type | 1 byte | 207 * | Content Type | 1 byte |
208 * | Status Indicator | 1 byte | 208 * | Status Indicator | 1 byte |
209 * | File Encryption Key Size | 1 or 2 bytes | 209 * | File Encryption Key Size | 1 or 2 bytes |
210 * | File Encryption Key | arbitrary | 210 * | File Encryption Key | arbitrary |
211 */ 211 */
212 message_len = msg->data_len; 212 message_len = msg->data_len;
213 data = msg->data; 213 data = msg->data;
214 if (message_len < 4) { 214 if (message_len < 4) {
215 rc = -EIO; 215 rc = -EIO;
216 goto out; 216 goto out;
217 } 217 }
218 if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) { 218 if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) {
219 ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n"); 219 ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n");
220 rc = -EIO; 220 rc = -EIO;
221 goto out; 221 goto out;
222 } 222 }
223 if (data[i++]) { 223 if (data[i++]) {
224 ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value " 224 ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value "
225 "[%d]\n", data[i-1]); 225 "[%d]\n", data[i-1]);
226 rc = -EIO; 226 rc = -EIO;
227 goto out; 227 goto out;
228 } 228 }
229 rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len); 229 rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len);
230 if (rc) { 230 if (rc) {
231 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 231 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
232 "rc = [%d]\n", rc); 232 "rc = [%d]\n", rc);
233 goto out; 233 goto out;
234 } 234 }
235 i += data_len; 235 i += data_len;
236 if (message_len < (i + m_size)) { 236 if (message_len < (i + m_size)) {
237 ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd " 237 ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd "
238 "is shorter than expected\n"); 238 "is shorter than expected\n");
239 rc = -EIO; 239 rc = -EIO;
240 goto out; 240 goto out;
241 } 241 }
242 if (m_size < 3) { 242 if (m_size < 3) {
243 ecryptfs_printk(KERN_ERR, 243 ecryptfs_printk(KERN_ERR,
244 "The decrypted key is not long enough to " 244 "The decrypted key is not long enough to "
245 "include a cipher code and checksum\n"); 245 "include a cipher code and checksum\n");
246 rc = -EIO; 246 rc = -EIO;
247 goto out; 247 goto out;
248 } 248 }
249 *cipher_code = data[i++]; 249 *cipher_code = data[i++];
250 /* The decrypted key includes 1 byte cipher code and 2 byte checksum */ 250 /* The decrypted key includes 1 byte cipher code and 2 byte checksum */
251 session_key->decrypted_key_size = m_size - 3; 251 session_key->decrypted_key_size = m_size - 3;
252 if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) { 252 if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) {
253 ecryptfs_printk(KERN_ERR, "key_size [%d] larger than " 253 ecryptfs_printk(KERN_ERR, "key_size [%d] larger than "
254 "the maximum key size [%d]\n", 254 "the maximum key size [%d]\n",
255 session_key->decrypted_key_size, 255 session_key->decrypted_key_size,
256 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); 256 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
257 rc = -EIO; 257 rc = -EIO;
258 goto out; 258 goto out;
259 } 259 }
260 memcpy(session_key->decrypted_key, &data[i], 260 memcpy(session_key->decrypted_key, &data[i],
261 session_key->decrypted_key_size); 261 session_key->decrypted_key_size);
262 i += session_key->decrypted_key_size; 262 i += session_key->decrypted_key_size;
263 expected_checksum += (unsigned char)(data[i++]) << 8; 263 expected_checksum += (unsigned char)(data[i++]) << 8;
264 expected_checksum += (unsigned char)(data[i++]); 264 expected_checksum += (unsigned char)(data[i++]);
265 for (i = 0; i < session_key->decrypted_key_size; i++) 265 for (i = 0; i < session_key->decrypted_key_size; i++)
266 checksum += session_key->decrypted_key[i]; 266 checksum += session_key->decrypted_key[i];
267 if (expected_checksum != checksum) { 267 if (expected_checksum != checksum) {
268 ecryptfs_printk(KERN_ERR, "Invalid checksum for file " 268 ecryptfs_printk(KERN_ERR, "Invalid checksum for file "
269 "encryption key; expected [%x]; calculated " 269 "encryption key; expected [%x]; calculated "
270 "[%x]\n", expected_checksum, checksum); 270 "[%x]\n", expected_checksum, checksum);
271 rc = -EIO; 271 rc = -EIO;
272 } 272 }
273 out: 273 out:
274 return rc; 274 return rc;
275 } 275 }
276 276
277 277
278 static int 278 static int
279 write_tag_66_packet(char *signature, u8 cipher_code, 279 write_tag_66_packet(char *signature, u8 cipher_code,
280 struct ecryptfs_crypt_stat *crypt_stat, char **packet, 280 struct ecryptfs_crypt_stat *crypt_stat, char **packet,
281 size_t *packet_len) 281 size_t *packet_len)
282 { 282 {
283 size_t i = 0; 283 size_t i = 0;
284 size_t j; 284 size_t j;
285 size_t data_len; 285 size_t data_len;
286 size_t checksum = 0; 286 size_t checksum = 0;
287 size_t packet_size_len; 287 size_t packet_size_len;
288 char *message; 288 char *message;
289 int rc; 289 int rc;
290 290
291 /* 291 /*
292 * ***** TAG 66 Packet Format ***** 292 * ***** TAG 66 Packet Format *****
293 * | Content Type | 1 byte | 293 * | Content Type | 1 byte |
294 * | Key Identifier Size | 1 or 2 bytes | 294 * | Key Identifier Size | 1 or 2 bytes |
295 * | Key Identifier | arbitrary | 295 * | Key Identifier | arbitrary |
296 * | File Encryption Key Size | 1 or 2 bytes | 296 * | File Encryption Key Size | 1 or 2 bytes |
297 * | File Encryption Key | arbitrary | 297 * | File Encryption Key | arbitrary |
298 */ 298 */
299 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); 299 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size);
300 *packet = kmalloc(data_len, GFP_KERNEL); 300 *packet = kmalloc(data_len, GFP_KERNEL);
301 message = *packet; 301 message = *packet;
302 if (!message) { 302 if (!message) {
303 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 303 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
304 rc = -ENOMEM; 304 rc = -ENOMEM;
305 goto out; 305 goto out;
306 } 306 }
307 message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE; 307 message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE;
308 rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, 308 rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
309 &packet_size_len); 309 &packet_size_len);
310 if (rc) { 310 if (rc) {
311 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " 311 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
312 "header; cannot generate packet length\n"); 312 "header; cannot generate packet length\n");
313 goto out; 313 goto out;
314 } 314 }
315 i += packet_size_len; 315 i += packet_size_len;
316 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); 316 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
317 i += ECRYPTFS_SIG_SIZE_HEX; 317 i += ECRYPTFS_SIG_SIZE_HEX;
318 /* The encrypted key includes 1 byte cipher code and 2 byte checksum */ 318 /* The encrypted key includes 1 byte cipher code and 2 byte checksum */
319 rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3, 319 rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3,
320 &packet_size_len); 320 &packet_size_len);
321 if (rc) { 321 if (rc) {
322 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " 322 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
323 "header; cannot generate packet length\n"); 323 "header; cannot generate packet length\n");
324 goto out; 324 goto out;
325 } 325 }
326 i += packet_size_len; 326 i += packet_size_len;
327 message[i++] = cipher_code; 327 message[i++] = cipher_code;
328 memcpy(&message[i], crypt_stat->key, crypt_stat->key_size); 328 memcpy(&message[i], crypt_stat->key, crypt_stat->key_size);
329 i += crypt_stat->key_size; 329 i += crypt_stat->key_size;
330 for (j = 0; j < crypt_stat->key_size; j++) 330 for (j = 0; j < crypt_stat->key_size; j++)
331 checksum += crypt_stat->key[j]; 331 checksum += crypt_stat->key[j];
332 message[i++] = (checksum / 256) % 256; 332 message[i++] = (checksum / 256) % 256;
333 message[i++] = (checksum % 256); 333 message[i++] = (checksum % 256);
334 *packet_len = i; 334 *packet_len = i;
335 out: 335 out:
336 return rc; 336 return rc;
337 } 337 }
338 338
339 static int 339 static int
340 parse_tag_67_packet(struct ecryptfs_key_record *key_rec, 340 parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
341 struct ecryptfs_message *msg) 341 struct ecryptfs_message *msg)
342 { 342 {
343 size_t i = 0; 343 size_t i = 0;
344 char *data; 344 char *data;
345 size_t data_len; 345 size_t data_len;
346 size_t message_len; 346 size_t message_len;
347 int rc; 347 int rc;
348 348
349 /* 349 /*
350 * ***** TAG 65 Packet Format ***** 350 * ***** TAG 65 Packet Format *****
351 * | Content Type | 1 byte | 351 * | Content Type | 1 byte |
352 * | Status Indicator | 1 byte | 352 * | Status Indicator | 1 byte |
353 * | Encrypted File Encryption Key Size | 1 or 2 bytes | 353 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
354 * | Encrypted File Encryption Key | arbitrary | 354 * | Encrypted File Encryption Key | arbitrary |
355 */ 355 */
356 message_len = msg->data_len; 356 message_len = msg->data_len;
357 data = msg->data; 357 data = msg->data;
358 /* verify that everything through the encrypted FEK size is present */ 358 /* verify that everything through the encrypted FEK size is present */
359 if (message_len < 4) { 359 if (message_len < 4) {
360 rc = -EIO; 360 rc = -EIO;
361 printk(KERN_ERR "%s: message_len is [%zd]; minimum acceptable " 361 printk(KERN_ERR "%s: message_len is [%zd]; minimum acceptable "
362 "message length is [%d]\n", __func__, message_len, 4); 362 "message length is [%d]\n", __func__, message_len, 4);
363 goto out; 363 goto out;
364 } 364 }
365 if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) { 365 if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) {
366 rc = -EIO; 366 rc = -EIO;
367 printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n", 367 printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n",
368 __func__); 368 __func__);
369 goto out; 369 goto out;
370 } 370 }
371 if (data[i++]) { 371 if (data[i++]) {
372 rc = -EIO; 372 rc = -EIO;
373 printk(KERN_ERR "%s: Status indicator has non zero " 373 printk(KERN_ERR "%s: Status indicator has non zero "
374 "value [%d]\n", __func__, data[i-1]); 374 "value [%d]\n", __func__, data[i-1]);
375 375
376 goto out; 376 goto out;
377 } 377 }
378 rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size, 378 rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size,
379 &data_len); 379 &data_len);
380 if (rc) { 380 if (rc) {
381 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 381 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
382 "rc = [%d]\n", rc); 382 "rc = [%d]\n", rc);
383 goto out; 383 goto out;
384 } 384 }
385 i += data_len; 385 i += data_len;
386 if (message_len < (i + key_rec->enc_key_size)) { 386 if (message_len < (i + key_rec->enc_key_size)) {
387 rc = -EIO; 387 rc = -EIO;
388 printk(KERN_ERR "%s: message_len [%zd]; max len is [%zd]\n", 388 printk(KERN_ERR "%s: message_len [%zd]; max len is [%zd]\n",
389 __func__, message_len, (i + key_rec->enc_key_size)); 389 __func__, message_len, (i + key_rec->enc_key_size));
390 goto out; 390 goto out;
391 } 391 }
392 if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 392 if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
393 rc = -EIO; 393 rc = -EIO;
394 printk(KERN_ERR "%s: Encrypted key_size [%zd] larger than " 394 printk(KERN_ERR "%s: Encrypted key_size [%zd] larger than "
395 "the maximum key size [%d]\n", __func__, 395 "the maximum key size [%d]\n", __func__,
396 key_rec->enc_key_size, 396 key_rec->enc_key_size,
397 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); 397 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
398 goto out; 398 goto out;
399 } 399 }
400 memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size); 400 memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size);
401 out: 401 out:
402 return rc; 402 return rc;
403 } 403 }
404 404
405 static int 405 static int
406 ecryptfs_find_global_auth_tok_for_sig( 406 ecryptfs_find_global_auth_tok_for_sig(
407 struct ecryptfs_global_auth_tok **global_auth_tok, 407 struct ecryptfs_global_auth_tok **global_auth_tok,
408 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) 408 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
409 { 409 {
410 struct ecryptfs_global_auth_tok *walker; 410 struct ecryptfs_global_auth_tok *walker;
411 int rc = 0; 411 int rc = 0;
412 412
413 (*global_auth_tok) = NULL; 413 (*global_auth_tok) = NULL;
414 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 414 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
415 list_for_each_entry(walker, 415 list_for_each_entry(walker,
416 &mount_crypt_stat->global_auth_tok_list, 416 &mount_crypt_stat->global_auth_tok_list,
417 mount_crypt_stat_list) { 417 mount_crypt_stat_list) {
418 if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) { 418 if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) {
419 (*global_auth_tok) = walker; 419 (*global_auth_tok) = walker;
420 goto out; 420 goto out;
421 } 421 }
422 } 422 }
423 rc = -EINVAL; 423 rc = -EINVAL;
424 out: 424 out:
425 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 425 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
426 return rc; 426 return rc;
427 } 427 }
428 428
429 /** 429 /**
430 * ecryptfs_find_auth_tok_for_sig 430 * ecryptfs_find_auth_tok_for_sig
431 * @auth_tok: Set to the matching auth_tok; NULL if not found 431 * @auth_tok: Set to the matching auth_tok; NULL if not found
432 * @crypt_stat: inode crypt_stat crypto context 432 * @crypt_stat: inode crypt_stat crypto context
433 * @sig: Sig of auth_tok to find 433 * @sig: Sig of auth_tok to find
434 * 434 *
435 * For now, this function simply looks at the registered auth_tok's 435 * For now, this function simply looks at the registered auth_tok's
436 * linked off the mount_crypt_stat, so all the auth_toks that can be 436 * linked off the mount_crypt_stat, so all the auth_toks that can be
437 * used must be registered at mount time. This function could 437 * used must be registered at mount time. This function could
438 * potentially try a lot harder to find auth_tok's (e.g., by calling 438 * potentially try a lot harder to find auth_tok's (e.g., by calling
439 * out to ecryptfsd to dynamically retrieve an auth_tok object) so 439 * out to ecryptfsd to dynamically retrieve an auth_tok object) so
440 * that static registration of auth_tok's will no longer be necessary. 440 * that static registration of auth_tok's will no longer be necessary.
441 * 441 *
442 * Returns zero on no error; non-zero on error 442 * Returns zero on no error; non-zero on error
443 */ 443 */
444 static int 444 static int
445 ecryptfs_find_auth_tok_for_sig( 445 ecryptfs_find_auth_tok_for_sig(
446 struct ecryptfs_auth_tok **auth_tok, 446 struct ecryptfs_auth_tok **auth_tok,
447 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 447 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
448 char *sig) 448 char *sig)
449 { 449 {
450 struct ecryptfs_global_auth_tok *global_auth_tok; 450 struct ecryptfs_global_auth_tok *global_auth_tok;
451 int rc = 0; 451 int rc = 0;
452 452
453 (*auth_tok) = NULL; 453 (*auth_tok) = NULL;
454 if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, 454 if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
455 mount_crypt_stat, sig)) { 455 mount_crypt_stat, sig)) {
456 struct key *auth_tok_key; 456 struct key *auth_tok_key;
457 457
458 rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok, 458 rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok,
459 sig); 459 sig);
460 } else 460 } else
461 (*auth_tok) = global_auth_tok->global_auth_tok; 461 (*auth_tok) = global_auth_tok->global_auth_tok;
462 return rc; 462 return rc;
463 } 463 }
464 464
465 /** 465 /**
466 * write_tag_70_packet can gobble a lot of stack space. We stuff most 466 * write_tag_70_packet can gobble a lot of stack space. We stuff most
467 * of the function's parameters in a kmalloc'd struct to help reduce 467 * of the function's parameters in a kmalloc'd struct to help reduce
468 * eCryptfs' overall stack usage. 468 * eCryptfs' overall stack usage.
469 */ 469 */
470 struct ecryptfs_write_tag_70_packet_silly_stack { 470 struct ecryptfs_write_tag_70_packet_silly_stack {
471 u8 cipher_code; 471 u8 cipher_code;
472 size_t max_packet_size; 472 size_t max_packet_size;
473 size_t packet_size_len; 473 size_t packet_size_len;
474 size_t block_aligned_filename_size; 474 size_t block_aligned_filename_size;
475 size_t block_size; 475 size_t block_size;
476 size_t i; 476 size_t i;
477 size_t j; 477 size_t j;
478 size_t num_rand_bytes; 478 size_t num_rand_bytes;
479 struct mutex *tfm_mutex; 479 struct mutex *tfm_mutex;
480 char *block_aligned_filename; 480 char *block_aligned_filename;
481 struct ecryptfs_auth_tok *auth_tok; 481 struct ecryptfs_auth_tok *auth_tok;
482 struct scatterlist src_sg; 482 struct scatterlist src_sg;
483 struct scatterlist dst_sg; 483 struct scatterlist dst_sg;
484 struct blkcipher_desc desc; 484 struct blkcipher_desc desc;
485 char iv[ECRYPTFS_MAX_IV_BYTES]; 485 char iv[ECRYPTFS_MAX_IV_BYTES];
486 char hash[ECRYPTFS_TAG_70_DIGEST_SIZE]; 486 char hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
487 char tmp_hash[ECRYPTFS_TAG_70_DIGEST_SIZE]; 487 char tmp_hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
488 struct hash_desc hash_desc; 488 struct hash_desc hash_desc;
489 struct scatterlist hash_sg; 489 struct scatterlist hash_sg;
490 }; 490 };
491 491
492 /** 492 /**
493 * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK 493 * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK
494 * @filename: NULL-terminated filename string 494 * @filename: NULL-terminated filename string
495 * 495 *
496 * This is the simplest mechanism for achieving filename encryption in 496 * This is the simplest mechanism for achieving filename encryption in
497 * eCryptfs. It encrypts the given filename with the mount-wide 497 * eCryptfs. It encrypts the given filename with the mount-wide
498 * filename encryption key (FNEK) and stores it in a packet to @dest, 498 * filename encryption key (FNEK) and stores it in a packet to @dest,
499 * which the callee will encode and write directly into the dentry 499 * which the callee will encode and write directly into the dentry
500 * name. 500 * name.
501 */ 501 */
502 int 502 int
503 ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, 503 ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
504 size_t *packet_size, 504 size_t *packet_size,
505 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 505 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
506 char *filename, size_t filename_size) 506 char *filename, size_t filename_size)
507 { 507 {
508 struct ecryptfs_write_tag_70_packet_silly_stack *s; 508 struct ecryptfs_write_tag_70_packet_silly_stack *s;
509 int rc = 0; 509 int rc = 0;
510 510
511 s = kmalloc(sizeof(*s), GFP_KERNEL); 511 s = kmalloc(sizeof(*s), GFP_KERNEL);
512 if (!s) { 512 if (!s) {
513 printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc " 513 printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
514 "[%zd] bytes of kernel memory\n", __func__, sizeof(*s)); 514 "[%zd] bytes of kernel memory\n", __func__, sizeof(*s));
515 goto out; 515 goto out;
516 } 516 }
517 s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; 517 s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
518 (*packet_size) = 0; 518 (*packet_size) = 0;
519 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name( 519 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(
520 &s->desc.tfm, 520 &s->desc.tfm,
521 &s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name); 521 &s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name);
522 if (unlikely(rc)) { 522 if (unlikely(rc)) {
523 printk(KERN_ERR "Internal error whilst attempting to get " 523 printk(KERN_ERR "Internal error whilst attempting to get "
524 "tfm and mutex for cipher name [%s]; rc = [%d]\n", 524 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
525 mount_crypt_stat->global_default_fn_cipher_name, rc); 525 mount_crypt_stat->global_default_fn_cipher_name, rc);
526 goto out; 526 goto out;
527 } 527 }
528 mutex_lock(s->tfm_mutex); 528 mutex_lock(s->tfm_mutex);
529 s->block_size = crypto_blkcipher_blocksize(s->desc.tfm); 529 s->block_size = crypto_blkcipher_blocksize(s->desc.tfm);
530 /* Plus one for the \0 separator between the random prefix 530 /* Plus one for the \0 separator between the random prefix
531 * and the plaintext filename */ 531 * and the plaintext filename */
532 s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1); 532 s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1);
533 s->block_aligned_filename_size = (s->num_rand_bytes + filename_size); 533 s->block_aligned_filename_size = (s->num_rand_bytes + filename_size);
534 if ((s->block_aligned_filename_size % s->block_size) != 0) { 534 if ((s->block_aligned_filename_size % s->block_size) != 0) {
535 s->num_rand_bytes += (s->block_size 535 s->num_rand_bytes += (s->block_size
536 - (s->block_aligned_filename_size 536 - (s->block_aligned_filename_size
537 % s->block_size)); 537 % s->block_size));
538 s->block_aligned_filename_size = (s->num_rand_bytes 538 s->block_aligned_filename_size = (s->num_rand_bytes
539 + filename_size); 539 + filename_size);
540 } 540 }
541 /* Octet 0: Tag 70 identifier 541 /* Octet 0: Tag 70 identifier
542 * Octets 1-N1: Tag 70 packet size (includes cipher identifier 542 * Octets 1-N1: Tag 70 packet size (includes cipher identifier
543 * and block-aligned encrypted filename size) 543 * and block-aligned encrypted filename size)
544 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) 544 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
545 * Octet N2-N3: Cipher identifier (1 octet) 545 * Octet N2-N3: Cipher identifier (1 octet)
546 * Octets N3-N4: Block-aligned encrypted filename 546 * Octets N3-N4: Block-aligned encrypted filename
547 * - Consists of a minimum number of random characters, a \0 547 * - Consists of a minimum number of random characters, a \0
548 * separator, and then the filename */ 548 * separator, and then the filename */
549 s->max_packet_size = (1 /* Tag 70 identifier */ 549 s->max_packet_size = (1 /* Tag 70 identifier */
550 + 3 /* Max Tag 70 packet size */ 550 + 3 /* Max Tag 70 packet size */
551 + ECRYPTFS_SIG_SIZE /* FNEK sig */ 551 + ECRYPTFS_SIG_SIZE /* FNEK sig */
552 + 1 /* Cipher identifier */ 552 + 1 /* Cipher identifier */
553 + s->block_aligned_filename_size); 553 + s->block_aligned_filename_size);
554 if (dest == NULL) { 554 if (dest == NULL) {
555 (*packet_size) = s->max_packet_size; 555 (*packet_size) = s->max_packet_size;
556 goto out_unlock; 556 goto out_unlock;
557 } 557 }
558 if (s->max_packet_size > (*remaining_bytes)) { 558 if (s->max_packet_size > (*remaining_bytes)) {
559 printk(KERN_WARNING "%s: Require [%zd] bytes to write; only " 559 printk(KERN_WARNING "%s: Require [%zd] bytes to write; only "
560 "[%zd] available\n", __func__, s->max_packet_size, 560 "[%zd] available\n", __func__, s->max_packet_size,
561 (*remaining_bytes)); 561 (*remaining_bytes));
562 rc = -EINVAL; 562 rc = -EINVAL;
563 goto out_unlock; 563 goto out_unlock;
564 } 564 }
565 s->block_aligned_filename = kzalloc(s->block_aligned_filename_size, 565 s->block_aligned_filename = kzalloc(s->block_aligned_filename_size,
566 GFP_KERNEL); 566 GFP_KERNEL);
567 if (!s->block_aligned_filename) { 567 if (!s->block_aligned_filename) {
568 printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 568 printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
569 "kzalloc [%zd] bytes\n", __func__, 569 "kzalloc [%zd] bytes\n", __func__,
570 s->block_aligned_filename_size); 570 s->block_aligned_filename_size);
571 rc = -ENOMEM; 571 rc = -ENOMEM;
572 goto out_unlock; 572 goto out_unlock;
573 } 573 }
574 s->i = 0; 574 s->i = 0;
575 dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE; 575 dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE;
576 rc = ecryptfs_write_packet_length(&dest[s->i], 576 rc = ecryptfs_write_packet_length(&dest[s->i],
577 (ECRYPTFS_SIG_SIZE 577 (ECRYPTFS_SIG_SIZE
578 + 1 /* Cipher code */ 578 + 1 /* Cipher code */
579 + s->block_aligned_filename_size), 579 + s->block_aligned_filename_size),
580 &s->packet_size_len); 580 &s->packet_size_len);
581 if (rc) { 581 if (rc) {
582 printk(KERN_ERR "%s: Error generating tag 70 packet " 582 printk(KERN_ERR "%s: Error generating tag 70 packet "
583 "header; cannot generate packet length; rc = [%d]\n", 583 "header; cannot generate packet length; rc = [%d]\n",
584 __func__, rc); 584 __func__, rc);
585 goto out_free_unlock; 585 goto out_free_unlock;
586 } 586 }
587 s->i += s->packet_size_len; 587 s->i += s->packet_size_len;
588 ecryptfs_from_hex(&dest[s->i], 588 ecryptfs_from_hex(&dest[s->i],
589 mount_crypt_stat->global_default_fnek_sig, 589 mount_crypt_stat->global_default_fnek_sig,
590 ECRYPTFS_SIG_SIZE); 590 ECRYPTFS_SIG_SIZE);
591 s->i += ECRYPTFS_SIG_SIZE; 591 s->i += ECRYPTFS_SIG_SIZE;
592 s->cipher_code = ecryptfs_code_for_cipher_string( 592 s->cipher_code = ecryptfs_code_for_cipher_string(
593 mount_crypt_stat->global_default_fn_cipher_name, 593 mount_crypt_stat->global_default_fn_cipher_name,
594 mount_crypt_stat->global_default_fn_cipher_key_bytes); 594 mount_crypt_stat->global_default_fn_cipher_key_bytes);
595 if (s->cipher_code == 0) { 595 if (s->cipher_code == 0) {
596 printk(KERN_WARNING "%s: Unable to generate code for " 596 printk(KERN_WARNING "%s: Unable to generate code for "
597 "cipher [%s] with key bytes [%zd]\n", __func__, 597 "cipher [%s] with key bytes [%zd]\n", __func__,
598 mount_crypt_stat->global_default_fn_cipher_name, 598 mount_crypt_stat->global_default_fn_cipher_name,
599 mount_crypt_stat->global_default_fn_cipher_key_bytes); 599 mount_crypt_stat->global_default_fn_cipher_key_bytes);
600 rc = -EINVAL; 600 rc = -EINVAL;
601 goto out_free_unlock; 601 goto out_free_unlock;
602 } 602 }
603 dest[s->i++] = s->cipher_code; 603 dest[s->i++] = s->cipher_code;
604 rc = ecryptfs_find_auth_tok_for_sig( 604 rc = ecryptfs_find_auth_tok_for_sig(
605 &s->auth_tok, mount_crypt_stat, 605 &s->auth_tok, mount_crypt_stat,
606 mount_crypt_stat->global_default_fnek_sig); 606 mount_crypt_stat->global_default_fnek_sig);
607 if (rc) { 607 if (rc) {
608 printk(KERN_ERR "%s: Error attempting to find auth tok for " 608 printk(KERN_ERR "%s: Error attempting to find auth tok for "
609 "fnek sig [%s]; rc = [%d]\n", __func__, 609 "fnek sig [%s]; rc = [%d]\n", __func__,
610 mount_crypt_stat->global_default_fnek_sig, rc); 610 mount_crypt_stat->global_default_fnek_sig, rc);
611 goto out_free_unlock; 611 goto out_free_unlock;
612 } 612 }
613 /* TODO: Support other key modules than passphrase for 613 /* TODO: Support other key modules than passphrase for
614 * filename encryption */ 614 * filename encryption */
615 BUG_ON(s->auth_tok->token_type != ECRYPTFS_PASSWORD); 615 BUG_ON(s->auth_tok->token_type != ECRYPTFS_PASSWORD);
616 sg_init_one( 616 sg_init_one(
617 &s->hash_sg, 617 &s->hash_sg,
618 (u8 *)s->auth_tok->token.password.session_key_encryption_key, 618 (u8 *)s->auth_tok->token.password.session_key_encryption_key,
619 s->auth_tok->token.password.session_key_encryption_key_bytes); 619 s->auth_tok->token.password.session_key_encryption_key_bytes);
620 s->hash_desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; 620 s->hash_desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
621 s->hash_desc.tfm = crypto_alloc_hash(ECRYPTFS_TAG_70_DIGEST, 0, 621 s->hash_desc.tfm = crypto_alloc_hash(ECRYPTFS_TAG_70_DIGEST, 0,
622 CRYPTO_ALG_ASYNC); 622 CRYPTO_ALG_ASYNC);
623 if (IS_ERR(s->hash_desc.tfm)) { 623 if (IS_ERR(s->hash_desc.tfm)) {
624 rc = PTR_ERR(s->hash_desc.tfm); 624 rc = PTR_ERR(s->hash_desc.tfm);
625 printk(KERN_ERR "%s: Error attempting to " 625 printk(KERN_ERR "%s: Error attempting to "
626 "allocate hash crypto context; rc = [%d]\n", 626 "allocate hash crypto context; rc = [%d]\n",
627 __func__, rc); 627 __func__, rc);
628 goto out_free_unlock; 628 goto out_free_unlock;
629 } 629 }
630 rc = crypto_hash_init(&s->hash_desc); 630 rc = crypto_hash_init(&s->hash_desc);
631 if (rc) { 631 if (rc) {
632 printk(KERN_ERR 632 printk(KERN_ERR
633 "%s: Error initializing crypto hash; rc = [%d]\n", 633 "%s: Error initializing crypto hash; rc = [%d]\n",
634 __func__, rc); 634 __func__, rc);
635 goto out_release_free_unlock; 635 goto out_release_free_unlock;
636 } 636 }
637 rc = crypto_hash_update( 637 rc = crypto_hash_update(
638 &s->hash_desc, &s->hash_sg, 638 &s->hash_desc, &s->hash_sg,
639 s->auth_tok->token.password.session_key_encryption_key_bytes); 639 s->auth_tok->token.password.session_key_encryption_key_bytes);
640 if (rc) { 640 if (rc) {
641 printk(KERN_ERR 641 printk(KERN_ERR
642 "%s: Error updating crypto hash; rc = [%d]\n", 642 "%s: Error updating crypto hash; rc = [%d]\n",
643 __func__, rc); 643 __func__, rc);
644 goto out_release_free_unlock; 644 goto out_release_free_unlock;
645 } 645 }
646 rc = crypto_hash_final(&s->hash_desc, s->hash); 646 rc = crypto_hash_final(&s->hash_desc, s->hash);
647 if (rc) { 647 if (rc) {
648 printk(KERN_ERR 648 printk(KERN_ERR
649 "%s: Error finalizing crypto hash; rc = [%d]\n", 649 "%s: Error finalizing crypto hash; rc = [%d]\n",
650 __func__, rc); 650 __func__, rc);
651 goto out_release_free_unlock; 651 goto out_release_free_unlock;
652 } 652 }
653 for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) { 653 for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) {
654 s->block_aligned_filename[s->j] = 654 s->block_aligned_filename[s->j] =
655 s->hash[(s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)]; 655 s->hash[(s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)];
656 if ((s->j % ECRYPTFS_TAG_70_DIGEST_SIZE) 656 if ((s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)
657 == (ECRYPTFS_TAG_70_DIGEST_SIZE - 1)) { 657 == (ECRYPTFS_TAG_70_DIGEST_SIZE - 1)) {
658 sg_init_one(&s->hash_sg, (u8 *)s->hash, 658 sg_init_one(&s->hash_sg, (u8 *)s->hash,
659 ECRYPTFS_TAG_70_DIGEST_SIZE); 659 ECRYPTFS_TAG_70_DIGEST_SIZE);
660 rc = crypto_hash_init(&s->hash_desc); 660 rc = crypto_hash_init(&s->hash_desc);
661 if (rc) { 661 if (rc) {
662 printk(KERN_ERR 662 printk(KERN_ERR
663 "%s: Error initializing crypto hash; " 663 "%s: Error initializing crypto hash; "
664 "rc = [%d]\n", __func__, rc); 664 "rc = [%d]\n", __func__, rc);
665 goto out_release_free_unlock; 665 goto out_release_free_unlock;
666 } 666 }
667 rc = crypto_hash_update(&s->hash_desc, &s->hash_sg, 667 rc = crypto_hash_update(&s->hash_desc, &s->hash_sg,
668 ECRYPTFS_TAG_70_DIGEST_SIZE); 668 ECRYPTFS_TAG_70_DIGEST_SIZE);
669 if (rc) { 669 if (rc) {
670 printk(KERN_ERR 670 printk(KERN_ERR
671 "%s: Error updating crypto hash; " 671 "%s: Error updating crypto hash; "
672 "rc = [%d]\n", __func__, rc); 672 "rc = [%d]\n", __func__, rc);
673 goto out_release_free_unlock; 673 goto out_release_free_unlock;
674 } 674 }
675 rc = crypto_hash_final(&s->hash_desc, s->tmp_hash); 675 rc = crypto_hash_final(&s->hash_desc, s->tmp_hash);
676 if (rc) { 676 if (rc) {
677 printk(KERN_ERR 677 printk(KERN_ERR
678 "%s: Error finalizing crypto hash; " 678 "%s: Error finalizing crypto hash; "
679 "rc = [%d]\n", __func__, rc); 679 "rc = [%d]\n", __func__, rc);
680 goto out_release_free_unlock; 680 goto out_release_free_unlock;
681 } 681 }
682 memcpy(s->hash, s->tmp_hash, 682 memcpy(s->hash, s->tmp_hash,
683 ECRYPTFS_TAG_70_DIGEST_SIZE); 683 ECRYPTFS_TAG_70_DIGEST_SIZE);
684 } 684 }
685 if (s->block_aligned_filename[s->j] == '\0') 685 if (s->block_aligned_filename[s->j] == '\0')
686 s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL; 686 s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL;
687 } 687 }
688 memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename, 688 memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename,
689 filename_size); 689 filename_size);
690 rc = virt_to_scatterlist(s->block_aligned_filename, 690 rc = virt_to_scatterlist(s->block_aligned_filename,
691 s->block_aligned_filename_size, &s->src_sg, 1); 691 s->block_aligned_filename_size, &s->src_sg, 1);
692 if (rc != 1) { 692 if (rc != 1) {
693 printk(KERN_ERR "%s: Internal error whilst attempting to " 693 printk(KERN_ERR "%s: Internal error whilst attempting to "
694 "convert filename memory to scatterlist; " 694 "convert filename memory to scatterlist; "
695 "expected rc = 1; got rc = [%d]. " 695 "expected rc = 1; got rc = [%d]. "
696 "block_aligned_filename_size = [%zd]\n", __func__, rc, 696 "block_aligned_filename_size = [%zd]\n", __func__, rc,
697 s->block_aligned_filename_size); 697 s->block_aligned_filename_size);
698 goto out_release_free_unlock; 698 goto out_release_free_unlock;
699 } 699 }
700 rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size, 700 rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size,
701 &s->dst_sg, 1); 701 &s->dst_sg, 1);
702 if (rc != 1) { 702 if (rc != 1) {
703 printk(KERN_ERR "%s: Internal error whilst attempting to " 703 printk(KERN_ERR "%s: Internal error whilst attempting to "
704 "convert encrypted filename memory to scatterlist; " 704 "convert encrypted filename memory to scatterlist; "
705 "expected rc = 1; got rc = [%d]. " 705 "expected rc = 1; got rc = [%d]. "
706 "block_aligned_filename_size = [%zd]\n", __func__, rc, 706 "block_aligned_filename_size = [%zd]\n", __func__, rc,
707 s->block_aligned_filename_size); 707 s->block_aligned_filename_size);
708 goto out_release_free_unlock; 708 goto out_release_free_unlock;
709 } 709 }
710 /* The characters in the first block effectively do the job 710 /* The characters in the first block effectively do the job
711 * of the IV here, so we just use 0's for the IV. Note the 711 * of the IV here, so we just use 0's for the IV. Note the
712 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 712 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
713 * >= ECRYPTFS_MAX_IV_BYTES. */ 713 * >= ECRYPTFS_MAX_IV_BYTES. */
714 memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES); 714 memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES);
715 s->desc.info = s->iv; 715 s->desc.info = s->iv;
716 rc = crypto_blkcipher_setkey( 716 rc = crypto_blkcipher_setkey(
717 s->desc.tfm, 717 s->desc.tfm,
718 s->auth_tok->token.password.session_key_encryption_key, 718 s->auth_tok->token.password.session_key_encryption_key,
719 mount_crypt_stat->global_default_fn_cipher_key_bytes); 719 mount_crypt_stat->global_default_fn_cipher_key_bytes);
720 if (rc < 0) { 720 if (rc < 0) {
721 printk(KERN_ERR "%s: Error setting key for crypto context; " 721 printk(KERN_ERR "%s: Error setting key for crypto context; "
722 "rc = [%d]. s->auth_tok->token.password.session_key_" 722 "rc = [%d]. s->auth_tok->token.password.session_key_"
723 "encryption_key = [0x%p]; mount_crypt_stat->" 723 "encryption_key = [0x%p]; mount_crypt_stat->"
724 "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, 724 "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
725 rc, 725 rc,
726 s->auth_tok->token.password.session_key_encryption_key, 726 s->auth_tok->token.password.session_key_encryption_key,
727 mount_crypt_stat->global_default_fn_cipher_key_bytes); 727 mount_crypt_stat->global_default_fn_cipher_key_bytes);
728 goto out_release_free_unlock; 728 goto out_release_free_unlock;
729 } 729 }
730 rc = crypto_blkcipher_encrypt_iv(&s->desc, &s->dst_sg, &s->src_sg, 730 rc = crypto_blkcipher_encrypt_iv(&s->desc, &s->dst_sg, &s->src_sg,
731 s->block_aligned_filename_size); 731 s->block_aligned_filename_size);
732 if (rc) { 732 if (rc) {
733 printk(KERN_ERR "%s: Error attempting to encrypt filename; " 733 printk(KERN_ERR "%s: Error attempting to encrypt filename; "
734 "rc = [%d]\n", __func__, rc); 734 "rc = [%d]\n", __func__, rc);
735 goto out_release_free_unlock; 735 goto out_release_free_unlock;
736 } 736 }
737 s->i += s->block_aligned_filename_size; 737 s->i += s->block_aligned_filename_size;
738 (*packet_size) = s->i; 738 (*packet_size) = s->i;
739 (*remaining_bytes) -= (*packet_size); 739 (*remaining_bytes) -= (*packet_size);
740 out_release_free_unlock: 740 out_release_free_unlock:
741 crypto_free_hash(s->hash_desc.tfm); 741 crypto_free_hash(s->hash_desc.tfm);
742 out_free_unlock: 742 out_free_unlock:
743 memset(s->block_aligned_filename, 0, s->block_aligned_filename_size); 743 memset(s->block_aligned_filename, 0, s->block_aligned_filename_size);
744 kfree(s->block_aligned_filename); 744 kfree(s->block_aligned_filename);
745 out_unlock: 745 out_unlock:
746 mutex_unlock(s->tfm_mutex); 746 mutex_unlock(s->tfm_mutex);
747 out: 747 out:
748 kfree(s); 748 kfree(s);
749 return rc; 749 return rc;
750 } 750 }
751 751
752 struct ecryptfs_parse_tag_70_packet_silly_stack { 752 struct ecryptfs_parse_tag_70_packet_silly_stack {
753 u8 cipher_code; 753 u8 cipher_code;
754 size_t max_packet_size; 754 size_t max_packet_size;
755 size_t packet_size_len; 755 size_t packet_size_len;
756 size_t parsed_tag_70_packet_size; 756 size_t parsed_tag_70_packet_size;
757 size_t block_aligned_filename_size; 757 size_t block_aligned_filename_size;
758 size_t block_size; 758 size_t block_size;
759 size_t i; 759 size_t i;
760 struct mutex *tfm_mutex; 760 struct mutex *tfm_mutex;
761 char *decrypted_filename; 761 char *decrypted_filename;
762 struct ecryptfs_auth_tok *auth_tok; 762 struct ecryptfs_auth_tok *auth_tok;
763 struct scatterlist src_sg; 763 struct scatterlist src_sg;
764 struct scatterlist dst_sg; 764 struct scatterlist dst_sg;
765 struct blkcipher_desc desc; 765 struct blkcipher_desc desc;
766 char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1]; 766 char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1];
767 char iv[ECRYPTFS_MAX_IV_BYTES]; 767 char iv[ECRYPTFS_MAX_IV_BYTES];
768 char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE]; 768 char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE];
769 }; 769 };
770 770
771 /** 771 /**
772 * parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet 772 * parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet
773 * @filename: This function kmalloc's the memory for the filename 773 * @filename: This function kmalloc's the memory for the filename
774 * @filename_size: This function sets this to the amount of memory 774 * @filename_size: This function sets this to the amount of memory
775 * kmalloc'd for the filename 775 * kmalloc'd for the filename
776 * @packet_size: This function sets this to the the number of octets 776 * @packet_size: This function sets this to the the number of octets
777 * in the packet parsed 777 * in the packet parsed
778 * @mount_crypt_stat: The mount-wide cryptographic context 778 * @mount_crypt_stat: The mount-wide cryptographic context
779 * @data: The memory location containing the start of the tag 70 779 * @data: The memory location containing the start of the tag 70
780 * packet 780 * packet
781 * @max_packet_size: The maximum legal size of the packet to be parsed 781 * @max_packet_size: The maximum legal size of the packet to be parsed
782 * from @data 782 * from @data
783 * 783 *
784 * Returns zero on success; non-zero otherwise 784 * Returns zero on success; non-zero otherwise
785 */ 785 */
786 int 786 int
787 ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, 787 ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
788 size_t *packet_size, 788 size_t *packet_size,
789 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 789 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
790 char *data, size_t max_packet_size) 790 char *data, size_t max_packet_size)
791 { 791 {
792 struct ecryptfs_parse_tag_70_packet_silly_stack *s; 792 struct ecryptfs_parse_tag_70_packet_silly_stack *s;
793 int rc = 0; 793 int rc = 0;
794 794
795 (*packet_size) = 0; 795 (*packet_size) = 0;
796 (*filename_size) = 0; 796 (*filename_size) = 0;
797 (*filename) = NULL; 797 (*filename) = NULL;
798 s = kmalloc(sizeof(*s), GFP_KERNEL); 798 s = kmalloc(sizeof(*s), GFP_KERNEL);
799 if (!s) { 799 if (!s) {
800 printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc " 800 printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
801 "[%zd] bytes of kernel memory\n", __func__, sizeof(*s)); 801 "[%zd] bytes of kernel memory\n", __func__, sizeof(*s));
802 goto out; 802 goto out;
803 } 803 }
804 s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; 804 s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
805 if (max_packet_size < (1 + 1 + ECRYPTFS_SIG_SIZE + 1 + 1)) { 805 if (max_packet_size < (1 + 1 + ECRYPTFS_SIG_SIZE + 1 + 1)) {
806 printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be " 806 printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be "
807 "at least [%d]\n", __func__, max_packet_size, 807 "at least [%d]\n", __func__, max_packet_size,
808 (1 + 1 + ECRYPTFS_SIG_SIZE + 1 + 1)); 808 (1 + 1 + ECRYPTFS_SIG_SIZE + 1 + 1));
809 rc = -EINVAL; 809 rc = -EINVAL;
810 goto out; 810 goto out;
811 } 811 }
812 /* Octet 0: Tag 70 identifier 812 /* Octet 0: Tag 70 identifier
813 * Octets 1-N1: Tag 70 packet size (includes cipher identifier 813 * Octets 1-N1: Tag 70 packet size (includes cipher identifier
814 * and block-aligned encrypted filename size) 814 * and block-aligned encrypted filename size)
815 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) 815 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
816 * Octet N2-N3: Cipher identifier (1 octet) 816 * Octet N2-N3: Cipher identifier (1 octet)
817 * Octets N3-N4: Block-aligned encrypted filename 817 * Octets N3-N4: Block-aligned encrypted filename
818 * - Consists of a minimum number of random numbers, a \0 818 * - Consists of a minimum number of random numbers, a \0
819 * separator, and then the filename */ 819 * separator, and then the filename */
820 if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) { 820 if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) {
821 printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be " 821 printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be "
822 "tag [0x%.2x]\n", __func__, 822 "tag [0x%.2x]\n", __func__,
823 data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE); 823 data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE);
824 rc = -EINVAL; 824 rc = -EINVAL;
825 goto out; 825 goto out;
826 } 826 }
827 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], 827 rc = ecryptfs_parse_packet_length(&data[(*packet_size)],
828 &s->parsed_tag_70_packet_size, 828 &s->parsed_tag_70_packet_size,
829 &s->packet_size_len); 829 &s->packet_size_len);
830 if (rc) { 830 if (rc) {
831 printk(KERN_WARNING "%s: Error parsing packet length; " 831 printk(KERN_WARNING "%s: Error parsing packet length; "
832 "rc = [%d]\n", __func__, rc); 832 "rc = [%d]\n", __func__, rc);
833 goto out; 833 goto out;
834 } 834 }
835 s->block_aligned_filename_size = (s->parsed_tag_70_packet_size 835 s->block_aligned_filename_size = (s->parsed_tag_70_packet_size
836 - ECRYPTFS_SIG_SIZE - 1); 836 - ECRYPTFS_SIG_SIZE - 1);
837 if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size) 837 if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size)
838 > max_packet_size) { 838 > max_packet_size) {
839 printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet " 839 printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet "
840 "size is [%zd]\n", __func__, max_packet_size, 840 "size is [%zd]\n", __func__, max_packet_size,
841 (1 + s->packet_size_len + 1 841 (1 + s->packet_size_len + 1
842 + s->block_aligned_filename_size)); 842 + s->block_aligned_filename_size));
843 rc = -EINVAL; 843 rc = -EINVAL;
844 goto out; 844 goto out;
845 } 845 }
846 (*packet_size) += s->packet_size_len; 846 (*packet_size) += s->packet_size_len;
847 ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)], 847 ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)],
848 ECRYPTFS_SIG_SIZE); 848 ECRYPTFS_SIG_SIZE);
849 s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 849 s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0';
850 (*packet_size) += ECRYPTFS_SIG_SIZE; 850 (*packet_size) += ECRYPTFS_SIG_SIZE;
851 s->cipher_code = data[(*packet_size)++]; 851 s->cipher_code = data[(*packet_size)++];
852 rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code); 852 rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code);
853 if (rc) { 853 if (rc) {
854 printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n", 854 printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n",
855 __func__, s->cipher_code); 855 __func__, s->cipher_code);
856 goto out; 856 goto out;
857 } 857 }
858 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->desc.tfm, 858 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->desc.tfm,
859 &s->tfm_mutex, 859 &s->tfm_mutex,
860 s->cipher_string); 860 s->cipher_string);
861 if (unlikely(rc)) { 861 if (unlikely(rc)) {
862 printk(KERN_ERR "Internal error whilst attempting to get " 862 printk(KERN_ERR "Internal error whilst attempting to get "
863 "tfm and mutex for cipher name [%s]; rc = [%d]\n", 863 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
864 s->cipher_string, rc); 864 s->cipher_string, rc);
865 goto out; 865 goto out;
866 } 866 }
867 mutex_lock(s->tfm_mutex); 867 mutex_lock(s->tfm_mutex);
868 rc = virt_to_scatterlist(&data[(*packet_size)], 868 rc = virt_to_scatterlist(&data[(*packet_size)],
869 s->block_aligned_filename_size, &s->src_sg, 1); 869 s->block_aligned_filename_size, &s->src_sg, 1);
870 if (rc != 1) { 870 if (rc != 1) {
871 printk(KERN_ERR "%s: Internal error whilst attempting to " 871 printk(KERN_ERR "%s: Internal error whilst attempting to "
872 "convert encrypted filename memory to scatterlist; " 872 "convert encrypted filename memory to scatterlist; "
873 "expected rc = 1; got rc = [%d]. " 873 "expected rc = 1; got rc = [%d]. "
874 "block_aligned_filename_size = [%zd]\n", __func__, rc, 874 "block_aligned_filename_size = [%zd]\n", __func__, rc,
875 s->block_aligned_filename_size); 875 s->block_aligned_filename_size);
876 goto out_unlock; 876 goto out_unlock;
877 } 877 }
878 (*packet_size) += s->block_aligned_filename_size; 878 (*packet_size) += s->block_aligned_filename_size;
879 s->decrypted_filename = kmalloc(s->block_aligned_filename_size, 879 s->decrypted_filename = kmalloc(s->block_aligned_filename_size,
880 GFP_KERNEL); 880 GFP_KERNEL);
881 if (!s->decrypted_filename) { 881 if (!s->decrypted_filename) {
882 printk(KERN_ERR "%s: Out of memory whilst attempting to " 882 printk(KERN_ERR "%s: Out of memory whilst attempting to "
883 "kmalloc [%zd] bytes\n", __func__, 883 "kmalloc [%zd] bytes\n", __func__,
884 s->block_aligned_filename_size); 884 s->block_aligned_filename_size);
885 rc = -ENOMEM; 885 rc = -ENOMEM;
886 goto out_unlock; 886 goto out_unlock;
887 } 887 }
888 rc = virt_to_scatterlist(s->decrypted_filename, 888 rc = virt_to_scatterlist(s->decrypted_filename,
889 s->block_aligned_filename_size, &s->dst_sg, 1); 889 s->block_aligned_filename_size, &s->dst_sg, 1);
890 if (rc != 1) { 890 if (rc != 1) {
891 printk(KERN_ERR "%s: Internal error whilst attempting to " 891 printk(KERN_ERR "%s: Internal error whilst attempting to "
892 "convert decrypted filename memory to scatterlist; " 892 "convert decrypted filename memory to scatterlist; "
893 "expected rc = 1; got rc = [%d]. " 893 "expected rc = 1; got rc = [%d]. "
894 "block_aligned_filename_size = [%zd]\n", __func__, rc, 894 "block_aligned_filename_size = [%zd]\n", __func__, rc,
895 s->block_aligned_filename_size); 895 s->block_aligned_filename_size);
896 goto out_free_unlock; 896 goto out_free_unlock;
897 } 897 }
898 /* The characters in the first block effectively do the job of 898 /* The characters in the first block effectively do the job of
899 * the IV here, so we just use 0's for the IV. Note the 899 * the IV here, so we just use 0's for the IV. Note the
900 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 900 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
901 * >= ECRYPTFS_MAX_IV_BYTES. */ 901 * >= ECRYPTFS_MAX_IV_BYTES. */
902 memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES); 902 memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES);
903 s->desc.info = s->iv; 903 s->desc.info = s->iv;
904 rc = ecryptfs_find_auth_tok_for_sig(&s->auth_tok, mount_crypt_stat, 904 rc = ecryptfs_find_auth_tok_for_sig(&s->auth_tok, mount_crypt_stat,
905 s->fnek_sig_hex); 905 s->fnek_sig_hex);
906 if (rc) { 906 if (rc) {
907 printk(KERN_ERR "%s: Error attempting to find auth tok for " 907 printk(KERN_ERR "%s: Error attempting to find auth tok for "
908 "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex, 908 "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex,
909 rc); 909 rc);
910 goto out_free_unlock; 910 goto out_free_unlock;
911 } 911 }
912 /* TODO: Support other key modules than passphrase for 912 /* TODO: Support other key modules than passphrase for
913 * filename encryption */ 913 * filename encryption */
914 BUG_ON(s->auth_tok->token_type != ECRYPTFS_PASSWORD); 914 BUG_ON(s->auth_tok->token_type != ECRYPTFS_PASSWORD);
915 rc = crypto_blkcipher_setkey( 915 rc = crypto_blkcipher_setkey(
916 s->desc.tfm, 916 s->desc.tfm,
917 s->auth_tok->token.password.session_key_encryption_key, 917 s->auth_tok->token.password.session_key_encryption_key,
918 mount_crypt_stat->global_default_fn_cipher_key_bytes); 918 mount_crypt_stat->global_default_fn_cipher_key_bytes);
919 if (rc < 0) { 919 if (rc < 0) {
920 printk(KERN_ERR "%s: Error setting key for crypto context; " 920 printk(KERN_ERR "%s: Error setting key for crypto context; "
921 "rc = [%d]. s->auth_tok->token.password.session_key_" 921 "rc = [%d]. s->auth_tok->token.password.session_key_"
922 "encryption_key = [0x%p]; mount_crypt_stat->" 922 "encryption_key = [0x%p]; mount_crypt_stat->"
923 "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, 923 "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
924 rc, 924 rc,
925 s->auth_tok->token.password.session_key_encryption_key, 925 s->auth_tok->token.password.session_key_encryption_key,
926 mount_crypt_stat->global_default_fn_cipher_key_bytes); 926 mount_crypt_stat->global_default_fn_cipher_key_bytes);
927 goto out_free_unlock; 927 goto out_free_unlock;
928 } 928 }
929 rc = crypto_blkcipher_decrypt_iv(&s->desc, &s->dst_sg, &s->src_sg, 929 rc = crypto_blkcipher_decrypt_iv(&s->desc, &s->dst_sg, &s->src_sg,
930 s->block_aligned_filename_size); 930 s->block_aligned_filename_size);
931 if (rc) { 931 if (rc) {
932 printk(KERN_ERR "%s: Error attempting to decrypt filename; " 932 printk(KERN_ERR "%s: Error attempting to decrypt filename; "
933 "rc = [%d]\n", __func__, rc); 933 "rc = [%d]\n", __func__, rc);
934 goto out_free_unlock; 934 goto out_free_unlock;
935 } 935 }
936 s->i = 0; 936 s->i = 0;
937 while (s->decrypted_filename[s->i] != '\0' 937 while (s->decrypted_filename[s->i] != '\0'
938 && s->i < s->block_aligned_filename_size) 938 && s->i < s->block_aligned_filename_size)
939 s->i++; 939 s->i++;
940 if (s->i == s->block_aligned_filename_size) { 940 if (s->i == s->block_aligned_filename_size) {
941 printk(KERN_WARNING "%s: Invalid tag 70 packet; could not " 941 printk(KERN_WARNING "%s: Invalid tag 70 packet; could not "
942 "find valid separator between random characters and " 942 "find valid separator between random characters and "
943 "the filename\n", __func__); 943 "the filename\n", __func__);
944 rc = -EINVAL; 944 rc = -EINVAL;
945 goto out_free_unlock; 945 goto out_free_unlock;
946 } 946 }
947 s->i++; 947 s->i++;
948 (*filename_size) = (s->block_aligned_filename_size - s->i); 948 (*filename_size) = (s->block_aligned_filename_size - s->i);
949 if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) { 949 if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) {
950 printk(KERN_WARNING "%s: Filename size is [%zd], which is " 950 printk(KERN_WARNING "%s: Filename size is [%zd], which is "
951 "invalid\n", __func__, (*filename_size)); 951 "invalid\n", __func__, (*filename_size));
952 rc = -EINVAL; 952 rc = -EINVAL;
953 goto out_free_unlock; 953 goto out_free_unlock;
954 } 954 }
955 (*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL); 955 (*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL);
956 if (!(*filename)) { 956 if (!(*filename)) {
957 printk(KERN_ERR "%s: Out of memory whilst attempting to " 957 printk(KERN_ERR "%s: Out of memory whilst attempting to "
958 "kmalloc [%zd] bytes\n", __func__, 958 "kmalloc [%zd] bytes\n", __func__,
959 ((*filename_size) + 1)); 959 ((*filename_size) + 1));
960 rc = -ENOMEM; 960 rc = -ENOMEM;
961 goto out_free_unlock; 961 goto out_free_unlock;
962 } 962 }
963 memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size)); 963 memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size));
964 (*filename)[(*filename_size)] = '\0'; 964 (*filename)[(*filename_size)] = '\0';
965 out_free_unlock: 965 out_free_unlock:
966 kfree(s->decrypted_filename); 966 kfree(s->decrypted_filename);
967 out_unlock: 967 out_unlock:
968 mutex_unlock(s->tfm_mutex); 968 mutex_unlock(s->tfm_mutex);
969 out: 969 out:
970 if (rc) { 970 if (rc) {
971 (*packet_size) = 0; 971 (*packet_size) = 0;
972 (*filename_size) = 0; 972 (*filename_size) = 0;
973 (*filename) = NULL; 973 (*filename) = NULL;
974 } 974 }
975 kfree(s); 975 kfree(s);
976 return rc; 976 return rc;
977 } 977 }
978 978
979 static int 979 static int
980 ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok) 980 ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
981 { 981 {
982 int rc = 0; 982 int rc = 0;
983 983
984 (*sig) = NULL; 984 (*sig) = NULL;
985 switch (auth_tok->token_type) { 985 switch (auth_tok->token_type) {
986 case ECRYPTFS_PASSWORD: 986 case ECRYPTFS_PASSWORD:
987 (*sig) = auth_tok->token.password.signature; 987 (*sig) = auth_tok->token.password.signature;
988 break; 988 break;
989 case ECRYPTFS_PRIVATE_KEY: 989 case ECRYPTFS_PRIVATE_KEY:
990 (*sig) = auth_tok->token.private_key.signature; 990 (*sig) = auth_tok->token.private_key.signature;
991 break; 991 break;
992 default: 992 default:
993 printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n", 993 printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
994 auth_tok->token_type); 994 auth_tok->token_type);
995 rc = -EINVAL; 995 rc = -EINVAL;
996 } 996 }
997 return rc; 997 return rc;
998 } 998 }
999 999
1000 /** 1000 /**
1001 * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok. 1001 * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok.
1002 * @auth_tok: The key authentication token used to decrypt the session key 1002 * @auth_tok: The key authentication token used to decrypt the session key
1003 * @crypt_stat: The cryptographic context 1003 * @crypt_stat: The cryptographic context
1004 * 1004 *
1005 * Returns zero on success; non-zero error otherwise. 1005 * Returns zero on success; non-zero error otherwise.
1006 */ 1006 */
1007 static int 1007 static int
1008 decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1008 decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1009 struct ecryptfs_crypt_stat *crypt_stat) 1009 struct ecryptfs_crypt_stat *crypt_stat)
1010 { 1010 {
1011 u8 cipher_code = 0; 1011 u8 cipher_code = 0;
1012 struct ecryptfs_msg_ctx *msg_ctx; 1012 struct ecryptfs_msg_ctx *msg_ctx;
1013 struct ecryptfs_message *msg = NULL; 1013 struct ecryptfs_message *msg = NULL;
1014 char *auth_tok_sig; 1014 char *auth_tok_sig;
1015 char *payload; 1015 char *payload;
1016 size_t payload_len; 1016 size_t payload_len;
1017 int rc; 1017 int rc;
1018 1018
1019 rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok); 1019 rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok);
1020 if (rc) { 1020 if (rc) {
1021 printk(KERN_ERR "Unrecognized auth tok type: [%d]\n", 1021 printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
1022 auth_tok->token_type); 1022 auth_tok->token_type);
1023 goto out; 1023 goto out;
1024 } 1024 }
1025 rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key), 1025 rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
1026 &payload, &payload_len); 1026 &payload, &payload_len);
1027 if (rc) { 1027 if (rc) {
1028 ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n"); 1028 ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n");
1029 goto out; 1029 goto out;
1030 } 1030 }
1031 rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); 1031 rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
1032 if (rc) { 1032 if (rc) {
1033 ecryptfs_printk(KERN_ERR, "Error sending message to " 1033 ecryptfs_printk(KERN_ERR, "Error sending message to "
1034 "ecryptfsd\n"); 1034 "ecryptfsd\n");
1035 goto out; 1035 goto out;
1036 } 1036 }
1037 rc = ecryptfs_wait_for_response(msg_ctx, &msg); 1037 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1038 if (rc) { 1038 if (rc) {
1039 ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet " 1039 ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
1040 "from the user space daemon\n"); 1040 "from the user space daemon\n");
1041 rc = -EIO; 1041 rc = -EIO;
1042 goto out; 1042 goto out;
1043 } 1043 }
1044 rc = parse_tag_65_packet(&(auth_tok->session_key), 1044 rc = parse_tag_65_packet(&(auth_tok->session_key),
1045 &cipher_code, msg); 1045 &cipher_code, msg);
1046 if (rc) { 1046 if (rc) {
1047 printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n", 1047 printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
1048 rc); 1048 rc);
1049 goto out; 1049 goto out;
1050 } 1050 }
1051 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1051 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1052 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1052 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1053 auth_tok->session_key.decrypted_key_size); 1053 auth_tok->session_key.decrypted_key_size);
1054 crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; 1054 crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
1055 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code); 1055 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
1056 if (rc) { 1056 if (rc) {
1057 ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n", 1057 ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
1058 cipher_code) 1058 cipher_code)
1059 goto out; 1059 goto out;
1060 } 1060 }
1061 crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1061 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
1062 if (ecryptfs_verbosity > 0) { 1062 if (ecryptfs_verbosity > 0) {
1063 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n"); 1063 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
1064 ecryptfs_dump_hex(crypt_stat->key, 1064 ecryptfs_dump_hex(crypt_stat->key,
1065 crypt_stat->key_size); 1065 crypt_stat->key_size);
1066 } 1066 }
1067 out: 1067 out:
1068 if (msg) 1068 if (msg)
1069 kfree(msg); 1069 kfree(msg);
1070 return rc; 1070 return rc;
1071 } 1071 }
1072 1072
1073 static void wipe_auth_tok_list(struct list_head *auth_tok_list_head) 1073 static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
1074 { 1074 {
1075 struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1075 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1076 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1076 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1077 1077
1078 list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp, 1078 list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
1079 auth_tok_list_head, list) { 1079 auth_tok_list_head, list) {
1080 list_del(&auth_tok_list_item->list); 1080 list_del(&auth_tok_list_item->list);
1081 kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1081 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1082 auth_tok_list_item); 1082 auth_tok_list_item);
1083 } 1083 }
1084 } 1084 }
1085 1085
1086 struct kmem_cache *ecryptfs_auth_tok_list_item_cache; 1086 struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
1087 1087
1088 /** 1088 /**
1089 * parse_tag_1_packet 1089 * parse_tag_1_packet
1090 * @crypt_stat: The cryptographic context to modify based on packet contents 1090 * @crypt_stat: The cryptographic context to modify based on packet contents
1091 * @data: The raw bytes of the packet. 1091 * @data: The raw bytes of the packet.
1092 * @auth_tok_list: eCryptfs parses packets into authentication tokens; 1092 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
1093 * a new authentication token will be placed at the 1093 * a new authentication token will be placed at the
1094 * end of this list for this packet. 1094 * end of this list for this packet.
1095 * @new_auth_tok: Pointer to a pointer to memory that this function 1095 * @new_auth_tok: Pointer to a pointer to memory that this function
1096 * allocates; sets the memory address of the pointer to 1096 * allocates; sets the memory address of the pointer to
1097 * NULL on error. This object is added to the 1097 * NULL on error. This object is added to the
1098 * auth_tok_list. 1098 * auth_tok_list.
1099 * @packet_size: This function writes the size of the parsed packet 1099 * @packet_size: This function writes the size of the parsed packet
1100 * into this memory location; zero on error. 1100 * into this memory location; zero on error.
1101 * @max_packet_size: The maximum allowable packet size 1101 * @max_packet_size: The maximum allowable packet size
1102 * 1102 *
1103 * Returns zero on success; non-zero on error. 1103 * Returns zero on success; non-zero on error.
1104 */ 1104 */
1105 static int 1105 static int
1106 parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat, 1106 parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
1107 unsigned char *data, struct list_head *auth_tok_list, 1107 unsigned char *data, struct list_head *auth_tok_list,
1108 struct ecryptfs_auth_tok **new_auth_tok, 1108 struct ecryptfs_auth_tok **new_auth_tok,
1109 size_t *packet_size, size_t max_packet_size) 1109 size_t *packet_size, size_t max_packet_size)
1110 { 1110 {
1111 size_t body_size; 1111 size_t body_size;
1112 struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1112 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1113 size_t length_size; 1113 size_t length_size;
1114 int rc = 0; 1114 int rc = 0;
1115 1115
1116 (*packet_size) = 0; 1116 (*packet_size) = 0;
1117 (*new_auth_tok) = NULL; 1117 (*new_auth_tok) = NULL;
1118 /** 1118 /**
1119 * This format is inspired by OpenPGP; see RFC 2440 1119 * This format is inspired by OpenPGP; see RFC 2440
1120 * packet tag 1 1120 * packet tag 1
1121 * 1121 *
1122 * Tag 1 identifier (1 byte) 1122 * Tag 1 identifier (1 byte)
1123 * Max Tag 1 packet size (max 3 bytes) 1123 * Max Tag 1 packet size (max 3 bytes)
1124 * Version (1 byte) 1124 * Version (1 byte)
1125 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE) 1125 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
1126 * Cipher identifier (1 byte) 1126 * Cipher identifier (1 byte)
1127 * Encrypted key size (arbitrary) 1127 * Encrypted key size (arbitrary)
1128 * 1128 *
1129 * 12 bytes minimum packet size 1129 * 12 bytes minimum packet size
1130 */ 1130 */
1131 if (unlikely(max_packet_size < 12)) { 1131 if (unlikely(max_packet_size < 12)) {
1132 printk(KERN_ERR "Invalid max packet size; must be >=12\n"); 1132 printk(KERN_ERR "Invalid max packet size; must be >=12\n");
1133 rc = -EINVAL; 1133 rc = -EINVAL;
1134 goto out; 1134 goto out;
1135 } 1135 }
1136 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { 1136 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
1137 printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n", 1137 printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
1138 ECRYPTFS_TAG_1_PACKET_TYPE); 1138 ECRYPTFS_TAG_1_PACKET_TYPE);
1139 rc = -EINVAL; 1139 rc = -EINVAL;
1140 goto out; 1140 goto out;
1141 } 1141 }
1142 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 1142 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
1143 * at end of function upon failure */ 1143 * at end of function upon failure */
1144 auth_tok_list_item = 1144 auth_tok_list_item =
1145 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, 1145 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
1146 GFP_KERNEL); 1146 GFP_KERNEL);
1147 if (!auth_tok_list_item) { 1147 if (!auth_tok_list_item) {
1148 printk(KERN_ERR "Unable to allocate memory\n"); 1148 printk(KERN_ERR "Unable to allocate memory\n");
1149 rc = -ENOMEM; 1149 rc = -ENOMEM;
1150 goto out; 1150 goto out;
1151 } 1151 }
1152 (*new_auth_tok) = &auth_tok_list_item->auth_tok; 1152 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
1153 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 1153 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
1154 &length_size); 1154 &length_size);
1155 if (rc) { 1155 if (rc) {
1156 printk(KERN_WARNING "Error parsing packet length; " 1156 printk(KERN_WARNING "Error parsing packet length; "
1157 "rc = [%d]\n", rc); 1157 "rc = [%d]\n", rc);
1158 goto out_free; 1158 goto out_free;
1159 } 1159 }
1160 if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) { 1160 if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
1161 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1161 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
1162 rc = -EINVAL; 1162 rc = -EINVAL;
1163 goto out_free; 1163 goto out_free;
1164 } 1164 }
1165 (*packet_size) += length_size; 1165 (*packet_size) += length_size;
1166 if (unlikely((*packet_size) + body_size > max_packet_size)) { 1166 if (unlikely((*packet_size) + body_size > max_packet_size)) {
1167 printk(KERN_WARNING "Packet size exceeds max\n"); 1167 printk(KERN_WARNING "Packet size exceeds max\n");
1168 rc = -EINVAL; 1168 rc = -EINVAL;
1169 goto out_free; 1169 goto out_free;
1170 } 1170 }
1171 if (unlikely(data[(*packet_size)++] != 0x03)) { 1171 if (unlikely(data[(*packet_size)++] != 0x03)) {
1172 printk(KERN_WARNING "Unknown version number [%d]\n", 1172 printk(KERN_WARNING "Unknown version number [%d]\n",
1173 data[(*packet_size) - 1]); 1173 data[(*packet_size) - 1]);
1174 rc = -EINVAL; 1174 rc = -EINVAL;
1175 goto out_free; 1175 goto out_free;
1176 } 1176 }
1177 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature, 1177 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
1178 &data[(*packet_size)], ECRYPTFS_SIG_SIZE); 1178 &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
1179 *packet_size += ECRYPTFS_SIG_SIZE; 1179 *packet_size += ECRYPTFS_SIG_SIZE;
1180 /* This byte is skipped because the kernel does not need to 1180 /* This byte is skipped because the kernel does not need to
1181 * know which public key encryption algorithm was used */ 1181 * know which public key encryption algorithm was used */
1182 (*packet_size)++; 1182 (*packet_size)++;
1183 (*new_auth_tok)->session_key.encrypted_key_size = 1183 (*new_auth_tok)->session_key.encrypted_key_size =
1184 body_size - (ECRYPTFS_SIG_SIZE + 2); 1184 body_size - (ECRYPTFS_SIG_SIZE + 2);
1185 if ((*new_auth_tok)->session_key.encrypted_key_size 1185 if ((*new_auth_tok)->session_key.encrypted_key_size
1186 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 1186 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
1187 printk(KERN_WARNING "Tag 1 packet contains key larger " 1187 printk(KERN_WARNING "Tag 1 packet contains key larger "
1188 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES"); 1188 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
1189 rc = -EINVAL; 1189 rc = -EINVAL;
1190 goto out; 1190 goto out;
1191 } 1191 }
1192 memcpy((*new_auth_tok)->session_key.encrypted_key, 1192 memcpy((*new_auth_tok)->session_key.encrypted_key,
1193 &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2))); 1193 &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
1194 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size; 1194 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
1195 (*new_auth_tok)->session_key.flags &= 1195 (*new_auth_tok)->session_key.flags &=
1196 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1196 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1197 (*new_auth_tok)->session_key.flags |= 1197 (*new_auth_tok)->session_key.flags |=
1198 ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 1198 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
1199 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY; 1199 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
1200 (*new_auth_tok)->flags = 0; 1200 (*new_auth_tok)->flags = 0;
1201 (*new_auth_tok)->session_key.flags &= 1201 (*new_auth_tok)->session_key.flags &=
1202 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 1202 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
1203 (*new_auth_tok)->session_key.flags &= 1203 (*new_auth_tok)->session_key.flags &=
1204 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 1204 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
1205 list_add(&auth_tok_list_item->list, auth_tok_list); 1205 list_add(&auth_tok_list_item->list, auth_tok_list);
1206 goto out; 1206 goto out;
1207 out_free: 1207 out_free:
1208 (*new_auth_tok) = NULL; 1208 (*new_auth_tok) = NULL;
1209 memset(auth_tok_list_item, 0, 1209 memset(auth_tok_list_item, 0,
1210 sizeof(struct ecryptfs_auth_tok_list_item)); 1210 sizeof(struct ecryptfs_auth_tok_list_item));
1211 kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1211 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1212 auth_tok_list_item); 1212 auth_tok_list_item);
1213 out: 1213 out:
1214 if (rc) 1214 if (rc)
1215 (*packet_size) = 0; 1215 (*packet_size) = 0;
1216 return rc; 1216 return rc;
1217 } 1217 }
1218 1218
1219 /** 1219 /**
1220 * parse_tag_3_packet 1220 * parse_tag_3_packet
1221 * @crypt_stat: The cryptographic context to modify based on packet 1221 * @crypt_stat: The cryptographic context to modify based on packet
1222 * contents. 1222 * contents.
1223 * @data: The raw bytes of the packet. 1223 * @data: The raw bytes of the packet.
1224 * @auth_tok_list: eCryptfs parses packets into authentication tokens; 1224 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
1225 * a new authentication token will be placed at the end 1225 * a new authentication token will be placed at the end
1226 * of this list for this packet. 1226 * of this list for this packet.
1227 * @new_auth_tok: Pointer to a pointer to memory that this function 1227 * @new_auth_tok: Pointer to a pointer to memory that this function
1228 * allocates; sets the memory address of the pointer to 1228 * allocates; sets the memory address of the pointer to
1229 * NULL on error. This object is added to the 1229 * NULL on error. This object is added to the
1230 * auth_tok_list. 1230 * auth_tok_list.
1231 * @packet_size: This function writes the size of the parsed packet 1231 * @packet_size: This function writes the size of the parsed packet
1232 * into this memory location; zero on error. 1232 * into this memory location; zero on error.
1233 * @max_packet_size: maximum number of bytes to parse 1233 * @max_packet_size: maximum number of bytes to parse
1234 * 1234 *
1235 * Returns zero on success; non-zero on error. 1235 * Returns zero on success; non-zero on error.
1236 */ 1236 */
1237 static int 1237 static int
1238 parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, 1238 parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
1239 unsigned char *data, struct list_head *auth_tok_list, 1239 unsigned char *data, struct list_head *auth_tok_list,
1240 struct ecryptfs_auth_tok **new_auth_tok, 1240 struct ecryptfs_auth_tok **new_auth_tok,
1241 size_t *packet_size, size_t max_packet_size) 1241 size_t *packet_size, size_t max_packet_size)
1242 { 1242 {
1243 size_t body_size; 1243 size_t body_size;
1244 struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1244 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1245 size_t length_size; 1245 size_t length_size;
1246 int rc = 0; 1246 int rc = 0;
1247 1247
1248 (*packet_size) = 0; 1248 (*packet_size) = 0;
1249 (*new_auth_tok) = NULL; 1249 (*new_auth_tok) = NULL;
1250 /** 1250 /**
1251 *This format is inspired by OpenPGP; see RFC 2440 1251 *This format is inspired by OpenPGP; see RFC 2440
1252 * packet tag 3 1252 * packet tag 3
1253 * 1253 *
1254 * Tag 3 identifier (1 byte) 1254 * Tag 3 identifier (1 byte)
1255 * Max Tag 3 packet size (max 3 bytes) 1255 * Max Tag 3 packet size (max 3 bytes)
1256 * Version (1 byte) 1256 * Version (1 byte)
1257 * Cipher code (1 byte) 1257 * Cipher code (1 byte)
1258 * S2K specifier (1 byte) 1258 * S2K specifier (1 byte)
1259 * Hash identifier (1 byte) 1259 * Hash identifier (1 byte)
1260 * Salt (ECRYPTFS_SALT_SIZE) 1260 * Salt (ECRYPTFS_SALT_SIZE)
1261 * Hash iterations (1 byte) 1261 * Hash iterations (1 byte)
1262 * Encrypted key (arbitrary) 1262 * Encrypted key (arbitrary)
1263 * 1263 *
1264 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size 1264 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
1265 */ 1265 */
1266 if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) { 1266 if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
1267 printk(KERN_ERR "Max packet size too large\n"); 1267 printk(KERN_ERR "Max packet size too large\n");
1268 rc = -EINVAL; 1268 rc = -EINVAL;
1269 goto out; 1269 goto out;
1270 } 1270 }
1271 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { 1271 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
1272 printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n", 1272 printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
1273 ECRYPTFS_TAG_3_PACKET_TYPE); 1273 ECRYPTFS_TAG_3_PACKET_TYPE);
1274 rc = -EINVAL; 1274 rc = -EINVAL;
1275 goto out; 1275 goto out;
1276 } 1276 }
1277 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 1277 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
1278 * at end of function upon failure */ 1278 * at end of function upon failure */
1279 auth_tok_list_item = 1279 auth_tok_list_item =
1280 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); 1280 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
1281 if (!auth_tok_list_item) { 1281 if (!auth_tok_list_item) {
1282 printk(KERN_ERR "Unable to allocate memory\n"); 1282 printk(KERN_ERR "Unable to allocate memory\n");
1283 rc = -ENOMEM; 1283 rc = -ENOMEM;
1284 goto out; 1284 goto out;
1285 } 1285 }
1286 (*new_auth_tok) = &auth_tok_list_item->auth_tok; 1286 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
1287 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 1287 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
1288 &length_size); 1288 &length_size);
1289 if (rc) { 1289 if (rc) {
1290 printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n", 1290 printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
1291 rc); 1291 rc);
1292 goto out_free; 1292 goto out_free;
1293 } 1293 }
1294 if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) { 1294 if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
1295 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1295 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
1296 rc = -EINVAL; 1296 rc = -EINVAL;
1297 goto out_free; 1297 goto out_free;
1298 } 1298 }
1299 (*packet_size) += length_size; 1299 (*packet_size) += length_size;
1300 if (unlikely((*packet_size) + body_size > max_packet_size)) { 1300 if (unlikely((*packet_size) + body_size > max_packet_size)) {
1301 printk(KERN_ERR "Packet size exceeds max\n"); 1301 printk(KERN_ERR "Packet size exceeds max\n");
1302 rc = -EINVAL; 1302 rc = -EINVAL;
1303 goto out_free; 1303 goto out_free;
1304 } 1304 }
1305 (*new_auth_tok)->session_key.encrypted_key_size = 1305 (*new_auth_tok)->session_key.encrypted_key_size =
1306 (body_size - (ECRYPTFS_SALT_SIZE + 5)); 1306 (body_size - (ECRYPTFS_SALT_SIZE + 5));
1307 if (unlikely(data[(*packet_size)++] != 0x04)) { 1307 if (unlikely(data[(*packet_size)++] != 0x04)) {
1308 printk(KERN_WARNING "Unknown version number [%d]\n", 1308 printk(KERN_WARNING "Unknown version number [%d]\n",
1309 data[(*packet_size) - 1]); 1309 data[(*packet_size) - 1]);
1310 rc = -EINVAL; 1310 rc = -EINVAL;
1311 goto out_free; 1311 goto out_free;
1312 } 1312 }
1313 ecryptfs_cipher_code_to_string(crypt_stat->cipher, 1313 ecryptfs_cipher_code_to_string(crypt_stat->cipher,
1314 (u16)data[(*packet_size)]); 1314 (u16)data[(*packet_size)]);
1315 /* A little extra work to differentiate among the AES key 1315 /* A little extra work to differentiate among the AES key
1316 * sizes; see RFC2440 */ 1316 * sizes; see RFC2440 */
1317 switch(data[(*packet_size)++]) { 1317 switch(data[(*packet_size)++]) {
1318 case RFC2440_CIPHER_AES_192: 1318 case RFC2440_CIPHER_AES_192:
1319 crypt_stat->key_size = 24; 1319 crypt_stat->key_size = 24;
1320 break; 1320 break;
1321 default: 1321 default:
1322 crypt_stat->key_size = 1322 crypt_stat->key_size =
1323 (*new_auth_tok)->session_key.encrypted_key_size; 1323 (*new_auth_tok)->session_key.encrypted_key_size;
1324 } 1324 }
1325 ecryptfs_init_crypt_ctx(crypt_stat); 1325 ecryptfs_init_crypt_ctx(crypt_stat);
1326 if (unlikely(data[(*packet_size)++] != 0x03)) { 1326 if (unlikely(data[(*packet_size)++] != 0x03)) {
1327 printk(KERN_WARNING "Only S2K ID 3 is currently supported\n"); 1327 printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
1328 rc = -ENOSYS; 1328 rc = -ENOSYS;
1329 goto out_free; 1329 goto out_free;
1330 } 1330 }
1331 /* TODO: finish the hash mapping */ 1331 /* TODO: finish the hash mapping */
1332 switch (data[(*packet_size)++]) { 1332 switch (data[(*packet_size)++]) {
1333 case 0x01: /* See RFC2440 for these numbers and their mappings */ 1333 case 0x01: /* See RFC2440 for these numbers and their mappings */
1334 /* Choose MD5 */ 1334 /* Choose MD5 */
1335 memcpy((*new_auth_tok)->token.password.salt, 1335 memcpy((*new_auth_tok)->token.password.salt,
1336 &data[(*packet_size)], ECRYPTFS_SALT_SIZE); 1336 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
1337 (*packet_size) += ECRYPTFS_SALT_SIZE; 1337 (*packet_size) += ECRYPTFS_SALT_SIZE;
1338 /* This conversion was taken straight from RFC2440 */ 1338 /* This conversion was taken straight from RFC2440 */
1339 (*new_auth_tok)->token.password.hash_iterations = 1339 (*new_auth_tok)->token.password.hash_iterations =
1340 ((u32) 16 + (data[(*packet_size)] & 15)) 1340 ((u32) 16 + (data[(*packet_size)] & 15))
1341 << ((data[(*packet_size)] >> 4) + 6); 1341 << ((data[(*packet_size)] >> 4) + 6);
1342 (*packet_size)++; 1342 (*packet_size)++;
1343 /* Friendly reminder: 1343 /* Friendly reminder:
1344 * (*new_auth_tok)->session_key.encrypted_key_size = 1344 * (*new_auth_tok)->session_key.encrypted_key_size =
1345 * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */ 1345 * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
1346 memcpy((*new_auth_tok)->session_key.encrypted_key, 1346 memcpy((*new_auth_tok)->session_key.encrypted_key,
1347 &data[(*packet_size)], 1347 &data[(*packet_size)],
1348 (*new_auth_tok)->session_key.encrypted_key_size); 1348 (*new_auth_tok)->session_key.encrypted_key_size);
1349 (*packet_size) += 1349 (*packet_size) +=
1350 (*new_auth_tok)->session_key.encrypted_key_size; 1350 (*new_auth_tok)->session_key.encrypted_key_size;
1351 (*new_auth_tok)->session_key.flags &= 1351 (*new_auth_tok)->session_key.flags &=
1352 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1352 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1353 (*new_auth_tok)->session_key.flags |= 1353 (*new_auth_tok)->session_key.flags |=
1354 ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 1354 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
1355 (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */ 1355 (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
1356 break; 1356 break;
1357 default: 1357 default:
1358 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: " 1358 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
1359 "[%d]\n", data[(*packet_size) - 1]); 1359 "[%d]\n", data[(*packet_size) - 1]);
1360 rc = -ENOSYS; 1360 rc = -ENOSYS;
1361 goto out_free; 1361 goto out_free;
1362 } 1362 }
1363 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD; 1363 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
1364 /* TODO: Parametarize; we might actually want userspace to 1364 /* TODO: Parametarize; we might actually want userspace to
1365 * decrypt the session key. */ 1365 * decrypt the session key. */
1366 (*new_auth_tok)->session_key.flags &= 1366 (*new_auth_tok)->session_key.flags &=
1367 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 1367 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
1368 (*new_auth_tok)->session_key.flags &= 1368 (*new_auth_tok)->session_key.flags &=
1369 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 1369 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
1370 list_add(&auth_tok_list_item->list, auth_tok_list); 1370 list_add(&auth_tok_list_item->list, auth_tok_list);
1371 goto out; 1371 goto out;
1372 out_free: 1372 out_free:
1373 (*new_auth_tok) = NULL; 1373 (*new_auth_tok) = NULL;
1374 memset(auth_tok_list_item, 0, 1374 memset(auth_tok_list_item, 0,
1375 sizeof(struct ecryptfs_auth_tok_list_item)); 1375 sizeof(struct ecryptfs_auth_tok_list_item));
1376 kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1376 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1377 auth_tok_list_item); 1377 auth_tok_list_item);
1378 out: 1378 out:
1379 if (rc) 1379 if (rc)
1380 (*packet_size) = 0; 1380 (*packet_size) = 0;
1381 return rc; 1381 return rc;
1382 } 1382 }
1383 1383
1384 /** 1384 /**
1385 * parse_tag_11_packet 1385 * parse_tag_11_packet
1386 * @data: The raw bytes of the packet 1386 * @data: The raw bytes of the packet
1387 * @contents: This function writes the data contents of the literal 1387 * @contents: This function writes the data contents of the literal
1388 * packet into this memory location 1388 * packet into this memory location
1389 * @max_contents_bytes: The maximum number of bytes that this function 1389 * @max_contents_bytes: The maximum number of bytes that this function
1390 * is allowed to write into contents 1390 * is allowed to write into contents
1391 * @tag_11_contents_size: This function writes the size of the parsed 1391 * @tag_11_contents_size: This function writes the size of the parsed
1392 * contents into this memory location; zero on 1392 * contents into this memory location; zero on
1393 * error 1393 * error
1394 * @packet_size: This function writes the size of the parsed packet 1394 * @packet_size: This function writes the size of the parsed packet
1395 * into this memory location; zero on error 1395 * into this memory location; zero on error
1396 * @max_packet_size: maximum number of bytes to parse 1396 * @max_packet_size: maximum number of bytes to parse
1397 * 1397 *
1398 * Returns zero on success; non-zero on error. 1398 * Returns zero on success; non-zero on error.
1399 */ 1399 */
1400 static int 1400 static int
1401 parse_tag_11_packet(unsigned char *data, unsigned char *contents, 1401 parse_tag_11_packet(unsigned char *data, unsigned char *contents,
1402 size_t max_contents_bytes, size_t *tag_11_contents_size, 1402 size_t max_contents_bytes, size_t *tag_11_contents_size,
1403 size_t *packet_size, size_t max_packet_size) 1403 size_t *packet_size, size_t max_packet_size)
1404 { 1404 {
1405 size_t body_size; 1405 size_t body_size;
1406 size_t length_size; 1406 size_t length_size;
1407 int rc = 0; 1407 int rc = 0;
1408 1408
1409 (*packet_size) = 0; 1409 (*packet_size) = 0;
1410 (*tag_11_contents_size) = 0; 1410 (*tag_11_contents_size) = 0;
1411 /* This format is inspired by OpenPGP; see RFC 2440 1411 /* This format is inspired by OpenPGP; see RFC 2440
1412 * packet tag 11 1412 * packet tag 11
1413 * 1413 *
1414 * Tag 11 identifier (1 byte) 1414 * Tag 11 identifier (1 byte)
1415 * Max Tag 11 packet size (max 3 bytes) 1415 * Max Tag 11 packet size (max 3 bytes)
1416 * Binary format specifier (1 byte) 1416 * Binary format specifier (1 byte)
1417 * Filename length (1 byte) 1417 * Filename length (1 byte)
1418 * Filename ("_CONSOLE") (8 bytes) 1418 * Filename ("_CONSOLE") (8 bytes)
1419 * Modification date (4 bytes) 1419 * Modification date (4 bytes)
1420 * Literal data (arbitrary) 1420 * Literal data (arbitrary)
1421 * 1421 *
1422 * We need at least 16 bytes of data for the packet to even be 1422 * We need at least 16 bytes of data for the packet to even be
1423 * valid. 1423 * valid.
1424 */ 1424 */
1425 if (max_packet_size < 16) { 1425 if (max_packet_size < 16) {
1426 printk(KERN_ERR "Maximum packet size too small\n"); 1426 printk(KERN_ERR "Maximum packet size too small\n");
1427 rc = -EINVAL; 1427 rc = -EINVAL;
1428 goto out; 1428 goto out;
1429 } 1429 }
1430 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { 1430 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
1431 printk(KERN_WARNING "Invalid tag 11 packet format\n"); 1431 printk(KERN_WARNING "Invalid tag 11 packet format\n");
1432 rc = -EINVAL; 1432 rc = -EINVAL;
1433 goto out; 1433 goto out;
1434 } 1434 }
1435 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 1435 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
1436 &length_size); 1436 &length_size);
1437 if (rc) { 1437 if (rc) {
1438 printk(KERN_WARNING "Invalid tag 11 packet format\n"); 1438 printk(KERN_WARNING "Invalid tag 11 packet format\n");
1439 goto out; 1439 goto out;
1440 } 1440 }
1441 if (body_size < 14) { 1441 if (body_size < 14) {
1442 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1442 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
1443 rc = -EINVAL; 1443 rc = -EINVAL;
1444 goto out; 1444 goto out;
1445 } 1445 }
1446 (*packet_size) += length_size; 1446 (*packet_size) += length_size;
1447 (*tag_11_contents_size) = (body_size - 14); 1447 (*tag_11_contents_size) = (body_size - 14);
1448 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { 1448 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
1449 printk(KERN_ERR "Packet size exceeds max\n"); 1449 printk(KERN_ERR "Packet size exceeds max\n");
1450 rc = -EINVAL; 1450 rc = -EINVAL;
1451 goto out; 1451 goto out;
1452 } 1452 }
1453 if (data[(*packet_size)++] != 0x62) { 1453 if (data[(*packet_size)++] != 0x62) {
1454 printk(KERN_WARNING "Unrecognizable packet\n"); 1454 printk(KERN_WARNING "Unrecognizable packet\n");
1455 rc = -EINVAL; 1455 rc = -EINVAL;
1456 goto out; 1456 goto out;
1457 } 1457 }
1458 if (data[(*packet_size)++] != 0x08) { 1458 if (data[(*packet_size)++] != 0x08) {
1459 printk(KERN_WARNING "Unrecognizable packet\n"); 1459 printk(KERN_WARNING "Unrecognizable packet\n");
1460 rc = -EINVAL; 1460 rc = -EINVAL;
1461 goto out; 1461 goto out;
1462 } 1462 }
1463 (*packet_size) += 12; /* Ignore filename and modification date */ 1463 (*packet_size) += 12; /* Ignore filename and modification date */
1464 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); 1464 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
1465 (*packet_size) += (*tag_11_contents_size); 1465 (*packet_size) += (*tag_11_contents_size);
1466 out: 1466 out:
1467 if (rc) { 1467 if (rc) {
1468 (*packet_size) = 0; 1468 (*packet_size) = 0;
1469 (*tag_11_contents_size) = 0; 1469 (*tag_11_contents_size) = 0;
1470 } 1470 }
1471 return rc; 1471 return rc;
1472 } 1472 }
1473 1473
1474 /** 1474 /**
1475 * ecryptfs_verify_version 1475 * ecryptfs_verify_version
1476 * @version: The version number to confirm 1476 * @version: The version number to confirm
1477 * 1477 *
1478 * Returns zero on good version; non-zero otherwise 1478 * Returns zero on good version; non-zero otherwise
1479 */ 1479 */
1480 static int ecryptfs_verify_version(u16 version) 1480 static int ecryptfs_verify_version(u16 version)
1481 { 1481 {
1482 int rc = 0; 1482 int rc = 0;
1483 unsigned char major; 1483 unsigned char major;
1484 unsigned char minor; 1484 unsigned char minor;
1485 1485
1486 major = ((version >> 8) & 0xFF); 1486 major = ((version >> 8) & 0xFF);
1487 minor = (version & 0xFF); 1487 minor = (version & 0xFF);
1488 if (major != ECRYPTFS_VERSION_MAJOR) { 1488 if (major != ECRYPTFS_VERSION_MAJOR) {
1489 ecryptfs_printk(KERN_ERR, "Major version number mismatch. " 1489 ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
1490 "Expected [%d]; got [%d]\n", 1490 "Expected [%d]; got [%d]\n",
1491 ECRYPTFS_VERSION_MAJOR, major); 1491 ECRYPTFS_VERSION_MAJOR, major);
1492 rc = -EINVAL; 1492 rc = -EINVAL;
1493 goto out; 1493 goto out;
1494 } 1494 }
1495 if (minor != ECRYPTFS_VERSION_MINOR) { 1495 if (minor != ECRYPTFS_VERSION_MINOR) {
1496 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. " 1496 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
1497 "Expected [%d]; got [%d]\n", 1497 "Expected [%d]; got [%d]\n",
1498 ECRYPTFS_VERSION_MINOR, minor); 1498 ECRYPTFS_VERSION_MINOR, minor);
1499 rc = -EINVAL; 1499 rc = -EINVAL;
1500 goto out; 1500 goto out;
1501 } 1501 }
1502 out: 1502 out:
1503 return rc; 1503 return rc;
1504 } 1504 }
1505 1505
1506 int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, 1506 int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
1507 struct ecryptfs_auth_tok **auth_tok, 1507 struct ecryptfs_auth_tok **auth_tok,
1508 char *sig) 1508 char *sig)
1509 { 1509 {
1510 int rc = 0; 1510 int rc = 0;
1511 1511
1512 (*auth_tok_key) = request_key(&key_type_user, sig, NULL); 1512 (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
1513 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { 1513 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
1514 printk(KERN_ERR "Could not find key with description: [%s]\n", 1514 printk(KERN_ERR "Could not find key with description: [%s]\n",
1515 sig); 1515 sig);
1516 rc = process_request_key_err(PTR_ERR(*auth_tok_key)); 1516 rc = process_request_key_err(PTR_ERR(*auth_tok_key));
1517 goto out; 1517 goto out;
1518 } 1518 }
1519 (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key); 1519 (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
1520 if (ecryptfs_verify_version((*auth_tok)->version)) { 1520 if (ecryptfs_verify_version((*auth_tok)->version)) {
1521 printk(KERN_ERR 1521 printk(KERN_ERR
1522 "Data structure version mismatch. " 1522 "Data structure version mismatch. "
1523 "Userspace tools must match eCryptfs " 1523 "Userspace tools must match eCryptfs "
1524 "kernel module with major version [%d] " 1524 "kernel module with major version [%d] "
1525 "and minor version [%d]\n", 1525 "and minor version [%d]\n",
1526 ECRYPTFS_VERSION_MAJOR, 1526 ECRYPTFS_VERSION_MAJOR,
1527 ECRYPTFS_VERSION_MINOR); 1527 ECRYPTFS_VERSION_MINOR);
1528 rc = -EINVAL; 1528 rc = -EINVAL;
1529 goto out; 1529 goto out;
1530 } 1530 }
1531 if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD 1531 if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
1532 && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { 1532 && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
1533 printk(KERN_ERR "Invalid auth_tok structure " 1533 printk(KERN_ERR "Invalid auth_tok structure "
1534 "returned from key query\n"); 1534 "returned from key query\n");
1535 rc = -EINVAL; 1535 rc = -EINVAL;
1536 goto out; 1536 goto out;
1537 } 1537 }
1538 out: 1538 out:
1539 return rc; 1539 return rc;
1540 } 1540 }
1541 1541
1542 /** 1542 /**
1543 * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok. 1543 * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
1544 * @auth_tok: The passphrase authentication token to use to encrypt the FEK 1544 * @auth_tok: The passphrase authentication token to use to encrypt the FEK
1545 * @crypt_stat: The cryptographic context 1545 * @crypt_stat: The cryptographic context
1546 * 1546 *
1547 * Returns zero on success; non-zero error otherwise 1547 * Returns zero on success; non-zero error otherwise
1548 */ 1548 */
1549 static int 1549 static int
1550 decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1550 decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1551 struct ecryptfs_crypt_stat *crypt_stat) 1551 struct ecryptfs_crypt_stat *crypt_stat)
1552 { 1552 {
1553 struct scatterlist dst_sg[2]; 1553 struct scatterlist dst_sg[2];
1554 struct scatterlist src_sg[2]; 1554 struct scatterlist src_sg[2];
1555 struct mutex *tfm_mutex; 1555 struct mutex *tfm_mutex;
1556 struct blkcipher_desc desc = { 1556 struct blkcipher_desc desc = {
1557 .flags = CRYPTO_TFM_REQ_MAY_SLEEP 1557 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1558 }; 1558 };
1559 int rc = 0; 1559 int rc = 0;
1560 1560
1561 if (unlikely(ecryptfs_verbosity > 0)) { 1561 if (unlikely(ecryptfs_verbosity > 0)) {
1562 ecryptfs_printk( 1562 ecryptfs_printk(
1563 KERN_DEBUG, "Session key encryption key (size [%d]):\n", 1563 KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1564 auth_tok->token.password.session_key_encryption_key_bytes); 1564 auth_tok->token.password.session_key_encryption_key_bytes);
1565 ecryptfs_dump_hex( 1565 ecryptfs_dump_hex(
1566 auth_tok->token.password.session_key_encryption_key, 1566 auth_tok->token.password.session_key_encryption_key,
1567 auth_tok->token.password.session_key_encryption_key_bytes); 1567 auth_tok->token.password.session_key_encryption_key_bytes);
1568 } 1568 }
1569 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, 1569 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1570 crypt_stat->cipher); 1570 crypt_stat->cipher);
1571 if (unlikely(rc)) { 1571 if (unlikely(rc)) {
1572 printk(KERN_ERR "Internal error whilst attempting to get " 1572 printk(KERN_ERR "Internal error whilst attempting to get "
1573 "tfm and mutex for cipher name [%s]; rc = [%d]\n", 1573 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1574 crypt_stat->cipher, rc); 1574 crypt_stat->cipher, rc);
1575 goto out; 1575 goto out;
1576 } 1576 }
1577 rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, 1577 rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1578 auth_tok->session_key.encrypted_key_size, 1578 auth_tok->session_key.encrypted_key_size,
1579 src_sg, 2); 1579 src_sg, 2);
1580 if (rc < 1 || rc > 2) { 1580 if (rc < 1 || rc > 2) {
1581 printk(KERN_ERR "Internal error whilst attempting to convert " 1581 printk(KERN_ERR "Internal error whilst attempting to convert "
1582 "auth_tok->session_key.encrypted_key to scatterlist; " 1582 "auth_tok->session_key.encrypted_key to scatterlist; "
1583 "expected rc = 1; got rc = [%d]. " 1583 "expected rc = 1; got rc = [%d]. "
1584 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, 1584 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1585 auth_tok->session_key.encrypted_key_size); 1585 auth_tok->session_key.encrypted_key_size);
1586 goto out; 1586 goto out;
1587 } 1587 }
1588 auth_tok->session_key.decrypted_key_size = 1588 auth_tok->session_key.decrypted_key_size =
1589 auth_tok->session_key.encrypted_key_size; 1589 auth_tok->session_key.encrypted_key_size;
1590 rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, 1590 rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1591 auth_tok->session_key.decrypted_key_size, 1591 auth_tok->session_key.decrypted_key_size,
1592 dst_sg, 2); 1592 dst_sg, 2);
1593 if (rc < 1 || rc > 2) { 1593 if (rc < 1 || rc > 2) {
1594 printk(KERN_ERR "Internal error whilst attempting to convert " 1594 printk(KERN_ERR "Internal error whilst attempting to convert "
1595 "auth_tok->session_key.decrypted_key to scatterlist; " 1595 "auth_tok->session_key.decrypted_key to scatterlist; "
1596 "expected rc = 1; got rc = [%d]\n", rc); 1596 "expected rc = 1; got rc = [%d]\n", rc);
1597 goto out; 1597 goto out;
1598 } 1598 }
1599 mutex_lock(tfm_mutex); 1599 mutex_lock(tfm_mutex);
1600 rc = crypto_blkcipher_setkey( 1600 rc = crypto_blkcipher_setkey(
1601 desc.tfm, auth_tok->token.password.session_key_encryption_key, 1601 desc.tfm, auth_tok->token.password.session_key_encryption_key,
1602 crypt_stat->key_size); 1602 crypt_stat->key_size);
1603 if (unlikely(rc < 0)) { 1603 if (unlikely(rc < 0)) {
1604 mutex_unlock(tfm_mutex); 1604 mutex_unlock(tfm_mutex);
1605 printk(KERN_ERR "Error setting key for crypto context\n"); 1605 printk(KERN_ERR "Error setting key for crypto context\n");
1606 rc = -EINVAL; 1606 rc = -EINVAL;
1607 goto out; 1607 goto out;
1608 } 1608 }
1609 rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg, 1609 rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg,
1610 auth_tok->session_key.encrypted_key_size); 1610 auth_tok->session_key.encrypted_key_size);
1611 mutex_unlock(tfm_mutex); 1611 mutex_unlock(tfm_mutex);
1612 if (unlikely(rc)) { 1612 if (unlikely(rc)) {
1613 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); 1613 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
1614 goto out; 1614 goto out;
1615 } 1615 }
1616 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1616 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1617 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1617 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1618 auth_tok->session_key.decrypted_key_size); 1618 auth_tok->session_key.decrypted_key_size);
1619 crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1619 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
1620 if (unlikely(ecryptfs_verbosity > 0)) { 1620 if (unlikely(ecryptfs_verbosity > 0)) {
1621 ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n", 1621 ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
1622 crypt_stat->key_size); 1622 crypt_stat->key_size);
1623 ecryptfs_dump_hex(crypt_stat->key, 1623 ecryptfs_dump_hex(crypt_stat->key,
1624 crypt_stat->key_size); 1624 crypt_stat->key_size);
1625 } 1625 }
1626 out: 1626 out:
1627 return rc; 1627 return rc;
1628 } 1628 }
1629 1629
1630 /** 1630 /**
1631 * ecryptfs_parse_packet_set 1631 * ecryptfs_parse_packet_set
1632 * @crypt_stat: The cryptographic context 1632 * @crypt_stat: The cryptographic context
1633 * @src: Virtual address of region of memory containing the packets 1633 * @src: Virtual address of region of memory containing the packets
1634 * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set 1634 * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
1635 * 1635 *
1636 * Get crypt_stat to have the file's session key if the requisite key 1636 * Get crypt_stat to have the file's session key if the requisite key
1637 * is available to decrypt the session key. 1637 * is available to decrypt the session key.
1638 * 1638 *
1639 * Returns Zero if a valid authentication token was retrieved and 1639 * Returns Zero if a valid authentication token was retrieved and
1640 * processed; negative value for file not encrypted or for error 1640 * processed; negative value for file not encrypted or for error
1641 * conditions. 1641 * conditions.
1642 */ 1642 */
1643 int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, 1643 int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1644 unsigned char *src, 1644 unsigned char *src,
1645 struct dentry *ecryptfs_dentry) 1645 struct dentry *ecryptfs_dentry)
1646 { 1646 {
1647 size_t i = 0; 1647 size_t i = 0;
1648 size_t found_auth_tok; 1648 size_t found_auth_tok;
1649 size_t next_packet_is_auth_tok_packet; 1649 size_t next_packet_is_auth_tok_packet;
1650 struct list_head auth_tok_list; 1650 struct list_head auth_tok_list;
1651 struct ecryptfs_auth_tok *matching_auth_tok; 1651 struct ecryptfs_auth_tok *matching_auth_tok;
1652 struct ecryptfs_auth_tok *candidate_auth_tok; 1652 struct ecryptfs_auth_tok *candidate_auth_tok;
1653 char *candidate_auth_tok_sig; 1653 char *candidate_auth_tok_sig;
1654 size_t packet_size; 1654 size_t packet_size;
1655 struct ecryptfs_auth_tok *new_auth_tok; 1655 struct ecryptfs_auth_tok *new_auth_tok;
1656 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; 1656 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
1657 struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1657 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1658 size_t tag_11_contents_size; 1658 size_t tag_11_contents_size;
1659 size_t tag_11_packet_size; 1659 size_t tag_11_packet_size;
1660 int rc = 0; 1660 int rc = 0;
1661 1661
1662 INIT_LIST_HEAD(&auth_tok_list); 1662 INIT_LIST_HEAD(&auth_tok_list);
1663 /* Parse the header to find as many packets as we can; these will be 1663 /* Parse the header to find as many packets as we can; these will be
1664 * added the our &auth_tok_list */ 1664 * added the our &auth_tok_list */
1665 next_packet_is_auth_tok_packet = 1; 1665 next_packet_is_auth_tok_packet = 1;
1666 while (next_packet_is_auth_tok_packet) { 1666 while (next_packet_is_auth_tok_packet) {
1667 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i); 1667 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1668 1668
1669 switch (src[i]) { 1669 switch (src[i]) {
1670 case ECRYPTFS_TAG_3_PACKET_TYPE: 1670 case ECRYPTFS_TAG_3_PACKET_TYPE:
1671 rc = parse_tag_3_packet(crypt_stat, 1671 rc = parse_tag_3_packet(crypt_stat,
1672 (unsigned char *)&src[i], 1672 (unsigned char *)&src[i],
1673 &auth_tok_list, &new_auth_tok, 1673 &auth_tok_list, &new_auth_tok,
1674 &packet_size, max_packet_size); 1674 &packet_size, max_packet_size);
1675 if (rc) { 1675 if (rc) {
1676 ecryptfs_printk(KERN_ERR, "Error parsing " 1676 ecryptfs_printk(KERN_ERR, "Error parsing "
1677 "tag 3 packet\n"); 1677 "tag 3 packet\n");
1678 rc = -EIO; 1678 rc = -EIO;
1679 goto out_wipe_list; 1679 goto out_wipe_list;
1680 } 1680 }
1681 i += packet_size; 1681 i += packet_size;
1682 rc = parse_tag_11_packet((unsigned char *)&src[i], 1682 rc = parse_tag_11_packet((unsigned char *)&src[i],
1683 sig_tmp_space, 1683 sig_tmp_space,
1684 ECRYPTFS_SIG_SIZE, 1684 ECRYPTFS_SIG_SIZE,
1685 &tag_11_contents_size, 1685 &tag_11_contents_size,
1686 &tag_11_packet_size, 1686 &tag_11_packet_size,
1687 max_packet_size); 1687 max_packet_size);
1688 if (rc) { 1688 if (rc) {
1689 ecryptfs_printk(KERN_ERR, "No valid " 1689 ecryptfs_printk(KERN_ERR, "No valid "
1690 "(ecryptfs-specific) literal " 1690 "(ecryptfs-specific) literal "
1691 "packet containing " 1691 "packet containing "
1692 "authentication token " 1692 "authentication token "
1693 "signature found after " 1693 "signature found after "
1694 "tag 3 packet\n"); 1694 "tag 3 packet\n");
1695 rc = -EIO; 1695 rc = -EIO;
1696 goto out_wipe_list; 1696 goto out_wipe_list;
1697 } 1697 }
1698 i += tag_11_packet_size; 1698 i += tag_11_packet_size;
1699 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { 1699 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1700 ecryptfs_printk(KERN_ERR, "Expected " 1700 ecryptfs_printk(KERN_ERR, "Expected "
1701 "signature of size [%d]; " 1701 "signature of size [%d]; "
1702 "read size [%d]\n", 1702 "read size [%d]\n",
1703 ECRYPTFS_SIG_SIZE, 1703 ECRYPTFS_SIG_SIZE,
1704 tag_11_contents_size); 1704 tag_11_contents_size);
1705 rc = -EIO; 1705 rc = -EIO;
1706 goto out_wipe_list; 1706 goto out_wipe_list;
1707 } 1707 }
1708 ecryptfs_to_hex(new_auth_tok->token.password.signature, 1708 ecryptfs_to_hex(new_auth_tok->token.password.signature,
1709 sig_tmp_space, tag_11_contents_size); 1709 sig_tmp_space, tag_11_contents_size);
1710 new_auth_tok->token.password.signature[ 1710 new_auth_tok->token.password.signature[
1711 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0'; 1711 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
1712 crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1712 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
1713 break; 1713 break;
1714 case ECRYPTFS_TAG_1_PACKET_TYPE: 1714 case ECRYPTFS_TAG_1_PACKET_TYPE:
1715 rc = parse_tag_1_packet(crypt_stat, 1715 rc = parse_tag_1_packet(crypt_stat,
1716 (unsigned char *)&src[i], 1716 (unsigned char *)&src[i],
1717 &auth_tok_list, &new_auth_tok, 1717 &auth_tok_list, &new_auth_tok,
1718 &packet_size, max_packet_size); 1718 &packet_size, max_packet_size);
1719 if (rc) { 1719 if (rc) {
1720 ecryptfs_printk(KERN_ERR, "Error parsing " 1720 ecryptfs_printk(KERN_ERR, "Error parsing "
1721 "tag 1 packet\n"); 1721 "tag 1 packet\n");
1722 rc = -EIO; 1722 rc = -EIO;
1723 goto out_wipe_list; 1723 goto out_wipe_list;
1724 } 1724 }
1725 i += packet_size; 1725 i += packet_size;
1726 crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1726 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
1727 break; 1727 break;
1728 case ECRYPTFS_TAG_11_PACKET_TYPE: 1728 case ECRYPTFS_TAG_11_PACKET_TYPE:
1729 ecryptfs_printk(KERN_WARNING, "Invalid packet set " 1729 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1730 "(Tag 11 not allowed by itself)\n"); 1730 "(Tag 11 not allowed by itself)\n");
1731 rc = -EIO; 1731 rc = -EIO;
1732 goto out_wipe_list; 1732 goto out_wipe_list;
1733 break; 1733 break;
1734 default: 1734 default:
1735 ecryptfs_printk(KERN_DEBUG, "No packet at offset " 1735 ecryptfs_printk(KERN_DEBUG, "No packet at offset "
1736 "[%d] of the file header; hex value of " 1736 "[%d] of the file header; hex value of "
1737 "character is [0x%.2x]\n", i, src[i]); 1737 "character is [0x%.2x]\n", i, src[i]);
1738 next_packet_is_auth_tok_packet = 0; 1738 next_packet_is_auth_tok_packet = 0;
1739 } 1739 }
1740 } 1740 }
1741 if (list_empty(&auth_tok_list)) { 1741 if (list_empty(&auth_tok_list)) {
1742 printk(KERN_ERR "The lower file appears to be a non-encrypted " 1742 printk(KERN_ERR "The lower file appears to be a non-encrypted "
1743 "eCryptfs file; this is not supported in this version " 1743 "eCryptfs file; this is not supported in this version "
1744 "of the eCryptfs kernel module\n"); 1744 "of the eCryptfs kernel module\n");
1745 rc = -EINVAL; 1745 rc = -EINVAL;
1746 goto out; 1746 goto out;
1747 } 1747 }
1748 /* auth_tok_list contains the set of authentication tokens 1748 /* auth_tok_list contains the set of authentication tokens
1749 * parsed from the metadata. We need to find a matching 1749 * parsed from the metadata. We need to find a matching
1750 * authentication token that has the secret component(s) 1750 * authentication token that has the secret component(s)
1751 * necessary to decrypt the EFEK in the auth_tok parsed from 1751 * necessary to decrypt the EFEK in the auth_tok parsed from
1752 * the metadata. There may be several potential matches, but 1752 * the metadata. There may be several potential matches, but
1753 * just one will be sufficient to decrypt to get the FEK. */ 1753 * just one will be sufficient to decrypt to get the FEK. */
1754 find_next_matching_auth_tok: 1754 find_next_matching_auth_tok:
1755 found_auth_tok = 0; 1755 found_auth_tok = 0;
1756 list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { 1756 list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
1757 candidate_auth_tok = &auth_tok_list_item->auth_tok; 1757 candidate_auth_tok = &auth_tok_list_item->auth_tok;
1758 if (unlikely(ecryptfs_verbosity > 0)) { 1758 if (unlikely(ecryptfs_verbosity > 0)) {
1759 ecryptfs_printk(KERN_DEBUG, 1759 ecryptfs_printk(KERN_DEBUG,
1760 "Considering cadidate auth tok:\n"); 1760 "Considering cadidate auth tok:\n");
1761 ecryptfs_dump_auth_tok(candidate_auth_tok); 1761 ecryptfs_dump_auth_tok(candidate_auth_tok);
1762 } 1762 }
1763 rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, 1763 rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
1764 candidate_auth_tok); 1764 candidate_auth_tok);
1765 if (rc) { 1765 if (rc) {
1766 printk(KERN_ERR 1766 printk(KERN_ERR
1767 "Unrecognized candidate auth tok type: [%d]\n", 1767 "Unrecognized candidate auth tok type: [%d]\n",
1768 candidate_auth_tok->token_type); 1768 candidate_auth_tok->token_type);
1769 rc = -EINVAL; 1769 rc = -EINVAL;
1770 goto out_wipe_list; 1770 goto out_wipe_list;
1771 } 1771 }
1772 ecryptfs_find_auth_tok_for_sig(&matching_auth_tok, 1772 ecryptfs_find_auth_tok_for_sig(&matching_auth_tok,
1773 crypt_stat->mount_crypt_stat, 1773 crypt_stat->mount_crypt_stat,
1774 candidate_auth_tok_sig); 1774 candidate_auth_tok_sig);
1775 if (matching_auth_tok) { 1775 if (matching_auth_tok) {
1776 found_auth_tok = 1; 1776 found_auth_tok = 1;
1777 goto found_matching_auth_tok; 1777 goto found_matching_auth_tok;
1778 } 1778 }
1779 } 1779 }
1780 if (!found_auth_tok) { 1780 if (!found_auth_tok) {
1781 ecryptfs_printk(KERN_ERR, "Could not find a usable " 1781 ecryptfs_printk(KERN_ERR, "Could not find a usable "
1782 "authentication token\n"); 1782 "authentication token\n");
1783 rc = -EIO; 1783 rc = -EIO;
1784 goto out_wipe_list; 1784 goto out_wipe_list;
1785 } 1785 }
1786 found_matching_auth_tok: 1786 found_matching_auth_tok:
1787 if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 1787 if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1788 memcpy(&(candidate_auth_tok->token.private_key), 1788 memcpy(&(candidate_auth_tok->token.private_key),
1789 &(matching_auth_tok->token.private_key), 1789 &(matching_auth_tok->token.private_key),
1790 sizeof(struct ecryptfs_private_key)); 1790 sizeof(struct ecryptfs_private_key));
1791 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, 1791 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
1792 crypt_stat); 1792 crypt_stat);
1793 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { 1793 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
1794 memcpy(&(candidate_auth_tok->token.password), 1794 memcpy(&(candidate_auth_tok->token.password),
1795 &(matching_auth_tok->token.password), 1795 &(matching_auth_tok->token.password),
1796 sizeof(struct ecryptfs_password)); 1796 sizeof(struct ecryptfs_password));
1797 rc = decrypt_passphrase_encrypted_session_key( 1797 rc = decrypt_passphrase_encrypted_session_key(
1798 candidate_auth_tok, crypt_stat); 1798 candidate_auth_tok, crypt_stat);
1799 } 1799 }
1800 if (rc) { 1800 if (rc) {
1801 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1801 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1802 1802
1803 ecryptfs_printk(KERN_WARNING, "Error decrypting the " 1803 ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1804 "session key for authentication token with sig " 1804 "session key for authentication token with sig "
1805 "[%.*s]; rc = [%d]. Removing auth tok " 1805 "[%.*s]; rc = [%d]. Removing auth tok "
1806 "candidate from the list and searching for " 1806 "candidate from the list and searching for "
1807 "the next match.\n", candidate_auth_tok_sig, 1807 "the next match.\n", candidate_auth_tok_sig,
1808 ECRYPTFS_SIG_SIZE_HEX, rc); 1808 ECRYPTFS_SIG_SIZE_HEX, rc);
1809 list_for_each_entry_safe(auth_tok_list_item, 1809 list_for_each_entry_safe(auth_tok_list_item,
1810 auth_tok_list_item_tmp, 1810 auth_tok_list_item_tmp,
1811 &auth_tok_list, list) { 1811 &auth_tok_list, list) {
1812 if (candidate_auth_tok 1812 if (candidate_auth_tok
1813 == &auth_tok_list_item->auth_tok) { 1813 == &auth_tok_list_item->auth_tok) {
1814 list_del(&auth_tok_list_item->list); 1814 list_del(&auth_tok_list_item->list);
1815 kmem_cache_free( 1815 kmem_cache_free(
1816 ecryptfs_auth_tok_list_item_cache, 1816 ecryptfs_auth_tok_list_item_cache,
1817 auth_tok_list_item); 1817 auth_tok_list_item);
1818 goto find_next_matching_auth_tok; 1818 goto find_next_matching_auth_tok;
1819 } 1819 }
1820 } 1820 }
1821 BUG(); 1821 BUG();
1822 } 1822 }
1823 rc = ecryptfs_compute_root_iv(crypt_stat); 1823 rc = ecryptfs_compute_root_iv(crypt_stat);
1824 if (rc) { 1824 if (rc) {
1825 ecryptfs_printk(KERN_ERR, "Error computing " 1825 ecryptfs_printk(KERN_ERR, "Error computing "
1826 "the root IV\n"); 1826 "the root IV\n");
1827 goto out_wipe_list; 1827 goto out_wipe_list;
1828 } 1828 }
1829 rc = ecryptfs_init_crypt_ctx(crypt_stat); 1829 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1830 if (rc) { 1830 if (rc) {
1831 ecryptfs_printk(KERN_ERR, "Error initializing crypto " 1831 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1832 "context for cipher [%s]; rc = [%d]\n", 1832 "context for cipher [%s]; rc = [%d]\n",
1833 crypt_stat->cipher, rc); 1833 crypt_stat->cipher, rc);
1834 } 1834 }
1835 out_wipe_list: 1835 out_wipe_list:
1836 wipe_auth_tok_list(&auth_tok_list); 1836 wipe_auth_tok_list(&auth_tok_list);
1837 out: 1837 out:
1838 return rc; 1838 return rc;
1839 } 1839 }
1840 1840
1841 static int 1841 static int
1842 pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok, 1842 pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1843 struct ecryptfs_crypt_stat *crypt_stat, 1843 struct ecryptfs_crypt_stat *crypt_stat,
1844 struct ecryptfs_key_record *key_rec) 1844 struct ecryptfs_key_record *key_rec)
1845 { 1845 {
1846 struct ecryptfs_msg_ctx *msg_ctx = NULL; 1846 struct ecryptfs_msg_ctx *msg_ctx = NULL;
1847 char *payload = NULL; 1847 char *payload = NULL;
1848 size_t payload_len; 1848 size_t payload_len;
1849 struct ecryptfs_message *msg; 1849 struct ecryptfs_message *msg;
1850 int rc; 1850 int rc;
1851 1851
1852 rc = write_tag_66_packet(auth_tok->token.private_key.signature, 1852 rc = write_tag_66_packet(auth_tok->token.private_key.signature,
1853 ecryptfs_code_for_cipher_string( 1853 ecryptfs_code_for_cipher_string(
1854 crypt_stat->cipher, 1854 crypt_stat->cipher,
1855 crypt_stat->key_size), 1855 crypt_stat->key_size),
1856 crypt_stat, &payload, &payload_len); 1856 crypt_stat, &payload, &payload_len);
1857 if (rc) { 1857 if (rc) {
1858 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); 1858 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1859 goto out; 1859 goto out;
1860 } 1860 }
1861 rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); 1861 rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
1862 if (rc) { 1862 if (rc) {
1863 ecryptfs_printk(KERN_ERR, "Error sending message to " 1863 ecryptfs_printk(KERN_ERR, "Error sending message to "
1864 "ecryptfsd\n"); 1864 "ecryptfsd\n");
1865 goto out; 1865 goto out;
1866 } 1866 }
1867 rc = ecryptfs_wait_for_response(msg_ctx, &msg); 1867 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1868 if (rc) { 1868 if (rc) {
1869 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " 1869 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1870 "from the user space daemon\n"); 1870 "from the user space daemon\n");
1871 rc = -EIO; 1871 rc = -EIO;
1872 goto out; 1872 goto out;
1873 } 1873 }
1874 rc = parse_tag_67_packet(key_rec, msg); 1874 rc = parse_tag_67_packet(key_rec, msg);
1875 if (rc) 1875 if (rc)
1876 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); 1876 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1877 kfree(msg); 1877 kfree(msg);
1878 out: 1878 out:
1879 kfree(payload); 1879 kfree(payload);
1880 return rc; 1880 return rc;
1881 } 1881 }
1882 /** 1882 /**
1883 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet 1883 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1884 * @dest: Buffer into which to write the packet 1884 * @dest: Buffer into which to write the packet
1885 * @remaining_bytes: Maximum number of bytes that can be writtn 1885 * @remaining_bytes: Maximum number of bytes that can be writtn
1886 * @auth_tok: The authentication token used for generating the tag 1 packet 1886 * @auth_tok: The authentication token used for generating the tag 1 packet
1887 * @crypt_stat: The cryptographic context 1887 * @crypt_stat: The cryptographic context
1888 * @key_rec: The key record struct for the tag 1 packet 1888 * @key_rec: The key record struct for the tag 1 packet
1889 * @packet_size: This function will write the number of bytes that end 1889 * @packet_size: This function will write the number of bytes that end
1890 * up constituting the packet; set to zero on error 1890 * up constituting the packet; set to zero on error
1891 * 1891 *
1892 * Returns zero on success; non-zero on error. 1892 * Returns zero on success; non-zero on error.
1893 */ 1893 */
1894 static int 1894 static int
1895 write_tag_1_packet(char *dest, size_t *remaining_bytes, 1895 write_tag_1_packet(char *dest, size_t *remaining_bytes,
1896 struct ecryptfs_auth_tok *auth_tok, 1896 struct ecryptfs_auth_tok *auth_tok,
1897 struct ecryptfs_crypt_stat *crypt_stat, 1897 struct ecryptfs_crypt_stat *crypt_stat,
1898 struct ecryptfs_key_record *key_rec, size_t *packet_size) 1898 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1899 { 1899 {
1900 size_t i; 1900 size_t i;
1901 size_t encrypted_session_key_valid = 0; 1901 size_t encrypted_session_key_valid = 0;
1902 size_t packet_size_length; 1902 size_t packet_size_length;
1903 size_t max_packet_size; 1903 size_t max_packet_size;
1904 int rc = 0; 1904 int rc = 0;
1905 1905
1906 (*packet_size) = 0; 1906 (*packet_size) = 0;
1907 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, 1907 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1908 ECRYPTFS_SIG_SIZE); 1908 ECRYPTFS_SIG_SIZE);
1909 encrypted_session_key_valid = 0; 1909 encrypted_session_key_valid = 0;
1910 for (i = 0; i < crypt_stat->key_size; i++) 1910 for (i = 0; i < crypt_stat->key_size; i++)
1911 encrypted_session_key_valid |= 1911 encrypted_session_key_valid |=
1912 auth_tok->session_key.encrypted_key[i]; 1912 auth_tok->session_key.encrypted_key[i];
1913 if (encrypted_session_key_valid) { 1913 if (encrypted_session_key_valid) {
1914 memcpy(key_rec->enc_key, 1914 memcpy(key_rec->enc_key,
1915 auth_tok->session_key.encrypted_key, 1915 auth_tok->session_key.encrypted_key,
1916 auth_tok->session_key.encrypted_key_size); 1916 auth_tok->session_key.encrypted_key_size);
1917 goto encrypted_session_key_set; 1917 goto encrypted_session_key_set;
1918 } 1918 }
1919 if (auth_tok->session_key.encrypted_key_size == 0) 1919 if (auth_tok->session_key.encrypted_key_size == 0)
1920 auth_tok->session_key.encrypted_key_size = 1920 auth_tok->session_key.encrypted_key_size =
1921 auth_tok->token.private_key.key_size; 1921 auth_tok->token.private_key.key_size;
1922 rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec); 1922 rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1923 if (rc) { 1923 if (rc) {
1924 printk(KERN_ERR "Failed to encrypt session key via a key " 1924 printk(KERN_ERR "Failed to encrypt session key via a key "
1925 "module; rc = [%d]\n", rc); 1925 "module; rc = [%d]\n", rc);
1926 goto out; 1926 goto out;
1927 } 1927 }
1928 if (ecryptfs_verbosity > 0) { 1928 if (ecryptfs_verbosity > 0) {
1929 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n"); 1929 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1930 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); 1930 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1931 } 1931 }
1932 encrypted_session_key_set: 1932 encrypted_session_key_set:
1933 /* This format is inspired by OpenPGP; see RFC 2440 1933 /* This format is inspired by OpenPGP; see RFC 2440
1934 * packet tag 1 */ 1934 * packet tag 1 */
1935 max_packet_size = (1 /* Tag 1 identifier */ 1935 max_packet_size = (1 /* Tag 1 identifier */
1936 + 3 /* Max Tag 1 packet size */ 1936 + 3 /* Max Tag 1 packet size */
1937 + 1 /* Version */ 1937 + 1 /* Version */
1938 + ECRYPTFS_SIG_SIZE /* Key identifier */ 1938 + ECRYPTFS_SIG_SIZE /* Key identifier */
1939 + 1 /* Cipher identifier */ 1939 + 1 /* Cipher identifier */
1940 + key_rec->enc_key_size); /* Encrypted key size */ 1940 + key_rec->enc_key_size); /* Encrypted key size */
1941 if (max_packet_size > (*remaining_bytes)) { 1941 if (max_packet_size > (*remaining_bytes)) {
1942 printk(KERN_ERR "Packet length larger than maximum allowable; " 1942 printk(KERN_ERR "Packet length larger than maximum allowable; "
1943 "need up to [%td] bytes, but there are only [%td] " 1943 "need up to [%td] bytes, but there are only [%td] "
1944 "available\n", max_packet_size, (*remaining_bytes)); 1944 "available\n", max_packet_size, (*remaining_bytes));
1945 rc = -EINVAL; 1945 rc = -EINVAL;
1946 goto out; 1946 goto out;
1947 } 1947 }
1948 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; 1948 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
1949 rc = ecryptfs_write_packet_length(&dest[(*packet_size)], 1949 rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
1950 (max_packet_size - 4), 1950 (max_packet_size - 4),
1951 &packet_size_length); 1951 &packet_size_length);
1952 if (rc) { 1952 if (rc) {
1953 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " 1953 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
1954 "header; cannot generate packet length\n"); 1954 "header; cannot generate packet length\n");
1955 goto out; 1955 goto out;
1956 } 1956 }
1957 (*packet_size) += packet_size_length; 1957 (*packet_size) += packet_size_length;
1958 dest[(*packet_size)++] = 0x03; /* version 3 */ 1958 dest[(*packet_size)++] = 0x03; /* version 3 */
1959 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE); 1959 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
1960 (*packet_size) += ECRYPTFS_SIG_SIZE; 1960 (*packet_size) += ECRYPTFS_SIG_SIZE;
1961 dest[(*packet_size)++] = RFC2440_CIPHER_RSA; 1961 dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
1962 memcpy(&dest[(*packet_size)], key_rec->enc_key, 1962 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1963 key_rec->enc_key_size); 1963 key_rec->enc_key_size);
1964 (*packet_size) += key_rec->enc_key_size; 1964 (*packet_size) += key_rec->enc_key_size;
1965 out: 1965 out:
1966 if (rc) 1966 if (rc)
1967 (*packet_size) = 0; 1967 (*packet_size) = 0;
1968 else 1968 else
1969 (*remaining_bytes) -= (*packet_size); 1969 (*remaining_bytes) -= (*packet_size);
1970 return rc; 1970 return rc;
1971 } 1971 }
1972 1972
1973 /** 1973 /**
1974 * write_tag_11_packet 1974 * write_tag_11_packet
1975 * @dest: Target into which Tag 11 packet is to be written 1975 * @dest: Target into which Tag 11 packet is to be written
1976 * @remaining_bytes: Maximum packet length 1976 * @remaining_bytes: Maximum packet length
1977 * @contents: Byte array of contents to copy in 1977 * @contents: Byte array of contents to copy in
1978 * @contents_length: Number of bytes in contents 1978 * @contents_length: Number of bytes in contents
1979 * @packet_length: Length of the Tag 11 packet written; zero on error 1979 * @packet_length: Length of the Tag 11 packet written; zero on error
1980 * 1980 *
1981 * Returns zero on success; non-zero on error. 1981 * Returns zero on success; non-zero on error.
1982 */ 1982 */
1983 static int 1983 static int
1984 write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents, 1984 write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
1985 size_t contents_length, size_t *packet_length) 1985 size_t contents_length, size_t *packet_length)
1986 { 1986 {
1987 size_t packet_size_length; 1987 size_t packet_size_length;
1988 size_t max_packet_size; 1988 size_t max_packet_size;
1989 int rc = 0; 1989 int rc = 0;
1990 1990
1991 (*packet_length) = 0; 1991 (*packet_length) = 0;
1992 /* This format is inspired by OpenPGP; see RFC 2440 1992 /* This format is inspired by OpenPGP; see RFC 2440
1993 * packet tag 11 */ 1993 * packet tag 11 */
1994 max_packet_size = (1 /* Tag 11 identifier */ 1994 max_packet_size = (1 /* Tag 11 identifier */
1995 + 3 /* Max Tag 11 packet size */ 1995 + 3 /* Max Tag 11 packet size */
1996 + 1 /* Binary format specifier */ 1996 + 1 /* Binary format specifier */
1997 + 1 /* Filename length */ 1997 + 1 /* Filename length */
1998 + 8 /* Filename ("_CONSOLE") */ 1998 + 8 /* Filename ("_CONSOLE") */
1999 + 4 /* Modification date */ 1999 + 4 /* Modification date */
2000 + contents_length); /* Literal data */ 2000 + contents_length); /* Literal data */
2001 if (max_packet_size > (*remaining_bytes)) { 2001 if (max_packet_size > (*remaining_bytes)) {
2002 printk(KERN_ERR "Packet length larger than maximum allowable; " 2002 printk(KERN_ERR "Packet length larger than maximum allowable; "
2003 "need up to [%td] bytes, but there are only [%td] " 2003 "need up to [%td] bytes, but there are only [%td] "
2004 "available\n", max_packet_size, (*remaining_bytes)); 2004 "available\n", max_packet_size, (*remaining_bytes));
2005 rc = -EINVAL; 2005 rc = -EINVAL;
2006 goto out; 2006 goto out;
2007 } 2007 }
2008 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; 2008 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
2009 rc = ecryptfs_write_packet_length(&dest[(*packet_length)], 2009 rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
2010 (max_packet_size - 4), 2010 (max_packet_size - 4),
2011 &packet_size_length); 2011 &packet_size_length);
2012 if (rc) { 2012 if (rc) {
2013 printk(KERN_ERR "Error generating tag 11 packet header; cannot " 2013 printk(KERN_ERR "Error generating tag 11 packet header; cannot "
2014 "generate packet length. rc = [%d]\n", rc); 2014 "generate packet length. rc = [%d]\n", rc);
2015 goto out; 2015 goto out;
2016 } 2016 }
2017 (*packet_length) += packet_size_length; 2017 (*packet_length) += packet_size_length;
2018 dest[(*packet_length)++] = 0x62; /* binary data format specifier */ 2018 dest[(*packet_length)++] = 0x62; /* binary data format specifier */
2019 dest[(*packet_length)++] = 8; 2019 dest[(*packet_length)++] = 8;
2020 memcpy(&dest[(*packet_length)], "_CONSOLE", 8); 2020 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
2021 (*packet_length) += 8; 2021 (*packet_length) += 8;
2022 memset(&dest[(*packet_length)], 0x00, 4); 2022 memset(&dest[(*packet_length)], 0x00, 4);
2023 (*packet_length) += 4; 2023 (*packet_length) += 4;
2024 memcpy(&dest[(*packet_length)], contents, contents_length); 2024 memcpy(&dest[(*packet_length)], contents, contents_length);
2025 (*packet_length) += contents_length; 2025 (*packet_length) += contents_length;
2026 out: 2026 out:
2027 if (rc) 2027 if (rc)
2028 (*packet_length) = 0; 2028 (*packet_length) = 0;
2029 else 2029 else
2030 (*remaining_bytes) -= (*packet_length); 2030 (*remaining_bytes) -= (*packet_length);
2031 return rc; 2031 return rc;
2032 } 2032 }
2033 2033
2034 /** 2034 /**
2035 * write_tag_3_packet 2035 * write_tag_3_packet
2036 * @dest: Buffer into which to write the packet 2036 * @dest: Buffer into which to write the packet
2037 * @remaining_bytes: Maximum number of bytes that can be written 2037 * @remaining_bytes: Maximum number of bytes that can be written
2038 * @auth_tok: Authentication token 2038 * @auth_tok: Authentication token
2039 * @crypt_stat: The cryptographic context 2039 * @crypt_stat: The cryptographic context
2040 * @key_rec: encrypted key 2040 * @key_rec: encrypted key
2041 * @packet_size: This function will write the number of bytes that end 2041 * @packet_size: This function will write the number of bytes that end
2042 * up constituting the packet; set to zero on error 2042 * up constituting the packet; set to zero on error
2043 * 2043 *
2044 * Returns zero on success; non-zero on error. 2044 * Returns zero on success; non-zero on error.
2045 */ 2045 */
2046 static int 2046 static int
2047 write_tag_3_packet(char *dest, size_t *remaining_bytes, 2047 write_tag_3_packet(char *dest, size_t *remaining_bytes,
2048 struct ecryptfs_auth_tok *auth_tok, 2048 struct ecryptfs_auth_tok *auth_tok,
2049 struct ecryptfs_crypt_stat *crypt_stat, 2049 struct ecryptfs_crypt_stat *crypt_stat,
2050 struct ecryptfs_key_record *key_rec, size_t *packet_size) 2050 struct ecryptfs_key_record *key_rec, size_t *packet_size)
2051 { 2051 {
2052 size_t i; 2052 size_t i;
2053 size_t encrypted_session_key_valid = 0; 2053 size_t encrypted_session_key_valid = 0;
2054 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; 2054 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
2055 struct scatterlist dst_sg[2]; 2055 struct scatterlist dst_sg[2];
2056 struct scatterlist src_sg[2]; 2056 struct scatterlist src_sg[2];
2057 struct mutex *tfm_mutex = NULL; 2057 struct mutex *tfm_mutex = NULL;
2058 u8 cipher_code; 2058 u8 cipher_code;
2059 size_t packet_size_length; 2059 size_t packet_size_length;
2060 size_t max_packet_size; 2060 size_t max_packet_size;
2061 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 2061 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2062 crypt_stat->mount_crypt_stat; 2062 crypt_stat->mount_crypt_stat;
2063 struct blkcipher_desc desc = { 2063 struct blkcipher_desc desc = {
2064 .tfm = NULL, 2064 .tfm = NULL,
2065 .flags = CRYPTO_TFM_REQ_MAY_SLEEP 2065 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
2066 }; 2066 };
2067 int rc = 0; 2067 int rc = 0;
2068 2068
2069 (*packet_size) = 0; 2069 (*packet_size) = 0;
2070 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, 2070 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
2071 ECRYPTFS_SIG_SIZE); 2071 ECRYPTFS_SIG_SIZE);
2072 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, 2072 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
2073 crypt_stat->cipher); 2073 crypt_stat->cipher);
2074 if (unlikely(rc)) { 2074 if (unlikely(rc)) {
2075 printk(KERN_ERR "Internal error whilst attempting to get " 2075 printk(KERN_ERR "Internal error whilst attempting to get "
2076 "tfm and mutex for cipher name [%s]; rc = [%d]\n", 2076 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
2077 crypt_stat->cipher, rc); 2077 crypt_stat->cipher, rc);
2078 goto out; 2078 goto out;
2079 } 2079 }
2080 if (mount_crypt_stat->global_default_cipher_key_size == 0) { 2080 if (mount_crypt_stat->global_default_cipher_key_size == 0) {
2081 struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm); 2081 struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
2082 2082
2083 printk(KERN_WARNING "No key size specified at mount; " 2083 printk(KERN_WARNING "No key size specified at mount; "
2084 "defaulting to [%d]\n", alg->max_keysize); 2084 "defaulting to [%d]\n", alg->max_keysize);
2085 mount_crypt_stat->global_default_cipher_key_size = 2085 mount_crypt_stat->global_default_cipher_key_size =
2086 alg->max_keysize; 2086 alg->max_keysize;
2087 } 2087 }
2088 if (crypt_stat->key_size == 0) 2088 if (crypt_stat->key_size == 0)
2089 crypt_stat->key_size = 2089 crypt_stat->key_size =
2090 mount_crypt_stat->global_default_cipher_key_size; 2090 mount_crypt_stat->global_default_cipher_key_size;
2091 if (auth_tok->session_key.encrypted_key_size == 0) 2091 if (auth_tok->session_key.encrypted_key_size == 0)
2092 auth_tok->session_key.encrypted_key_size = 2092 auth_tok->session_key.encrypted_key_size =
2093 crypt_stat->key_size; 2093 crypt_stat->key_size;
2094 if (crypt_stat->key_size == 24 2094 if (crypt_stat->key_size == 24
2095 && strcmp("aes", crypt_stat->cipher) == 0) { 2095 && strcmp("aes", crypt_stat->cipher) == 0) {
2096 memset((crypt_stat->key + 24), 0, 8); 2096 memset((crypt_stat->key + 24), 0, 8);
2097 auth_tok->session_key.encrypted_key_size = 32; 2097 auth_tok->session_key.encrypted_key_size = 32;
2098 } else 2098 } else
2099 auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; 2099 auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
2100 key_rec->enc_key_size = 2100 key_rec->enc_key_size =
2101 auth_tok->session_key.encrypted_key_size; 2101 auth_tok->session_key.encrypted_key_size;
2102 encrypted_session_key_valid = 0; 2102 encrypted_session_key_valid = 0;
2103 for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) 2103 for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
2104 encrypted_session_key_valid |= 2104 encrypted_session_key_valid |=
2105 auth_tok->session_key.encrypted_key[i]; 2105 auth_tok->session_key.encrypted_key[i];
2106 if (encrypted_session_key_valid) { 2106 if (encrypted_session_key_valid) {
2107 ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " 2107 ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
2108 "using auth_tok->session_key.encrypted_key, " 2108 "using auth_tok->session_key.encrypted_key, "
2109 "where key_rec->enc_key_size = [%d]\n", 2109 "where key_rec->enc_key_size = [%d]\n",
2110 key_rec->enc_key_size); 2110 key_rec->enc_key_size);
2111 memcpy(key_rec->enc_key, 2111 memcpy(key_rec->enc_key,
2112 auth_tok->session_key.encrypted_key, 2112 auth_tok->session_key.encrypted_key,
2113 key_rec->enc_key_size); 2113 key_rec->enc_key_size);
2114 goto encrypted_session_key_set; 2114 goto encrypted_session_key_set;
2115 } 2115 }
2116 if (auth_tok->token.password.flags & 2116 if (auth_tok->token.password.flags &
2117 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { 2117 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
2118 ecryptfs_printk(KERN_DEBUG, "Using previously generated " 2118 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
2119 "session key encryption key of size [%d]\n", 2119 "session key encryption key of size [%d]\n",
2120 auth_tok->token.password. 2120 auth_tok->token.password.
2121 session_key_encryption_key_bytes); 2121 session_key_encryption_key_bytes);
2122 memcpy(session_key_encryption_key, 2122 memcpy(session_key_encryption_key,
2123 auth_tok->token.password.session_key_encryption_key, 2123 auth_tok->token.password.session_key_encryption_key,
2124 crypt_stat->key_size); 2124 crypt_stat->key_size);
2125 ecryptfs_printk(KERN_DEBUG, 2125 ecryptfs_printk(KERN_DEBUG,
2126 "Cached session key " "encryption key: \n"); 2126 "Cached session key " "encryption key: \n");
2127 if (ecryptfs_verbosity > 0) 2127 if (ecryptfs_verbosity > 0)
2128 ecryptfs_dump_hex(session_key_encryption_key, 16); 2128 ecryptfs_dump_hex(session_key_encryption_key, 16);
2129 } 2129 }
2130 if (unlikely(ecryptfs_verbosity > 0)) { 2130 if (unlikely(ecryptfs_verbosity > 0)) {
2131 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); 2131 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
2132 ecryptfs_dump_hex(session_key_encryption_key, 16); 2132 ecryptfs_dump_hex(session_key_encryption_key, 16);
2133 } 2133 }
2134 rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size, 2134 rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
2135 src_sg, 2); 2135 src_sg, 2);
2136 if (rc < 1 || rc > 2) { 2136 if (rc < 1 || rc > 2) {
2137 ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 2137 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
2138 "for crypt_stat session key; expected rc = 1; " 2138 "for crypt_stat session key; expected rc = 1; "
2139 "got rc = [%d]. key_rec->enc_key_size = [%d]\n", 2139 "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
2140 rc, key_rec->enc_key_size); 2140 rc, key_rec->enc_key_size);
2141 rc = -ENOMEM; 2141 rc = -ENOMEM;
2142 goto out; 2142 goto out;
2143 } 2143 }
2144 rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size, 2144 rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
2145 dst_sg, 2); 2145 dst_sg, 2);
2146 if (rc < 1 || rc > 2) { 2146 if (rc < 1 || rc > 2) {
2147 ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 2147 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
2148 "for crypt_stat encrypted session key; " 2148 "for crypt_stat encrypted session key; "
2149 "expected rc = 1; got rc = [%d]. " 2149 "expected rc = 1; got rc = [%d]. "
2150 "key_rec->enc_key_size = [%d]\n", rc, 2150 "key_rec->enc_key_size = [%d]\n", rc,
2151 key_rec->enc_key_size); 2151 key_rec->enc_key_size);
2152 rc = -ENOMEM; 2152 rc = -ENOMEM;
2153 goto out; 2153 goto out;
2154 } 2154 }
2155 mutex_lock(tfm_mutex); 2155 mutex_lock(tfm_mutex);
2156 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key, 2156 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
2157 crypt_stat->key_size); 2157 crypt_stat->key_size);
2158 if (rc < 0) { 2158 if (rc < 0) {
2159 mutex_unlock(tfm_mutex); 2159 mutex_unlock(tfm_mutex);
2160 ecryptfs_printk(KERN_ERR, "Error setting key for crypto " 2160 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
2161 "context; rc = [%d]\n", rc); 2161 "context; rc = [%d]\n", rc);
2162 goto out; 2162 goto out;
2163 } 2163 }
2164 rc = 0; 2164 rc = 0;
2165 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n", 2165 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
2166 crypt_stat->key_size); 2166 crypt_stat->key_size);
2167 rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg, 2167 rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg,
2168 (*key_rec).enc_key_size); 2168 (*key_rec).enc_key_size);
2169 mutex_unlock(tfm_mutex); 2169 mutex_unlock(tfm_mutex);
2170 if (rc) { 2170 if (rc) {
2171 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); 2171 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
2172 goto out; 2172 goto out;
2173 } 2173 }
2174 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); 2174 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
2175 if (ecryptfs_verbosity > 0) { 2175 if (ecryptfs_verbosity > 0) {
2176 ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n", 2176 ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
2177 key_rec->enc_key_size); 2177 key_rec->enc_key_size);
2178 ecryptfs_dump_hex(key_rec->enc_key, 2178 ecryptfs_dump_hex(key_rec->enc_key,
2179 key_rec->enc_key_size); 2179 key_rec->enc_key_size);
2180 } 2180 }
2181 encrypted_session_key_set: 2181 encrypted_session_key_set:
2182 /* This format is inspired by OpenPGP; see RFC 2440 2182 /* This format is inspired by OpenPGP; see RFC 2440
2183 * packet tag 3 */ 2183 * packet tag 3 */
2184 max_packet_size = (1 /* Tag 3 identifier */ 2184 max_packet_size = (1 /* Tag 3 identifier */
2185 + 3 /* Max Tag 3 packet size */ 2185 + 3 /* Max Tag 3 packet size */
2186 + 1 /* Version */ 2186 + 1 /* Version */
2187 + 1 /* Cipher code */ 2187 + 1 /* Cipher code */
2188 + 1 /* S2K specifier */ 2188 + 1 /* S2K specifier */
2189 + 1 /* Hash identifier */ 2189 + 1 /* Hash identifier */
2190 + ECRYPTFS_SALT_SIZE /* Salt */ 2190 + ECRYPTFS_SALT_SIZE /* Salt */
2191 + 1 /* Hash iterations */ 2191 + 1 /* Hash iterations */
2192 + key_rec->enc_key_size); /* Encrypted key size */ 2192 + key_rec->enc_key_size); /* Encrypted key size */
2193 if (max_packet_size > (*remaining_bytes)) { 2193 if (max_packet_size > (*remaining_bytes)) {
2194 printk(KERN_ERR "Packet too large; need up to [%td] bytes, but " 2194 printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
2195 "there are only [%td] available\n", max_packet_size, 2195 "there are only [%td] available\n", max_packet_size,
2196 (*remaining_bytes)); 2196 (*remaining_bytes));
2197 rc = -EINVAL; 2197 rc = -EINVAL;
2198 goto out; 2198 goto out;
2199 } 2199 }
2200 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; 2200 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
2201 /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) 2201 /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
2202 * to get the number of octets in the actual Tag 3 packet */ 2202 * to get the number of octets in the actual Tag 3 packet */
2203 rc = ecryptfs_write_packet_length(&dest[(*packet_size)], 2203 rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
2204 (max_packet_size - 4), 2204 (max_packet_size - 4),
2205 &packet_size_length); 2205 &packet_size_length);
2206 if (rc) { 2206 if (rc) {
2207 printk(KERN_ERR "Error generating tag 3 packet header; cannot " 2207 printk(KERN_ERR "Error generating tag 3 packet header; cannot "
2208 "generate packet length. rc = [%d]\n", rc); 2208 "generate packet length. rc = [%d]\n", rc);
2209 goto out; 2209 goto out;
2210 } 2210 }
2211 (*packet_size) += packet_size_length; 2211 (*packet_size) += packet_size_length;
2212 dest[(*packet_size)++] = 0x04; /* version 4 */ 2212 dest[(*packet_size)++] = 0x04; /* version 4 */
2213 /* TODO: Break from RFC2440 so that arbitrary ciphers can be 2213 /* TODO: Break from RFC2440 so that arbitrary ciphers can be
2214 * specified with strings */ 2214 * specified with strings */
2215 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher, 2215 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher,
2216 crypt_stat->key_size); 2216 crypt_stat->key_size);
2217 if (cipher_code == 0) { 2217 if (cipher_code == 0) {
2218 ecryptfs_printk(KERN_WARNING, "Unable to generate code for " 2218 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
2219 "cipher [%s]\n", crypt_stat->cipher); 2219 "cipher [%s]\n", crypt_stat->cipher);
2220 rc = -EINVAL; 2220 rc = -EINVAL;
2221 goto out; 2221 goto out;
2222 } 2222 }
2223 dest[(*packet_size)++] = cipher_code; 2223 dest[(*packet_size)++] = cipher_code;
2224 dest[(*packet_size)++] = 0x03; /* S2K */ 2224 dest[(*packet_size)++] = 0x03; /* S2K */
2225 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */ 2225 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
2226 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt, 2226 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
2227 ECRYPTFS_SALT_SIZE); 2227 ECRYPTFS_SALT_SIZE);
2228 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */ 2228 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
2229 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */ 2229 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
2230 memcpy(&dest[(*packet_size)], key_rec->enc_key, 2230 memcpy(&dest[(*packet_size)], key_rec->enc_key,
2231 key_rec->enc_key_size); 2231 key_rec->enc_key_size);
2232 (*packet_size) += key_rec->enc_key_size; 2232 (*packet_size) += key_rec->enc_key_size;
2233 out: 2233 out:
2234 if (rc) 2234 if (rc)
2235 (*packet_size) = 0; 2235 (*packet_size) = 0;
2236 else 2236 else
2237 (*remaining_bytes) -= (*packet_size); 2237 (*remaining_bytes) -= (*packet_size);
2238 return rc; 2238 return rc;
2239 } 2239 }
2240 2240
2241 struct kmem_cache *ecryptfs_key_record_cache; 2241 struct kmem_cache *ecryptfs_key_record_cache;
2242 2242
2243 /** 2243 /**
2244 * ecryptfs_generate_key_packet_set 2244 * ecryptfs_generate_key_packet_set
2245 * @dest_base: Virtual address from which to write the key record set 2245 * @dest_base: Virtual address from which to write the key record set
2246 * @crypt_stat: The cryptographic context from which the 2246 * @crypt_stat: The cryptographic context from which the
2247 * authentication tokens will be retrieved 2247 * authentication tokens will be retrieved
2248 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat 2248 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
2249 * for the global parameters 2249 * for the global parameters
2250 * @len: The amount written 2250 * @len: The amount written
2251 * @max: The maximum amount of data allowed to be written 2251 * @max: The maximum amount of data allowed to be written
2252 * 2252 *
2253 * Generates a key packet set and writes it to the virtual address 2253 * Generates a key packet set and writes it to the virtual address
2254 * passed in. 2254 * passed in.
2255 * 2255 *
2256 * Returns zero on success; non-zero on error. 2256 * Returns zero on success; non-zero on error.
2257 */ 2257 */
2258 int 2258 int
2259 ecryptfs_generate_key_packet_set(char *dest_base, 2259 ecryptfs_generate_key_packet_set(char *dest_base,
2260 struct ecryptfs_crypt_stat *crypt_stat, 2260 struct ecryptfs_crypt_stat *crypt_stat,
2261 struct dentry *ecryptfs_dentry, size_t *len, 2261 struct dentry *ecryptfs_dentry, size_t *len,
2262 size_t max) 2262 size_t max)
2263 { 2263 {
2264 struct ecryptfs_auth_tok *auth_tok; 2264 struct ecryptfs_auth_tok *auth_tok;
2265 struct ecryptfs_global_auth_tok *global_auth_tok; 2265 struct ecryptfs_global_auth_tok *global_auth_tok;
2266 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 2266 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2267 &ecryptfs_superblock_to_private( 2267 &ecryptfs_superblock_to_private(
2268 ecryptfs_dentry->d_sb)->mount_crypt_stat; 2268 ecryptfs_dentry->d_sb)->mount_crypt_stat;
2269 size_t written; 2269 size_t written;
2270 struct ecryptfs_key_record *key_rec; 2270 struct ecryptfs_key_record *key_rec;
2271 struct ecryptfs_key_sig *key_sig; 2271 struct ecryptfs_key_sig *key_sig;
2272 int rc = 0; 2272 int rc = 0;
2273 2273
2274 (*len) = 0; 2274 (*len) = 0;
2275 mutex_lock(&crypt_stat->keysig_list_mutex); 2275 mutex_lock(&crypt_stat->keysig_list_mutex);
2276 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); 2276 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
2277 if (!key_rec) { 2277 if (!key_rec) {
2278 rc = -ENOMEM; 2278 rc = -ENOMEM;
2279 goto out; 2279 goto out;
2280 } 2280 }
2281 list_for_each_entry(key_sig, &crypt_stat->keysig_list, 2281 list_for_each_entry(key_sig, &crypt_stat->keysig_list,
2282 crypt_stat_list) { 2282 crypt_stat_list) {
2283 memset(key_rec, 0, sizeof(*key_rec)); 2283 memset(key_rec, 0, sizeof(*key_rec));
2284 rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, 2284 rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
2285 mount_crypt_stat, 2285 mount_crypt_stat,
2286 key_sig->keysig); 2286 key_sig->keysig);
2287 if (rc) { 2287 if (rc) {
2288 printk(KERN_ERR "Error attempting to get the global " 2288 printk(KERN_ERR "Error attempting to get the global "
2289 "auth_tok; rc = [%d]\n", rc); 2289 "auth_tok; rc = [%d]\n", rc);
2290 goto out_free; 2290 goto out_free;
2291 } 2291 }
2292 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) { 2292 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
2293 printk(KERN_WARNING 2293 printk(KERN_WARNING
2294 "Skipping invalid auth tok with sig = [%s]\n", 2294 "Skipping invalid auth tok with sig = [%s]\n",
2295 global_auth_tok->sig); 2295 global_auth_tok->sig);
2296 continue; 2296 continue;
2297 } 2297 }
2298 auth_tok = global_auth_tok->global_auth_tok; 2298 auth_tok = global_auth_tok->global_auth_tok;
2299 if (auth_tok->token_type == ECRYPTFS_PASSWORD) { 2299 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
2300 rc = write_tag_3_packet((dest_base + (*len)), 2300 rc = write_tag_3_packet((dest_base + (*len)),
2301 &max, auth_tok, 2301 &max, auth_tok,
2302 crypt_stat, key_rec, 2302 crypt_stat, key_rec,
2303 &written); 2303 &written);
2304 if (rc) { 2304 if (rc) {
2305 ecryptfs_printk(KERN_WARNING, "Error " 2305 ecryptfs_printk(KERN_WARNING, "Error "
2306 "writing tag 3 packet\n"); 2306 "writing tag 3 packet\n");
2307 goto out_free; 2307 goto out_free;
2308 } 2308 }
2309 (*len) += written; 2309 (*len) += written;
2310 /* Write auth tok signature packet */ 2310 /* Write auth tok signature packet */
2311 rc = write_tag_11_packet((dest_base + (*len)), &max, 2311 rc = write_tag_11_packet((dest_base + (*len)), &max,
2312 key_rec->sig, 2312 key_rec->sig,
2313 ECRYPTFS_SIG_SIZE, &written); 2313 ECRYPTFS_SIG_SIZE, &written);
2314 if (rc) { 2314 if (rc) {
2315 ecryptfs_printk(KERN_ERR, "Error writing " 2315 ecryptfs_printk(KERN_ERR, "Error writing "
2316 "auth tok signature packet\n"); 2316 "auth tok signature packet\n");
2317 goto out_free; 2317 goto out_free;
2318 } 2318 }
2319 (*len) += written; 2319 (*len) += written;
2320 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 2320 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
2321 rc = write_tag_1_packet(dest_base + (*len), 2321 rc = write_tag_1_packet(dest_base + (*len),
2322 &max, auth_tok, 2322 &max, auth_tok,
2323 crypt_stat, key_rec, &written); 2323 crypt_stat, key_rec, &written);
2324 if (rc) { 2324 if (rc) {
2325 ecryptfs_printk(KERN_WARNING, "Error " 2325 ecryptfs_printk(KERN_WARNING, "Error "
2326 "writing tag 1 packet\n"); 2326 "writing tag 1 packet\n");
2327 goto out_free; 2327 goto out_free;
2328 } 2328 }
2329 (*len) += written; 2329 (*len) += written;
2330 } else { 2330 } else {
2331 ecryptfs_printk(KERN_WARNING, "Unsupported " 2331 ecryptfs_printk(KERN_WARNING, "Unsupported "
2332 "authentication token type\n"); 2332 "authentication token type\n");
2333 rc = -EINVAL; 2333 rc = -EINVAL;
2334 goto out_free; 2334 goto out_free;
2335 } 2335 }
2336 } 2336 }
2337 if (likely(max > 0)) { 2337 if (likely(max > 0)) {
2338 dest_base[(*len)] = 0x00; 2338 dest_base[(*len)] = 0x00;
2339 } else { 2339 } else {
2340 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); 2340 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
2341 rc = -EIO; 2341 rc = -EIO;
2342 } 2342 }
2343 out_free: 2343 out_free:
2344 kmem_cache_free(ecryptfs_key_record_cache, key_rec); 2344 kmem_cache_free(ecryptfs_key_record_cache, key_rec);
2345 out: 2345 out:
2346 if (rc) 2346 if (rc)
2347 (*len) = 0; 2347 (*len) = 0;
2348 mutex_unlock(&crypt_stat->keysig_list_mutex); 2348 mutex_unlock(&crypt_stat->keysig_list_mutex);
2349 return rc; 2349 return rc;
2350 } 2350 }
2351 2351
2352 struct kmem_cache *ecryptfs_key_sig_cache; 2352 struct kmem_cache *ecryptfs_key_sig_cache;
2353 2353
2354 int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) 2354 int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
2355 { 2355 {
2356 struct ecryptfs_key_sig *new_key_sig; 2356 struct ecryptfs_key_sig *new_key_sig;
2357 int rc = 0; 2357 int rc = 0;
2358 2358
2359 new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); 2359 new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
2360 if (!new_key_sig) { 2360 if (!new_key_sig) {
2361 rc = -ENOMEM; 2361 rc = -ENOMEM;
2362 printk(KERN_ERR 2362 printk(KERN_ERR
2363 "Error allocating from ecryptfs_key_sig_cache\n"); 2363 "Error allocating from ecryptfs_key_sig_cache\n");
2364 goto out; 2364 goto out;
2365 } 2365 }
2366 memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); 2366 memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
2367 mutex_lock(&crypt_stat->keysig_list_mutex); 2367 mutex_lock(&crypt_stat->keysig_list_mutex);
2368 list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); 2368 list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
2369 mutex_unlock(&crypt_stat->keysig_list_mutex); 2369 mutex_unlock(&crypt_stat->keysig_list_mutex);
2370 out: 2370 out:
2371 return rc; 2371 return rc;
2372 } 2372 }
2373 2373
2374 struct kmem_cache *ecryptfs_global_auth_tok_cache; 2374 struct kmem_cache *ecryptfs_global_auth_tok_cache;
2375 2375
2376 int 2376 int
2377 ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 2377 ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
2378 char *sig) 2378 char *sig, u32 global_auth_tok_flags)
2379 { 2379 {
2380 struct ecryptfs_global_auth_tok *new_auth_tok; 2380 struct ecryptfs_global_auth_tok *new_auth_tok;
2381 int rc = 0; 2381 int rc = 0;
2382 2382
2383 new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache, 2383 new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
2384 GFP_KERNEL); 2384 GFP_KERNEL);
2385 if (!new_auth_tok) { 2385 if (!new_auth_tok) {
2386 rc = -ENOMEM; 2386 rc = -ENOMEM;
2387 printk(KERN_ERR "Error allocating from " 2387 printk(KERN_ERR "Error allocating from "
2388 "ecryptfs_global_auth_tok_cache\n"); 2388 "ecryptfs_global_auth_tok_cache\n");
2389 goto out; 2389 goto out;
2390 } 2390 }
2391 memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); 2391 memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
2392 new_auth_tok->flags = global_auth_tok_flags;
2392 new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 2393 new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
2393 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 2394 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
2394 list_add(&new_auth_tok->mount_crypt_stat_list, 2395 list_add(&new_auth_tok->mount_crypt_stat_list,
2395 &mount_crypt_stat->global_auth_tok_list); 2396 &mount_crypt_stat->global_auth_tok_list);
2396 mount_crypt_stat->num_global_auth_toks++; 2397 mount_crypt_stat->num_global_auth_toks++;
2397 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 2398 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
2398 out: 2399 out:
2399 return rc; 2400 return rc;
2400 } 2401 }
2401 2402
2402 2403
1 /** 1 /**
2 * eCryptfs: Linux filesystem encryption layer 2 * eCryptfs: Linux filesystem encryption layer
3 * 3 *
4 * Copyright (C) 1997-2003 Erez Zadok 4 * Copyright (C) 1997-2003 Erez Zadok
5 * Copyright (C) 2001-2003 Stony Brook University 5 * Copyright (C) 2001-2003 Stony Brook University
6 * Copyright (C) 2004-2007 International Business Machines Corp. 6 * Copyright (C) 2004-2007 International Business Machines Corp.
7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com> 8 * Michael C. Thompson <mcthomps@us.ibm.com>
9 * Tyler Hicks <tyhicks@ou.edu> 9 * Tyler Hicks <tyhicks@ou.edu>
10 * 10 *
11 * This program is free software; you can redistribute it and/or 11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as 12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the 13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version. 14 * License, or (at your option) any later version.
15 * 15 *
16 * This program is distributed in the hope that it will be useful, but 16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of 17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details. 19 * General Public License for more details.
20 * 20 *
21 * You should have received a copy of the GNU General Public License 21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software 22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 * 02111-1307, USA. 24 * 02111-1307, USA.
25 */ 25 */
26 26
27 #include <linux/dcache.h> 27 #include <linux/dcache.h>
28 #include <linux/file.h> 28 #include <linux/file.h>
29 #include <linux/module.h> 29 #include <linux/module.h>
30 #include <linux/namei.h> 30 #include <linux/namei.h>
31 #include <linux/skbuff.h> 31 #include <linux/skbuff.h>
32 #include <linux/crypto.h> 32 #include <linux/crypto.h>
33 #include <linux/mount.h> 33 #include <linux/mount.h>
34 #include <linux/pagemap.h> 34 #include <linux/pagemap.h>
35 #include <linux/key.h> 35 #include <linux/key.h>
36 #include <linux/parser.h> 36 #include <linux/parser.h>
37 #include <linux/fs_stack.h> 37 #include <linux/fs_stack.h>
38 #include "ecryptfs_kernel.h" 38 #include "ecryptfs_kernel.h"
39 39
40 /** 40 /**
41 * Module parameter that defines the ecryptfs_verbosity level. 41 * Module parameter that defines the ecryptfs_verbosity level.
42 */ 42 */
43 int ecryptfs_verbosity = 0; 43 int ecryptfs_verbosity = 0;
44 44
45 module_param(ecryptfs_verbosity, int, 0); 45 module_param(ecryptfs_verbosity, int, 0);
46 MODULE_PARM_DESC(ecryptfs_verbosity, 46 MODULE_PARM_DESC(ecryptfs_verbosity,
47 "Initial verbosity level (0 or 1; defaults to " 47 "Initial verbosity level (0 or 1; defaults to "
48 "0, which is Quiet)"); 48 "0, which is Quiet)");
49 49
50 /** 50 /**
51 * Module parameter that defines the number of message buffer elements 51 * Module parameter that defines the number of message buffer elements
52 */ 52 */
53 unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS; 53 unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
54 54
55 module_param(ecryptfs_message_buf_len, uint, 0); 55 module_param(ecryptfs_message_buf_len, uint, 0);
56 MODULE_PARM_DESC(ecryptfs_message_buf_len, 56 MODULE_PARM_DESC(ecryptfs_message_buf_len,
57 "Number of message buffer elements"); 57 "Number of message buffer elements");
58 58
59 /** 59 /**
60 * Module parameter that defines the maximum guaranteed amount of time to wait 60 * Module parameter that defines the maximum guaranteed amount of time to wait
61 * for a response from ecryptfsd. The actual sleep time will be, more than 61 * for a response from ecryptfsd. The actual sleep time will be, more than
62 * likely, a small amount greater than this specified value, but only less if 62 * likely, a small amount greater than this specified value, but only less if
63 * the message successfully arrives. 63 * the message successfully arrives.
64 */ 64 */
65 signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ; 65 signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
66 66
67 module_param(ecryptfs_message_wait_timeout, long, 0); 67 module_param(ecryptfs_message_wait_timeout, long, 0);
68 MODULE_PARM_DESC(ecryptfs_message_wait_timeout, 68 MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
69 "Maximum number of seconds that an operation will " 69 "Maximum number of seconds that an operation will "
70 "sleep while waiting for a message response from " 70 "sleep while waiting for a message response from "
71 "userspace"); 71 "userspace");
72 72
73 /** 73 /**
74 * Module parameter that is an estimate of the maximum number of users 74 * Module parameter that is an estimate of the maximum number of users
75 * that will be concurrently using eCryptfs. Set this to the right 75 * that will be concurrently using eCryptfs. Set this to the right
76 * value to balance performance and memory use. 76 * value to balance performance and memory use.
77 */ 77 */
78 unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS; 78 unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
79 79
80 module_param(ecryptfs_number_of_users, uint, 0); 80 module_param(ecryptfs_number_of_users, uint, 0);
81 MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of " 81 MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
82 "concurrent users of eCryptfs"); 82 "concurrent users of eCryptfs");
83 83
84 void __ecryptfs_printk(const char *fmt, ...) 84 void __ecryptfs_printk(const char *fmt, ...)
85 { 85 {
86 va_list args; 86 va_list args;
87 va_start(args, fmt); 87 va_start(args, fmt);
88 if (fmt[1] == '7') { /* KERN_DEBUG */ 88 if (fmt[1] == '7') { /* KERN_DEBUG */
89 if (ecryptfs_verbosity >= 1) 89 if (ecryptfs_verbosity >= 1)
90 vprintk(fmt, args); 90 vprintk(fmt, args);
91 } else 91 } else
92 vprintk(fmt, args); 92 vprintk(fmt, args);
93 va_end(args); 93 va_end(args);
94 } 94 }
95 95
96 /** 96 /**
97 * ecryptfs_init_persistent_file 97 * ecryptfs_init_persistent_file
98 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with 98 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
99 * the lower dentry and the lower mount set 99 * the lower dentry and the lower mount set
100 * 100 *
101 * eCryptfs only ever keeps a single open file for every lower 101 * eCryptfs only ever keeps a single open file for every lower
102 * inode. All I/O operations to the lower inode occur through that 102 * inode. All I/O operations to the lower inode occur through that
103 * file. When the first eCryptfs dentry that interposes with the first 103 * file. When the first eCryptfs dentry that interposes with the first
104 * lower dentry for that inode is created, this function creates the 104 * lower dentry for that inode is created, this function creates the
105 * persistent file struct and associates it with the eCryptfs 105 * persistent file struct and associates it with the eCryptfs
106 * inode. When the eCryptfs inode is destroyed, the file is closed. 106 * inode. When the eCryptfs inode is destroyed, the file is closed.
107 * 107 *
108 * The persistent file will be opened with read/write permissions, if 108 * The persistent file will be opened with read/write permissions, if
109 * possible. Otherwise, it is opened read-only. 109 * possible. Otherwise, it is opened read-only.
110 * 110 *
111 * This function does nothing if a lower persistent file is already 111 * This function does nothing if a lower persistent file is already
112 * associated with the eCryptfs inode. 112 * associated with the eCryptfs inode.
113 * 113 *
114 * Returns zero on success; non-zero otherwise 114 * Returns zero on success; non-zero otherwise
115 */ 115 */
116 int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry) 116 int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
117 { 117 {
118 const struct cred *cred = current_cred(); 118 const struct cred *cred = current_cred();
119 struct ecryptfs_inode_info *inode_info = 119 struct ecryptfs_inode_info *inode_info =
120 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode); 120 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
121 int rc = 0; 121 int rc = 0;
122 122
123 mutex_lock(&inode_info->lower_file_mutex); 123 mutex_lock(&inode_info->lower_file_mutex);
124 if (!inode_info->lower_file) { 124 if (!inode_info->lower_file) {
125 struct dentry *lower_dentry; 125 struct dentry *lower_dentry;
126 struct vfsmount *lower_mnt = 126 struct vfsmount *lower_mnt =
127 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry); 127 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
128 128
129 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); 129 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
130 rc = ecryptfs_privileged_open(&inode_info->lower_file, 130 rc = ecryptfs_privileged_open(&inode_info->lower_file,
131 lower_dentry, lower_mnt, cred); 131 lower_dentry, lower_mnt, cred);
132 if (rc || IS_ERR(inode_info->lower_file)) { 132 if (rc || IS_ERR(inode_info->lower_file)) {
133 printk(KERN_ERR "Error opening lower persistent file " 133 printk(KERN_ERR "Error opening lower persistent file "
134 "for lower_dentry [0x%p] and lower_mnt [0x%p]; " 134 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
135 "rc = [%d]\n", lower_dentry, lower_mnt, rc); 135 "rc = [%d]\n", lower_dentry, lower_mnt, rc);
136 rc = PTR_ERR(inode_info->lower_file); 136 rc = PTR_ERR(inode_info->lower_file);
137 inode_info->lower_file = NULL; 137 inode_info->lower_file = NULL;
138 } 138 }
139 } 139 }
140 mutex_unlock(&inode_info->lower_file_mutex); 140 mutex_unlock(&inode_info->lower_file_mutex);
141 return rc; 141 return rc;
142 } 142 }
143 143
144 /** 144 /**
145 * ecryptfs_interpose 145 * ecryptfs_interpose
146 * @lower_dentry: Existing dentry in the lower filesystem 146 * @lower_dentry: Existing dentry in the lower filesystem
147 * @dentry: ecryptfs' dentry 147 * @dentry: ecryptfs' dentry
148 * @sb: ecryptfs's super_block 148 * @sb: ecryptfs's super_block
149 * @flags: flags to govern behavior of interpose procedure 149 * @flags: flags to govern behavior of interpose procedure
150 * 150 *
151 * Interposes upper and lower dentries. 151 * Interposes upper and lower dentries.
152 * 152 *
153 * Returns zero on success; non-zero otherwise 153 * Returns zero on success; non-zero otherwise
154 */ 154 */
155 int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry, 155 int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
156 struct super_block *sb, u32 flags) 156 struct super_block *sb, u32 flags)
157 { 157 {
158 struct inode *lower_inode; 158 struct inode *lower_inode;
159 struct inode *inode; 159 struct inode *inode;
160 int rc = 0; 160 int rc = 0;
161 161
162 lower_inode = lower_dentry->d_inode; 162 lower_inode = lower_dentry->d_inode;
163 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) { 163 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
164 rc = -EXDEV; 164 rc = -EXDEV;
165 goto out; 165 goto out;
166 } 166 }
167 if (!igrab(lower_inode)) { 167 if (!igrab(lower_inode)) {
168 rc = -ESTALE; 168 rc = -ESTALE;
169 goto out; 169 goto out;
170 } 170 }
171 inode = iget5_locked(sb, (unsigned long)lower_inode, 171 inode = iget5_locked(sb, (unsigned long)lower_inode,
172 ecryptfs_inode_test, ecryptfs_inode_set, 172 ecryptfs_inode_test, ecryptfs_inode_set,
173 lower_inode); 173 lower_inode);
174 if (!inode) { 174 if (!inode) {
175 rc = -EACCES; 175 rc = -EACCES;
176 iput(lower_inode); 176 iput(lower_inode);
177 goto out; 177 goto out;
178 } 178 }
179 if (inode->i_state & I_NEW) 179 if (inode->i_state & I_NEW)
180 unlock_new_inode(inode); 180 unlock_new_inode(inode);
181 else 181 else
182 iput(lower_inode); 182 iput(lower_inode);
183 if (S_ISLNK(lower_inode->i_mode)) 183 if (S_ISLNK(lower_inode->i_mode))
184 inode->i_op = &ecryptfs_symlink_iops; 184 inode->i_op = &ecryptfs_symlink_iops;
185 else if (S_ISDIR(lower_inode->i_mode)) 185 else if (S_ISDIR(lower_inode->i_mode))
186 inode->i_op = &ecryptfs_dir_iops; 186 inode->i_op = &ecryptfs_dir_iops;
187 if (S_ISDIR(lower_inode->i_mode)) 187 if (S_ISDIR(lower_inode->i_mode))
188 inode->i_fop = &ecryptfs_dir_fops; 188 inode->i_fop = &ecryptfs_dir_fops;
189 if (special_file(lower_inode->i_mode)) 189 if (special_file(lower_inode->i_mode))
190 init_special_inode(inode, lower_inode->i_mode, 190 init_special_inode(inode, lower_inode->i_mode,
191 lower_inode->i_rdev); 191 lower_inode->i_rdev);
192 dentry->d_op = &ecryptfs_dops; 192 dentry->d_op = &ecryptfs_dops;
193 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD) 193 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
194 d_add(dentry, inode); 194 d_add(dentry, inode);
195 else 195 else
196 d_instantiate(dentry, inode); 196 d_instantiate(dentry, inode);
197 fsstack_copy_attr_all(inode, lower_inode, NULL); 197 fsstack_copy_attr_all(inode, lower_inode, NULL);
198 /* This size will be overwritten for real files w/ headers and 198 /* This size will be overwritten for real files w/ headers and
199 * other metadata */ 199 * other metadata */
200 fsstack_copy_inode_size(inode, lower_inode); 200 fsstack_copy_inode_size(inode, lower_inode);
201 out: 201 out:
202 return rc; 202 return rc;
203 } 203 }
204 204
205 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, 205 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
206 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher, 206 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
207 ecryptfs_opt_ecryptfs_key_bytes, 207 ecryptfs_opt_ecryptfs_key_bytes,
208 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata, 208 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
209 ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig, 209 ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
210 ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes, 210 ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
211 ecryptfs_opt_err }; 211 ecryptfs_opt_err };
212 212
213 static const match_table_t tokens = { 213 static const match_table_t tokens = {
214 {ecryptfs_opt_sig, "sig=%s"}, 214 {ecryptfs_opt_sig, "sig=%s"},
215 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"}, 215 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
216 {ecryptfs_opt_cipher, "cipher=%s"}, 216 {ecryptfs_opt_cipher, "cipher=%s"},
217 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"}, 217 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
218 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"}, 218 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
219 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"}, 219 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
220 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"}, 220 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
221 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"}, 221 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
222 {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"}, 222 {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
223 {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"}, 223 {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
224 {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"}, 224 {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
225 {ecryptfs_opt_err, NULL} 225 {ecryptfs_opt_err, NULL}
226 }; 226 };
227 227
228 static int ecryptfs_init_global_auth_toks( 228 static int ecryptfs_init_global_auth_toks(
229 struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 229 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
230 { 230 {
231 struct ecryptfs_global_auth_tok *global_auth_tok; 231 struct ecryptfs_global_auth_tok *global_auth_tok;
232 int rc = 0; 232 int rc = 0;
233 233
234 list_for_each_entry(global_auth_tok, 234 list_for_each_entry(global_auth_tok,
235 &mount_crypt_stat->global_auth_tok_list, 235 &mount_crypt_stat->global_auth_tok_list,
236 mount_crypt_stat_list) { 236 mount_crypt_stat_list) {
237 rc = ecryptfs_keyring_auth_tok_for_sig( 237 rc = ecryptfs_keyring_auth_tok_for_sig(
238 &global_auth_tok->global_auth_tok_key, 238 &global_auth_tok->global_auth_tok_key,
239 &global_auth_tok->global_auth_tok, 239 &global_auth_tok->global_auth_tok,
240 global_auth_tok->sig); 240 global_auth_tok->sig);
241 if (rc) { 241 if (rc) {
242 printk(KERN_ERR "Could not find valid key in user " 242 printk(KERN_ERR "Could not find valid key in user "
243 "session keyring for sig specified in mount " 243 "session keyring for sig specified in mount "
244 "option: [%s]\n", global_auth_tok->sig); 244 "option: [%s]\n", global_auth_tok->sig);
245 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID; 245 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
246 goto out; 246 goto out;
247 } else 247 } else
248 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID; 248 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
249 } 249 }
250 out: 250 out:
251 return rc; 251 return rc;
252 } 252 }
253 253
254 static void ecryptfs_init_mount_crypt_stat( 254 static void ecryptfs_init_mount_crypt_stat(
255 struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 255 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
256 { 256 {
257 memset((void *)mount_crypt_stat, 0, 257 memset((void *)mount_crypt_stat, 0,
258 sizeof(struct ecryptfs_mount_crypt_stat)); 258 sizeof(struct ecryptfs_mount_crypt_stat));
259 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list); 259 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
260 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex); 260 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
261 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED; 261 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
262 } 262 }
263 263
264 /** 264 /**
265 * ecryptfs_parse_options 265 * ecryptfs_parse_options
266 * @sb: The ecryptfs super block 266 * @sb: The ecryptfs super block
267 * @options: The options pased to the kernel 267 * @options: The options pased to the kernel
268 * 268 *
269 * Parse mount options: 269 * Parse mount options:
270 * debug=N - ecryptfs_verbosity level for debug output 270 * debug=N - ecryptfs_verbosity level for debug output
271 * sig=XXX - description(signature) of the key to use 271 * sig=XXX - description(signature) of the key to use
272 * 272 *
273 * Returns the dentry object of the lower-level (lower/interposed) 273 * Returns the dentry object of the lower-level (lower/interposed)
274 * directory; We want to mount our stackable file system on top of 274 * directory; We want to mount our stackable file system on top of
275 * that lower directory. 275 * that lower directory.
276 * 276 *
277 * The signature of the key to use must be the description of a key 277 * The signature of the key to use must be the description of a key
278 * already in the keyring. Mounting will fail if the key can not be 278 * already in the keyring. Mounting will fail if the key can not be
279 * found. 279 * found.
280 * 280 *
281 * Returns zero on success; non-zero on error 281 * Returns zero on success; non-zero on error
282 */ 282 */
283 static int ecryptfs_parse_options(struct super_block *sb, char *options) 283 static int ecryptfs_parse_options(struct super_block *sb, char *options)
284 { 284 {
285 char *p; 285 char *p;
286 int rc = 0; 286 int rc = 0;
287 int sig_set = 0; 287 int sig_set = 0;
288 int cipher_name_set = 0; 288 int cipher_name_set = 0;
289 int fn_cipher_name_set = 0; 289 int fn_cipher_name_set = 0;
290 int cipher_key_bytes; 290 int cipher_key_bytes;
291 int cipher_key_bytes_set = 0; 291 int cipher_key_bytes_set = 0;
292 int fn_cipher_key_bytes; 292 int fn_cipher_key_bytes;
293 int fn_cipher_key_bytes_set = 0; 293 int fn_cipher_key_bytes_set = 0;
294 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 294 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
295 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat; 295 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
296 substring_t args[MAX_OPT_ARGS]; 296 substring_t args[MAX_OPT_ARGS];
297 int token; 297 int token;
298 char *sig_src; 298 char *sig_src;
299 char *cipher_name_dst; 299 char *cipher_name_dst;
300 char *cipher_name_src; 300 char *cipher_name_src;
301 char *fn_cipher_name_dst; 301 char *fn_cipher_name_dst;
302 char *fn_cipher_name_src; 302 char *fn_cipher_name_src;
303 char *fnek_dst; 303 char *fnek_dst;
304 char *fnek_src; 304 char *fnek_src;
305 char *cipher_key_bytes_src; 305 char *cipher_key_bytes_src;
306 char *fn_cipher_key_bytes_src; 306 char *fn_cipher_key_bytes_src;
307 307
308 if (!options) { 308 if (!options) {
309 rc = -EINVAL; 309 rc = -EINVAL;
310 goto out; 310 goto out;
311 } 311 }
312 ecryptfs_init_mount_crypt_stat(mount_crypt_stat); 312 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
313 while ((p = strsep(&options, ",")) != NULL) { 313 while ((p = strsep(&options, ",")) != NULL) {
314 if (!*p) 314 if (!*p)
315 continue; 315 continue;
316 token = match_token(p, tokens, args); 316 token = match_token(p, tokens, args);
317 switch (token) { 317 switch (token) {
318 case ecryptfs_opt_sig: 318 case ecryptfs_opt_sig:
319 case ecryptfs_opt_ecryptfs_sig: 319 case ecryptfs_opt_ecryptfs_sig:
320 sig_src = args[0].from; 320 sig_src = args[0].from;
321 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat, 321 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
322 sig_src); 322 sig_src, 0);
323 if (rc) { 323 if (rc) {
324 printk(KERN_ERR "Error attempting to register " 324 printk(KERN_ERR "Error attempting to register "
325 "global sig; rc = [%d]\n", rc); 325 "global sig; rc = [%d]\n", rc);
326 goto out; 326 goto out;
327 } 327 }
328 sig_set = 1; 328 sig_set = 1;
329 break; 329 break;
330 case ecryptfs_opt_cipher: 330 case ecryptfs_opt_cipher:
331 case ecryptfs_opt_ecryptfs_cipher: 331 case ecryptfs_opt_ecryptfs_cipher:
332 cipher_name_src = args[0].from; 332 cipher_name_src = args[0].from;
333 cipher_name_dst = 333 cipher_name_dst =
334 mount_crypt_stat-> 334 mount_crypt_stat->
335 global_default_cipher_name; 335 global_default_cipher_name;
336 strncpy(cipher_name_dst, cipher_name_src, 336 strncpy(cipher_name_dst, cipher_name_src,
337 ECRYPTFS_MAX_CIPHER_NAME_SIZE); 337 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
338 cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0'; 338 cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
339 cipher_name_set = 1; 339 cipher_name_set = 1;
340 break; 340 break;
341 case ecryptfs_opt_ecryptfs_key_bytes: 341 case ecryptfs_opt_ecryptfs_key_bytes:
342 cipher_key_bytes_src = args[0].from; 342 cipher_key_bytes_src = args[0].from;
343 cipher_key_bytes = 343 cipher_key_bytes =
344 (int)simple_strtol(cipher_key_bytes_src, 344 (int)simple_strtol(cipher_key_bytes_src,
345 &cipher_key_bytes_src, 0); 345 &cipher_key_bytes_src, 0);
346 mount_crypt_stat->global_default_cipher_key_size = 346 mount_crypt_stat->global_default_cipher_key_size =
347 cipher_key_bytes; 347 cipher_key_bytes;
348 cipher_key_bytes_set = 1; 348 cipher_key_bytes_set = 1;
349 break; 349 break;
350 case ecryptfs_opt_passthrough: 350 case ecryptfs_opt_passthrough:
351 mount_crypt_stat->flags |= 351 mount_crypt_stat->flags |=
352 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED; 352 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
353 break; 353 break;
354 case ecryptfs_opt_xattr_metadata: 354 case ecryptfs_opt_xattr_metadata:
355 mount_crypt_stat->flags |= 355 mount_crypt_stat->flags |=
356 ECRYPTFS_XATTR_METADATA_ENABLED; 356 ECRYPTFS_XATTR_METADATA_ENABLED;
357 break; 357 break;
358 case ecryptfs_opt_encrypted_view: 358 case ecryptfs_opt_encrypted_view:
359 mount_crypt_stat->flags |= 359 mount_crypt_stat->flags |=
360 ECRYPTFS_XATTR_METADATA_ENABLED; 360 ECRYPTFS_XATTR_METADATA_ENABLED;
361 mount_crypt_stat->flags |= 361 mount_crypt_stat->flags |=
362 ECRYPTFS_ENCRYPTED_VIEW_ENABLED; 362 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
363 break; 363 break;
364 case ecryptfs_opt_fnek_sig: 364 case ecryptfs_opt_fnek_sig:
365 fnek_src = args[0].from; 365 fnek_src = args[0].from;
366 fnek_dst = 366 fnek_dst =
367 mount_crypt_stat->global_default_fnek_sig; 367 mount_crypt_stat->global_default_fnek_sig;
368 strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX); 368 strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
369 mount_crypt_stat->global_default_fnek_sig[ 369 mount_crypt_stat->global_default_fnek_sig[
370 ECRYPTFS_SIG_SIZE_HEX] = '\0'; 370 ECRYPTFS_SIG_SIZE_HEX] = '\0';
371 rc = ecryptfs_add_global_auth_tok( 371 rc = ecryptfs_add_global_auth_tok(
372 mount_crypt_stat, 372 mount_crypt_stat,
373 mount_crypt_stat->global_default_fnek_sig); 373 mount_crypt_stat->global_default_fnek_sig,
374 ECRYPTFS_AUTH_TOK_FNEK);
374 if (rc) { 375 if (rc) {
375 printk(KERN_ERR "Error attempting to register " 376 printk(KERN_ERR "Error attempting to register "
376 "global fnek sig [%s]; rc = [%d]\n", 377 "global fnek sig [%s]; rc = [%d]\n",
377 mount_crypt_stat->global_default_fnek_sig, 378 mount_crypt_stat->global_default_fnek_sig,
378 rc); 379 rc);
379 goto out; 380 goto out;
380 } 381 }
381 mount_crypt_stat->flags |= 382 mount_crypt_stat->flags |=
382 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES 383 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
383 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK); 384 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
384 break; 385 break;
385 case ecryptfs_opt_fn_cipher: 386 case ecryptfs_opt_fn_cipher:
386 fn_cipher_name_src = args[0].from; 387 fn_cipher_name_src = args[0].from;
387 fn_cipher_name_dst = 388 fn_cipher_name_dst =
388 mount_crypt_stat->global_default_fn_cipher_name; 389 mount_crypt_stat->global_default_fn_cipher_name;
389 strncpy(fn_cipher_name_dst, fn_cipher_name_src, 390 strncpy(fn_cipher_name_dst, fn_cipher_name_src,
390 ECRYPTFS_MAX_CIPHER_NAME_SIZE); 391 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
391 mount_crypt_stat->global_default_fn_cipher_name[ 392 mount_crypt_stat->global_default_fn_cipher_name[
392 ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0'; 393 ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
393 fn_cipher_name_set = 1; 394 fn_cipher_name_set = 1;
394 break; 395 break;
395 case ecryptfs_opt_fn_cipher_key_bytes: 396 case ecryptfs_opt_fn_cipher_key_bytes:
396 fn_cipher_key_bytes_src = args[0].from; 397 fn_cipher_key_bytes_src = args[0].from;
397 fn_cipher_key_bytes = 398 fn_cipher_key_bytes =
398 (int)simple_strtol(fn_cipher_key_bytes_src, 399 (int)simple_strtol(fn_cipher_key_bytes_src,
399 &fn_cipher_key_bytes_src, 0); 400 &fn_cipher_key_bytes_src, 0);
400 mount_crypt_stat->global_default_fn_cipher_key_bytes = 401 mount_crypt_stat->global_default_fn_cipher_key_bytes =
401 fn_cipher_key_bytes; 402 fn_cipher_key_bytes;
402 fn_cipher_key_bytes_set = 1; 403 fn_cipher_key_bytes_set = 1;
403 break; 404 break;
404 case ecryptfs_opt_err: 405 case ecryptfs_opt_err:
405 default: 406 default:
406 printk(KERN_WARNING 407 printk(KERN_WARNING
407 "%s: eCryptfs: unrecognized option [%s]\n", 408 "%s: eCryptfs: unrecognized option [%s]\n",
408 __func__, p); 409 __func__, p);
409 } 410 }
410 } 411 }
411 if (!sig_set) { 412 if (!sig_set) {
412 rc = -EINVAL; 413 rc = -EINVAL;
413 ecryptfs_printk(KERN_ERR, "You must supply at least one valid " 414 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
414 "auth tok signature as a mount " 415 "auth tok signature as a mount "
415 "parameter; see the eCryptfs README\n"); 416 "parameter; see the eCryptfs README\n");
416 goto out; 417 goto out;
417 } 418 }
418 if (!cipher_name_set) { 419 if (!cipher_name_set) {
419 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER); 420 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
420 421
421 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE); 422 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
422 strcpy(mount_crypt_stat->global_default_cipher_name, 423 strcpy(mount_crypt_stat->global_default_cipher_name,
423 ECRYPTFS_DEFAULT_CIPHER); 424 ECRYPTFS_DEFAULT_CIPHER);
424 } 425 }
425 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) 426 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
426 && !fn_cipher_name_set) 427 && !fn_cipher_name_set)
427 strcpy(mount_crypt_stat->global_default_fn_cipher_name, 428 strcpy(mount_crypt_stat->global_default_fn_cipher_name,
428 mount_crypt_stat->global_default_cipher_name); 429 mount_crypt_stat->global_default_cipher_name);
429 if (!cipher_key_bytes_set) 430 if (!cipher_key_bytes_set)
430 mount_crypt_stat->global_default_cipher_key_size = 0; 431 mount_crypt_stat->global_default_cipher_key_size = 0;
431 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) 432 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
432 && !fn_cipher_key_bytes_set) 433 && !fn_cipher_key_bytes_set)
433 mount_crypt_stat->global_default_fn_cipher_key_bytes = 434 mount_crypt_stat->global_default_fn_cipher_key_bytes =
434 mount_crypt_stat->global_default_cipher_key_size; 435 mount_crypt_stat->global_default_cipher_key_size;
435 mutex_lock(&key_tfm_list_mutex); 436 mutex_lock(&key_tfm_list_mutex);
436 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name, 437 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
437 NULL)) { 438 NULL)) {
438 rc = ecryptfs_add_new_key_tfm( 439 rc = ecryptfs_add_new_key_tfm(
439 NULL, mount_crypt_stat->global_default_cipher_name, 440 NULL, mount_crypt_stat->global_default_cipher_name,
440 mount_crypt_stat->global_default_cipher_key_size); 441 mount_crypt_stat->global_default_cipher_key_size);
441 if (rc) { 442 if (rc) {
442 printk(KERN_ERR "Error attempting to initialize " 443 printk(KERN_ERR "Error attempting to initialize "
443 "cipher with name = [%s] and key size = [%td]; " 444 "cipher with name = [%s] and key size = [%td]; "
444 "rc = [%d]\n", 445 "rc = [%d]\n",
445 mount_crypt_stat->global_default_cipher_name, 446 mount_crypt_stat->global_default_cipher_name,
446 mount_crypt_stat->global_default_cipher_key_size, 447 mount_crypt_stat->global_default_cipher_key_size,
447 rc); 448 rc);
448 rc = -EINVAL; 449 rc = -EINVAL;
449 mutex_unlock(&key_tfm_list_mutex); 450 mutex_unlock(&key_tfm_list_mutex);
450 goto out; 451 goto out;
451 } 452 }
452 } 453 }
453 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) 454 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
454 && !ecryptfs_tfm_exists( 455 && !ecryptfs_tfm_exists(
455 mount_crypt_stat->global_default_fn_cipher_name, NULL)) { 456 mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
456 rc = ecryptfs_add_new_key_tfm( 457 rc = ecryptfs_add_new_key_tfm(
457 NULL, mount_crypt_stat->global_default_fn_cipher_name, 458 NULL, mount_crypt_stat->global_default_fn_cipher_name,
458 mount_crypt_stat->global_default_fn_cipher_key_bytes); 459 mount_crypt_stat->global_default_fn_cipher_key_bytes);
459 if (rc) { 460 if (rc) {
460 printk(KERN_ERR "Error attempting to initialize " 461 printk(KERN_ERR "Error attempting to initialize "
461 "cipher with name = [%s] and key size = [%td]; " 462 "cipher with name = [%s] and key size = [%td]; "
462 "rc = [%d]\n", 463 "rc = [%d]\n",
463 mount_crypt_stat->global_default_fn_cipher_name, 464 mount_crypt_stat->global_default_fn_cipher_name,
464 mount_crypt_stat->global_default_fn_cipher_key_bytes, 465 mount_crypt_stat->global_default_fn_cipher_key_bytes,
465 rc); 466 rc);
466 rc = -EINVAL; 467 rc = -EINVAL;
467 mutex_unlock(&key_tfm_list_mutex); 468 mutex_unlock(&key_tfm_list_mutex);
468 goto out; 469 goto out;
469 } 470 }
470 } 471 }
471 mutex_unlock(&key_tfm_list_mutex); 472 mutex_unlock(&key_tfm_list_mutex);
472 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat); 473 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
473 if (rc) 474 if (rc)
474 printk(KERN_WARNING "One or more global auth toks could not " 475 printk(KERN_WARNING "One or more global auth toks could not "
475 "properly register; rc = [%d]\n", rc); 476 "properly register; rc = [%d]\n", rc);
476 out: 477 out:
477 return rc; 478 return rc;
478 } 479 }
479 480
480 struct kmem_cache *ecryptfs_sb_info_cache; 481 struct kmem_cache *ecryptfs_sb_info_cache;
481 482
482 /** 483 /**
483 * ecryptfs_fill_super 484 * ecryptfs_fill_super
484 * @sb: The ecryptfs super block 485 * @sb: The ecryptfs super block
485 * @raw_data: The options passed to mount 486 * @raw_data: The options passed to mount
486 * @silent: Not used but required by function prototype 487 * @silent: Not used but required by function prototype
487 * 488 *
488 * Sets up what we can of the sb, rest is done in ecryptfs_read_super 489 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
489 * 490 *
490 * Returns zero on success; non-zero otherwise 491 * Returns zero on success; non-zero otherwise
491 */ 492 */
492 static int 493 static int
493 ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent) 494 ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
494 { 495 {
495 int rc = 0; 496 int rc = 0;
496 497
497 /* Released in ecryptfs_put_super() */ 498 /* Released in ecryptfs_put_super() */
498 ecryptfs_set_superblock_private(sb, 499 ecryptfs_set_superblock_private(sb,
499 kmem_cache_zalloc(ecryptfs_sb_info_cache, 500 kmem_cache_zalloc(ecryptfs_sb_info_cache,
500 GFP_KERNEL)); 501 GFP_KERNEL));
501 if (!ecryptfs_superblock_to_private(sb)) { 502 if (!ecryptfs_superblock_to_private(sb)) {
502 ecryptfs_printk(KERN_WARNING, "Out of memory\n"); 503 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
503 rc = -ENOMEM; 504 rc = -ENOMEM;
504 goto out; 505 goto out;
505 } 506 }
506 sb->s_op = &ecryptfs_sops; 507 sb->s_op = &ecryptfs_sops;
507 /* Released through deactivate_super(sb) from get_sb_nodev */ 508 /* Released through deactivate_super(sb) from get_sb_nodev */
508 sb->s_root = d_alloc(NULL, &(const struct qstr) { 509 sb->s_root = d_alloc(NULL, &(const struct qstr) {
509 .hash = 0,.name = "/",.len = 1}); 510 .hash = 0,.name = "/",.len = 1});
510 if (!sb->s_root) { 511 if (!sb->s_root) {
511 ecryptfs_printk(KERN_ERR, "d_alloc failed\n"); 512 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
512 rc = -ENOMEM; 513 rc = -ENOMEM;
513 goto out; 514 goto out;
514 } 515 }
515 sb->s_root->d_op = &ecryptfs_dops; 516 sb->s_root->d_op = &ecryptfs_dops;
516 sb->s_root->d_sb = sb; 517 sb->s_root->d_sb = sb;
517 sb->s_root->d_parent = sb->s_root; 518 sb->s_root->d_parent = sb->s_root;
518 /* Released in d_release when dput(sb->s_root) is called */ 519 /* Released in d_release when dput(sb->s_root) is called */
519 /* through deactivate_super(sb) from get_sb_nodev() */ 520 /* through deactivate_super(sb) from get_sb_nodev() */
520 ecryptfs_set_dentry_private(sb->s_root, 521 ecryptfs_set_dentry_private(sb->s_root,
521 kmem_cache_zalloc(ecryptfs_dentry_info_cache, 522 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
522 GFP_KERNEL)); 523 GFP_KERNEL));
523 if (!ecryptfs_dentry_to_private(sb->s_root)) { 524 if (!ecryptfs_dentry_to_private(sb->s_root)) {
524 ecryptfs_printk(KERN_ERR, 525 ecryptfs_printk(KERN_ERR,
525 "dentry_info_cache alloc failed\n"); 526 "dentry_info_cache alloc failed\n");
526 rc = -ENOMEM; 527 rc = -ENOMEM;
527 goto out; 528 goto out;
528 } 529 }
529 rc = 0; 530 rc = 0;
530 out: 531 out:
531 /* Should be able to rely on deactivate_super called from 532 /* Should be able to rely on deactivate_super called from
532 * get_sb_nodev */ 533 * get_sb_nodev */
533 return rc; 534 return rc;
534 } 535 }
535 536
536 /** 537 /**
537 * ecryptfs_read_super 538 * ecryptfs_read_super
538 * @sb: The ecryptfs super block 539 * @sb: The ecryptfs super block
539 * @dev_name: The path to mount over 540 * @dev_name: The path to mount over
540 * 541 *
541 * Read the super block of the lower filesystem, and use 542 * Read the super block of the lower filesystem, and use
542 * ecryptfs_interpose to create our initial inode and super block 543 * ecryptfs_interpose to create our initial inode and super block
543 * struct. 544 * struct.
544 */ 545 */
545 static int ecryptfs_read_super(struct super_block *sb, const char *dev_name) 546 static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
546 { 547 {
547 struct path path; 548 struct path path;
548 int rc; 549 int rc;
549 550
550 rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path); 551 rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
551 if (rc) { 552 if (rc) {
552 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n"); 553 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
553 goto out; 554 goto out;
554 } 555 }
555 ecryptfs_set_superblock_lower(sb, path.dentry->d_sb); 556 ecryptfs_set_superblock_lower(sb, path.dentry->d_sb);
556 sb->s_maxbytes = path.dentry->d_sb->s_maxbytes; 557 sb->s_maxbytes = path.dentry->d_sb->s_maxbytes;
557 sb->s_blocksize = path.dentry->d_sb->s_blocksize; 558 sb->s_blocksize = path.dentry->d_sb->s_blocksize;
558 ecryptfs_set_dentry_lower(sb->s_root, path.dentry); 559 ecryptfs_set_dentry_lower(sb->s_root, path.dentry);
559 ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt); 560 ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt);
560 rc = ecryptfs_interpose(path.dentry, sb->s_root, sb, 0); 561 rc = ecryptfs_interpose(path.dentry, sb->s_root, sb, 0);
561 if (rc) 562 if (rc)
562 goto out_free; 563 goto out_free;
563 rc = 0; 564 rc = 0;
564 goto out; 565 goto out;
565 out_free: 566 out_free:
566 path_put(&path); 567 path_put(&path);
567 out: 568 out:
568 return rc; 569 return rc;
569 } 570 }
570 571
571 /** 572 /**
572 * ecryptfs_get_sb 573 * ecryptfs_get_sb
573 * @fs_type 574 * @fs_type
574 * @flags 575 * @flags
575 * @dev_name: The path to mount over 576 * @dev_name: The path to mount over
576 * @raw_data: The options passed into the kernel 577 * @raw_data: The options passed into the kernel
577 * 578 *
578 * The whole ecryptfs_get_sb process is broken into 4 functions: 579 * The whole ecryptfs_get_sb process is broken into 4 functions:
579 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any 580 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
580 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block 581 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
581 * with as much information as it can before needing 582 * with as much information as it can before needing
582 * the lower filesystem. 583 * the lower filesystem.
583 * ecryptfs_read_super(): this accesses the lower filesystem and uses 584 * ecryptfs_read_super(): this accesses the lower filesystem and uses
584 * ecryptfs_interpolate to perform most of the linking 585 * ecryptfs_interpolate to perform most of the linking
585 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs 586 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
586 */ 587 */
587 static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags, 588 static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
588 const char *dev_name, void *raw_data, 589 const char *dev_name, void *raw_data,
589 struct vfsmount *mnt) 590 struct vfsmount *mnt)
590 { 591 {
591 int rc; 592 int rc;
592 struct super_block *sb; 593 struct super_block *sb;
593 594
594 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt); 595 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
595 if (rc < 0) { 596 if (rc < 0) {
596 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc); 597 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
597 goto out; 598 goto out;
598 } 599 }
599 sb = mnt->mnt_sb; 600 sb = mnt->mnt_sb;
600 rc = ecryptfs_parse_options(sb, raw_data); 601 rc = ecryptfs_parse_options(sb, raw_data);
601 if (rc) { 602 if (rc) {
602 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc); 603 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
603 goto out_abort; 604 goto out_abort;
604 } 605 }
605 rc = ecryptfs_read_super(sb, dev_name); 606 rc = ecryptfs_read_super(sb, dev_name);
606 if (rc) { 607 if (rc) {
607 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc); 608 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
608 goto out_abort; 609 goto out_abort;
609 } 610 }
610 goto out; 611 goto out;
611 out_abort: 612 out_abort:
612 dput(sb->s_root); 613 dput(sb->s_root);
613 up_write(&sb->s_umount); 614 up_write(&sb->s_umount);
614 deactivate_super(sb); 615 deactivate_super(sb);
615 out: 616 out:
616 return rc; 617 return rc;
617 } 618 }
618 619
619 /** 620 /**
620 * ecryptfs_kill_block_super 621 * ecryptfs_kill_block_super
621 * @sb: The ecryptfs super block 622 * @sb: The ecryptfs super block
622 * 623 *
623 * Used to bring the superblock down and free the private data. 624 * Used to bring the superblock down and free the private data.
624 * Private data is free'd in ecryptfs_put_super() 625 * Private data is free'd in ecryptfs_put_super()
625 */ 626 */
626 static void ecryptfs_kill_block_super(struct super_block *sb) 627 static void ecryptfs_kill_block_super(struct super_block *sb)
627 { 628 {
628 generic_shutdown_super(sb); 629 generic_shutdown_super(sb);
629 } 630 }
630 631
631 static struct file_system_type ecryptfs_fs_type = { 632 static struct file_system_type ecryptfs_fs_type = {
632 .owner = THIS_MODULE, 633 .owner = THIS_MODULE,
633 .name = "ecryptfs", 634 .name = "ecryptfs",
634 .get_sb = ecryptfs_get_sb, 635 .get_sb = ecryptfs_get_sb,
635 .kill_sb = ecryptfs_kill_block_super, 636 .kill_sb = ecryptfs_kill_block_super,
636 .fs_flags = 0 637 .fs_flags = 0
637 }; 638 };
638 639
639 /** 640 /**
640 * inode_info_init_once 641 * inode_info_init_once
641 * 642 *
642 * Initializes the ecryptfs_inode_info_cache when it is created 643 * Initializes the ecryptfs_inode_info_cache when it is created
643 */ 644 */
644 static void 645 static void
645 inode_info_init_once(void *vptr) 646 inode_info_init_once(void *vptr)
646 { 647 {
647 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr; 648 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
648 649
649 inode_init_once(&ei->vfs_inode); 650 inode_init_once(&ei->vfs_inode);
650 } 651 }
651 652
652 static struct ecryptfs_cache_info { 653 static struct ecryptfs_cache_info {
653 struct kmem_cache **cache; 654 struct kmem_cache **cache;
654 const char *name; 655 const char *name;
655 size_t size; 656 size_t size;
656 void (*ctor)(void *obj); 657 void (*ctor)(void *obj);
657 } ecryptfs_cache_infos[] = { 658 } ecryptfs_cache_infos[] = {
658 { 659 {
659 .cache = &ecryptfs_auth_tok_list_item_cache, 660 .cache = &ecryptfs_auth_tok_list_item_cache,
660 .name = "ecryptfs_auth_tok_list_item", 661 .name = "ecryptfs_auth_tok_list_item",
661 .size = sizeof(struct ecryptfs_auth_tok_list_item), 662 .size = sizeof(struct ecryptfs_auth_tok_list_item),
662 }, 663 },
663 { 664 {
664 .cache = &ecryptfs_file_info_cache, 665 .cache = &ecryptfs_file_info_cache,
665 .name = "ecryptfs_file_cache", 666 .name = "ecryptfs_file_cache",
666 .size = sizeof(struct ecryptfs_file_info), 667 .size = sizeof(struct ecryptfs_file_info),
667 }, 668 },
668 { 669 {
669 .cache = &ecryptfs_dentry_info_cache, 670 .cache = &ecryptfs_dentry_info_cache,
670 .name = "ecryptfs_dentry_info_cache", 671 .name = "ecryptfs_dentry_info_cache",
671 .size = sizeof(struct ecryptfs_dentry_info), 672 .size = sizeof(struct ecryptfs_dentry_info),
672 }, 673 },
673 { 674 {
674 .cache = &ecryptfs_inode_info_cache, 675 .cache = &ecryptfs_inode_info_cache,
675 .name = "ecryptfs_inode_cache", 676 .name = "ecryptfs_inode_cache",
676 .size = sizeof(struct ecryptfs_inode_info), 677 .size = sizeof(struct ecryptfs_inode_info),
677 .ctor = inode_info_init_once, 678 .ctor = inode_info_init_once,
678 }, 679 },
679 { 680 {
680 .cache = &ecryptfs_sb_info_cache, 681 .cache = &ecryptfs_sb_info_cache,
681 .name = "ecryptfs_sb_cache", 682 .name = "ecryptfs_sb_cache",
682 .size = sizeof(struct ecryptfs_sb_info), 683 .size = sizeof(struct ecryptfs_sb_info),
683 }, 684 },
684 { 685 {
685 .cache = &ecryptfs_header_cache_1, 686 .cache = &ecryptfs_header_cache_1,
686 .name = "ecryptfs_headers_1", 687 .name = "ecryptfs_headers_1",
687 .size = PAGE_CACHE_SIZE, 688 .size = PAGE_CACHE_SIZE,
688 }, 689 },
689 { 690 {
690 .cache = &ecryptfs_header_cache_2, 691 .cache = &ecryptfs_header_cache_2,
691 .name = "ecryptfs_headers_2", 692 .name = "ecryptfs_headers_2",
692 .size = PAGE_CACHE_SIZE, 693 .size = PAGE_CACHE_SIZE,
693 }, 694 },
694 { 695 {
695 .cache = &ecryptfs_xattr_cache, 696 .cache = &ecryptfs_xattr_cache,
696 .name = "ecryptfs_xattr_cache", 697 .name = "ecryptfs_xattr_cache",
697 .size = PAGE_CACHE_SIZE, 698 .size = PAGE_CACHE_SIZE,
698 }, 699 },
699 { 700 {
700 .cache = &ecryptfs_key_record_cache, 701 .cache = &ecryptfs_key_record_cache,
701 .name = "ecryptfs_key_record_cache", 702 .name = "ecryptfs_key_record_cache",
702 .size = sizeof(struct ecryptfs_key_record), 703 .size = sizeof(struct ecryptfs_key_record),
703 }, 704 },
704 { 705 {
705 .cache = &ecryptfs_key_sig_cache, 706 .cache = &ecryptfs_key_sig_cache,
706 .name = "ecryptfs_key_sig_cache", 707 .name = "ecryptfs_key_sig_cache",
707 .size = sizeof(struct ecryptfs_key_sig), 708 .size = sizeof(struct ecryptfs_key_sig),
708 }, 709 },
709 { 710 {
710 .cache = &ecryptfs_global_auth_tok_cache, 711 .cache = &ecryptfs_global_auth_tok_cache,
711 .name = "ecryptfs_global_auth_tok_cache", 712 .name = "ecryptfs_global_auth_tok_cache",
712 .size = sizeof(struct ecryptfs_global_auth_tok), 713 .size = sizeof(struct ecryptfs_global_auth_tok),
713 }, 714 },
714 { 715 {
715 .cache = &ecryptfs_key_tfm_cache, 716 .cache = &ecryptfs_key_tfm_cache,
716 .name = "ecryptfs_key_tfm_cache", 717 .name = "ecryptfs_key_tfm_cache",
717 .size = sizeof(struct ecryptfs_key_tfm), 718 .size = sizeof(struct ecryptfs_key_tfm),
718 }, 719 },
719 { 720 {
720 .cache = &ecryptfs_open_req_cache, 721 .cache = &ecryptfs_open_req_cache,
721 .name = "ecryptfs_open_req_cache", 722 .name = "ecryptfs_open_req_cache",
722 .size = sizeof(struct ecryptfs_open_req), 723 .size = sizeof(struct ecryptfs_open_req),
723 }, 724 },
724 }; 725 };
725 726
726 static void ecryptfs_free_kmem_caches(void) 727 static void ecryptfs_free_kmem_caches(void)
727 { 728 {
728 int i; 729 int i;
729 730
730 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { 731 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
731 struct ecryptfs_cache_info *info; 732 struct ecryptfs_cache_info *info;
732 733
733 info = &ecryptfs_cache_infos[i]; 734 info = &ecryptfs_cache_infos[i];
734 if (*(info->cache)) 735 if (*(info->cache))
735 kmem_cache_destroy(*(info->cache)); 736 kmem_cache_destroy(*(info->cache));
736 } 737 }
737 } 738 }
738 739
739 /** 740 /**
740 * ecryptfs_init_kmem_caches 741 * ecryptfs_init_kmem_caches
741 * 742 *
742 * Returns zero on success; non-zero otherwise 743 * Returns zero on success; non-zero otherwise
743 */ 744 */
744 static int ecryptfs_init_kmem_caches(void) 745 static int ecryptfs_init_kmem_caches(void)
745 { 746 {
746 int i; 747 int i;
747 748
748 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { 749 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
749 struct ecryptfs_cache_info *info; 750 struct ecryptfs_cache_info *info;
750 751
751 info = &ecryptfs_cache_infos[i]; 752 info = &ecryptfs_cache_infos[i];
752 *(info->cache) = kmem_cache_create(info->name, info->size, 753 *(info->cache) = kmem_cache_create(info->name, info->size,
753 0, SLAB_HWCACHE_ALIGN, info->ctor); 754 0, SLAB_HWCACHE_ALIGN, info->ctor);
754 if (!*(info->cache)) { 755 if (!*(info->cache)) {
755 ecryptfs_free_kmem_caches(); 756 ecryptfs_free_kmem_caches();
756 ecryptfs_printk(KERN_WARNING, "%s: " 757 ecryptfs_printk(KERN_WARNING, "%s: "
757 "kmem_cache_create failed\n", 758 "kmem_cache_create failed\n",
758 info->name); 759 info->name);
759 return -ENOMEM; 760 return -ENOMEM;
760 } 761 }
761 } 762 }
762 return 0; 763 return 0;
763 } 764 }
764 765
765 static struct kobject *ecryptfs_kobj; 766 static struct kobject *ecryptfs_kobj;
766 767
767 static ssize_t version_show(struct kobject *kobj, 768 static ssize_t version_show(struct kobject *kobj,
768 struct kobj_attribute *attr, char *buff) 769 struct kobj_attribute *attr, char *buff)
769 { 770 {
770 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK); 771 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
771 } 772 }
772 773
773 static struct kobj_attribute version_attr = __ATTR_RO(version); 774 static struct kobj_attribute version_attr = __ATTR_RO(version);
774 775
775 static struct attribute *attributes[] = { 776 static struct attribute *attributes[] = {
776 &version_attr.attr, 777 &version_attr.attr,
777 NULL, 778 NULL,
778 }; 779 };
779 780
780 static struct attribute_group attr_group = { 781 static struct attribute_group attr_group = {
781 .attrs = attributes, 782 .attrs = attributes,
782 }; 783 };
783 784
784 static int do_sysfs_registration(void) 785 static int do_sysfs_registration(void)
785 { 786 {
786 int rc; 787 int rc;
787 788
788 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj); 789 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
789 if (!ecryptfs_kobj) { 790 if (!ecryptfs_kobj) {
790 printk(KERN_ERR "Unable to create ecryptfs kset\n"); 791 printk(KERN_ERR "Unable to create ecryptfs kset\n");
791 rc = -ENOMEM; 792 rc = -ENOMEM;
792 goto out; 793 goto out;
793 } 794 }
794 rc = sysfs_create_group(ecryptfs_kobj, &attr_group); 795 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
795 if (rc) { 796 if (rc) {
796 printk(KERN_ERR 797 printk(KERN_ERR
797 "Unable to create ecryptfs version attributes\n"); 798 "Unable to create ecryptfs version attributes\n");
798 kobject_put(ecryptfs_kobj); 799 kobject_put(ecryptfs_kobj);
799 } 800 }
800 out: 801 out:
801 return rc; 802 return rc;
802 } 803 }
803 804
804 static void do_sysfs_unregistration(void) 805 static void do_sysfs_unregistration(void)
805 { 806 {
806 sysfs_remove_group(ecryptfs_kobj, &attr_group); 807 sysfs_remove_group(ecryptfs_kobj, &attr_group);
807 kobject_put(ecryptfs_kobj); 808 kobject_put(ecryptfs_kobj);
808 } 809 }
809 810
810 static int __init ecryptfs_init(void) 811 static int __init ecryptfs_init(void)
811 { 812 {
812 int rc; 813 int rc;
813 814
814 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) { 815 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
815 rc = -EINVAL; 816 rc = -EINVAL;
816 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is " 817 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
817 "larger than the host's page size, and so " 818 "larger than the host's page size, and so "
818 "eCryptfs cannot run on this system. The " 819 "eCryptfs cannot run on this system. The "
819 "default eCryptfs extent size is [%d] bytes; " 820 "default eCryptfs extent size is [%d] bytes; "
820 "the page size is [%d] bytes.\n", 821 "the page size is [%d] bytes.\n",
821 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE); 822 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
822 goto out; 823 goto out;
823 } 824 }
824 rc = ecryptfs_init_kmem_caches(); 825 rc = ecryptfs_init_kmem_caches();
825 if (rc) { 826 if (rc) {
826 printk(KERN_ERR 827 printk(KERN_ERR
827 "Failed to allocate one or more kmem_cache objects\n"); 828 "Failed to allocate one or more kmem_cache objects\n");
828 goto out; 829 goto out;
829 } 830 }
830 rc = register_filesystem(&ecryptfs_fs_type); 831 rc = register_filesystem(&ecryptfs_fs_type);
831 if (rc) { 832 if (rc) {
832 printk(KERN_ERR "Failed to register filesystem\n"); 833 printk(KERN_ERR "Failed to register filesystem\n");
833 goto out_free_kmem_caches; 834 goto out_free_kmem_caches;
834 } 835 }
835 rc = do_sysfs_registration(); 836 rc = do_sysfs_registration();
836 if (rc) { 837 if (rc) {
837 printk(KERN_ERR "sysfs registration failed\n"); 838 printk(KERN_ERR "sysfs registration failed\n");
838 goto out_unregister_filesystem; 839 goto out_unregister_filesystem;
839 } 840 }
840 rc = ecryptfs_init_kthread(); 841 rc = ecryptfs_init_kthread();
841 if (rc) { 842 if (rc) {
842 printk(KERN_ERR "%s: kthread initialization failed; " 843 printk(KERN_ERR "%s: kthread initialization failed; "
843 "rc = [%d]\n", __func__, rc); 844 "rc = [%d]\n", __func__, rc);
844 goto out_do_sysfs_unregistration; 845 goto out_do_sysfs_unregistration;
845 } 846 }
846 rc = ecryptfs_init_messaging(); 847 rc = ecryptfs_init_messaging();
847 if (rc) { 848 if (rc) {
848 printk(KERN_ERR "Failure occured while attempting to " 849 printk(KERN_ERR "Failure occured while attempting to "
849 "initialize the communications channel to " 850 "initialize the communications channel to "
850 "ecryptfsd\n"); 851 "ecryptfsd\n");
851 goto out_destroy_kthread; 852 goto out_destroy_kthread;
852 } 853 }
853 rc = ecryptfs_init_crypto(); 854 rc = ecryptfs_init_crypto();
854 if (rc) { 855 if (rc) {
855 printk(KERN_ERR "Failure whilst attempting to init crypto; " 856 printk(KERN_ERR "Failure whilst attempting to init crypto; "
856 "rc = [%d]\n", rc); 857 "rc = [%d]\n", rc);
857 goto out_release_messaging; 858 goto out_release_messaging;
858 } 859 }
859 if (ecryptfs_verbosity > 0) 860 if (ecryptfs_verbosity > 0)
860 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values " 861 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
861 "will be written to the syslog!\n", ecryptfs_verbosity); 862 "will be written to the syslog!\n", ecryptfs_verbosity);
862 863
863 goto out; 864 goto out;
864 out_release_messaging: 865 out_release_messaging:
865 ecryptfs_release_messaging(); 866 ecryptfs_release_messaging();
866 out_destroy_kthread: 867 out_destroy_kthread:
867 ecryptfs_destroy_kthread(); 868 ecryptfs_destroy_kthread();
868 out_do_sysfs_unregistration: 869 out_do_sysfs_unregistration:
869 do_sysfs_unregistration(); 870 do_sysfs_unregistration();
870 out_unregister_filesystem: 871 out_unregister_filesystem:
871 unregister_filesystem(&ecryptfs_fs_type); 872 unregister_filesystem(&ecryptfs_fs_type);
872 out_free_kmem_caches: 873 out_free_kmem_caches:
873 ecryptfs_free_kmem_caches(); 874 ecryptfs_free_kmem_caches();
874 out: 875 out:
875 return rc; 876 return rc;
876 } 877 }
877 878
878 static void __exit ecryptfs_exit(void) 879 static void __exit ecryptfs_exit(void)
879 { 880 {
880 int rc; 881 int rc;
881 882
882 rc = ecryptfs_destroy_crypto(); 883 rc = ecryptfs_destroy_crypto();
883 if (rc) 884 if (rc)
884 printk(KERN_ERR "Failure whilst attempting to destroy crypto; " 885 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
885 "rc = [%d]\n", rc); 886 "rc = [%d]\n", rc);
886 ecryptfs_release_messaging(); 887 ecryptfs_release_messaging();
887 ecryptfs_destroy_kthread(); 888 ecryptfs_destroy_kthread();
888 do_sysfs_unregistration(); 889 do_sysfs_unregistration();
889 unregister_filesystem(&ecryptfs_fs_type); 890 unregister_filesystem(&ecryptfs_fs_type);
890 ecryptfs_free_kmem_caches(); 891 ecryptfs_free_kmem_caches();
891 } 892 }
892 893
893 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>"); 894 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
894 MODULE_DESCRIPTION("eCryptfs"); 895 MODULE_DESCRIPTION("eCryptfs");
895 896
896 MODULE_LICENSE("GPL"); 897 MODULE_LICENSE("GPL");
897 898
898 module_init(ecryptfs_init) 899 module_init(ecryptfs_init)
899 module_exit(ecryptfs_exit) 900 module_exit(ecryptfs_exit)
900 901