Commit | Line | Data |
---|---|---|
237fead6 MH |
1 | /** |
2 | * eCryptfs: Linux filesystem encryption layer | |
3 | * | |
4 | * Copyright (C) 1997-2003 Erez Zadok | |
5 | * Copyright (C) 2001-2003 Stony Brook University | |
dd2a3b7a | 6 | * Copyright (C) 2004-2007 International Business Machines Corp. |
237fead6 MH |
7 | * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> |
8 | * Michael C. Thompson <mcthomps@us.ibm.com> | |
dddfa461 | 9 | * Tyler Hicks <tyhicks@ou.edu> |
237fead6 MH |
10 | * |
11 | * This program is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU General Public License as | |
13 | * published by the Free Software Foundation; either version 2 of the | |
14 | * License, or (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, but | |
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
24 | * 02111-1307, USA. | |
25 | */ | |
26 | ||
27 | #include <linux/dcache.h> | |
28 | #include <linux/file.h> | |
29 | #include <linux/module.h> | |
30 | #include <linux/namei.h> | |
31 | #include <linux/skbuff.h> | |
32 | #include <linux/crypto.h> | |
237fead6 | 33 | #include <linux/mount.h> |
237fead6 MH |
34 | #include <linux/pagemap.h> |
35 | #include <linux/key.h> | |
36 | #include <linux/parser.h> | |
0cc72dc7 | 37 | #include <linux/fs_stack.h> |
5a0e3ad6 | 38 | #include <linux/slab.h> |
070baa51 | 39 | #include <linux/magic.h> |
237fead6 MH |
40 | #include "ecryptfs_kernel.h" |
41 | ||
42 | /** | |
43 | * Module parameter that defines the ecryptfs_verbosity level. | |
44 | */ | |
45 | int ecryptfs_verbosity = 0; | |
46 | ||
47 | module_param(ecryptfs_verbosity, int, 0); | |
48 | MODULE_PARM_DESC(ecryptfs_verbosity, | |
49 | "Initial verbosity level (0 or 1; defaults to " | |
50 | "0, which is Quiet)"); | |
51 | ||
dddfa461 | 52 | /** |
624ae528 | 53 | * Module parameter that defines the number of message buffer elements |
dddfa461 MH |
54 | */ |
55 | unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS; | |
56 | ||
57 | module_param(ecryptfs_message_buf_len, uint, 0); | |
58 | MODULE_PARM_DESC(ecryptfs_message_buf_len, | |
59 | "Number of message buffer elements"); | |
60 | ||
61 | /** | |
62 | * Module parameter that defines the maximum guaranteed amount of time to wait | |
624ae528 | 63 | * for a response from ecryptfsd. The actual sleep time will be, more than |
dddfa461 | 64 | * likely, a small amount greater than this specified value, but only less if |
624ae528 | 65 | * the message successfully arrives. |
dddfa461 MH |
66 | */ |
67 | signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ; | |
68 | ||
69 | module_param(ecryptfs_message_wait_timeout, long, 0); | |
70 | MODULE_PARM_DESC(ecryptfs_message_wait_timeout, | |
71 | "Maximum number of seconds that an operation will " | |
72 | "sleep while waiting for a message response from " | |
73 | "userspace"); | |
74 | ||
75 | /** | |
76 | * Module parameter that is an estimate of the maximum number of users | |
77 | * that will be concurrently using eCryptfs. Set this to the right | |
78 | * value to balance performance and memory use. | |
79 | */ | |
80 | unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS; | |
81 | ||
82 | module_param(ecryptfs_number_of_users, uint, 0); | |
83 | MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of " | |
84 | "concurrent users of eCryptfs"); | |
85 | ||
237fead6 MH |
86 | void __ecryptfs_printk(const char *fmt, ...) |
87 | { | |
88 | va_list args; | |
89 | va_start(args, fmt); | |
90 | if (fmt[1] == '7') { /* KERN_DEBUG */ | |
91 | if (ecryptfs_verbosity >= 1) | |
92 | vprintk(fmt, args); | |
93 | } else | |
94 | vprintk(fmt, args); | |
95 | va_end(args); | |
96 | } | |
97 | ||
4981e081 MH |
98 | /** |
99 | * ecryptfs_init_persistent_file | |
100 | * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with | |
101 | * the lower dentry and the lower mount set | |
102 | * | |
103 | * eCryptfs only ever keeps a single open file for every lower | |
104 | * inode. All I/O operations to the lower inode occur through that | |
105 | * file. When the first eCryptfs dentry that interposes with the first | |
106 | * lower dentry for that inode is created, this function creates the | |
107 | * persistent file struct and associates it with the eCryptfs | |
108 | * inode. When the eCryptfs inode is destroyed, the file is closed. | |
109 | * | |
110 | * The persistent file will be opened with read/write permissions, if | |
111 | * possible. Otherwise, it is opened read-only. | |
112 | * | |
113 | * This function does nothing if a lower persistent file is already | |
114 | * associated with the eCryptfs inode. | |
115 | * | |
116 | * Returns zero on success; non-zero otherwise | |
117 | */ | |
72b55fff | 118 | int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry) |
4981e081 | 119 | { |
745ca247 | 120 | const struct cred *cred = current_cred(); |
4981e081 MH |
121 | struct ecryptfs_inode_info *inode_info = |
122 | ecryptfs_inode_to_private(ecryptfs_dentry->d_inode); | |
123 | int rc = 0; | |
124 | ||
4981e081 MH |
125 | if (!inode_info->lower_file) { |
126 | struct dentry *lower_dentry; | |
127 | struct vfsmount *lower_mnt = | |
128 | ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry); | |
129 | ||
130 | lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); | |
746f1e55 | 131 | rc = ecryptfs_privileged_open(&inode_info->lower_file, |
745ca247 | 132 | lower_dentry, lower_mnt, cred); |
ac22ba23 | 133 | if (rc) { |
4981e081 | 134 | printk(KERN_ERR "Error opening lower persistent file " |
746f1e55 MH |
135 | "for lower_dentry [0x%p] and lower_mnt [0x%p]; " |
136 | "rc = [%d]\n", lower_dentry, lower_mnt, rc); | |
4981e081 | 137 | inode_info->lower_file = NULL; |
b65a9cfc | 138 | } |
4981e081 | 139 | } |
4981e081 MH |
140 | return rc; |
141 | } | |
142 | ||
6254b32b | 143 | static struct inode *ecryptfs_get_inode(struct inode *lower_inode, |
66cb7666 | 144 | struct super_block *sb) |
237fead6 | 145 | { |
237fead6 MH |
146 | struct inode *inode; |
147 | int rc = 0; | |
148 | ||
237fead6 MH |
149 | if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) { |
150 | rc = -EXDEV; | |
151 | goto out; | |
152 | } | |
153 | if (!igrab(lower_inode)) { | |
154 | rc = -ESTALE; | |
155 | goto out; | |
156 | } | |
157 | inode = iget5_locked(sb, (unsigned long)lower_inode, | |
158 | ecryptfs_inode_test, ecryptfs_inode_set, | |
159 | lower_inode); | |
160 | if (!inode) { | |
161 | rc = -EACCES; | |
162 | iput(lower_inode); | |
163 | goto out; | |
164 | } | |
165 | if (inode->i_state & I_NEW) | |
166 | unlock_new_inode(inode); | |
167 | else | |
168 | iput(lower_inode); | |
169 | if (S_ISLNK(lower_inode->i_mode)) | |
170 | inode->i_op = &ecryptfs_symlink_iops; | |
171 | else if (S_ISDIR(lower_inode->i_mode)) | |
172 | inode->i_op = &ecryptfs_dir_iops; | |
173 | if (S_ISDIR(lower_inode->i_mode)) | |
174 | inode->i_fop = &ecryptfs_dir_fops; | |
26da8205 | 175 | if (special_file(lower_inode->i_mode)) |
237fead6 MH |
176 | init_special_inode(inode, lower_inode->i_mode, |
177 | lower_inode->i_rdev); | |
9afa2fb6 | 178 | fsstack_copy_attr_all(inode, lower_inode); |
237fead6 MH |
179 | /* This size will be overwritten for real files w/ headers and |
180 | * other metadata */ | |
0cc72dc7 | 181 | fsstack_copy_inode_size(inode, lower_inode); |
66cb7666 AV |
182 | return inode; |
183 | out: | |
184 | return ERR_PTR(rc); | |
185 | } | |
186 | ||
187 | /** | |
188 | * ecryptfs_interpose | |
189 | * @lower_dentry: Existing dentry in the lower filesystem | |
190 | * @dentry: ecryptfs' dentry | |
191 | * @sb: ecryptfs's super_block | |
192 | * @flags: flags to govern behavior of interpose procedure | |
193 | * | |
194 | * Interposes upper and lower dentries. | |
195 | * | |
196 | * Returns zero on success; non-zero otherwise | |
197 | */ | |
198 | int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry, | |
199 | struct super_block *sb, u32 flags) | |
200 | { | |
201 | struct inode *lower_inode = lower_dentry->d_inode; | |
202 | struct inode *inode = ecryptfs_get_inode(lower_inode, sb); | |
6254b32b | 203 | if (IS_ERR(inode)) |
66cb7666 | 204 | return PTR_ERR(inode); |
ae6e8459 TH |
205 | if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD) |
206 | d_add(dentry, inode); | |
207 | else | |
208 | d_instantiate(dentry, inode); | |
66cb7666 | 209 | return 0; |
237fead6 MH |
210 | } |
211 | ||
2830bfd6 ES |
212 | enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, |
213 | ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher, | |
214 | ecryptfs_opt_ecryptfs_key_bytes, | |
17398957 | 215 | ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata, |
87c94c4d MH |
216 | ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig, |
217 | ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes, | |
f16feb51 RS |
218 | ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only, |
219 | ecryptfs_opt_err }; | |
237fead6 | 220 | |
a447c093 | 221 | static const match_table_t tokens = { |
237fead6 MH |
222 | {ecryptfs_opt_sig, "sig=%s"}, |
223 | {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"}, | |
237fead6 MH |
224 | {ecryptfs_opt_cipher, "cipher=%s"}, |
225 | {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"}, | |
226 | {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"}, | |
227 | {ecryptfs_opt_passthrough, "ecryptfs_passthrough"}, | |
17398957 MH |
228 | {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"}, |
229 | {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"}, | |
87c94c4d MH |
230 | {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"}, |
231 | {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"}, | |
232 | {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"}, | |
e77cc8d2 | 233 | {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"}, |
f16feb51 | 234 | {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"}, |
237fead6 MH |
235 | {ecryptfs_opt_err, NULL} |
236 | }; | |
237 | ||
f4aad16a MH |
238 | static int ecryptfs_init_global_auth_toks( |
239 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) | |
237fead6 | 240 | { |
f4aad16a | 241 | struct ecryptfs_global_auth_tok *global_auth_tok; |
0e1fc5ef | 242 | struct ecryptfs_auth_tok *auth_tok; |
237fead6 | 243 | int rc = 0; |
237fead6 | 244 | |
f4aad16a MH |
245 | list_for_each_entry(global_auth_tok, |
246 | &mount_crypt_stat->global_auth_tok_list, | |
247 | mount_crypt_stat_list) { | |
5dda6992 | 248 | rc = ecryptfs_keyring_auth_tok_for_sig( |
0e1fc5ef | 249 | &global_auth_tok->global_auth_tok_key, &auth_tok, |
5dda6992 MH |
250 | global_auth_tok->sig); |
251 | if (rc) { | |
f4aad16a MH |
252 | printk(KERN_ERR "Could not find valid key in user " |
253 | "session keyring for sig specified in mount " | |
254 | "option: [%s]\n", global_auth_tok->sig); | |
255 | global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID; | |
982363c9 | 256 | goto out; |
b5695d04 | 257 | } else { |
f4aad16a | 258 | global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID; |
b5695d04 RS |
259 | up_write(&(global_auth_tok->global_auth_tok_key)->sem); |
260 | } | |
237fead6 | 261 | } |
982363c9 | 262 | out: |
237fead6 MH |
263 | return rc; |
264 | } | |
265 | ||
f4aad16a MH |
266 | static void ecryptfs_init_mount_crypt_stat( |
267 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) | |
268 | { | |
269 | memset((void *)mount_crypt_stat, 0, | |
270 | sizeof(struct ecryptfs_mount_crypt_stat)); | |
271 | INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list); | |
272 | mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex); | |
273 | mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED; | |
274 | } | |
275 | ||
237fead6 MH |
276 | /** |
277 | * ecryptfs_parse_options | |
278 | * @sb: The ecryptfs super block | |
279 | * @options: The options pased to the kernel | |
280 | * | |
281 | * Parse mount options: | |
282 | * debug=N - ecryptfs_verbosity level for debug output | |
283 | * sig=XXX - description(signature) of the key to use | |
284 | * | |
285 | * Returns the dentry object of the lower-level (lower/interposed) | |
286 | * directory; We want to mount our stackable file system on top of | |
287 | * that lower directory. | |
288 | * | |
289 | * The signature of the key to use must be the description of a key | |
290 | * already in the keyring. Mounting will fail if the key can not be | |
291 | * found. | |
292 | * | |
293 | * Returns zero on success; non-zero on error | |
294 | */ | |
2ccde7c6 | 295 | static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options) |
237fead6 MH |
296 | { |
297 | char *p; | |
298 | int rc = 0; | |
299 | int sig_set = 0; | |
300 | int cipher_name_set = 0; | |
87c94c4d | 301 | int fn_cipher_name_set = 0; |
237fead6 MH |
302 | int cipher_key_bytes; |
303 | int cipher_key_bytes_set = 0; | |
87c94c4d MH |
304 | int fn_cipher_key_bytes; |
305 | int fn_cipher_key_bytes_set = 0; | |
237fead6 | 306 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
2ccde7c6 | 307 | &sbi->mount_crypt_stat; |
237fead6 MH |
308 | substring_t args[MAX_OPT_ARGS]; |
309 | int token; | |
310 | char *sig_src; | |
237fead6 MH |
311 | char *cipher_name_dst; |
312 | char *cipher_name_src; | |
87c94c4d MH |
313 | char *fn_cipher_name_dst; |
314 | char *fn_cipher_name_src; | |
315 | char *fnek_dst; | |
316 | char *fnek_src; | |
237fead6 | 317 | char *cipher_key_bytes_src; |
87c94c4d | 318 | char *fn_cipher_key_bytes_src; |
237fead6 MH |
319 | |
320 | if (!options) { | |
321 | rc = -EINVAL; | |
322 | goto out; | |
323 | } | |
956159c3 | 324 | ecryptfs_init_mount_crypt_stat(mount_crypt_stat); |
237fead6 MH |
325 | while ((p = strsep(&options, ",")) != NULL) { |
326 | if (!*p) | |
327 | continue; | |
328 | token = match_token(p, tokens, args); | |
329 | switch (token) { | |
330 | case ecryptfs_opt_sig: | |
331 | case ecryptfs_opt_ecryptfs_sig: | |
332 | sig_src = args[0].from; | |
f4aad16a | 333 | rc = ecryptfs_add_global_auth_tok(mount_crypt_stat, |
84814d64 | 334 | sig_src, 0); |
f4aad16a MH |
335 | if (rc) { |
336 | printk(KERN_ERR "Error attempting to register " | |
337 | "global sig; rc = [%d]\n", rc); | |
338 | goto out; | |
339 | } | |
237fead6 MH |
340 | sig_set = 1; |
341 | break; | |
237fead6 MH |
342 | case ecryptfs_opt_cipher: |
343 | case ecryptfs_opt_ecryptfs_cipher: | |
344 | cipher_name_src = args[0].from; | |
345 | cipher_name_dst = | |
346 | mount_crypt_stat-> | |
347 | global_default_cipher_name; | |
348 | strncpy(cipher_name_dst, cipher_name_src, | |
349 | ECRYPTFS_MAX_CIPHER_NAME_SIZE); | |
87c94c4d | 350 | cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0'; |
237fead6 MH |
351 | cipher_name_set = 1; |
352 | break; | |
353 | case ecryptfs_opt_ecryptfs_key_bytes: | |
354 | cipher_key_bytes_src = args[0].from; | |
355 | cipher_key_bytes = | |
356 | (int)simple_strtol(cipher_key_bytes_src, | |
357 | &cipher_key_bytes_src, 0); | |
358 | mount_crypt_stat->global_default_cipher_key_size = | |
359 | cipher_key_bytes; | |
237fead6 MH |
360 | cipher_key_bytes_set = 1; |
361 | break; | |
362 | case ecryptfs_opt_passthrough: | |
363 | mount_crypt_stat->flags |= | |
364 | ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED; | |
365 | break; | |
17398957 MH |
366 | case ecryptfs_opt_xattr_metadata: |
367 | mount_crypt_stat->flags |= | |
368 | ECRYPTFS_XATTR_METADATA_ENABLED; | |
369 | break; | |
370 | case ecryptfs_opt_encrypted_view: | |
371 | mount_crypt_stat->flags |= | |
372 | ECRYPTFS_XATTR_METADATA_ENABLED; | |
373 | mount_crypt_stat->flags |= | |
374 | ECRYPTFS_ENCRYPTED_VIEW_ENABLED; | |
375 | break; | |
87c94c4d MH |
376 | case ecryptfs_opt_fnek_sig: |
377 | fnek_src = args[0].from; | |
378 | fnek_dst = | |
379 | mount_crypt_stat->global_default_fnek_sig; | |
380 | strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX); | |
381 | mount_crypt_stat->global_default_fnek_sig[ | |
382 | ECRYPTFS_SIG_SIZE_HEX] = '\0'; | |
383 | rc = ecryptfs_add_global_auth_tok( | |
384 | mount_crypt_stat, | |
84814d64 TH |
385 | mount_crypt_stat->global_default_fnek_sig, |
386 | ECRYPTFS_AUTH_TOK_FNEK); | |
87c94c4d MH |
387 | if (rc) { |
388 | printk(KERN_ERR "Error attempting to register " | |
389 | "global fnek sig [%s]; rc = [%d]\n", | |
390 | mount_crypt_stat->global_default_fnek_sig, | |
391 | rc); | |
392 | goto out; | |
393 | } | |
394 | mount_crypt_stat->flags |= | |
395 | (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES | |
396 | | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK); | |
397 | break; | |
398 | case ecryptfs_opt_fn_cipher: | |
399 | fn_cipher_name_src = args[0].from; | |
400 | fn_cipher_name_dst = | |
401 | mount_crypt_stat->global_default_fn_cipher_name; | |
402 | strncpy(fn_cipher_name_dst, fn_cipher_name_src, | |
403 | ECRYPTFS_MAX_CIPHER_NAME_SIZE); | |
404 | mount_crypt_stat->global_default_fn_cipher_name[ | |
405 | ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0'; | |
406 | fn_cipher_name_set = 1; | |
407 | break; | |
408 | case ecryptfs_opt_fn_cipher_key_bytes: | |
409 | fn_cipher_key_bytes_src = args[0].from; | |
410 | fn_cipher_key_bytes = | |
411 | (int)simple_strtol(fn_cipher_key_bytes_src, | |
412 | &fn_cipher_key_bytes_src, 0); | |
413 | mount_crypt_stat->global_default_fn_cipher_key_bytes = | |
414 | fn_cipher_key_bytes; | |
415 | fn_cipher_key_bytes_set = 1; | |
416 | break; | |
e77cc8d2 TH |
417 | case ecryptfs_opt_unlink_sigs: |
418 | mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS; | |
419 | break; | |
f16feb51 RS |
420 | case ecryptfs_opt_mount_auth_tok_only: |
421 | mount_crypt_stat->flags |= | |
422 | ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY; | |
423 | break; | |
237fead6 MH |
424 | case ecryptfs_opt_err: |
425 | default: | |
87c94c4d MH |
426 | printk(KERN_WARNING |
427 | "%s: eCryptfs: unrecognized option [%s]\n", | |
428 | __func__, p); | |
237fead6 MH |
429 | } |
430 | } | |
237fead6 MH |
431 | if (!sig_set) { |
432 | rc = -EINVAL; | |
956159c3 MH |
433 | ecryptfs_printk(KERN_ERR, "You must supply at least one valid " |
434 | "auth tok signature as a mount " | |
237fead6 MH |
435 | "parameter; see the eCryptfs README\n"); |
436 | goto out; | |
437 | } | |
438 | if (!cipher_name_set) { | |
8f236809 MS |
439 | int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER); |
440 | ||
441 | BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE); | |
8f236809 MS |
442 | strcpy(mount_crypt_stat->global_default_cipher_name, |
443 | ECRYPTFS_DEFAULT_CIPHER); | |
237fead6 | 444 | } |
87c94c4d MH |
445 | if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) |
446 | && !fn_cipher_name_set) | |
447 | strcpy(mount_crypt_stat->global_default_fn_cipher_name, | |
448 | mount_crypt_stat->global_default_cipher_name); | |
449 | if (!cipher_key_bytes_set) | |
e5d9cbde | 450 | mount_crypt_stat->global_default_cipher_key_size = 0; |
87c94c4d MH |
451 | if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) |
452 | && !fn_cipher_key_bytes_set) | |
453 | mount_crypt_stat->global_default_fn_cipher_key_bytes = | |
454 | mount_crypt_stat->global_default_cipher_key_size; | |
af440f52 ES |
455 | mutex_lock(&key_tfm_list_mutex); |
456 | if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name, | |
87c94c4d | 457 | NULL)) { |
af440f52 ES |
458 | rc = ecryptfs_add_new_key_tfm( |
459 | NULL, mount_crypt_stat->global_default_cipher_name, | |
460 | mount_crypt_stat->global_default_cipher_key_size); | |
87c94c4d MH |
461 | if (rc) { |
462 | printk(KERN_ERR "Error attempting to initialize " | |
463 | "cipher with name = [%s] and key size = [%td]; " | |
464 | "rc = [%d]\n", | |
465 | mount_crypt_stat->global_default_cipher_name, | |
466 | mount_crypt_stat->global_default_cipher_key_size, | |
467 | rc); | |
468 | rc = -EINVAL; | |
469 | mutex_unlock(&key_tfm_list_mutex); | |
470 | goto out; | |
471 | } | |
472 | } | |
473 | if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) | |
474 | && !ecryptfs_tfm_exists( | |
475 | mount_crypt_stat->global_default_fn_cipher_name, NULL)) { | |
476 | rc = ecryptfs_add_new_key_tfm( | |
477 | NULL, mount_crypt_stat->global_default_fn_cipher_name, | |
478 | mount_crypt_stat->global_default_fn_cipher_key_bytes); | |
479 | if (rc) { | |
480 | printk(KERN_ERR "Error attempting to initialize " | |
481 | "cipher with name = [%s] and key size = [%td]; " | |
482 | "rc = [%d]\n", | |
483 | mount_crypt_stat->global_default_fn_cipher_name, | |
484 | mount_crypt_stat->global_default_fn_cipher_key_bytes, | |
485 | rc); | |
486 | rc = -EINVAL; | |
487 | mutex_unlock(&key_tfm_list_mutex); | |
488 | goto out; | |
489 | } | |
237fead6 | 490 | } |
87c94c4d | 491 | mutex_unlock(&key_tfm_list_mutex); |
5dda6992 | 492 | rc = ecryptfs_init_global_auth_toks(mount_crypt_stat); |
87c94c4d | 493 | if (rc) |
f4aad16a MH |
494 | printk(KERN_WARNING "One or more global auth toks could not " |
495 | "properly register; rc = [%d]\n", rc); | |
237fead6 MH |
496 | out: |
497 | return rc; | |
498 | } | |
499 | ||
500 | struct kmem_cache *ecryptfs_sb_info_cache; | |
4403158b | 501 | static struct file_system_type ecryptfs_fs_type; |
237fead6 | 502 | |
237fead6 MH |
503 | /** |
504 | * ecryptfs_get_sb | |
505 | * @fs_type | |
506 | * @flags | |
507 | * @dev_name: The path to mount over | |
508 | * @raw_data: The options passed into the kernel | |
237fead6 | 509 | */ |
4d143beb AV |
510 | static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags, |
511 | const char *dev_name, void *raw_data) | |
237fead6 | 512 | { |
2ccde7c6 AV |
513 | struct super_block *s; |
514 | struct ecryptfs_sb_info *sbi; | |
515 | struct ecryptfs_dentry_info *root_info; | |
516 | const char *err = "Getting sb failed"; | |
66cb7666 AV |
517 | struct inode *inode; |
518 | struct path path; | |
237fead6 | 519 | int rc; |
237fead6 | 520 | |
2ccde7c6 AV |
521 | sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL); |
522 | if (!sbi) { | |
523 | rc = -ENOMEM; | |
237fead6 MH |
524 | goto out; |
525 | } | |
2ccde7c6 AV |
526 | |
527 | rc = ecryptfs_parse_options(sbi, raw_data); | |
237fead6 | 528 | if (rc) { |
2ccde7c6 AV |
529 | err = "Error parsing options"; |
530 | goto out; | |
237fead6 | 531 | } |
2ccde7c6 AV |
532 | |
533 | s = sget(fs_type, NULL, set_anon_super, NULL); | |
534 | if (IS_ERR(s)) { | |
535 | rc = PTR_ERR(s); | |
536 | goto out; | |
537 | } | |
538 | ||
539 | s->s_flags = flags; | |
540 | rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY); | |
66cb7666 AV |
541 | if (rc) |
542 | goto out1; | |
2ccde7c6 AV |
543 | |
544 | ecryptfs_set_superblock_private(s, sbi); | |
545 | s->s_bdi = &sbi->bdi; | |
546 | ||
547 | /* ->kill_sb() will take care of sbi after that point */ | |
548 | sbi = NULL; | |
549 | s->s_op = &ecryptfs_sops; | |
66cb7666 | 550 | s->s_d_op = &ecryptfs_dops; |
2ccde7c6 | 551 | |
66cb7666 AV |
552 | err = "Reading sb failed"; |
553 | rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path); | |
554 | if (rc) { | |
555 | ecryptfs_printk(KERN_WARNING, "kern_path() failed\n"); | |
556 | goto out1; | |
557 | } | |
558 | if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) { | |
559 | rc = -EINVAL; | |
560 | printk(KERN_ERR "Mount on filesystem of type " | |
561 | "eCryptfs explicitly disallowed due to " | |
562 | "known incompatibilities\n"); | |
563 | goto out_free; | |
564 | } | |
565 | ecryptfs_set_superblock_lower(s, path.dentry->d_sb); | |
566 | s->s_maxbytes = path.dentry->d_sb->s_maxbytes; | |
567 | s->s_blocksize = path.dentry->d_sb->s_blocksize; | |
070baa51 | 568 | s->s_magic = ECRYPTFS_SUPER_MAGIC; |
66cb7666 AV |
569 | |
570 | inode = ecryptfs_get_inode(path.dentry->d_inode, s); | |
571 | rc = PTR_ERR(inode); | |
572 | if (IS_ERR(inode)) | |
573 | goto out_free; | |
574 | ||
575 | s->s_root = d_alloc_root(inode); | |
2ccde7c6 | 576 | if (!s->s_root) { |
66cb7666 AV |
577 | iput(inode); |
578 | rc = -ENOMEM; | |
579 | goto out_free; | |
2ccde7c6 | 580 | } |
2ccde7c6 | 581 | |
66cb7666 | 582 | rc = -ENOMEM; |
2ccde7c6 | 583 | root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL); |
66cb7666 AV |
584 | if (!root_info) |
585 | goto out_free; | |
586 | ||
2ccde7c6 AV |
587 | /* ->kill_sb() will take care of root_info */ |
588 | ecryptfs_set_dentry_private(s->s_root, root_info); | |
66cb7666 AV |
589 | ecryptfs_set_dentry_lower(s->s_root, path.dentry); |
590 | ecryptfs_set_dentry_lower_mnt(s->s_root, path.mnt); | |
591 | ||
2ccde7c6 | 592 | s->s_flags |= MS_ACTIVE; |
4d143beb | 593 | return dget(s->s_root); |
2ccde7c6 | 594 | |
66cb7666 AV |
595 | out_free: |
596 | path_put(&path); | |
597 | out1: | |
598 | deactivate_locked_super(s); | |
237fead6 | 599 | out: |
2ccde7c6 AV |
600 | if (sbi) { |
601 | ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat); | |
602 | kmem_cache_free(ecryptfs_sb_info_cache, sbi); | |
603 | } | |
604 | printk(KERN_ERR "%s; rc = [%d]\n", err, rc); | |
4d143beb | 605 | return ERR_PTR(rc); |
237fead6 MH |
606 | } |
607 | ||
608 | /** | |
609 | * ecryptfs_kill_block_super | |
610 | * @sb: The ecryptfs super block | |
611 | * | |
612 | * Used to bring the superblock down and free the private data. | |
237fead6 MH |
613 | */ |
614 | static void ecryptfs_kill_block_super(struct super_block *sb) | |
615 | { | |
decabd66 AV |
616 | struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb); |
617 | kill_anon_super(sb); | |
618 | if (!sb_info) | |
619 | return; | |
620 | ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat); | |
621 | bdi_destroy(&sb_info->bdi); | |
622 | kmem_cache_free(ecryptfs_sb_info_cache, sb_info); | |
237fead6 MH |
623 | } |
624 | ||
625 | static struct file_system_type ecryptfs_fs_type = { | |
626 | .owner = THIS_MODULE, | |
627 | .name = "ecryptfs", | |
4d143beb | 628 | .mount = ecryptfs_mount, |
237fead6 MH |
629 | .kill_sb = ecryptfs_kill_block_super, |
630 | .fs_flags = 0 | |
631 | }; | |
632 | ||
633 | /** | |
634 | * inode_info_init_once | |
635 | * | |
636 | * Initializes the ecryptfs_inode_info_cache when it is created | |
637 | */ | |
638 | static void | |
51cc5068 | 639 | inode_info_init_once(void *vptr) |
237fead6 MH |
640 | { |
641 | struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr; | |
642 | ||
a35afb83 | 643 | inode_init_once(&ei->vfs_inode); |
237fead6 MH |
644 | } |
645 | ||
646 | static struct ecryptfs_cache_info { | |
e18b890b | 647 | struct kmem_cache **cache; |
237fead6 MH |
648 | const char *name; |
649 | size_t size; | |
51cc5068 | 650 | void (*ctor)(void *obj); |
237fead6 MH |
651 | } ecryptfs_cache_infos[] = { |
652 | { | |
653 | .cache = &ecryptfs_auth_tok_list_item_cache, | |
654 | .name = "ecryptfs_auth_tok_list_item", | |
655 | .size = sizeof(struct ecryptfs_auth_tok_list_item), | |
656 | }, | |
657 | { | |
658 | .cache = &ecryptfs_file_info_cache, | |
659 | .name = "ecryptfs_file_cache", | |
660 | .size = sizeof(struct ecryptfs_file_info), | |
661 | }, | |
662 | { | |
663 | .cache = &ecryptfs_dentry_info_cache, | |
664 | .name = "ecryptfs_dentry_info_cache", | |
665 | .size = sizeof(struct ecryptfs_dentry_info), | |
666 | }, | |
667 | { | |
668 | .cache = &ecryptfs_inode_info_cache, | |
669 | .name = "ecryptfs_inode_cache", | |
670 | .size = sizeof(struct ecryptfs_inode_info), | |
671 | .ctor = inode_info_init_once, | |
672 | }, | |
673 | { | |
674 | .cache = &ecryptfs_sb_info_cache, | |
675 | .name = "ecryptfs_sb_cache", | |
676 | .size = sizeof(struct ecryptfs_sb_info), | |
677 | }, | |
237fead6 MH |
678 | { |
679 | .cache = &ecryptfs_header_cache_1, | |
680 | .name = "ecryptfs_headers_1", | |
681 | .size = PAGE_CACHE_SIZE, | |
682 | }, | |
683 | { | |
684 | .cache = &ecryptfs_header_cache_2, | |
685 | .name = "ecryptfs_headers_2", | |
686 | .size = PAGE_CACHE_SIZE, | |
687 | }, | |
dd2a3b7a MH |
688 | { |
689 | .cache = &ecryptfs_xattr_cache, | |
690 | .name = "ecryptfs_xattr_cache", | |
691 | .size = PAGE_CACHE_SIZE, | |
692 | }, | |
eb95e7ff MH |
693 | { |
694 | .cache = &ecryptfs_key_record_cache, | |
695 | .name = "ecryptfs_key_record_cache", | |
696 | .size = sizeof(struct ecryptfs_key_record), | |
697 | }, | |
956159c3 MH |
698 | { |
699 | .cache = &ecryptfs_key_sig_cache, | |
700 | .name = "ecryptfs_key_sig_cache", | |
701 | .size = sizeof(struct ecryptfs_key_sig), | |
702 | }, | |
703 | { | |
704 | .cache = &ecryptfs_global_auth_tok_cache, | |
705 | .name = "ecryptfs_global_auth_tok_cache", | |
706 | .size = sizeof(struct ecryptfs_global_auth_tok), | |
707 | }, | |
708 | { | |
709 | .cache = &ecryptfs_key_tfm_cache, | |
710 | .name = "ecryptfs_key_tfm_cache", | |
711 | .size = sizeof(struct ecryptfs_key_tfm), | |
712 | }, | |
746f1e55 MH |
713 | { |
714 | .cache = &ecryptfs_open_req_cache, | |
715 | .name = "ecryptfs_open_req_cache", | |
716 | .size = sizeof(struct ecryptfs_open_req), | |
717 | }, | |
237fead6 MH |
718 | }; |
719 | ||
720 | static void ecryptfs_free_kmem_caches(void) | |
721 | { | |
722 | int i; | |
723 | ||
724 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { | |
725 | struct ecryptfs_cache_info *info; | |
726 | ||
727 | info = &ecryptfs_cache_infos[i]; | |
728 | if (*(info->cache)) | |
729 | kmem_cache_destroy(*(info->cache)); | |
730 | } | |
731 | } | |
732 | ||
733 | /** | |
734 | * ecryptfs_init_kmem_caches | |
735 | * | |
736 | * Returns zero on success; non-zero otherwise | |
737 | */ | |
738 | static int ecryptfs_init_kmem_caches(void) | |
739 | { | |
740 | int i; | |
741 | ||
742 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { | |
743 | struct ecryptfs_cache_info *info; | |
744 | ||
745 | info = &ecryptfs_cache_infos[i]; | |
746 | *(info->cache) = kmem_cache_create(info->name, info->size, | |
20c2df83 | 747 | 0, SLAB_HWCACHE_ALIGN, info->ctor); |
237fead6 MH |
748 | if (!*(info->cache)) { |
749 | ecryptfs_free_kmem_caches(); | |
750 | ecryptfs_printk(KERN_WARNING, "%s: " | |
751 | "kmem_cache_create failed\n", | |
752 | info->name); | |
753 | return -ENOMEM; | |
754 | } | |
755 | } | |
756 | return 0; | |
757 | } | |
758 | ||
6e90aa97 | 759 | static struct kobject *ecryptfs_kobj; |
237fead6 | 760 | |
386f275f KS |
761 | static ssize_t version_show(struct kobject *kobj, |
762 | struct kobj_attribute *attr, char *buff) | |
237fead6 MH |
763 | { |
764 | return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK); | |
765 | } | |
766 | ||
386f275f | 767 | static struct kobj_attribute version_attr = __ATTR_RO(version); |
237fead6 | 768 | |
30a468b1 GKH |
769 | static struct attribute *attributes[] = { |
770 | &version_attr.attr, | |
30a468b1 GKH |
771 | NULL, |
772 | }; | |
773 | ||
774 | static struct attribute_group attr_group = { | |
775 | .attrs = attributes, | |
776 | }; | |
237fead6 MH |
777 | |
778 | static int do_sysfs_registration(void) | |
779 | { | |
780 | int rc; | |
781 | ||
6e90aa97 GKH |
782 | ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj); |
783 | if (!ecryptfs_kobj) { | |
917e865d GKH |
784 | printk(KERN_ERR "Unable to create ecryptfs kset\n"); |
785 | rc = -ENOMEM; | |
237fead6 MH |
786 | goto out; |
787 | } | |
6e90aa97 | 788 | rc = sysfs_create_group(ecryptfs_kobj, &attr_group); |
237fead6 MH |
789 | if (rc) { |
790 | printk(KERN_ERR | |
30a468b1 | 791 | "Unable to create ecryptfs version attributes\n"); |
197b12d6 | 792 | kobject_put(ecryptfs_kobj); |
237fead6 MH |
793 | } |
794 | out: | |
795 | return rc; | |
796 | } | |
797 | ||
a75de1b3 RK |
798 | static void do_sysfs_unregistration(void) |
799 | { | |
6e90aa97 | 800 | sysfs_remove_group(ecryptfs_kobj, &attr_group); |
197b12d6 | 801 | kobject_put(ecryptfs_kobj); |
a75de1b3 RK |
802 | } |
803 | ||
237fead6 MH |
804 | static int __init ecryptfs_init(void) |
805 | { | |
806 | int rc; | |
807 | ||
808 | if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) { | |
809 | rc = -EINVAL; | |
810 | ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is " | |
811 | "larger than the host's page size, and so " | |
812 | "eCryptfs cannot run on this system. The " | |
888d57bb JP |
813 | "default eCryptfs extent size is [%u] bytes; " |
814 | "the page size is [%lu] bytes.\n", | |
815 | ECRYPTFS_DEFAULT_EXTENT_SIZE, | |
816 | (unsigned long)PAGE_CACHE_SIZE); | |
237fead6 MH |
817 | goto out; |
818 | } | |
819 | rc = ecryptfs_init_kmem_caches(); | |
820 | if (rc) { | |
821 | printk(KERN_ERR | |
822 | "Failed to allocate one or more kmem_cache objects\n"); | |
823 | goto out; | |
824 | } | |
825 | rc = register_filesystem(&ecryptfs_fs_type); | |
826 | if (rc) { | |
827 | printk(KERN_ERR "Failed to register filesystem\n"); | |
cf81f89d | 828 | goto out_free_kmem_caches; |
237fead6 | 829 | } |
237fead6 MH |
830 | rc = do_sysfs_registration(); |
831 | if (rc) { | |
832 | printk(KERN_ERR "sysfs registration failed\n"); | |
cf81f89d | 833 | goto out_unregister_filesystem; |
237fead6 | 834 | } |
746f1e55 MH |
835 | rc = ecryptfs_init_kthread(); |
836 | if (rc) { | |
837 | printk(KERN_ERR "%s: kthread initialization failed; " | |
838 | "rc = [%d]\n", __func__, rc); | |
839 | goto out_do_sysfs_unregistration; | |
840 | } | |
624ae528 | 841 | rc = ecryptfs_init_messaging(); |
dddfa461 | 842 | if (rc) { |
746f1e55 | 843 | printk(KERN_ERR "Failure occured while attempting to " |
624ae528 TH |
844 | "initialize the communications channel to " |
845 | "ecryptfsd\n"); | |
746f1e55 | 846 | goto out_destroy_kthread; |
956159c3 MH |
847 | } |
848 | rc = ecryptfs_init_crypto(); | |
849 | if (rc) { | |
850 | printk(KERN_ERR "Failure whilst attempting to init crypto; " | |
851 | "rc = [%d]\n", rc); | |
cf81f89d | 852 | goto out_release_messaging; |
dddfa461 | 853 | } |
2830bfd6 ES |
854 | if (ecryptfs_verbosity > 0) |
855 | printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values " | |
856 | "will be written to the syslog!\n", ecryptfs_verbosity); | |
857 | ||
cf81f89d MH |
858 | goto out; |
859 | out_release_messaging: | |
624ae528 | 860 | ecryptfs_release_messaging(); |
746f1e55 MH |
861 | out_destroy_kthread: |
862 | ecryptfs_destroy_kthread(); | |
cf81f89d MH |
863 | out_do_sysfs_unregistration: |
864 | do_sysfs_unregistration(); | |
865 | out_unregister_filesystem: | |
866 | unregister_filesystem(&ecryptfs_fs_type); | |
867 | out_free_kmem_caches: | |
868 | ecryptfs_free_kmem_caches(); | |
237fead6 MH |
869 | out: |
870 | return rc; | |
871 | } | |
872 | ||
873 | static void __exit ecryptfs_exit(void) | |
874 | { | |
cf81f89d MH |
875 | int rc; |
876 | ||
877 | rc = ecryptfs_destroy_crypto(); | |
878 | if (rc) | |
879 | printk(KERN_ERR "Failure whilst attempting to destroy crypto; " | |
880 | "rc = [%d]\n", rc); | |
624ae528 | 881 | ecryptfs_release_messaging(); |
746f1e55 | 882 | ecryptfs_destroy_kthread(); |
cf81f89d | 883 | do_sysfs_unregistration(); |
237fead6 MH |
884 | unregister_filesystem(&ecryptfs_fs_type); |
885 | ecryptfs_free_kmem_caches(); | |
886 | } | |
887 | ||
888 | MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>"); | |
889 | MODULE_DESCRIPTION("eCryptfs"); | |
890 | ||
891 | MODULE_LICENSE("GPL"); | |
892 | ||
893 | module_init(ecryptfs_init) | |
894 | module_exit(ecryptfs_exit) |