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