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