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